Completed
Pull Request — gcconnex (#1506)
by
unknown
14:50
created

wire.php ➔ get_wirepostsbycolleagues()   C

Complexity

Conditions 11
Paths 80

Size

Total Lines 69
Code Lines 49

Duplication

Lines 46
Ratio 66.67 %

Importance

Changes 0
Metric Value
cc 11
eloc 49
nc 80
nop 4
dl 46
loc 69
rs 5.7847
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * Exposes API endpoints for Wire entities
4
 */
5
6
elgg_ws_expose_function(
7
	"get.wirepost",
8
	"get_wirepost",
9
	array(
10
		"user" => array('type' => 'string', 'required' => true),
11
		"guid" => array('type' => 'int', 'required' => true),
12
		"thread" => array('type' => 'int', 'required' => false, 'default' => 0),
13
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
14
	),
15
	'Retrieves a wire post & all replies based on user id and wire post id',
16
	'POST',
17
	true,
18
	false
19
);
20
21
elgg_ws_expose_function(
22
	"get.wireposts",
23
	"get_wireposts",
24
	array(
25
		"user" => array('type' => 'string', 'required' => true),
26
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
27
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
28
		"filters" => array('type' => 'string', 'required' => false, 'default' => ""),
29
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
30
	),
31
	'Retrieves a user\'s wire posts based on user id',
32
	'POST',
33
	true,
34
	false
35
);
36
37
elgg_ws_expose_function(
38
	"get.wirepostsbycolleagues",
39
	"get_wirepostsbycolleagues",
40
	array(
41
		"user" => array('type' => 'string', 'required' => true),
42
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
43
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
44
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
45
	),
46
	'Retrieves a user\'s colleague\'s wire posts based on user id',
47
	'POST',
48
	true,
49
	false
50
);
51
52
elgg_ws_expose_function(
53
	"get.wirepostsbyuser",
54
	"get_wirepostsbyuser",
55
	array(
56
		"profileemail" => array('type' => 'string', 'required' => true),
57
		"user" => array('type' => 'string', 'required' => true),
58
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
59
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
60
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
61
	),
62
	'Retrieves a user\'s wire posts based on user id',
63
	'POST',
64
	true,
65
	false
66
);
67
68
elgg_ws_expose_function(
69
	"reply.wire",
70
	"reply_wire",
71
	array(
72
		"user" => array('type' => 'string', 'required' => true),
73
		"message" => array('type' => 'string', 'required' => true),
74
		"guid" => array('type' => 'int', 'required' => false, 'default' => 0),
75
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
76
	),
77
	'Submits a reply to a wire post based on user id and wire post id',
78
	'POST',
79
	true,
80
	false
81
);
82
83
function get_wirepost( $user, $guid, $thread, $lang ){
84
	$user_entity = is_numeric($user) ? get_user($user) : ( strpos($user, '@') !== FALSE ? get_user_by_email($user)[0] : get_user_by_username($user) );
85
 	if( !$user_entity ) return "User was not found. Please try a different GUID, username, or email address";
86
	if( !$user_entity instanceof ElggUser ) return "Invalid user. Please try a different GUID, username, or email address";
87
88
	$entity = get_entity( $guid );
89
	if( !$entity ) return "Wire was not found. Please try a different GUID";
90
	if( !$entity instanceof ElggWire ) return "Invalid wire. Please try a different GUID";
91
92
	$thread_id = $entity->wire_thread;
93
94
	if( !elgg_is_logged_in() )
95
		login($user_entity);
96
97
	if( $thread ){
98
		$all_wire_posts = elgg_list_entities_from_metadata(array(
99
			"metadata_name" => "wire_thread",
100
			"metadata_value" => $thread_id,
101
			"type" => "object",
102
			"subtype" => "thewire",
103
			"limit" => 0,
104
			"preload_owners" => true
105
		));
106
		$wire_posts = json_decode($all_wire_posts);
107
108 View Code Duplication
		foreach($wire_posts as $wire_post){
109
			$wire_post_obj = get_entity($wire_post->guid);
110
			$reshare = $wire_post_obj->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
111
112
			$url = "";
113
			if( !empty( $reshare ) ){
114
				$url = $reshare->getURL();
115
			}
116
117
			$text = "";
118
			if ( !empty($reshare->title) ) {
119
				$text = $reshare->title;
120
			} else if ( !empty($reshare->name) ) {
121
				$text = $reshare->name;
122
			} else if ( !empty($reshare->description) ) {
123
				$text = elgg_get_excerpt($reshare->description, 140);
124
			}
125
126
			$wire_post->shareURL = $url;
127
			$wire_post->shareText = gc_explode_translation($text, $lang);
128
129
			$likes = elgg_get_annotations(array(
130
				'guid' => $wire_post->guid,
131
				'annotation_name' => 'likes'
132
			));
133
			$wire_post->likes = count($likes);
134
135
			$liked = elgg_get_annotations(array(
136
				'guid' => $wire_post->guid,
137
				'annotation_owner_guid' => $user_entity->guid,
138
				'annotation_name' => 'likes'
139
			));
140
			$wire_post->liked = count($liked) > 0;
141
142
			$replied = elgg_get_entities_from_metadata(array(
143
				"metadata_name" => "wire_thread",
144
				"metadata_value" => $thread_id,
145
				"type" => "object",
146
				"subtype" => "thewire",
147
				"owner_guid" => $user_entity->guid
148
			));
149
			$wire_post->replied = count($replied) > 0;
150
151
			$wire_post->thread_id = $thread_id;
152
153
			$wire_post->userDetails = get_user_block($wire_post->owner_guid, $lang);
154
			$wire_post->description = wire_filter($wire_post->description);
155
		}
156
	} else {
157
		$wire_posts = elgg_list_entities(array(
158
			"type" => "object",
159
			"subtype" => "thewire",
160
			"guid" => $guid
161
		));
162
		$wire_post = json_decode($wire_posts)[0];
163
164
		$wire_post_obj = get_entity($wire_post->guid);
165
		$reshare = $wire_post_obj->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
166
167
		$url = "";
168
		if( !empty( $reshare ) ){
169
			$url = $reshare->getURL();
170
		}
171
172
		$text = "";
173
		if ( !empty($reshare->title) ) {
174
			$text = $reshare->title;
175
		} else if ( !empty($reshare->name) ) {
176
			$text = $reshare->name;
177
		} else if ( !empty($reshare->description) ) {
178
			$text = elgg_get_excerpt($reshare->description, 140);
179
		}
180
181
		$wire_post->shareURL = $url;
182
		$wire_post->shareText = gc_explode_translation($text, $lang);
183
184
		$likes = elgg_get_annotations(array(
185
			'guid' => $wire_post->guid,
186
			'annotation_name' => 'likes'
187
		));
188
		$wire_post->likes = count($likes);
189
190
		$liked = elgg_get_annotations(array(
191
			'guid' => $wire_post->guid,
192
			'annotation_owner_guid' => $user_entity->guid,
193
			'annotation_name' => 'likes'
194
		));
195
		$wire_post->liked = count($liked) > 0;
196
197
		$replied = elgg_get_entities_from_metadata(array(
198
			"metadata_name" => "wire_thread",
199
			"metadata_value" => $thread_id,
200
			"type" => "object",
201
			"subtype" => "thewire",
202
			"owner_guid" => $user_entity->guid
203
		));
204
		$wire_post->replied = count($replied) > 0;
205
206
		$wire_post->thread_id = $thread_id;
207
		
208
		$wire_post->userDetails = get_user_block($wire_post->owner_guid, $lang);
209
		$wire_post->description = wire_filter($wire_post->description);
210
211
		$wire_posts = $wire_post;
212
	}
213
214
	return $wire_posts;
215
}
216
217
function get_wireposts( $user, $limit, $filters, $offset, $lang ){
218
	$user_entity = is_numeric($user) ? get_user($user) : ( strpos($user, '@') !== FALSE ? get_user_by_email($user)[0] : get_user_by_username($user) );
219
 	if( !$user_entity ) return "User was not found. Please try a different GUID, username, or email address";
220
	if( !$user_entity instanceof ElggUser ) return "Invalid user. Please try a different GUID, username, or email address";
221
222
	if( !elgg_is_logged_in() )
223
		login($user_entity);
224
225
	$filter_data = json_decode($filters);
226
	if( !empty($filter_data) ){
227
		$params = array(
228
	        'type' => 'object',
229
	        'subtype' => 'thewire',
230
			'limit' => $limit,
231
	        'offset' => $offset
232
		);
233
234
		// if( $filter_data->mine ){
235
		// 	$params['relationship'] = 'member';
236
		// 	$params['relationship_guid'] = $user_entity->guid;
237
		// 	$params['inverse_relationship'] = FALSE;
238
		// }
239
240
		// if( $filter_data->name ){
241
		// 	$db_prefix = elgg_get_config('dbprefix');
242
  //       	$params['joins'] = array("JOIN {$db_prefix}groups_entity ge ON e.guid = ge.guid");
243
		// 	$params['wheres'] = array("(ge.name LIKE '%" . $filter_data->name . "%' OR ge.description LIKE '%" . $filter_data->name . "%')");
244
  //       }
245
246
        if( $filter_data->mine ){
247
	    	$all_wire_posts = elgg_list_entities_from_relationship($params);
248
        } else {
249
	    	$all_wire_posts = elgg_list_entities_from_metadata($params);
250
        }
251
	} else {
252
		$all_wire_posts = elgg_list_entities(array(
253
			'type' => 'object',
254
			'subtype' => 'thewire',
255
			'limit' => $limit,
256
			'offset' => $offset
257
		));
258
	}
259
260
	$wire_posts = json_decode($all_wire_posts);
261
262 View Code Duplication
	foreach($wire_posts as $wire_post){
263
		$wire_post_obj = get_entity($wire_post->guid);
264
		$reshare = $wire_post_obj->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
265
266
		$url = "";
267
		if( !empty( $reshare ) ){
268
			$url = $reshare->getURL();
269
		}
270
271
		$text = "";
272
		if ( !empty($reshare->title) ) {
273
			$text = $reshare->title;
274
		} else if ( !empty($reshare->name) ) {
275
			$text = $reshare->name;
276
		} else if ( !empty($reshare->description) ) {
277
			$text = elgg_get_excerpt($reshare->description, 140);
278
		}
279
280
		$wire_post->shareURL = $url;
281
		$wire_post->shareText = gc_explode_translation($text, $lang);
282
283
		$likes = elgg_get_annotations(array(
284
			'guid' => $wire_post->guid,
285
			'annotation_name' => 'likes'
286
		));
287
		$wire_post->likes = count($likes);
288
289
		$liked = elgg_get_annotations(array(
290
			'guid' => $wire_post->guid,
291
			'annotation_owner_guid' => $user_entity->guid,
292
			'annotation_name' => 'likes'
293
		));
294
		$wire_post->liked = count($liked) > 0;
295
296
		$replied = elgg_get_entities_from_metadata(array(
297
			"metadata_name" => "wire_thread",
298
			"metadata_value" => $wire_post->wire_thread,
299
			"type" => "object",
300
			"subtype" => "thewire",
301
			"owner_guid" => $user_entity->guid
302
		));
303
		$wire_post->replied = count($replied) > 0;
304
305
		$wire_post->userDetails = get_user_block($wire_post->owner_guid, $lang);
306
		$wire_post->description = wire_filter($wire_post->description);
307
	}
308
309
	return $wire_posts;
310
}
311
312
function get_wirepostsbycolleagues( $user, $limit, $offset, $lang ){
313
	$user_entity = is_numeric($user) ? get_user($user) : ( strpos($user, '@') !== FALSE ? get_user_by_email($user)[0] : get_user_by_username($user) );
314
 	if( !$user_entity ) return "User was not found. Please try a different GUID, username, or email address";
315
	if( !$user_entity instanceof ElggUser ) return "Invalid user. Please try a different GUID, username, or email address";
316
317
	if( !elgg_is_logged_in() )
318
		login($user_entity);
319
320
	$all_wire_posts = elgg_list_entities_from_relationship(array(
321
    	'type' => 'object',
322
		'subtype' => 'thewire',
323
		'relationship' => 'friend',
324
		'relationship_guid' => $user_entity->guid,
325
		'relationship_join_on' => 'container_guid',
326
		'limit' => $limit,
327
		'offset' => $offset
328
	));
329
	$wire_posts = json_decode($all_wire_posts);
330
331 View Code Duplication
	foreach( $wire_posts as $wire_post ){
332
		$wire_post_obj = get_entity($wire_post->guid);
333
334
		$reshare = $wire_post_obj->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
335
336
		$url = "";
337
		if( !empty( $reshare ) ){
338
			$url = $reshare->getURL();
339
		}
340
341
		$text = "";
342
		if ( !empty($reshare->title) ) {
343
			$text = $reshare->title;
344
		} else if ( !empty($reshare->name) ) {
345
			$text = $reshare->name;
346
		} else if ( !empty($reshare->description) ) {
347
			$text = elgg_get_excerpt($reshare->description, 140);
348
		}
349
350
		$wire_post->shareURL = $url;
351
		$wire_post->shareText = gc_explode_translation($text, $lang);
352
353
		$likes = elgg_get_annotations(array(
354
			'guid' => $wire_post->guid,
355
			'annotation_name' => 'likes'
356
		));
357
		$wire_post->likes = count($likes);
358
359
		$liked = elgg_get_annotations(array(
360
			'guid' => $wire_post->guid,
361
			'annotation_owner_guid' => $user_entity->guid,
362
			'annotation_name' => 'likes'
363
		));
364
		$wire_post->liked = count($liked) > 0;
365
366
		$replied = elgg_get_entities_from_metadata(array(
367
			"metadata_name" => "wire_thread",
368
			"metadata_value" => $wire_post->wire_thread,
369
			"type" => "object",
370
			"subtype" => "thewire",
371
			'owner_guid' => $user_entity->guid
372
		));
373
		$wire_post->replied = count($replied) > 0;
374
375
		$wire_post->userDetails = get_user_block($wire_post->owner_guid, $lang);
376
		$wire_post->description = wire_filter($wire_post->description);
377
	}
378
379
	return $wire_posts;
380
}
381
382
function get_wirepostsbyuser( $profileemail, $user, $limit, $offset, $lang ){
383
	$user_entity = is_numeric($profileemail) ? get_user($profileemail) : ( strpos($profileemail, '@') !== FALSE ? get_user_by_email($profileemail)[0] : get_user_by_username($profileemail) );
384
 	if( !$user_entity ) return "User was not found. Please try a different GUID, username, or email address";
385
	if( !$user_entity instanceof ElggUser ) return "Invalid user. Please try a different GUID, username, or email address";
386
387
	$viewer = is_numeric($user) ? get_user($user) : ( strpos($user, '@') !== FALSE ? get_user_by_email($user)[0] : get_user_by_username($user) );
388
 	if( !$viewer ) return "Viewer user was not found. Please try a different GUID, username, or email address";
389
	if( !$viewer instanceof ElggUser ) return "Invalid viewer user. Please try a different GUID, username, or email address";
390
391
	if( !elgg_is_logged_in() )
392
		login($user_entity);
393
394
	$all_wire_posts = elgg_list_entities(array(
395
		'type' => 'object',
396
		'subtype' => 'thewire',
397
		'owner_guid' => $user_entity->guid,
398
		'limit' => $limit,
399
		'offset' => $offset
400
	));
401
	$wire_posts = json_decode($all_wire_posts);
402
403
	foreach($wire_posts as $wire_post){
404
		$wire_post_obj = get_entity($wire_post->guid);
405
		$reshare = $wire_post_obj->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
406
407
		$url = "";
408
		if( !empty( $reshare ) ){
409
			$url = $reshare->getURL();
410
		}
411
412
		$text = "";
413
		if ( !empty($reshare->title) ) {
414
			$text = $reshare->title;
415
		} else if ( !empty($reshare->name) ) {
416
			$text = $reshare->name;
417
		} else if ( !empty($reshare->description) ) {
418
			$text = elgg_get_excerpt($reshare->description, 140);
419
		}
420
421
		$wire_post->shareURL = $url;
422
		$wire_post->shareText = gc_explode_translation($text, $lang);
423
424
		$likes = elgg_get_annotations(array(
425
			'guid' => $wire_post->guid,
426
			'annotation_name' => 'likes'
427
		));
428
		$wire_post->likes = count($likes);
429
430
		$liked = elgg_get_annotations(array(
431
			'guid' => $wire_post->guid,
432
			'annotation_owner_guid' => $viewer->guid,
433
			'annotation_name' => 'likes'
434
		));
435
		$wire_post->liked = count($liked) > 0;
436
437
		$replied = elgg_get_entities_from_metadata(array(
438
			"metadata_name" => "wire_thread",
439
			"metadata_value" => $wire_post->wire_thread,
440
			"type" => "object",
441
			"subtype" => "thewire",
442
			"owner_guid" => $viewer->guid
443
		));
444
		$wire_post->replied = count($replied) > 0;
445
446
		$wire_post->userDetails = get_user_block($wire_post->owner_guid, $lang);
447
		$wire_post->description = wire_filter($wire_post->description);
448
	}
449
450
	return $wire_posts;
451
}
452
453
function reply_wire( $user, $message, $guid, $lang ){
454
	$user_entity = is_numeric($user) ? get_user($user) : ( strpos($user, '@') !== FALSE ? get_user_by_email($user)[0] : get_user_by_username($user) );
455
 	if( !$user_entity ) return "User was not found. Please try a different GUID, username, or email address";
456
	if( !$user_entity instanceof ElggUser ) return "Invalid user. Please try a different GUID, username, or email address";
457
458
	if( trim($message) == "" ) return elgg_echo("thewire:blank");
459
460
	if( !elgg_is_logged_in() )
461
		login($user_entity);
462
463
	$message = utf8_encode($message);
464
465
	$new_wire = thewire_save_post($message, $user_entity->guid, ACCESS_PUBLIC, $guid);
466
	if( !$new_wire ) return elgg_echo("thewire:notsaved");
467
468
	return elgg_echo("thewire:posted");
469
}
470