This repository has been archived by the owner on May 14, 2019. It is now read-only.
forked from bermi/akelos
/
ak_menu_helper.php
110 lines (99 loc) · 5.25 KB
/
ak_menu_helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
# This file is part of the Akelos Framework
# (Copyright) 2004-2010 Bermi Ferrer bermi a t bermilabs com
# See LICENSE and CREDITS for details
/**
* The Menu Helper makes it easier to create simple menus from controllers actions.
*/
class AkMenuHelper extends AkBaseHelper
{
/**
* Returns a menu for all or several actions in all or several controllers.
*
* Let +menu_options+ defaults and this will generate a menu with all actions in all controllers.
* Set +menu_options+ to an array with keys as controller name and values as actions names.
*
* <?php echo $menu_helper->menu_for_controllers(array('advertiser' => array('buy', 'partial_in_template'))); ?>
* will generate something like :
* <div id="menu">
* <ul>
* <li>
* <h2><a href="/advertiser">Advertiser</a></h2>
* <ul>
* <li><a href="/advertiser/buy">Buy</a></li>
* <li><a href="/advertiser/partial_in_template">Partial in template</a></li>
* </ul>
* </li>
* </ul>
* </div>
*
* +div_menu_id+: the id of the main div (default is "menu")
* +current_class+: the class name of the current controller or the current action (default is "current")
* +title_tag+: the tag that will contain the controller name link (default is "h2"). If it's empty, it won't be present
*/
public function menu_for_controllers($menu_options = array(), $div_menu_id = 'menu', $current_class = 'current', $title_tag = 'h2') {
$menu_options = empty($menu_options) ? $this->_get_default_full_menu() : $menu_options;
$menu = '';
foreach ($menu_options as $controller => $actions){
$controller_name = AkInflector::classify($controller);
$current_controller_name = $this->_controller->getControllerName();
$current_action_name = $this->_controller->Request->getAction();
$controller_class_name = $controller_name.'Controller';
$controller_human_name = AkInflector::humanize($controller);
$controller_file_name = AkInflector::toControllerFilename($controller);
if(file_exists($controller_file_name)){
include_once($controller_file_name);
if(class_exists($controller_class_name)){
$class = $current_controller_name == $controller_name ? array('class' => $current_class) : array();
$href = array('href' => $this->_controller->urlFor(array('controller' => $controller)));
if (empty($title_tag)) {
$_title_tag = 'a';
$content = $controller_human_name;
$options = array_merge($class, $href);
} else {
$content = AkTagHelper::content_tag('a', $controller_human_name, $href);
$options = $class;
$_title_tag = $title_tag;
}
$menu_header = AkTagHelper::content_tag($_title_tag, $content, $options);
$submenu = '';
foreach ((array)$actions as $action){
if($action[0] == '_'){
continue;
}
$submenu .= AkTagHelper::content_tag('li', AkTagHelper::content_tag('a', AkInflector::humanize($action), array('href' => $this->_controller->urlFor(array('controller' => $controller, 'action' => $action)))), $current_controller_name == $controller_name && $current_action_name == $action ? array('class' => $current_class) : array());
}
$menu .= !empty($submenu) ? AkTagHelper::content_tag('ul', AkTagHelper::content_tag('li', $menu_header.AkTagHelper::content_tag('ul', $submenu))) : '';
}
}
}
return AkTagHelper::content_tag('div', $menu, array('id' => $div_menu_id));
}
public function _get_default_full_menu() {
$controllers_dir = AkConfig::getDir('controllers');
$controller_file_names = array_map('array_pop', (array)AkFileSystem::dir($controllers_dir, array('files'=>false)));
sort($controller_file_names);
$menu_options = array();
foreach ($controller_file_names as $controller_file_name){
$controller_name = str_replace('.php','',$controller_file_name);
if(strstr($controller_file_name,'_controller.php') && file_exists($controllers_dir.DS.$controller_file_name)){
include_once($controllers_dir.DS.$controller_file_name);
$controller_class_name = AkInflector::classify($controller_name);
$menu_options[str_replace('_controller','',$controller_name)] = $this->_get_this_class_methods($controller_class_name);
}
}
return $menu_options;
}
public function _get_this_class_methods($class) {
$array1 = get_class_methods($class);
if($parent_class = get_parent_class($class)){
$array2 = get_class_methods($parent_class);
$array3 = array_diff($array1, $array2);
}else{
$array3 = $array1;
}
$array3 = array_map('strtolower',(array)$array3);
$array3 = array_diff($array3, array(strtolower($class), 'index', 'destroy', 'edit', 'show'));
return($array3);
}
}