Tuesday, September 15, 2020

Javascript Helpers for urgent reference

Javascript date and time, Indian date format, todays date

date format d-m-Y H:i:s


​var date = new Date();

var d = date.getDate();

var m = date.getMonth() + 1;

var y = date.getFullYear();

var H= date.getHours();

var i= date.getMinutes();

var s= date.getSeconds();

var mydate=d+'-'+m+'-'+y+' '+H+':'+i+':'+s;




add background image with javascript


<div id="tab1" style="position:relative; background-image:url(buttons/off.png);
    <a href="javascript:ChangeBackgroundImageOfTab('tab1', 'on');">
        <img id="DivBtn1" name="DivBtn1" src="buttons/text.png" >
    </a>
</div>
<script type="text/javascript">
function ChangeBackgroungImageOfTab(tabName, imagePrefix)
{
    var urlString = 'url(buttons/' + imagePrefix + '.png)';
    document.getElementById(tabName).style.backgroundImage =  urlString;
}

</script>

How to set a buttons innerHTML as an image?


<button id="button">Notepad</button>

<script type="text/javascript">
$(document).ready(function() {
var btn = document.getElementById("button");

btn.style.backgroundImage = "url('/media/notepad.jpg')";

});
</script>


How to validate an email address in JavaScript

<form>
  <p>Enter an email address:</p>
  <input id='email'>
  <button type='submit' id='validate'>Validate!</button>
</form>
<h2 id='result'></h2>

<script type="text/javascript">
function validateEmail(email) {
const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return re.test(email);
}

function validate() {
  const $result = $("#result");
  const email = $("#email").val();
  $result.text("");

  if (validateEmail(email)) {
    $result.text(email + " is valid :)");
    $result.css("color", "green");
  } else {
    $result.text(email + " is not valid :(");
    $result.css("color", "red");
  }
  return false;
}

$("#validate").on("click", validate);
</script>

How to generate a simple popup contact us using jQuery

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
 <link rel="stylesheet" href="/resources/demos/style.css">
 <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
 <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="messagepop pop">
  <form method="post" id="new_message" action="/messages">
    <p><label for="email">Your email or name</label><input type="text" size="30" name="email" id="email" /></p>
    <p><label for="body">Message</label><textarea rows="6" name="body" id="body" cols="35"></textarea></p>
    <p><input type="submit" value="Send Message" name="commit" id="message_submit"/> or <a class="close" href="/">Cancel</a></p>
  </form>
</div>

<a href="/contact" id="contact">Contact Us</a>

<script type="text/javascript">
$(document).ready(function() {
    $("#contact").dialog();
});
</script>

include http://docs.jquery.com/UI/Dialog

Note:dont need any css if using jquery dialog

<style type="text/css">
a.selected {
  background-color:#1F75CC;  color:white;  z-index:100;
}

.messagepop {
  background-color:#FFFFFF;
  border:1px solid #999999;
  cursor:default;
  display:none;
  margin-top: 15px;
  position:absolute;
  text-align:left;
  width:394px;
  z-index:50;
  padding: 25px 25px 20px;
}

label {
  display: block;
  margin-bottom: 3px;
  padding-left: 15px;
  text-indent: -15px;
}

.messagepop p, .messagepop.div {
  border-bottom: 1px solid #EFEFEF;
  margin: 8px 0;
  padding-bottom: 8px;
}
</style>

How to use check all checkboxes using jQuery?


<script type="text/javascript">

$(document).ready(function() {
$('#checkedAll').click(function () {    
     $('input:checkbox').prop('checked', this.checked);    
 });
 });
</script>
<div class="checkall">
<table><tr><td>
<input type="checkbox" name="checkedAll" id="checkedAll" />&nbsp;Check All</td></tr>

<tr><td><input type="checkbox" name="checkAll" class="checkSingle" />&nbsp;Cat</td></tr>

<tr><td><input type="checkbox" name="checkAll" class="checkSingle" />&nbsp;Dog/td></tr>

<tr><td><input type="checkbox" name="checkAll" class="checkSingle" />&nbsp;Cow</td></tr>
</table></div>

How to select all checkboxes using jQuery?

<form>
  <table>
    <tr>
      <td><input type="checkbox" id="select_all" /></td>
    </tr>
    <tr>
      <td><input type="checkbox" name="select[]" /></td>
    </tr>
    <tr>
      <td><input type="checkbox" name="select[]" /></td>
    </tr>
    <tr>
      <td><input type="checkbox" name="select[]" /></td>
    </tr>
  </table>
</form>
<script type="text/javascript">

$(document).ready(function() {
$('#select_all').change(function() {
  var checkboxes = $(this).closest('form').find(':checkbox');
  checkboxes.prop('checked', $(this).is(':checked'));
});
});
</script>



jquery contact us popup
how to validate email with jquery



Sunday, May 24, 2020

Wordpress Pdf Annotation Plugin | PHP API for annotation.js | API for Insert,Update and Delete annotation

I have search for annotation plugin in Wordpress plugin center but not able to found a good one so i developed a annotation plugin with the help of http://annotatorjs.org/
This plugin will provided you full control over data as it save it in your wordpress database itself.
With the plugin 
Plugin for annotation
Viewer page template for viewing all pages in pdf file with scroll
Manage annotation page for edit the text used for annotation and for deleting the annotation

<?php 
    /*
    Plugin Name: G-annotator
    Plugin URI: http://php-you.blogspot.com
    Description: Plugin for inline annotation in post
    Author:GRS
    Version: 1.0
    Author URI:http://php-you.blogspot.com
    */


  add_action('init','gannotator_init');
  add_action('wp','gannotator_wp');
  function gannotator_init()
  {
  if(is_user_logged_in() )
 
     add_action('parse_request', 'custom_requests');
 
  }
  
  }
   function gannotator_wp()
  {
  if(is_user_logged_in())
  {
    add_action('wp_enqueue_scripts', 'g_annatator_enqueue_script');
 
  
 
  }
  
  }
  
  function g_annatator_enqueue_script() { 
    wp_enqueue_style( 'annatator_css', plugins_url( 'js/annotator.min.css', __FILE__ ) );  
    wp_enqueue_script( 'annatator_script', plugin_dir_url( __FILE__ ) . 'js/annotator-full.min.js', array('jquery'), '', true );
    wp_enqueue_script( 'g_annatator_script', plugin_dir_url( __FILE__ ) . 'js/g-annotator.js', array('jquery'), '', true );
}

function custom_requests ( $wp ) { 

global $current_user; 


  $valid_actions = array('api', 'action2');
  
  if(
    !empty($wp->query_vars['myroute']) &&
    in_array($wp->query_vars['myroute'], $valid_actions) 
  ) {
  
     
  /***************Insert and Update annotation************************/ 
  $method = $_SERVER['REQUEST_METHOD'];
if ('PUT' === $method || 'POST' === $method ) {
   $jsondata=file_get_contents('php://input');
    
    $jsondecode=json_decode($jsondata);
    
    if($jsondecode->id=='')
    {
   
               $order   = array("\r\n", "\n", "\r");
                  $replace = '<br/>';
                $jsondecode->text=str_replace($order, $replace,$jsondecode->text);
               
        $my_cptpost_args = array(

         'post_title'    => $jsondecode->quote,

         'post_content'  => $jsondecode->text,

          'post_status'   => 'private',

         'post_type' => 'document_tags'

       );
       
                

      // insert the post into the database

     $cpt_id = wp_insert_post( $my_cptpost_args, $wp_error);

    add_post_meta($cpt_id, 'uri', $jsondecode->uri);
    add_post_meta($cpt_id, 'ranges', json_encode($jsondecode->ranges));
    add_post_meta($cpt_id, 'rawdata', json_encode($jsondecode));
    $jsondecode->id=$cpt_id;

    }
    else
    {
    $my_post = array(
      'ID'           => $jsondecode->id,
      'post_title'   => $jsondecode->quote,
      'post_content' => $jsondecode->text,
  );
                 $order   = array("\r\n", "\n", "\r");
                  $replace = '<br/>';
                $jsondecode->text=str_replace($order, $replace,$jsondecode->text);
 
// Update the post into the database
  wp_update_post( $my_post );
  update_post_meta( $jsondecode->id, 'ranges', json_encode($jsondecode->ranges));  
  update_post_meta( $jsondecode->id, 'rawdata', json_encode($jsondecode));  
    }
    
    
     $order   = array("<br/>");
   $replace = "\n";
   $jsondecode->text=str_replace($order, $replace,$jsondecode->text);
    echo json_encode($jsondecode);
       
    die(0);
}
 /***************Insert and Update annotation************************/ 
  /***************Delete annotation************************/ 
if ('DELETE' === $method)
{
$jsondata=file_get_contents('php://input');
    
 $jsondecode=json_decode($jsondata);
    
    if($jsondecode->id!=='')
    {
      wp_delete_post($jsondecode->id);
       //wp_trash_post($jsondecode->id);
      
    }
}
 
  /***************Delete annotation************************/   
  /************ listing annotation as per uri **********************/ 
  $current_url = $_GET['uri'];
  if($current_url!=='')
  {
    $mydata=array();
    $masterdata=array();
    $posts = get_posts(array(
'post_type' => 'document_tags',
'meta_key' => 'uri',
'meta_value' => $current_url,
 'post_status' => 'private',
 'author'        =>  $current_user->ID,
  ));

  
  foreach($posts as $pdata)
  {
  $order   = array("<br/>");
   $replace = '\n';
    $rawdata=str_replace($order, $replace,get_post_meta($pdata->ID, 'rawdata',false));
    $dat_ass_arr=json_decode($rawdata[0],true);
    $dat_ass_arr['id']=$pdata->ID;
  
   array_push($mydata,$dat_ass_arr);
  
  }
  
  $masterdata['total']=count($mydata);
   $masterdata['rows']= $mydata;
  
  echo json_encode($masterdata);
 
    
  }
 /************ listing annotation as per uri **********************/

exit();
    // do something here

  }//checking gannotation/api && myroute is api

}//close function

add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp_loaded','my_flush_rules' );

// flush_rules() if our rules are not yet included
function my_flush_rules(){
    $rules = get_option( 'rewrite_rules' );

    if ( ! isset( $rules['gannotation/(.*?)/(.+?)'] ) ) {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }
}

// Adding a new rule
function my_insert_rewrite_rules( $rules )
{
    $newrules = array();
    $newrules['gannotation/(.*?)/(.+?)'] = 'index.php?myroute=$matches[1]&myargument=$matches[2]';
       return $newrules + $rules;
}

// Adding the id var so that WP recognizes it
function my_insert_query_vars( $vars )
{
     array_push($vars, 'myroute', 'myargument');
     return $vars;
}
add_action('init', function() {

     add_rewrite_rule(
        'gannotation/(.*?)/(.+?)/?',
        'index.php?myroute=$matches[1]&myargument=$matches[2]',
        'top'
    );

});




?>