Completed
Push — Mobile-Group-Blogs ( 812477 )
by
unknown
29:34
created

blog.php ➔ foreach_blogs()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 20

Duplication

Lines 28
Ratio 87.5 %

Importance

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