-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenus.plugin.php
883 lines (796 loc) · 28.7 KB
/
menus.plugin.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
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
<?php
namespace Habari;
/**
* Menus
*
* @property Vocabulary $vocabulary The Vocabulary object used to hold the menu
*
* @todo allow renaming/editing of menu items
* @todo style everything so it looks good
* @todo show description with name on post publish checkboxes
* @todo PHPDoc
* @todo ACL, CSRF, etc.
*/
class Menus extends Plugin
{
/**
* Return properties of this object
* @param $name
* @return Vocabulary
*/
public function __get($name)
{
switch ( $name ) {
case 'vocabulary':
if ( !isset($this->_vocabulary) ) {
$this->_vocabulary = Vocabulary::get(self::$vocabulary);
}
return $this->_vocabulary;
}
}
/**
* Create an admin token for editing menus
*/
public function action_plugin_activation($file)
{
// create default access token
ACL::create_token( 'manage_menus', _t( 'Manage menus' ), 'Administration', false );
$group = UserGroup::get_by_name( 'admin' );
$group->grant( 'manage_menus' );
// register menu types
Vocabulary::add_object_type( 'menu_link' );
Vocabulary::add_object_type( 'menu_spacer' );
}
/**
* function filter_token_description_display
* Plugin filter to localize token descriptions
* @param string Token to get the description of
* @return string The localized token description
*/
public function filter_token_description_display( $token )
{
$desc = array(
'manage_menus' => _t( 'Manage menus' ),
);
return isset( $desc[$token] ) ? $desc[$token] : $token;
}
/**
* Register the templates - one for the admin page, the other for the block.
*/
public function action_init()
{
$this->add_template( 'menus_admin', dirname( __FILE__ ) . '/menus_admin.php' );
$this->add_template( 'menu_iframe', dirname( __FILE__ ) . '/menu_iframe.php' );
$this->add_template( 'block.menu', dirname( __FILE__ ) . '/block.menu.php' );
// formcontrol for tokens
$this->add_template( 'text_tokens', dirname( __FILE__ ) . '/formcontrol_tokens.php' );
$this->add_template( 'transparent_text', dirname( __FILE__ ) . '/admincontrol_text_transparent.php' );
}
/**
* Remove the admin token
*/
public function action_plugin_deactivation( $file )
{
// delete default access token
ACL::destroy_token( 'manage_menus' );
// delete menu vocabularies that were created
$vocabs = DB::get_results( 'SELECT * FROM {vocabularies} WHERE name LIKE "menu_%"', array(), 'Vocabulary' );
foreach( $vocabs as $vocab ) {
// This should only delete the ones that are menu vocabularies, unless others have been named 'menu_xxxxx'
$vocab->delete();
}
// delete blocks that were created
$blocks = DB::get_results( 'SELECT * FROM {blocks} WHERE type = "menu"', array(), 'Block') ;
foreach( $blocks as $block ) {
$block->delete();
}
}
/**
* Add to the list of possible block types.
*/
public function filter_block_list($block_list)
{
$block_list['menu'] = _t( 'Menu' );
return $block_list;
}
/**
* Produce the form to configure a menu
*/
public function action_block_form_menu( $form, $block )
{
$form->append(FormControlLabel::wrap(_t( 'Menu Taxonomy' ), FormControlSelect::create('menu_taxonomy', $block)->set_options($this->get_menus( true ))));
$form->append(FormControlLabel::wrap(_t( 'Wrap each menu link in a div' ), FormControlCheckbox::create('div_wrap', $block)));
$form->append(FormControlLabel::wrap(_t( 'Custom class for the tree\'s ordered list element' ), FormControlText::create('list_class', $block)));
}
/**
* Populate the block with some content
*/
public function action_block_content_menu( $block, $theme ) {
$vocab = Vocabulary::get_by_id($block->menu_taxonomy);
$block->vocabulary = $vocab;
if ($block->div_wrap) {
$block->wrapper = '<div>%s</div>';
}
else {
$block->wrapper = '%s';
}
// preprocess some things
$block->tree = $vocab->get_tree();
$block->render_menu_item = array($this, 'render_menu_item');
}
/**
* Add menus to the publish form
*/
public function action_form_publish ( $form, $post )
{
$menus = $this->get_menus();
$menulist = array();
foreach($menus as $menu) {
$menulist[$menu->id] = $menu->name;
}
$settings = $form->publish_controls->append( FormControlFieldset::create('menu_set')->set_caption(_t( 'Menus' ) ) );
$settings->append( FormControlCheckboxes::create('menus')->set_options($menulist) );
// If this is an existing post, see if it has categories already
if ( 0 != $post->id ) {
// Get the terms associated to this post
$object_terms = Vocabulary::get_all_object_terms( 'post', $post->id );
$menu_ids = array_keys( $menulist );
$value = array();
// if the term is in a menu vocab, enable that checkbox
foreach( $object_terms as $term ) {
if ( in_array( $term->vocabulary_id, $menu_ids ) ) {
$value[] = $term->vocabulary_id;
}
}
$form->menus->value = $value;
}
}
/**
* Process menus when the publish form is received
*
*/
public function action_publish_post( $post, $form )
{
// might not hurt to turn this into a function to be more DRY
$term_title = $post->title;
$selected_menus = $form->menus->value;
/* foreach( $this->get_menus() as $menu ) {
$terms = $menu->get_object_terms( 'post', $post->id );
if ( in_array( $menu->id, $selected_menus ) ) {
if ( count( $terms ) == 0 ) {
$term = new Term(array(
'term_display' => $post->title,
'term' => $post->slug,
));
$term->info->menu = $menu->id;
$menu->add_term( $term );
$menu->set_object_terms( 'post',
$post->id,
array( $term->term ) );
}
}
else {
foreach( $terms as $term ) {
$term->delete();
}
}
}*/
}
/**
* Add creation and management links to the main menu
*
*/
public function filter_adminhandler_post_loadplugins_main_menu( $menu ) {
$menus_array = array( 'create_menu' => array(
'title' => "Create a new Menu",
'text' => "New Menu",
'hotkey' => '1',
'url' => URL::get( 'admin', array( 'page' => 'menus', 'action' => 'create' ) ),
'class' => 'over-spacer',
'access' => array( 'manage_menus' => true ),
));
$items = 0;
foreach ( $this->get_menus() as $item ) {
$menus_array[ ++$items ] = array(
'title' => "{$item->name}: {$item->description}",
'text' => $item->name,
'hotkey' => $items+1,
'url' => URL::get( 'admin', array( 'page' => 'menus', 'action' => 'edit', 'menu' => $item->id ) ),
'access' => array( 'manage_menus' => true ),
);
}
if ( count( $menus_array ) > 1 ) {
$menus_array[1]['class'] = 'under-spacer';
}
// add to main menu
$item_menu = array( 'menus' =>
array(
'url' => URL::get( 'admin', 'page=menus' ),
'title' => _t( 'Menus' ),
'text' => _t( 'Menus' ),
'hotkey' => 'E',
'selected' => false,
'submenu' => $menus_array,
)
);
$slice_point = array_search( 'themes', array_keys( $menu ) ); // Element will be inserted before "themes"
$pre_slice = array_slice( $menu, 0, $slice_point);
$post_slice = array_slice( $menu, $slice_point);
$menu = array_merge( $pre_slice, $item_menu, $post_slice );
return $menu;
}
/**
* Handle GET and POST requests
*
*/
public function alias()
{
return array(
'action_admin_theme_get_menus' => 'action_admin_theme_post_menus',
'action_admin_theme_get_menu_iframe' => 'action_admin_theme_post_menu_iframe',
);
}
/**
* Restrict access to the admin page
*
*/
public function filter_admin_access_tokens( array $require_any, $page )
{
switch ( $page ) {
case 'menu_iframe':
case 'menus':
$require_any = array( 'manage_menus' => true );
break;
}
return $require_any;
}
/**
* Convenience function for obtaining menu type data and caching it so that it's not called repeatedly
*
* The return value should include specific paramters, which are used to feed the menu creation routines.
* The array should return a structure like the following:
* <code>
* $menus = array(
* 'typename' => array(
* 'form' => function(FormUI $form, Term|null $term){ },
*
* )
* );
* </code>
* @return array
*/
public function get_menu_type_data()
{
static $menu_type_data = null;
if ( empty($menu_type_data) ) {
$menu_type_data = Plugins::filter('menu_type_data', array());
}
return $menu_type_data;
}
/**
* Implementation of menu_type_data filter, created by this plugin
* @param array $menu_type_data Existing menu type data
* @return array Updated menu type data
*/
public function filter_menu_type_data($menu_type_data)
{
$menu_type_data['menu_link'] = array(
'label' => _t( 'Link' ),
'form' => function($form, $term) {
$link_name = FormControlText::create( 'link_name' )
->add_validator( 'validate_required', _t( 'A name is required.' ) );
$link_url = FormControlText::create( 'link_url' )
->add_validator( 'validate_required' )
->add_validator( 'validate_url', _t( 'You must supply a valid URL.' ) );
if ( $term ) {
$link_name->value = $term->term_display;
$link_url->value = $term->info->url;
$form->append( FormControlHidden::create('term') )->set_value($term->id);
}
$form->append( $link_name->label( _t( 'Link Title' ) ) );
$form->append( $link_url->label( _t( 'Link URL' ) ) );
},
'save' => function($menu, $form) {
if ( ! isset( $form->term->value ) ) {
$term = new Term(array(
'term_display' => $form->link_name->value,
'term' => Utils::slugify($form->link_name->value),
));
$term->info->type = "link";
$term->info->url = $form->link_url->value;
$term->info->menu = $menu->id;
$menu->add_term($term);
$term->associate('menu_link', 0);
Session::notice( _t( 'Link added.' ) );
} else {
$term = Term::get( intval( $form->term->value ) );
$updated = false;
if ( $term->info->url !== $form->link_url->value ) {
$term->info->url = $form->link_url->value;
$updated = true;
}
if ( $form->link_name->value !== $term->term_display ) {
$term->term_display = $form->link_name->value;
$term->term = Utils::slugify( $form->link_name->value );
$updated = true;
}
$term->info->url = $form->link_url->value;
if ( $updated ) {
$term->update();
Session::notice( _t( 'Link updated.' ) );
}
}
},
'render' => function($term, $object_id, $config) {
$result = array(
'link' => $term->info->url,
);
return $result;
}
);
$menu_type_data['menu_spacer'] = array(
'label' => _t( 'Spacer' ),
'form' => function(FormUI $form, $term) {
$spacer = FormControlText::create( 'spacer_text' )->set_helptext( _t( 'Leave blank for blank space' ) )->label( _t( 'Item text' ) );
if ( $term ) {
$spacer->set_value( $term->term_display );
$form->append( FormControlHidden::create('term')->set_value( $term->id ) );
}
$form->append( $spacer );
},
'save' => function($menu, $form) {
if ( ! isset( $form->term->value ) ) {
$term = new Term(array(
'term_display' => ($form->spacer_text->value !== '' ? $form->spacer_text->value : ' '), // totally blank values collapse the term display in the formcontrol
'term' => 'menu_spacer',
));
$term->info->type = "spacer";
$term->info->menu = $menu->id;
$menu->add_term($term);
$term->associate('menu_spacer', 0);
Session::notice( _t( 'Spacer added.' ) );
} else {
$term = Term::get( intval( $form->term->value ) );
if ($form->spacer_text->value !== $term->term_display ) {
$term->term_display = $form->spacer_text->value;
$term->update();
Session::notice( _t( 'Spacer updated.' ) );
}
}
}
);
$menu_type_data['post'] = array(
'label' => _t( 'Links to Posts' ),
'form' => function( FormUI $form, $term ) {
if ( $term ) {
$object_types = $term->object_types();
$term_object = reset( $object_types );
$form->append( FormControlText::create('term_display')->set_value($term->term_display)->label(_t( 'Title to display' ) ) );
$post = Post::get( $term_object->object_id );
$form->append( FormControlStatic::create('post_link')->set_static( _t( "Links to <a target='_blank' href='{$post->permalink}'>{$post->title}</a>" ) ) );
$form->append( FormControlHidden::create('term')->set_value($term->id) );
}
else {
$post_ids = $form->append(
FormControlAutocomplete::create('post_ids', null, array('style' => 'width:90%;'), array())
->set_ajax(URL::auth_ajax('post_list' ))
->label( _t( 'Posts' ) )->set_template('control.label.outsideleft')
);
// $post_ids->template = 'text_tokens';
// $post_ids->ready_function = "$('#{$post_ids->field}').tokenInput( habari.url.ajaxPostTokens )";
}
},
'save' => function($menu, $form) {
if ( ! isset( $form->term->value ) ) {
$post_ids = explode( ',', $form->post_ids->value );
foreach( $post_ids as $post_id ) {
$post = Post::get( array( 'id' => $post_id ) );
$term_title = $post->title;
$terms = $menu->get_object_terms( 'post', $post->id );
if ( count( $terms ) == 0 ) {
$term = new Term( array( 'term_display' => $post->title, 'term' => $post->slug ) );
$term->info->menu = $menu->id;
$menu->add_term( $term );
$menu->set_object_terms( 'post', $post->id, array( $term->term ) );
}
}
Session::notice(_t( 'Link(s) added.' ));
}
else {
$term = Term::get( intval( $form->term->value ) );
if ($form->term_display->value !== $term->term_display ) {
$term->term_display = $form->term_display->value;
$term->update();
Session::notice( _t( 'Link updated.' ) );
}
}
},
'render' => function($term, $object_id, $config) {
$result = array();
if ($post = Post::get($object_id)) {
$rule = Controller::get_matched_rule();
if(isset($rule->named_arg_values['slug']) && $rule->named_arg_values['slug'] == $post->slug) {
$result['active'] = true;
}
$result['link'] = $post->permalink;
}
return $result;
}
);
return $menu_type_data;
}
/**
* @return array The data array
*/
public function get_menu_type_ids()
{
static $menu_item_ids = null;
if ( empty($menu_item_ids) ) {
$menu_item_types = $this->get_menu_type_data();
$menu_item_types = Utils::array_map_field($menu_item_types, 'type_id');
$menu_item_types = array_flip($menu_item_ids);
}
return $menu_item_ids;
}
/**
* Minimal modal forms
*
*/
public function action_admin_theme_get_menu_iframe( AdminHandler $handler, Theme $theme )
{
$action = isset($_GET[ 'action' ]) ? $_GET[ 'action' ] : 'create';
$term = null;
if ( isset( $_GET[ 'term' ] ) ) {
$term = Term::get( intval( $_GET[ 'term' ] ) );
$object_types = $term->object_types();
$action = $object_types[0]->type; // the 'menu_whatever' we seek should be the only element in the array.
$form_action = URL::get( 'admin', array( 'page' => 'menu_iframe', 'menu' => $_GET[ 'menu' ], 'term' => $_GET[ 'term' ], 'action' => "$action" ) );
} else {
$form_action = URL::get( 'admin', array( 'page' => 'menu_iframe', 'menu' => $_GET[ 'menu' ], 'action' => "$action" ) );
}
$form = new FormUI(
'menu_item_edit',
$action
);
$form->set_properties(array(
'class' => 'tm_db_action',
'action' => $form_action,
'onsubmit' => "return habari.menu_admin.submit_menu_item_edit(this)",
));
$form->on_success( array( $this, 'term_form_save' ) );
$form->append( FormControlHidden::create('menu') )->set_value($_GET[ 'menu' ]);
$menu_types = $this->get_menu_type_data();
if ( isset($menu_types[$action]) ) {
$menu_types[$action]['form']($form, $term);
$form->append( FormControlHidden::create('menu_type' )->set_value($action));
if($term) {
$label = _t( 'Update %s', array( $menu_types[$action]['label'] ));
}
else {
$label = _t( 'Add %s', array( $menu_types[$action]['label'] ));
}
$form->append( FormControlSubmit::create('submit')->set_caption($label) );
}
$theme->page_content = $form->get();
if ( isset($form->has_result) ) {
switch ( $form->has_result ) {
case 'added':
$treeurl = URL::get( 'admin', array('page' => 'menus', 'menu' => $_GET[ 'menu' ], 'action' => 'edit') ) . ' #edit_menu>*';
$msg = _t( 'Menu item added.' ); // @todo: update this to reflect if more than one item has been added, or reword entirely.
$theme->page_content .= <<< JAVSCRIPT_RESPONSE
<script type="text/javascript">
human_msg.display_msg('{$msg}');
$('#edit_menu').load('{$treeurl}', habari.menu_admin.init_form);
</script>
JAVSCRIPT_RESPONSE;
break;
case 'updated':
$treeurl = URL::get( 'admin', array('page' => 'menus', 'menu' => $_GET[ 'menu' ], 'action' => 'edit') ) . ' #edit_menu>*';
$msg = _t( 'Menu item updated.' ); // @todo: update this to reflect if more than one item has been added, or reword entirely.
$theme->page_content .= <<< JAVSCRIPT_RESPONSE
<script type="text/javascript">
human_msg.display_msg('{$msg}');
$('#menu_popup').dialog('close');
$('#edit_menu').load('{$treeurl}', habari.menu_admin.init_form);
</script>
JAVSCRIPT_RESPONSE;
break;
}
}
$theme->display( 'menu_iframe' );
exit;
}
/**
* Prepare and display admin page
*
*/
public function action_admin_theme_get_menus( AdminHandler $handler, Theme $theme )
{
$theme->page_content = '';
$action = isset($_GET[ 'action' ]) ? $_GET[ 'action' ] : 'create';
switch ( $action ) {
case 'edit':
$vocabulary = Vocabulary::get_by_id( intval( $_GET[ 'menu' ] ) );
if ( $vocabulary == false ) {
$theme->page_content = '<h2>' . _t( 'Invalid Menu.' );
// that's it, we're done. Maybe we show the list of menus instead?
break;
}
$form = new FormUI( 'edit_menu' );
$form->append( FormControlText::create( 'menuname' )
->add_validator( 'validate_required', _t( 'You must supply a valid menu name' ) )
->add_validator( array( $this, 'validate_newvocab' ) )
->set_value( $vocabulary->name)
->label( _t( 'Name' ) ) );
$form->append( FormControlHidden::create( 'oldname' )->set_value($vocabulary->name) );
$form->append( FormControlText::create( 'description' )
->set_value($vocabulary->description)
->label(_t( 'Description' )));
$edit_items_array = $this->get_menu_type_data();
$edit_items = '';
foreach( $edit_items_array as $action => $menu_type ) {
$edit_items .= '<a class="modal_popup_form menu_button_dark" href="' . URL::get('admin', array(
'page' => 'menu_iframe',
'action' => $action,
'menu' => $vocabulary->id,
) ) . "\">" . _t( 'Add %s', array($menu_type['label'] ) ) . "</a>";
}
if ( $vocabulary->is_empty() ) {
$form->append( FormControlStatic::create('buttons')->set_static('<div id="menu_item_button_container">' . $edit_items . '</div>') );
}
else {
$form->append( FormControlTree::create('tree', $vocabulary->get_tree(), array(), array( 'itemcallback' => array( $this, 'tree_item_callback' ) )));
// $form->tree->value = $vocabulary->get_root_terms();
// append other needed controls, if there are any.
$form->append( FormControlStatic::create('buttons')->set_static('<div id="menu_item_button_container">' . $edit_items . '</div>') );
$form->append( FormControlSubmit::create('save')->set_caption( _t( 'Apply Changes' ) ) );
}
$delete_link = URL::get( 'admin', Utils::WSSE( array( 'page' => 'menus', 'action' => 'delete_menu', 'menu' => $_GET[ 'menu' ] ) ) );
//$delete_link = URL::get( 'admin', array( 'page' => 'menus', 'action' => 'delete_menu', 'menu' => $_GET[ 'menu' ] ) );
$form->append( FormControlStatic::create('deletebutton')->set_static('<a class="a_button" href="' . $delete_link . '">' . _t( 'Delete Menu' ) . '</a>') );
$form->append( FormControlHidden::create( 'menu' ) )->set_value($_GET[ 'menu' ]);
$form->on_success( array( $this, 'rename_menu_form_save' ) );
$form->set_properties(array('onsubmit' => "return habari.menu_admin.submit_menu_update();"));
$theme->page_content .= $form->get();
break;
case 'create':
$form = new FormUI('create_menu');
$form->append( FormControlText::create('menuname')
->add_validator( 'validate_required', _t( 'You must supply a valid menu name' ) )
->add_validator( array($this, 'validate_newvocab' ) )
->label(_t( 'Menu Name' ) ) );
$form->append( FormControlText::create('description')->label( _t( 'Description' ) ) );
$form->append( FormControlSubmit::create('submit')->set_caption( _t( 'Create Menu' ) ) );
$form->on_success( array( $this, 'add_menu_form_save' ) );
$theme->page_content = $form->get();
break;
case 'delete_menu':
if(Utils::verify_wsse($_GET, true)) {
$menu_vocab = Vocabulary::get_by_id( intval( $_GET[ 'menu' ] ) );
$menu_vocab->delete();
// log that it has been deleted?
Session::notice( _t( 'Menu deleted.' ) );
// redirect to a blank menu creation form
Utils::redirect( URL::get( 'admin', array( 'page' => 'menus', 'action' => 'create' ) ) );
}
else {
Session::notice( _t( 'Menu deletion failed - please try again.' ) );
Utils::redirect(URL::get('admin', array('page' => 'menus', 'action' => 'edit', 'menu' => $_GET[ 'menu' ])));
}
break;
case 'delete_term':
$term = Term::get( intval( $_GET[ 'term' ] ) );
$menu_vocab = $term->vocabulary_id;
if(Utils::verify_wsse($_GET, true)) {
$term->delete();
// log that it has been deleted?
Session::notice( _t( 'Item deleted.' ) );
Utils::redirect( URL::get( 'admin', array( 'page' => 'menus', 'action' => 'edit', 'menu' => $menu_vocab ) ) );
}
else {
Session::notice( _t( 'Item deletion failed - please try again.' ) );
Utils::redirect(URL::get('admin', array('page' => 'menus', 'action' => 'edit', 'menu' => $menu_vocab)));
}
break;
default:
Utils::debug( $_GET, $action ); die();
}
$theme->display( 'menus_admin' );
// End everything
exit;
}
public function add_menu_form_save( $form )
{
$params = array(
'name' => $form->menuname->value,
'description' => $form->description->value,
'features' => array(
'term_menu', // a special feature that marks the vocabulary as a menu, but has no functional purpose
'unique', // a special feature that applies a one-to-one relationship between term and object, enforced by the Vocabulary class
),
);
$vocab = Vocabulary::create( $params );
Utils::redirect( URL::get( 'admin', array( 'page' => 'menus', 'action' => 'edit', 'menu' => $vocab->id ) ) );
}
public function rename_menu_form_save( $form )
{
// The name of this should probably change, since it is the on_success for the whole menu edit, no longer just for renaming.
// It only renames/modifies the description currently, as item adding/rearranging is done by the NestedSortable tree.
// get the menu from the form, grab the values, modify the vocabulary.
$menu_vocab = intval( $form->menu->value );
// create a term for the link, store the URL
$menu = Vocabulary::get_by_id( $menu_vocab );
if ( $menu->name != $form->menuname->value ) {
$menu->name = $form->menuname->value; // could use Vocabulary::rename for this
}
$menu->description = $form->description->value; // no Vocabulary function for this
$menu->update();
$form->save();
Session::notice( _t( 'Updated menu "%s".', array( $form->menuname->value ) ) );
Utils::redirect( URL::get( 'admin', array(
'page' => 'menus',
'action' => 'edit',
'menu' => $menu->id,
) ) );
}
public function term_form_save( $form )
{
$menu_vocab = intval( $form->menu->value );
$menu = Vocabulary::get_by_id( $menu_vocab );
$menu_type_data = $this->get_menu_type_data();
if ( isset( $form->term ) ) {
$term = Term::get( intval( (string) $form->term->value ) );
// maybe we should check if term exists? Or put that in the conditional above?
$object_types = $term->object_types();
$type = $object_types[0]->type; // that's twice we've grabbed the $term->object_types()[0]. Maybe this is a job for a function?
if ( isset($menu_type_data[$type]['save']) ) {
$menu_type_data[$type]['save']($menu, $form);
}
$form->has_result = 'updated';
}
else { // if no term is set, create a new item.
// create a term for the link, store the URL
$type = $form->menu_type->value;
if ( isset($menu_type_data[$type]['save']) ) {
$menu_type_data[$type]['save']($menu, $form);
}
$form->has_result = 'added';
}
}
public function validate_newvocab( $value, $control, $form )
{
if ( isset( $form->oldname ) && ( $form->oldname->value ) && ( $value == $form->oldname->value ) ) {
return array();
}
if ( Vocabulary::get( $value ) instanceof Vocabulary ) {
return array( _t( 'Please choose a vocabulary name that does not already exist.' ) );
}
return array();
}
public function get_menus($as_array = false)
{
$vocabularies = Vocabulary::get_all();
$outarray = array();
foreach ( $vocabularies as $index => $menu ) {
if ( !$menu->term_menu ) { // check for the term_menu feature we added.
unset( $vocabularies[ $index ] );
}
else {
if ( $as_array ) {
$outarray[ $menu->id ] = $menu->name;
}
}
}
if ( $as_array ) {
return $outarray;
}
else {
return $vocabularies;
}
}
/**
*
* Callback for Format::term_tree to use with $config['linkcallback']
*
* @param Term $term
* @param array $config
* @return array $config modified with the new wrapper div
*/
public function tree_item_callback( Term $term, $config )
{
// coming into this, default $config['wrapper'] is "<div>%s</div>"
// make the links
$edit_link = URL::get( 'admin', array(
'page' => 'menu_iframe',
'action' => $term->info->type,
'term' => $term->id,
'menu' => $term->info->menu,
) );
$delete_link = URL::get( 'admin', Utils::WSSE( array(
'page' => 'menus',
'action' => 'delete_term',
'term' => $term->id,
'menu' => $term->info->menu,
) ) );
$delete_link = str_replace('%', '%%', $delete_link); // This is so it doesn't break the sprintf in Format::term_tree()
// insert them into the wrapper
$edit_title = _t('Edit this');
$edit_label = _t('Edit');
$delete_title = _t('Delete this');
$delete_label = _t('Delete');
/** @var FormControlDropbutton $dbtn */
$dbtn = FormControlDropbutton::create('menu_item');
$dbtn->append(FormControlSubmit::create('edit')->set_caption($edit_label)->set_url($edit_link)->set_property('title', $edit_title));
$dbtn->append(FormControlSubmit::create('delete')->set_caption($delete_label)->set_url($delete_link)->set_property('title', $delete_title));
$links = $dbtn->pre_out() . $dbtn->get($dbtn->get_theme());
// Put the dropbutton links for each item at the end of the item's div
$config[ 'wrapper' ] = "<div><i class=\"icon-list handle\"></i>%s {$links}</div>";
return $config;
}
/**
* Callback function for block output of menu list item
*/
public function render_menu_item( Term $term, $config )
{
$title = $term->term_display;
$active = false;
$menu_type_data = $this->get_menu_type_data();
$spacer = false;
$active = false;
$link = null;
if ( !isset($term->object_id) ) {
$objects = $term->object_types();
$term->type = reset($objects);
$term->object_id = key($objects);
}
if ( isset($menu_type_data[$term->type]['render']) ) {
$result = $menu_type_data[$term->type]['render']($term, $term->object_id, $config);
$result = array_intersect_key(
$result,
array(
'link' => 1,
'title' => 1,
'active' => 1,
'spacer' => 1,
'config' => 1,
)
);
extract($result);
}
if ( empty( $link ) ) {
$config[ 'wrapper' ] = sprintf($config[ 'linkwrapper' ], $title);
}
else {
$config[ 'wrapper' ] = str_replace('%', '%%', sprintf( $config[ 'linkwrapper' ], "<a href=\"{$link}\">{$title}</a>" ));
}
if ( $active ) {
$config[ 'itemattr' ][ 'class' ] = 'active';
}
else {
$config[ 'itemattr' ][ 'class' ] = 'inactive';
}
if ( $spacer ) {
$config[ 'itemattr' ][ 'class' ] .= ' spacer';
}
return $config;
}
/**
* Add required Javascript and, for now, CSS.
*/
public function action_admin_header( $theme )
{
if ( $theme->page == 'menus' ) {
// Ideally the plugin would reuse reusable portions of the existing admin CSS. Until then, let's only add the CSS needed on the menus page.
Stack::add( 'admin_stylesheet', array( $this->get_url( '/admin.css' ), 'screen' ), 'menus-admin-css', 'admin-css' );
// Load the plugin and its css
Stack::add( 'admin_header_javascript', Site::get_url( 'vendor' ) . "/jquery.tokeninput.js", 'jquery-tokeninput', 'jquery.ui' );
Stack::add( 'admin_stylesheet', array( Site::get_url( 'admin_theme' ) . '/css/token-input.css', 'screen' ), 'admin_tokeninput', 'jquery.ui-css' );
// Add the callback URL.
$url = "habari.url.ajaxPostTokens = '" . URL::get( 'auth_ajax', array( 'context' => 'post_tokens' ) ) . "';";
Stack::add( 'admin_header_javascript', $url, 'post_tokens_url', 'post_tokens' );
// Add the menu administration javascript
Stack::add( 'admin_header_javascript', $this->get_url('/menus_admin.js'), 'menus_admin', 'admin-js');
}
}
}
?>