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

Source for file function.pnformcategoryselector.php

Documentation is available at function.pnformcategoryselector.php

  1. <?php
  2. /**
  3.  * Category selector plugin
  4.  *
  5.  * @copyright (c) 2006, Zikula Development Team
  6.  * @link http://www.zikula.org
  7.  * @version $Id: function.pnformtextinput.php 21046 2007-01-11 21:36:57Z jornlind $
  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. /** Make sure to use require_once() instead of Loader::requireOnce() since "function.pnformdropdownlist.php"
  15.  is loaded by Smarty (the base render class) with the use of require_once(). We do not want to
  16.  get in conflict with that.*/
  17. require_once 'system/pnForm/plugins/function.pnformdropdownlist.php';
  18.  
  19. /**
  20.  * Category selector
  21.  *
  22.  * This plugin creates a category selector using a dropdown list.
  23.  * The selected value of the base dropdown list will be set to ID of the selected category.
  24.  *
  25.  * @package pnForm
  26.  * @subpackage Plugins
  27.  */
  28. {
  29.     var $editLink;
  30.     var $category;
  31.  
  32.     /** 
  33.      * Enable inclusion of an empty null value element
  34.      * @var bool (default false)
  35.      */
  36.     var $includeEmptyElement;
  37.  
  38.     /**
  39.      * Enable save/load of values in separate __CATEGORIES_ field for use in DBUtil.
  40.      *
  41.      * If enabled then selected category is returned in a sub-array named __CATEGORIES__
  42.      * such that it can be used directly with DBUtils standard categorization of
  43.      * data items. Example code:
  44.      * <code>
  45.      * // Template: <!--[pnformcategoryselector id=myCat category=xxx enableDBUtil=1]-->
  46.      * // Result:
  47.      * array('title' => 'Item title',
  48.      *       '__CATEGORIES__' => array('myCat' => zzz)
  49.      *      )
  50.      * </code>
  51.      * @var bool (default false)
  52.      */
  53.     var $enableDBUtil;
  54.  
  55.  
  56.     function getFilename()
  57.     {
  58.         return __FILE__;
  59.     }
  60.  
  61.  
  62.     /* Shared by other category plugins */
  63.     /* static */ function loadParameters(&$list$includeEmptyElement$params)
  64.     {
  65.         $list->category     = isset($params['category'])         $params['category']         0;
  66.         $path               = isset($params['path'])             $params['path']             '';
  67.         $pathfield          = isset($params['pathfield'])        $params['pathfield']        'path';
  68.         $lang               = isset($params['lang'])             $params['lang']             pnUserGetLang();
  69.         $recurse            = isset($params['recurse'])          $params['recurse']          true;
  70.         $relative           = isset($params['relative'])         $params['relative']         true;
  71.         $includeRoot        = isset($params['includeRoot'])      $params['includeRoot']      false;
  72.         $includeLeaf        = isset($params['includeLeaf'])      $params['includeLeaf']      true;
  73.         $all                = isset($params['all'])              $params['all']              false;
  74.         $list->editLink     = isset($params['editLink'])         $params['editLink']         true;
  75.  
  76.         Loader::loadClass('CategoryUtil');
  77.  
  78.         $allCats array();
  79.  
  80.         // if we don't have a category-id we see if we can get a category by path
  81.         if (!$list->category && $path{
  82.             $list->category CategoryUtil::getCategoryByPath ($path$pathfield);
  83.             $allCats  CategoryUtil::getSubCategoriesForCategory ($list->category$recurse$relative$includeRoot
  84.                                                                    $includeLeaf$all);
  85.         }
  86.         else
  87.         // check if we have an actual category object with a numeric ID set
  88.         if (is_array($list->category&& isset($list->category['id']&& is_integer($list->category['id'])) {
  89.             $allCats  CategoryUtil::getSubCategoriesForCategory ($list->category$recurse$relative$includeRoot
  90.                                                                    $includeLeaf$all);
  91.         }
  92.         else
  93.         // check if we have a numeric category 
  94.         if (is_numeric($list->category)) {
  95.             $list->category CategoryUtil::getCategoryByID ($list->category);
  96.             $allCats  CategoryUtil::getSubCategoriesForCategory ($list->category$recurse$relative$includeRoot
  97.                                                                    $includeLeaf$all);
  98.         }
  99.         else
  100.         // check if we have a string/path category 
  101.         if (is_string($list->category&& strpos($list->category'/')===0{
  102.             $list->category CategoryUtil::getCategoryByPath ($list->category$pathfield);
  103.             $allCats  CategoryUtil::getSubCategoriesForCategory ($list->category$recurse$relative$includeRoot
  104.                                                                    $includeLeaf$all);
  105.         }
  106.  
  107.         if ($list->mandatory)
  108.             $list->addItem('- - -'null);
  109.  
  110.         $line '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -';
  111.  
  112.         if ($includeEmptyElement)
  113.             $list->addItem(''null);
  114.  
  115.         foreach ($allCats as $cat)
  116.         {
  117.             $cslash StringUtil::countInstances (isset($cat['ipath_relative']$cat['ipath_relative'$cat['ipath']'/');
  118.             $indent '';
  119.             if ($cslash 0)
  120.                 $indent '| ' substr ($line0$cslash*2);
  121.  
  122.             $list->addItem($indent ' ' (isset($cat['display_name'][$lang]$cat['display_name'][$lang$cat['name'])$cat['id']);
  123.         }
  124.  
  125.     }
  126.  
  127.  
  128.     function load(&$render$params)
  129.     {
  130.         $this->enableDBUtil = (isset($params['enableDBUtil']$params['enableDBUtil'false);
  131.         pnFormCategorySelector::loadParameters($this$this->includeEmptyElement$params);
  132.         parent::load($render$params);
  133.     }
  134.  
  135.  
  136.     function render(&$render)
  137.     {
  138.       $result parent::render($render);
  139.  
  140.       if ($this->editLink && !empty($this->category
  141.           && SecurityUtil::checkPermission'Categories::'"$this->category[id]::"ACCESS_EDIT)) 
  142.       {
  143.           $url DataUtil::formatForDisplay(pnModURL ('Categories''user''edit'array('dr' => $this->category['id'])));
  144.           $result .= "&nbsp;&nbsp;<a href=\"$url\"><img src=\"images/icons/extrasmall/xedit.gif\" title=\"_EDIT '" alt="' _EDIT '" /></a>';
  145.       }
  146.  
  147.       return $result;
  148.     }
  149.  
  150.  
  151.     function saveValue(&$render&$data)
  152.     {
  153.         if ($this->enableDBUtil && $this->dataBased)
  154.         {
  155.             if ($this->group == null)
  156.             {
  157.                 $data['__CATEGORIES__'][$this->dataField$this->getSelectedValue();
  158.             }
  159.             else
  160.             {
  161.                 if (!array_key_exists($this->group$data))
  162.                     $data[$this->grouparray();
  163.                 $data[$this->group]['__CATEGORIES__'][$this->dataField$this->getSelectedValue();
  164.             }
  165.         }
  166.         else
  167.             parent::saveValue($render$data);
  168.     }
  169.  
  170.  
  171.     function loadValue(&$render&$values)
  172.     {
  173.         if ($this->enableDBUtil && $this->dataBased)
  174.         {
  175.             $items null;
  176.             $value null;
  177.  
  178.             if ($this->group == null)
  179.             {
  180.                 if ($this->dataField != null  &&  isset($values['__CATEGORIES__'][$this->dataField]))
  181.                     $value $values['__CATEGORIES__'][$this->dataField];
  182.                 if ($this->itemsDataField != null  &&  isset($values[$this->itemsDataField])) 
  183.                     $items $values[$this->itemsDataField];
  184.             }
  185.             else
  186.             {
  187.                 if (isset($values[$this->group]))
  188.                 {
  189.                     $data $values[$this->group];
  190.                     if (isset($data['__CATEGORIES__'][$this->dataField]))
  191.                     {
  192.                         $value $data['__CATEGORIES__'][$this->dataField];
  193.                         if ($this->itemsDataField != null  &&  isset($data[$this->itemsDataField]))
  194.                             $items $data[$this->itemsDataField];
  195.                     }
  196.                 }
  197.             }
  198.  
  199.             if ($items != null)
  200.                 $this->setItems($items);
  201.  
  202.             $this->setSelectedValue($value);
  203.         }
  204.         else
  205.             parent::loadValue($render$values);
  206.     }
  207. }
  208.  
  209.  
  210. function smarty_function_pnformcategoryselector($params&$render)
  211. {
  212.   return $render->pnFormRegisterPlugin('pnFormCategorySelector'$params);
  213. }

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