-
Notifications
You must be signed in to change notification settings - Fork 1
/
mvc.module
221 lines (212 loc) · 5.41 KB
/
mvc.module
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* @file
* Druapl MVC Framework
*
* This module allows creating mvc applications from within drupal
*
* @author Samuel Leathers <[email protected]>
* @version 7.x-0.1
* @copyright Samuel Leathers <[email protected]>, 18 November, 2011
* @package mvc
**/
/**
* Instantiate class autoloader
*/
module_load_include('inc', 'mvc', 'autoload');
/**
* Implementation of hook_init().
*/
function mvc_init() {
global $mvc_modules;
$mvc_modules = module_invoke_all('mvc_app');
foreach($mvc_modules as $mod) {
AutoLoader::addPath($mod['path']);
}
AutoLoader::addPath(drupal_get_path('module','mvc'));
}
/**
* Sample Implementation of hook_mvc_app
*
* {{{
* function mvc_mvc_app() {
* $mvc = new ArrayObject();
* $mvc['base'] = 'mvc';
* $mvc['path'] = drupal_get_path('module','mvc');
* $mvc['blocks'] = array(array(
* 'name' => 'Block Name',
* 'title' => 'Block Title',
* 'controller' => 'controller',
* 'method' => 'method',
* 'args' => array('arg1','arg2'),
* ));
* $mvc['menu_items'] = array(array(
* 'path' => 'path/to/wherever',
* 'title' => 'Menu Title',
* 'controller' => 'controller',
* 'method' => 'method',
* 'args' => array('arg1','arg2'),
* ));
* return $mvc;
* }
* }}}
*
*/
/**
* Implementation of hook_menu().
*/
function mvc_menu() {
global $mvc_modules;
if(!empty($mvc_modules)) {
$items = array();
// Graduate Application Page
foreach($mvc_modules as $mod) {
$items[ $mod['base'].'/%/%'] = array(
'page callback' => 'mvc_controller',
'page arguments' => array($mod['path'],$mod['base']),
'page arguments' => array($mod['path'],$mod['base'],NULL,NULL,NULL,NULL),
'access callback' => TRUE,
'type' => MENU_CALLBACK
);
foreach($mod['menu_items'] as $menu) {
if(!isset($menu['args'])) {
$menu['args'] = NULL;
}
if(!isset($menu['controller'])) {
$menu['controller'] = NULL;
}
if(!isset($menu['controller'])) {
$menu['method'] = NULL;
}
$items[$menu['path']] = array(
'title' => $menu['title'],
'page callback' => 'mvc_controller',
'page arguments' => array($mod['path'],NULL,$menu['path'],$menu['controller'],$menu['method'],$menu['args']),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM
);
}
}
return $items;
}
return array();
}
/**
* Implements hook_block_info()
*/
function mvc_block_info() {
$blocks = array();
global $mvc_modules;
if(!empty($mvc_modules)) {
foreach($mvc_modules as $mod) {
if(!empty($mod['blocks'])) {
foreach($mod['blocks'] as $block) {
$blocks[$block['name']] = array();
$blocks[$block['name']]['info'] = $block['title'];
if(!empty($block['region'])) {
$blocks[$block['name']]['region'] = $block['region'];
}
if(!empty($block['pages'])) {
$blocks[$block['name']]['pages'] = $block['pages'];
}
$blocks[$block['name']]['cache'] = DRUPAL_NO_CACHE;
}
}
}
}
return $blocks;
}
/**
* Implements hook_block_view()
*/
function mvc_block_view($delta = '') {
global $mvc_modules;
$block = array();
foreach($mvc_modules as $mod) {
if(!empty($mod['blocks'])) {
foreach($mod['blocks'] as $block) {
switch ($delta) {
case $block['name']:
$block['subject'] = $block['title'];
$block['content'] = mvc_controller($mod['path'],NULL,NULL,$block['controller'],$block['method'],$block['args']);
break;
}
}
}
}
return $block;
}
/**
* MVC Dispatch Function
*
* dispatcher function for controller
*
* @return object View rendered by controller
* @author Samuel Leathers <[email protected]>
**/
function mvc_controller($modpath,$base,$menu_path = NULL,$pass_controller = NULL,$pass_method = NULL, $pass_args = NULL) {
global $controllerName, $method, $module_path;
/**
* If `pass_controller` is not set, evaluate from args()
* Otherwise, `controllerName` is what is passed as a parameter
*/
$module_path = $modpath;
if(!$pass_controller) {
$controllerName = arg(1);
}
else {
$controllerName = $pass_controller;
}
$fullControllerName = $controllerName.'Controller';
/**
* If `pass_method` is not set, set to 2nd argument
* Otherwise, `method` is what is passed as a parameter
*/
if(!$pass_method) {
$method = arg(2);
}
else {
$method = $pass_method;
}
/**
* If `pass_args` is not set, set to remaining arguments
* Otherwise, `params` is what is passed as parameter
*/
if(!$pass_args) {
$args = arg();
if(!$menu_path) {
$exp_args = array($controllerName,$method,$base);
}
else {
$exp_args = explode('/',$menu_path);
}
$diff = array_diff($args,$exp_args);
$params = array_merge($diff);
}
else {
$params = $pass_args;
}
if(class_exists($fullControllerName)) {
$controller = new $fullControllerName;
if($controller->access()) {
if(method_exists($controller, $method)) {
if(is_array($params)) {
return call_user_func_array(array($controller,$method),$params);
}
else {
return $controller->$method($params);
}
}
else {
drupal_not_found();
}
}
else {
drupal_access_denied();
}
}
else {
drupal_not_found();
}
}
?>