-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmm_content.inc
5028 lines (4504 loc) · 183 KB
/
mm_content.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// $Id: mm_content.inc 5089 2011-03-01 15:19:22Z dan $
/**
* @file
* Functions related to the retrieval of data for monster menus
*/
// Constants used in mm_content_get()
define('MM_GET_ARCHIVE', '_arch'); // return archive status
define('MM_GET_FLAGS', '_flags'); // return flags
define('MM_GET_PARENTS', '_par'); // return parents
// Constants used in mm_content_get_tree() and mm_content_get_query()
define('MM_GET_TREE_ADD_SELECT', '_sel'); // add to list of selected columns
define('MM_GET_TREE_BIAS_ANON', '_bias'); // assume user 0 can't read any groups
define('MM_GET_TREE_DEPTH', '_depth'); // tree recursion depth
define('MM_GET_TREE_FAKE_READ_BINS', '_read'); // pretend user has read on all bins (used internally)
define('MM_GET_TREE_FILTER_BINS', '_bins'); // return recycle bins
define('MM_GET_TREE_FILTER_DOTS', '_dots'); // return all entries with names starting with '.'
define('MM_GET_TREE_FILTER_GROUPS', '_grps'); // get groups
define('MM_GET_TREE_FILTER_NORMAL', '_norm'); // get entries not group or in /users
define('MM_GET_TREE_FILTER_USERS', '_usrs'); // get entries in /users
define('MM_GET_TREE_MMTID', '_mmtid'); // tree ID to query
define('MM_GET_TREE_NODE', '_node'); // node object to query permissions for
define('MM_GET_TREE_RETURN_BINS', '_rbins'); // return list of parent recycle bins
define('MM_GET_TREE_RETURN_BLOCK', '_rblk'); // return attributes from the mm_tree_block table
define('MM_GET_TREE_RETURN_FLAGS', '_rflgs'); // return attributes from the mm_tree_flags table
define('MM_GET_TREE_RETURN_KID_COUNT', '_rkids'); // return the number of children each entry has
define('MM_GET_TREE_RETURN_MTIME', '_rmods'); // return the mtime and muid fields
define('MM_GET_TREE_RETURN_NODE_COUNT', '_rnode'); // include a "nodecount" field, containing the number of nodes using this entry
define('MM_GET_TREE_RETURN_PERMS', '_rprms'); // return whether or not the user can perform an action
define('MM_GET_TREE_RETURN_TREE', '_rtree'); // return attributes from the mm_tree table
define('MM_GET_TREE_SORT', '_sort'); // sort results by weight, alpha, etc.; always TRUE when depth != 0
define('MM_GET_TREE_USER', '_user'); // user object to test permissions against
define('MM_GET_TREE_WHERE', '_where'); // add a WHERE clause to the outermost query
// Constants used only in mm_content_get_tree()
define('MM_GET_TREE_ADD_TO_CACHE', '_cache'); // add results to the caches used by mm_content_get() and mm_content_get_parents()
define('MM_GET_TREE_BLOCK', '_block'); // retrieve entries which appear in a particular block
define('MM_GET_TREE_FILTER_HIDDEN', '_hide'); // get "hidden" entries
define('MM_GET_TREE_HERE', '_here'); // list of tree IDs currently being viewed
define('MM_GET_TREE_ITERATOR', '_iter'); // getTreeIterator (or subclass)
define('MM_GET_TREE_PRUNE_PARENTS', '_ppar'); // prune parents depending upon max_parents
define('MM_GET_TREE_VIRTUAL', '_virt'); // include virtual user list sub-entries
define('MM_GET_TREE_STATE_COLLAPSED', (1<<0));
define('MM_GET_TREE_STATE_DENIED', (1<<1));
define('MM_GET_TREE_STATE_EXPANDED', (1<<2));
define('MM_GET_TREE_STATE_HERE', (1<<3));
define('MM_GET_TREE_STATE_HIDDEN', (1<<4));
define('MM_GET_TREE_STATE_LEAF', (1<<5));
define('MM_GET_TREE_STATE_RECYCLE', (1<<6));
// Constants used in mm_content_copy()
define('MM_COPY_ALIAS', 'alia');
define('MM_COPY_COMMENTS', 'comm');
define('MM_COPY_CONTENTS', 'cont');
define('MM_COPY_ITERATE_ALTER', 'itra');
define('MM_COPY_NAME', 'name');
define('MM_COPY_NODE_PRESAVE_ALTER', 'noda');
define('MM_COPY_OWNER', 'ownr');
define('MM_COPY_READABLE', 'read');
define('MM_COPY_RECUR', 'recr');
define('MM_COPY_TREE_PRESAVE_ALTER', 'trea');
define('MM_COPY_TREE_SKIP_DUPS', 'tdup');
// Constants present in mm_tree.name
define('MM_ENTRY_NAME_DEFAULT_USER', '.Default');
define('MM_ENTRY_NAME_DISABLED_USER', '.Disabled');
define('MM_ENTRY_NAME_GROUPS', '.Groups');
define('MM_ENTRY_NAME_RECYCLE', '.Recycle');
define('MM_ENTRY_NAME_USERS', '.Users');
define('MM_ENTRY_NAME_VIRTUAL_GROUP', '.Virtual');
// The maximum number of sub-items per item in the tree is
// MM_CONTENT_BTOA_BASE ^ MM_CONTENT_BTOA_CHARS. If you might have more than
// this many /Users (or any other level of the tree) someday, increase
// MM_CONTENT_BTOA_CHARS and run mm_content_update_sort(). A larger
// MM_CONTENT_BTOA_BASE cannot be used, unless you are using a case-sensitive
// collation on the mmtree.sort_idx database column.
//
// The maximum nesting level of the tree is the length of mm_tree.sort_idx /
// MM_CONTENT_BTOA_CHARS. While you can increase this by altering the schema,
// you may find that MySQL starts to complain about there being too many tables
// in the JOIN in custom_url_rewrite_inbound(). MM_CONTENT_MYSQL_MAX_JOINS is
// used in monster_menus.install to ensure that this limit isn't exceeded.
define('MM_CONTENT_BTOA_START', ord('!'));
define('MM_CONTENT_BTOA_BASE', 64);
define('MM_CONTENT_BTOA_CHARS', 4);
define('MM_CONTENT_MYSQL_MAX_JOINS', 61);
// Constants related to the virtual group "dirty" field
define('MM_VGROUP_DIRTY_NOT', 0); // not dirty
define('MM_VGROUP_DIRTY_NEXT_CRON', 1); // update during next hook_cron()
define('MM_VGROUP_DIRTY_FAILED', 2); // previously failed sanity check
define('MM_VGROUP_DIRTY_REDO', 3); // failed, but OK to regenerate
// If the count of users in a virtual group decreases by more than this ratio,
// return an error message and stop the vgroup regeneration. Set the matching
// record's "dirty" field in mm_vgroup_query to MM_VGROUP_DIRTY_REDO to ignore
// this condition and regenerate the group during the next run.
define('MM_VGROUP_COUNT_SANITY', 0.20);
class mmRenderer {
public function __construct() {
// To avoid having to sort, this array must be in increasing order
$this->state_to_css = array( // Do not translate these with t()
MM_GET_TREE_STATE_COLLAPSED => 'collapsed',
MM_GET_TREE_STATE_DENIED => 'denied',
MM_GET_TREE_STATE_EXPANDED => 'expanded',
MM_GET_TREE_STATE_HERE => 'here',
MM_GET_TREE_STATE_HIDDEN => 'hidden-entry',
MM_GET_TREE_STATE_LEAF => 'leaf',
MM_GET_TREE_STATE_RECYCLE => 'recycle-bin',
);
$this->state_cache = array();
$keys = array_keys($this->state_to_css);
$this->state_max = array_pop($keys);
}
public function state_class($leaf) {
if (!isset($this->state_cache[$leaf->state])) {
$state = $leaf->state;
$state_css = array();
$i = 0;
$bit = 0;
while ($state && $bit != $this->state_max) {
$bit = 1 << $i++;
if ($state & $bit && isset($this->state_to_css[$bit])) {
$state_css[] = $this->state_to_css[$bit];
$state ^= $bit;
}
}
$this->state_cache[$leaf->state] = join(' ', $state_css);
}
return $this->state_cache[$leaf->state];
}
public function prefix_leaf($leaf, $item) {
return '<li class="' . $this->state_class($leaf) . '">';
}
public function link_leaf($leaf, $item, $link_item) {
// if( $leaf->nodecount === '0' ) $item['title'] .= t(' [no pages]');
$atts = !empty($item['description']) ? array('title' => $item['description']) : array();
if ($link_item['path'] != $_GET['q'] &&
($leaf->state & (MM_GET_TREE_STATE_EXPANDED|MM_GET_TREE_STATE_HERE)) == (MM_GET_TREE_STATE_EXPANDED|MM_GET_TREE_STATE_HERE))
$atts['class'] = 'active';
if ($leaf->state & MM_GET_TREE_STATE_HIDDEN) $item['title'] .= ' ' . t('(hidden)');
if ($leaf->state & MM_GET_TREE_STATE_DENIED) $atts['rel'] = 'nofollow';
return l($item['title'], $link_item['path'], array('attributes' => $atts, 'query' => isset($item['query']) ? $item['query'] : NULL));
}
public function suffix_leaf($leaf, $item) {
return "</li>\n";
}
public function prefix_parent($leaf, $item) {
return "\n<ul class=\"menu\">";
}
public function suffix_parent($leaf, $item) {
return "</ul>\n";
}
public function is_visible($leaf) {
if ($leaf->name == MM_ENTRY_NAME_RECYCLE) {
return mm_content_user_can_recycle($leaf->mmtid, 'r');
}
if (isset($leaf->perms) && ($leaf->perms['w'] || $leaf->perms['a'] || $leaf->perms['u'])) {
return TRUE;
}
return !variable_get('mm_hide_empty_pages', FALSE) || $leaf->nodecount !== '0'; // returns true if $leaf->nodecount is not set
}
}
global $_mm_content_default_renderer;
$_mm_content_default_renderer = new mmRenderer();
/**
* Render an entry and its children as HTML
*
* @param $tree
* Array of tree nodes, as generated by mm_get_tree
* @param &$i
* Optional starting index
* @param &$path
* Optional menu array, suitable to be passed to menu_set_location()
* @param &$parents
* Optional array containing the full GET path of the current item. Used
* internally.
* @param $depth
* Depth to display; set to -1 to show all levels
* @param $renderer
* mmRenderer (or subclass) to use in rendering the HTML. If omitted,
* use $_mm_content_default_renderer, which produces a simple menu tree.
* @return
* The HTML code
*/
function mm_content_render_tree($tree, &$i = 0, &$path = NULL, &$parents = array('mm'), $depth = -1, $renderer = NULL) {
if ($i >= count($tree)) return;
if (!isset($renderer)) {
global $_mm_content_default_renderer;
$renderer = $_mm_content_default_renderer;
}
$lev0 = $tree[$i]->level;
while ($i < count($tree) && $tree[$i]->level == $lev0) {
$leaf = $tree[$i];
$name = mm_content_expand_name($leaf->name);
if (!$renderer->is_visible($leaf) ||
$name[0] == '.' && !(isset($leaf->perms) ?
$leaf->perms['r'] : mm_content_user_can($leaf->mmtid, 'r'))) {
while (++$i < count($tree) && $tree[$i]->level > $lev0) ; // skip kids
continue; // get next sibling
}
$item = array('title' => $name);
if ($leaf->hover) $item['description'] = $leaf->hover;
$link_item = array('path' => implode('/', $parents) . '/' . $leaf->mmtid);
if (is_array($path) && $leaf->state & MM_GET_TREE_STATE_EXPANDED) {
$path[] = array(
'path' => $link_item['path'],
'title' => $name,
'type' => MENU_DYNAMIC_ITEM);
}
$output .= $renderer->prefix_leaf($leaf, $item) .
$renderer->link_leaf($leaf, $item, $link_item);
while (++$i < count($tree) && $tree[$i]->level > $lev0) {
if ($tree[$i]->level == $lev0 + 1 && ($depth || $leaf->state & MM_GET_TREE_STATE_EXPANDED)) {
$parents[] = $leaf->mmtid;
$output .= $renderer->prefix_parent($tree[$i], $item) .
mm_content_render_tree($tree, $i, $path, $parents, 0, $renderer) .
$renderer->suffix_parent($tree[$i], $item);
array_pop($parents);
$i--;
}
}
$output .= $renderer->suffix_leaf($leaf, $item);
}
return $output;
}
/**
* Return a nicer version of entry names starting with '.'
*
* @param $name
* The entry name, obtained from mm_content_get_tree
* @return
* The nicer version
*/
function mm_content_expand_name($name) {
static $aliases;
if (!isset($aliases)) {
$aliases = array(
MM_ENTRY_NAME_DEFAULT_USER => t('[New account defaults]'),
MM_ENTRY_NAME_DISABLED_USER => t('[Disabled accounts]'),
MM_ENTRY_NAME_GROUPS => t('Permission groups'),
MM_ENTRY_NAME_RECYCLE => t('[Recycle bin]'),
MM_ENTRY_NAME_USERS => t('User list'),
MM_ENTRY_NAME_VIRTUAL_GROUP => t('[Pre-defined groups]'),
);
$aliases += mm_module_invoke_all('mm_item_name');
}
if (isset($aliases[$name])) return $aliases[$name];
return $name;
}
/**
* Class used with mm_content_get_tree to take an action as each node is found.
* IMPORTANT: Do not depend on $item->state in iterate(). It is not correct.
*/
class getTreeIterator {
private $parent_is_group, $parent_is_user;
public function iterate($item) {
// function must return 1 if no error, 0 if error, -1 if this node
// and any of its children should be skipped
return 1;
}
}
/**
* Traverse the tree
*
* @param $mmtid (1)
* Starting tree ID
* @param $params
* An array containing parameters. The array is indexed using the constants
* below.
* - MM_GET_TREE_ADD_SELECT (none):
* A string or array of strings to add to the SELECT portion of the query
* - MM_GET_TREE_ADD_TO_CACHE (FALSE):
* Add results to the caches used by mm_content_get() and
* mm_content_get_parents()
* - MM_GET_TREE_BIAS_ANON (TRUE):
* If TRUE, assume user 0 can't read any groups (more secure)
* - MM_GET_TREE_BLOCK (0):
* Only retrieve entries that are part of one block. Defaults to all blocks.
* - MM_GET_TREE_DEPTH (-1):
* When 'mmtid' is used, a query to return all items in the tree below that
* point can be returned. This field specifies the depth of recursion:
* - 0: just the item specified by $mmtid
* - -1: all levels
* - 1: the item and its immediate children
* - N: any other other number will return that many levels (can be slow)
* - MM_GET_TREE_FAKE_READ_BINS (FALSE):
* Pretend the user can read all recycle bins (used internally)
* - MM_GET_TREE_FILTER_BINS (TRUE):
* Get entries that are recycle bins
* - MM_GET_TREE_FILTER_DOTS (TRUE):
* Get all entries with names that start with '.'. If FALSE, only .Groups,
* .Users, and .Virtual are returned.
* - MM_GET_TREE_FILTER_GROUPS (TRUE):
* Get entries that are groups
* - MM_GET_TREE_FILTER_HIDDEN (FALSE):
* If TRUE, return entries with the "hidden" attribute set, even if the
* current user does not normally have permission to view them
* - MM_GET_TREE_FILTER_NORMAL (TRUE):
* Get entries that are neither groups nor in /users
* - MM_GET_TREE_FILTER_USERS (TRUE):
* Get entries in /users
* - MM_GET_TREE_HERE (none)
* An array of MM Tree IDs currently being viewed by the user. Parent
* entries will have their their state set to MM_GET_TREE_STATE_EXPANDED.
* - MM_GET_TREE_ITERATOR (none):
* getTreeIterator (or subclass) to call as each new item is found.
* When this option is used, memory is conserved by not returning anything.
* - MM_GET_TREE_PRUNE_PARENTS (FALSE):
* If TRUE, prune parents, depending upon max_parents in the block
* - MM_GET_TREE_RETURN_BINS (FALSE):
* A comma-separated list of the mmtids of any parent recycle bins
* - MM_GET_TREE_RETURN_BLOCK (FALSE):
* Attributes from the mm_tree_block table
* - MM_GET_TREE_RETURN_FLAGS (FALSE):
* Flags from the mm_tree_flags table
* - MM_GET_TREE_RETURN_KID_COUNT (FALSE):
* A count of the number of children each tree entry has
* - MM_GET_TREE_RETURN_MTIME (FALSE):
* The muid (user ID who made the last modification) and mtime (time) of the
* modification
* - MM_GET_TREE_RETURN_NODE_COUNT (FALSE):
* If TRUE, return a count of the number of nodes assigned to each item. If
* a string or array of strings, return a count of the number of nodes of
* that type.
* - MM_GET_TREE_RETURN_PERMS (none):
* If set, return whether or not the user can perform that action ('r', 'w',
* 'a', 'u', 'IS_USER', 'IS_GROUP', 'IS_RECYCLE_BIN', 'IS_RECYCLED'). The
* requested permission can either be a single value or an array. If an
* empty array or TRUE is passed, all permissions are returned.
* - MM_GET_TREE_SORT (FALSE):
* If TRUE, sort the entries according to sort_idx; always TRUE when
* MM_GET_TREE_DEPTH != 0
* - MM_GET_TREE_USER (current user):
* User object to test permissions against
* - MM_GET_TREE_VIRTUAL (TRUE):
* Include virtual user list sub-entries
* - MM_GET_TREE_WHERE (none):
* Add a WHERE clause to the outermost query
* If none of ([...USERS], [...GROUPS], [...NORMAL]) is TRUE, all types are
* retrieved. MM_GET_TREE_RETURN_TREE is always TRUE.
* @return
* Array of tree entries, unless $iter is used
*/
function mm_content_get_tree($mmtid = 1, $params = NULL) {
global $user;
$defaults = array(
MM_GET_TREE_BLOCK => 0,
MM_GET_TREE_DEPTH => -1,
MM_GET_TREE_FILTER_BINS => TRUE,
MM_GET_TREE_FILTER_DOTS => TRUE,
MM_GET_TREE_FILTER_HIDDEN => FALSE,
MM_GET_TREE_HERE => NULL,
MM_GET_TREE_ITERATOR => NULL,
MM_GET_TREE_PRUNE_PARENTS => FALSE,
MM_GET_TREE_USER => $user,
MM_GET_TREE_VIRTUAL => TRUE,
'found' => -1,
'level' => 0,
'parent_level' => -1,
'pprune' => -1,
'q' => NULL,
);
if (!is_array($params)) $params = array();
$params = array_merge($defaults, $params);
if (!$params[MM_GET_TREE_FILTER_GROUPS] && !$params[MM_GET_TREE_FILTER_USERS] && !$params[MM_GET_TREE_FILTER_NORMAL]) {
$params[MM_GET_TREE_FILTER_GROUPS] = $params[MM_GET_TREE_FILTER_USERS] = $params[MM_GET_TREE_FILTER_NORMAL] = TRUE;
}
if ($params[MM_GET_TREE_VIRTUAL] && !variable_get('mm_use_virtual_user_dir', TRUE)) {
$params[MM_GET_TREE_VIRTUAL] = FALSE;
}
if ($params[MM_GET_TREE_SORT] || $params[MM_GET_TREE_DEPTH] != 0) mm_content_update_sort_queue();
return _mm_content_get_tree($mmtid, $params);
}
function _mm_content_get_tree($mmtid, &$params) {
$users_mmtid = mm_content_users_mmtid();
if (!$params['q']) {
$params['q'] = _mm_content_get_tree_query($mmtid, $params);
if (is_array($params[MM_GET_TREE_HERE]))
foreach ($params[MM_GET_TREE_HERE] as $i => $h)
if ($h < 0) {
unset($params[MM_GET_TREE_HERE][$i]);
$have_virtual = TRUE;
if ($params[MM_GET_TREE_DEPTH] > 0) $params[MM_GET_TREE_DEPTH]--;
break;
}
else if ($params[MM_GET_TREE_VIRTUAL] && $h == $users_mmtid && $params[MM_GET_TREE_DEPTH]) {
$have_virtual = TRUE;
}
}
$rows = array();
while ($r = $params['q']->next()) {
if (!isset($params['q']->start_level)) $params['q']->start_level = strlen($r->sort_idx) / MM_CONTENT_BTOA_CHARS;
$r->level = strlen($r->sort_idx) / MM_CONTENT_BTOA_CHARS - $params['q']->start_level + $params['q']->level_offset;
if ($r->level <= $params['parent_level']) {
$params['q']->back();
break;
}
else if ($r->level > $params['parent_level']) {
if (is_null($r->bid) || !isset($r->bid)) {
$r->bid = 0;
$r->max_depth = $r->max_parents = -1;
}
$add = _mm_content_get_tree_recurs($r, $params, $parent_is_group, $parent_is_user, $last);
}
if (is_array($rows) && is_array($add)) {
$rows = array_merge($rows, $add);
unset($add); // save some memory
}
if ($last) break;
}
if ($params['pprune'] > 0 && $params['found']) $params['pprune']--;
if (isset($params[MM_GET_TREE_ITERATOR])) return NULL;
if (!$params['level'] && ($have_virtual || $mmtid == $users_mmtid) && $params[MM_GET_TREE_DEPTH]) {
if ($params[MM_GET_TREE_VIRTUAL]) {
$letters = mm_query_result("SELECT GROUP_CONCAT(DISTINCT UCASE(SUBSTR(name, %d, %d)) ORDER BY name SEPARATOR '') FROM {mm_tree} WHERE parent = %d", 1, 1, $users_mmtid);
$letters = preg_replace('/[\W_]/', '', $letters, -1, $matches);
if ($matches) $letters = "~$letters";
$letters = str_split($letters);
$parent = NULL;
for ($i = 0; $i < count($rows); $i++) {
if ($rows[$i]->mmtid == $users_mmtid) {
$parent = $rows[$remainder = $i];
$parent->state &= ~MM_GET_TREE_STATE_HERE;
$last = 0;
while (++$i < count($rows) && $rows[$i]->level > $parent->level) {
if ($rows[$i]->level == $parent->level + 1) {
$letr = drupal_strtoupper($rows[$i]->name[0]);
$name = ctype_alpha($letr) ? $letr : t('(other)');
if (!$last || $name != $rows[$last]->name) {
$alias = ctype_alpha($letr) ? $letr : '~';
while ($letters) {
$add = array_shift($letters);
$new = _mm_content_virtual_dir(-ord($add), $parent->mmtid, $parent->level + 1, $add == $alias ? MM_GET_TREE_STATE_EXPANDED|MM_GET_TREE_STATE_HERE : MM_GET_TREE_STATE_COLLAPSED);
$new->default_mode = $parent->default_mode;
array_splice($rows, $last = $i++, 0, array($new)); // insert virtual dir
$remainder++;
if ($add == $alias) break;
}
}
$rows[$i]->parent = $rows[$last]->mmtid;
if ($rows[$i]->state & MM_GET_TREE_STATE_EXPANDED) $rows[$last]->state = MM_GET_TREE_STATE_EXPANDED;
} // if
$remainder++;
$rows[$i]->level++;
} // while
break; // exit outer for loop
} // if
} // for
}
if (!user_access('administer all users')) {
$hidden_names = variable_get('mm_hidden_user_names', array());
$dels = array();
foreach ($rows as $i => $r) {
if ($r->alias == '~') {
$other = $i;
}
else if ($r->parent == -126 || $r->parent == $users_mmtid) { // -126 = -ord('~')
if (in_array($r->name, $hidden_names)) {
$dels[] = $i;
}
else {
unset($other);
}
}
}
if (isset($other)) {
// All 'other' rows are invisible to the user
array_unshift($dels, $other);
}
foreach (array_reverse($dels) as $i)
array_splice($rows, $i, 1);
}
if ($params[MM_GET_TREE_VIRTUAL]) {
$i = $parent ? $remainder + 1 : count($rows);
foreach ($letters as $add) {
$new = _mm_content_virtual_dir(-ord($add), $users_mmtid, $parent ? $parent->level + 1 : 0, MM_GET_TREE_STATE_COLLAPSED);
if ($parent) $new->default_mode = $parent->default_mode;
array_splice($rows, $i++, 0, array($new)); // insert virtual dir
}
}
}
// if( !$params['level'] ) debug_add_dump( $rows );
return $rows;
}
/**
* Helper function for _mm_content_get_tree()/mm_content_get()
*/
function _mm_content_split_flags($flags) {
if (is_array($flags)) return $flags;
preg_match_all('/(?:(.*?)\|1(.*?)(?:\|2|$))/', $flags, $matches);
return $matches[0] ? array_combine($matches[1], $matches[2]) : array();
}
/**
* Helper function for _mm_content_get_tree()
*/
function _mm_content_get_tree_query($mmtid, $params) {
$params[MM_GET_TREE_RETURN_TREE] = TRUE;
if (isset($params[MM_GET_TREE_BLOCK]) && $params[MM_GET_TREE_BLOCK] != 0) $params[MM_GET_TREE_RETURN_BLOCK] = TRUE;
if (!is_array($params[MM_GET_TREE_HERE])) $params[MM_GET_TREE_HERE] = array($mmtid);
else if (!count($params[MM_GET_TREE_HERE])) $params[MM_GET_TREE_HERE][] = $mmtid;
else if ($params[MM_GET_TREE_DEPTH] != 0) $params[MM_GET_TREE_DEPTH] = 1;
$query = array();
$max = count($params[MM_GET_TREE_HERE]) - 1;
$users_mmtid = $params[MM_GET_TREE_VIRTUAL] ? mm_content_users_mmtid() : -1;
if (isset($params[MM_GET_TREE_RETURN_PERMS])) {
if (!isset($params[MM_GET_TREE_ITERATOR])) {
$params[MM_GET_TREE_RETURN_BINS] = TRUE;
$params[MM_GET_TREE_FAKE_READ_BINS] = TRUE;
}
}
else {
$params[MM_GET_TREE_RETURN_PERMS] = array('IS_GROUP', 'IS_USER');
}
for ($i = 0; $i <= $max; $i++) {
$mmtid = $params[MM_GET_TREE_HERE][$i];
if ($mmtid != $users_mmtid || $i == $max || $params[MM_GET_TREE_HERE][$i + 1] >= 0) {
$params2 = $params;
if ($mmtid < 0) {
$ch = chr(-$mmtid);
$re = $ch == '~' ? "t.name REGEXP '^[^[:alpha:]]'" : "UCASE(t.name) LIKE '$ch%'";
$params2[MM_GET_TREE_INNER_FILTER] = " AND $re";
$params2[MM_GET_TREE_DEPTH] = 1;
$params2[MM_GET_TREE_MMTID] = $users_mmtid;
}
else {
$params2[MM_GET_TREE_DEPTH] = $mmtid == $users_mmtid ? 0 : 1;
$params2[MM_GET_TREE_MMTID] = $mmtid;
}
if ($i == $max) {
if ($mmtid != $users_mmtid) $params2[MM_GET_TREE_DEPTH] = $params[MM_GET_TREE_DEPTH];
$params2[MM_GET_TREE_BLOCK] = $params[MM_GET_TREE_BLOCK];
$params2[MM_GET_TREE_SORT] = $params[MM_GET_TREE_DEPTH] != 0 || $params[MM_GET_TREE_SORT];
$query[] = mm_content_get_query($params2);
}
else {
$params2[MM_GET_TREE_BLOCK] = 0;
$params2[MM_GET_TREE_SORT] = FALSE;
$query[] = preg_replace('/ ORDER BY NULL$/', '', mm_content_get_query($params2));
}
}
}
// debug_add_dump($mode, $params[MM_GET_TREE_HERE], db_prefix_tables(join(' UNION ', $query)));
$params['q'] = mm_query(join(' UNION ', $query));
$params['q']->level_offset = $params['level'];
return $params['q'];
}
function _mm_content_get_tree_recurs($r, $params, $parent_is_group, $parent_is_user, &$last) {
global $_mmtbt_cache, $_mmgp_cache, $_mmuc_cache;
$rows = array();
$last = TRUE;
$xlate = array('w', 'a', 'u', 'r', 'IS_GROUP', 'IS_USER', 'ADMIN', 'IS_RECYCLE_BIN', 'IS_RECYCLED');
foreach ($xlate as $field)
if (isset($r->$field)) {
if (!isset($r->perms)) $r->perms = array();
$r->perms[$field] = $r->$field != 0;
unset($r->$field);
}
if ($params[MM_GET_TREE_RETURN_PERMS] && !isset($params[MM_GET_TREE_ITERATOR])) {
if ($r->perms['IS_RECYCLE_BIN']) {
$r->perms['u'] = TRUE;
$r->perms['r'] = mm_content_user_can_recycle($r->mmtid, 'r', $params[MM_GET_TREE_USER]);
}
else if (isset($r->recycle_bins)) {
foreach (split(',', $r->recycle_bins) as $bin) {
$r->perms['r'] = $r->perms['r'] && mm_content_user_can_recycle($bin, 'r', $params[MM_GET_TREE_USER]);
}
}
}
if ($params[MM_GET_TREE_ADD_TO_CACHE]) {
if (!isset($_mmtbt_cache[$r->mmtid])) {
$_mmtbt_cache[$r->mmtid] = $r;
}
if (!isset($_mmgp_cache[$r->mmtid])) {
$_mmgp_cache[$r->mmtid] = $r->parent;
}
if (isset($r->perms))
foreach ($r->perms as $field => $val)
if (!isset($_mmuc_cache[$r->mmtid][$params[MM_GET_TREE_USER]->uid][$field]))
$_mmuc_cache[$r->mmtid][$params[MM_GET_TREE_USER]->uid][$field] = $val;
}
if ($params[MM_GET_TREE_RETURN_FLAGS]) {
$r->flags = _mm_content_split_flags($r->flags);
}
if (!isset($r->is_group)) $r->is_group =
$parent_is_group || $r->name == MM_ENTRY_NAME_GROUPS ||
isset($params[MM_GET_TREE_ITERATOR]) && $params[MM_GET_TREE_ITERATOR]->parent_is_group ||
(isset($r->perms) ? $r->perms['IS_GROUP'] : mm_content_user_can($r->mmtid, 'IS_GROUP'));
if (!isset($r->is_user)) $r->is_user =
$parent_is_user || $r->name == MM_ENTRY_NAME_USERS ||
isset($params[MM_GET_TREE_ITERATOR]) && $params[MM_GET_TREE_ITERATOR]->parent_is_user ||
(isset($r->perms) ? $r->perms['IS_USER'] : mm_content_user_can($r->mmtid, 'IS_USER'));
$r->is_dot = $r->name[0] == '.';
if ($r->is_group) unset($r->nodecount);
$visible = ($params[MM_GET_TREE_FILTER_GROUPS] || !$r->is_group) &&
($params[MM_GET_TREE_FILTER_NORMAL] || $r->is_group || $r->is_user) &&
($params[MM_GET_TREE_FILTER_USERS] || !$r->is_user);
if ($r->is_user && in_array($r->name, variable_get('mm_hidden_user_names', array()))) {
$r->bid = -1;
}
if ($visible || $r->mmtid == 1) {
if ($r->is_group || $r->name == MM_ENTRY_NAME_USERS || $r->mmtid == 1) {
unset($r->nodecount);
}
$params2 = $params;
if (is_array($params[MM_GET_TREE_HERE])) $params2[MM_GET_TREE_HERE] =& $params[MM_GET_TREE_HERE];
$params2[MM_GET_TREE_DEPTH] = $params[MM_GET_TREE_DEPTH] < 0 ? -1 : $params[MM_GET_TREE_DEPTH] - 1;
if (count($params[MM_GET_TREE_HERE]) && $r->mmtid == $params[MM_GET_TREE_HERE][0]) {
$r->state = count($params[MM_GET_TREE_HERE]) >= 2 ? MM_GET_TREE_STATE_EXPANDED : MM_GET_TREE_STATE_EXPANDED|MM_GET_TREE_STATE_HERE;
array_shift($params[MM_GET_TREE_HERE]);
if ($params[MM_GET_TREE_BLOCK] && ($r->bid < 0 && $r->max_depth >= 0 || $r->bid > 0)) {
$depth_new = $r->max_depth;
if ($depth_new == -1) {
$params2[MM_GET_TREE_DEPTH] = $params[MM_GET_TREE_DEPTH] = -1;
}
else {
$params[MM_GET_TREE_DEPTH] = $depth_new;
$params2[MM_GET_TREE_DEPTH] = $depth_new - 1;
}
}
else if ($params[MM_GET_TREE_DEPTH] < 0 || $params[MM_GET_TREE_DEPTH] > 2) {
$params[MM_GET_TREE_DEPTH] = count($params[MM_GET_TREE_HERE]) + 2;
$params2[MM_GET_TREE_DEPTH] = $params[MM_GET_TREE_DEPTH] - 1;
}
$params['found'] = $r->mmtid;
if ($params[MM_GET_TREE_PRUNE_PARENTS] && $r->max_parents != '' && $r->max_parents >= 0) {
$params['pprune'] = $r->max_parents + 2;
}
}
else {
$r->state = $params[MM_GET_TREE_DEPTH] && $r->parent <= 0 ? MM_GET_TREE_STATE_EXPANDED : (isset($r->kids) && $r->kids > 0 ? MM_GET_TREE_STATE_COLLAPSED : MM_GET_TREE_STATE_LEAF);
if (is_array($params[MM_GET_TREE_HERE])) {
$params2[MM_GET_TREE_DEPTH] = 0;
$params2['once'] = TRUE;
foreach (array(MM_GET_TREE_PRUNE_PARENTS, MM_GET_TREE_RETURN_NODE_COUNT) as $mode)
$params2[$mode] = FALSE;
}
}
if ((!$params[MM_GET_TREE_BLOCK] || $r->bid == $params[MM_GET_TREE_BLOCK] || $r->bid <= 0) && (!$r->hidden || !$r->level || $params[MM_GET_TREE_FILTER_HIDDEN] || isset($params[MM_GET_TREE_ITERATOR]) || !isset($r->perms) || $r->perms['w'] || $r->perms['a'] || $r->perms['u'] || user_access('view all menus'))) {
if (!isset($params[MM_GET_TREE_ITERATOR])) $parent = count($rows);
if ($r->state) {
if ($r->hidden) {
$r->state |= MM_GET_TREE_STATE_HIDDEN;
}
else if ($r->name == MM_ENTRY_NAME_RECYCLE) {
$r->state |= MM_GET_TREE_STATE_RECYCLE;
}
if (!$r->is_group && isset($r->perms['r']) && !$r->perms['r']) {
$r->state |= MM_GET_TREE_STATE_DENIED;
$skip_kids = TRUE;
}
if ($visible) {
if (!isset($params[MM_GET_TREE_ITERATOR])) {
$rows[] = $r;
if ($params['once']) return $rows;
}
else if ($params['once']) return;
else if (($iter_ok = $params[MM_GET_TREE_ITERATOR]->iterate($r)) < 0) {
$last = FALSE;
$skip_kids = TRUE;
}
else if (!$iter_ok) return;
}
}
if (!isset($skip_kids) && $params[MM_GET_TREE_DEPTH]) {
if (isset($params[MM_GET_TREE_ITERATOR])) {
$ois_grp = $params[MM_GET_TREE_ITERATOR]->parent_is_group;
$ois_user = $params[MM_GET_TREE_ITERATOR]->parent_is_user;
$params[MM_GET_TREE_ITERATOR]->parent_is_group = $r->is_group;
$params[MM_GET_TREE_ITERATOR]->parent_is_user = $r->is_user;
}
$params2['found'] = -1;
if ($params2['once']) $params2['pprune'] = -1;
$params2['level'] = $params['level'] + 1;
$params2['parent_level'] = $r->level;
$kids = _mm_content_get_tree($r->mmtid, $params2);
if ($params2['pprune'] >= 0)
if ($params2['pprune'] == 0) {
$params['pprune'] = 0;
return $kids;
}
else if ($params2['found']) $params['pprune'] = $params2['pprune'];
if (isset($params[MM_GET_TREE_ITERATOR])) {
$params[MM_GET_TREE_ITERATOR]->parent_is_group = $ois_grp;
$params[MM_GET_TREE_ITERATOR]->parent_is_user = $ois_user;
}
else {
if (count($rows) > $parent) {
if ($rows[$parent]->is_group)
foreach ($kids as $k) {
$k->is_group = TRUE;
unset($k->nodecount);
}
if ($rows[$parent]->is_user)
foreach ($kids as $k) {
$k->is_user = TRUE;
$k->is_user_home = $k->level == $rows[$parent]->level + 1 &&
$rows[$parent]->name == MM_ENTRY_NAME_USERS;
}
if ($params['found'] != $r->mmtid) {
$rows[$parent]->state &= ~(MM_GET_TREE_STATE_EXPANDED|MM_GET_TREE_STATE_COLLAPSED|MM_GET_TREE_STATE_LEAF);
$rows[$parent]->state |= $params2['found'] >= 0 ? MM_GET_TREE_STATE_EXPANDED :
(count($kids) || isset($r->kids) && $r->kids > 0 ? MM_GET_TREE_STATE_COLLAPSED : MM_GET_TREE_STATE_LEAF);
}
}
if (!$params['once'] && is_array($kids)) $rows = array_merge($rows, $kids);
}
if ($params2['found'] >= 0) $params['found'] = $params2['found'];
} // if( $params[MM_GET_TREE_DEPTH] )
else $skip_kids = TRUE;
} // if( !$params[MM_GET_TREE_BLOCK] || ...
else $skip_kids = TRUE;
} // if( $visible || $r->parent<=0 )
else $skip_kids = TRUE;
if (isset($skip_kids)) {
while (($row = $params['q']->next()) && strlen($row->sort_idx) > strlen($r->sort_idx));
if ($row) $params['q']->back();
}
$last = FALSE;
return $rows;
}
/**
* Get the cascaded (inherited by children) settings for an entry
*
* @param $mmtid
* ID of the entry to load settings for. If NULL, return a list of possible
* settings and their data representation. The structure of the returned array
* in this case is:
* - data_type: 'int' (integer) or 'string'
* - multiple: TRUE if multiple values are accepted
* - user_access: user must have user_access() for this value in order to set
* the setting
* - not_empty: TRUE if only !empty() values should be stored
* @return
* array containing the settings
*/
function mm_content_get_cascaded_settings($mmtid = NULL, $name = NULL) {
static $cascaded_settings;
if (is_null($mmtid)) {
if (!isset($cascaded_settings)) {
$cascaded_settings = array(
'allow_reorder' => array('data_type' => 'int', 'user_access' => 'administer all menus'),
'allowed_themes' => array('data_type' => 'string', 'multiple' => TRUE, 'user_access' => 'administer all menus'),
'allowed_node_types' => array('data_type' => 'string', 'multiple' => TRUE, 'user_access' => 'administer all menus'),
'comments_readable' => array('data_type' => 'string', 'not_empty' => TRUE),
'nodes_per_page' => array('data_type' => 'int', 'not_empty' => TRUE),
);
// check for mm_cascaded_settings hooks
$cascaded_settings = array_merge($cascaded_settings, module_invoke_all('mm_cascaded_settings'));
}
return $cascaded_settings;
}
$cascaded = array();
$q = mm_query($mmtid, "SELECT * FROM {mm_cascaded_settings} WHERE mmtid = %d", $mmtid);
while ($r = $q->next()) {
if ($r->data_type == 'int') $r->data = (int)$r->data;
if ($r->multiple) {
if (!is_array($cascaded[$r->name])) $cascaded[$r->name] = array();
if ($r->array_key != '') $cascaded[$r->name][$r->array_key] = $r->data;
else $cascaded[$r->name][] = $r->data;
}
else $cascaded[$r->name] = $r->data;
}
return isset($name) ? $cascaded[$name] : $cascaded;
}
/**
* Set the cascaded (inherited by children) settings for an entry
*
* @param $mmtid
* Tree ID of the entry to set settings for
* @param $settings
* Array containing the settings
* @param $delete
* If TRUE, delete the old settings first
*/
function mm_content_set_cascaded_settings($mmtid, $settings, $delete = TRUE) {
static $cascaded_settings;
if (!isset($cascaded_settings)) $cascaded_settings = mm_content_get_cascaded_settings();
if ($delete) db_query('DELETE FROM {mm_cascaded_settings} WHERE mmtid = %d', $mmtid);
foreach ($cascaded_settings as $name => $desc)
if (isset($settings[$name]))
if ($desc['multiple']) {
foreach ($settings[$name] as $array_key => $data)
_mm_content_insert_cascaded_setting($mmtid, $name, $desc, $array_key, $data);
}
else
_mm_content_insert_cascaded_setting($mmtid, $name, $desc, '', $settings[$name]);
}
/**
* Scan a tree entry and its parents upward, looking for the closest change in a
* cascaded setting
*
* @param $name
* Setting to look for
* @param $mmtid
* Tree ID of the entry (and its parents) to query
* @param $at
* Tree ID where the closest change occurs
* @param $parent
* Tree ID of the nearest parent after $at containing a change in state
* @param $new_entry
* Set to TRUE if $mmtid is that of the (future) parent of a new child
* @return
* An array or single value (depending on the data type) containing the state
* of the given settings at the level $at
*/
function mm_content_resolve_cascaded_setting($name, $mmtid, &$at, &$parent, $new_entry = FALSE) {
$q = mm_query("SELECT s.* FROM (SELECT %d AS mmtid, %d AS depth UNION SELECT parent, depth FROM {mm_tree_parents} WHERE mmtid = %d ORDER BY depth DESC) t INNER JOIN {mm_cascaded_settings} s ON s.mmtid = t.mmtid WHERE s.name = '%s'", $mmtid, 10000, $mmtid, $name);
$out = array();
$r = $q->next();
while ($r) {
$this_mmtid = $r->mmtid;
if (is_array($out) && !$out) {
if ($r->multiple) {
do {
if ($r->data_type == 'int') $r->data = (int)$r->data;
if ($r->array_key != '') $out[$r->array_key] = $r->data;
else $out[] = $r->data;
$r = $q->next();
} while ($r && $r->multiple && $r->mmtid == $this_mmtid);
}
else {
if ($r->data_type == 'int') $r->data = (int)$r->data;
$out = $r->data;
}
$at = $this_mmtid;
}
else if ($r->multiple) {
do {
$r = $q->next();
} while ($r && $r->multiple && $r->mmtid == $this_mmtid);
}
else {
$r = $q->next();
}
if ($new_entry || $this_mmtid != $mmtid) {
$parent = $this_mmtid;
return $out;
}
}
$parent = 0;
if (!$out) {
$cascaded_settings = mm_content_get_cascaded_settings();
if (!$cascaded_settings[$name]['multiple']) return NULL;
}
return $out;
}
/**
* Get the parent tree ID of an entry
*
* @param $mmtid
* Tree ID of the entry whose parent we are looking for
* @return
* Tree ID of the parent
*/
function mm_content_get_parent($mmtid) {
$t = mm_content_get($mmtid);
if ($t) return $t->parent;
return NULL;
}
/**
* Get all parent tree IDs of a entry
*
* @param $mmtid
* Tree ID of the entry whose parent we are looking for
* @param $slow
* If TRUE, don't rely on the 'parents' field of the mm_tree table, instead
* slowly traverse up the tree
* @param $virtual
* If TRUE, include the negative IDs that are added to children of the
* /.Users entry by mm_content_get_tree().
* @return
* Array of parent tree IDs, listed highest-first
*/
function mm_content_get_parents($mmtid, $slow = FALSE, $virtual = TRUE) {
global $_mmgp_cache, $_mmtbt_cache;
$list = array();
$mmtid0 = $mmtid;
if ($mmtid < 0) {
return array(1, mm_content_users_mmtid());
}
if (!$slow) {
if ($mmtid == 1) return $list;
while ($mmtid > 1 && isset($_mmgp_cache[$mmtid])) {