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

Source for file function.pnformcheckbox.php

Documentation is available at function.pnformcheckbox.php

  1. <?php
  2. /**
  3.  * Checkbox plugin
  4.  *
  5.  * @copyright (c) 2006, Zikula Development Team
  6.  * @link http://www.zikula.org
  7.  * @version $Id: function.pnformcheckbox.php 24342 2008-06-06 12:03: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.  * Check box plugin
  16.  *
  17.  * Plugin to generate a checkbox for true/false selection.
  18.  *
  19.  * @package pnForm
  20.  * @subpackage Plugins
  21.  */
  22. {
  23.     /**
  24.      * Checked value
  25.      *
  26.      * Set to true when checkbox is checked, false otherwise.
  27.      * @var bool 
  28.      */
  29.     var $checked;
  30.  
  31.     /**
  32.      * Enable or disable read only mode
  33.      */
  34.     var $readOnly;
  35.  
  36.     /**
  37.      * Data field name for looking up initial data
  38.      *
  39.      * The name stored here is used to lookup initial data for the plugin in the render's variables.
  40.      * Defaults to the ID of the plugin. See also tutorials on the Zikula site.
  41.      * @var string 
  42.      */
  43.     var $dataField;
  44.  
  45.     /**
  46.      * Enable or disable use of $dataField
  47.      * @var bool 
  48.      */
  49.     var $dataBased;
  50.  
  51.     /**
  52.      * Group name for this input
  53.      *
  54.      * The group name is used to locate data in the render (when databased) and to restrict which
  55.      * plugins to do validation on (to be implemented).
  56.      * @see pnFormRender::pnFormGetValues()
  57.      * @see pnFormRender::pnFormIsValid()
  58.      * @var string 
  59.      */
  60.     var $group;
  61.  
  62.     /**
  63.     * HTML input name for this plugin. Defaults to the ID of the plugin.
  64.     * @var string 
  65.     */
  66.     var $inputName;
  67.  
  68.  
  69.     function getFilename()
  70.     {
  71.         return __FILE__// FIXME: should be found in smarty's data???
  72.     }
  73.  
  74.  
  75.     function create(&$render$params)
  76.     {
  77.         // Load all special and non-string parameters
  78.         // - the rest are fetched automatically
  79.  
  80.         $this->checked = (array_key_exists('checked'$params$params['checked'false);
  81.  
  82.         $this->inputName = (array_key_exists('inputName'$params$params['inputName'$this->id);
  83.         $this->readOnly = (array_key_exists('readOnly'$params$params['readOnly'false);
  84.  
  85.         $this->dataBased = (array_key_exists('dataBased'$params$params['dataBased'true);
  86.         $this->dataField = (array_key_exists('dataField'$params$params['dataField'$this->id);
  87.     }
  88.  
  89.  
  90.     function load(&$render&$params)
  91.     {
  92.         $this->loadValue($render$render->get_template_vars());
  93.     }
  94.  
  95.  
  96.     function loadValue(&$render&$values)
  97.     {
  98.         if ($this->dataBased)
  99.         {
  100.             $value null;
  101.  
  102.             if ($this->group == null)
  103.             {
  104.                 if (array_key_exists($this->dataField$values))
  105.                     $value $values[$this->dataField];
  106.             }
  107.             else
  108.             {
  109.                 if (array_key_exists($this->group$values))
  110.                 {
  111.                     $value $values[$this->group][$this->dataField];
  112.                 }
  113.             }
  114.  
  115.             if ($value !== null)
  116.                 $this->checked = $value;
  117.         }
  118.     }
  119.  
  120.  
  121.     function render(&$render)
  122.     {
  123.         $idHtml $this->getIdHtml();
  124.  
  125.         $nameHtml " name=\"{$this->inputName}\"";
  126.         $readOnlyHtml = ($this->readOnly ? " disabled=\"disabled\"" '');
  127.         $checkedHtml ($this->checked ? " checked=\"checked\"" '');
  128.  
  129.         $attributes $this->renderAttributes($render);
  130.  
  131.         $result "<input type=\"checkbox\" value=\"1\" class=\"cbx\"{$idHtml}{$nameHtml}{$readOnlyHtml}{$checkedHtml}{$attributes}/>";
  132.  
  133.         return $result;
  134.     }
  135.  
  136.  
  137.     function decode(&$render)
  138.     {
  139.         // Do not read new value if readonly (evil submiter might have forged it)
  140.         if (!$this->readOnly)
  141.             $this->checked = (FormUtil::getPassedValue($this->inputNamenull'REQUEST'== null false true);
  142.     }
  143.  
  144.  
  145.     function saveValue(&$render, &$data)
  146.     {
  147.         if ($this->dataBased)
  148.         {
  149.             if ($this->group == null)
  150.             {
  151.                 $data[$this->dataField$this->checked;
  152.             }
  153.             else
  154.             {
  155.                 if (!array_key_exists($this->group$data))
  156.                     $data[$this->grouparray();
  157.                 $data[$this->group][$this->dataField$this->checked;
  158.             }
  159.         }
  160.     }
  161. }
  162.  
  163.  
  164. function smarty_function_pnformcheckbox($params, &$render)
  165. {
  166.     return $render->pnFormRegisterPlugin('pnFormCheckbox'$params);

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