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

Source for file ScMenus.php

Documentation is available at ScMenus.php

  1. <?php
  2. /**
  3.  * SCZF
  4.  *
  5.  * An open source SmartClient View Helper for Zend Framework
  6.  *
  7.  * @package        SCZF
  8.  * @author        Fernando Marcelo Morgenstern <fernando@consultorpc.com>
  9.  * @copyright    Copyright (c) 2009, ConsultorPC
  10.  * @license        http://www.gnu.org/licenses/lgpl-3.0-standalone.html
  11.  * @link        http://smartclientphp.com/
  12.  * @since        Version 0.1b
  13.  * @filesource
  14.  */
  15.  
  16.  
  17. /**
  18.  * Smart Client Menus Helper
  19.  *
  20.  * @uses viewHelper SmartClient
  21.  */
  22. {
  23.  
  24.     /**
  25.      * @var Zend_View_Interface 
  26.      */
  27.     public $view;
  28.     
  29.     /**
  30.      * @var Array 
  31.      */
  32.     protected $_items;
  33.     
  34.     /**
  35.      * 
  36.      * Return instance
  37.      * 
  38.      */
  39.     public function ScMenus()
  40.     {
  41.         return $this;
  42.     }
  43.  
  44.     /**
  45.      * 
  46.      * Sets the view field
  47.      * @param $view Zend_View_Interface
  48.      * 
  49.      */
  50.     public function setViewZend_View_Interface $view )
  51.     {
  52.         $this->view = $view;
  53.     }
  54.     
  55.     /**
  56.      * 
  57.      * Add Item to this menu
  58.      *
  59.      * @param mixed $data If string, it must be the title of the item. If array, them we will loop on it
  60.      * @param string $click Click action
  61.      * @param string $subMenu Submenu, if applicable
  62.      * @param array $options Additional options
  63.      * @return string 
  64.      * 
  65.      */
  66.     public function addItem$data $click '' $subMenu '' $options '' )
  67.     {
  68.         $newItem array();
  69.         
  70.         // Check if it is an array
  71.         if is_array$data ) )
  72.         {
  73.             $newItem $data;
  74.         }
  75.         else
  76.         {
  77.             // Add name and click to the new item
  78.             $newItem['title'$data;
  79.             $newItem['click'$click;
  80.             
  81.             // Check if there is a submenu and addQuotesStr
  82.             if !empty$subMenu ) )
  83.             {
  84.                 $subMenu $this->addQuotesStr$subMenu );
  85.             }
  86.             
  87.             $newItem['subMenu'$subMenu;
  88.             
  89.             // Check if options is array and do the loop
  90.             if is_array$options ) )
  91.             {
  92.                 foreach $options as $name => $value )
  93.                 {
  94.                     $newItem[$name$value;
  95.                 }
  96.             }
  97.         }
  98.         
  99.         // Add item to the array
  100.         $this->_items[$newItem;
  101.     }
  102.     
  103.     /**
  104.      *
  105.      * Get an array and do a loop using the addItem function
  106.      * 
  107.      * @param array $data Array containing multiple items
  108.      *  
  109.      */
  110.     public function addItems$data )
  111.     {
  112.         if is_array$data ) )
  113.         {
  114.             foreach $data as $row )
  115.             {
  116.                 $this->addItem$row );
  117.             }
  118.         }
  119.     }
  120.     
  121.     /**
  122.      * 
  123.      * Get items
  124.      * 
  125.      * @return array 
  126.      *  
  127.      */
  128.     public function getItems()
  129.     {
  130.         return $this->_items;
  131.     }
  132.     
  133.     /**
  134.      * 
  135.      * Clear items array
  136.      *  
  137.      */
  138.     public function clearItem()
  139.     {
  140.         unset$this->_items );
  141.     }
  142.     
  143.     /**
  144.      * 
  145.      * Add Separator
  146.      *
  147.      * @param mixed $data If string, it must be the id of the field. If array, them we will loop on it
  148.      * @param string $title Title of the field
  149.      * @param array $options Additional options
  150.      * @return string 
  151.      * 
  152.      */
  153.     public function addSeparator$options '' )
  154.     {
  155.         $newSeparator array();
  156.         
  157.         // Check if it is an array
  158.         if is_array$options ) )
  159.         {
  160.             $newSeparator $options;
  161.         }
  162.         else
  163.         {
  164.             // Add this as an separator
  165.             $newSeparator['isSeparator'true;
  166.             
  167.             // Check if options is array and do the loop
  168.             if is_array$options ) )
  169.             {
  170.                 foreach $options as $name => $value )
  171.                 {
  172.                     $newSeparator[$name$value;
  173.                 }
  174.             }
  175.         }
  176.         
  177.         // Add separator to the array
  178.         $this->_items[$newSeparator;
  179.     }
  180.  
  181.     /**
  182.      * 
  183.      * Create a new menu
  184.      *
  185.      * @param mixed $data If string, it must be the ID of the menu. If array, them we will loop through it
  186.      * @param boolean $createButton Set true if a button must also be created
  187.      * @param string $buttonTitle Title of the button
  188.      * @param array $options Additional options
  189.      * @param array $buttonOptions Additional options to button
  190.      * @return string 
  191.      * 
  192.      */
  193.     public function create$data $createButton true $buttonTitle '' $options '' $buttonOptions '' )
  194.     {
  195.         $newMenu array();
  196.         
  197.         // Check if it is an array
  198.         if is_array$data ) )
  199.         {
  200.             $newMenu $data;
  201.         }
  202.         else
  203.         {
  204.             // Add vars to the new button
  205.             $newMenu['ID'$data;
  206.  
  207.             
  208.             // Set items
  209.             $newMenu['data'$this->_items;
  210.             
  211.             // Check if options is array and do the loop
  212.             if is_array$options ) )
  213.             {
  214.                 foreach $options as $name => $value )
  215.                 {
  216.                     $newMenu[$name$value;
  217.                 }
  218.             }
  219.         }
  220.         
  221.         // Encode all data
  222.         $dataEncoded Zend_Json::encode$newMenu );
  223.         
  224.         // Remove vars that shouldn't be quoted
  225.         $dataEncoded $this->removeQuotes$dataEncoded );
  226.         
  227.         // Generate js code
  228.         $js '
  229.         <script type="text/javascript">
  230.             isc.Menu.create(
  231.                 '$dataEncoded .'
  232.             );
  233.         ';
  234.         
  235.         // Check if we need to create a button
  236.         if $createButton )
  237.         {
  238.             $newMenuButton['title'$buttonTitle;
  239.             $newMenuButton['menu'$this->addQuotesStr$newMenu['ID');
  240.             
  241.             // Check if options is array and do the loop
  242.             if is_array$buttonOptions ) )
  243.             {
  244.                 foreach $buttonOptions as $name => $value )
  245.                 {
  246.                     $newMenuButton[$name$value;
  247.                 }
  248.             }
  249.             
  250.             // Encode all data
  251.             $dataEncoded Zend_Json::encode$newMenuButton );
  252.             
  253.             // Remove vars that shouldn't be quoted
  254.             $dataEncoded $this->removeQuotes$dataEncoded );
  255.             
  256.             
  257.             $js .= '
  258.             isc.MenuButton.create(
  259.                 '$dataEncoded .'
  260.             );
  261.             ';
  262.         }
  263.         
  264.         $js .= '</script>';
  265.         
  266.         return $js;
  267.     }
  268. }

Documentation generated on Mon, 20 Jul 2009 16:51:57 -0300 by phpDocumentor 1.4.1