Completed
Push — Opportunity-API ( f578e8 )
by
unknown
17:44
created

bookmark.php ➔ get_bookmarks()   B

Complexity

Conditions 8
Paths 32

Size

Total Lines 54
Code Lines 35

Duplication

Lines 27
Ratio 50 %

Importance

Changes 0
Metric Value
cc 8
eloc 35
nc 32
nop 5
dl 27
loc 54
rs 7.4119
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 Bookmark entities
4
*/
5
elgg_ws_expose_function(
6
	"get.bookmark",
7
	"get_bookmark",
8
	array(
9
		"user" => array('type' => 'string', 'required' => true),
10
		"guid" => array('type' => 'int', 'required' => true),
11
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
12
	),
13
	'Retrieves an bookmark based on user id and bookmark id',
14
	'POST',
15
	true,
16
	false
17
);
18
19
elgg_ws_expose_function(
20
    "get.bookmarks",
21
    "get_bookmarks",
22
    array(
23
		"user" => array('type' => 'string', 'required' => true),
24
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
25
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
26
		"filters" => array('type' => 'string', 'required' => false, 'default' => ""),
27
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
28
	),
29
	'Retrieves bookmarks on GCcollab',
30
	'POST',
31
	true,
32
	false
33
);
34
35
elgg_ws_expose_function(
36
	"get.bookmarkscolleague",
37
	"get_bookmarks_colleague",
38
	array(
39
		"user" => array('type' => 'string', 'required' => true),
40
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
41
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
42
		"filters" => array('type' => 'string', 'required' => false, 'default' => ""),
43
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
44
	),
45
	'Retrieves bookmarks posted by colleagues of a given user',
46
	'POST',
47
	true,
48
	false
49
);
50
51
elgg_ws_expose_function(
52
  "get.bookmarksbyuser",
53
  "get_bookmarks_by_owner",
54
  array(
55
    "user" => array('type' => 'string', 'required' => true),
56
    "limit" => array('type' => 'int', 'required' => false, 'default' => 10),
57
    "offset" => array('type' => 'int', 'required' => false, 'default' => 0),
58
    "filters" => array('type' => 'string', 'required' => false, 'default' => ""),
59
    "lang" => array('type' => 'string', 'required' => false, 'default' => "en"),
60
    "target" => array('type' => 'string', 'required' => false, 'default' => "")
61
  ),
62
  'Retrieves bookmarks posed by a user',
63
  'POST',
64
  true,
65
  false
66
);
67
68
function get_bookmark($user, $guid, $lang)
69
{
70
  // Check provided USER information.
71
  $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
72
  if (!$user_entity) {
73
    return "User was not found. Please try a different GUID, username, or email address";
74
  }
75
  if (!$user_entity instanceof ElggUser) {
76
    return "Invalid user. Please try a different GUID, username, or email address";
77
  }
78
  if (!elgg_is_logged_in()) {
79
    login($user_entity);
80
  }
81
82
  $entity = get_entity($guid);
83
  if (!$entity) {
84
    return "Event was not found. Please try a different GUID";
85
  }
86
  //check if entity is bookmark? subtype bookmarks
87
88
  $bookmark_ent = elgg_list_entities(array(
89
    'type' => 'object',
90
    'subtype' => 'bookmarks',
91
    'guid' => $guid
92
  ));
93
  $bookmarks = json_decode($bookmark_ent);
94
95 View Code Duplication
  foreach ($bookmarks as $bookmark) {
96
    $bookmark->title = gc_explode_translation($bookmark->title, $lang);
97
98
    $likes = elgg_get_annotations(array(
99
			'guid' => $bookmark->guid,
100
			'annotation_name' => 'likes'
101
		));
102
		$bookmark->likes = count($likes);
103
		$liked = elgg_get_annotations(array(
104
			'guid' => $bookmark->guid,
105
			'annotation_owner_guid' => $user_entity->guid,
106
			'annotation_name' => 'likes'
107
        ));
108
    $bookmark->liked = count($liked) > 0;
109
110
    $bookmarkObject = get_entity($bookmark->guid);
111
    $bookmark->description = gc_explode_translation($bookmarkObject->description, $lang);
112
    $bookmark->address = $bookmarkObject->address;
113
114
    $bookmark->userDetails = get_user_block($bookmark->owner_guid, $lang); //Should go through and only pass revelant infromation
115
    $bookmark->group_guid = "";
116
    if ($bookmark->container_guid != $bookmark->owner_guid){
117
      $bookmark->group_guid = $bookmark->container_guid;
118
      $bookmarkGroup = get_entity($bookmark->group_guid);
119
      $bookmark->group = gc_explode_translation($bookmarkGroup->name, $lang);
120
    }
121
  }
122
  return $bookmarks;
123
}//end get_bookmark
124
125
function get_bookmarks($user, $limit, $offset, $filters, $lang)
126
{
127
    // Check provided USER information.
128
    $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
129
	if (!$user_entity) {
130
		return "User was not found. Please try a different GUID, username, or email address";
131
	}
132
	if (!$user_entity instanceof ElggUser) {
133
		return "Invalid user. Please try a different GUID, username, or email address";
134
  }
135
  if (!elgg_is_logged_in()) {
136
		login($user_entity);
137
  }
138
139
140
  //Check FILTER information would go here
141
  $all_bookmarks = elgg_list_entities(array(
142
    'type' => 'object',
143
    'subtype' => 'bookmarks',
144
    'limit' => $limit,
145
    'offset' => $offset
146
  ));
147
  $bookmarks = json_decode($all_bookmarks);
148
149 View Code Duplication
  foreach ($bookmarks as $bookmark) {
150
    $bookmark->title = gc_explode_translation($bookmark->title, $lang);
151
152
    $likes = elgg_get_annotations(array(
153
			'guid' => $bookmark->guid,
154
			'annotation_name' => 'likes'
155
		));
156
		$bookmark->likes = count($likes);
157
		$liked = elgg_get_annotations(array(
158
			'guid' => $bookmark->guid,
159
			'annotation_owner_guid' => $user_entity->guid,
160
			'annotation_name' => 'likes'
161
        ));
162
    $bookmark->liked = count($liked) > 0;
163
164
    $bookmarkObject = get_entity($bookmark->guid);
165
    $bookmark->description = gc_explode_translation($bookmarkObject->description, $lang);
166
    $bookmark->address = $bookmarkObject->address;
167
168
    $bookmark->userDetails = get_user_block($bookmark->owner_guid, $lang); //Should go through and only pass revelant infromation
169
    $bookmark->group_guid = "";
170
    if ($bookmark->container_guid != $bookmark->owner_guid){
171
      $bookmark->group_guid = $bookmark->container_guid;
172
      $bookmarkGroup = get_entity($bookmark->group_guid);
173
      $bookmark->group = gc_explode_translation($bookmarkGroup->name, $lang);
174
    }
175
  }
176
177
  return $bookmarks;
178
}
179
180
function get_bookmarks_colleague($user, $limit, $offset, $filters, $lang)
181
{
182
  // Check provided USER information.
183
  $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
184
  if (!$user_entity) {
185
    return "User was not found. Please try a different GUID, username, or email address";
186
  }
187
  if (!$user_entity instanceof ElggUser) {
188
    return "Invalid user. Please try a different GUID, username, or email address";
189
  }
190
  if (!elgg_is_logged_in()) {
191
    login($user_entity);
192
  }
193
194
  $all_bookmarks = elgg_list_entities_from_relationship(array(
195
    'type' => 'object',
196
		'subtype' => 'bookmarks',
197
		'relationship' => 'friend',
198
		'relationship_guid' => $user_entity->guid,
199
		'relationship_join_on' => 'container_guid',
200
		'limit' => $limit,
201
		'offset' => $offset
202
  ));
203
  $bookmarks = json_decode($all_bookmarks);
204
205 View Code Duplication
  foreach ($bookmarks as $bookmark) {
206
    $bookmark->title = gc_explode_translation($bookmark->title, $lang);
207
208
    $likes = elgg_get_annotations(array(
209
			'guid' => $bookmark->guid,
210
			'annotation_name' => 'likes'
211
		));
212
		$bookmark->likes = count($likes);
213
		$liked = elgg_get_annotations(array(
214
			'guid' => $bookmark->guid,
215
			'annotation_owner_guid' => $user_entity->guid,
216
			'annotation_name' => 'likes'
217
        ));
218
    $bookmark->liked = count($liked) > 0;
219
220
    $bookmarkObject = get_entity($bookmark->guid);
221
    $bookmark->description = gc_explode_translation($bookmarkObject->description, $lang);
222
    $bookmark->address = $bookmarkObject->address;
223
224
    $bookmark->userDetails = get_user_block($bookmark->owner_guid, $lang); //Should go through and only pass revelant infromation
225
    $bookmark->group_guid = "";
226
    if ($bookmark->container_guid != $bookmark->owner_guid){
227
      $bookmark->group_guid = $bookmark->container_guid;
228
      $bookmarkGroup = get_entity($bookmark->group_guid);
229
      $bookmark->group = gc_explode_translation($bookmarkGroup->name, $lang);
230
    }
231
  }
232
233
  return $bookmarks;
234
235
}
236
237
function get_bookmarks_by_owner($user, $limit, $offset, $filters, $lang, $target)
238
{
239
  // Check provided USER information.
240
  $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
241
  if (!$user_entity) {
242
    return "User was not found. Please try a different GUID, username, or email address";
243
  }
244
  if (!$user_entity instanceof ElggUser) {
245
    return "Invalid user. Please try a different GUID, username, or email address";
246
  }
247
  if (!elgg_is_logged_in()) {
248
    login($user_entity);
249
  }
250
  $target_entity = $user_entity;
251
  if ($target != ''){
252
    $target_entity = is_numeric($target) ? get_user($target) : (strpos($target, '@') !== false ? get_user_by_email($target)[0] : get_user_by_username($target));
253
  }
254
255
  //add conditional for target later
256
  $all_bookmarks = elgg_list_entities(array(
257
    'type' => 'object',
258
    'subtype' => 'bookmarks',
259
    'container_guid' => $target_entity->guid,
260
    'limit' => $limit,
261
    'offset' => $offset
262
  ));
263
  $bookmarks = json_decode($all_bookmarks);
264
265 View Code Duplication
  foreach ($bookmarks as $bookmark) {
266
    $bookmark->title = gc_explode_translation($bookmark->title, $lang);
267
268
    $likes = elgg_get_annotations(array(
269
			'guid' => $bookmark->guid,
270
			'annotation_name' => 'likes'
271
		));
272
		$bookmark->likes = count($likes);
273
		$liked = elgg_get_annotations(array(
274
			'guid' => $bookmark->guid,
275
			'annotation_owner_guid' => $user_entity->guid,
276
			'annotation_name' => 'likes'
277
        ));
278
    $bookmark->liked = count($liked) > 0;
279
280
    $bookmarkObject = get_entity($bookmark->guid);
281
    $bookmark->description = gc_explode_translation($bookmarkObject->description, $lang);
282
    $bookmark->address = $bookmarkObject->address;
283
284
    $bookmark->userDetails = get_user_block($bookmark->owner_guid, $lang); //Should go through and only pass revelant infromation
285
    $bookmark->group_guid = "";
286
    if ($bookmark->container_guid != $bookmark->owner_guid){
287
      $bookmark->group_guid = $bookmark->container_guid;
288
      $bookmarkGroup = get_entity($bookmark->group_guid);
289
      $bookmark->group = gc_explode_translation($bookmarkGroup->name, $lang);
290
    }
291
  }
292
293
  return $bookmarks;
294
}
295