Passed
Push — master ( c2d8e3...289151 )
by Jeroen
06:06
created

profile_page_handler()   C

Complexity

Conditions 8
Paths 24

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 18
nc 24
nop 1
dl 0
loc 31
ccs 0
cts 18
cp 0
crap 72
rs 5.3846
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A profile_default_widgets_hook() 0 12 1
1
<?php
2
/**
3
 * Elgg profile plugin
4
 *
5
 * @package ElggProfile
6
 */
7
8
/**
9
 * Profile init function
10
 *
11
 * @return void
12
 */
13
function profile_init() {
14
	elgg_extend_view('elgg.css', 'profile/css');
15
	
16 31
	elgg_register_ajax_view('forms/profile/fields/add');
17
18 31
	// allow ECML in parts of the profile
19
	elgg_register_plugin_hook_handler('get_views', 'ecml', 'profile_ecml_views_hook');
20
21 31
	// allow admins to set default widgets for users on profiles
22
	elgg_register_plugin_hook_handler('get_list', 'default_widgets', 'profile_default_widgets_hook');
23 31
	
24
	elgg_register_plugin_hook_handler('register', 'menu:topbar', '_profile_topbar_menu');
25 31
	elgg_register_plugin_hook_handler('register', 'menu:title', '_profile_title_menu');
26
	elgg_register_plugin_hook_handler('register', 'menu:page', '_profile_admin_page_menu');
27
	elgg_register_plugin_hook_handler('register', 'menu:page', '_profile_user_page_menu');
28 31
	elgg_register_plugin_hook_handler('register', 'menu:user_hover', '_profile_user_hover_menu');
29
}
30
31 31
/**
32
 * Parse ECML on parts of the profile
33 31
 *
34 31
 * @param string $hook         'get_views'
35 31
 * @param string $type         'ecml'
36 31
 * @param array  $return_value current return value
37 31
 *
38 31
 * @return array
39
 */
40
function profile_ecml_views_hook($hook, $type, $return_value) {
2 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

40
function profile_ecml_views_hook(/** @scrutinizer ignore-unused */ $hook, $type, $return_value) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

40
function profile_ecml_views_hook($hook, /** @scrutinizer ignore-unused */ $type, $return_value) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
	$return_value['profile/profile_content'] = elgg_echo('profile');
42
43
	return $return_value;
44
}
45
46
/**
47
 * Register profile widgets with default widgets
48
 *
49
 * @param string $hook   'get_list'
50
 * @param string $type   'default_widgets'
51
 * @param array  $return current return value
52
 *
53
 * @return array
54
 */
55
function profile_default_widgets_hook($hook, $type, $return) {
2 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

55
function profile_default_widgets_hook(/** @scrutinizer ignore-unused */ $hook, $type, $return) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

55
function profile_default_widgets_hook($hook, /** @scrutinizer ignore-unused */ $type, $return) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
	$return[] = [
57
		'name' => elgg_echo('profile'),
58
		'widget_context' => 'profile',
59
		'widget_columns' => 2,
60
61
		'event' => 'create',
62
		'entity_type' => 'user',
63
		'entity_subtype' => ELGG_ENTITIES_ANY_VALUE,
64
	];
65
66
	return $return;
67
}
68
69
/**
70
 * This function loads a set of default fields into the profile, then triggers a hook letting other plugins to edit
71
 * add and delete fields.
72
 *
73
 * Note: This is a secondary system:init call and is run at a super low priority to guarantee that it is called after all
74
 * other plugins have initialised.
75
 *
76
 * @return void
77
 *
78
 * @access private
79
 */
80
function _profile_fields_setup() {
81
	$profile_defaults =  [
82
		'description' => 'longtext',
83
		'briefdescription' => 'text',
84
		'location' => 'location',
85
		'interests' => 'tags',
86
		'skills' => 'tags',
87
		'contactemail' => 'email',
88
		'phone' => 'text',
89
		'mobile' => 'text',
90
		'website' => 'url',
91 1
		'twitter' => 'text',
92 1
	];
93
94
	$loaded_defaults = [];
95
	$fieldlist = elgg_get_config('profile_custom_fields');
96 1
	if ($fieldlist || $fieldlist === '0') {
97
		$fieldlistarray = explode(',', $fieldlist);
98
		foreach ($fieldlistarray as $listitem) {
99
			if ($translation = elgg_get_config("admin_defined_profile_{$listitem}")) {
100
				$type = elgg_get_config("admin_defined_profile_type_{$listitem}");
101
				$loaded_defaults["admin_defined_profile_{$listitem}"] = $type;
102
				add_translation(get_current_language(), ["profile:admin_defined_profile_{$listitem}" => $translation]);
103
			}
104
		}
105
	}
106
107
	if (count($loaded_defaults)) {
108
		elgg_set_config('profile_using_custom', true);
109
		$profile_defaults = $loaded_defaults;
110
	}
111
	
112
	$profile_fields = elgg_trigger_plugin_hook('profile:fields', 'profile', null, $profile_defaults);
113
	elgg_set_config('profile_fields', $profile_fields);
114
115
	// register any tag metadata names
116
	foreach ($profile_fields as $name => $type) {
117
		if ($type == 'tags' || $type == 'location' || $type == 'tag') {
118
			elgg_register_tag_metadata_name($name);
119
			// register a tag name translation
120
			add_translation(get_current_language(), ["tag_names:$name" => elgg_echo("profile:$name")]);
121
		}
122
	}
123
}
124 18
125 18
/**
126 18
 * Register menu items for the topbar menu
127 18
 *
128
 * @param \Elgg\Hook $hook 'register' 'menu:topbar'
129 18
 *
130 18
 * @return void|ElggMenuItem[]
131
 *
132
 * @access private
133
 * @since 3.0
134 18
 */
135
function _profile_topbar_menu(\Elgg\Hook $hook) {
136
137
	$viewer = elgg_get_logged_in_user_entity();
138
	if (!$viewer) {
139
		 return;
140
	}
141
	$return = $hook->getValue();
142
	
143
	$return[] = \ElggMenuItem::factory([
144
		'name' => 'profile',
145
		'href' => $viewer->getURL(),
146
		'text' => elgg_echo('profile'),
147
		'icon' => 'user',
148
		'parent_name' => 'account',
149
		'section' => 'alt',
150 31
		'priority' => 100,
151
	]);
152
	
153
	return $return;
154
}
155
156
/**
157
 * Register menu items for the user hover menu
158
 *
159
 * @param \Elgg\Hook $hook 'register' 'menu:user_hover'
160
 *
161
 * @return void|ElggMenuItem[]
162 31
 *
163 31
 * @access private
164 31
 * @since 3.0
165
 */
166
function _profile_user_hover_menu(\Elgg\Hook $hook) {
167
168
	if (!elgg_is_logged_in()) {
169
		return;
170
	}
171
	
172
	$user = $hook->getEntityParam();
173
	if (!($user instanceof \ElggUser) || !$user->canEdit()) {
174
		return;
175 31
	}
176
	
177
	$return = $hook->getValue();
178
	$return[] = ElggMenuItem::factory([
179
		'name' => 'profile:edit',
180 31
		'text' => elgg_echo('profile:edit'),
181 31
		'icon' => 'address-card',
182
		'href' => elgg_generate_entity_url($user, 'edit'),
183
		'section' => (elgg_get_logged_in_user_guid() == $user->guid) ? 'action' : 'admin',
184 31
	]);
185 31
	
186 31
	return $return;
187
}
188 31
189
/**
190
 * Register menu items for the admin page menu
191 31
 *
192
 * @param \Elgg\Hook $hook 'register' 'menu:page'
193
 *
194
 * @return void|ElggMenuItem[]
195
 *
196
 * @access private
197
 * @since 3.0
198
 */
199
function _profile_admin_page_menu(\Elgg\Hook $hook) {
200
201
	if (!elgg_in_context('admin') || !elgg_is_admin_logged_in()) {
202
		return;
203
	}
204
	
205 1
	$return = $hook->getValue();
206 1
	
207 1
	$return[] = \ElggMenuItem::factory([
208
		'name' => 'configure_utilities:profile_fields',
209
		'text' => elgg_echo('admin:configure_utilities:profile_fields'),
210
		'href' => 'admin/configure_utilities/profile_fields',
211
		'section' => 'configure',
212
		'parent_name' => 'configure_utilities',
213
	]);
214
	
215
	return $return;
216
}
217
218
/**
219
 * Register menu items for the page menu
220
 *
221
 * @param \Elgg\Hook $hook 'register' 'menu:page'
222
 *
223
 * @return void|ElggMenuItem
224
 *
225
 * @access private
226
 * @since 3.0
227
 */
228
function _profile_user_page_menu(\Elgg\Hook $hook) {
229
230
	$owner = elgg_get_page_owner_entity();
231
	if (!$owner instanceof \ElggUser) {
232
		return;
233
	}
234
	
235
	$return = $hook->getValue();
236 1
	
237 1
	$return[] = \ElggMenuItem::factory([
238
		'name' => 'edit_profile',
239
		'href' => elgg_generate_entity_url($owner, 'edit'),
240
		'text' => elgg_echo('profile:edit'),
241
		'section' => '1_profile',
242
		'contexts' => ['settings'],
243
	]);
244
	
245
	return $return;
246
}
247
248
/**
249
 * Register menu items for the title menu
250
 *
251
 * @param string $hook   'register'
252
 * @param string $type   'menu:title'
253
 * @param array  $return Current return value
254
 * @param array  $params Hook parameters
255
 *
256
 * @return void|ElggMenuItem[]
257
 *
258
 * @access private
259
 * @since 3.0
260
 */
261
function _profile_title_menu($hook, $type, $return, $params) {
3 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

261
function _profile_title_menu($hook, $type, $return, /** @scrutinizer ignore-unused */ $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

261
function _profile_title_menu(/** @scrutinizer ignore-unused */ $hook, $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

261
function _profile_title_menu($hook, /** @scrutinizer ignore-unused */ $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
262
263
	if (!elgg_in_context('profile') || elgg_in_context('profile_edit')) {
264
		return;
265
	}
266
	
267
	$user = elgg_get_page_owner_entity();
268
	
269 1
	// grab the actions and admin menu items from user hover
270 1
	$menu = elgg()->menus->getMenu('user_hover', [
271
		'entity' => $user,
272
		'username' => $user->username,
273
	]);
274
	
275
	$actions = $menu->getSection('action', []);
276
	foreach ($actions as $action) {
277
		$action->addLinkClass('elgg-button elgg-button-action');
278
		$return[] = $action;
279
	}
280
	
281
	return $return;
282
}
283
284
return function() {
285
	elgg_register_event_handler('init', 'system', 'profile_init', 1);
286
	elgg_register_event_handler('init', 'system', '_profile_fields_setup', 10000); // Ensure this runs after other plugins
287
};
288