/
views.php
1758 lines (1537 loc) · 51.4 KB
/
views.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
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
/**
* Elgg's view system.
*
* The view system is the primary templating engine in Elgg and renders
* all output. Views are short, parameterised PHP scripts for displaying
* output that can be regsitered, overridden, or extended. The view type
* determines the output format and location of the files that renders the view.
*
* Elgg uses a two step process to render full output: first
* content-specific elements are rendered, then the resulting
* content is inserted into a layout and displayed. This makes it
* easy to maintain a consistent look on all pages.
*
* A view corresponds to a single file on the filesystem and the views
* name is its directory structure. A file in
* <code>mod/plugins/views/default/myplugin/example.php</code>
* is called by saying (with the default viewtype):
* <code>echo elgg_view('myplugin/example');</code>
*
* View names that are registered later override those that are
* registered earlier. For plugins this corresponds directly
* to their load order: views in plugins lower in the list override
* those higher in the list.
*
* Plugin views belong in the views/ directory under an appropriate
* viewtype. Views are automatically registered.
*
* Views can be embedded-you can call a view from within a view.
* Views can also be prepended or extended by any other view.
*
* Any view can extend any other view if registered with
* {@link elgg_extend_view()}.
*
* Viewtypes are set by passing $_REQUEST['view']. The viewtype
* 'default' is a standard HTML view. Types can be defined on the fly
* and you can get the current viewtype with {@link elgg_get_viewtype()}.
*
* @note Internal: Plugin views are autoregistered before their init functions
* are called, so the init order doesn't affect views.
*
* @note Internal: The file that determines the output of the view is the last
* registered by {@link elgg_set_view_location()}.
*
* @package Elgg.Core
* @subpackage Views
*/
/**
* The viewtype override.
*
* @global string $CURRENT_SYSTEM_VIEWTYPE
* @see elgg_set_viewtype()
*/
global $CURRENT_SYSTEM_VIEWTYPE;
$CURRENT_SYSTEM_VIEWTYPE = "";
/**
* Manually set the viewtype.
*
* View types are detected automatically. This function allows
* you to force subsequent views to use a different viewtype.
*
* @tip Call elgg_set_viewtype() with no parameter to reset.
*
* @param string $viewtype The view type, e.g. 'rss', or 'default'.
*
* @return bool
*/
function elgg_set_viewtype($viewtype = "") {
global $CURRENT_SYSTEM_VIEWTYPE;
$CURRENT_SYSTEM_VIEWTYPE = $viewtype;
return true;
}
/**
* Return the current view type.
*
* Viewtypes are automatically detected and can be set with $_REQUEST['view']
* or {@link elgg_set_viewtype()}.
*
* @note Internal: Viewtype is determined in this order:
* - $CURRENT_SYSTEM_VIEWTYPE Any overrides by {@link elgg_set_viewtype()}
* - $CONFIG->view The default view as saved in the DB.
*
* @return string The view.
* @see elgg_set_viewtype()
*/
function elgg_get_viewtype() {
global $CURRENT_SYSTEM_VIEWTYPE;
if (empty($CURRENT_SYSTEM_VIEWTYPE)) {
$CURRENT_SYSTEM_VIEWTYPE = _elgg_get_initial_viewtype();
}
return $CURRENT_SYSTEM_VIEWTYPE;
}
/**
* Get the initial viewtype
*
* @return string
* @access private
* @since 2.0.0
*/
function _elgg_get_initial_viewtype() {
global $CONFIG;
$viewtype = get_input('view', '', false);
if (_elgg_is_valid_viewtype($viewtype)) {
return $viewtype;
}
if (isset($CONFIG->view) && _elgg_is_valid_viewtype($CONFIG->view)) {
return $CONFIG->view;
}
return 'default';
}
/**
* Register a viewtype.
*
* @param string $viewtype The view type to register
* @return bool
*/
function elgg_register_viewtype($viewtype) {
if (!isset($GLOBALS['_ELGG']->view_types) || !is_array($GLOBALS['_ELGG']->view_types)) {
$GLOBALS['_ELGG']->view_types = array();
}
if (!in_array($viewtype, $GLOBALS['_ELGG']->view_types)) {
$GLOBALS['_ELGG']->view_types[] = $viewtype;
}
return true;
}
/**
* Checks if $viewtype is registered.
*
* @param string $viewtype The viewtype name
*
* @return bool
* @since 1.9.0
*/
function elgg_is_registered_viewtype($viewtype) {
if (!isset($GLOBALS['_ELGG']->view_types) || !is_array($GLOBALS['_ELGG']->view_types)) {
return false;
}
return in_array($viewtype, $GLOBALS['_ELGG']->view_types);
}
/**
* Checks if $viewtype is a string suitable for use as a viewtype name
*
* @param string $viewtype Potential viewtype name. Alphanumeric chars plus _ allowed.
*
* @return bool
* @access private
* @since 1.9
*/
function _elgg_is_valid_viewtype($viewtype) {
if (!is_string($viewtype) || $viewtype === '') {
return false;
}
if (preg_match('/\W/', $viewtype)) {
return false;
}
return true;
}
/**
* Register a viewtype to fall back to a default view if a view isn't
* found for that viewtype.
*
* @tip This is useful for alternate html viewtypes (such as for mobile devices).
*
* @param string $viewtype The viewtype to register
*
* @return void
* @since 1.7.2
*/
function elgg_register_viewtype_fallback($viewtype) {
_elgg_services()->views->registerViewtypeFallback($viewtype);
}
/**
* Checks if a viewtype falls back to default.
*
* @param string $viewtype Viewtype
*
* @return boolean
* @since 1.7.2
*/
function elgg_does_viewtype_fallback($viewtype) {
return _elgg_services()->views->doesViewtypeFallback($viewtype);
}
/**
* Register a view to be available for ajax calls
*
* @warning Only views that begin with 'js/' and 'css/' have their content
* type set to 'text/javascript' and 'text/css'. Other views are served as
* 'text/html'.
*
* @param string $view The view name
* @return void
* @since 1.8.3
*/
function elgg_register_ajax_view($view) {
elgg_register_external_view($view, false);
}
/**
* Unregister a view for ajax calls
*
* @param string $view The view name
* @return void
* @since 1.8.3
*/
function elgg_unregister_ajax_view($view) {
elgg_unregister_external_view($view);
}
/**
* Registers a view as being available externally (i.e. via URL).
*
* @param string $view The name of the view.
* @param boolean $cacheable Whether this view can be cached.
* @return void
* @since 1.9.0
*/
function elgg_register_external_view($view, $cacheable = false) {
if (!isset($GLOBALS['_ELGG']->allowed_ajax_views)) {
$GLOBALS['_ELGG']->allowed_ajax_views = array();
}
$GLOBALS['_ELGG']->allowed_ajax_views[$view] = true;
if ($cacheable) {
_elgg_services()->views->registerCacheableView($view);
}
}
/**
* Unregister a view for ajax calls
*
* @param string $view The view name
* @return void
* @since 1.9.0
*/
function elgg_unregister_external_view($view) {
if (isset($GLOBALS['_ELGG']->allowed_ajax_views[$view])) {
unset($GLOBALS['_ELGG']->allowed_ajax_views[$view]);
}
}
/**
* Set an alternative base location for a view.
*
* Views are expected to be in plugin_name/views/. This function can
* be used to change that location.
*
* @note Internal: Core view locations are stored in $CONFIG->view_path.
*
* @tip This is useful to optionally register views in a plugin.
*
* @param string $view The name of the view
* @param string $location The full path to the view
* @param string $viewtype The view type
*
* @return void
*/
function elgg_set_view_location($view, $location, $viewtype = '') {
_elgg_services()->views->setViewDir($view, $location, $viewtype);
}
/**
* Returns whether the specified view exists
*
* @note If $recurse is true, also checks if a view exists only as an extension.
*
* @param string $view The view name
* @param string $viewtype If set, forces the viewtype
* @param bool $recurse If false, do not check extensions
*
* @return bool
*/
function elgg_view_exists($view, $viewtype = '', $recurse = true) {
return _elgg_services()->views->viewExists($view, $viewtype, $recurse);
}
/**
* List all views in a viewtype
*
* @param string $viewtype Viewtype
*
* @return string[]
*
* @since 2.0
*/
function elgg_list_views($viewtype = 'default') {
return _elgg_services()->views->listViews($viewtype);
}
/**
* Return a parsed view.
*
* Views are rendered by a template handler and returned as strings.
*
* Views are called with a special $vars variable set,
* which includes any variables passed as the second parameter.
*
* The input of views can be intercepted by registering for the
* view_vars, $view_name plugin hook.
*
* The output of views can be intercepted by registering for the
* view, $view_name plugin hook.
*
* @param string $view The name and location of the view to use
* @param array $vars Variables to pass to the view.
* @param boolean $bypass This argument is ignored and will be removed eventually
* @param boolean $ignored This argument is ignored and will be removed eventually
* @param string $viewtype If set, forces the viewtype for the elgg_view call to be
* this value (default: standard detection)
*
* @return string The parsed view
*/
function elgg_view($view, $vars = array(), $bypass = false, $ignored = false, $viewtype = '') {
return _elgg_services()->views->renderView($view, $vars, $bypass, $viewtype);
}
/**
* Display a view with a deprecation notice. No missing view NOTICE is logged
*
* @see elgg_view()
*
* @param string $view The name and location of the view to use
* @param array $vars Variables to pass to the view
* @param string $suggestion Suggestion with the deprecation message
* @param string $version Human-readable *release* version: 1.7, 1.8, ...
*
* @return string The parsed view
* @access private
*/
function elgg_view_deprecated($view, array $vars, $suggestion, $version) {
return _elgg_services()->views->renderDeprecatedView($view, $vars, $suggestion, $version);
}
/**
* Extends a view with another view.
*
* The output of any view can be prepended or appended to any other view.
*
* The default action is to append a view. If the priority is less than 500,
* the output of the extended view will be appended to the original view.
*
* Views can be extended multiple times, and extensions are not checked for
* uniqueness. Use {@link elgg_unextend_view()} to help manage duplicates.
*
* Priority can be specified and affects the order in which extensions
* are appended or prepended.
*
* @note Internal: View extensions are stored in
* $CONFIG->views->extensions[$view][$priority] = $view_extension
*
* @param string $view The view to extend.
* @param string $view_extension This view is added to $view
* @param int $priority The priority, from 0 to 1000, to add at (lowest numbers displayed first)
*
* @return void
* @since 1.7.0
*/
function elgg_extend_view($view, $view_extension, $priority = 501) {
_elgg_services()->views->extendView($view, $view_extension, $priority);
}
/**
* Unextends a view.
*
* @param string $view The view that was extended.
* @param string $view_extension This view that was added to $view
*
* @return bool
* @since 1.7.2
*/
function elgg_unextend_view($view, $view_extension) {
return _elgg_services()->views->unextendView($view, $view_extension);
}
/**
* Assembles and outputs a full page.
*
* A "page" in Elgg is determined by the current view type and
* can be HTML for a browser, RSS for a feed reader, or
* Javascript, PHP and a number of other formats.
*
* For HTML pages, use the 'head', 'page' plugin hook for setting meta elements
* and links.
*
* @param string $title Title
* @param string $body Body
* @param string $page_shell Optional page shell to use. See page/shells view directory
* @param array $vars Optional vars array to pass to the page
* shell. Automatically adds title, body, head, and sysmessages
*
* @return string The contents of the page
* @since 1.8
*/
function elgg_view_page($title, $body, $page_shell = 'default', $vars = array()) {
$timer = _elgg_services()->timer;
if (!$timer->hasEnded(['build page'])) {
$timer->end(['build page']);
}
$timer->begin([__FUNCTION__]);
$params = array();
$params['identifier'] = _elgg_services()->request->getFirstUrlSegment();
$params['segments'] = _elgg_services()->request->getUrlSegments();
array_shift($params['segments']);
$page_shell = elgg_trigger_plugin_hook('shell', 'page', $params, $page_shell);
$system_messages = _elgg_services()->systemMessages;
$messages = null;
if ($system_messages->count()) {
$messages = $system_messages->dumpRegister();
if (isset($messages['error'])) {
// always make sure error is the first type
$errors = array(
'error' => $messages['error']
);
unset($messages['error']);
$messages = array_merge($errors, $messages);
}
}
$vars['title'] = $title;
$vars['body'] = $body;
$vars['sysmessages'] = $messages;
// head has keys 'title', 'metas', 'links'
$head_params = _elgg_views_prepare_head($title);
$vars['head'] = elgg_trigger_plugin_hook('head', 'page', $vars, $head_params);
$vars = elgg_trigger_plugin_hook('output:before', 'page', null, $vars);
$output = elgg_view("page/$page_shell", $vars);
$vars['page_shell'] = $page_shell;
// Allow plugins to modify the output
$output = elgg_trigger_plugin_hook('output', 'page', $vars, $output);
$timer->end([__FUNCTION__]);
return $output;
}
/**
* Render a resource view. Use this in your page handler to hand off page rendering to
* a view in "resources/". If not found in the current viewtype, we try the "default" viewtype.
*
* @param string $name The view name without the leading "resources/"
* @param array $vars Arguments passed to the view
*
* @return string
* @throws SecurityException
*/
function elgg_view_resource($name, array $vars = []) {
$view = "resources/$name";
if (elgg_view_exists($view)) {
return _elgg_services()->views->renderView($view, $vars);
}
if (elgg_get_viewtype() !== 'default' && elgg_view_exists($view, 'default')) {
return _elgg_services()->views->renderView($view, $vars, false, 'default');
}
_elgg_services()->logger->error("The view $view is missing.");
if (elgg_get_viewtype() === 'default') {
// only works for default viewtype
forward('', '404');
} else {
register_error(elgg_echo('error:404:content'));
forward('');
}
}
/**
* Prepare the variables for the html head
*
* @param string $title Page title for <head>
* @return array
* @access private
*/
function _elgg_views_prepare_head($title) {
$params = array(
'links' => array(),
'metas' => array(),
);
if (empty($title)) {
$params['title'] = elgg_get_config('sitename');
} else {
$params['title'] = $title . ' : ' . elgg_get_config('sitename');
}
$params['metas']['content-type'] = array(
'http-equiv' => 'Content-Type',
'content' => 'text/html; charset=utf-8',
);
$params['metas']['description'] = array(
'name' => 'description',
'content' => elgg_get_config('sitedescription')
);
// https://developer.chrome.com/multidevice/android/installtohomescreen
$params['metas']['viewport'] = array(
'name' => 'viewport',
'content' => 'width=device-width',
);
$params['metas']['mobile-web-app-capable'] = array(
'name' => 'mobile-web-app-capable',
'content' => 'yes',
);
$params['metas']['apple-mobile-web-app-capable'] = array(
'name' => 'apple-mobile-web-app-capable',
'content' => 'yes',
);
$params['links']['apple-touch-icon'] = array(
'rel' => 'apple-touch-icon',
'href' => elgg_get_simplecache_url('favicon-128.png'),
);
// favicons
$params['links']['icon-ico'] = array(
'rel' => 'icon',
'href' => elgg_get_simplecache_url('favicon.ico'),
);
$params['links']['icon-vector'] = array(
'rel' => 'icon',
'sizes' => '16x16 32x32 48x48 64x64 128x128',
'type' => 'image/svg+xml',
'href' => elgg_get_simplecache_url('favicon.svg'),
);
$params['links']['icon-16'] = array(
'rel' => 'icon',
'sizes' => '16x16',
'type' => 'image/png',
'href' => elgg_get_simplecache_url('favicon-16.png'),
);
$params['links']['icon-32'] = array(
'rel' => 'icon',
'sizes' => '32x32',
'type' => 'image/png',
'href' => elgg_get_simplecache_url('favicon-32.png'),
);
$params['links']['icon-64'] = array(
'rel' => 'icon',
'sizes' => '64x64',
'type' => 'image/png',
'href' => elgg_get_simplecache_url('favicon-64.png'),
);
$params['links']['icon-128'] = array(
'rel' => 'icon',
'sizes' => '128x128',
'type' => 'image/png',
'href' => elgg_get_simplecache_url('favicon-128.png'),
);
// RSS feed link
if (_elgg_has_rss_link()) {
$url = current_page_url();
if (substr_count($url,'?')) {
$url .= "&view=rss";
} else {
$url .= "?view=rss";
}
$params['links']['rss'] = array(
'rel' => 'alternative',
'type' => 'application/rss+xml',
'title' => 'RSS',
'href' => elgg_format_url($url),
);
}
return $params;
}
/**
* Displays a layout with optional parameters.
*
* Layouts provide consistent organization of pages and other blocks of content.
* There are a few default layouts in core:
* - admin A special layout for the admin area.
* - one_column A single content column.
* - one_sidebar A content column with sidebar.
* - two_sidebar A content column with two sidebars.
* - widgets A widget canvas.
*
* The layout views take the form page/layouts/$layout_name
* See the individual layouts for what options are supported. The three most
* common layouts have these parameters:
* one_column
* content => string
* one_sidebar
* content => string
* sidebar => string (optional)
* content
* content => string
* sidebar => string (optional)
* buttons => string (override the default add button)
* title => string (override the default title)
* filter_context => string (selected content filter)
* See the content layout view for more parameters
*
* @param string $layout_name The name of the view in page/layouts/.
* @param array $vars Associative array of parameters for the layout view
*
* @return string The layout
*/
function elgg_view_layout($layout_name, $vars = array()) {
$timer = _elgg_services()->timer;
if (!$timer->hasEnded(['build page'])) {
$timer->end(['build page']);
}
$timer->begin([__FUNCTION__]);
$params = array();
$params['identifier'] = _elgg_services()->request->getFirstUrlSegment();
$params['segments'] = _elgg_services()->request->getUrlSegments();
array_shift($params['segments']);
$layout_name = elgg_trigger_plugin_hook('layout', 'page', $params, $layout_name);
$param_array = $vars;
$param_array['layout'] = $layout_name;
$params = elgg_trigger_plugin_hook('output:before', 'layout', null, $param_array);
if (elgg_view_exists("page/layouts/$layout_name")) {
$output = elgg_view("page/layouts/$layout_name", $params);
} else {
$output = elgg_view("page/layouts/default", $params);
}
$output = elgg_trigger_plugin_hook('output:after', 'layout', $params, $output);
$timer->end([__FUNCTION__]);
return $output;
}
/**
* Render a menu
*
* @see elgg_register_menu_item() for documentation on adding menu items and
* navigation.php for information on the different menus available.
*
* This function triggers a 'register', 'menu:<menu name>' plugin hook that enables
* plugins to add menu items just before a menu is rendered. This is used by
* dynamic menus (menus that change based on some input such as the user hover
* menu). Using elgg_register_menu_item() in response to the hook can cause
* incorrect links to show up. See the blog plugin's blog_owner_block_menu()
* for an example of using this plugin hook.
*
* An additional hook is the 'prepare', 'menu:<menu name>' which enables plugins
* to modify the structure of the menu (sort it, remove items, set variables on
* the menu items).
*
* elgg_view_menu() uses views in navigation/menu
*
* @param string $menu_name The name of the menu
* @param array $vars An associative array of display options for the menu.
* Options include:
* sort_by => string or php callback
* string options: 'name', 'priority', 'title' (default),
* 'register' (registration order) or a
* php callback (a compare function for usort)
* handler: string the page handler to build action URLs
* entity: \ElggEntity to use to build action URLs
* class: string the class for the entire menu.
* show_section_headers: bool show headers before menu sections.
*
* @return string
* @since 1.8.0
*/
function elgg_view_menu($menu_name, array $vars = array()) {
global $CONFIG;
$vars['name'] = $menu_name;
$vars = elgg_trigger_plugin_hook('parameters', "menu:$menu_name", $vars, $vars);
$sort_by = elgg_extract('sort_by', $vars, 'text');
if (isset($CONFIG->menus[$menu_name])) {
$menu = $CONFIG->menus[$menu_name];
} else {
$menu = array();
}
// Give plugins a chance to add menu items just before creation.
// This supports dynamic menus (example: user_hover).
$menu = elgg_trigger_plugin_hook('register', "menu:$menu_name", $vars, $menu);
$builder = new \ElggMenuBuilder($menu);
$vars['menu'] = $builder->getMenu($sort_by);
$vars['selected_item'] = $builder->getSelected();
// Let plugins modify the menu
$vars['menu'] = elgg_trigger_plugin_hook('prepare', "menu:$menu_name", $vars, $vars['menu']);
if (elgg_view_exists("navigation/menu/$menu_name")) {
return elgg_view("navigation/menu/$menu_name", $vars);
} else {
return elgg_view("navigation/menu/default", $vars);
}
}
/**
* Render a menu item (usually as a link)
*
* @param \ElggMenuItem $item The menu item
* @param array $vars Options to pass to output/url if a link
* @return string
* @since 1.9.0
*/
function elgg_view_menu_item(\ElggMenuItem $item, array $vars = array()) {
if (!isset($vars['class'])) {
$vars['class'] = 'elgg-menu-content';
}
$vars = array_merge($item->getValues(), $vars);
if ($item->getLinkClass()) {
$vars['class'] .= ' ' . $item->getLinkClass();
}
if ($item->getHref() === false || $item->getHref() === null) {
$text = $item->getText();
// if contains elements, don't wrap
if (preg_match('~<[a-z]~', $text)) {
return $text;
} else {
return elgg_format_element('span', array('class' => 'elgg-non-link'), $text);
}
}
if (!isset($vars['rel']) && !isset($vars['is_trusted'])) {
$vars['is_trusted'] = true;
}
if ($item->getConfirmText()) {
$vars['confirm'] = $item->getConfirmText();
}
return elgg_view('output/url', $vars);
}
/**
* Returns a string of a rendered entity.
*
* Entity views are either determined by setting the view property on the entity
* or by having a view named after the entity $type/$subtype. Entities that have
* neither a view property nor a defined $type/$subtype view will fall back to
* using the $type/default view.
*
* The entity view is called with the following in $vars:
* - \ElggEntity 'entity' The entity being viewed
*
* @tip This function can automatically appends annotations to entities if in full
* view and a handler is registered for the entity:annotate. See https://github.com/Elgg/Elgg/issues/964 and
* {@link elgg_view_entity_annotations()}.
*
* @param \ElggEntity $entity The entity to display
* @param array $vars Array of variables to pass to the entity view.
* 'full_view' Whether to show a full or condensed view. (Default: true)
* 'item_view' Alternative view used to render this entity
* @param boolean $bypass Ignored and will be removed eventually
* @param boolean $debug Complain if views are missing
*
* @return string HTML to display or false
* @todo The annotation hook might be better as a generic plugin hook to append content.
*/
function elgg_view_entity(\ElggEntity $entity, array $vars = array(), $bypass = false, $debug = false) {
// No point continuing if entity is null
if (!$entity || !($entity instanceof \ElggEntity)) {
return false;
}
elgg_register_rss_link();
$defaults = array(
'full_view' => true,
);
$vars = array_merge($defaults, $vars);
$vars['entity'] = $entity;
$entity_type = $entity->getType();
$entity_subtype = $entity->getSubtype();
if (empty($entity_subtype)) {
$entity_subtype = 'default';
}
$entity_views = array(
elgg_extract('item_view', $vars, ''),
"$entity_type/$entity_subtype",
"$entity_type/default",
);
$contents = '';
foreach ($entity_views as $view) {
if (elgg_view_exists($view)) {
$contents = elgg_view($view, $vars, $bypass, $debug);
break;
}
}
// Marcus Povey 20090616 : Speculative and low impact approach for fixing #964
if ($vars['full_view']) {
$annotations = elgg_view_entity_annotations($entity, $vars['full_view']);
if ($annotations) {
$contents .= $annotations;
}
}
return $contents;
}
/**
* View the icon of an entity
*
* Entity views are determined by having a view named after the entity $type/$subtype.
* Entities that do not have a defined icon/$type/$subtype view will fall back to using
* the icon/$type/default view.
*
* @param \ElggEntity $entity The entity to display
* @param string $size The size: tiny, small, medium, large
* @param array $vars An array of variables to pass to the view. Some possible
* variables are img_class and link_class. See the
* specific icon view for more parameters.
*
* @return string HTML to display or false
*/
function elgg_view_entity_icon(\ElggEntity $entity, $size = 'medium', $vars = array()) {
// No point continuing if entity is null
if (!$entity || !($entity instanceof \ElggEntity)) {
return false;
}
$vars['entity'] = $entity;
$vars['size'] = $size;
$entity_type = $entity->getType();
$subtype = $entity->getSubtype();
if (empty($subtype)) {
$subtype = 'default';
}
$contents = '';
if (elgg_view_exists("icon/$entity_type/$subtype")) {
$contents = elgg_view("icon/$entity_type/$subtype", $vars);
}
if (empty($contents)) {
$contents = elgg_view("icon/$entity_type/default", $vars);
}
if (empty($contents)) {
$contents = elgg_view("icon/default", $vars);
}
return $contents;
}
/**
* Returns a string of a rendered annotation.
*
* Annotation views are expected to be in annotation/$annotation_name.
* If a view is not found for $annotation_name, the default annotation/default
* will be used.
*
* @warning annotation/default is not currently defined in core.
*
* The annotation view is called with the following in $vars:
* - \ElggEntity 'annotation' The annotation being viewed.
*
* @param \ElggAnnotation $annotation The annotation to display
* @param array $vars Variable array for view.
* 'item_view' Alternative view used to render an annotation
* @param bool $bypass Ignored and will be removed eventually
* @param bool $debug Complain if views are missing
*
* @return string/false Rendered annotation
*/
function elgg_view_annotation(\ElggAnnotation $annotation, array $vars = array(), $bypass = false, $debug = false) {
elgg_register_rss_link();
$defaults = array(
'full_view' => true,
);
$vars = array_merge($defaults, $vars);
$vars['annotation'] = $annotation;
$name = $annotation->name;
if (empty($name)) {
return false;
}
$annotation_views = array(
elgg_extract('item_view', $vars, ''),
"annotation/$name",
"annotation/default",
);
$contents = '';
foreach ($annotation_views as $view) {
if (elgg_view_exists($view)) {
$contents = elgg_view($view, $vars, $bypass, $debug);
break;
}
}
return $contents;
}
/**
* Returns a rendered list of entities with pagination. This function should be
* called by wrapper functions.
*
* @see elgg_list_entities()
* @see list_user_friends_objects()
* @see elgg_list_entities_from_metadata()
* @see elgg_list_entities_from_relationships()
* @see elgg_list_entities_from_annotations()
*
* @param array $entities Array of entities
* @param array $vars Display variables
* 'count' The total number of entities across all pages
* 'offset' The current indexing offset
* 'limit' The number of entities to display per page (default from settings)
* 'full_view' Display the full view of the entities?
* 'list_class' CSS class applied to the list
* 'item_class' CSS class applied to the list items
* 'item_view' Alternative view to render list items
* 'pagination' Display pagination?
* 'base_url' Base URL of list (optional)
* 'url_fragment' URL fragment to add to links if not present in base_url (optional)
* 'position' Position of the pagination: before, after, or both
* 'list_type' List type: 'list' (default), 'gallery'
* 'list_type_toggle' Display the list type toggle?
* 'no_results' Message to display if no results (string|Closure)
*
* @return string The rendered list of entities
*/
function elgg_view_entity_list($entities, array $vars = array()) {
$offset = (int)get_input('offset', 0);
// list type can be passed as request parameter
$list_type = get_input('list_type', 'list');
$defaults = array(
'items' => $entities,
'list_class' => 'elgg-list-entity',
'full_view' => true,
'pagination' => true,
'list_type' => $list_type,
'list_type_toggle' => false,
'offset' => $offset,
'limit' => null,
);
$vars = array_merge($defaults, $vars);
if (!$vars["limit"] && !$vars["offset"]) {
// no need for pagination if listing is unlimited
$vars["pagination"] = false;
}
if ($vars['list_type'] != 'list') {
return elgg_view('page/components/gallery', $vars);