Completed
Pull Request — master (#1887)
by
unknown
26:25 queued 05:07
created

bookmark.php ➔ foreach_bookmark()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 3
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
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
  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
    $bookmark->userDetails = get_user_block($bookmark->owner_guid, $lang); //Should go through and only pass revelant infromation
114
    $bookmark->group_guid = "";
115
		$bookmark->comment_count = elgg_get_entities(array(
116
			'container_guid' => $guid,
117
			'count' => true,
118
			'distinct' => false,
119
		));
120
    if ($bookmark->container_guid != $bookmark->owner_guid){
121
      $bookmark->group_guid = $bookmark->container_guid;
122
      $bookmarkGroup = get_entity($bookmark->group_guid);
123
      $bookmark->group = gc_explode_translation($bookmarkGroup->name, $lang);
124
    }
125
  }
126
  return $bookmarks;
127
}//end get_bookmark
128
129
function foreach_bookmark($bookmarks, $user_entity, $lang)
130
{
131
  foreach ($bookmarks as $bookmark){
132
    $bookmark->title = gc_explode_translation($bookmark->title, $lang);
133
134
    $likes = elgg_get_annotations(array(
135
			'guid' => $bookmark->guid,
136
			'annotation_name' => 'likes'
137
		));
138
		$bookmark->likes = count($likes);
139
		$liked = elgg_get_annotations(array(
140
			'guid' => $bookmark->guid,
141
			'annotation_owner_guid' => $user_entity->guid,
142
			'annotation_name' => 'likes'
143
        ));
144
    $bookmark->liked = count($liked) > 0;
145
146
    $bookmarkObject = get_entity($bookmark->guid);
147
    $bookmark->description = gc_explode_translation($bookmarkObject->description, $lang);
148
    $bookmark->address = $bookmarkObject->address;
149
150
    $bookmark->userDetails = get_user_block($bookmark->owner_guid, $lang); //Should go through and only pass revelant infromation
151
    $bookmark->group_guid = "";
152
    if ($bookmark->container_guid != $bookmark->owner_guid){
153
      $bookmark->group_guid = $bookmark->container_guid;
154
      $bookmarkGroup = get_entity($bookmark->group_guid);
155
      $bookmark->group = gc_explode_translation($bookmarkGroup->name, $lang);
156
    }
157
  }
158
  return $bookmarks;
159
} // end foreach_bookmark
160
161
function get_bookmarks($user, $limit, $offset, $filters, $lang)
162
{
163
    // Check provided USER information.
164
    $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
165
	if (!$user_entity) {
166
		return "User was not found. Please try a different GUID, username, or email address";
167
	}
168
	if (!$user_entity instanceof ElggUser) {
169
		return "Invalid user. Please try a different GUID, username, or email address";
170
  }
171
  if (!elgg_is_logged_in()) {
172
		login($user_entity);
173
  }
174
175
176
  //Check FILTER information would go here
177
  $all_bookmarks = elgg_list_entities(array(
178
    'type' => 'object',
179
    'subtype' => 'bookmarks',
180
    'limit' => $limit,
181
    'offset' => $offset
182
  ));
183
  $bookmarks = json_decode($all_bookmarks);
184
185
  $bookmarks = foreach_bookmark($bookmarks, $user_entity, $lang);
186
187
  return $bookmarks;
188
}
189
190 View Code Duplication
function get_bookmarks_colleague($user, $limit, $offset, $filters, $lang)
191
{
192
  // Check provided USER information.
193
  $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
194
  if (!$user_entity) {
195
    return "User was not found. Please try a different GUID, username, or email address";
196
  }
197
  if (!$user_entity instanceof ElggUser) {
198
    return "Invalid user. Please try a different GUID, username, or email address";
199
  }
200
  if (!elgg_is_logged_in()) {
201
    login($user_entity);
202
  }
203
204
  $all_bookmarks = elgg_list_entities_from_relationship(array(
205
    'type' => 'object',
206
		'subtype' => 'bookmarks',
207
		'relationship' => 'friend',
208
		'relationship_guid' => $user_entity->guid,
209
		'relationship_join_on' => 'container_guid',
210
		'limit' => $limit,
211
		'offset' => $offset
212
  ));
213
  $bookmarks = json_decode($all_bookmarks);
214
215
  $bookmarks = foreach_bookmark($bookmarks, $user_entity, $lang);
216
217
  return $bookmarks;
218
219
}
220
221
function get_bookmarks_by_owner($user, $limit, $offset, $filters, $lang, $target)
222
{
223
  // Check provided USER information.
224
  $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
225
  if (!$user_entity) {
226
    return "User was not found. Please try a different GUID, username, or email address";
227
  }
228
  if (!$user_entity instanceof ElggUser) {
229
    return "Invalid user. Please try a different GUID, username, or email address";
230
  }
231
  if (!elgg_is_logged_in()) {
232
    login($user_entity);
233
  }
234
  $target_entity = $user_entity;
235
  if ($target != ''){
236
    $target_entity = get_entity($target);
237
  }
238
239
  //add conditional for target later
240
  $all_bookmarks = elgg_list_entities(array(
241
    'type' => 'object',
242
    'subtype' => 'bookmarks',
243
    'container_guid' => $target_entity->guid,
244
    'limit' => $limit,
245
    'offset' => $offset
246
  ));
247
  $bookmarks = json_decode($all_bookmarks);
248
249
  $bookmarks = foreach_bookmark($bookmarks, $user_entity, $lang);
250
251
  return $bookmarks;
252
}
253