Advertisement
Guest User

nextgen-gallery-custom-fields/ngg-custom-fields

a guest
Jan 22nd, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 37.81 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: NextGEN/NextCellent Custom Fields
  4. Plugin URI: http://shauno.co.za/wordpress/nextgen-gallery-custom-fields/
  5. Description: This plugin allows users to add custom fields to images in NextGEN/NextCellent Gallery
  6. Version: 1.2.2
  7. Author: Shaun Alberts
  8. Author URI: http://shauno.co.za
  9. */
  10. /*
  11. 2014.01.22 Changes by biziclop:
  12. - all $wpdb->escape() calls were replaced to esc_sql()
  13. - search for 'biziclop', 'nggcf_ai' for other changes
  14. - 'NextCellent' words added
  15. */
  16. /*
  17. Copyright 2012  Shaun Alberts  (email : shaunalberts@gmail.com)
  18.  
  19. This program is free software; you can redistribute it and/or modify
  20. it under the terms of the GNU General Public License as published by
  21. the Free Software Foundation; either version 2 of the License, or
  22. (at your option) any later version.
  23.  
  24. This program is distributed in the hope that it will be useful,
  25. but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27. GNU General Public License for more details.
  28.  
  29. You should have received a copy of the GNU General Public License
  30. along with this program; if not, write to the Free Software
  31. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  32. */
  33.  
  34. // stop direct call
  35. if(preg_match("#".basename(__FILE__)."#", $_SERVER["PHP_SELF"])) {die("You are not allowed to call this page directly.");}
  36.  
  37. /*
  38. This was pretty much my first from-scratch plugin. The code structure is a little all-over-the-place as a result of adding features without while maintaining legacy features
  39. But it works, so I'm not going to do a whole-scale rewrite when I have better things to do. Like drink beer. Mmmm, delicious beer...
  40.  
  41. Why are you even reading this? Maybe you should consider helping me stock that fridge? http://shauno.co.za/donate/
  42. */
  43.  
  44.  
  45.  
  46.  
  47. //{
  48.  
  49. // by biziclop
  50. // AI( $array_or_object, 1, 'default value' ) <=> $array_or_object[1] or 'default value'/null
  51. function nggcf_ai( $o, $i, $d = null ){
  52.   if( is_array ($o)){ if( array_key_exists( $i, $o ))  return $o[ $i ];   return $d;  }
  53.   if( is_object($o)){ if( property_exists(  $o, $i ))  return $o -> $i;   return $d;  }
  54.   return $d;
  55. }
  56.  
  57.  
  58.   define("NGGCF_IMAGES", 1);
  59.   define("NGGCF_GALLERY", 2);
  60.  
  61.   define("NGGCF_FIELD_TYPE_INPUT", 1);
  62.   define("NGGCF_FIELD_TYPE_TEXTAREA", 2);
  63.   define("NGGCF_FIELD_TYPE_SELECT", 3);
  64.   define("NGGCF_FIELD_TYPE_DATE", 4);
  65.  
  66.   //install funcs{
  67.     register_activation_hook(__FILE__, "nggcf_install");
  68.  
  69.     function nggcf_install($upgrade=false) {
  70.       global $wpdb;
  71.       require_once(ABSPATH."wp-admin/includes/upgrade.php");
  72.  
  73.       //collation and charsets added v1.2. Please excuse crappy altering tables/cols code
  74.       $charset_collate = '';
  75.       if(!empty($wpdb->charset)) {
  76.         $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
  77.       }
  78.       if (!empty($wpdb->collate)) {
  79.         $charset_collate .= " COLLATE $wpdb->collate";
  80.       }
  81.  
  82.       $table_name = $wpdb->prefix."nggcf_fields";
  83.       $sql = "CREATE TABLE ".$table_name." (
  84.        id BIGINT(19) NOT NULL AUTO_INCREMENT,
  85.        field_name TEXT NULL,
  86.        field_type TEXT NULL,
  87.        ngg_type INT NOT NULL DEFAULT 1,
  88.        drop_options TEXT NULL,
  89.        dateadded DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL,
  90.        UNIQUE KEY id (id)
  91.      ) ".$charset_collate.";";
  92.       dbDelta($sql);
  93.       //update tables to correct charset (default latin1 was set when built with old versions)
  94.       $wpdb->query('ALTER TABLE '.$table_name.' CHARSET utf8;');
  95.       $wpdb->query('ALTER TABLE '.$table_name.' MODIFY field_name varchar(255) CHARSET utf8;');
  96.       $wpdb->query('ALTER TABLE '.$table_name.' MODIFY field_type varchar(255) CHARSET utf8;');
  97.       $wpdb->query('ALTER TABLE '.$table_name.' MODIFY drop_options TEXT CHARSET utf8;');
  98.  
  99.       $table_name = $wpdb->prefix."nggcf_fields_link";
  100.       $sql = "CREATE TABLE ".$table_name." (
  101.        id BIGINT(19) NOT NULL AUTO_INCREMENT,
  102.        field_id BIGINT(19) NOT NULL DEFAULT 0,
  103.        gid BIGINT(19) NOT NULL DEFAULT 0,
  104.        UNIQUE KEY id (id)
  105.      ) ".$charset_collate.";";
  106.       dbDelta($sql);
  107.       //update tables to correct charset (default latin1 was set when built with old versions)
  108.       $wpdb->query('ALTER TABLE '.$table_name.' CHARSET utf8;');
  109.  
  110.  
  111.       $table_name = $wpdb->prefix."nggcf_field_values";
  112.       $sql = "CREATE TABLE ".$table_name." (
  113.        id BIGINT(19) NOT NULL AUTO_INCREMENT,
  114.        pid bigint(19) DEFAULT '0' NOT NULL,
  115.        fid bigint(19) DEFAULT '0' NOT NULL,
  116.        field_value TEXT NULL,
  117.        ngg_type INT NOT NULL DEFAULT 1,
  118.        dateadded DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL,
  119.        UNIQUE KEY id (id)
  120.      ) ".$charset_collate.";";
  121.       dbDelta($sql);
  122.       //update tables to correct charset (default latin1 was set when built with old versions)
  123.       $wpdb->query('ALTER TABLE '.$table_name.' CHARSET utf8;');
  124.       $wpdb->query('ALTER TABLE '.$table_name.' MODIFY field_value TEXT CHARSET utf8;');
  125.  
  126.       if($upgrade) {
  127.         $fields = nggcf_get_field_list(NGGCF_IMAGES);
  128.         global $nggdb;
  129.         $nggGalleries = $nggdb->find_all_galleries();
  130.  
  131.  
  132.         if($fields && $nggGalleries) {
  133.           $linkErr = false;
  134.           foreach ($fields as $key=>$val) {
  135.             if(!nggcf_get_linked_galleries($val->id)) {
  136.               foreach ($nggGalleries as $gal) {
  137.                 $qry = "INSERT INTO ".$wpdb->prefix."nggcf_fields_link (`id`, `field_id`, `gid`) VALUES(null, '".esc_sql($val->id)."', '".esc_sql($gal->gid)."')";
  138.                 if(!$wpdb->query($qry)) {
  139.                   $linkErr = true;
  140.                 }
  141.               }
  142.             }
  143.           }
  144.         }
  145.  
  146.         if($linkErr) {
  147.           _e("Database upgrade done, but there was a problem linking your existing fields to galleries.  You will need to manually link each field", "nggcustomfields");
  148.         }else{
  149.           _e("Database upgrade done", "nggcustomfields");
  150.         }
  151.       }
  152.     }
  153.   //}
  154.  
  155.   //api stuff{
  156.     //save custom field values (checks if it needs to insert or update)
  157.     function nggcf_save_pics($gid, $post) {
  158.       global $wpdb;
  159.       if ( is_array($post["nggcf_fields"]) ) {
  160.         foreach (stripslashes_deep($post["nggcf_fields"]) as $pid=>$fields) {
  161.           foreach ((array)$fields as $fid=>$val) {
  162.             if($row = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."nggcf_field_values WHERE fid = '$fid' AND pid = '$pid' AND ngg_type = '".NGGCF_IMAGES."'")) {
  163.               $wpdb->query("UPDATE ".$wpdb->prefix."nggcf_field_values SET field_value = '".esc_sql($val)."' WHERE id = ".$row->id);
  164.             }else{
  165.               if(esc_sql($val)) { //only if there is a value, add it to the db
  166.                 $wpdb->query("INSERT INTO ".$wpdb->prefix."nggcf_field_values (id, pid, fid, field_value, dateadded, ngg_type) VALUES (null, '$pid', '$fid', '".esc_sql($val)."', '".date("Y-m-d H:i:s", time())."', '".NGGCF_IMAGES."')");
  167.               }
  168.             }
  169.           }
  170.         }
  171.       }
  172.  
  173.       if(is_array($post["nggcf_gallery"])) {
  174.         $galleryId = $post["nggcf_gallery"]["ngg_gallery_id"];
  175.         unset($post["nggcf_gallery"]["ngg_gallery_id"]);
  176.  
  177.         foreach (stripslashes_deep($post["nggcf_gallery"]) as $fid=>$val) {
  178.           if($row = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."nggcf_field_values WHERE fid='".$fid."' AND pid='".$galleryId."' AND ngg_type = '".NGGCF_GALLERY."'")) {
  179.             $wpdb->query("UPDATE ".$wpdb->prefix."nggcf_field_values SET field_value = '".esc_sql($val)."' WHERE id = ".$row->id);
  180.           }else{
  181.             if(esc_sql($val)) { //only if there is a value, add it to the db
  182.               $wpdb->query("INSERT INTO ".$wpdb->prefix."nggcf_field_values (id, pid, fid, field_value, dateadded, ngg_type) VALUES (null, '".$galleryId."', '$fid', '".esc_sql($val)."', '".date("Y-m-d H:i:s", time())."', '".NGGCF_GALLERY."')");
  183.             }
  184.           }
  185.         }
  186.       }
  187.     }
  188.  
  189.     //api that saves new custom fields
  190.     function nggcf_save_field($config) {
  191.       global $wpdb;
  192.  
  193.       // by biziclop
  194.       $id = nggcf_ai( $config, "id");
  195.       $esc_id = esc_sql( $id );
  196.       $field_name = nggcf_ai( $config, "field_name");
  197.       $esc_field_name = esc_sql( $field_name );
  198.  
  199.       $linkedit = nggcf_ai( $config, "linkedit");
  200.  
  201.       if( $field_name || $id ) {
  202.         if($esc_id && esc_sql($config["drop_options"])) {
  203.           $qry = "UPDATE ".$wpdb->prefix."nggcf_fields SET drop_options = '".esc_sql($config["drop_options"])."' WHERE id = '".$esc_id."'";
  204.           if($wpdb->query($qry) !== false) {
  205.             return true;
  206.           }else{
  207.             return "ERROR: Failed to save field";
  208.           }
  209.         }else if( $esc_id && $field_name ) {
  210.           $qry = "UPDATE ".$wpdb->prefix."nggcf_fields SET field_name = '".$esc_field_name."' WHERE id = '".$esc_id."'";
  211.           if($wpdb->query($qry) !== false) {
  212.             return true;
  213.           }else{
  214.             return "ERROR: Failed to save field name";
  215.           }
  216.         }else if( $id && $linkedit ) {
  217.           $links = nggcf_get_linked_galleries( $id ); //get current links
  218.  
  219.           //loop current links
  220.           foreach ((array)$links as $key=>$val) {
  221.             //delete if not in post list (the link, and any field value)
  222.             if(!$config["galleries"][$val->gid]) {
  223.               $field = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."nggcf_fields WHERE id = ".$val->nggcf_field_id);
  224.               if($field->ngg_type == NGGCF_GALLERY) {
  225.                 $wpdb->query("DELETE FROM ".$wpdb->prefix."nggcf_field_values WHERE fid = ".$val->nggcf_field_id." AND pid = ".$val->gid); //remove data from that field in that gallery
  226.               }else if($field->ngg_type == NGGCF_IMAGES) {
  227.                 $list = array();
  228.                 if($fields = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."ngg_pictures WHERE galleryid = ".$val->gid)) {
  229.                   foreach ($fields as $pic) {
  230.                     $list[] = $pic->pid;
  231.                   }
  232.                   if($list) { //meh, if there was $fields the should always be $list, but this pony isnt built for speed :)
  233.                     $wpdb->query("DELETE FROM ".$wpdb->prefix."nggcf_field_values WHERE fid = ".$val->nggcf_field_id." AND pid IN (".implode(", ", $list).")"); //remove data from that field in that gallery
  234.                   }
  235.                 }
  236.               }
  237.               $wpdb->query("DELETE FROM ".$wpdb->prefix."nggcf_fields_link WHERE id = ".$val->link_id); //remove link
  238.               //$wpdb->query("DELETE FROM ".$wpdb->prefix."nggcf_field_values WHERE fid = ".$val->nggcf_field_id); //remove data from that field in that gallery
  239.             }
  240.  
  241.             //remove from post list if in currnt
  242.             if($config["galleries"][$val->gid]) {
  243.               unset($config["galleries"][$val->gid]);
  244.             }
  245.           }
  246.  
  247.           //insert whats left in post list (the new links)
  248.           foreach ((array)$config["galleries"] as $key=>$val) {
  249.             $qry = "INSERT INTO ".$wpdb->prefix."nggcf_fields_link (`id`, `field_id`, `gid`) VALUES(null, '".$esc_id."', '".esc_sql($key)."')";
  250.             $wpdb->query($qry);
  251.           }
  252.  
  253.           return true;
  254.         }else{
  255.           if($wpdb->get_row("SELECT * FROM ".$wpdb->prefix."nggcf_fields WHERE field_name = '".$esc_field_name."' AND ngg_type = '".esc_sql($config["ngg_type"])."'")) {
  256.             return "ERROR: Field name already exists";
  257.           }
  258.           if($config["field_type"] != NGGCF_FIELD_TYPE_SELECT) { //can only have drop opts if it is a drop down
  259.             $config["drop_options"] = "";
  260.           }
  261.           $qry = "INSERT INTO ".$wpdb->prefix."nggcf_fields (`id`, `field_name`, `field_type`, `drop_options`, `ngg_type`) VALUES (null, '".$esc_field_name."', '".esc_sql($config["field_type"])."', '".esc_sql($config["drop_options"])."', '".esc_sql($config["ngg_type"])."')";
  262.           if($wpdb->query($qry)) {
  263.             $linkerr = false;
  264.             $fid = $wpdb->insert_id;
  265.             foreach ((array)$config["galleries"] as $key=>$val) {
  266.               $qry = "INSERT INTO ".$wpdb->prefix."nggcf_fields_link (`id`, `field_id`, `gid`) VALUES(null, '".esc_sql($fid)."', '".esc_sql($key)."')";
  267.               if(!$wpdb->query($qry)) {
  268.                 $linkerr = true;
  269.               }
  270.             }
  271.             if($linkerr) {
  272.               return "ERROR: Field was saved successfully, but the system failed to link the field to 1 or more galleries";
  273.             }else{
  274.               return true;
  275.             }
  276.           }else{
  277.             return "ERROR: Failed to save field";
  278.           }
  279.         }
  280.       }else{
  281.         return "ERROR: Bad field name";
  282.       }
  283.     }
  284.  
  285.     //api that deletes a column from the list, and removes all values saved for it
  286.     function nggcf_delete_field($fid) {
  287.       global $wpdb;
  288.       if(is_numeric($fid)) {
  289.         if($wpdb->query("DELETE FROM ".$wpdb->prefix."nggcf_field_values WHERE fid = ".$fid) !== false) {
  290.           if($wpdb->query("DELETE FROM ".$wpdb->prefix."nggcf_fields WHERE id = ".$fid) !== false) {
  291.             return true;
  292.           }
  293.         }
  294.         return false;
  295.       }else{
  296.         return false;
  297.       }
  298.     }
  299.  
  300.     //get a list of all custom fields
  301.     function nggcf_get_field_list($ngg_type, $gid=null) {
  302.       global $wpdb;
  303.  
  304.       if($gid) {
  305.         $qry = "SELECT field.* FROM ".$wpdb->prefix."nggcf_fields_link AS link LEFT JOIN ".$wpdb->prefix."nggcf_fields AS field ON link.field_id = field.id WHERE field.ngg_type = ".esc_sql($ngg_type)." AND link.gid = '".esc_sql($gid)."'";
  306.       }else{
  307.         $qry = "SELECT * FROM ".$wpdb->prefix."nggcf_fields WHERE ngg_type = ".esc_sql($ngg_type);
  308.       }
  309.       $fields = $wpdb->get_results($qry);
  310.       return $fields;
  311.     }
  312.  
  313.     //get what galleries are linked to a field
  314.     function nggcf_get_linked_galleries($fid) {
  315.       global $wpdb;
  316.       if(is_numeric($fid)) {
  317.         return $wpdb->get_results("SELECT gal.*, link.id AS link_id, link.field_id AS nggcf_field_id FROM ".$wpdb->prefix."nggcf_fields_link AS link LEFT JOIN ".$wpdb->prefix."ngg_gallery AS gal ON link.gid = gal.gid WHERE link.field_id = '".esc_sql($fid)."'");
  318.       }else{
  319.         return false;
  320.       }
  321.     }
  322.  
  323.     function nggcf_hold_field_values($pid) {
  324.       global $wpdb, $nggcf_values;
  325.  
  326.       //only run the select once (store results in mem for access later if func called again with same pid)
  327.       if(!$nggcf_values[$pid]) {
  328.         $value = $wpdb->get_results("SELECT vals.*, cols.field_name, cols.id AS field_id, cols.field_type FROM ".$wpdb->prefix."nggcf_field_values AS vals LEFT JOIN ".$wpdb->prefix."nggcf_fields AS cols ON vals.fid = cols.id WHERE vals.pid = '".esc_sql($pid)."' AND cols.ngg_type = '".NGGCF_IMAGES."'");
  329.         $nggcf_values[$pid] = array();
  330.         foreach ((array)$value as $key=>$val) {
  331.           $nggcf_values[$pid][$val->field_name] = $val;
  332.         }
  333.       }
  334.     }
  335.  
  336.     function nggcf_hold_gallery_field_values($gid) {
  337.       global $wpdb, $nggcf_field_values;
  338.  
  339.       if(!$nggcf_field_values[$gid]) {
  340.         $value = $wpdb->get_results("SELECT vals.*, cols.field_name, cols.id AS field_id, cols.field_type FROM ".$wpdb->prefix."nggcf_field_values AS vals LEFT JOIN ".$wpdb->prefix."nggcf_fields AS cols ON vals.fid = cols.id WHERE cols.ngg_type = '".NGGCF_GALLERY."' AND vals.pid = '".esc_sql($gid)."'");
  341.  
  342.         foreach ((array)$value as $key=>$val) {
  343.           $nggcf_field_values[$gid][$val->field_name] = $val->field_value;
  344.         }
  345.       }
  346.     }
  347.   //}
  348.  
  349.   //admin stuff{
  350.     add_action("admin_menu", "nggcf_admin_menu");
  351.  
  352.     add_action("ngg_manage_image_custom_column", "nggcf_admin_col", 10 ,2);
  353.     add_filter("ngg_manage_images_columns", "nggcf_manage_cols");
  354.     add_action("ngg_update_gallery", "nggcf_save_pics", 10, 2);
  355.     add_filter("ngg_image_object", "nggcf_image_obj", 10, 2); // new in ngg 1.2.1
  356.  
  357.     //adding new gallery to NGG
  358.     add_action("ngg_add_new_gallery_form", "nggcf_new_gallery_form"); //new in ngg 1.4.0a
  359.     add_action("ngg_created_new_gallery", "nggcf_add_new_gallery"); //new in ngg 1.4.0a
  360.  
  361.     function nggcf_admin_menu() {
  362.       add_menu_page("NextGEN/NextCellent Gallery Custom Fields", "NGG Custom Fields", "manage_options", __FILE__, "nggcf_plugin_options");
  363.       add_submenu_page(__FILE__, "Setup NextGEN/NextCellent Gallery Custom Fields", "Setup Fields", "manage_options", __FILE__, "nggcf_plugin_options");
  364.     }
  365.  
  366.     function nggcf_plugin_options() {
  367.       $filepath = admin_url()."admin.php?page=".$_GET["page"];
  368.       ?>
  369.       <div class="wrap">
  370.         <?php
  371.         switch( nggcf_ai( $_GET, "mode")) {
  372.         case "gallery"  : nggcf_image_options(NGGCF_GALLERY); break;
  373.         case "images"   : nggcf_image_options(NGGCF_IMAGES);  break;
  374.         case "upgrade"  : nggcf_install(true);  break;
  375.         default :
  376.         ?>
  377.         <h2><?php _e("NextGEN/NextCellent Gallery Custom Fields Options", "nggcustomfields"); ?></h2>
  378.         <div id="poststuff">
  379.           <ul>
  380.             <li><a href="<?php echo $filepath."&mode=images"; ?>">Image Custom Fields</a> <em>(Add custom fields to be added to images inside NextGEN/NextCellent Galleries)</em></li>
  381.             <li><a href="<?php echo $filepath."&mode=gallery"; ?>">Gallery Custom Fields</a> <em>(Add custom fields to be added to NextGEN/NextCellent Galleries)</em></li>
  382.           </ul>
  383.         </div>
  384.         <?php
  385.         break;
  386.         }
  387.         ?>
  388.       </div> <!-- end .wrap -->
  389.       <?php
  390.     }
  391.  
  392.     function nggcf_image_options($type) {
  393.       if($type == NGGCF_GALLERY) {
  394.         $filepath = admin_url()."admin.php?page=".$_GET["page"]."&mode=gallery";
  395.         $nggtype = NGGCF_GALLERY;
  396.         ?>
  397.         <h2><?php _e("Setup Gallery Custom Fields", "nggcustomfields"); ?></h2>
  398.         <?php
  399.       }elseif($type == NGGCF_IMAGES) {
  400.         $filepath = admin_url()."admin.php?page=".$_GET["page"]."&mode=images";
  401.         $nggtype = NGGCF_IMAGES;
  402.         ?>
  403.         <h2><?php _e("Setup Image Custom Fields", "nggcustomfields"); ?></h2>
  404.         <?php
  405.       }
  406.  
  407.       if($_POST) {
  408.         $conf = stripslashes_deep($_POST["conf"]);
  409.         //TODO, if save existing drop records, maybe we should make sure none of the options that are currently linked to images have been changed?
  410.         if(($err = nggcf_save_field($conf)) !== true) {
  411.           ?>
  412.           <div class="nggcf-error">
  413.           <h3 class="nggcf-error-h3">Error Saving Data</h3>
  414.           The following error was returned when trying to save the custom field:<br />
  415.           <?php echo $err; ?>
  416.           </div>
  417.           <?php
  418.         }
  419.       }
  420.       if( is_numeric( nggcf_ai( $_GET, "delete"))) {
  421.         if(!nggcf_delete_field($_GET["delete"])) {
  422.           ?>
  423.           <div class="nggcf-error">
  424.           <h3 class="nggcf-error-h3">Error Deleting Data</h3>
  425.           There was an error trying to delete the field and its data.  Please try again in a moment.
  426.           <?php echo $err; ?>
  427.           </div>
  428.           <?php
  429.         }
  430.       }
  431.       ?>
  432.  
  433.       <?php
  434.       $fields = nggcf_get_field_list($nggtype);
  435.       global $nggdb;
  436.       $nggGalleries = $nggdb->find_all_galleries();
  437.       if($fields) {
  438.         ?>
  439.         <h3><?php _e("Existing Fields", "nggcustomfields");?></h3>
  440.         <table cellspacing="0" class="widefat fixed" id="table-existing">
  441.           <thead>
  442.             <tr>
  443.               <th style="width:250px;"><?php _e("Field Name", "nggcustomfields");?></th>
  444.               <th style="width:250px;"><?php _e("Field Type", "nggcustomfields");?></th>
  445.               <th><?php _e("Actions", "nggcustomfields");?></th>
  446.             </tr>
  447.           </thead>
  448.           <?php
  449.           foreach ((array)$fields as $key=>$val) {
  450.             $linked = nggcf_get_linked_galleries($val->id);
  451.             ?>
  452.             <tr>
  453.               <td>
  454.                 <?php
  455.                 if( nggcf_ai( $_GET, "editname") == $val->id) {
  456.                   $fstyle = "block";
  457.                   $nstyle = "none";
  458.                 }else{
  459.                   $fstyle = "none";
  460.                   $nstyle = "block";
  461.                 }
  462.                 ?>
  463.                 <form id="edit_field_form_<?php echo $val->id ?>" method="POST" action="<?php echo $filepath; ?>" accept-charset="utf-8" style="display:<?php echo $fstyle; ?>;">
  464.                   <input type="hidden" name="conf[id]" value="<?php echo $val->id; ?>" />
  465.                   <input type="text" name="conf[field_name]" value="<?php echo esc_attr($val->field_name);  ?>" />
  466.                   <input class="button-primary" type="submit" value="<?php _e("Save", "nggcustomfields") ;?>"/>
  467.                 </form>
  468.                 <span id="edit_field_name_<?php echo $val->id ?>" style="display:<?php echo $nstyle; ?>"><?php echo htmlspecialchars($val->field_name); ?></span>
  469.               </td>
  470.               <td>
  471.                 <?php
  472.                 switch($val->field_type) {
  473.                 case NGGCF_FIELD_TYPE_INPUT : echo "Text Input"; break;
  474.                 case NGGCF_FIELD_TYPE_TEXTAREA : echo "Text Area"; break;
  475.                 case NGGCF_FIELD_TYPE_SELECT : echo "Drop Down"; break;
  476.                 }
  477.                 ?>
  478.               </td>
  479.               <td>
  480.                 [ <a id="nggcf_edit_field_name_<?php echo $val->id; ?>" class="nggcf_edit_field_name" href="<?php echo $filepath; ?>&editname=<?php echo $val->id; ?>">Edit Name</a> ]
  481.                 [ <a id="nggcf_edit_field_link_<?php echo $val->id; ?>" class="nggcf_edit_field_link" href="<?php echo $filepath; ?>&editlink=<?php echo $val->id; ?>">Edit Linked Galleries</a> ]
  482.                 [ <a class="nggcf_del_field" href="<?php echo $filepath; ?>&delete=<?php echo $val->id; ?>">Delete Field</a> ]
  483.                 <?php if($val->field_type == NGGCF_FIELD_TYPE_SELECT) { ?>
  484.                   [ <a id="nggcf_edit_field_<?php echo $val->id; ?>" class="nggcf_edit_field" href="<?php echo $filepath; ?>&edit=<?php echo $val->id; ?>">Edit Options</a> ]
  485.                 <?php } ?>
  486.  
  487.                 <div id="nggcf-edit-link-<?php echo $val->id; ?>" class="nggcf-edit-link" style="display:<?php echo (nggcf_ai( $_GET, "editlink") == $val->id ? "block" : "none"); ?>;">
  488.                   <form id="" method="POST" action="<?php echo $filepath; ?>" accept-charset="utf-8" >
  489.                     <input type="hidden" name="conf[id]" value="<?php echo $val->id; ?>" />
  490.                     <input type="hidden" name="conf[linkedit]" value="1" /> <!-- just so the api know what we are trying to save -->
  491.                     <?php
  492.                     foreach ($nggGalleries as $gval) {
  493.                       $isLinked = "";
  494.                       foreach ($linked as $link) {
  495.                         if($gval->gid == $link->gid) {
  496.                           $isLinked = "checked";
  497.                           break;
  498.                         }
  499.                       }
  500.                       echo "<input type='checkbox' name='conf[galleries][".$gval->gid."]' value='1' ".$isLinked." /> ".$gval->name."<br />";
  501.                     }
  502.                     ?>
  503.  
  504.                     <br /><strong>(If you unselect a gallery, any values saved for this field in that gallery will be deleted too!)</strong>
  505.                     <div class="submit">
  506.                       <input class="button-primary" type="submit" value="<?php _e("Save Links", "nggcustomfields") ;?>"/>
  507.                     </div>
  508.                   </form>
  509.                 </div>
  510.  
  511.                 <?php if($val->field_type == NGGCF_FIELD_TYPE_SELECT) { ?>
  512.                   <div id="nggcf-edit-options-<?php echo $val->id; ?>" class="nggcf-edit-options" style="display:<?php echo (nggcf_ai( $_GET, "edit") == $val->id ? "block" : "none"); ?>;">
  513.                     <form id="" method="POST" action="<?php echo $filepath; ?>" accept-charset="utf-8" >
  514.                       <input type="hidden" name="conf[id]" value="<?php echo $val->id; ?>" />
  515.                       <textarea cols="33" name="conf[drop_options]"><?php echo htmlspecialchars($val->drop_options); ?></textarea><br /><i>eg: Some Option, Another Option, Third Option</i>
  516.                       <div class="submit">
  517.                         <input class="button-primary" type="submit" value="<?php _e("Save Field", "nggcustomfields") ;?>"/>
  518.                       </div>
  519.                     </form>
  520.                   </div>
  521.                   <?php } ?>
  522.               </td>
  523.             </tr>
  524.             <?php
  525.           }
  526.         ?>
  527.         </table>
  528.         <?php
  529.       }
  530.       ?>
  531.  
  532.       <br />
  533.       <div id="poststuff">
  534.         <form id="" method="POST" action="<?php echo $filepath; ?>" accept-charset="utf-8" >
  535.           <input type="hidden" name="conf[ngg_type]" value="<?php echo $nggtype; ?>" />
  536.           <div class="postbox">
  537.             <h3><?php _e("Add New Field", "nggcustomfields") ;?></h3>
  538.             <table class="form-table">
  539.               <tr valign="top">
  540.                 <th><?php _e("Display Name", "nggcustomfields") ;?>:</th>
  541.                 <td><input type="text" size="35" name="conf[field_name]" value="" /></td>
  542.               </tr>
  543.  
  544.               <tr valign="top">
  545.                 <th><?php _e("Show on Galleries", "nggcustomfields") ;?>:</th>
  546.                 <td>
  547.                 <?php
  548.                 if($nggGalleries) {
  549.                   foreach ($nggGalleries as $key=>$val) {
  550.                     echo "<input type='checkbox' name='conf[galleries][".$val->gid."]' value='1' checked /> ".$val->name."<br />";
  551.                   }
  552.                 }else{
  553.                   _e("No Galleries Found Yet", "nggcustomfields");
  554.                   echo "!";
  555.                 }
  556.                 ?>
  557.                 </td>
  558.               </tr>
  559.  
  560.  
  561.               <tr valign="top">
  562.                 <th><?php _e("Field Type", "nggcustomfields") ;?>:</th>
  563.                 <td>
  564.                   <select id="field_type" name="conf[field_type]" onchange="toggleDropOps(this.value);">
  565.                   <option value="<?php echo NGGCF_FIELD_TYPE_INPUT; ?>">Text Input</option>
  566.                   <option value="<?php echo NGGCF_FIELD_TYPE_TEXTAREA; ?>">Text Area</option>
  567.                   <option value="<?php echo NGGCF_FIELD_TYPE_SELECT; ?>">Select Drop Down</option>
  568.                   </select>
  569.                 </td>
  570.               </tr>
  571.  
  572.               <tr valign="top" id="drop_options">
  573.                 <th><?php _e("Drop Down Options (comma seperated list)", "nggcustomfields") ;?>:</th>
  574.                 <td><textarea cols="33" name="conf[drop_options]"></textarea><br /><i>eg: Some Option, Another Option, Third Option</i></td>
  575.               </tr>
  576.             </table>
  577.  
  578.             <div class="submit">
  579.               <input class="button-primary" type="submit" value="<?php _e("Create Field", "nggcustomfields") ;?>"/>
  580.             </div>
  581.           </div>
  582.         </form>
  583.       </div>
  584.  
  585.       <script>
  586.       jQuery(document).ready(function(){
  587.           //hide drop opts boxes for fields not needing them
  588.           if(document.getElementById("field_type").value != <?php echo NGGCF_FIELD_TYPE_SELECT; ?>) {
  589.             document.getElementById("drop_options").style.display = "none";
  590.           }
  591.  
  592.           //js confirm on the delete links
  593.           jQuery("a.nggcf_del_field").each(function() {
  594.               this.onclick = function() {
  595.                 if(confirm("Are you sure you want to permanently remove this field and all its saved values?")) {
  596.                   return true;
  597.                 }else{
  598.                   return false;
  599.                 }
  600.               }
  601.           });
  602.  
  603.           jQuery("a.nggcf_edit_field").each(function() {
  604.               this.onclick = function() {
  605.                 var fid = this.id.substr(17); //get the db row id off the string id of the el
  606.                 jQuery('#nggcf-edit-options-'+fid).toggle("normal");
  607.                 return false;
  608.               }
  609.           });
  610.  
  611.           jQuery("a.nggcf_edit_field_name").each(function() {
  612.               this.onclick = function() {
  613.                 var fid = this.id.substr(22); //get the db row id off the string id of the el
  614.                 jQuery('#edit_field_form_'+fid).toggle("normal");
  615.                 jQuery('#edit_field_name_'+fid).toggle("normal");
  616.                 return false;
  617.               }
  618.           });
  619.  
  620.           jQuery("a.nggcf_edit_field_link").each(function() {
  621.               this.onclick = function() {
  622.                 var fid = this.id.substr(22); //get the db row id off the string id of the el
  623.                 jQuery('#nggcf-edit-link-'+fid).toggle("normal");
  624.                 return false;
  625.               }
  626.           });
  627.  
  628.  
  629.       });
  630.  
  631.       function toggleDropOps(val) {
  632.         if(val == <?php echo NGGCF_FIELD_TYPE_SELECT; ?>) {
  633.           document.getElementById("drop_options").style.display = "";
  634.         }else{
  635.           document.getElementById("drop_options").style.display = "none";
  636.         }
  637.       }
  638.       </script>
  639.       <?php
  640.     }
  641.  
  642.     function nggcf_new_gallery_form() {
  643.       $iList = nggcf_get_field_list(NGGCF_IMAGES);
  644.       $gList = nggcf_get_field_list(NGGCF_GALLERY);
  645.  
  646.       if($iList) {
  647.         ?>
  648.         <tr valign="top">
  649.         <th scope="row"><?php _e('Link Image Custom Fields', 'nggcustomfields') ;?>:</th>
  650.         <td>
  651.         <?php foreach ($iList as $key=>$val) { ?>
  652.           <input type="checkbox" name="conf[nggcf_fields][<?php echo $val->id ?>]" value="1" checked /> <?php echo htmlspecialchars($val->field_name) ?><br />
  653.         <?php } ?>
  654.         </td>
  655.         </tr>
  656.         <?php
  657.       }
  658.  
  659.       if($gList) {
  660.         ?>
  661.         <tr valign="top">
  662.         <th scope="row"><?php _e('Link Gallery Custom Fields', 'nggcustomfields') ;?>:</th>
  663.         <td>
  664.         <?php foreach ($gList as $key=>$val) { ?>
  665.           <input type="checkbox" name="conf[nggcf_fields][<?php echo $val->id ?>]" value="1" checked /> <?php echo htmlspecialchars($val->field_name) ?><br />
  666.         <?php } ?>
  667.         </td>
  668.         </tr>
  669.         <?php
  670.       }
  671.     }
  672.  
  673.     function nggcf_add_new_gallery($gid) {
  674.       global $wpdb;
  675.       if($fields = stripslashes_deep($_POST["conf"]["nggcf_fields"])) {
  676.         foreach ((array)$fields as $key=>$val) {
  677.           $qry = "INSERT INTO ".$wpdb->prefix."nggcf_fields_link (`id`, `field_id`, `gid`) VALUES(null, '".esc_sql($key)."', '".esc_sql($gid)."')";
  678.           $wpdb->query($qry);
  679.         }
  680.       }
  681.     }
  682.  
  683.     //add the col to array of cols
  684.     function nggcf_manage_cols($gallery_columns) {
  685.       global $wpdb;
  686.       $fields = nggcf_get_field_list(NGGCF_IMAGES, $_GET["gid"]);
  687.  
  688.       foreach ((array)$fields as $key=>$val) {
  689.         $gallery_columns[htmlspecialchars($val->field_name)] = htmlspecialchars($val->field_name);
  690.       }
  691.  
  692.       return $gallery_columns;
  693.     }
  694.  
  695.     //the field for managing the images in a gallery
  696.     function nggcf_admin_col($gallery_column_key, $pid) {
  697.       global $wpdb, $ngg_edit_gallery;
  698.  
  699.       //htmlspecialchars_decode() as the $gallery_column_key was htmlspecialchars() when adding the custom fields
  700.       $custCol = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."nggcf_fields WHERE field_name = '".esc_sql(htmlspecialchars_decode($gallery_column_key))."'");
  701.       $value = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."nggcf_field_values WHERE fid = '".$custCol->id."' AND pid = '$pid'");
  702.  
  703.       // by biziclop
  704.       $name = "name=\"nggcf_fields[{$pid}][{$custCol->id}]\"";
  705.       $esc_field_value = esc_attr( nggcf_ai( $value, 'field_value', ''));
  706.  
  707.       switch($custCol->field_type) {
  708.       case NGGCF_FIELD_TYPE_INPUT :?>
  709.         <input <?php echo $name; ?> style="width:95%;" value="<?php echo $esc_field_value; ?>" />
  710.         <?php break;
  711.       case NGGCF_FIELD_TYPE_TEXTAREA :?>
  712.         <textarea <?php echo $name; ?> style="width:95%;"><?php echo $esc_field_value; ?></textarea>
  713.         <?php break;
  714.       case NGGCF_FIELD_TYPE_SELECT :
  715.         $opts = explode(",", $custCol->drop_options);
  716.         ?>
  717.         <select <?php echo $name; ?>>
  718.         <option value=""></option>
  719.         <?php
  720.           foreach ((array)$opts as $key=>$val) {
  721.             ?>
  722.             <option value="<?php echo trim(esc_attr($val)); ?>"<?php echo trim(esc_attr($val)) == $esc_field_value ? " selected" : "" ?>><?php echo trim(esc_attr($val)); ?></option>
  723.             <?php
  724.           }
  725.         ?>
  726.         </select>
  727.         <?php break;
  728.       }
  729.  
  730.       /* dependency on JS dropped thanks to new action hook 'ngg_manage_gallery_settings'. See hook/func below for rewrite (keeping code for the hell of it, it's free)
  731.       if(!$ngg_edit_gallery) {
  732.         $ngg_edit_gallery = true;
  733.  
  734.         $fields = nggcf_get_field_list(NGGCF_GALLERY, $_GET["gid"]);
  735.         ?>
  736.         <script type="text/javascript">
  737.           jQuery(document).ready(function() {
  738.               jQuery("#gallerydiv table.form-table").each(function(item) { //the table with gallery fields
  739.                   var row = this.insertRow(this.rows.length);
  740.                   var cell = row.insertCell(0);
  741.                   cell.align = "left";
  742.                   cell.colSpan = 4;
  743.                   cell.innerHTML = "Custom Columns<hr /><input type='hidden' name='nggcf_gallery[ngg_gallery_id]' value='<?php echo $_GET["gid"]; ?>' />";
  744.  
  745.                   <?php
  746.                   foreach ((array)$fields as $key=>$val) {
  747.                     ?>
  748.                     row = this.insertRow(this.rows.length);
  749.                     cell = row.insertCell(0);
  750.                     cell.align = "right";
  751.                     cell.innerHTML = '<?php echo $val->field_name; ?>';
  752.  
  753.                     cell = row.insertCell(1);
  754.  
  755.                     <?php
  756.                     $value = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."nggcf_field_values WHERE pid = '".esc_sql($_GET["gid"])."' AND fid = '".$val->id."' AND ngg_type = '".NGGCF_GALLERY."'");
  757.  
  758.                     switch($val->field_type) {
  759.                     case NGGCF_FIELD_TYPE_INPUT :?>
  760.                       cell.innerHTML = '<input name="nggcf_gallery[<?php echo $val->id; ?>]" style="width:95%;" value="<?php echo $value->field_value; ?>" />';
  761.                       <?php break;
  762.                     case NGGCF_FIELD_TYPE_TEXTAREA :?>
  763.                       cell.innerHTML = '<textarea name="nggcf_gallery[<?php echo $val->id; ?>]" style="width:95%;"><?php echo str_replace(array("\r","\n"), array("\\r", "\\n"), $value->field_value); ?></textarea>';
  764.                       <?php break;
  765.                     case NGGCF_FIELD_TYPE_SELECT :
  766.                       $opts = explode(",", $val->drop_options);
  767.                       ?>
  768.                       var str;
  769.                       str = '<select name="nggcf_gallery[<?php echo $val->id; ?>]">';
  770.                       str += '<option value=""></option>';
  771.                       <?php
  772.                       foreach ((array)$opts as $optVal) {
  773.                         ?>
  774.                         str += '<option value="<?php echo trim($optVal); ?>"<?php if(trim($optVal) == $value->field_value) {echo " selected";} ?>><?php echo trim($optVal); ?></option>';
  775.                         <?php
  776.                       }
  777.                       ?>
  778.                       str += '</select>';
  779.                       cell.innerHTML = str;
  780.                       <?php break;
  781.                     }
  782.                   }
  783.                   ?>
  784.               });
  785.           });
  786.         </script>
  787.         <?php
  788.       }
  789.       */
  790.     }
  791.  
  792.     add_action('ngg_manage_gallery_settings', 'nggcf_admin_gallery_col');
  793.     function nggcf_admin_gallery_col($act_gid) {
  794.       global $wpdb;
  795.       $fields = nggcf_get_field_list(NGGCF_GALLERY, $act_gid);
  796.  
  797.       echo '<tr>';
  798.       echo '<td colspan="4" align="left">';
  799.       echo '<strong>Custom Columns</strong><hr /><input type="hidden" name="nggcf_gallery[ngg_gallery_id]" value="'.$act_gid.'" />';
  800.  
  801.       foreach ((array)$fields as $key=>$val) {
  802.         echo '<tr>';
  803.         echo '<td align="right">'.htmlspecialchars($val->field_name).'</td>';
  804.         echo '<td>';
  805.  
  806.         $value = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."nggcf_field_values WHERE pid = '".esc_sql($act_gid)."' AND fid = '".$val->id."' AND ngg_type = '".NGGCF_GALLERY."'");
  807.  
  808.         // added by biziclop:
  809.         $esc_field_value = esc_attr( nggcf_ai( $value, 'field_value', ''));
  810.  
  811.         switch($val->field_type) {
  812.         case NGGCF_FIELD_TYPE_INPUT :
  813.           echo '<input name="nggcf_gallery['.$val->id.']" style="width:95%;" value="'.$esc_field_value.'" />';
  814.           break;
  815.         case NGGCF_FIELD_TYPE_TEXTAREA :
  816.           echo '<textarea name="nggcf_gallery['.$val->id.']" style="width:95%;">'.$esc_field_value.'</textarea>';
  817.           break;
  818.         case NGGCF_FIELD_TYPE_SELECT :
  819.           $opts = explode(",", $val->drop_options);
  820.           echo '<select name="nggcf_gallery['.$val->id.']">';
  821.           echo '<option value=""></option>';
  822.           foreach ((array)$opts as $optVal) {
  823.             echo '<option value="'.trim(esc_attr($optVal)).'" '.(trim(esc_attr($optVal)) == $esc_field_value ? ' selected="selected"' : '').'>'.trim(esc_attr($optVal)).'</option>';
  824.           }
  825.           echo '</select>';
  826.           break;
  827.         }
  828.  
  829.         echo '</td>';
  830.         echo '</tr>';
  831.       }
  832.  
  833.       echo '</td>';
  834.       echo '</tr>';
  835.     }
  836.   //}
  837.  
  838.   //front end stuff{
  839.     //returns a specific fields value for a specific image
  840.     function nggcf_get_field($pid, $fname) {
  841.       global $nggcf_values;
  842.       nggcf_hold_field_values($pid);
  843.  
  844.       return $nggcf_values[$pid][$fname]->field_value;
  845.     }
  846.  
  847.     //new filter in ngg 1.2.1 allows us to add to the list of images (later to be passed to the templates), while its being created!  Thanks Alex
  848.     function nggcf_image_obj($pictureObj, $pid) {
  849.       global $nggcf_values;
  850.  
  851.       nggcf_hold_field_values($pid);
  852.  
  853.       @$pictureObj->ngg_custom_fields = array();
  854.       foreach ($nggcf_values[$pid] as $key=>$val) {
  855.         @$pictureObj->ngg_custom_fields[$key] = $val->field_value;
  856.       }
  857.  
  858.       return $pictureObj;
  859.     }
  860.  
  861.     function nggcf_get_gallery_field($gid, $fname) {
  862.       global $nggcf_field_values;
  863.  
  864.       nggcf_hold_gallery_field_values($gid);
  865.  
  866.       return $nggcf_field_values[$gid][$fname];
  867.     }
  868.   //}
  869. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement