22
33/**
44 * Class Quick Menu
5- * @author David Ticona Saravia <davicotico@gmail .com>
6- * @version 0.7 (03/2017 )
5+ * @author David Ticona Saravia <davicotico@yandex .com>
6+ * @version 1.0.1 (04/2020 )
77 */
88class QuickMenu
99{
@@ -12,7 +12,6 @@ class QuickMenu
1212 private $ activeClass = 'active ' ;
1313 protected $ activeItem = '' ;
1414 private $ arrAttr = array ();
15- private $ strAttr = array ();
1615 private $ arrData = array ();
1716 private $ result = array ();
1817
@@ -34,11 +33,16 @@ public function __construct($options = array())
3433 */
3534 public function set ($ name , $ value )
3635 {
37- $ tags = array ('ul ' , 'ul-root ' , 'li ' , 'li-parent ' , 'a ' , 'a-parent ' , ' active-class ' );
36+ $ tags = array ('ul ' , 'ul-root ' , 'li ' , 'li-parent ' , 'a ' , 'a-parent ' );
3837 if (in_array ($ name , $ tags ))
3938 {
4039 $ this ->arrAttr [$ name ] = $ value ;
4140 }
41+ /* legacy */
42+ if ($ name =='active-class ' )
43+ {
44+ $ this ->activeClass = $ value ;
45+ }
4246 }
4347 /**
4448 * Set dropdown icon for display with submenus
@@ -59,8 +63,8 @@ public function setActiveItem($href, $activeClass = '')
5963 if ($ activeClass != '' )
6064 {
6165 $ this ->activeClass = $ activeClass ;
66+ $ this ->set ('active-class ' , $ this ->activeClass ); /* legacy */
6267 }
63- $ this ->set ('active-class ' , array ('class ' => $ this ->activeClass ));
6468 }
6569
6670 /**
@@ -168,10 +172,6 @@ public function html()
168172 {
169173 return $ this ->buildFromResult ($ this ->result );
170174 }
171- foreach ($ this ->arrAttr as $ tag => $ attr )
172- {
173- $ this ->strAttr [$ tag ] = $ this ->buildAttributes ($ tag );
174- }
175175 return $ this ->build ($ this ->arrData );
176176 }
177177
@@ -208,11 +208,12 @@ public function setResult($result, $columnID, $columnParent)
208208
209209 /**
210210 * @param string $tag
211+ * @param string $extra Add css class
211212 * @return string Tag Attributes stored
212213 */
213- protected function getAttr ($ tag )
214+ protected function getAttr ($ tag, $ extra = '' )
214215 {
215- return isset ( $ this ->strAttr [ $ tag]) ? $ this -> strAttr [ $ tag ] : '' ;
216+ return $ this ->buildAttributes ( $ tag, $ extra ) ;
216217 }
217218
218219 /**
@@ -230,15 +231,20 @@ protected function getTextItem($item, $isParent)
230231 /**
231232 * Renderize the tag attributes from array
232233 * @param string $tag The tag
234+ * @param string $extra append a css class
233235 * @return string The string atributes
234236 */
235- private function buildAttributes ($ tag )
237+ private function buildAttributes ($ tag, $ extra = '' )
236238 {
237239 $ str = '' ;
238240 if (isset ($ this ->arrAttr [$ tag ]))
239241 {
240242 foreach ($ this ->arrAttr [$ tag ] as $ name => $ value )
241243 {
244+ if (($ extra !='' )&&($ name =='class ' ))
245+ {
246+ $ value = "{$ value } {$ extra }" ;
247+ }
242248 $ str .= " {$ name }= \"{$ value }\"" ;
243249 }
244250 }
@@ -259,8 +265,8 @@ protected function build($array, $depth = 0)
259265 $ isParent = isset ($ item ['children ' ]);
260266 $ li = ($ isParent ) ? 'li-parent ' : 'li ' ;
261267 $ a = ($ isParent ) ? 'a-parent ' : 'a ' ;
262- $ active = ($ this ->activeItem == $ item ['href ' ]) ? $ this ->getAttr ( ' active-class ' ) : '' ;
263- $ str .= '<li ' . $ this ->getAttr ($ li ) . " { $ active } > " ;
268+ $ activeClass = ($ this ->activeItem == $ item ['href ' ]) ? $ this ->activeClass : '' ;
269+ $ str .= '<li ' . $ this ->getAttr ($ li, $ activeClass ) . "> " ;
264270 $ str .= '<a href=" ' . $ item ['href ' ] . '" title=" ' . $ item ['title ' ] . '" ' . $ this ->getAttr ($ a ) . '> ' . $ this ->getTextItem ($ item , $ isParent ) . '</a> ' ;
265271 if ($ isParent )
266272 {
@@ -287,7 +293,8 @@ protected function buildFromResult(array $array, $parent = 0, $level = 0)
287293 $ isParent = isset ($ array [$ item_id ]);
288294 $ li = ($ isParent ) ? 'li-parent ' : 'li ' ;
289295 $ a = ($ isParent ) ? 'a-parent ' : 'a ' ;
290- $ str .= '<li ' . $ this ->getAttr ($ li ) . '> ' ;
296+ $ activeClass = ($ this ->activeItem == $ item ['href ' ]) ? $ this ->activeClass : '' ;
297+ $ str .= '<li ' . $ this ->getAttr ($ li , $ activeClass ) . '> ' ;
291298 $ str .= "<a href= \"{$ item ['href ' ]}\" target= \"{$ item ['target ' ]}\"" . $ this ->getAttr ($ a ) . '> ' . $ this ->getTextItem ($ item , $ isParent ) . '</a> ' ;
292299 if ($ isParent )
293300 {
0 commit comments