Monday, March 28, 2011

wordpress plugin for assigning value to javascript variable

<?php
/*
Plugin Name: Url To Text
Plugin URI: http://www.microxsolutions.com/
Description:Plugin to replace key from url in frontend
Version: 1.0.0
Author: Afroz
Author URI: http://www.microxsolutions.com/
*/

/* Runs when plugin is activated */
register_activation_hook(__FILE__,'install_url_text');

/* Runs on plugin deactivation*/
register_deactivation_hook( __FILE__, 'url_text_deactivate' );
function itg_admin_css_all_page() {
    /**
     * <span class="IL_AD" id="IL_AD4">Register</span> the style handle
     */
   wp_register_style($handle = 'itg-admin-css-all', $src = plugins_url('css/url_text.css', __FILE__), $deps = array(), $ver = '1.0.0', $media = 'all');

    /**
     * Now enqueue it
     */
    wp_enqueue_style('itg-admin-css-all');
}

/**
 * Finally hook the itg_admin_css_all_page to admin_print_styles
 * As it is done during the init of the admin page, so the enqueue gets executed.
 * This can also be <span class="IL_AD" id="IL_AD8">attached</span> to the admin_init, or admin_menu hook
 */
add_action('admin_print_styles', 'itg_admin_css_all_page');
add_action('admin_menu','my_plugin_menu');
function my_plugin_menu(){
   add_options_page('My Plugin Options','Manage Url to Text','1','url-text','showRecords');
   add_options_page('My Plugin Options','','1','url-text','add_records');
}
function install_url_text()
{
    global $wpdb;
    $table = $wpdb->prefix."url_text";
    $structure = "CREATE TABLE $table(
                                id INT(11) NOT NULL auto_increment,
                                key_text VARCHAR(222) NOT NULL,
                                url VARCHAR(222) NOT NULL,
                                UNIQUE KEY id (id)
                                );";  
    $wpdb->query($structure);
}
function url_text_deactivate() {
        global $wpdb;
        $table = $wpdb->prefix."url_text";    
        $wpdb->query("DROP TABLE IF EXISTS $table");
}  
$action = $_REQUEST['action'];
switch($action)
{
  case "addemp":
       $key_text = $_REQUEST['key_text'];
       $url = $_REQUEST['url'];
       if(!isset($_GET['id']) || $_GET['id']=='')
       {
           if($key_text!='' && $url!='')
           {     
               addRecords($key_text,$url);
            }
            else
            {
                header("location:".$_SERVER['PHP_SELF']."?page=url-text&er=2");
            }
        }
  break;
  case "updaterec":
       $key_text = $_REQUEST['key_text'];
       $url = $_REQUEST['url'];
       $urlid = $_GET['id'];
       updateRecords($key_text,$url,$urlid);
  break;
  case "deleterec":
       $key_text = $_REQUEST['key_text'];
       $url = $_REQUEST['url'];
       $urlid = $_GET['id'];
       deleteRecords($urlid);
  break;
  default:               
}
function showRecords()
{  
         global $wpdb;
          $table = $wpdb->prefix."url_text";      
          $myrows = $wpdb->get_results( "SELECT *  FROM $table",ARRAY_A );
        if($wpdb->num_rows>0)
        {
        ?>
        <table width="70%">
        <tr><td colspan="4" class="heading1">List of Url and Key</td></tr>
        <tr><td colspan="4"><hr /></td></tr>
        <tr>
        <td width="19%" class="heading">S.No</td>
        <td width="21%" class="heading">Key</td>
        <td width="36%" class="heading">Url</td>
        <td width="24%" class="heading">Action</td>
        </tr>
        <tr><td colspan="4"><hr /></td></tr><?php
        $i=1;
        foreach($myrows as $key =>$value)
        {
         ?><tr>
        <td><?php _e($i);$i++;?></td>
        <td><?php _e($value['key_text']);?></td>
        <td><?php _e($value['url']);?></td>
        <td><a href="<?php _e($_SERVER['PHP_SELF']);?>?page=url-text&action=addemp&id=<?php _e($value['id']);?>">Edit</a> |
            <a href="<?php _e($_SERVER['PHP_SELF']);?>?page=url-text&action=deleterec&id=<?php _e($value['id']);?>">Delete</a></td></tr><?php
        }
         ?><tr><td colspan="4"><hr /></td></tr>      
        </table><?php
        }
        else{_e('<table width="70%">
        <tr><td colspan="4" class="heading1" id="errorfield1">No record to Show.</td></tr></table>');}
}
function addRecords($key_text,$url)
{
  global $wpdb;
  $counts=0;
  $table = $wpdb->prefix."url_text";
  $myrows = $wpdb->get_results( "SELECT *  FROM $table",ARRAY_A );
  foreach($myrows as $value)
      {
         if($value['key_text'] == $key_text)
         {$counts++;}
      }
  if($counts==0)
      {
      $results = $wpdb->insert($table,array('key_text' => $key_text, 'url' => $url));
      header("location:".$_SERVER['PHP_SELF']."?page=url-text");
      }
    else
    {
    header("location:".$_SERVER['PHP_SELF']."?page=url-text&er=1");
    }
}
function updateRecords($key_text,$url,$urlid)
{
    
        if($key_text != '' && $url != '')
        {
            global $wpdb;
            $counts=0;
            $table = $wpdb->prefix."url_text";
            $row = $wpdb->get_results( "SELECT * FROM $table where id!=$urlid ",ARRAY_A );
            foreach($row as $value)
            {
                 if($value['key_text'] == $key_text)
                 {$counts++;}
            }
            if($counts==0)
            {    
                $results = $wpdb->update($table,array('key_text' => $key_text, 'url' => $url), array( 'id' => $urlid ), array( '%s', '%s' ), array( '%d' ));
                header("location:".$_SERVER['PHP_SELF']."?page=url-text");
            }
            else
            {
                header("location:".$_SERVER['PHP_SELF']."?page=url-text&action=addemp&id=".$urlid."&er=1");
            }
        }
        else
        {
            header("location:".$_SERVER['PHP_SELF']."?page=url-text&action=addemp&id=".$urlid."&er=2");          
        }
 
}
function deleteRecords($id)
{
  global $wpdb;
  $table = $wpdb->prefix."url_text";
  $wpdb->query("DELETE FROM $table WHERE id = $id");
  header("location:".$_SERVER['PHP_SELF']."?page=url-text");
}
//add_action('pre_get_posts','replacekey_text_url');
//add_action('wp_print_scripts','replacekey_text_url');
add_action('wp_head','replacekey_text_url');
function replacekey_text_url()
{
//selecting all key urls from db start
global $wpdb;
$text = '';
$table = $wpdb->prefix."url_text";
$myrows = $wpdb->get_results( "SELECT *  FROM $table",ARRAY_N );
//selecting all key urls from db end
$text.= '<script type="text/javascript">';
$text.= "var TextKey = new Array();\n";  
    foreach($myrows as $value)
    {
        $text.= "TextKey[\"$value[1]\"]";
        $text.= " = \"$value[2]\";\n";          
    }
$text.= '</script>';
_e($text);
}
function add_records(){
if(isset($_GET['id']) && $_GET['id']!='')
{
$id=$_GET['id'];
global $wpdb;
$table = $wpdb->prefix."url_text";
$row = $wpdb->get_row( "SELECT * FROM $table where id=$id",ARRAY_A );
}
?><div class="wrap">
  <h3 class="heading1"><?php if(isset($_GET['id']) && $_GET['id'] !=''){_e('Update URL and Key Values');}else{_e('Add URL and Key Values');}?></h3>
    <form action="" method="post">
         <table width="60%" class="form-table" border="0">
            <tr><td></td><td id="errorfield"><?php if(isset($_GET['er']) && $_GET['er']==1){_e("Key already Exist!");}
            elseif(isset($_GET['er']) && $_GET['er']==2){_e("Key/Url is empty!");}?></td></tr>
            <tr>
              <td class="heading"><label for="key_text"><?php _e('Key')?></label></td>
              <td><input type="text" name="key_text" value="<?php if(isset($_GET['id']) && $_GET['id']!=''){_e($row['key_text']);}?>"></td>
            </tr>
            <tr>
              <td class="heading"><label for="url"><?php _e('URL')?></label></td>
              <td><input type="text" name="url" value="<?php if(isset($_GET['id']) && $_GET['id']!=''){_e($row['url']);}?>"></td>
            </tr>
            <tr>
              <td><input type="hidden" name="action" value="<?php if(isset($_GET['id']) && $_GET['id']!=''){_e('updaterec');}else{_e('addemp');}?>" /></td>
              <td><input type="submit" name="submit"  value="<?php if(isset($_GET['id']) && $_GET['id']!=''){_e('Update');}else{_e('Insert');} ?>"/></td>
            </tr>        
         </table>  
    </form>
</div><?php
}
?>

No comments:

Post a Comment