Completed
Push — edit-blog ( ae1525 )
by
unknown
17:35
created

blog.php ➔ get_blog_edit()   C

Complexity

Conditions 11
Paths 52

Size

Total Lines 44
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 26
nc 52
nop 3
dl 0
loc 44
rs 5.2653
c 0
b 0
f 0

How to fix   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 Blog entities
4
 */
5
6
elgg_ws_expose_function(
7
	"get.blogpost",
8
	"get_blogpost",
9
	array(
10
		"user" => array('type' => 'string', 'required' => true),
11
		"guid" => array('type' => 'int', 'required' => true),
12
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
13
	),
14
	'Retrieves a blog post & all replies based on user id and blog post id',
15
	'POST',
16
	true,
17
	false
18
);
19
20
elgg_ws_expose_function(
21
	"get.blogposts",
22
	"get_blogposts",
23
	array(
24
		"user" => array('type' => 'string', 'required' => true),
25
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
26
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
27
		"filters" => array('type' => 'string', 'required' => false, 'default' => ""),
28
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
29
	),
30
	'Retrieves blog posts & all replies based on user id',
31
	'POST',
32
	true,
33
	false
34
);
35
36
elgg_ws_expose_function(
37
	"get.blogpostsbyowner",
38
	"get_blogposts_by_owner",
39
	array(
40
		"user" => array('type' => 'string', 'required' => true),
41
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
42
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
43
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en"),
44
		"target" => array('type' => 'string', 'required'=> false, 'default' => '')
45
	),
46
	'Retrieves blog posts & all replies based on user id',
47
	'POST',
48
	true,
49
	false
50
);
51
52
elgg_ws_expose_function(
53
 "get.blogpostsbycolleague",
54
 "get_blogposts_by_colleague",
55
 array(
56
	"user" => array('type' => 'string', 'required' => true),
57
	"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
58
	"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
59
	"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
60
 ),
61
 'Retrieves a container\'s blogs based on user id and container guid. Used for groups, as a group\'s blogs have container_id of the group.',
62
 'POST',
63
 true,
64
 false
65
);
66
67
elgg_ws_expose_function(
68
 "get.blogpostsbycontainer",
69
 "get_blogposts_by_container",
70
 array(
71
	 "user" => array('type' => 'string', 'required' => true),
72
	 "guid" => array('type' => 'int', 'required' => true),
73
	 "limit" => array('type' => 'int', 'required' => false, 'default' => 10),
74
	 "offset" => array('type' => 'int', 'required' => false, 'default' => 0),
75
	 "lang" => array('type' => 'string', 'required' => false, 'default' => "en")
76
 ),
77
 'Retrieves a container\'s blogs based on user id and container guid. Used for groups, as a group\'s blogs have container_id of the group.',
78
 'POST',
79
 true,
80
 false
81
);
82
83
elgg_ws_expose_function(
84
 "get.blogedit",
85
 "get_blog_edit",
86
 array(
87
	 "user" => array('type' => 'string', 'required' => true),
88
	 "guid" => array('type' => 'int', 'required' => true),
89
	 "lang" => array('type' => 'string', 'required' => false, 'default' => "en")
90
 ),
91
 'Retrieves a blog post based on user id and blog post id, with only info needed for edit form',
92
 'POST',
93
 true,
94
 false
95
);
96
97
elgg_ws_expose_function(
98
 "post.blog",
99
 "post_blog",
100
 array(
101
	 "user" => array('type' => 'string', 'required' => true),
102
	 "title" => array('type' => 'string', 'required' => true),
103
	 "excerpt" => array('type' =>'string', 'required' => false, 'default' => ''),
104
	 "body" => array('type' =>'string', 'required' => true),
105
	 "container_guid" => array('type' =>'string', 'required' => false, 'default' => ''),
106
	 "comments" => array('type' =>'int', 'required' => false, 'default' => 1),
107
	 "access" => array('type' =>'int', 'required' => false, 'default' => 1),
108
	 "status" => array('type' =>'int', 'required' => false, 'default' => 0),
109
	 "lang" => array('type' => 'string', 'required' => false, 'default' => "en")
110
 ),
111
 'Posts/Saves a new blog post',
112
 'POST',
113
 true,
114
 false
115
);
116
117
function foreach_blogs($blogs, $user_entity, $lang)
118
{
119
	foreach ($blogs as $blog_post) {
120
		$blog_post->title = gc_explode_translation($blog_post->title, $lang);
121
		$blog_post->description = gc_explode_translation($blog_post->description, $lang);
122
123
		$likes = elgg_get_annotations(array(
124
			'guid' => $blog_post->guid,
125
			'annotation_name' => 'likes'
126
		));
127
		$blog_post->likes = count($likes);
128
129
		$liked = elgg_get_annotations(array(
130
			'guid' => $blog_post->guid,
131
			'annotation_owner_guid' => $user_entity->guid,
132
			'annotation_name' => 'likes'
133
		));
134
		$blog_post->liked = count($liked) > 0;
135
136
		$blog_post->comments = get_entity_comments($blog_post->guid);
137
138
		$blog_post->userDetails = get_user_block($blog_post->owner_guid, $lang);
139
140
		$group = get_entity($blog_post->container_guid);
141
		$blog_post->group = gc_explode_translation($group->name, $lang);
142
143
		if (is_callable(array($group, 'getURL'))) {
144
			$blog_post->groupURL = $group->getURL();
145
		}
146
	}
147
	return $blogs;
148
}
149
150
function get_blogpost($user, $guid, $lang)
151
{
152
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
153
	if (!$user_entity) {
154
		return "User was not found. Please try a different GUID, username, or email address";
155
	}
156
	if (!$user_entity instanceof ElggUser) {
157
		return "Invalid user. Please try a different GUID, username, or email address";
158
	}
159
160
	$entity = get_entity($guid);
161
	if (!isset($entity)) {
162
		return "Blog was not found. Please try a different GUID";
163
	}
164
165
	if (!elgg_is_logged_in()) {
166
		login($user_entity);
167
	}
168
169
	$blog_posts = elgg_list_entities(array(
170
		'type' => 'object',
171
		'subtype' => 'blog',
172
		'guid' => $guid
173
	));
174
	$blog_post = json_decode($blog_posts)[0];
175
176
	$blog_post->title = gc_explode_translation($blog_post->title, $lang);
177
	$blog_post->description = gc_explode_translation($blog_post->description, $lang);
178
179
	$likes = elgg_get_annotations(array(
180
		'guid' => $blog_post->guid,
181
		'annotation_name' => 'likes'
182
	));
183
	$blog_post->likes = count($likes);
184
185
	$liked = elgg_get_annotations(array(
186
		'guid' => $blog_post->guid,
187
		'annotation_owner_guid' => $user_entity->guid,
188
		'annotation_name' => 'likes'
189
	));
190
	$blog_post->liked = count($liked) > 0;
191
192
	$blog_post->comments = get_entity_comments($blog_post->guid);
193
194
	$blog_post->userDetails = get_user_block($blog_post->owner_guid, $lang);
195
196
	$group = get_entity($blog_post->container_guid);
197
	$blog_post->group = gc_explode_translation($group->name, $lang);
198
199
	if (is_callable(array($group, 'getURL'))) {
200
		$blog_post->groupURL = $group->getURL();
201
	}
202
203
	return $blog_post;
204
}
205
206
function get_blogposts($user, $limit, $offset, $filters, $lang)
207
{
208
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
209
	if (!$user_entity) {
210
		return "User was not found. Please try a different GUID, username, or email address";
211
	}
212
	if (!$user_entity instanceof ElggUser) {
213
		return "Invalid user. Please try a different GUID, username, or email address";
214
	}
215
216
	if (!elgg_is_logged_in()) {
217
		login($user_entity);
218
	}
219
220
	$filter_data = json_decode($filters);
221
	if (!empty($filter_data)) {
222
		$params = array(
223
			'type' => 'object',
224
			'subtype' => 'blog',
225
			'limit' => $limit,
226
			'offset' => $offset
227
		);
228
229 View Code Duplication
		if ($filter_data->name) {
230
			$db_prefix = elgg_get_config('dbprefix');
231
			$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
232
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
233
		}
234
235
		$all_blog_posts = elgg_list_entities_from_metadata($params);
236
	} else {
237
		$all_blog_posts = elgg_list_entities(array(
238
			'type' => 'object',
239
			'subtype' => 'blog',
240
			'limit' => $limit,
241
			'offset' => $offset
242
		));
243
	}
244
245
	$blog_posts = json_decode($all_blog_posts);
246
247
	$blogs = foreach_blogs($blog_posts, $user_entity, $lang);
248
249
	return $blogs;
250
}
251
252
function get_blogposts_by_owner($user, $limit, $offset, $lang, $target)
253
{
254
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
255
	if (!$user_entity) {
256
		return "User was not found. Please try a different GUID, username, or email address";
257
	}
258
	if (!$user_entity instanceof ElggUser) {
259
		return "Invalid user. Please try a different GUID, username, or email address";
260
	}
261
262
	$target_entity = $user_entity;
263
	if (!empty($target)){
264
		$target_entity =  is_numeric($target) ? get_user($target) : (strpos($target, '@') !== false ? get_user_by_email($target)[0] : get_user_by_username($target));
265
		if (!$target_entity) {
266
			return "Target user was not found. Please try a different GUID, username, or email address";
267
		}
268
		if (!$target_entity instanceof ElggUser) {
269
			return "Invalid target user. Please try a different GUID, username, or email address";
270
		}
271
	}
272
273
	if (!elgg_is_logged_in()) {
274
		login($user_entity);
275
	}
276
277
	$all_blog_posts = elgg_list_entities(array(
278
		'type' => 'object',
279
		'subtype' => 'blog',
280
		'owner_guid' => $target_entity->guid,
281
		'limit' => $limit,
282
		'offset' => $offset
283
	));
284
285
	$blog_posts = json_decode($all_blog_posts);
286
287
	$blogs = foreach_blogs($blog_posts, $user_entity, $lang);
288
289
	return $blogs;
290
}
291
292 View Code Duplication
function get_blogposts_by_colleague($user, $limit, $offset, $lang, $target)
293
{
294
 $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
295
 if (!$user_entity) {
296
	 return "User was not found. Please try a different GUID, username, or email address";
297
 }
298
 if (!$user_entity instanceof ElggUser) {
299
	 return "Invalid user. Please try a different GUID, username, or email address";
300
 }
301
302
 if (!elgg_is_logged_in()) {
303
	 login($user_entity);
304
 }
305
306
 $all_blog_posts = elgg_list_entities_from_relationship(array(
307
	 'type' => 'object',
308
	 'subtype' => 'blog',
309
	 'relationship' => 'friend',
310
	 'relationship_guid' => $user_entity->guid,
311
	 'relationship_join_on' => 'container_guid',
312
	 'limit' => $limit,
313
	 'offset' => $offset
314
 ));
315
316
 $blog_posts = json_decode($all_blog_posts);
317
318
 $blogs = foreach_blogs($blog_posts, $user_entity, $lang);
319
320
 return $blogs;
321
}
322
323
324
function get_blogposts_by_container($user, $guid, $limit, $offset, $lang)
325
{
326
 $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
327
 if (!$user_entity) {
328
	 return "User was not found. Please try a different GUID, username, or email address";
329
 }
330
 if (!$user_entity instanceof ElggUser) {
331
	 return "Invalid user. Please try a different GUID, username, or email address";
332
 }
333
 if (!elgg_is_logged_in()) {
334
	 login($user_entity);
335
 }
336
337
 $group = get_entity($guid);
338
 if (!$group) {
339
	 return "Group was not found. Please try a different GUID";
340
 }
341
 if (!$group instanceof ElggGroup) {
342
	 return "Invalid group. Please try a different GUID";
343
 }
344
345
 $all_blog_posts = elgg_list_entities(array(
346
	 'type' => 'object',
347
	 'subtype' => 'blog',
348
	 'container_guid' => $guid,
349
	 'limit' => $limit,
350
	 'offset' => $offset,
351
	 'order_by' => 'e.last_action desc'
352
 ));
353
354
 $blog_posts = json_decode($all_blog_posts);
355
356
 $blogs_final = foreach_blogs($blog_posts, $user_entity, $lang);
357
358
 return $blogs_final;
359
}
360
361
function get_blog_edit($user, $guid, $lang)
362
{
363
 $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
364
 if (!$user_entity) {
365
	 return "User was not found. Please try a different GUID, username, or email address";
366
 }
367
 if (!$user_entity instanceof ElggUser) {
368
	 return "Invalid user. Please try a different GUID, username, or email address";
369
 }
370
371
 $entity = get_entity($guid);
372
 if (!isset($entity)) {
373
	 return "Blog was not found. Please try a different GUID";
374
 }
375
376
 if (!elgg_is_logged_in()) {
377
	 login($user_entity);
378
 }
379
380
 $blog_posts = elgg_list_entities(array(
381
	 'type' => 'object',
382
	 'subtype' => 'blog',
383
	 'guid' => $guid
384
 ));
385
 $blog_post = json_decode($blog_posts)[0];
386
387
 $blog_post->title = json_decode($blog_post->title);
388
 //$blog_post->excerpt = json_decode($blog_post->excerpt); //not correct
389
 $blog_post->description = json_decode($blog_post->description);
390
391
 $container = get_entity($blog_post->container_guid);
392
 if ($container instanceof ElggGroup){
393
	 $blog_post->group->public = $container->isPublicMembership();
394
	 if (!$blog_post->group->public && !$container->isMember($user_entity)){
395
		return elgg_echo('discussion:error:permissions');
396
	 }
397
 }
398
399
 if (is_callable(array($group, 'getURL'))) {
400
	 $blog_post->groupURL = $group->getURL();
0 ignored issues
show
Bug introduced by
The variable $group does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
401
 }
402
403
 return $blog_post;
404
}
405
406
function post_blog($user, $title, $excerpt, $body, $container_guid, $comments, $access, $status, $lang)
407
{
408
 $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
409
	 if (!$user_entity) {
410
		 return "User was not found. Please try a different GUID, username, or email address";
411
	 }
412
	 if (!$user_entity instanceof ElggUser) {
413
		 return "Invalid user. Please try a different GUID, username, or email address";
414
	 }
415
	 if (!elgg_is_logged_in()) {
416
		 login($user_entity);
417
	 }
418
	 $error = FALSE;
419
	 //check required fields being not empty
420
	 $titles = json_decode($title);
421
	 $bodies = json_decode($body);
422
	 $excerpts = json_decode($excerpt);
423
	 //Check Required
424
	 if (!$titles->en && !$titles->fr) { return elgg_echo("blog:error:missing:title"); }
425
	 if (!$bodies->en && !$bodies->fr) { return elgg_echo("blog:error:missing:description");  }
426 View Code Duplication
	 if (!($titles->en && $bodies->en) && !($titles->fr && $bodies->fr)) { return "require-same-lang"; }
427
	 //Default any Missing or faulty
428
	 if (!$titles->en) { $titles->en = ''; }
429
	 if (!$titles->fr) { $titles->fr = ''; }
430
	 if (!$bodies->en) { $bodies->en = ''; }
431
	 if (!$bodies->fr) { $bodies->fr = ''; }
432
	 if (!$excerpts->en) { $excerpts->en = ''; }
433
	 if (!$excerpts->fr) { $excerpts->fr = ''; }
434
	 if ($comments != 0 && $comments != 1) { $comments = 1; }
435
	 if ($access != 0 && $access != 1 && $access != -2 && $access !=2 ) { $access = 1; }
436
	 if ($status != 0 && $status != 1) { $status = 0; }
437
438
	 // if there is a container_guid, .: group, and access is set to group only, set access to proper group only
439
	 if (!empty($container_guid) && $access == 2){
440
		 $container = get_entity($container_guid);
441
		 //validate container and ability to write to it
442
		 if (!$container || !$container->canWriteToContainer(0, 'object', 'blog')) {
443
 			return elgg_echo('blog:error:cannot_write_to_container');
444
 		} else {
445
			$access = $container->group_acl;
446
		}
447
		 //If no group container, use user guid.
448
	 } else if ($container_guid=='') { $container_guid = $user_entity->guid; }
449
450
	 //Set int variables to correct
451
	 if ($status == 1) { $status = 'published'; } else { $status = 'draft'; }
452
	 if ($comments == 1) { $comments = 'On'; } else { $comments = 'Off'; }
453
	 if ($status == 'draft') { $access = 0; }
454
	 $titles->en = htmlspecialchars($titles->en, ENT_QUOTES, 'UTF-8');
455
	 $titles->fr = htmlspecialchars($titles->fr, ENT_QUOTES, 'UTF-8');
456
	 $excerpts->en = elgg_get_excerpt($excerpts->en);
457
	 $excerpts->fr = elgg_get_excerpt($excerpts->fr);
458
459
	 $values = array(
460
		 'title' => JSON_encode($titles),
461
		 'title2' => '',
462
		 //'title3' => '',
463
		 'description' => JSON_encode($bodies),
464
		 'description2' => '',
465
		 'description3' => '',
466
		 'status' => $status,
467
		 'access_id' => $access,
468
		 'comments_on' => $comments,
469
		 'excerpt' => JSON_encode($excerpts),
470
		 'excerpt2' => '',
471
		 'excerpt3' => '',
472
		 'tags' => '',
473
		 'publication_date' => '',
474
		 'expiration_date' => '',
475
		 'show_owner' => 'no'
476
	 );
477
478
	 //return $values;
479
	 //return elgg_echo("test: pre blog creation");
480
	 //Create blog
481
	 $blog = new ElggBlog();
482
	 $blog->subtype = 'blog';
483
	 $blog->container_guid = $container_guid;
484
	 $new_post = TRUE;
485
486
	 $old_status = $blog->status;
487
488
	 // assign values to the entity, stopping on error.
489 View Code Duplication
	 if (!$error) {
490
		 foreach ($values as $name => $value) {
491
			 if (($name != 'title2') && ($name != 'description2') &&  ($name != 'excerpt2')){ // remove input 2 in metastring table
492
			 $blog->$name = $value;
493
			 }
494
		 }
495
	 }
496
497
	 if (!$error){
498
		 if ($blog->save()){
499
500
				 $icon_file = get_resized_image_from_uploaded_file("icon", 100, 100);
501
				 $icon_sizes = elgg_get_config("icon_sizes");
502
503 View Code Duplication
				 if (!empty($icon_file) && !empty($icon_sizes)) {
504
					 // create icon
505
					 $prefix = "blogs/" . $blog->getGUID();
506
507
					 $fh = new ElggFile();
508
					 $fh->owner_guid = $blog->getOwnerGUID();
509
510
					 foreach ($icon_sizes as $icon_name => $icon_info) {
511
						 $icon_file = get_resized_image_from_uploaded_file("icon", $icon_info["w"], $icon_info["h"], $icon_info["square"], $icon_info["upscale"]);
512
						 if (!empty($icon_file)) {
513
							 $fh->setFilename($prefix . $icon_name . ".jpg");
514
515
							 if ($fh->open("write")) {
516
								 $fh->write($icon_file);
517
								 $fh->close();
518
							 }
519
						 }
520
					 }
521
522
					 $blog->icontime = time();
523
			 }
524
			 // no longer a brand new post.
525
			 $blog->deleteMetadata('new_post');
526
527
			 $status = $blog->status;
528
				 // add to river if changing status or published, regardless of new post
529
				 // because we remove it for drafts.
530
				 if (($new_post || $old_status == 'draft' ||  $old_status == 'published') && $status == 'published') {
531
					 elgg_create_river_item(array(
532
						 'view' => 'river/object/blog/create',
533
						 'action_type' => 'create',
534
						 'subject_guid' => $blog->owner_guid,
535
						 'object_guid' => $blog->getGUID(),
536
					 ));
537
					 // we only want notifications sent when post published
538
					 elgg_trigger_event('publish', 'object', $blog);
0 ignored issues
show
Documentation introduced by
$blog is of type object<ElggBlog>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
539
540
					 // reset the creation time for posts that move from draft to published
541
					 if ($guid) {
0 ignored issues
show
Bug introduced by
The variable $guid does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
542
						 $blog->time_created = time();
543
						 $blog->save();
544
					 }
545
				 } elseif ($old_status == 'published' && $status == 'draft') {
546
					 elgg_delete_river(array(
547
						 'object_guid' => $blog->guid,
548
						 'action_type' => 'create',
549
					 ));
550
				 }
551
				 if ($blog->status == 'published' || $save == false) {
0 ignored issues
show
Bug introduced by
The variable $save does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
552
					 return ($blog->getURL());
553
				 } else {
554
					 return ("blog/edit/$blog->guid");
555
				 }
556
557
		 } else {
558
			 return elgg_echo('blog:error:cannot_save');
559
		 }
560
	 }
561
}
562