Development

/plugins/dgDojoPlugin/trunk/lib/dojo/DojoMenuItem.class.php

You must first sign up to be able to contribute.

root/plugins/dgDojoPlugin/trunk/lib/dojo/DojoMenuItem.class.php

Revision 10334, 4.8 kB (checked in by Dean.Glazeski, 5 years ago)

Initial import of source.

Line 
1 <?php
2
3 /**
4  * Class to represent a Dojo Menu Item.  This handles dealing with a submenu so
5  * classes like the Dojo Button actually inherit from here.  It automatically
6  * will adjust the type to a popup menu item if the submenu is setup.
7  *
8  */
9 class DojoMenuItem extends DojoWidget implements ArrayAccess
10 {
11     protected
12         /**
13          * Name of the item.
14          */
15         $name,
16         
17         /**
18          * Set of options to be passed to a submenu for this item
19          */
20         $menuOptions = array(),
21         
22         /**
23          * The submenu object if there is one.
24          */
25         $menu = null;
26     
27     /**
28      * Base constructor to set the name and options for the Dojo Menu Item.
29      *
30      * @param string $name Name for the button, the text on the button
31      * @param mixed $options Set of options for the HTML tag
32      */
33     public function __construct( $name, $options = array(), $attributes = array() )
34     {
35         parent::__construct( $options, $attributes );
36         $this->name = $name;
37     }
38     
39     protected function configure( $options = array(), $attributes = array() )
40     {
41         $this->setAttribute( 'dojoType', DojoTypes::MENU_ITEM );
42         $this->addProtectedAttribute( 'dojoType' );
43     }
44     
45     /**
46      * Returns all of the items associated with the submenu.
47      *
48      * @return mixed Null if no submenu, otherwise the items for the menu
49      */
50     public function getItems()
51     {
52         if ( $this->menu === null )
53         {
54             return null;
55         }
56         
57         return $this->menu->getItems();
58     }
59     
60     /**
61      * Returns all of the positions associated with the submenu.
62      *
63      * @return mixed Null if no submenu, otherwise the set of positions
64      */
65     public function getPositions()
66     {
67         if ( $this->menu === null )
68         {
69             return null;
70         }
71         
72         return $this->menu->getPositions();
73     }
74     
75     /**
76      * Sets a menu option.  Stores values in a separate array until the menu
77      * object is actually created for this item.
78      *
79      * @param string $option Option to be set
80      * @param mixed $value Value for the option
81      */
82     public function setMenuAttribute( $option, $value )
83     {
84         if ( $this->menu !== null )
85         {
86             $this->menu->setAttribute( $option, $value );
87         }
88         else
89         {
90             $this->menuOptions[$option] = $value;
91         }
92     }
93     
94     /**
95      * Determines if a particular menu item exists in the submenu.  Returns
96      * false if there isn't a menu yet.
97      *
98      * @param string $name Name of the item looking for
99      * @return boolean True if the item exists
100      */
101     public function offsetExists( $name )
102     {
103         $rval = false;
104         if ( $this->menu !== null )
105         {
106             $rval = isset( $this->menu[$name] );
107         }
108         
109         return $rval;
110     }
111     
112     /**
113      * Gets a particular menu item from the submenu if it exists.  Returns null
114      * if there is no menu set yet.
115      *
116      * @param string $name Name of the item looking for
117      * @return DojoMenuItem The item looking for if it exists
118      */
119     public function offsetGet( $name )
120     {
121         $rval = null;
122         if ( $this->menu !== null )
123         {
124             $rval = $this->menu[$name];
125         }
126         
127         return $rval;
128     }
129     
130     /**
131      * Sets a particular menu item.  Relies on the menu to decide if the item
132      * is valid.  This method will create the submenu if it doesn't yet exist.
133      *
134      * @param string $name Name of the new item
135      * @param DojoMenuItem $item Item for the submenu
136      */
137     public function offsetSet( $name, $item )
138     {       
139         if ( $this->menu === null )
140         { // create the menu if we haven't yet
141             $this->menu = new DojoMenu( array(), $this->menuOptions );
142         }
143         
144         $this->menu[$name] = clone $item;
145     }
146     
147     /**
148      * Unsets a particular item in the submenu.
149      *
150      * @param string $name Name of the item to be unset
151      */
152     public function offsetUnset( $name )
153     {
154         if ( $this->menu !== null )
155         {
156             unset( $this->menu[$name] );
157         }
158     }
159     
160     /**
161      * Adds an item based on a string to the submenu.  This name will be used
162      * in the array offset to access the new menu item.
163      *
164      * @param string $name Name for the new menu item.
165      */
166     public function addItem( $name )
167     {
168         $this[$name] = new DojoMenuItem();
169     }
170     
171     /**
172      * Returns the rendered from of the DojoMenuItem including its submenu if
173      * necessary.  Inheriting classes should override this for printing.
174      *
175      * @return string The rendered menu item
176      */
177     public function render( $name, $value = null, $attributes = array(), $errors = array() )
178     {
179         // adds that the dojo menu is required
180         DojoManager::addRequire( DojoTypes::MENU );
181         $attributes['dojoType'] = $this->getAttribute( 'dojoType' );
182         
183         // if we have a submenu, this needs to be a popup menu item in dojo
184         if ( $this->menu !== null ) $attributes['dojoType'] = DojoTypes::POPUP_MENU_ITEM;
185         
186         $rval = '';
187         if ( $this->menu !== null )
188         { // submenus require slightly different rendering
189             $rval = "<span>$name</span>\n";
190             $rval .= $this->menu->__toString();
191         }
192         else
193         {
194             $rval = $this->name;
195         }
196         
197         return $this->renderContentTag( 'div', $rval, $attributes );
198     }
199
200     /**
201      * Returns the string form of this object.
202      *
203      * @return string Rendered form of the object
204      */
205     public function __toString()
206     {
207         return $this->render( $this->name );
208     }
209 }
210 ?>
Note: See TracBrowser for help on using the browser.