start.php ➔ embed_list_items()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * Elgg media embed plugin
4
 *
5
 * @package ElggEmbed
6
 */
7
8
9
elgg_register_event_handler('init', 'system', 'embed_init');
10
11
/**
12
 * Init function
13
 */
14
function embed_init() {
15
	elgg_extend_view('css/elgg', 'embed/css');
16
	elgg_extend_view('css/admin', 'embed/css');
17
18
	if (elgg_is_logged_in()) {
19
		elgg_register_plugin_hook_handler('register', 'menu:longtext', 'embed_longtext_menu');
20
	}
21
	elgg_register_plugin_hook_handler('register', 'menu:embed', 'embed_select_tab', 1000);
22
23
	// Page handler for the modal media embed
24
	elgg_register_page_handler('embed', 'embed_page_handler');
25
	
26
	$embed_js = elgg_get_simplecache_url('js', 'embed/embed');
27
	elgg_register_js('elgg.embed', $embed_js, 'footer');
28
}
29
30
/**
31
 * Add the embed menu item to the long text menu
32
 *
33
 * @param string $hook
34
 * @param string $type
35
 * @param array $items
36
 * @param array $vars
37
 * @return array
38
 */
39
function embed_longtext_menu($hook, $type, $items, $vars) {
40
41
	if (elgg_get_context() == 'embed') {
42
		return $items;
43
	}
44
45
	$url = 'embed';
46
47
	$page_owner = elgg_get_page_owner_entity();
48
	if (elgg_instanceof($page_owner, 'group') && $page_owner->isMember()) {
49
		$url = 'embed?container_guid=' . $page_owner->getGUID();
50
	}
51
52
	elgg_load_js('lightbox');
53
	elgg_load_css('lightbox');
54
	elgg_require_js('jquery.form');
55
	elgg_load_js('elgg.embed');
56
57
	$text = elgg_echo('embed:media');
58
59
	// if loaded through ajax (like on /activity), pull in JS libs manually
60
	// hack for #6422 because we haven't converted everything to amd yet
61
	if (elgg_in_context('ajax')) {
62
		$externals = elgg_get_config('externals_map');
63
		$embed = elgg_extract('elgg.embed', $externals['js']);
64
		$lightbox_js = elgg_extract('lightbox', $externals['js']);
65
		$lightbox_css = elgg_extract('lightbox', $externals['css']);
66
		
67
		$text .= <<<___JS
68
<script>
69
	require(['jquery.form']);
70
	if (typeof $.fancybox === 'undefined') {
71
		$.getScript('$lightbox_js->url');
72
		$('head').append('<link rel="stylesheet" href="$lightbox_css->url"></link>');
73
	}
74
	if (typeof elgg.embed === 'undefined') {
75
		$.getScript('$embed->url');
76
	}
77
</script>
78
___JS;
79
	}
80
81
	$items[] = ElggMenuItem::factory(array(
82
		'name' => 'embed',
83
		'href' => 'javascript:void()',
84
		'data-colorbox-opts' => json_encode([
85
			'href' => elgg_normalize_url($url),
86
		]),
87
		'text' => $text,
88
		'rel' => "embed-lightbox-{$vars['id']}",
89
		'link_class' => "elgg-longtext-control elgg-lightbox embed-control embed-control-{$vars['id']}",
90
		'priority' => 10,
91
	));
92
	
93
	return $items;
94
}
95
96
/**
97
 * Select the correct embed tab for display
98
 *
99
 * @param string $hook
100
 * @param string $type
101
 * @param array $items
102
 * @param array $vars
103
 */
104
function embed_select_tab($hook, $type, $items, $vars) {
105
106
	// can this ba called from page handler instead?
107
	$page = get_input('page');
108
	$tab_name = array_pop(explode('/', $page));
0 ignored issues
show
Bug introduced by
explode('/', $page) cannot be passed to array_pop() as the parameter $array expects a reference.
Loading history...
109
	foreach ($items as $item) {
110
		if ($item->getName() == $tab_name) {
111
			$item->setSelected();
112
			elgg_set_config('embed_tab', $item);
113
		}
114
	}
115
116
	if (!elgg_get_config('embed_tab') && count($items) > 0) {
117
		$items[0]->setSelected();
118
		elgg_set_config('embed_tab', $items[0]);
119
	}
120
}
121
122
/**
123
 * Serves the content for the embed lightbox
124
 *
125
 * @param array $page URL segments
126
 */
127
function embed_page_handler($page) {
128
129
	$container_guid = (int)get_input('container_guid');
130
	if ($container_guid) {
131
		$container = get_entity($container_guid);
132
133
		if (elgg_instanceof($container, 'group') && $container->isMember()) {
134
			// embedding inside a group so save file to group files
135
			elgg_set_page_owner_guid($container_guid);
136
		}
137
	}
138
139
	set_input('page', $page[1]); 
140
141
	echo elgg_view('embed/layout');
142
143
	// exit because this is in a modal display.
144
	exit;
145
}
146
147
/**
148
 * A special listing function for selectable content
149
 *
150
 * This calls a custom list view for entities. 
151
 *
152
 * @param array $entities Array of ElggEntity objects
153
 * @param array $vars     Display parameters
154
 * @return string
155
 */
156
function embed_list_items($entities, $vars = array()) {
157
158
	$defaults = array(
159
		'items' => $entities,
160
		'list_class' => 'elgg-list-entity',
161
	);
162
163
	$vars = array_merge($defaults, $vars);
164
165
	return elgg_view('embed/list', $vars);
166
}
167
168
/**
169
 * Set the options for the list of embedable content
170
 *
171
 * @param array $options
172
 * @return array
173
 */
174
function embed_get_list_options($options = array()) {
175
176
	$container_guids = array(elgg_get_logged_in_user_guid());
177
	if (elgg_get_page_owner_guid()) {
178
		$page_owner_guid = elgg_get_page_owner_guid();
179
		if ($page_owner_guid != elgg_get_logged_in_user_guid()) {
180
			$container_guids[] = $page_owner_guid;
181
		}
182
	}
183
184
	$defaults = array(
185
		'limit' => 6,
186
		'container_guids' => $container_guids,
187
		'item_class' => 'embed-item',
188
	);
189
190
	$options = array_merge($defaults, $options);
191
192
	return $options;
193
}
194