/
ElggFinder.php
463 lines (416 loc) · 8.66 KB
/
ElggFinder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<?php
/**
* Elgg helper to find entities in the database
*
* Usage :
* $entities = Finder('album')->
* createdBetween('4 days ago')->and('yesterday')->
* ownedByMe()->
* find(10);
*/
class ElggFinder
{
protected
$params = array(
// 'type' => null,
// 'subtype' => null,
// 'offset' => null,
// 'limit' => null,
// 'order_by' => null,
// 'user_guid' => null,
'joins' => array(),
),
$and = null,
$entities = null;
/**
*
* @param string $subtype
* @param string $type
*/
public function __construct($subtype = null, $type = null)
{
$this->params['subtype'] = $subtype;
$this->params['type'] = is_null($type) ? 'object' : $type;
}
// DATE & TIME
/**
*
* @param string $date
* @return ElggFinder
*/
public function createdBefore($date)
{
$this->params['created_time_upper'] = strtotime($date);
return $this;
}
/**
*
* @param string $date
* @return ElggFinder
*/
public function modifiedBefore($date)
{
$this->params['modified_time_upper'] = strtotime($date);
return $this;
}
/**
*
* @param string $date
* @return ElggFinder
*/
public function before($date)
{
return $this->createdBefore($date);
}
/**
*
* @param string $date
* @return ElggFinder
*/
public function createdAfter($date)
{
$this->params['created_time_lower'] = strtotime($date);
return $this;
}
/**
*
* @param string $date
* @return ElggFinder
*/
public function modifiedAfter($date)
{
$this->params['modified_time_lower'] = strtotime($date);
return $this;
}
/**
*
* @param string $date
* @return ElggFinder
*/
public function after($date)
{
return $this->createdAfter($date);
}
/**
*
* @param string $date
* @return ElggFinder
*/
public function createdBetween($date)
{
$this->params['created_time_lower'] = strtotime($date);
$this->and = 'createdBefore';
return $this;
}
/**
*
* @param string $date
* @return ElggFinder
*/
public function modifiedBetween($date)
{
$this->params['modified_time_lower'] = strtotime($date);
$this->and = 'modifiedBefore';
return $this;
}
/**
*
* @param string $date
* @return ElggFinder
*/
public function between($date)
{
$this->and = 'createdBefore';
return $this->createdBetween($date);
}
// OWNER
/**
*
* @param mixed Owner(s)
* @return ElggFinder
*/
public function ownedBy()
{
$this->owner_guids = '';
foreach (func_get_args() as $arg)
{
$this->owner_guids .= ', '.$this->getUserGUID($arg);
}
$this->owner_guids = substr($this->owner_guids, 2);
return $this;
}
/**
*
* @return ElggFinder
*/
public function ownedByMe()
{
$this->owner_guids = $this->getUserGUID();
return $this;
}
/**
*
* @return ElggFinder
*/
public function ownedByUser($user)
{
$this->owner_guids = $this->getUserGUID($user);
return $this;
}
/**
*
* @param mixed $user Owner
* @return ElggFinder
*/
public function ownedByFriendsOf($user)
{
// $friends = Finder('user')->
$this->owner_guids = $this->getUserGUID($user);
return $this;
}
// WHERE
/**
*
* @param string $relationship
* @param mixed $target guid_2 or array of guid_2
* @return ElggFinder
*/
public function rel($relationship, $target = null)
{
$this->joins[] = 'JOIN entity_relationships rel ON rel.guid_one = entities.guid';
$this->where[] = 'rel.relationship = "'.$relationship.'"';
if (isset($target))
{
if (is_array($target))
{
$this->wheres[] = 'rel.guid_two IN('.implode(', ', $target).')';
}
else
{
$this->wheres[] = 'rel.guid_two = '.$target;
}
}
return $this;
}
// FIND
/**
* Find entities in the database
*
* Params :
* - types => NULL|STR entity type (SQL: type IN ('type1', 'type2') Joined with subtypes by AND...see below)
* - subtypes => NULL|STR entity subtype (SQL: subtype IN ('subtype1', 'subtype2))
* - type_subtype_pairs => NULL|ARR (array('type' => 'subtype')) (SQL: type = '$type' AND subtype = '$subtype') pairs
* - owner_guids => NULL|INT entity guid
* - container_guids => NULL|INT container_guid
* - site_guids => NULL (current_site)|INT site_guid
* - order_by => NULL (time_created desc)|STR SQL order by clause
* - limit => NULL (10)|INT SQL limit clause
* - offset => NULL (0)|INT SQL offset clause
* - created_time_lower => NULL|INT Created time lower boundary in epoch time
* - created_time_upper => NULL|INT Created time upper boundary in epoch time
* - modified_time_lower => NULL|INT Modified time lower boundary in epoch time
* - modified_time_upper => NULL|INT Modified time upper boundary in epoch time
* - count => TRUE|FALSE return a count instead of entities
* - wheres => array() Additional where clauses to AND together
* - joins => array() Additional joins
*
* @param int $limit
* @param int $offset
* @return Array of ElggEntity
*/
public function find($limit = null, $offset = null)
{
if ($limit)
{
$this->params['limit'] = $limit;
}
if ($offset)
{
$this->params['offset'] = $limit;
}
return elgg_get_entities($this->params);
}
/**
*
* @return Array of ElggEntity
*/
public function findAll()
{
return $this->find();
}
/**
*
* @return ElggEntity
*/
public function findOne()
{
return $this->find(1);
}
/**
*
* @return ElggEntity
*/
public function findByGUID($guid)
{
return get_entity($guid);
}
// ALTERATION (CREATE, UPDATE & DELETE)
/**
*
* @param int $limit
* @param int $offset
* @return int
*/
public function delete($limit = null, $offset = null)
{
$entities = $this->find($limit, $offset);
$count = 0;
foreach ($entities as $entity)
{
delete_entity($entity->guid);
$count++;
}
return $count;
}
/**
* Disable entities but don't delete them
*
* @param string $reason Reason of deletion
* @param int $limit
* @param int $offset
* @return int
*/
public function disable($reason = '', $limit = null, $offset = null)
{
$entities = $this->find($limit, $offset);
$count = 0;
foreach ($entities as $entity)
{
disable_entity($entity->guid, $reason);
$count++;
}
return $count;
}
/**
* Enable entities
*
* @param int $limit
* @param int $offset
* @return int
*/
public function enable($limit = null, $offset = null)
{
$entities = $this->find($limit, $offset);
$count = 0;
foreach ($entities as $entity)
{
enable_entity($entity->guid);
$count++;
}
return $count;
}
/**
*
* @param string $attribute
* @param mixed $value
* @return ElggFinder
*/
public function set($attribute, $value)
{
if (is_null($this->entities))
{
$this->find();
}
foreach ($this->entities as $entity)
{
$entity->$attribute = $value;
}
return $this;
}
/**
*
* @return int
*/
public function save()
{
$count = 0;
foreach ($this->entities as $entity)
{
$entity->save();
$count++;
}
return $count;
}
// UNLEASH THE MAGIC
/**
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments)
{
switch(true)
{
// set<Attribute>(value)
case substr($name, 0, 3) == 'set':
return call_user_method_array(substr($name, 3), $this, $arguments);
break;
}
}
/**
*
* @return ElggFinder
*/
public function _and()
{
if (!is_null($this->and))
{
call_user_method_array($this->and, $this, func_get_args());
}
$this->and = null;
return $this;
}
/**
* Retrieve user GUID from various data (user GUID, user object, online user)
* @param mixed $user User
* @return int
*/
protected function getUserGUID($user = null)
{
if (is_null($user))
{
return get_loggedin_userid();
}
if (is_string($user))
{
return get_user_by_username($user)->guid;
}
if (is_int($user))
{
return $user;
}
if (is_object($user))
{
return $user->guid;
}
return null;
}
}
/**
*
* @param string $subtype
* @return ElggFinder
*/
function Finder($subtype = null)
{
if ($subtype && class_exists($finderClass = $subtype.'Finder'))
{
return new $finderClass();
}
elseif ($subtype)
{
return new ElggFinder($subtype);
}
return new ElggFinder();
}