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

Source for file function.pnformradiobutton.php

Documentation is available at function.pnformradiobutton.php

  1. <?php
  2. /**
  3.  * Radio button plugin
  4.  *
  5.  * @copyright (c) 2006, Zikula Development Team
  6.  * @link http://www.zikula.org
  7.  * @version $Id: function.pnformcheckbox.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. /**
  15.  * Radiobutton plugin
  16.  *
  17.  * Plugin to generate a radiobutton for selecting one-of-X.
  18.  * Usage with fixed number of radiobuttons:
  19.  *
  20.  * <code>
  21.  * <!--[pnformradiobutton id=yesButton dataField=ok]--> <!--[pnformlabel text=Yes for=yesButton]--> <br/>
  22.  * <!--[pnformradiobutton id=noButton dataField=ok]--> <!--[pnformlabel text=No for=noButton]-->
  23.  * </code>
  24.  *
  25.  * The above case sets 'ok' to either 'yesButton' or 'noButton' in the hashtable returned
  26.  * by {@link pnFormRender::pnFormGetValues()}. As you can see the radiobutton defaults to using the ID for the returned value
  27.  * in the hashtable. You can override this by setting 'value' to something different.
  28.  *
  29.  * You can also enforce a selection:
  30.  *
  31.  * <code>
  32.  * <!--[pnformradiobutton id=yesButton dataField=ok mandatory=1]--> <!--[pnformlabel text=Yes for=yesButton]--> <br/>
  33.  * <!--[pnformradiobutton id=noButton dataField=ok mandatory=1]--> <!--[pnformlabel text=No for=noButton]-->
  34.  * </code>
  35.  *
  36.  * If you have a list of radiobuttons inside a for/each loop then you can set the ID to something from the data loop
  37.  * like here:
  38.  * <code>
  39.  * <!--[foreach from=$items item=item]-->
  40.  *   <!--[pnformradiobutton id=$item.name dataField=item mandatory=true]--> <!--[pnformlabel text=$item.title for=$item.name]-->
  41.  * <!--[/foreach]-->
  42.  * </code>
  43.  *
  44.  * @package pnForm
  45.  * @subpackage Plugins
  46.  */
  47. {
  48.     /**
  49.      * Value
  50.      *
  51.      * The value returned in pnFormGetValues() when this radio button is checked.
  52.      * @var string 
  53.      */
  54.     var $value;
  55.  
  56.     /**
  57.      * Checked
  58.      *
  59.      * The current state of the radio button
  60.      * @var bool 
  61.      */
  62.     var $checked;
  63.  
  64.     /**
  65.      * Enable or disable read only mode
  66.      */
  67.     var $readOnly;
  68.  
  69.     /**
  70.      * Data field name for looking up initial data
  71.      *
  72.      * The name stored here is used to lookup initial data for the plugin in the render's variables.
  73.      * Defaults to the ID of the plugin. See also tutorials on the Zikula site.
  74.      * @var string 
  75.      */
  76.     var $dataField;
  77.  
  78.     /**
  79.      * Enable or disable use of $dataField
  80.      * @var bool 
  81.      */
  82.     var $dataBased;
  83.  
  84.     /**
  85.      * Group name for this input
  86.      *
  87.      * The group name is used to locate data in the render (when databased) and to restrict which
  88.      * plugins to do validation on (to be implemented).
  89.      * @see pnFormRender::pnFormGetValues()
  90.      * @see pnFormRender::pnFormIsValid()
  91.      * @var string 
  92.      */
  93.     var $group;
  94.  
  95.     /**
  96.     * Radiobutton selection group name
  97.     * @var string 
  98.     */
  99.     var $groupName;
  100.  
  101.     /**
  102.      * Validation indicator used by the framework.
  103.      *
  104.      * The true/false value of this variable indicates whether or not radiobutton selection is valid
  105.      * (a valid (set of) radiobuttons satisfies the mandatory requirement).
  106.      * Use {@link pnFormRadioButton::setError()} and {@link pnFormRadioButton::clearValidation()}
  107.      * to change the value.
  108.      * @var bool 
  109.      */
  110.     var $isValid = true;
  111.  
  112.     /**
  113.      * Enable or disable mandatory check
  114.      *
  115.      * By enabling mandatory checking you force the user to check one of the radio buttons on the page
  116.      * that shares the same groupName.
  117.      * @var bool 
  118.      */
  119.     var $mandatory;
  120.  
  121.     /**
  122.      * Enable or disable auto postback
  123.      *
  124.      * Auto postback means "generate a server side event when selection changes".
  125.      * If enabled then the event handler named in $onSelectedIndexChanged will be fired
  126.      * in the main form event handler.
  127.      * @var bool 
  128.      */
  129.     var $autoPostBack;
  130.  
  131.     /**
  132.      * Name of checked changed method
  133.      *
  134.      * @var string Default is "handleCheckedChanged"
  135.      */
  136.     var $onCheckedChanged = 'handleCheckedChanged';
  137.  
  138.     /**
  139.      * Error message to display when input does not validate
  140.      *
  141.      * Use {@link pnFormRadioButton::setError()} and {@link pnFormRadioButton::clearValidation()}
  142.      * to change the value.
  143.      * @var string 
  144.      */
  145.     var $errorMessage;
  146.  
  147.     /**
  148.      * Text label for this plugin
  149.      *
  150.      * This variable contains the label text for the radiobutton. The {@link pnFormLabel} plugin will set
  151.      * this text automatically when it is a label for this input.
  152.      * @var string 
  153.      */
  154.     var $myLabel;
  155.  
  156.  
  157.     var $validationChecked = false;
  158.  
  159.  
  160.     function getFilename()
  161.     {
  162.         return __FILE__// FIXME: should be found in smarty's data???
  163.     }
  164.  
  165.  
  166.     function create(&$render$params)
  167.     {
  168.         // Load all special and non-string parameters
  169.         // - the rest are fetched automatically
  170.  
  171.         $this->checked = (array_key_exists('checked'$params$params['checked'false);
  172.  
  173.         $this->readOnly = (array_key_exists('readOnly'$params$params['readOnly'false);
  174.  
  175.         $this->dataBased = (array_key_exists('dataBased'$params$params['dataBased'true);
  176.         $this->value = (string)(array_key_exists('value'$params$params['value'$this->id);
  177.         $this->groupName = (array_key_exists('groupName'$params$params['groupName'$this->dataField);
  178.     }
  179.  
  180.  
  181.     function load(&$render&$params)
  182.     {
  183.         $this->loadValue($render$render->get_template_vars());
  184.     }
  185.  
  186.  
  187.     function loadValue(&$render&$values)
  188.     {
  189.         if ($this->dataBased)
  190.         {
  191.             $value null;
  192.  
  193.             if ($this->group == null)
  194.             {
  195.                 if (array_key_exists($this->dataField$values))
  196.                     $value = (string)$values[$this->dataField];
  197.             }
  198.             else
  199.             {
  200.                 if (array_key_exists($this->group$values&& array_key_exists($this->dataField$values[$this->group]))
  201.                 {
  202.                     $value = (string)$values[$this->group][$this->dataField];
  203.                 }
  204.             }
  205.  
  206.             if ($value !== null)
  207.                 $this->checked = ($this->value === $value);
  208.             else
  209.                 $this->checked = false;
  210.         }
  211.     }
  212.  
  213.  
  214.     function initialize(&$render)
  215.     {
  216.         $render->pnFormAddValidator($this);
  217.     }
  218.  
  219.  
  220.     function render(&$render)
  221.     {
  222.         $idHtml $this->getIdHtml();
  223.  
  224.         $nameHtml " name=\"{$this->groupName}\"";
  225.         $readOnlyHtml = ($this->readOnly ? " disabled=\"disabled\"" '');
  226.         $checkedHtml ($this->checked ? " checked=\"checked\"" '');
  227.  
  228.         $postbackHtml '';
  229.         if ($this->autoPostBack)
  230.         {
  231.             $postbackHtml = " onclick=\"" . $render->pnFormGetPostBackEventReference($this''"\"";
  232.         }
  233.  
  234.         $attributes = $this->renderAttributes($render);
  235.  
  236.         $result "<input type=\"radio\" value=\"{$this->value}\" class=\"radio\"{$idHtml}{$nameHtml}{$readOnlyHtml}{$checkedHtml}{$postbackHtml}{$attributes}/>";
  237.  
  238.         return $result;
  239.     }
  240.  
  241.  
  242.     function raisePostBackEvent(&$render, $eventArgument)
  243.     {
  244.         $args = array('commandName' => null, 'commandArgument' => null);
  245.         if (!empty($this->onCheckedChanged))
  246.             $render->pnFormRaiseEvent($this->onCheckedChanged$args);
  247.     }
  248.  
  249.  
  250.     function decode(&$render)
  251.     {
  252.         // Do not read new value if readonly (evil submiter might have forged it)
  253.         if (!$this->readOnly)
  254.             $this->checked = (FormUtil::getPassedValue($this->groupNamenull'POST'=== $this->value ? true false);
  255.     }
  256.  
  257.  
  258.     function validate(&$render)
  259.     {
  260.         $this->clearValidation($render);
  261.  
  262.         if ($this->mandatory && !$this->validationChecked)
  263.         {
  264.             $firstRadioButton = null;
  265.             if (!$this->findCheckedRadioButton($render$firstRadioButton))
  266.             {
  267.                 $this->setError(_PNFORM_MANDATORYSELECTERROR);
  268.             }
  269.         }
  270.     }
  271.  
  272.  
  273.     function findCheckedRadioButton(&$render, &$firstRadioButton)
  274.     {
  275.         $lim = count($render->pnFormPlugins);
  276.         for ($i=0$i<$lim++$i)
  277.         {
  278.             if ($this->findCheckedRadioButton_rec($firstRadioButton$render->pnFormPlugins[$i]))
  279.                 return true;
  280.         }
  281.         return false;
  282.     }
  283.  
  284.  
  285.     function findCheckedRadioButton_rec(&$firstRadioButton, &$plugin)
  286.     {
  287.         if (is_a($plugin, 'pnFormRadioButton') && $plugin->groupName == $this->groupName)
  288.         {
  289.             $plugin->validationChecked true;
  290.             if ($firstRadioButton == null)
  291.                 $firstRadioButton $plugin;
  292.             if ($plugin->checked)
  293.                 return true;
  294.         }
  295.  
  296.         $lim = count($plugin->plugins);
  297.         for ($i=0$i<$lim++$i)
  298.         {
  299.             if ($this->findCheckedRadioButton_rec($firstRadioButton$plugin->plugins[$i]))
  300.                 return true;
  301.         }
  302.         return false;
  303.     }
  304.  
  305.  
  306.     function setError($msg)
  307.     {
  308.         $this->isValid = false;
  309.         $this->errorMessage = $msg;
  310.     }
  311.  
  312.  
  313.     function clearValidation(&$render)
  314.     {
  315.         $this->isValid = true;
  316.         $this->errorMessage = null;
  317.     }
  318.  
  319.  
  320.     function saveValue(&$render, &$data)
  321.     {
  322.         if ($this->dataBased)
  323.         {
  324.             if ($this->group == null)
  325.             {
  326.                 if ($this->checked)
  327.                     $data[$this->dataField$this->value;
  328.             }
  329.             else
  330.             {
  331.                 if ($this->checked)
  332.                 {
  333.                     if (!array_key_exists($this->group$data))
  334.                         $data[$this->grouparray();
  335.                     $data[$this->group][$this->dataField$this->value;
  336.                 }
  337.             }
  338.         }
  339.     }
  340. }
  341.  
  342.  
  343. function smarty_function_pnformradiobutton($params, &$render)
  344. {
  345.     return $render->pnFormRegisterPlugin('pnFormRadioButton'$params);

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