Elgg  Version 3.0
river.php
Go to the documentation of this file.
1 <?php
11 
33 function elgg_create_river_item(array $options = []) {
34 
35  $view = elgg_extract('view', $options, '');
36  // use default viewtype for when called from web services api
37  if (!empty($view) && !elgg_view_exists($view, 'default')) {
38  return false;
39  }
40 
41  $action_type = elgg_extract('action_type', $options);
42  if (empty($action_type)) {
43  return false;
44  }
45 
46  $subject_guid = elgg_extract('subject_guid', $options, elgg_get_logged_in_user_guid());
47  if (!(get_entity($subject_guid) instanceof ElggEntity)) {
48  return false;
49  }
50 
51  $object_guid = elgg_extract('object_guid', $options, 0);
52  if (!(get_entity($object_guid) instanceof ElggEntity)) {
53  return false;
54  }
55 
56  $target_guid = elgg_extract('target_guid', $options, 0);
57  if ($target_guid) {
58  // target_guid is not a required parameter so check
59  // it only if it is included in the parameters
60  if (!(get_entity($target_guid) instanceof ElggEntity)) {
61  return false;
62  }
63  }
64 
65  $posted = elgg_extract('posted', $options, time());
66 
67  $annotation_id = elgg_extract('annotation_id', $options, 0);
68  if ($annotation_id) {
69  if (!elgg_get_annotation_from_id($annotation_id)) {
70  return false;
71  }
72  }
73 
74  $return_item = elgg_extract('return_item', $options, false);
75 
76  $values = [
77  'action_type' => $action_type,
78  'view' => $view,
79  'subject_guid' => $subject_guid,
80  'object_guid' => $object_guid,
81  'target_guid' => $target_guid,
82  'annotation_id' => $annotation_id,
83  'posted' => $posted,
84  ];
85  $col_types = [
86  'action_type' => ELGG_VALUE_STRING,
87  'view' => ELGG_VALUE_STRING,
88  'subject_guid' => ELGG_VALUE_INTEGER,
89  'object_guid' => ELGG_VALUE_INTEGER,
90  'target_guid' => ELGG_VALUE_INTEGER,
91  'annotation_id' => ELGG_VALUE_INTEGER,
92  'posted' => ELGG_VALUE_INTEGER,
93  ];
94 
95  // return false to stop insert
96  $values = elgg_trigger_plugin_hook('creating', 'river', null, $values);
97  if ($values == false) {
98  // inserting did not fail - it was just prevented
99  return true;
100  }
101 
102  $qb = \Elgg\Database\Insert::intoTable('river');
103  $query_params = [];
104  foreach ($values as $name => $value) {
105  $query_params[$name] = $qb->param($value, $col_types[$name]);
106  }
107  $qb->values($query_params);
108 
109  $id = _elgg_services()->db->insertData($qb);
110  if (!$id) {
111  return false;
112  }
113 
114  if (!$return_item) {
115  return $id;
116  }
117 
118  $ia = elgg_set_ignore_access(true);
121 
122  if (!$item) {
123  return false;
124  }
125 
126  elgg_trigger_event('created', 'river', $item);
127 
128  return $item;
129 }
130 
184 function elgg_get_river(array $options = []) {
185  return \Elgg\Database\River::find($options);
186 }
187 
196  'id' => $id,
197  ]);
198 
199  return $items ? $items[0] : false;
200 }
201 
220 function elgg_delete_river(array $options = []) {
221 
223  // requirements not met
224  return false;
225  }
226 
227  return elgg_call(ELGG_IGNORE_ACCESS, function() use ($options) {
228  $options['batch'] = true;
229  $options['batch_size'] = 25;
230  $options['batch_inc_offset'] = false;
231 
232  $river = elgg_get_river($options);
233  $count = $river->count();
234 
235  if (!$count) {
236  return;
237  }
238 
239  $success = 0;
240  foreach ($river as $river_item) {
241  if ($river_item->delete()) {
242  $success++;
243  }
244  }
245 
246  return $success == $count;
247  });
248 }
249 
261 function elgg_list_river(array $options = []) {
262  $defaults = [
263  'offset' => (int) max(get_input('offset', 0), 0),
264  'limit' => (int) max(get_input('limit', max(20, _elgg_config()->default_limit)), 0),
265  'pagination' => true,
266  'list_class' => 'elgg-list-river',
267  ];
268 
269  $options = array_merge($defaults, $options);
270 
271  $options['register_rss_link'] = elgg_extract('register_rss_link', $options, elgg_extract('pagination', $options));
272  if ($options['register_rss_link']) {
274  }
275 
276  if (!$options["limit"] && !$options["offset"]) {
277  // no need for pagination if listing is unlimited
278  $options["pagination"] = false;
279  }
280 
281  $options['count'] = false;
283  $options['count'] = is_array($items) ? count($items) : 0;
284 
285  if (!empty($items)) {
286  $count_needed = true;
287  if (!$options['pagination']) {
288  $count_needed = false;
289  } elseif (!$options['offset'] && !$options['limit']) {
290  $count_needed = false;
291  } elseif (($options['count'] < (int) $options['limit']) && !$options['offset']) {
292  $count_needed = false;
293  }
294 
295  if ($count_needed) {
296  $options['count'] = true;
297 
298  $options['count'] = (int) elgg_get_river($options);
299  }
300  }
301 
302  $options['items'] = $items;
303 
304  return elgg_view('page/components/list', $options);
305 }
306 
317 function _elgg_river_test($hook, $type, $value) {
319  return $value;
320 }
321 
334  if (!$item instanceof \ElggRiverItem) {
335  return;
336  }
337 
338  $object = $item->getObjectEntity();
339  if (!$object) {
340  return;
341  }
342 
343  $object->updateLastAction($item->getTimePosted());
344 }
345 
357 function _elgg_river_disable($event, $type, $entity) {
358 
359  if (!$entity instanceof ElggEntity) {
360  return;
361  }
362 
363  $dbprefix = _elgg_config()->dbprefix;
364  $query = <<<QUERY
365  UPDATE {$dbprefix}river AS rv
366  SET rv.enabled = 'no'
367  WHERE (rv.subject_guid = {$entity->guid} OR rv.object_guid = {$entity->guid} OR rv.target_guid = {$entity->guid});
368 QUERY;
369 
370  elgg()->db->updateData($query);
371  return;
372 }
373 
374 
386 function _elgg_river_enable($event, $type, $entity) {
387 
388  if (!$entity instanceof ElggEntity) {
389  return;
390  }
391 
392  $dbprefix = _elgg_config()->dbprefix;
393  $query = <<<QUERY
394  UPDATE {$dbprefix}river AS rv
395  LEFT JOIN {$dbprefix}entities AS se ON se.guid = rv.subject_guid
396  LEFT JOIN {$dbprefix}entities AS oe ON oe.guid = rv.object_guid
397  LEFT JOIN {$dbprefix}entities AS te ON te.guid = rv.target_guid
398  SET rv.enabled = 'yes'
399  WHERE (
400  (se.enabled = 'yes' OR se.guid IS NULL) AND
401  (oe.enabled = 'yes' OR oe.guid IS NULL) AND
402  (te.enabled = 'yes' OR te.guid IS NULL)
403  )
404  AND (se.guid = {$entity->guid} OR oe.guid = {$entity->guid} OR te.guid = {$entity->guid});
405 QUERY;
406 
407  elgg()->db->updateData($query);
408  return;
409 }
410 
420 function _elgg_river_menu_setup(\Elgg\Hook $hook) {
421  if (!elgg_is_logged_in()) {
422  return;
423  }
424 
425  $item = $hook->getParam('item');
426  if (!($item instanceof ElggRiverItem)) {
427  return;
428  }
429 
430  if (!$item->canDelete()) {
431  return;
432  }
433 
434  $return = $hook->getValue();
435 
436  $return[] = \ElggMenuItem::factory([
437  'name' => 'delete',
438  'href' => "action/river/delete?id={$item->id}",
439  'is_action' => true,
440  'icon' => 'delete',
441  'text' => elgg_echo('river:delete'),
442  'confirm' => elgg_echo('deleteconfirm'),
443  'priority' => 999,
444  ]);
445 
446  return $return;
447 }
448 
456 function _elgg_river_init() {
457  elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_river_test');
458 
459  elgg_register_plugin_hook_handler('register', 'menu:river', '_elgg_river_menu_setup');
460 
461  elgg_register_event_handler('created', 'river', '_elgg_river_update_object_last_action');
462 }
463 
467 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
468  $events->registerHandler('init', 'system', '_elgg_river_init');
469  $events->registerHandler('disable:after', 'all', '_elgg_river_disable', 600);
470  $events->registerHandler('enable:after', 'all', '_elgg_river_enable', 600);
471 };
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags...
Definition: elgglib.php:1176
elgg_view_exists($view, $viewtype= '', $recurse=true)
Returns whether the specified view exists.
Definition: views.php:205
elgg_is_logged_in()
Returns whether or not the user is currently logged in.
Definition: sessions.php:48
$query
Definition: groups.php:8
if(!$item instanceof ElggRiverItem) $object
Definition: responses.php:23
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
$CONFIG default_limit
The default "limit" used in site queries.
Definition: config.php:121
_elgg_is_valid_options_for_batch_operation($options, $type)
Checks if there are some constraints on the options array for potentially dangerous operations...
Definition: elgglib.php:1401
if(!$items) $item
Definition: delete.php:13
$defaults
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:138
elgg_delete_river(array $options=[])
Delete river items based on $options.
Definition: river.php:220
Events service.
$items
Definition: delete.php:8
$target_guid
Definition: groups.php:22
$type
Definition: delete.php:21
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:634
elgg_create_river_item(array $options=[])
Adds an item to the river.
Definition: river.php:33
_elgg_river_init()
Initialize river library.
Definition: river.php:456
elgg_echo($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
_elgg_river_menu_setup(\Elgg\Hook $hook)
Add the delete to river actions menu.
Definition: river.php:420
$options
Elgg admin footer.
Definition: footer.php:6
static factory($options)
Create an ElggMenuItem from an associative array.
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:156
Configuration exception.
$id
River item delete action.
Definition: delete.php:6
if(!$owner||!$owner->canEdit()) if(!$owner->hasIcon('master')) if(!$owner->saveIconFromElggFile($owner->getIcon('master'), 'icon', $coords)) $view
Definition: crop.php:30
Base class for events and hooks.
elgg_get_annotation_from_id($id)
Elgg annotations Functions to manage object annotations.
Definition: annotations.php:16
_elgg_river_update_object_last_action($event, $type, $item)
Updates the last action of the object of an river item.
Definition: river.php:333
$entity
Definition: reset.php:8
elgg_set_ignore_access($ignore=true)
Set if Elgg&#39;s access system should be ignored.
Definition: access.php:52
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
_elgg_river_disable($event, $type, $entity)
Disable river entries that reference a disabled entity as subject/object/target.
Definition: river.php:357
elgg_get_river(array $options=[])
Get river items.
Definition: river.php:184
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:720
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif(is_callable([$item, 'getType']))
Definition: item.php:39
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1131
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:449
$posted
Definition: comment.php:64
_elgg_config()
Get the Elgg config service.
$value
Definition: debugging.php:7
const ELGG_VALUE_STRING
Definition: constants.php:139
_elgg_river_test($hook, $type, $value)
Register river unit tests.
Definition: river.php:317
if(elgg_in_context('widget')) $count
Definition: pagination.php:21
class
Definition: placeholder.php:21
elgg_get_river_item_from_id($id)
Get river item from its ID.
Definition: river.php:194
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
_elgg_river_enable($event, $type, $entity)
Enable river entries that reference a re-enabled entity as subject/object/target. ...
Definition: river.php:386
elgg_list_river(array $options=[])
List river items.
Definition: river.php:261
elgg_trigger_event($event, $object_type, $object=null)
Definition: elgglib.php:513
elgg_view($view, $vars=[], $viewtype= '')
Return a parsed view.
Definition: views.php:246
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:39
var elgg
Definition: elgglib.js:4
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:87
elgg_register_rss_link()
Include the RSS icon link and link element in the head.
Definition: views.php:1575