Advertisement
Guest User

Checkbox problem

a guest
May 31st, 2012
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.32 KB | None | 0 0
  1. <?php
  2.  
  3. error_reporting(E_ALL);
  4. ini_set("display_errors", 1);
  5.  
  6. /*
  7.     Plugin Name: Image Gallery Uploader
  8.     Plugin URI: https://github.com/kschat
  9.     Description: This plugin allows visitors of your WordPress site to upload images to a gallery.
  10.     Version: 0.1
  11.     License: GPL2
  12.  
  13. include_once 'Debug.php';
  14.  
  15. define('PLUGIN_PATH', plugin_dir_url(__FILE__));
  16.  
  17. class UploaderWidget extends WP_Widget {
  18.    
  19.     // Constructor
  20.     function __construct() {
  21.         $widget_ops = array(
  22.         'classname' => 'image-uploader',
  23.         'description' => 'A widget used to upload photos to a gallery.');
  24.        
  25.         $control_ops = array('width'=> '200', 'height'=> '350', 'id_base' => 'image-uploader-id');
  26.        
  27.         parent::__construct('image-uploader-id', 'Uploader Widget', $widget_ops, $control_ops);
  28.     }
  29.    
  30.     function widget($args, $instance) {
  31.         extract($args);
  32.        
  33.         $title = apply_filters('widget_title', $instance['title']);
  34.        
  35.         echo $before_widget;
  36.        
  37.         if($title) {
  38.             echo $before_title . $title . $after_title;
  39.         }
  40.         ?>
  41.         <img src="<?php echo PLUGIN_PATH;?>images/defaultImage.gif"><br />
  42.         <p>
  43.         <label for="upload-button" style="color:black;">Upload new Image:</label>
  44.         <input type="file" name="upload-button" />
  45.         </p>
  46.         <?php
  47.        
  48.         echo $after_widget;
  49.     }
  50.    
  51.     function update($new_instance, $old_instance) {
  52.         $instance = $old_instance;
  53.        
  54.         $instance['title'] = $new_instance['title'];
  55.         $instance['imgDir'] = $new_instance['imgDir'];
  56.         //$instance['categories'][0]['selected'] = 1;
  57.         //$instance['categories'][0]['label'] = '';
  58.         //$instance['newCategory'] = '';
  59.        
  60.        
  61.        
  62.         $instance['categories'] = $this->parseAndCombineCategories((get_option('UploaderWidget_categories')), $new_instance['newCategory']);
  63.        
  64.         for($i=0; $i<sizeof($instance['categories']); $i++) {
  65.             $instance['categories'][$i]['selected'] = (int)$new_instance['categories'][$i]['selected'];
  66.         }
  67.        
  68.         //$instance['categories'][0]['selected'] = 1;
  69.         //$instance['categories'][0]['label'] = 'label';
  70.        
  71.         update_option('UploaderWidget_categories', $instance['categories']);
  72.        
  73.         return $instance;
  74.     }
  75.    
  76.     function form($instance) {
  77.         $defaults = array('title'       => 'Uploader Widget',
  78.                           'imgDir'      => PLUGIN_PATH.'images/',
  79.                           'categories'  => array(0 => array('selected'   => 0,
  80.                                                             'label'      => 'option1'),
  81.                                                  1 => array('selected'   => 0,
  82.                                                             'label'      => 'option2'),
  83.                                                  2 => array('selected'   => 0,
  84.                                                             'label'      => 'option3')));
  85.        
  86.         $instance = wp_parse_args((array)$instance, $defaults);
  87.         //$instance['categories'] = null;
  88.         //$instance['newCategory'] = null;
  89.         ?>
  90.         <p>
  91.             <label for="<?php echo $this->get_field_id('title');?>">Title:</label><br />
  92.             <input type= "text" id="<?php echo $this->get_field_id('title');?>" name="<?php echo $this->get_field_name('title');?>"
  93.                    value="<?php echo $instance['title'];?>" />
  94.         </p>
  95.        
  96.         <p>
  97.             <label for="<?php echo $this->get_field_id('imgDir');?>">Saved Images Directory:</label>
  98.             <input type="text" id="<?php echo $this->get_field_id('imgDir');?>" name="<?php echo $this->get_field_name('imgDir');?>"
  99.                     value="<?php echo $instance['imgDir'];?>" readonly="readonly" />
  100.             <input type="file" />
  101.         </p>
  102.        
  103.         <p>
  104.             Categories:<br />
  105.             <?php
  106.             $this->displayCategories($instance['categories']);
  107.             $this->displayNewCategoriesInput('newCategory');
  108.             ?>
  109.         </p>
  110.         <?php
  111.     }
  112.    
  113.     function parseAndCombineCategories($categories, $newCategories) {
  114.         //unset($categories);
  115.         if(trim($newCategories) != "") {
  116.             unset($categories['NoCategories']);
  117.             $newCategories = split(',', $newCategories);
  118.             $newSize = sizeof($categories) + sizeof($newCategories);
  119.             $oldSize = sizeof($categories);
  120.            
  121.             for($i=sizeof($categories); $i<$newSize; $i++) {
  122.                 $categories[$i]['selected'] = 0;
  123.                 $categories[$i]['label'] = $newCategories[$i - $oldSize];
  124.             }
  125.         }
  126.         else if(empty($categories)) {
  127.             return $categories = array('NoCategories' => '<b>No categories have been made yet.</b>');
  128.         }
  129.         return (array)$categories;
  130.     }
  131.    
  132.     function displayCategories($categories) {
  133.         if(array_key_exists('NoCategories', $categories)) {
  134.             ?> <p> <?php
  135.             echo $categories['NoCategories'];
  136.             ?> </p> <?php
  137.             return;
  138.         }
  139.         ?> <table class="form-table" id="image-gallery-form-table"> <?php
  140.         for($i=0; $i<sizeof($categories); $i++) {
  141.             echo ($i % 2 == 0) ? "<tr><td>": "<td>"; ?>
  142.             <input class="checkboxes" type="checkbox" <?php checked((int)$categories[$i]['selected'], 1); ?> id="<?php echo $this->get_field_id($categories[$i]['label']); ?>"
  143.                    name="<?php echo $this->get_field_name($categories[$i]['label']);?>" />
  144.                 <label for="<?php echo $this->get_field_id($categories[$i]['label']); ?>"><?php echo $categories[$i]['label']; ?></label>
  145.                 <?php echo ($i % 2 == 0) ? "</td>": "</td></tr>";
  146.         }
  147.         ?> </table> <?php echo Debug::printArray($categories, false);
  148.     }
  149.    
  150.     function displayNewCategoriesInput($field) {
  151.         ?>
  152.         <label for="<?php echo $this->get_field_id($field); ?>">Add a new category
  153.             (seperate names with a ',' to add multiple categories):</label>
  154.             <input type="text" name="<?php echo $this->get_field_name($field); ?>"
  155.                    id="<?php echo $this->get_field_id($field); ?>" />
  156.         <?php
  157.     }
  158. }
  159.  
  160. function registerUploaderWidget() {
  161.     register_widget('UploaderWidget');
  162. }
  163.  
  164. add_action('widgets_init', 'registerUploaderWidget');
  165. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement