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

Source for file ScMenus.php

Documentation is available at ScMenus.php

  1. <?php
  2. /**
  3.  * SCPHP
  4.  *
  5.  * An open source SmartClient library for PHP
  6.  *
  7.  * @package        SCPHP
  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.  */
  21. class ScMenus extends ScGeneral 
  22. {
  23.     
  24.     /**
  25.      * @var Array 
  26.      */
  27.     protected $_items;
  28.     
  29.     /**
  30.      * 
  31.      * Add Item to this menu
  32.      *
  33.      * @param mixed $data If string, it must be the title of the item. If array, them we will loop on it
  34.      * @param string $click Click action
  35.      * @param string $subMenu Submenu, if applicable
  36.      * @param array $options Additional options
  37.      * @return string 
  38.      * 
  39.      */
  40.     public function addItem$data $click '' $subMenu '' $options '' )
  41.     {
  42.         $newItem array();
  43.         
  44.         // Check if it is an array
  45.         if is_array$data ) )
  46.         {
  47.             $newItem $data;
  48.         }
  49.         else
  50.         {
  51.             // Add name and click to the new item
  52.             $newItem['title'$data;
  53.             $newItem['click'$click;
  54.             
  55.             // Check if there is a submenu and addQuotesStr
  56.             if !empty$subMenu ) )
  57.             {
  58.                 $subMenu $this->addQuotesStr$subMenu );
  59.             }
  60.             
  61.             $newItem['subMenu'$subMenu;
  62.             
  63.             // Check if options is array and do the loop
  64.             if is_array$options ) )
  65.             {
  66.                 foreach $options as $name => $value )
  67.                 {
  68.                     $newItem[$name$value;
  69.                 }
  70.             }
  71.         }
  72.         
  73.         // Add item to the array
  74.         $this->_items[$newItem;
  75.     }
  76.     
  77.     /**
  78.      *
  79.      * Get an array and do a loop using the addItem function
  80.      * 
  81.      * @param array $data Array containing multiple items
  82.      *  
  83.      */
  84.     public function addItems$data )
  85.     {
  86.         if is_array$data ) )
  87.         {
  88.             foreach $data as $row )
  89.             {
  90.                 $this->addItem$row );
  91.             }
  92.         }
  93.     }
  94.     
  95.     /**
  96.      * 
  97.      * Get items
  98.      * 
  99.      * @return array 
  100.      *  
  101.      */
  102.     public function getItems()
  103.     {
  104.         return $this->_items;
  105.     }
  106.     
  107.     /**
  108.      * 
  109.      * Clear items array
  110.      *  
  111.      */
  112.     public function clearItem()
  113.     {
  114.         unset$this->_items );
  115.     }
  116.     
  117.     /**
  118.      * 
  119.      * Add Separator
  120.      *
  121.      * @param mixed $data If string, it must be the id of the field. If array, them we will loop on it
  122.      * @param string $title Title of the field
  123.      * @param array $options Additional options
  124.      * @return string 
  125.      * 
  126.      */
  127.     public function addSeparator$options '' )
  128.     {
  129.         $newSeparator array();
  130.         
  131.         // Check if it is an array
  132.         if is_array$options ) )
  133.         {
  134.             $newSeparator $options;
  135.         }
  136.         else
  137.         {
  138.             // Add this as an separator
  139.             $newSeparator['isSeparator'true;
  140.             
  141.             // Check if options is array and do the loop
  142.             if is_array$options ) )
  143.             {
  144.                 foreach $options as $name => $value )
  145.                 {
  146.                     $newSeparator[$name$value;
  147.                 }
  148.             }
  149.         }
  150.         
  151.         // Add separator to the array
  152.         $this->_items[$newSeparator;
  153.     }
  154.  
  155.     /**
  156.      * 
  157.      * Create a new menu
  158.      *
  159.      * @param mixed $data If string, it must be the ID of the menu. If array, them we will loop through it
  160.      * @param boolean $createButton Set true if a button must also be created
  161.      * @param string $buttonTitle Title of the button
  162.      * @param array $options Additional options
  163.      * @param array $buttonOptions Additional options to button
  164.      * @return string 
  165.      * 
  166.      */
  167.     public function create$data $createButton true $buttonTitle '' $options '' $buttonOptions '' )
  168.     {
  169.         $newMenu array();
  170.         
  171.         // Check if it is an array
  172.         if is_array$data ) )
  173.         {
  174.             $newMenu $data;
  175.         }
  176.         else
  177.         {
  178.             // Add vars to the new button
  179.             $newMenu['ID'$data;
  180.  
  181.             
  182.             // Set items
  183.             $newMenu['data'$this->_items;
  184.             
  185.             // Check if options is array and do the loop
  186.             if is_array$options ) )
  187.             {
  188.                 foreach $options as $name => $value )
  189.                 {
  190.                     $newMenu[$name$value;
  191.                 }
  192.             }
  193.         }
  194.         
  195.         // Encode all data
  196.         $dataEncoded Zend_Json::encode$newMenu );
  197.         
  198.         // Remove vars that shouldn't be quoted
  199.         $dataEncoded $this->removeQuotes$dataEncoded );
  200.         
  201.         // Generate js code
  202.         $js '
  203.         <script type="text/javascript">
  204.             isc.Menu.create(
  205.                 '$dataEncoded .'
  206.             );
  207.         ';
  208.         
  209.         // Check if we need to create a button
  210.         if $createButton )
  211.         {
  212.             $newMenuButton['title'$buttonTitle;
  213.             $newMenuButton['menu'$this->addQuotesStr$newMenu['ID');
  214.             
  215.             // Check if options is array and do the loop
  216.             if is_array$buttonOptions ) )
  217.             {
  218.                 foreach $buttonOptions as $name => $value )
  219.                 {
  220.                     $newMenuButton[$name$value;
  221.                 }
  222.             }
  223.             
  224.             // Encode all data
  225.             $dataEncoded json_encode$newMenuButton );
  226.             
  227.             // Remove vars that shouldn't be quoted
  228.             $dataEncoded $this->removeQuotes$dataEncoded );
  229.             
  230.             
  231.             $js .= '
  232.             isc.MenuButton.create(
  233.                 '$dataEncoded .'
  234.             );
  235.             ';
  236.         }
  237.         
  238.         $js .= '</script>';
  239.         
  240.         return $js;
  241.     }
  242. }

Documentation generated on Wed, 29 Jul 2009 22:37:14 -0300 by phpDocumentor 1.4.1