* @copyright Copyright (c) 2010, ConsultorPC * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html * @link http://smartclientphp.com/ * @since Version 0.1b * @filesource */ /** * Smart Client Menu Helper * */ class ScMenu extends ScGeneral { /** * @var Array */ protected $_items; /** * * Add Item to this menu * * @param mixed $data If string, it must be the title of the item. If array, them we will loop on it * @param string $click Click action * @param string $subMenu Submenu, if applicable * @param array $options Additional options * @return string * */ public function addItem( $data , $click = '' , $subMenu = '' , $options = '' ) { $newItem = array(); // Check if it is an array if ( is_array( $data ) ) { $newItem = $data; } else { // Add name and click to the new item $newItem['title'] = $data; $newItem['click'] = $click; // Check if there is a submenu and addQuotesStr if ( !empty( $subMenu ) ) { $subMenu = $this->addQuotesStr( $subMenu ); } $newItem['submenu'] = $subMenu; // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newItem[$name] = $value; } } } // Add item to the array $this->_items[] = $newItem; } /** * * Get an array and do a loop using the addItem function * * @param array $data Array containing multiple items * */ public function addItems( $data ) { if ( is_array( $data ) ) { foreach ( $data as $row ) { $this->addItem( $row ); } } } /** * * Get items * * @return array * */ public function getItems() { return $this->_items; } /** * * Clear items array * */ public function clearItems() { unset( $this->_items ); } /** * * Add Separator * * @param mixed $data If string, it must be the id of the field. If array, them we will loop on it * @param string $title Title of the field * @param array $options Additional options * @return string * */ public function addSeparator( $options = '' ) { $newSeparator = array(); // Check if it is an array if ( is_array( $options ) ) { $newSeparator = $options; } else { // Add this as an separator $newSeparator['isSeparator'] = true; // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newSeparator[$name] = $value; } } } // Add separator to the array $this->_items[] = $newSeparator; } /** * * Create a new menu * * @param mixed $data If string, it must be the ID of the menu. If array, them we will loop through it * @param boolean $createButton Set true if a button must also be created * @param string $buttonTitle Title of the button * @param array $options Additional options * @param array $buttonOptions Additional options to button * @return string * */ public function create( $data , $createButton = true , $buttonTitle = '' , $options = '' , $buttonOptions = '' ) { $newMenu = array(); // Check if it is an array if ( is_array( $data ) ) { $newMenu = $data; } else { // Add vars to the new button $newMenu['ID'] = $data; // Set items $newMenu['data'] = $this->_items; // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newMenu[$name] = $value; } } } // Encode data $dataEncoded = $this->encode( $newMenu ); // Generate js code $js = 'isc.Menu.create( '. $dataEncoded .' );'; // Check if we need to create a button if ( $createButton ) { $newMenuButton['title'] = $buttonTitle; $newMenuButton['menu'] = $this->addQuotesStr( $newMenu['ID'] ); // Check if options is array and do the loop if ( is_array( $buttonOptions ) ) { foreach ( $buttonOptions as $name => $value ) { $newMenuButton[$name] = $value; } } // Encode data $dataEncoded = $this->encode( $newMenuButton ); $js .= 'isc.MenuButton.create( '. $dataEncoded .' );'; } // Add script tag $js = $this->addScriptTags( $js ); return $js; } }