Elgg  Version 6.1
RiverTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Database;
9 
16 class RiverTable {
17 
18  use TimeUsing;
19 
23  public const TABLE_NAME = 'river';
24 
25  public const DEFAULT_JOIN_ALIAS = 'rv';
26 
36  public function __construct(
37  protected Database $db,
38  protected AnnotationsTable $annotationsTable,
39  protected EntityTable $entityTable,
40  protected EventsService $events,
41  protected ViewsService $views
42  ) {
43  }
44 
52  public function get(int $id): ?\ElggRiverItem {
53  $select = Select::fromTable(self::TABLE_NAME);
54  $select->select('*')
55  ->where($select->compare('id', '=', $id, ELGG_VALUE_ID));
56 
57  $row = $this->db->getDataRow($select);
58 
59  return $row ? new \ElggRiverItem($row) : null;
60  }
61 
69  public function create(\ElggRiverItem $item): bool {
70  if ($item->id) {
71  // already created
72  return false;
73  }
74 
75  if (!empty($item->view) && !$this->views->viewExists($item->view)) {
76  return false;
77  }
78 
79  if (empty($item->action_type)) {
80  return false;
81  }
82 
83  if (empty($item->subject_guid) || !$this->entityTable->exists($item->subject_guid)) {
84  return false;
85  }
86 
87  if (empty($item->object_guid) || !$this->entityTable->exists($item->object_guid)) {
88  return false;
89  }
90 
91  if (!empty($item->target_guid) && !$this->entityTable->exists($item->target_guid)) {
92  return false;
93  }
94 
95  if (!empty($item->annotation_id) && !$this->annotationsTable->get($item->annotation_id)) {
96  return false;
97  }
98 
99  $created = $item->posted ?: $this->getCurrentTime()->getTimestamp();
100 
101  if (!$this->events->triggerBefore('create', 'river', $item)) {
102  return false;
103  }
104 
105  $insert = Insert::intoTable(self::TABLE_NAME);
106  $insert->values([
107  'action_type' => $insert->param($item->action_type, ELGG_VALUE_STRING),
108  'view' => $insert->param($item->view ?? '', ELGG_VALUE_STRING),
109  'subject_guid' => $insert->param($item->subject_guid, ELGG_VALUE_GUID),
110  'object_guid' => $insert->param($item->object_guid, ELGG_VALUE_GUID),
111  'target_guid' => $insert->param($item->target_guid ?? 0, ELGG_VALUE_GUID),
112  'annotation_id' => $insert->param($item->annotation_id ?? 0, ELGG_VALUE_ID),
113  'posted' => $insert->param($created, ELGG_VALUE_TIMESTAMP),
114  'last_action' => $insert->param($created, ELGG_VALUE_TIMESTAMP),
115  ]);
116 
117  $id = $this->db->insertData($insert);
118  if (empty($id)) {
119  return false;
120  }
121 
122  $item->id = $id;
123  $item->posted = $created;
124  $item->last_action = $created;
125 
126  $this->events->triggerAfter('create', 'river', $item);
127 
128  return true;
129  }
130 
138  public function delete(\ElggRiverItem $item): bool {
139  if (!$item->id) {
140  return false;
141  }
142 
143  if (!$this->events->triggerBefore('delete', 'river', $item)) {
144  return false;
145  }
146 
147  $delete = Delete::fromTable(self::TABLE_NAME);
148  $delete->where($delete->compare('id', '=', $item->id, ELGG_VALUE_ID));
149 
150  $result = (bool) $this->db->deleteData($delete);
151 
152  $this->events->triggerAfter('delete', 'river', $item);
153 
154  return $result;
155  }
156 
165  public function updateLastAction(\ElggRiverItem $item, int $last_action = null): int {
166  if ($last_action === null) {
167  $last_action = $this->getCurrentTime()->getTimestamp();
168  }
169 
170  $update = Update::table(self::TABLE_NAME);
171  $update->set('last_action', $update->param($last_action, ELGG_VALUE_TIMESTAMP))
172  ->where($update->compare('id', '=', $item->id, ELGG_VALUE_ID));
173 
174  $this->db->updateData($update);
175 
176  return (int) $last_action;
177  }
178 }
if(!$items) $item
Definition: delete.php:13
static table(string $table)
Returns a QueryBuilder for updating data in a given table.
Definition: Update.php:17
create(\ElggRiverItem $item)
Save a river item to the database.
Definition: RiverTable.php:69
__construct(protected Database $db, protected AnnotationsTable $annotationsTable, protected EntityTable $entityTable, protected EventsService $events, protected ViewsService $views)
Create the river table service.
Definition: RiverTable.php:36
The Elgg database.
Definition: Database.php:26
const ELGG_VALUE_GUID
Definition: constants.php:113
Events service.
$delete
const ELGG_VALUE_ID
Definition: constants.php:114
trait TimeUsing
Adds methods for setting the current time (for testing)
Definition: TimeUsing.php:10
static intoTable(string $table)
Returns a QueryBuilder for inserting data in a given table.
Definition: Insert.php:17
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:25
foreach($recommendedExtensions as $extension) if(empty(ini_get('session.gc_probability'))||empty(ini_get('session.gc_divisor'))) $db
Views service.
static fromTable(string $table)
Returns a QueryBuilder for deleting data from a given table.
Definition: Delete.php:17
updateLastAction(\ElggRiverItem $item, int $last_action=null)
Update the last_action column in the river table for $item.
Definition: RiverTable.php:165
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:115
const ELGG_VALUE_STRING
Definition: constants.php:112
static fromTable(string $table, string $alias=null)
Returns a QueryBuilder for selecting data from a given table.
Definition: Select.php:18
Interfaces with the database to perform CRUD operations on annotations.
River table database action.
Definition: RiverTable.php:16
$id
Generic annotation delete action.
Definition: delete.php:6
$views
Definition: item.php:17
Entity table database service.
Definition: EntityTable.php:24