-
Notifications
You must be signed in to change notification settings - Fork 1
/
mmoda.lib.inc
462 lines (409 loc) · 13.5 KB
/
mmoda.lib.inc
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php
/**
*/
function _mmoda_install_started()
{
variable_set('mmoda_install_started', true);
}
/**
*/
function _mmoda_install_finished()
{
variable_del('mmoda_install_started');
}
/**
*/
function _mmoda_uninstall_started()
{
variable_set('mmoda_uninstall_started', true);
}
/**
*/
function _mmoda_uninstall_finished()
{
variable_del('mmoda_uninstall_started');
}
/**
*/
function _mmoda_core($module_name)
{
# $core_module_name = basename(__FILE__, '.lib.inc');
list ($core_module_name) = explode('_', $module_name);
return ($core_module_name == $module_name);
}
/**
* Implements hook_install().
*/
function _mmoda_common_install($module_name, $instrument, $module_settings)
{
_mmoda_install_started();
list ($core_module_name) = explode('_', $module_name);
$core_module_settings = array();
$module_settings['help_page_nids'] = array();
$field_mmoda_module = field_info_field('field_mmoda_module');
if (_mmoda_core($module_name)) {
$core_module_settings = $module_settings;
$core_module_settings['module_name'] = $core_module_name;
$field_mmoda_module['settings']['allowed_values'][$module_name] = strtoupper($module_name);
} else {
$core_module_settings = variable_get($core_module_name . '_settings');
$core_module_settings['instruments'][$instrument] = $module_settings;
$core_module_settings['module_instrument'][$module_name] = $instrument;
$field_mmoda_module['settings']['allowed_values'][$module_name] = $module_settings['title'];
}
field_update_field($field_mmoda_module);
variable_set($core_module_name . '_settings', $core_module_settings);
// add help nodes
_mmoda_help_pages_import($core_module_name, $module_name, $instrument);
_mmoda_install_finished();
}
/**
* Implements hook_uninstall().
*/
function _mmoda_common_uninstall($module_name, $instrument = '')
{
list ($core_module_name) = explode('_', $module_name);
_mmoda_uninstall_started();
$core_module_settings = variable_get($core_module_name . '_settings');
// Delete module help page nodes
_mmoda_help_pages_delete($core_module_name, $module_name, $instrument);
// Delete module settings
if (_mmoda_core($module_name)) {
variable_del($module_name . '_settings');
} else {
unset($core_module_settings['instruments'][$instrument]);
variable_set($core_module_name . '_settings', $core_module_settings);
}
$field_mmoda_module = field_info_field('field_mmoda_module');
if (isset($field_mmoda_module['settings']['allowed_values'][$module_name])) {
unset($core_module_settings['module_instrument'][$module_name]);
unset($field_mmoda_module['settings']['allowed_values'][$module_name]);
field_update_field($field_mmoda_module);
}
_mmoda_uninstall_finished();
}
/**
* .
*/
function _mmoda_build_tree(array $elements, $parentId = 0)
{
$branch = array();
foreach ($elements as $element) {
if ($element['parent_id'] == $parentId) {
$children = _mmoda_build_tree($elements, $element['id']);
if ($children) {
$element['children'] = $children;
}
$branch[] = $element;
}
}
return $branch;
}
/**
* .
*/
function _mmoda_get_node_root_uuids(array $elements)
{
$all_ids = array();
foreach ($elements as $element) {
$all_ids[] = $element['id'];
}
$root_ids = array();
foreach ($elements as $element) {
if (! in_array($element['parent_id'], $all_ids)) {
$root_ids[] = $element;
}
}
return $root_ids;
}
/**
* .
*/
function _mmoda_import_nodes_tree(array $nodes_tree, &$help_page_nids)
{
foreach ($nodes_tree as $node_tree) {
$help_file = $node_tree['file_name'];
$node_export = file_get_contents($help_file);
if (! empty($node_export)) {
$import_output = node_export_import($node_export);
if ($import_output['success']) {
$node = node_load($import_output['nids'][0]);
$node->uid = 1;
$node->status = 1;
node_save($node);
$help_page_nids[] = $import_output['nids'][0];
drupal_set_message("Help file $help_file imported successfully");
}
if (isset($node_tree['children'])) {
_mmoda_import_nodes_tree($node_tree['children'], $help_page_nids);
}
} else {
throw new Exception("Can not load xml help page file $help_file !");
}
}
}
/**
* .
*/
function _mmoda_php_help_pages_import($core_module_name, $module_name, $instrument = '')
{
$module_path = drupal_get_path('module', $module_name);
$help_pages_dir = $module_path . '/help_book';
if (! is_dir($help_pages_dir))
return;
$book_root_files = array();
$book_parent_files = array();
$not_book_root_files = array();
$dh = @opendir($help_pages_dir);
$pages_hierarchy = array();
if (FALSE !== $dh)
while (($help_file = readdir($dh)) !== FALSE) {
if (preg_match('/\.php$/', $help_file)) {
$help_file = $help_pages_dir . DIRECTORY_SEPARATOR . $help_file;
$code_string = file_get_contents($help_file);
$node = node_export_drupal_import($code_string)[0];
$node_export_book = $node->node_export_book;
if ($node_export_book['#is_root'])
$book_root_files[] = $help_file;
else if ($node_export_book['#book_uuid'] == $node_export_book['#parent_uuid'])
$book_parent_files[] = $help_file;
else
$not_book_root_files[] = $help_file;
$parent_uuid = '';
if (array_key_exists('#parent_uuid', $node->node_export_book))
$parent_uuid = $node->node_export_book['#parent_uuid'];
array_push($pages_hierarchy, array(
'id' => $node->uuid,
'parent_id' => $parent_uuid,
'file_name' => $help_file
));
}
}
else {
throw new Exception("The help pages directory is not readable.\nCan not import help pages !");
}
closedir($dh);
$root_ids = _mmoda_get_node_root_uuids($pages_hierarchy);
$help_page_nids = array();
foreach ($root_ids as $element) {
$nodes_tree = _mmoda_build_tree($pages_hierarchy, $element['id']);
_mmoda_import_nodes_tree(array(
$element
), $help_page_nids);
_mmoda_import_nodes_tree($nodes_tree, $help_page_nids);
}
$module_settings = variable_get($core_module_name . '_settings', array());
if (empty($instrument))
$module_settings['help_page_nids'] = $help_page_nids;
else
$module_settings['instruments'][$instrument]['help_page_nids'] = $help_page_nids;
$module_settings['modules'][$module_name]['just_installed'] = count($help_page_nids);
variable_set($core_module_name . '_settings', $module_settings);
}
/**
* .
*/
function _mmoda_html_help_pages_import($core_module_name, $module_name, $instrument = '')
{
$module_path = drupal_get_path('module', $module_name);
$help_pages_dir = $module_path . '/help_book';
if (! is_dir($help_pages_dir))
return;
$module_settings = variable_get($core_module_name . '_settings', array());
$help_page_title = $module_settings['instruments'][$instrument]['title'];
// get MMOD help pages book id
foreach (book_get_books() as $book)
if ($book['title'] == 'MMODA')
$mmoda_bid = $book['nid'];
$dh = @opendir($help_pages_dir);
$help_page_nids = array();
if (FALSE !== $dh)
while (($help_file = readdir($dh)) !== FALSE) {
if (preg_match('/\.html$/', $help_file)) {
$help_file = $help_pages_dir . DIRECTORY_SEPARATOR . $help_file;
$code_string = file_get_contents($help_file);
$node = new stdClass();
$node->language = LANGUAGE_NONE;
// title
$node->title = $help_page_title;
// field_mmoda_module
$node->field_mmoda_module[LANGUAGE_NONE][0]['value'] = $module_name;
// body
$node->type = "mmoda_help_page";
// Sets some defaults. Invokes hook_prepare() and hook_node_prepare().
node_object_prepare($node);
// Or e.g. 'en' if locale is enabled.
$node->uid = 1;
// Status is 1 or 0; published or not.
$node->status = 1;
// Promote is 1 or 0; promoted to front page or not.
$node->promote = 0;
// Comment is 0, 1, 2; 0 = disabled, 1 = read only, or 2 = read/write.
$node->comment = 0;
$node->body[$node->language][0]['format'] = 'full_html';
$node->body[$node->language][0]['value'] = $code_string;
$node = node_submit($node); // Prepare node for saving
$node->book['bid'] = $mmoda_bid;
$node->book['plid'] = 0;
$node->book['weight'] = _mmoda_get_help_page_weight();
node_save($node);
array_push($help_page_nids, $node->nid);
}
}
else {
throw new Exception("The help pages directory is not readable.\nCan not import help pages !");
}
closedir($dh);
if (empty($instrument))
$module_settings['help_page_nids'] = $help_page_nids;
else
$module_settings['instruments'][$instrument]['help_page_nids'] = $help_page_nids;
$module_settings['modules'][$module_name]['just_installed'] = count($help_page_nids);
variable_set($core_module_name . '_settings', $module_settings);
}
/**
* .
*/
function _mmoda_get_help_page_weight()
{
foreach (book_get_books() as $book)
if ($book['title'] == 'MMODA')
$mmoda_bid = $book['nid'];
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'mmoda_help_page')
->execute();
$nodes = node_load_multiple(array_keys($result['node']));
$node = node_load();
$max_weight = - 1000;
$books = array();
foreach ($nodes as $node) {
if ($node->book['bid'] == $mmoda_bid and $node->book['p3'] == 0)
array_push($books, $node->book);
if ($node->book['bid'] == $mmoda_bid and $node->book['p3'] == 0 and $mmoda_bid and $node->book['weight'] > $max_weight)
$max_weight = $node->book['weight'];
}
return $max_weight + 10;
}
/**
* .
*/
function _mmoda_help_pages_import($core_module_name, $module_name, $instrument = '')
{
$module_path = drupal_get_path('module', $module_name);
$help_pages_dir = $module_path . '/help_book';
if (! is_dir($help_pages_dir))
return;
$book_root_files = array();
$book_parent_files = array();
$not_book_root_files = array();
$dh = @opendir($help_pages_dir);
$pages_hierarchy = array();
$nb_html_files = 0;
$nb_php_files = 0;
if (FALSE !== $dh)
while (($help_file = readdir($dh)) !== FALSE) {
if (preg_match('/\.php$/', $help_file))
$nb_php_files ++;
else if (preg_match('/\.html$/', $help_file))
$nb_html_files ++;
}
else {
throw new Exception("The help pages directory is not readable.\nCan not import help pages !");
}
closedir($dh);
if ($nb_php_files > 0 and $nb_html_files > 0)
return;
else if ($nb_php_files > 0)
_mmoda_php_help_pages_import($core_module_name, $module_name, $instrument);
else if ($nb_html_files > 0)
_mmoda_html_help_pages_import($core_module_name, $module_name, $instrument);
}
function _mmoda_help_pages_delete($core_module_name, $module_name, $instrument = '')
{
$module_settings = variable_get($core_module_name . '_settings', array());
if ($module_name == $core_module_name)
$help_page_nids = $module_settings['help_page_nids'];
else
$help_page_nids = $module_settings['instruments'][$instrument]['help_page_nids'];
// Delete module help pages
node_delete_multiple($help_page_nids);
}
function _mmodaa_instrument_enable($instrument)
{
$core_module_name = basename(__FILE__, '.lib.inc');
$core_module_settings = variable_get($core_module_name . '_settings');
$core_module_settings['instruments'][$instrument]['enabled'] = true;
variable_set($core_module_name . '_settings', $core_module_settings);
}
function _mmodaa_instrument_disable($instrument)
{
$core_module_name = basename(__FILE__, '.lib.inc');
$core_module_settings = variable_get($core_module_name . '_settings');
$core_module_settings['instruments'][$instrument]['enabled'] = false;
variable_set($core_module_name . '_settings', $core_module_settings);
}
/**
* Return all users who have the given role.
*
* @param int|string $role
* Name of the role or the ID or the role.
* @param bool|TRUE $active_user
* Determine, if only the active users should be returned.
* @return array Array of user objects.
*/
function mmoda_get_users_with_role($role, $active_user = TRUE)
{
$users = array();
$rid = 0;
if (is_int($role)) {
$rid = $role;
} else {
if ($role_obj = user_role_load_by_name($role)) {
$rid = $role_obj->rid;
}
}
if ($rid) {
$uids = db_select('users_roles', 'ur')->fields('ur', array(
'uid'
))
->condition('ur.rid', $rid)
->execute()
->fetchCol();
if (! empty($uids)) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'user')->propertyCondition('uid', $uids, 'IN');
if ($active_user) {
$query->propertyCondition('status', 1);
}
$entities = $query->execute();
if (! empty($entities)) {
$users = user_load_multiple(array_keys($entities['user']));
}
}
}
return $users;
}
function mmoda_get_frontend_version()
{
$version = '';
$query = db_select('system', 'sys');
$query->addExpression('CONVERT(sys.info USING utf8)', 'info');
$query->condition('sys.name', 'mmoda');
$query_result = $query->execute()->fetch();
if (!empty($query_result)) {
$unserialized_info = unserialize($query_result->info);
$version = "{$unserialized_info['version']}";
}
else
watchdog('MMODA', 'frontened version not found in the database.');
return $version;
}
function _mmoda_write_log_file($filename, $content)
{
$filename = '/tmp/' . $filename;
$f = fopen($filename, 'w');
fwrite($f, $content);
fclose($f);
chmod($filename, 0777);
}