-
Notifications
You must be signed in to change notification settings - Fork 1
/
homebox.module
314 lines (259 loc) · 8.71 KB
/
homebox.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/**
* TODO
* implémenter un requester de widget/box
* implémenter un server de widget/box
*/
// CTools API requirements
define('HOMEBOX_MINIMUM_CTOOLS_API_VERSION', '1.2');
/**
* Testing part
*/
homebox_include('HomeboxPage.class');
homebox_include('HomeboxTab.class');
homebox_include('HomeboxBox.class');
homebox_include('handler.block');
drupal_add_js(drupal_get_path('module', 'homebox') .'/homebox.js');
// Includes revelant files
/**
* Implementation of hook_requirements()
*
* We need CTools API 1.2 (TODO: sure?)
*/
function homebox_requirements($phase) {
$requirements = array();
if (!module_invoke('ctools', 'api_version', HOMEBOX_MINIMUM_CTOOLS_API_VERSION)) {
$requirements['homebox_ctools'] = array(
'title' => $t('Homebox requires Chaos Tool Suite (CTools) API Version'),
'value' => t('At least @api', array('@api' => HOMEBOX_MINIMUM_CTOOLS_API_VERSION)),
'severity' => REQUIREMENT_ERROR,
);
}
return $requirements;
}
function homebox_menu() {
$items = array();
$items['homebox'] = array(
'title' => t('Homebox'),
'page callback' => 'homebox_render_page',
'access arguments' => array(TRUE),
'type' => MENU_NORMAL_ITEM,
);
$items['homebox/get/tab/%'] = array(
'page callback' => 'homebox_serve_tab',
'access arguments' => array(TRUE),
'type' => MENU_CALLBACK,
);
$items['homebox/get/box/%'] = array(
'page callback' => 'homebox_serve_box',
'access arguments' => array(TRUE),
'type' => MENU_CALLBACK,
);
$items['homebox/loadboxes'] = array(
'page callback' => 'homebox_modal_test',
'access arguments' => array(TRUE),
'type' => MENU_CALLBACK,
);
return $items;
}
function homebox_modal_test() {
ctools_include('ajax');
ctools_modal_add_js();
// $output[] = ctools_modal_command_display("Title", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
//$output[] = ctools_ajax_command_replace('#modal-message', '<div id="modal-message">Hello Dolly</div>');
$boxes = array();
foreach (module_implements('declare_boxes') as $module) {
$declared_boxes = module_invoke($module, 'declare_boxes');
$boxes = array_merge($boxes, $declared_boxes);
}
foreach ($boxes as $key => $box) {
//print_r($box);
$html = theme('homebox_box', $box);
$output[] = ctools_ajax_command_append('#homebox', $html);
}
ctools_ajax_render($output);
}
/**
* TODO Make this a menu arg not a arg() el
*/
function homebox_serve_box($identifer = NULL) {
ctools_include('ajax');
$identifer = arg(3);
$tab = "#". arg(4);
list($module, $name) = explode(':', $identifer);
// TODO on ne devrait pas déterminer qu'il s'agit d'un bloc
// à l'extérieure de la classe, mais dans le constructeur ou
// dans une fonction de Homebox générique
$box = new HomeboxBox($module, $name, TRUE);
$html = theme('homebox_box', $box);
$output[] = ctools_ajax_command_append($tab .' .column:first', $html);
ctools_ajax_render($output);
}
function homebox_serve_tab($tab_slug = NULL) {
ctools_include('ajax');
$tab_slug = arg(3);
// A function must find the right tab
$name = 'First tab';
$boxes_identifiers = homebox_find_boxes_identifiers_for_tab($tab_slug);
// $key = 'homebox'. $tab_slug;
// $settings = array('homebox' => array('tabs', array($tab_slug, $boxes_identifiers)));
//
// $output[] = ctools_ajax_command_settings($settings);
//
// ctools_ajax_render($output);
$response = array(
'tab' => $tab_slug,
'boxes' => $boxes_identifiers,
);
drupal_json($response);
die();
}
function homebox_render_page() {
ctools_include('ajax');
ctools_include('modal');
ctools_include('plugins');
ctools_modal_add_js();
jquery_ui_add(array('ui.draggable', 'ui.droppable', 'ui.sortable', 'ui.tabs')); // For a single file
$boxes = array();
foreach (module_implements('declare_boxes') as $module) {
$declared_boxes = module_invoke($module, 'declare_boxes');
$boxes = array_merge($boxes, $declared_boxes);
}
//print_r($boxes);
$identifiers = array();
foreach ($boxes as $key => $box) {
$identifiers[] = $box->getIdentifier();
}
$tab1 = new HomeboxTab('First tab');
$tab2 = new HomeboxTab('Second tab');
$tabs_name = array($tab1->getName(), $tab2->getName());
$tabs = array($tab1, $tab2);
drupal_add_js(array('homebox' => array('tabs' => $tabs_name)), $type = 'setting');
//
// // ctools_ajax_command_settings(array('homebox' => $identifiers));
drupal_add_js(array('homebox' => array('boxes' => $identifiers)), $type = 'setting');
return theme('homebox_page', $tabs);
// $box = new HomeboxBox('homebox', 'recent_comments');
// $box->setTitle(t('Recent comments'));
// $box->setDescription(t('Shows recent comments posted on the site'));
// $box->setContent('<div><h3>Recent comments</h3><ul><li>1 comment</li></ul></div>');
//
// $tab = new HomeboxTab('My new tab');
// $tab->addBox($box);
// $tab->addBox($box);
//
// $page = new HomeboxPage('My Homebox page');
// $page->addTab($tab);
// $page->addTab($tab);
}
/**
* @param $tab_slug
* A string representing a HomeboxTab slug
*
* @return array
* An array containing all boxes identifiers
*/
function homebox_find_boxes_identifiers_for_tab($tab_slug) {
// TODO Find the right tab
$boxes = array();
foreach (module_implements('declare_boxes') as $module) {
$declared_boxes = module_invoke($module, 'declare_boxes');
$boxes = array_merge($boxes, $declared_boxes);
}
//print_r($boxes);
$identifiers = array();
foreach ($boxes as $key => $box) {
$identifiers[] = $box->getIdentifier();
}
return $identifiers;
}
/**
* Loads one box from the database
*
* @param $id
* @param $parts
* Indicates which parts of the box should be loaded:
* 'all', the whole box is loaded (default)
* 'name', loads only box name/title
* 'config', loads configuration zone, this includes Views exposed filters as well
* 'content', only loads box content
* Of course you can load multiple parts at once as $parts is an array.
* You can even define your own 'parts' within your module be implementing:
* @code
* hook_homebox_define_parts()
* @endcode
* Which should return an array of parts name, i.e:
* @code
* array('my_custom_box_part', 'another_custom_box_part')
*/
function homebox_load_box($id, $parts = array('all')) {
}
/**
* Finds every available boxes parts defined in 3rd party module
*
* @param $sort
* As seen in module_implements()
* @param $refresh
* As seen in module_implements()
* @return Array keyed by module name
* Containing parts implemented by each modules
*/
function homebox_parts_implements($sort = FALSE, $refresh = FALSE) {
// TODO: why not use module_invoke_all
foreach (module_implements('homebox_define_parts', $sort, $refresh) as $module) {
$parts[$module] = module_invoke($module, 'homebox_define_parts');
}
}
function homebox_homebox_define_parts() {
return array('name', 'config', 'content');
}
/**
* Implementation of hook_theme().
*/
function homebox_theme() {
return array(
// Set hook name: see template_preprocess_homebox()
'homebox_page' => array(
'arguments' => array('tabs' => $tabs),
'template' => 'homebox-page', // Set template to homebox-page.tpl.php
),
'homebox_box' => array(
'arguments' => array('box' => $box),
'template' => 'homebox-box', // Set template to homebox-box.tpl.php
),
);
}
/**
* Include homebox .inc files as necessary.
*/
function homebox_include($file) {
static $used = array();
if (!isset($used[$file])) {
require_once './' . drupal_get_path('module', 'homebox') . "/includes/$file.inc";
}
$used[$file] = TRUE;
}
/**
* Implementation of hook_ctools_plugin_directory().
*
* It simply tells CTools where to find the .inc files that define various
* args, contexts, content_types.
*/
// function homebox_ctools_plugin_directory($module, $plugin) {
// if ($module == 'homebox' && !empty($plugin)) {
// return "plugins/$plugin";
// }
// }
// TODO
// function homebox_get_ctools_plugins() {
// ctools_include('plugins');
// ctools_get_plugins('homebox', 'homebox_boxes');
// }
function homebox_recent_comments_set_box_content($text) {
return $text;
}
/***
Dans le template identifier une box unique comme ceci:
"{$module}::{$name}::{$uid}::{$delta}::{$tab}" avec en primary key $uid + $delta
$tab == my-tab => My Tab
*/