Source for file function.pnformcheckboxlist.php
Documentation is available at function.pnformcheckboxlist.php
* @copyright (c) 2006, Zikula Development Team
* @link http://www.zikula.org
* @version $Id: function.pnformdropdownlist.php 22138 2007-06-01 10:19:14Z markwest $
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
* @package Zikula_Template_Plugins
* Renders a list of checkboxes with the supplied items.
* Usefull for selecting multiple items.
* You can set the items directly like this:
* <!--[pnformcheckboxlist 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:
* <!--[pnformchekboxlist 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
* The resulting dataset is a list of strings representing the selected
* values. So when you do a $data = $render->pnFormGetValues(); you will
* get a dataset like this:
* array('xxx' => 'valueXX',
* 'checkboxes' => array('15','17','22','34'),
* The selected value(s) of a checkboxlist is an array of the item values.
* You can assign to this in your templates like:
* <!--[pnformcheckboxlist selectedValue=B]-->
* But in your code you should use {@link pnFormCheckboxList::setSelectedValue()}
* and {@link pnFormCheckboxList::getSelectedValue()}.
* HTML input name for this plugin. Defaults to the ID of the plugin.
* Number of columns to display checkboxes in
* Width of each checkbox list item (combination of checkbox and label).
* @var string Width including CSS unit (for instance "200px")
* Enable saving of 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
function create(&$render, $params)
parent::create($render, $params);
function load(&$render, &$params)
parent::load($render, $params);
$readOnlyHtml = ($this->readOnly ? " disabled=\"disabled\"" : '');
if ($this->cssClass != null) {
$class .= ' ' . $this->cssClass;
$classHtml = ($class == '' ? '' : " class=\"$class\"");
$nameHtml = " name=\"{$this->inputName}[]\" ";
$selectedByValue = array();
$selectedByValue[$v] = 1;
$result = '<div class="checkboxlist pn-clearfix">';
for ($i= 0,$count= count($this->items); $i< $count; ++ $i)
$item = &$this->items[$i];
$idHtml = " id=\"{ $this->id}_ $i\" ";
$text = DataUtil::formatForDisplay($item['text']);
if ($item['value'] === null)
$value = DataUtil::formatForDisplay($item['value']);
if (isset($selectedByValue[$value]) && $selectedByValue[$value])
$selected = ' checked="checked"';
$result .= "<div class=\"checkboxlistitem\"$style>";
$result .= "<input type=\"checkbox\" value=\"$value\"{ $selected}{ $idHtml}{ $nameHtml}{ $readOnlyHtml}{ $classHtml}/> ";
$result .= "<label for=\"{ $this->id}_ $i\"> $text</label>\n ";
function decode(&$render)
// Do not read new value if readonly (evil submiter might have forged it)
// Besides that, a disabled checkbox returns nothing at all, so old values are good to keep
$value = FormUtil::getPassedValue($this->inputName, null, 'POST');
for ($i= 0,$count= count($value); $i< $count; ++ $i)
$value[$i] = ($value[$i] == '#null#' ? null : $value[$i]);
function validate(&$render)
if ($this->mandatory && $this->selectedIndex <= 0)
$this->setError(_PNFORM_MANDATORYSELECTERROR);
$this->errorMessage = $msg;
function clearValidation(&$render)
$this->errorMessage = null;
function saveValue(&$render, &$data)
if ($this->group == null)
if (!array_key_exists($this->group, $data))
$data[$this->group] = array();
// Called internally by the plugin itself to load values from the render.
// Can also by called when some one is calling the render object's pnFormSetValues
function loadValue(&$render, &$values)
if ($this->group == null)
if ($this->dataField != null && isset ($values[$this->dataField]))
$value = $values[$this->dataField];
if ($this->itemsDataField != null && isset ($values[$this->itemsDataField]))
$items = $values[$this->itemsDataField];
if (isset($values[$this->group]))
$data = $values[$this->group];
if (isset ($data[$this->dataField]))
$value = $data[$this->dataField];
if ($this->itemsDataField != null && isset ($data[$this->itemsDataField]))
$items = $data[$this->itemsDataField];
function setSelectedValue($value)
$value = split(':', $value);
else if (!is_array($value))
function getSelectedValue()
function smarty_function_pnformcheckboxlist($params, &$render)
return $render->pnFormRegisterPlugin('pnFormCheckboxList', $params);
|