Passed
Push — PostBlog-Mobile-API ( 8c5294 )
by
unknown
32:34 queued 14:15
created

blog.php ➔ post_blog()   F

Complexity

Conditions 53
Paths > 20000

Size

Total Lines 150
Code Lines 97

Duplication

Lines 28
Ratio 18.67 %

Importance

Changes 0
Metric Value
cc 53
eloc 97
nc 4718624
nop 9
dl 28
loc 150
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
 "post.blog",
54
 "post_blog",
55
 array(
56
	 "user" => array('type' => 'string', 'required' => true),
57
	 "title" => array('type' => 'string', 'required' => true),
58
	 "excerpt" => array('type' =>'string', 'required' => false, 'default' => ''),
59
	 "body" => array('type' =>'string', 'required' => true),
60
	 "container_guid" => array('type' =>'string', 'required' => false, 'default' => ''),
61
	 "comments" => array('type' =>'int', 'required' => false, 'default' => 1),
62
	 "access" => array('type' =>'int', 'required' => false, 'default' => 1),
63
	 "status" => array('type' =>'int', 'required' => false, 'default' => 0),
64
	 "lang" => array('type' => 'string', 'required' => false, 'default' => "en")
65
 ),
66
 'Posts/Saves a new blog post',
67
 'POST',
68
 true,
69
 false
70
);
71
72
function get_blogpost($user, $guid, $lang)
73
{
74
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
75
	if (!$user_entity) {
76
		return "User was not found. Please try a different GUID, username, or email address";
77
	}
78
	if (!$user_entity instanceof ElggUser) {
79
		return "Invalid user. Please try a different GUID, username, or email address";
80
	}
81
82
	$entity = get_entity($guid);
83
	if (!isset($entity)) {
84
		return "Blog was not found. Please try a different GUID";
85
	}
86
87
	if (!elgg_is_logged_in()) {
88
		login($user_entity);
89
	}
90
91
	$blog_posts = elgg_list_entities(array(
92
		'type' => 'object',
93
		'subtype' => 'blog',
94
		'guid' => $guid
95
	));
96
	$blog_post = json_decode($blog_posts)[0];
97
98
	$blog_post->title = gc_explode_translation($blog_post->title, $lang);
99
	$blog_post->description = gc_explode_translation($blog_post->description, $lang);
100
101
	$likes = elgg_get_annotations(array(
102
		'guid' => $blog_post->guid,
103
		'annotation_name' => 'likes'
104
	));
105
	$blog_post->likes = count($likes);
106
107
	$liked = elgg_get_annotations(array(
108
		'guid' => $blog_post->guid,
109
		'annotation_owner_guid' => $user_entity->guid,
110
		'annotation_name' => 'likes'
111
	));
112
	$blog_post->liked = count($liked) > 0;
113
114
	$blog_post->comments = get_entity_comments($blog_post->guid);
115
116
	$blog_post->userDetails = get_user_block($blog_post->owner_guid, $lang);
117
118
	$group = get_entity($blog_post->container_guid);
119
	$blog_post->group = gc_explode_translation($group->name, $lang);
120
121
	if (is_callable(array($group, 'getURL'))) {
122
		$blog_post->groupURL = $group->getURL();
123
	}
124
125
	return $blog_post;
126
}
127
128
function get_blogposts($user, $limit, $offset, $filters, $lang)
129
{
130
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
131
	if (!$user_entity) {
132
		return "User was not found. Please try a different GUID, username, or email address";
133
	}
134
	if (!$user_entity instanceof ElggUser) {
135
		return "Invalid user. Please try a different GUID, username, or email address";
136
	}
137
138
	if (!elgg_is_logged_in()) {
139
		login($user_entity);
140
	}
141
142
	$filter_data = json_decode($filters);
143
	if (!empty($filter_data)) {
144
		$params = array(
145
			'type' => 'object',
146
			'subtype' => 'blog',
147
			'limit' => $limit,
148
			'offset' => $offset
149
		);
150
151 View Code Duplication
		if ($filter_data->name) {
152
			$db_prefix = elgg_get_config('dbprefix');
153
			$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
154
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
155
		}
156
157
		$all_blog_posts = elgg_list_entities_from_metadata($params);
158
	} else {
159
		$all_blog_posts = elgg_list_entities(array(
160
			'type' => 'object',
161
			'subtype' => 'blog',
162
			'limit' => $limit,
163
			'offset' => $offset
164
		));
165
	}
166
167
	$blog_posts = json_decode($all_blog_posts);
168
169 View Code Duplication
	foreach ($blog_posts as $blog_post) {
170
		$blog_post->title = gc_explode_translation($blog_post->title, $lang);
171
		$blog_post->description = gc_explode_translation($blog_post->description, $lang);
172
173
		$likes = elgg_get_annotations(array(
174
			'guid' => $blog_post->guid,
175
			'annotation_name' => 'likes'
176
		));
177
		$blog_post->likes = count($likes);
178
179
		$liked = elgg_get_annotations(array(
180
			'guid' => $blog_post->guid,
181
			'annotation_owner_guid' => $user_entity->guid,
182
			'annotation_name' => 'likes'
183
		));
184
		$blog_post->liked = count($liked) > 0;
185
186
		$blog_post->comments = get_entity_comments($blog_post->guid);
187
188
		$blog_post->userDetails = get_user_block($blog_post->owner_guid, $lang);
189
190
		$group = get_entity($blog_post->container_guid);
191
		$blog_post->group = gc_explode_translation($group->name, $lang);
192
193
		if (is_callable(array($group, 'getURL'))) {
194
			$blog_post->groupURL = $group->getURL();
195
		}
196
	}
197
198
	return $blog_posts;
199
}
200
201
function get_blogposts_by_owner($user, $limit, $offset, $lang, $target)
202
{
203
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
204
	if (!$user_entity) {
205
		return "User was not found. Please try a different GUID, username, or email address";
206
	}
207
	if (!$user_entity instanceof ElggUser) {
208
		return "Invalid user. Please try a different GUID, username, or email address";
209
	}
210
211
	$target_entity = $user_entity;
212
	if (!empty($target)){
213
		$target_entity =  is_numeric($target) ? get_user($target) : (strpos($target, '@') !== false ? get_user_by_email($target)[0] : get_user_by_username($target));
214
		if (!$target_entity) {
215
			return "Target user was not found. Please try a different GUID, username, or email address";
216
		}
217
		if (!$target_entity instanceof ElggUser) {
218
			return "Invalid target user. Please try a different GUID, username, or email address";
219
		}
220
	}
221
222
	if (!elgg_is_logged_in()) {
223
		login($user_entity);
224
	}
225
226
	$all_blog_posts = elgg_list_entities(array(
227
		'type' => 'object',
228
		'subtype' => 'blog',
229
		'owner_guid' => $target_entity->guid,
230
		'limit' => $limit,
231
		'offset' => $offset
232
	));
233
234
	$blog_posts = json_decode($all_blog_posts);
235
236 View Code Duplication
	foreach ($blog_posts as $blog_post) {
237
		$blog_post->title = gc_explode_translation($blog_post->title, $lang);
238
		$blog_post->description = gc_explode_translation($blog_post->description, $lang);
239
240
		$likes = elgg_get_annotations(array(
241
			'guid' => $blog_post->guid,
242
			'annotation_name' => 'likes'
243
		));
244
		$blog_post->likes = count($likes);
245
246
		$liked = elgg_get_annotations(array(
247
			'guid' => $blog_post->guid,
248
			'annotation_owner_guid' => $user_entity->guid,
249
			'annotation_name' => 'likes'
250
		));
251
		$blog_post->liked = count($liked) > 0;
252
253
		$blog_post->comments = get_entity_comments($blog_post->guid);
254
255
		$blog_post->userDetails = get_user_block($blog_post->owner_guid, $lang);
256
257
		$group = get_entity($blog_post->container_guid);
258
		$blog_post->group = gc_explode_translation($group->name, $lang);
259
260
		if (is_callable(array($group, 'getURL'))) {
261
			$blog_post->groupURL = $group->getURL();
262
		}
263
	}
264
265
	return $blog_posts;
266
}
267
268
function post_blog($user, $title, $excerpt, $body, $container_guid, $comments, $access, $status, $lang)
269
{
270
 $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
271
	 if (!$user_entity) {
272
		 return "User was not found. Please try a different GUID, username, or email address";
273
	 }
274
	 if (!$user_entity instanceof ElggUser) {
275
		 return "Invalid user. Please try a different GUID, username, or email address";
276
	 }
277
	 if (!elgg_is_logged_in()) {
278
		 login($user_entity);
279
	 }
280
	 $error = FALSE;
281
	 //check required fields being not empty
282
	 $titles = json_decode($title);
283
	 $bodies = json_decode($body);
284
	 $excerpts = json_decode($excerpt);
285
	 //Check Required
286
	 if (!$titles->en && !$titles->fr) { return elgg_echo("blog:error:missing:title"); }
287
	 if (!$bodies->en && !$bodies->fr) { return elgg_echo("blog:error:missing:description");  }
288
	 if (!($titles->en && $bodies->en) && !($titles->fr && $bodies->fr)) { return "require-same-lang"; }
289
	 //Default any Missing or faulty
290
	 if (!$titles->en) { $titles->en = ''; }
291
	 if (!$titles->fr) { $titles->fr = ''; }
292
	 if (!$bodies->en) { $bodies->en = ''; }
293
	 if (!$bodies->fr) { $bodies->fr = ''; }
294
	 if (!$excerpts->en) { $excerpts->en = ''; }
295
	 if (!$excerpts->fr) { $excerpts->fr = ''; }
296
	 if ($comments != 0 && $comments != 1) { $comments = 1; }
297
	 if ($access != 0 && $access != 1 && $access != -2 ) { $access = 1; }
298
	 if ($status != 0 && $status != 1) { $status = 0; }
299
300
	 //If no group container, use user guid.
301
	 if ($container_guid==''){ $container_guid = $user_entity->guid; }
302
303
	 //Set int variables to correct
304
	 if ($status == 1) { $status = 'published'; } else { $status = 'draft'; }
305
	 if ($comments == 1) { $comments = 'On'; } else { $comments = 'Off'; }
306
	 if ($access == 1 ) { $access = ACCESS_PUBLIC; }
307
	 if ($status == 'draft') { $access = ACCESS_PRIVATE; }
308
	 $titles->en = htmlspecialchars($titles->en, ENT_QUOTES, 'UTF-8');
309
	 $titles->fr = htmlspecialchars($titles->fr, ENT_QUOTES, 'UTF-8');
310
	 $excerpts->en = elgg_get_excerpt($excerpts->en);
311
	 $excerpts->fr = elgg_get_excerpt($excerpts->fr);
312
313
	 $values = array(
314
		 'title' => JSON_encode($titles),
315
		 'title2' => '',
316
		 //'title3' => '',
317
		 'description' => JSON_encode($bodies),
318
		 'description2' => '',
319
		 'description3' => '',
320
		 'status' => $status,
321
		 'access_id' => $access,
322
		 'comments_on' => $comments,
323
		 'excerpt' => JSON_encode($excerpts),
324
		 'excerpt2' => '',
325
		 'excerpt3' => '',
326
		 'tags' => '',
327
		 'publication_date' => '',
328
		 'expiration_date' => '',
329
		 'show_owner' => 'no'
330
	 );
331
332
	 //return $values;
333
	 //return elgg_echo("test: pre blog creation");
334
	 //Create blog
335
	 $blog = new ElggBlog();
336
	 $blog->subtype = 'blog';
337
	 $blog->container_guid = $container_guid;
338
	 $new_post = TRUE;
339
340
	 $old_status = $blog->status;
341
342
	 // assign values to the entity, stopping on error.
343 View Code Duplication
	 if (!$error) {
344
		 foreach ($values as $name => $value) {
345
			 if (($name != 'title2') && ($name != 'description2') &&  ($name != 'excerpt2')){ // remove input 2 in metastring table
346
			 $blog->$name = $value;
347
			 }
348
		 }
349
	 }
350
351
	 if (!$error){
352
		 if ($blog->save()){
353
354
				 $icon_file = get_resized_image_from_uploaded_file("icon", 100, 100);
355
				 $icon_sizes = elgg_get_config("icon_sizes");
356
357 View Code Duplication
				 if (!empty($icon_file) && !empty($icon_sizes)) {
358
					 // create icon
359
					 $prefix = "blogs/" . $blog->getGUID();
360
361
					 $fh = new ElggFile();
362
					 $fh->owner_guid = $blog->getOwnerGUID();
363
364
					 foreach ($icon_sizes as $icon_name => $icon_info) {
365
						 $icon_file = get_resized_image_from_uploaded_file("icon", $icon_info["w"], $icon_info["h"], $icon_info["square"], $icon_info["upscale"]);
366
						 if (!empty($icon_file)) {
367
							 $fh->setFilename($prefix . $icon_name . ".jpg");
368
369
							 if ($fh->open("write")) {
370
								 $fh->write($icon_file);
371
								 $fh->close();
372
							 }
373
						 }
374
					 }
375
376
					 $blog->icontime = time();
377
			 }
378
			 // no longer a brand new post.
379
			 $blog->deleteMetadata('new_post');
380
381
			 $status = $blog->status;
382
				 // add to river if changing status or published, regardless of new post
383
				 // because we remove it for drafts.
384
				 if (($new_post || $old_status == 'draft' ||  $old_status == 'published') && $status == 'published') {
385
					 elgg_create_river_item(array(
386
						 'view' => 'river/object/blog/create',
387
						 'action_type' => 'create',
388
						 'subject_guid' => $blog->owner_guid,
389
						 'object_guid' => $blog->getGUID(),
390
					 ));
391
					 // we only want notifications sent when post published
392
					 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...
393
394
					 // reset the creation time for posts that move from draft to published
395
					 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...
396
						 $blog->time_created = time();
397
						 $blog->save();
398
					 }
399
				 } elseif ($old_status == 'published' && $status == 'draft') {
400
					 elgg_delete_river(array(
401
						 'object_guid' => $blog->guid,
402
						 'action_type' => 'create',
403
					 ));
404
				 }
405
				 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...
406
					 return ($blog->getURL());
407
				 } else {
408
					 return ("blog/edit/$blog->guid");
409
				 }
410
411
		 } else {
412
			 return elgg_echo('blog:error:cannot_save');
413
		 }
414
	 }
415
416
	 return "end, unknown result";
417
}
418