Completed
Push — EditDiscussion ( f93794 )
by
unknown
17:43
created

discussion.php ➔ get_discussion_edit()   C

Complexity

Conditions 11
Paths 48

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 27
nc 48
nop 3
dl 0
loc 46
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 Discussion entities
4
 */
5
6
elgg_ws_expose_function(
7
	"get.discussion",
8
	"get_discussion",
9
	array(
10
		"user" => array('type' => 'string', 'required' => true),
11
		"guid" => array('type' => 'int', 'required' => true),
12
		"thread" => array('type' => 'int', 'required' => false, 'default' => 1),
13
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
14
	),
15
	'Retrieves a discussion & all replies based on user id and discussion id',
16
	'POST',
17
	true,
18
	false
19
);
20
21
elgg_ws_expose_function(
22
	"get.discussions",
23
	"get_discussions",
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 discussions based on user id',
32
	'POST',
33
	true,
34
	false
35
);
36
37
elgg_ws_expose_function(
38
	"get.discussionedit",
39
	"get_discussion_edit",
40
	array(
41
		"user" => array('type' => 'string', 'required' => true),
42
		"guid" => array('type' => 'int', 'required' => true),
43
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
44
	),
45
	'Retrieves a discussion based on guid, returns only information required for edit',
46
	'POST',
47
	true,
48
	false
49
);
50
51
elgg_ws_expose_function(
52
 "post.discussion",
53
 "post_discussion",
54
 array(
55
	"user" => array('type' => 'string', 'required' => true),
56
	"title" => array('type' => 'string', 'required' => true),
57
	"message" => array('type' =>'string', 'required' => true),
58
	"container_guid" => array('type' =>'string', 'required' => false, 'default' => ''),
59
	"access" => array('type' =>'int', 'required' => false, 'default' => 2),
60
	"open" => array('type' =>'int', 'required' => false, 'default' => 1),
61
	"topic_guid" => array('type' =>'int', 'required' => false, 'default' => 0),
62
	"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
63
 ),
64
 'Posts/Saves a new discussion topic',
65
 'POST',
66
 true,
67
 false
68
);
69
70
function get_discussion($user, $guid, $thread, $lang)
71
{
72
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
73
	if (!$user_entity) {
74
		return "User was not found. Please try a different GUID, username, or email address";
75
	}
76
	if (!$user_entity instanceof ElggUser) {
77
		return "Invalid user. Please try a different GUID, username, or email address";
78
	}
79
80
	if (!elgg_is_logged_in()) {
81
		login($user_entity);
82
	}
83
84
	$entity = get_entity($guid);
85
	if (!$entity) {
86
		return "Discussion was not found. Please try a different GUID";
87
	}
88
	if (!elgg_instanceof($entity, "object", "groupforumtopic")) {
89
		return "Invalid discussion. Please try a different GUID";
90
	}
91
92
93
94
	$discussions = elgg_list_entities(array(
95
		'type' => 'object',
96
		'subtype' => 'groupforumtopic',
97
		'guid' => $guid
98
	));
99
	$discussion = json_decode($discussions)[0];
100
101
	$discussion->title = gc_explode_translation($discussion->title, $lang);
102
103
	$likes = elgg_get_annotations(array(
104
		'guid' => $discussion->guid,
105
		'annotation_name' => 'likes'
106
	));
107
	$discussion->likes = count($likes);
108
109
	$liked = elgg_get_annotations(array(
110
		'guid' => $discussion->guid,
111
		'annotation_owner_guid' => $user_entity->guid,
112
		'annotation_name' => 'likes'
113
	));
114
	$discussion->liked = count($liked) > 0;
115
116
	$discussion->userDetails = get_user_block($discussion->owner_guid, $lang);
117
	$discussion->description = gc_explode_translation($discussion->description, $lang);
118
119
	$discussionsArray = array();
120
	$discussionsArray[] = $discussion;
121
122
	if ($thread) {
123
		$all_replies = elgg_list_entities_from_metadata(array(
124
			'type' => 'object',
125
			'subtype' => 'discussion_reply',
126
			'container_guid' => $guid
127
		));
128
		$replies = json_decode($all_replies);
129
		$replies = array_reverse($replies);
130
131
		foreach ($replies as $reply) {
132
			$discussionsArray[] = $reply;
133
		}
134
	}
135
136
	return $discussionsArray;
137
}
138
139
function get_discussions($user, $limit, $offset, $filters, $lang)
140
{
141
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
142
	if (!$user_entity) {
143
		return "User was not found. Please try a different GUID, username, or email address";
144
	}
145
	if (!$user_entity instanceof ElggUser) {
146
		return "Invalid user. Please try a different GUID, username, or email address";
147
	}
148
149
	if (!elgg_is_logged_in()) {
150
		login($user_entity);
151
	}
152
153
	$filter_data = json_decode($filters);
154
	if (!empty($filter_data)) {
155
		$params = array(
156
			'type' => 'object',
157
			'subtype' => 'groupforumtopic',
158
			'limit' => $limit,
159
			'offset' => $offset
160
		);
161
162
		if ($filter_data->mine) {
163
			$params['owner_guid'] = $user_entity->guid;
164
		}
165
166 View Code Duplication
		if ($filter_data->name) {
167
			$db_prefix = elgg_get_config('dbprefix');
168
			$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
169
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
170
		}
171
172
		if ($filter_data->mine) {
173
			$all_discussions = elgg_list_entities_from_relationship($params);
174
		} else {
175
			$all_discussions = elgg_list_entities_from_metadata($params);
176
		}
177
	} else {
178
		$all_discussions = elgg_list_entities(array(
179
			'type' => 'object',
180
			'subtype' => 'groupforumtopic',
181
			'limit' => $limit,
182
			'offset' => $offset
183
		));
184
	}
185
186
	$discussions = json_decode($all_discussions);
187
188
	foreach ($discussions as $discussion) {
189
		$discussion->name = gc_explode_translation($discussion->name, $lang);
190
191
		$likes = elgg_get_annotations(array(
192
			'guid' => $discussion->guid,
193
			'annotation_name' => 'likes'
194
		));
195
		$discussion->likes = count($likes);
196
197
		$liked = elgg_get_annotations(array(
198
			'guid' => $discussion->guid,
199
			'annotation_owner_guid' => $user_entity->guid,
200
			'annotation_name' => 'likes'
201
		));
202
		$discussion->liked = count($liked) > 0;
203
204
		$discussion->userDetails = get_user_block($discussion->owner_guid, $lang);
205
		$discussion->description = gc_explode_translation($discussion->description, $lang);
206
	}
207
208
	return $discussions;
209
}
210
211
function get_discussion_edit($user, $guid, $lang)
212
{
213
 $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
214
 if (!$user_entity) {
215
	 return "User was not found. Please try a different GUID, username, or email address";
216
 }
217
 if (!$user_entity instanceof ElggUser) {
218
	 return "Invalid user. Please try a different GUID, username, or email address";
219
 }
220
221
 if (!elgg_is_logged_in()) {
222
	 login($user_entity);
223
 }
224
225
 $entity = get_entity($guid);
226
 if (!$entity) {
227
	 return "Discussion was not found. Please try a different GUID";
228
 }
229
 if (!elgg_instanceof($entity, "object", "groupforumtopic")) {
230
	 return "Invalid discussion. Please try a different GUID";
231
 }
232
233
234
235
 $discussions = elgg_list_entities(array(
236
	 'type' => 'object',
237
	 'subtype' => 'groupforumtopic',
238
	 'guid' => $guid
239
 ));
240
 $discussion = json_decode($discussions)[0];
241
 if ($discussion->owner_guid != $user_entity->getGUID()){
242
	 return elgg_echo('discussion:error:permissions');
243
 }
244
245
 $discussion->title = json_decode($discussion->title);
246
 $discussion->description = json_decode($discussion->description);
247
 //access id?
248
 //status?
249
 $container = get_entity($discussion->container_guid);
250
 $discussion->group->public = $container->isPublicMembership();
251
 if (!$discussion->group->public && !$container->isMember($user_entity)){
252
	 return elgg_echo('discussion:error:permissions');
253
 }
254
255
 return $discussion;
256
}
257
258
function post_discussion($user, $title, $message, $container_guid, $access, $open, $topic_guid, $lang)
259
{
260
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
261
		if (!$user_entity) {
262
			return "User was not found. Please try a different GUID, username, or email address";
263
		}
264
		if (!$user_entity instanceof ElggUser) {
265
			return "Invalid user. Please try a different GUID, username, or email address";
266
		}
267
		if (!elgg_is_logged_in()) {
268
			login($user_entity);
269
		}
270
271
		//check required fields
272
		$titles = json_decode($title);
273
		$message = json_decode($message);
274
		if (!$titles->en && !$titles->fr) { return elgg_echo("discussion:error:missing"); }
275
		if (!$message->en && !$message->fr) { return elgg_echo("discussion:error:missing");  }
276 View Code Duplication
		if (!($titles->en && $message->en) && !($titles->fr && $message->fr)) { return "require-same-lang"; }
277
278
		$container = get_entity($container_guid);
279
		if (!$container || !$container->canWriteToContainer(0, 'object', 'groupforumtopic')) {
280
			return elgg_echo('discussion:error:permissions');
281
		}
282
283
		//Check if new topic or edit
284
		$new_topic = true;
285
		if ($topic_guid > 0) {
286
		 $new_topic = false;
287
		}
288
289
		if ($new_topic) {
290
			$topic = new ElggObject();
291
			$topic->subtype = 'groupforumtopic';
292
		} else {
293
			$topic = get_entity($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...
294
			if (!elgg_instanceof($topic, 'object', 'groupforumtopic') || !$topic->canEdit()) {
295
				return elgg_echo('discussion:topic:notfound');
296
			}
297
		}
298
299
		//french english setup
300
		$title1 = htmlspecialchars($titles->en, ENT_QUOTES, 'UTF-8');
301
		$title2 = htmlspecialchars($titles->fr, ENT_QUOTES, 'UTF-8');
302
		$title =  gc_implode_translation($title1, $title2);
303
		$desc = gc_implode_translation($message->en, $message->fr);
304
305
		if ($access == 1 && !$container->isPublicMembership()){
306
			$access = 2; //Access cannot be public if group is not public. Default to group only.
307
		}
308
		$access_id = $access;
309
		if ($access_id === 2){
310
			$access_id = $container->group_acl; //Sets access id to match group only id.
311
		}
312
313
314
		$topic->title = $title;
315
		$topic->title2 = $title2;
316
		$topic->description = $desc;
317
		$topic->description2 = $message->fr;
318
		$topic->status = ($open == 1) ? "open" : "closed";
319
		$topic->access_id = $access_id;
320
		$topic->container_guid = $container_guid;
321
322
		$result = $topic->save();
323
		if (!$result) {
324
			return elgg_echo('discussion:error:notsaved');
325
		}
326
327
		//handle results differently for new topics and topic edits
328 View Code Duplication
		if ($new_topic) {
329
			system_message(elgg_echo('discussion:topic:created'));
330
			elgg_create_river_item(array(
331
				'view' => 'river/object/groupforumtopic/create',
332
				'action_type' => 'create',
333
				'subject_guid' => elgg_get_logged_in_user_guid(),
334
				'object_guid' => $topic->guid,
335
			));
336
		} else {
337
			system_message(elgg_echo('discussion:topic:updated'));
338
		}
339
340
		return $topic->getURL();
341
}
342