aboutsummaryrefslogtreecommitdiff
path: root/mod/defaultwidgets/start.php
blob: acc8951d38f451f753595e1351f0c0fa9df59adc (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
<?php
/**
 * Elgg default_widgets plugin.
 *
 * @package DefaultWidgets
 *
 * Code based on the work of:
 * @link http://www.tastyseed.com
 * @link http://www.chadsowald.com
 * @links http://www.somosmas.org
 *
 */

/**
 * Default widgets initialisation
 *
 * These parameters are required for the event API, but we won't use them:
 *
 * @param unknown_type $event
 * @param unknown_type $object_type
 * @param unknown_type $object
 */
function defaultwidgets_init() {
	// register create user event hook
	elgg_register_event_handler('create', 'user', 'defaultwidgets_newusers' );

	// set the widget access to the default access on validation if this is not an admin-created user
	if (!isadminloggedin()) {
		elgg_register_event_handler('validate', 'user', 'defaultwidgets_reset_access');
	}

	$action_dir = elgg_get_plugin_path() . 'defaultwidgets/actions';
	elgg_register_action("defaultwidgets/update", "$action_dir/update.php", 'admin');

	elgg_register_plugin_hook_handler('permissions_check', 'user', 'defaultwidgets_can_edit');
	elgg_register_plugin_hook_handler('permissions_check', 'object', 'defaultwidgets_can_edit');
	elgg_register_plugin_hook_handler('container_permissions_check', 'user', 'defaultwidgets_can_edit_container');
	
	// @todo These submenu pages should be DRYed up
	//elgg_add_admin_menu_item('default_profile_widgets', elgg_echo('defaultwidgets:menu:profile'), 'appearance');
	//elgg_add_admin_menu_item('default_dashboard_widgets', elgg_echo('defaultwidgets:menu:dashboard'), 'appearance');
}

/**
 * Overrides default permissions for the default widgets context
 *
 */
function defaultwidgets_can_edit($hook_name, $entity_type, $return_value, $parameters) {
	global $defaultwidget_access;

	if ($defaultwidget_access) {
		return true;
	}
	return $return_value;
}

/**
 * Override the canEditMetadata function to return true for messages
 *
 */
function defaultwidgets_can_edit_metadata($hook_name, $entity_type, $return_value, $parameters) {
	global $defaultwidget_access;

	if ($defaultwidget_access) {
		return true;
	}
	return $return_value;

}

/**
 * Override the canEdit function to return true for messages within a particular context.
 *
 */
function defaultwidgets_can_edit_container($hook_name, $entity_type, $return_value, $parameters) {
	global $defaultwidget_access;

	if ($defaultwidget_access) {
		return true;
	}
	return $return_value;
}

/**
 * Extends the create user event to add admin defined widgets to the dashboard/profile context
 */
function defaultwidgets_newusers($event, $object_type, $object) {

	// turn on permissions override
	global $defaultwidget_access, $CONFIG;
	$defaultwidget_access = true;

	// get the new user guid
	$guid = $object->guid;

	if (isadminloggedin()) {
		// this is an admin-created user
		// no permissions problems, so set proper access now
		// use system default access (not the admin's default access!, because that could be a personal access level)
		$widget_access = elgg_get_config('default_access');
	} else {
		// this is a regular registration
		// set widget access to public for now and reset it properly during the validate event
		// to avoid Elgg permissions problems
		$widget_access = ACCESS_PUBLIC;
	}

	// check if it's set
	if (! empty ( $guid )) {

		// get the user entity
		if ($user = get_entity ( $guid )) {

			// can this user edit
			if ($user->canEdit ()) {

				// each of the contexts to add widgets for
				$contexts = array ('profile', 'dashboard' );

				// get the entities for the module
				$entities = elgg_get_entities (array('type' => 'object', 'subtype' => 'moddefaultwidgets', 'limit' => 9999));

				// check if the entity exists
				if (isset ( $entities [0] )) {

					// get the widgets for the context
					$entity = $entities [0];

					foreach ( $contexts as $context ) {
						$current_widgets = $entity->$context;
						list ( $left, $middle, $right ) = split ( '%%', $current_widgets );

						// split columns into seperate widgets
						$area1widgets = split ( '::', $left );
						$area2widgets = split ( '::', $middle );
						$area3widgets = split ( '::', $right );

						// clear out variables if no widgets are available
						if ($area1widgets [0] == "")
							$area1widgets = false;
						if ($area2widgets [0] == "")
							$area2widgets = false;
						if ($area3widgets [0] == "")
							$area3widgets = false;

						// generate left column widgets for a new user
						if ($area1widgets) {
							foreach ( $area1widgets as $i => $widget ) {
								add_widget ( $guid, $widget, $context, ($i + 1), 1, $widget_access );
							}
						}

						// generate middle column widgets for a new user
						if ($area2widgets) {
							foreach ( $area2widgets as $i => $widget ) {
								add_widget ( $guid, $widget, $context, ($i + 1), 2, $widget_access );
							}
						}

						// generate right column widgets for a new user
						if ($area3widgets) {
							foreach ( $area3widgets as $i => $widget ) {
								add_widget ( $guid, $widget, $context, ($i + 1), 3, $widget_access );
							}
						}
					}
				}
			}
		}
	}

	// turn off permissions override
	$defaultwidget_access = false;
}

function defaultwidgets_reset_access($event, $object_type, $object) {

	global $defaultwidget_access;

	// turn on permissions override
	$defaultwidget_access = true;

	// the widgets are disabled, so turn on the ability to see disabled entities

	$access_status = access_get_show_hidden_status();
	access_show_hidden_entities(true);

	$widgets = elgg_get_entities(array('type' => 'object', 'subtype' => 'widget', 'owner_guid' => $object->getGUID()));

	if ($widgets) {
		foreach($widgets as $widget) {
			$widget->access_id = get_default_access();
			$widget->save();
		}
	}

	access_show_hidden_entities($access_status);

	// turn off permissions override
	$defaultwidget_access = false;

	return true;
}

// Make sure the status initialisation function is called on initialisation
elgg_register_event_handler('init', 'system', 'defaultwidgets_init');