Completed
Pull Request — master (#1883)
by
unknown
21:32
created

discussion.php ➔ get_discussion()   B

Complexity

Conditions 9
Paths 40

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 40
nop 4
dl 0
loc 59
rs 7.3389
c 0
b 0
f 0

How to fix   Long Method   

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
	if ($thread) {
120
		$discussion->comment_count = elgg_get_entities(array(
121
			'container_guid' => $guid,
122
			'count' => true,
123
			'distinct' => false,
124
		));
125
	}
126
127
	return $discussion;
128
}
129
130
function get_discussions($user, $limit, $offset, $filters, $lang)
131
{
132
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
133
	if (!$user_entity) {
134
		return "User was not found. Please try a different GUID, username, or email address";
135
	}
136
	if (!$user_entity instanceof ElggUser) {
137
		return "Invalid user. Please try a different GUID, username, or email address";
138
	}
139
140
	if (!elgg_is_logged_in()) {
141
		login($user_entity);
142
	}
143
144
	$filter_data = json_decode($filters);
145 View Code Duplication
	if (!empty($filter_data)) {
146
		$params = array(
147
			'type' => 'object',
148
			'subtype' => 'groupforumtopic',
149
			'limit' => $limit,
150
			'offset' => $offset
151
		);
152
153
		if ($filter_data->mine) {
154
			$params['owner_guid'] = $user_entity->guid;
155
		}
156
157
		if ($filter_data->name) {
158
			$db_prefix = elgg_get_config('dbprefix');
159
			$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
160
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
161
		}
162
163
		if ($filter_data->mine) {
164
			$all_discussions = elgg_list_entities_from_relationship($params);
165
		} else {
166
			$all_discussions = elgg_list_entities_from_metadata($params);
167
		}
168
	} else {
169
		$all_discussions = elgg_list_entities(array(
170
			'type' => 'object',
171
			'subtype' => 'groupforumtopic',
172
			'limit' => $limit,
173
			'offset' => $offset
174
		));
175
	}
176
177
	$discussions = json_decode($all_discussions);
178
179
	foreach ($discussions as $discussion) {
180
		$discussion->name = gc_explode_translation($discussion->name, $lang);
181
182
		$likes = elgg_get_annotations(array(
183
			'guid' => $discussion->guid,
184
			'annotation_name' => 'likes'
185
		));
186
		$discussion->likes = count($likes);
187
188
		$liked = elgg_get_annotations(array(
189
			'guid' => $discussion->guid,
190
			'annotation_owner_guid' => $user_entity->guid,
191
			'annotation_name' => 'likes'
192
		));
193
		$discussion->liked = count($liked) > 0;
194
195
		$discussion->userDetails = get_user_block($discussion->owner_guid, $lang);
196
		$discussion->description = gc_explode_translation($discussion->description, $lang);
197
	}
198
199
	return $discussions;
200
}
201
202
function get_discussion_edit($user, $guid, $lang)
203
{
204
 $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
205
 if (!$user_entity) {
206
	 return "User was not found. Please try a different GUID, username, or email address";
207
 }
208
 if (!$user_entity instanceof ElggUser) {
209
	 return "Invalid user. Please try a different GUID, username, or email address";
210
 }
211
212
 if (!elgg_is_logged_in()) {
213
	 login($user_entity);
214
 }
215
216
 $entity = get_entity($guid);
217
 if (!$entity) {
218
	 return "Discussion was not found. Please try a different GUID";
219
 }
220
 if (!elgg_instanceof($entity, "object", "groupforumtopic")) {
221
	 return "Invalid discussion. Please try a different GUID";
222
 }
223
224
225
226
 $discussions = elgg_list_entities(array(
227
	 'type' => 'object',
228
	 'subtype' => 'groupforumtopic',
229
	 'guid' => $guid
230
 ));
231
 $discussion = json_decode($discussions)[0];
232
 if ($discussion->owner_guid != $user_entity->getGUID()){
233
	 return elgg_echo('discussion:error:permissions');
234
 }
235
236
 $discussion->title = json_decode($discussion->title);
237
 $discussion->description = json_decode($discussion->description);
238
 $container = get_entity($discussion->container_guid);
239
 $discussion->group->public = $container->isPublicMembership();
240
 if (!$discussion->group->public && !$container->isMember($user_entity)){
241
	 return elgg_echo('discussion:error:permissions');
242
 }
243
244
 return $discussion;
245
}
246
247
function post_discussion($user, $title, $message, $container_guid, $access, $open, $topic_guid, $lang)
248
{
249
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
250
		if (!$user_entity) {
251
			return "User was not found. Please try a different GUID, username, or email address";
252
		}
253
		if (!$user_entity instanceof ElggUser) {
254
			return "Invalid user. Please try a different GUID, username, or email address";
255
		}
256
		if (!elgg_is_logged_in()) {
257
			login($user_entity);
258
		}
259
260
		//check required fields
261
		$titles = json_decode($title);
262
		$message = json_decode($message);
263
		if (!$titles->en && !$titles->fr) { return elgg_echo("discussion:error:missing"); }
264
		if (!$message->en && !$message->fr) { return elgg_echo("discussion:error:missing");  }
265 View Code Duplication
		if (!($titles->en && $message->en) && !($titles->fr && $message->fr)) { return "require-same-lang"; }
266
267
		$container = get_entity($container_guid);
268
		if (!$container || !$container->canWriteToContainer(0, 'object', 'groupforumtopic')) {
269
			return elgg_echo('discussion:error:permissions');
270
		}
271
272
		//Check if new topic or edit
273
		$new_topic = true;
274
		if ($topic_guid > 0) {
275
		 $new_topic = false;
276
		}
277
278
		if ($new_topic) {
279
			$topic = new ElggObject();
280
			$topic->subtype = 'groupforumtopic';
281
		} else {
282
			$topic = get_entity($topic_guid);
283
			if (!elgg_instanceof($topic, 'object', 'groupforumtopic') || !$topic->canEdit()) {
284
				return elgg_echo('discussion:topic:notfound');
285
			}
286
		}
287
288
		//french english setup
289
		$title1 = htmlspecialchars($titles->en, ENT_QUOTES, 'UTF-8');
290
		$title2 = htmlspecialchars($titles->fr, ENT_QUOTES, 'UTF-8');
291
		$title =  gc_implode_translation($title1, $title2);
292
		$desc = gc_implode_translation($message->en, $message->fr);
293
294
		if ($access == 1 && !$container->isPublicMembership()){
295
			$access = 2; //Access cannot be public if group is not public. Default to group only.
296
		}
297
		$access_id = $access;
298
		if ($access_id === 2){
299
			$access_id = $container->group_acl; //Sets access id to match group only id.
300
		}
301
302
303
		$topic->title = $title;
304
		$topic->title2 = $title2;
305
		$topic->description = $desc;
306
		$topic->description2 = $message->fr;
307
		$topic->status = ($open == 1) ? "open" : "closed";
308
		$topic->access_id = $access_id;
309
		$topic->container_guid = $container_guid;
310
311
		$result = $topic->save();
312
		if (!$result) {
313
			return elgg_echo('discussion:error:notsaved');
314
		}
315
316
		//handle results differently for new topics and topic edits
317 View Code Duplication
		if ($new_topic) {
318
			system_message(elgg_echo('discussion:topic:created'));
319
			elgg_create_river_item(array(
320
				'view' => 'river/object/groupforumtopic/create',
321
				'action_type' => 'create',
322
				'subject_guid' => elgg_get_logged_in_user_guid(),
323
				'object_guid' => $topic->guid,
324
			));
325
		} else {
326
			system_message(elgg_echo('discussion:topic:updated'));
327
		}
328
329
		return $topic->getURL();
330
}
331