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

Source for file function.pnformdropdownlist.php

Documentation is available at function.pnformdropdownlist.php

  1. <?php
  2.  
  3. Loader::requireOnce('system/pnForm/plugins/pnformbaselistselector.php');
  4.  
  5.  
  6. /**
  7.  * Dropdown list plugin
  8.  *
  9.  * @copyright (c) 2006, Zikula Development Team
  10.  * @link http://www.zikula.org
  11.  * @version $Id: function.pnformdropdownlist.php 24342 2008-06-06 12:03:14Z markwest $
  12.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  13.  * @author Jorn Wildt
  14.  * @package Zikula_Template_Plugins
  15.  * @subpackage Functions
  16.  */
  17.  
  18. /**
  19.  * Drop down list
  20.  *
  21.  * Renders an HTML <select> element with the supplied items.
  22.  *
  23.  * You can set the items directly like this:
  24.  * <code>
  25.  * <!--[pnformdropdownlist id="mylist" items=$items]-->
  26.  * </code>
  27.  * with the form event handler code like this:
  28.  * <code>
  29.  * class mymodule_user_testHandler extends pnFormHandler
  30.  * {
  31.  *   function initialize(&$render)
  32.  *   {
  33.  *       $items = array( array('text' => 'A', 'value' => '1'),
  34.  *                       array('text' => 'B', 'value' => '2'),
  35.  *                       array('text' => 'C', 'value' => '3') );
  36.  *
  37.  *       $render->assign('items', $items); // Supply items
  38.  *       $render->assign('mylist', 2);     // Supply selected value
  39.  *   }
  40.  * }
  41.  * </code>
  42.  * Or you can set them indirectly using the plugin's databased features:
  43.  * <code>
  44.  * <!--[pnformdropdownlist id="mylist"]-->
  45.  * </code>
  46.  * with the form event handler code like this:
  47.  * <code>
  48.  * class mymodule_user_testHandler extends pnFormHandler
  49.  * {
  50.  *   function initialize(&$render)
  51.  *   {
  52.  *       $items = array( array('text' => 'A', 'value' => '1'),
  53.  *                       array('text' => 'B', 'value' => '2'),
  54.  *                       array('text' => 'C', 'value' => '3') );
  55.  *
  56.  *       $render->assign('mylistItems', $items);  // Supply items
  57.  *       $render->assign('mylist', 2);            // Supply selected value
  58.  *   }
  59.  * }
  60.  * </code>
  61.  *
  62.  * Selected index is zero based. Selected value is a string - and the PHP null
  63.  * value is also a valid value.
  64.  *
  65.  * Option groups can be added by setting an 'optgroup' attribute on each item.
  66.  * For instance:
  67.  *
  68.  * <code>
  69.  * class mymodule_user_testHandler extends pnFormHandler
  70.  * {
  71.  *   function initialize(&$render)
  72.  *   {
  73.  *       $items = array( array('text' => 'A', 'value' => '1', 'optgroup' => 'AAA'),
  74.  *                       array('text' => 'B', 'value' => '2', 'optgroup' => 'BBB'),
  75.  *                       array('text' => 'C', 'value' => '3', 'optgroup' => 'CCC') );
  76.  *
  77.  *       $render->assign('mylistItems', $items);  // Supply items
  78.  *       $render->assign('mylist', 2);            // Supply selected value
  79.  *   }
  80.  * }
  81.  * </code>
  82.  *
  83.  * You can also encourage reuse of dropdown lists by inheriting from
  84.  * the dropdown list into a specialized list a'la MyCategorySelector or
  85.  * MyColorSelector, and then use this plugin where ever you want
  86.  * a category or color selector. In this way you don't have to remember
  87.  * to assign the items to the render every time you need such a selector.
  88.  * In these plugins you must set the items in the load event handler.
  89.  * See {@link pnFormLanguageSelector} for a good example of how this
  90.  * can be done.
  91.  *
  92.  * @package pnForm
  93.  * @subpackage Plugins
  94.  */
  95. {
  96.     /**
  97.      * Selected value
  98.      *
  99.      * You can assign to this in your templates like:
  100.      * <code>
  101.      *   <!--[pnformdropdownlist selectedValue=B]-->
  102.      * </code>
  103.      * But in your code you should use {@link pnFormDropdownList::setSelectedValue()}
  104.      * and {@link pnFormDropdownList::getSelectedValue()}.
  105.      *
  106.      * Selected value is an array of values if you have set selectionMode=multiple.
  107.      * @var mixed 
  108.      */
  109.     var $selectedValue;
  110.  
  111.     /**
  112.      * Selected item index
  113.      *
  114.      * You can assign to this in your templates like:
  115.      * <code>
  116.      *   <!--[pnformdropdownlist selectedIndex=2]-->
  117.      * </code>
  118.      * But in your code you should use {@link pnFormDropdownList::setSelectedIndex()}
  119.      * and {@link pnFormDropdownList::getSelectedIndex()}.
  120.      *
  121.      * Select index is not valid when selectionMode=multiple.
  122.      * @var int Zero based index
  123.      */
  124.     var $selectedIndex;
  125.  
  126.     /**
  127.      * Enable or disable auto postback
  128.      *
  129.      * Auto postback means "generate a server side event when selected index changes".
  130.      * If enabled then the event handler named in $onSelectedIndexChanged will be fired
  131.      * in the main form event handler.
  132.      * @var bool 
  133.      */
  134.     var $autoPostBack;
  135.  
  136.  
  137.     /**
  138.      * Selection mode
  139.      *
  140.      * Sets selection mode to either single item selection (standard dropdown) or
  141.      * multiple item selection.
  142.      * @var string Possible values are 'single' and 'multiple'
  143.      */
  144.     var $selectionMode = 'single';
  145.  
  146.     /**
  147.      * Size of dropdown
  148.      * 
  149.      * This corresponds to the "size" attribute of the HTML <select> element.
  150.      * @var int 
  151.      */
  152.     var $size = null;
  153.  
  154.     /**
  155.     * Enable saving of multiple selected values as a colon delimited string
  156.     * 
  157.     * Enable this to save the selected values as a single string instead of
  158.     * an array of selected values. The result is a colon separated string
  159.     * like ":10:20:30".
  160.     * @var bool 
  161.     */
  162.     var $saveAsString;
  163.  
  164.  
  165.     /**
  166.      * Name of selected index changed method
  167.      *
  168.      * @var string Default is "handleSelectedIndexChanged"
  169.      */
  170.     var $onSelectedIndexChanged = 'handleSelectedIndexChanged';
  171.  
  172.  
  173.     function getFilename()
  174.     {
  175.         return __FILE__// FIXME: should be found in smarty's data???
  176.     }
  177.  
  178.  
  179.     function create(&$render$params)
  180.     {
  181.         parent::create($render$params);
  182.         $this->selectedIndex = -1;
  183.     }
  184.  
  185.  
  186.     function load(&$render&$params)
  187.     {
  188.         parent::load($render$params);
  189.  
  190.         // If someone decided to set selected value from the template then try to "set it for real" 
  191.         // (meaning: set also selected Index) - after the items, potentially, have been loaded.
  192.         if (array_key_exists('selectedValue'$params))
  193.             $this->setSelectedValue($params['selectedValue']);
  194.         if (array_key_exists('selectedIndex'$params))
  195.             $this->setSelectedIndex($params['selectedIndex']);
  196.     }
  197.  
  198.  
  199.     function render(&$render)
  200.     {
  201.         $idHtml $this->getIdHtml();
  202.  
  203.         $nameHtml " name=\"{$this->inputName}[]\"";
  204.  
  205.         $readOnlyHtml = ($this->readOnly " disabled=\"disabled\"" '');
  206.  
  207.         $class '';
  208.         if (!$this->isValid{
  209.             $class .= ' error';
  210.         }
  211.         if ($this->readOnly{
  212.             $class .= ' readonly';
  213.         }
  214.         if ($this->cssClass != null{
  215.             $class .= ' ' . $this->cssClass;
  216.         }
  217.  
  218.         $classHtml = ($class == '' ? '' : " class=\"$class\"");
  219.  
  220.         $sizeHtml = ($this->size == null '' " size=\"$this->size\"");
  221.  
  222.         $postbackHtml = '';
  223.         if ($this->autoPostBack)
  224.         {
  225.             $postbackHtml = " onchange=\"" . $render->pnFormGetPostBackEventReference($this''"\"";
  226.         }
  227.  
  228.         $multipleHtml = '';
  229.         if ($this->selectionMode == 'multiple')
  230.             $multipleHtml " multiple=\"multiple\"";
  231.  
  232.         $attributes $this->renderAttributes($render);
  233.  
  234.         $result "<select{$idHtml}{$nameHtml}{$readOnlyHtml}{$classHtml}{$postbackHtml}{$multipleHtml}{$sizeHtml}{$attributes}>\n";
  235.         $currentOptGroup = null;
  236.         foreach ($this->items as $item)
  237.         {
  238.             $optgroup = (isset($item['optgroup']) ? $item['optgroup'] : null);
  239.             if ($optgroup != $currentOptGroup)
  240.             {
  241.                 if ($currentOptGroup != null)
  242.                     $result .= "</optgroup>\n";
  243.                 if ($optgroup != null)
  244.                     $result .= "<optgroup label=\"" . DataUtil::formatForDisplay($optgroup) ."\">\n";
  245.                 $currentOptGroup = $optgroup;
  246.             }
  247.  
  248.             $text = DataUtil::formatForDisplay($item['text']);
  249.  
  250.             if ($item['value'] === null)
  251.                 $value = '#null#';
  252.             else
  253.                 $value = DataUtil::formatForDisplay($item['value']);
  254.  
  255.             if ($this->selectionMode == 'single' &&  $value == $this->selectedValue)
  256.               $selected ' selected="selected"';
  257.             else if ($this->selectionMode == 'multiple' &&  in_array($value$this->selectedValue))
  258.               $selected ' selected="selected"';
  259.             else
  260.               $selected '';
  261.             $result .= "<option value=\"$value\"{$selected}>$text</option>\n";
  262.         }
  263.         if ($currentOptGroup != null)
  264.             $result .= "</optgroup>\n";
  265.         $result .= "</select>\n";
  266.  
  267.         return $result;
  268.     }
  269.  
  270.  
  271.     function raisePostBackEvent(&$render, $eventArgument)
  272.     {
  273.         $args = array('commandName' => null, 'commandArgument' => null);
  274.         if (!empty($this->onSelectedIndexChanged))
  275.             $render->pnFormRaiseEvent($this->onSelectedIndexChanged$args);
  276.     }
  277.  
  278.  
  279.     function decode(&$render)
  280.     {
  281.         // Do not read new value if readonly (evil submiter might have forged it)
  282.         if (!$this->readOnly)
  283.         {
  284.             $value = FormUtil::getPassedValue($this->inputNamenull'POST');
  285.             if ($value == null)
  286.                 $value array();
  287.  
  288.             for ($i=0,$count=count($value)$i<$count++$i)
  289.             {
  290.               if (get_magic_quotes_gpc())
  291.                   $value[$i] = stripslashes($value[$i]);
  292.               if ($value[$i] == '#null#')
  293.                   $value[$i] = null;
  294.             }
  295.             
  296.             if ($this->selectionMode == 'single')
  297.                 $value $value[0];
  298.  
  299.             $this->setSelectedValue($value);
  300.         }
  301.     }
  302.  
  303.  
  304.     function validate(&$render)
  305.     {
  306.         $this->clearValidation($render);
  307.  
  308.         if ($this->mandatory  &&  $this->selectedIndex <= 0)
  309.         {
  310.             $this->setError(_PNFORM_MANDATORYSELECTERROR);
  311.         }
  312.     }
  313.  
  314.  
  315.     function setSelectedValue($value)
  316.     {
  317.         if ($this->selectionMode == 'single')
  318.         {
  319.             // Check for exiting value in list (avoid tampering with post values)
  320.             for ($i=0,$count=count($this->items)$i<$count++$i)
  321.             {
  322.                 $item = &$this->items[$i];
  323.  
  324.                 if ($item['value'== $value)
  325.                 {
  326.                     $this->selectedValue = $value;
  327.                     $this->selectedIndex = $i;
  328.                 }
  329.             }
  330.         }
  331.         else
  332.         {
  333.             if (is_string($value))
  334.                 $value = split(':', $value);
  335.  
  336.             $ok = true;
  337.             for ($j=0,$count=count($value); $j<$count; ++$j)
  338.             {
  339.                 $ok2 = false;
  340.                 // Check for exiting value in list (avoid tampering with post values)
  341.                 for ($i=0,$count=count($this->items)$i<$count++$i)
  342.                 {
  343.                     $item = &$this->items[$i];
  344.  
  345.                     if ($item['value'== $value[$j])
  346.                         $ok2 true;
  347.                 }
  348.                 $ok = $ok && $ok2;
  349.             }
  350.             
  351.             if ($ok)
  352.             {
  353.                 $this->selectedValue = $value;
  354.                 $this->selectedIndex = 0;
  355.             }
  356.         }
  357.     }
  358.  
  359.  
  360.     function getSelectedValue()
  361.     {
  362.         if ($this->saveAsString)
  363.         {
  364.           $s = '';
  365.           for ($i=0,$count=count($this->selectedValue)$i<$count++$i)
  366.             $s .= (empty($s'' ':'$this->selectedValue[$i];
  367.           return $s;
  368.         }
  369.         
  370.         return $this->selectedValue;
  371.     }
  372.  
  373.  
  374.     function setSelectedIndex($index)
  375.     {
  376.         if ($index >=0  &&  $index <count($this->items))
  377.         {
  378.             $this->selectedValue = $this->items[$index]['value'];
  379.             $this->selectedIndex = $index;
  380.         }
  381.     }
  382.  
  383.  
  384.     function getSelectedIndex()
  385.     {
  386.         return $this->selectedIndex;
  387.     }
  388. }
  389.  
  390.  
  391.  
  392.  
  393. function smarty_function_pnformdropdownlist($params, &$render)
  394. {
  395.     return $render->pnFormRegisterPlugin('pnFormDropdownList'$params);

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