Source for file function.pnformdropdownlist.php
Documentation is available at function.pnformdropdownlist.php
* @copyright (c) 2006, Zikula Development Team
* @link http://www.zikula.org
* @version $Id: function.pnformdropdownlist.php 24342 2008-06-06 12:03:14Z markwest $
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
* @package Zikula_Template_Plugins
* Renders an HTML <select> element with the supplied items.
* You can set the items directly like this:
* <!--[pnformdropdownlist id="mylist" items=$items]-->
* with the form event handler code like this:
* class mymodule_user_testHandler extends pnFormHandler
* function initialize(&$render)
* $items = array( array('text' => 'A', 'value' => '1'),
* array('text' => 'B', 'value' => '2'),
* array('text' => 'C', 'value' => '3') );
* $render->assign('items', $items); // Supply items
* $render->assign('mylist', 2); // Supply selected value
* Or you can set them indirectly using the plugin's databased features:
* <!--[pnformdropdownlist id="mylist"]-->
* with the form event handler code like this:
* class mymodule_user_testHandler extends pnFormHandler
* function initialize(&$render)
* $items = array( array('text' => 'A', 'value' => '1'),
* array('text' => 'B', 'value' => '2'),
* array('text' => 'C', 'value' => '3') );
* $render->assign('mylistItems', $items); // Supply items
* $render->assign('mylist', 2); // Supply selected value
* Selected index is zero based. Selected value is a string - and the PHP null
* value is also a valid value.
* Option groups can be added by setting an 'optgroup' attribute on each item.
* class mymodule_user_testHandler extends pnFormHandler
* function initialize(&$render)
* $items = array( array('text' => 'A', 'value' => '1', 'optgroup' => 'AAA'),
* array('text' => 'B', 'value' => '2', 'optgroup' => 'BBB'),
* array('text' => 'C', 'value' => '3', 'optgroup' => 'CCC') );
* $render->assign('mylistItems', $items); // Supply items
* $render->assign('mylist', 2); // Supply selected value
* You can also encourage reuse of dropdown lists by inheriting from
* the dropdown list into a specialized list a'la MyCategorySelector or
* MyColorSelector, and then use this plugin where ever you want
* a category or color selector. In this way you don't have to remember
* to assign the items to the render every time you need such a selector.
* In these plugins you must set the items in the load event handler.
* See {@link pnFormLanguageSelector} for a good example of how this
* You can assign to this in your templates like:
* <!--[pnformdropdownlist selectedValue=B]-->
* But in your code you should use {@link pnFormDropdownList::setSelectedValue()}
* and {@link pnFormDropdownList::getSelectedValue()}.
* Selected value is an array of values if you have set selectionMode=multiple.
* You can assign to this in your templates like:
* <!--[pnformdropdownlist selectedIndex=2]-->
* But in your code you should use {@link pnFormDropdownList::setSelectedIndex()}
* and {@link pnFormDropdownList::getSelectedIndex()}.
* Select index is not valid when selectionMode=multiple.
* @var int Zero based index
* Enable or disable auto postback
* Auto postback means "generate a server side event when selected index changes".
* If enabled then the event handler named in $onSelectedIndexChanged will be fired
* in the main form event handler.
* Sets selection mode to either single item selection (standard dropdown) or
* multiple item selection.
* @var string Possible values are 'single' and 'multiple'
* This corresponds to the "size" attribute of the HTML <select> element.
* Enable saving of multiple selected values as a colon delimited string
* Enable this to save the selected values as a single string instead of
* an array of selected values. The result is a colon separated string
* Name of selected index changed method
* @var string Default is "handleSelectedIndexChanged"
return __FILE__ ; // FIXME: should be found in smarty's data???
function create(&$render, $params)
parent::create($render, $params);
function load(&$render, &$params)
parent::load($render, $params);
// If someone decided to set selected value from the template then try to "set it for real"
// (meaning: set also selected Index) - after the items, potentially, have been loaded.
$idHtml = $this->getIdHtml();
$nameHtml = " name=\"{$this->inputName}[]\" ";
$readOnlyHtml = ($this->readOnly ? " disabled=\"disabled\"" : '');
if ($this->cssClass != null) {
$class .= ' ' . $this->cssClass;
$classHtml = ($class == '' ? '' : " class=\"$class\"");
$sizeHtml = ($this->size == null ? '' : " size=\"$this->size\" ");
$postbackHtml = " onchange=\"" . $render->pnFormGetPostBackEventReference($this, '') . "\"";
$multipleHtml = " multiple=\"multiple\"";
$attributes = $this->renderAttributes($render);
$result = "<select{ $idHtml}{ $nameHtml}{ $readOnlyHtml}{ $classHtml}{ $postbackHtml}{ $multipleHtml}{ $sizeHtml}{ $attributes}>\n ";
foreach ($this->items as $item)
$optgroup = (isset($item['optgroup']) ? $item['optgroup'] : null);
if ($optgroup != $currentOptGroup)
if ($currentOptGroup != null)
$result .= "</optgroup>\n";
$result .= "<optgroup label=\"" . DataUtil::formatForDisplay($optgroup) ."\">\n";
$currentOptGroup = $optgroup;
$text = DataUtil::formatForDisplay($item['text']);
if ($item['value'] === null)
$value = DataUtil::formatForDisplay($item['value']);
$selected = ' selected="selected"';
$selected = ' selected="selected"';
$result .= "<option value=\"$value\"{ $selected}> $text</option>\n ";
if ($currentOptGroup != null)
$result .= "</optgroup>\n";
$result .= "</select>\n";
function raisePostBackEvent(&$render, $eventArgument)
$args = array('commandName' => null, 'commandArgument' => null);
function decode(&$render)
// Do not read new value if readonly (evil submiter might have forged it)
$value = FormUtil::getPassedValue($this->inputName, null, 'POST');
for ($i= 0,$count= count($value); $i< $count; ++ $i)
if (get_magic_quotes_gpc())
$value[$i] = stripslashes($value[$i]);
if ($value[$i] == '#null#')
function validate(&$render)
$this->clearValidation($render);
$this->setError(_PNFORM_MANDATORYSELECTERROR);
function setSelectedValue($value)
// Check for exiting value in list (avoid tampering with post values)
for ($i=0,$count=count($this->items); $i< $count; ++ $i)
$item = &$this->items[$i];
if ($item['value'] == $value)
$value = split(':', $value);
for ($j=0,$count=count($value); $j<$count; ++$j)
// Check for exiting value in list (avoid tampering with post values)
for ($i=0,$count=count($this->items); $i< $count; ++ $i)
$item = &$this->items[$i];
if ($item['value'] == $value[$j])
function getSelectedValue()
function setSelectedIndex($index)
if ($index >=0 && $index <count($this->items))
function getSelectedIndex()
function smarty_function_pnformdropdownlist($params, &$render)
return $render->pnFormRegisterPlugin('pnFormDropdownList', $params);
|