aboutsummaryrefslogtreecommitdiff
path: root/mod/pages/start.php
blob: 83c765ccd1d327fdfa679b9f0cb255debb301ff8 (plain)
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
<?php
/**
 * Elgg Pages
 *
 * @package ElggPages
 */

/**
 * Initialise the pages plugin.
 *
 */
function pages_init() {
	global $CONFIG;

	$item = new ElggMenuItem('pages', elgg_echo('pages'), 'mod/pages/world.php');
	elgg_register_menu_item('site', $item);

	// Register a page handler, so we can have nice URLs
	register_page_handler('pages','pages_page_handler');

	// Register a url handler
	register_entity_url_handler('pages_url','object', 'page_top');
	register_entity_url_handler('pages_url','object', 'page');

	// Register some actions
	elgg_register_action("pages/edit", $CONFIG->pluginspath . "pages/actions/pages/edit.php");
	elgg_register_action("pages/editwelcome", $CONFIG->pluginspath . "pages/actions/pages/editwelcome.php");
	elgg_register_action("pages/delete", $CONFIG->pluginspath . "pages/actions/pages/delete.php");

	// Extend some views
	elgg_extend_view('css/screen','pages/css');
	elgg_extend_view('groups/menu/links', 'pages/menu'); // Add to groups context
	elgg_extend_view('groups/right_column', 'pages/groupprofile_pages'); // Add to groups context

	// Register entity type
	register_entity_type('object','page');
	register_entity_type('object','page_top');

	// Register granular notification for this type
	if (is_callable('register_notification_object')) {
		register_notification_object('object', 'page', elgg_echo('pages:new'));
		register_notification_object('object', 'page_top', elgg_echo('pages:new'));
	}

	// Listen to notification events and supply a more useful message
	elgg_register_plugin_hook_handler('notify:entity:message', 'object', 'page_notify_message');

	// add the group pages tool option
	add_group_tool_option('pages',elgg_echo('groups:enablepages'),true);

	//add a widget
	elgg_register_widget_type('pages',elgg_echo('pages'),elgg_echo('pages:widget:description'));

	// For now, we'll hard code the groups profile items as follows:
	// TODO make this user configurable

	// Language short codes must be of the form "pages:key"
	// where key is the array key below
	$CONFIG->pages = array(
		'title' => 'text',
		'description' => 'longtext',
		'tags' => 'tags',
		'access_id' => 'access',
		'write_access_id' => 'access',
	);

	elgg_register_plugin_hook_handler('register', 'menu:user_ownerblock', 'pages_user_ownerblock_menu');

	// register ecml views to parse
	elgg_register_plugin_hook_handler('get_views', 'ecml', 'pages_ecml_views_hook');
}

function pages_url($entity) {

	$title = elgg_get_friendly_title($entity->title);
	return "pg/pages/view/{$entity->guid}/$title";
}

/**
 * Sets up submenus for the pages system.  Triggered on pagesetup.
 *
 */
function pages_submenus() {

	global $CONFIG;

	$page_owner = elgg_get_page_owner();

	// Group submenu option
		if ($page_owner instanceof ElggGroup && elgg_get_context() == 'groups') {
			if($page_owner->pages_enable != "no"){
				add_submenu_item(elgg_echo("pages:group", array($page_owner->name)), "pg/pages/owned/" . $page_owner->username);
			}
		}
}

/**
 * Add a menu item to the user ownerblock
 */
function pages_user_ownerblock_menu($hook, $type, $return, $params) {
	$item = new ElggMenuItem('pages', elgg_echo('pages'), "pg/pages/owner/{$params['user']->username}");
	elgg_register_menu_item('user_ownerblock', $item);
}

/**
 * Pages page handler.
 *
 * @param array $page
 */
function pages_page_handler($page) {
	global $CONFIG;

	if (isset($page[0])) {
		// See what context we're using
		switch($page[0]) {
			case "new" :
				include($CONFIG->pluginspath . "pages/new.php");
				break;

			case "welcome" :
				if (isset($page[1])) {
					set_input('username', $page[1]);
				}
				include($CONFIG->pluginspath . "pages/welcome.php");
				break;

			case "world":
				include($CONFIG->pluginspath . "pages/world.php");
				break;
			case "owned" :
				// Owned by a user
				if (isset($page[1])) {
					set_input('username',$page[1]);
				}

				include($CONFIG->pluginspath . "pages/index.php");
				break;

			case "edit" :
				if (isset($page[1])) {
					set_input('page_guid', $page[1]);
				}

				$entity = get_entity($page[1]);
				add_submenu_item(elgg_echo('pages:label:view'), "pg/pages/view/{$page[1]}", 'pageslinks');
				// add_submenu_item(elgg_echo('pages:user'), elgg_get_site_url() . "pg/pages/owned/" . get_loggedin_user()->username, 'pageslinksgeneral');
				if (($entity) && ($entity->canEdit())) {
					add_submenu_item(elgg_echo('pages:label:edit'), "pg/pages/edit/{$page[1]}", 'pagesactions');
				}
				add_submenu_item(elgg_echo('pages:label:history'), "pg/pages/history/{$page[1]}", 'pageslinks');

				include($CONFIG->pluginspath . "pages/edit.php");
				break;

			case "view" :
				if (isset($page[1])) {
					set_input('page_guid', $page[1]);
				}

				elgg_extend_view('html_head/extend','pages/metatags');

				$entity = get_entity($page[1]);
				//add_submenu_item(elgg_echo('pages:label:view'), "pg/pages/view/{$page[1]}", 'pageslinks');
				if (($entity) && ($entity->canEdit())) {
					add_submenu_item(elgg_echo('pages:label:edit'), "pg/pages/edit/{$page[1]}", 'pagesactions');
				}

				add_submenu_item(elgg_echo('pages:label:history'), "pg/pages/history/{$page[1]}", 'pageslinks');

				include($CONFIG->pluginspath . "pages/view.php");
				break;

			case "history" :
				if (isset($page[1])) {
					set_input('page_guid', $page[1]);
				}

				elgg_extend_view('html_head/extend','pages/metatags');

				$entity = get_entity($page[1]);
				add_submenu_item(elgg_echo('pages:label:view'), "pg/pages/view/{$page[1]}", 'pageslinks');
				if (($entity) && ($entity->canEdit())) {
					add_submenu_item(elgg_echo('pages:label:edit'), "pg/pages/edit/{$page[1]}", 'pagesactions');
				}
				add_submenu_item(elgg_echo('pages:label:history'), "pg/pages/history/{$page[1]}", 'pageslinks');

				include($CONFIG->pluginspath . "pages/history.php");
				break;

			default:
				include($CONFIG->pluginspath . "pages/new.php");
				break;
		}
	}
}

/**
* Returns a more meaningful message
*
* @param unknown_type $hook
* @param unknown_type $entity_type
* @param unknown_type $returnvalue
* @param unknown_type $params
*/
function page_notify_message($hook, $entity_type, $returnvalue, $params) {
	$entity = $params['entity'];
	$to_entity = $params['to_entity'];
	$method = $params['method'];
	if (($entity instanceof ElggEntity) && (($entity->getSubtype() == 'page_top') || ($entity->getSubtype() == 'page'))) {
		$descr = $entity->description;
		$title = $entity->title;
		global $CONFIG;
		$url = elgg_get_site_url() . "pg/view/" . $entity->guid;
		if ($method == 'sms') {
			$owner = $entity->getOwnerEntity();
			return $owner->name . ' ' . elgg_echo("pages:via") . ': ' . $url . ' (' . $title . ')';
		}
		if ($method == 'email') {
			$owner = $entity->getOwnerEntity();
			return $owner->name . ' ' . elgg_echo("pages:via") . ': ' . $title . "\n\n" . $descr . "\n\n" . $entity->getURL();
		}
		if ($method == 'site') {
			$owner = $entity->getOwnerEntity();
			return $owner->name . ' ' . elgg_echo("pages:via") . ': ' . $title . "\n\n" . $descr . "\n\n" . $entity->getURL();
		}
	}
	return null;
}


/**
 * Sets the parent of the current page, for navigation purposes
 *
 * @param ElggObject $entity
 */
function pages_set_navigation_parent(ElggObject $entity) {
	$guid = $entity->getGUID();

	while ($parent_guid = $entity->parent_guid) {
		$entity = get_entity($parent_guid);
		if ($entity) {
			$guid = $entity->getGUID();
		}
	}

	set_input('treeguid',$guid);
}

function pages_get_path($guid) {

	if (!$entity = get_entity($guid)) {
		return array();
	}

	$path = array($guid);

	while ($parent_guid = $entity->parent_guid) {
		$entity = get_entity($parent_guid);
		if ($entity) {
			$path[] = $entity->getGUID();
		}
	}

	return $path;
}

/**
 * Return the correct sidebar for a given entity
 *
 * @param ElggObject $entity
 */
function pages_get_entity_sidebar(ElggObject $entity, $fulltree = 0)
{
	$body = "";

	$children = elgg_get_entities_from_metadata(array('metadata_names' => 'parent_guid', 'metadata_values' => $entity->guid, 'limit' => 9999));
	$body .= elgg_view('pages/sidebar/sidebarthis', array('entity' => $entity,
														'children' => $children,
														'fulltree' => $fulltree));
	//$body = elgg_view('pages/sidebar/wrapper', array('body' => $body));

	return $body;
}

/**
 * Extend permissions checking to extend can-edit for write users.
 *
 * @param unknown_type $hook
 * @param unknown_type $entity_type
 * @param unknown_type $returnvalue
 * @param unknown_type $params
 */
function pages_write_permission_check($hook, $entity_type, $returnvalue, $params)
{
	if ($params['entity']->getSubtype() == 'page'
		|| $params['entity']->getSubtype() == 'page_top') {

		$write_permission = $params['entity']->write_access_id;
		$user = $params['user'];

		if (($write_permission) && ($user)) {
			// $list = get_write_access_array($user->guid);
			$list = get_access_array($user->guid); // get_access_list($user->guid);

			if (($write_permission!=0) && (in_array($write_permission,$list))) {
				return true;
			}
		}
	}
}

/**
 * Extend container permissions checking to extend can_write_to_container for write users.
 *
 * @param unknown_type $hook
 * @param unknown_type $entity_type
 * @param unknown_type $returnvalue
 * @param unknown_type $params
 */
function pages_container_permission_check($hook, $entity_type, $returnvalue, $params) {

	if (elgg_get_context() == "pages") {
		if (elgg_get_page_owner_guid()) {
			if (can_write_to_container(get_loggedin_userid(), elgg_get_page_owner_guid())) return true;
		}
		if ($page_guid = get_input('page_guid',0)) {
			$entity = get_entity($page_guid);
		} else if ($parent_guid = get_input('parent_guid',0)) {
			$entity = get_entity($parent_guid);
		}
		if ($entity instanceof ElggObject) {
			if (
					can_write_to_container(get_loggedin_userid(), $entity->container_guid)
					|| in_array($entity->write_access_id,get_access_list())
				) {
					return true;
			}
		}
	}

}

/**
 * Return views to parse for pages.
 *
 * @param unknown_type $hook
 * @param unknown_type $entity_type
 * @param unknown_type $return_value
 * @param unknown_type $params
 */
function pages_ecml_views_hook($hook, $entity_type, $return_value, $params) {
	$return_value['object/page'] = elgg_echo('item:object:page');
	$return_value['object/page_top'] = elgg_echo('item:object:page_top');

	return $return_value;
}

// write permission plugin hooks
elgg_register_plugin_hook_handler('permissions_check', 'object', 'pages_write_permission_check');
elgg_register_plugin_hook_handler('container_permissions_check', 'object', 'pages_container_permission_check');

// Make sure the pages initialisation function is called on initialisation
elgg_register_event_handler('init','system','pages_init');
elgg_register_event_handler('pagesetup','system','pages_submenus');