Zikula_Template_Plugins
[ class tree: Zikula_Template_Plugins ] [ index: Zikula_Template_Plugins ] [ all elements ]

Source for file function.pnformcheckboxlist.php

Documentation is available at function.pnformcheckboxlist.php

  1. <?php
  2. /**
  3.  * Checkbox list plugin
  4.  *
  5.  * @copyright (c) 2006, Zikula Development Team
  6.  * @link http://www.zikula.org
  7.  * @version $Id: function.pnformdropdownlist.php 22138 2007-06-01 10:19:14Z markwest $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  * @author Jorn Wildt
  10.  * @package Zikula_Template_Plugins
  11.  * @subpackage Functions
  12.  */
  13.  
  14. Loader::requireOnce('system/pnForm/plugins/pnformbaselistselector.php');
  15.  
  16. /**
  17.  * Checkbox list
  18.  *
  19.  * Renders a list of checkboxes with the supplied items.
  20.  * Usefull for selecting multiple items.
  21.  *
  22.  * You can set the items directly like this:
  23.  * <code>
  24.  * <!--[pnformcheckboxlist id="mylist" items=$items]-->
  25.  * </code>
  26.  * with the form event handler code like this:
  27.  * <code>
  28.  * class mymodule_user_testHandler extends pnFormHandler
  29.  * {
  30.  *   function initialize(&$render)
  31.  *   {
  32.  *       $items = array( array('text' => 'A', 'value' => '1'),
  33.  *                       array('text' => 'B', 'value' => '2'),
  34.  *                       array('text' => 'C', 'value' => '3') );
  35.  *
  36.  *       $render->assign('items', $items); // Supply items
  37.  *       $render->assign('mylist', 2);     // Supply selected value
  38.  *   }
  39.  * }
  40.  * </code>
  41.  * Or you can set them indirectly using the plugin's databased features:
  42.  * <code>
  43.  * <!--[pnformchekboxlist id="mylist"]-->
  44.  * </code>
  45.  * with the form event handler code like this:
  46.  * <code>
  47.  * class mymodule_user_testHandler extends pnFormHandler
  48.  * {
  49.  *   function initialize(&$render)
  50.  *   {
  51.  *       $items = array( array('text' => 'A', 'value' => '1'),
  52.  *                       array('text' => 'B', 'value' => '2'),
  53.  *                       array('text' => 'C', 'value' => '3') );
  54.  *
  55.  *       $render->assign('mylistItems', $items);  // Supply items
  56.  *       $render->assign('mylist', 2);            // Supply selected value
  57.  *   }
  58.  * }
  59.  * </code>
  60.  *
  61.  * The resulting dataset is a list of strings representing the selected
  62.  * values. So when you do a $data = $render->pnFormGetValues(); you will
  63.  * get a dataset like this:
  64.  *
  65.  * <code>
  66.  *   array('xxx' => 'valueXX',
  67.  *         'checkboxes' => array('15','17','22','34'),
  68.  *         'yyy' => 'valueYYY')
  69.  * </code>
  70.  *
  71.  * @package pnForm
  72.  * @subpackage Plugins
  73.  */
  74. {
  75.     /**
  76.      * Selected value(s)
  77.      *
  78.      * The selected value(s) of a checkboxlist is an array of the item values.
  79.      * You can assign to this in your templates like:
  80.      * <code>
  81.      *   <!--[pnformcheckboxlist selectedValue=B]-->
  82.      * </code>
  83.      * But in your code you should use {@link pnFormCheckboxList::setSelectedValue()}
  84.      * and {@link pnFormCheckboxList::getSelectedValue()}.
  85.      * @var array 
  86.      */
  87.     var $selectedValue;
  88.  
  89.     /**
  90.     * HTML input name for this plugin. Defaults to the ID of the plugin.
  91.     * @var string 
  92.     */
  93.     var $inputName;
  94.  
  95.     /**
  96.     * Number of columns to display checkboxes in
  97.     * @var int 
  98.     */
  99.     var $repeatColumns;
  100.  
  101.     /**
  102.     * Width of each checkbox list item (combination of checkbox and label).
  103.     * @var string Width including CSS unit (for instance "200px")
  104.     */
  105.     var $repeatWidth;
  106.  
  107.     /**
  108.     * Enable saving of selected values as a colon delimited string
  109.     * 
  110.     * Enable this to save the selected values as a single string instead of
  111.     * an array of selected values. The result is a colon separated string
  112.     * like ":10:20:30".
  113.     * @var bool 
  114.     */
  115.     var $saveAsString;
  116.  
  117.  
  118.     function getFilename()
  119.     {
  120.         return __FILE__;
  121.     }
  122.  
  123.  
  124.     function create(&$render$params)
  125.     {
  126.         parent::create($render$params);
  127.     }
  128.  
  129.  
  130.     function load(&$render&$params)
  131.     {
  132.         parent::load($render$params);
  133.  
  134.         if (array_key_exists('selectedValue'$params))
  135.             $this->setSelectedValue($params['selectedValue']);
  136.     }
  137.  
  138.  
  139.     function render(&$render)
  140.     {
  141.         $readOnlyHtml ($this->readOnly " disabled=\"disabled\"" '');
  142.  
  143.         $class '';
  144.         if ($this->readOnly{
  145.             $class .= ' readonly';
  146.         }
  147.         if ($this->cssClass != null{
  148.             $class .= ' ' $this->cssClass;
  149.         }
  150.  
  151.         $classHtml ($class == '' '' " class=\"$class\"");
  152.         $nameHtml " name=\"{$this->inputName}[]\"";
  153.  
  154.         $selectedByValue = array();
  155.         if (is_array($this->selectedValue)) {
  156.             foreach ($this->selectedValue as $v)
  157.                 $selectedByValue[$v1;
  158.         }
  159.  
  160.         $result = '<div class="checkboxlist pn-clearfix">';
  161.         if ($this->repeatColumns > 0)
  162.             $result .= '<table>';
  163.  
  164.         for ($i=0,$count=count($this->items)$i<$count++$i)
  165.         {
  166.             if ($this->repeatColumns > && ($i $this->repeatColumns== 0)
  167.                 $result .= '<tr>';
  168.  
  169.             $item &$this->items[$i];
  170.             $idHtml " id=\"{$this->id}_$i\"";
  171.  
  172.  
  173.             $text = DataUtil::formatForDisplay($item['text']);
  174.  
  175.             if ($item['value'] === null)
  176.                 $value = '#null#';
  177.             else
  178.                 $value = DataUtil::formatForDisplay($item['value']);
  179.  
  180.             if (isset($selectedByValue[$value]) && $selectedByValue[$value])
  181.                 $selected = ' checked="checked"';
  182.             else
  183.                 $selected = '';
  184.  
  185.             if ($this->repeatColumns > 0)
  186.                 $result .= '<td>';
  187.  
  188.             if (!empty($this->repeatWidth))
  189.                 $style " style=\"width: $this->repeatWidth\"";
  190.             else
  191.                 $style = '';
  192.  
  193.             $result .= "<div class=\"checkboxlistitem\"$style>";
  194.             $result .= "<input type=\"checkbox\" value=\"$value\"{$selected}{$idHtml}{$nameHtml}{$readOnlyHtml}{$classHtml}/> ";
  195.             $result .= "<label for=\"{$this->id}_$i\">$text</label>\n";
  196.             $result .= '</div>';
  197.  
  198.             if ($this->repeatColumns > 0)
  199.                 $result .= '</td>';
  200.  
  201.             if ($this->repeatColumns > && ($i $this->repeatColumns== $this->repeatColumns-1)
  202.                 $result .= '</tr>';
  203.         }
  204.  
  205.         if ($this->repeatColumns > && $i $this->repeatColumns != 0)
  206.             $result .= '</tr>';
  207.  
  208.         if ($this->repeatColumns > 0)
  209.             $result .= '</table>';
  210.  
  211.         $result .= '</div>';
  212.  
  213.         return $result;
  214.     }
  215.  
  216.  
  217.     function decode(&$render)
  218.     {
  219.         // Do not read new value if readonly (evil submiter might have forged it)
  220.         // Besides that, a disabled checkbox returns nothing at all, so old values are good to keep
  221.         if (!$this->readOnly)
  222.         {
  223.             $value = FormUtil::getPassedValue($this->inputNamenull'POST');
  224.             if ($value == null)
  225.                 $value array();
  226.             for ($i=0,$count=count($value)$i<$count++$i)
  227.               $value[$i($value[$i== '#null#' null $value[$i]);
  228.  
  229.             $this->setSelectedValue($value);
  230.         }
  231.     }
  232.  
  233.  
  234.     function validate(&$render)
  235.     {
  236.         $this->clearValidation($render);
  237.  
  238.         if ($this->mandatory  &&  $this->selectedIndex <= 0)
  239.         {
  240.             $this->setError(_PNFORM_MANDATORYSELECTERROR);
  241.         }
  242.     }
  243.  
  244.  
  245.     function setError($msg)
  246.     {
  247.         $this->isValid false;
  248.         $this->errorMessage $msg;
  249.     }
  250.  
  251.  
  252.     function clearValidation(&$render)
  253.     {
  254.         $this->isValid true;
  255.         $this->errorMessage null;
  256.     }
  257.  
  258.  
  259.     function saveValue(&$render, &$data)
  260.     {
  261.         if ($this->dataBased)
  262.         {
  263.             if ($this->group == null)
  264.             {
  265.                 $data[$this->dataField$this->getSelectedValue();
  266.             }
  267.             else
  268.             {
  269.                 if (!array_key_exists($this->group$data))
  270.                     $data[$this->grouparray();
  271.                 $data[$this->group][$this->dataField$this->getSelectedValue();
  272.             }
  273.         }
  274.     }
  275.  
  276.  
  277.     // Called internally by the plugin itself to load values from the render.
  278.     // Can also by called when some one is calling the render object's pnFormSetValues
  279.     function loadValue(&$render, &$values)
  280.     {
  281.         if ($this->dataBased)
  282.         {
  283.             $items = null;
  284.             $value = null;
  285.  
  286.             if ($this->group == null)
  287.             {
  288.                 if ($this->dataField != null  &&  isset($values[$this->dataField]))
  289.                     $value $values[$this->dataField];
  290.                 if ($this->itemsDataField != null  &&  isset($values[$this->itemsDataField])) 
  291.                     $items $values[$this->itemsDataField];
  292.             }
  293.             else
  294.             {
  295.                 if (isset($values[$this->group]))
  296.                 {
  297.                     $data = $values[$this->group];
  298.                     if (isset($data[$this->dataField]))
  299.                     {
  300.                         $value = $data[$this->dataField];
  301.                         if ($this->itemsDataField != null  &&  isset($data[$this->itemsDataField]))
  302.                             $items $data[$this->itemsDataField];
  303.                     }
  304.                 }
  305.             }
  306.  
  307.             if ($items != null)
  308.                 $this->setItems($items);
  309.  
  310.             $this->setSelectedValue($value);
  311.         }
  312.     }
  313.  
  314.  
  315.     function setSelectedValue($value)
  316.     {
  317.         if (is_string($value))
  318.             $value = split(':', $value);
  319.         else if (!is_array($value))
  320.             $value = array($value);
  321.         $this->selectedValue = $value;
  322.     }
  323.  
  324.  
  325.     function getSelectedValue()
  326.     {
  327.  
  328.         if ($this->saveAsString)
  329.         {
  330.           $s = '';
  331.           for ($i=0,$count=count($this->selectedValue)$i<$count++$i)
  332.             $s .= (empty($s'' ':'$this->selectedValue[$i];
  333.           return $s;
  334.         }
  335.  
  336.         return $this->selectedValue;
  337.     }
  338. }
  339.  
  340.  
  341.  
  342.  
  343. function smarty_function_pnformcheckboxlist($params, &$render)
  344. {
  345.     return $render->pnFormRegisterPlugin('pnFormCheckboxList'$params);

Documentation generated on Fri, 18 Jul 2008 21:45:45 +0200 by phpDocumentor 1.4.1