Elgg  Version 5.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  const TABLE_NAME = 'river';
24 
26 
27  protected Database $db;
28 
30 
32 
33  protected ViewsService $views;
34 
44  public function __construct(Database $db, AnnotationsTable $annotationsTable, EntityTable $entityTable, EventsService $events, ViewsService $views) {
45  $this->annotationsTable = $annotationsTable;
46  $this->db = $db;
47  $this->entityTable = $entityTable;
48  $this->events = $events;
49  $this->views = $views;
50  }
51 
59  public function get(int $id): ?\ElggRiverItem {
60  $select = Select::fromTable(self::TABLE_NAME);
61  $select->select('*')
62  ->where($select->compare('id', '=', $id, ELGG_VALUE_ID));
63 
64  $row = $this->db->getDataRow($select);
65 
66  return $row ? new \ElggRiverItem($row) : null;
67  }
68 
76  public function create(\ElggRiverItem $item): bool {
77  if ($item->id) {
78  // already created
79  return false;
80  }
81 
82  if (!empty($item->view) && !$this->views->viewExists($item->view)) {
83  return false;
84  }
85 
86  if (empty($item->action_type)) {
87  return false;
88  }
89 
90  if (empty($item->subject_guid) || !$this->entityTable->exists($item->subject_guid)) {
91  return false;
92  }
93 
94  if (empty($item->object_guid) || !$this->entityTable->exists($item->object_guid)) {
95  return false;
96  }
97 
98  if (!empty($item->target_guid) && !$this->entityTable->exists($item->target_guid)) {
99  return false;
100  }
101 
102  if (!empty($item->annotation_id) && !$this->annotationsTable->get($item->annotation_id)) {
103  return false;
104  }
105 
106  $created = $item->posted ?: $this->getCurrentTime()->getTimestamp();
107 
108  if (!$this->events->triggerBefore('create', 'river', $item)) {
109  return false;
110  }
111 
112  $insert = Insert::intoTable(self::TABLE_NAME);
113  $insert->values([
114  'action_type' => $insert->param($item->action_type, ELGG_VALUE_STRING),
115  'view' => $insert->param($item->view ?? '', ELGG_VALUE_STRING),
116  'subject_guid' => $insert->param($item->subject_guid, ELGG_VALUE_GUID),
117  'object_guid' => $insert->param($item->object_guid, ELGG_VALUE_GUID),
118  'target_guid' => $insert->param($item->target_guid ?? 0, ELGG_VALUE_GUID),
119  'annotation_id' => $insert->param($item->annotation_id ?? 0, ELGG_VALUE_ID),
120  'posted' => $insert->param($created, ELGG_VALUE_TIMESTAMP),
121  'last_action' => $insert->param($created, ELGG_VALUE_TIMESTAMP),
122  ]);
123 
124  $id = $this->db->insertData($insert);
125  if (empty($id)) {
126  return false;
127  }
128 
129  $item->id = $id;
130  $item->posted = $created;
131  $item->last_action = $created;
132 
133  $this->events->triggerAfter('create', 'river', $item);
134 
135  return true;
136  }
137 
145  public function delete(\ElggRiverItem $item): bool {
146  if (!$item->id) {
147  return false;
148  }
149 
150  if (!$this->events->triggerBefore('delete', 'river', $item)) {
151  return false;
152  }
153 
154  $delete = Delete::fromTable(self::TABLE_NAME);
155  $delete->where($delete->compare('id', '=', $item->id, ELGG_VALUE_ID));
156 
157  $result = (bool) $this->db->deleteData($delete);
158 
159  $this->events->triggerAfter('delete', 'river', $item);
160 
161  return $result;
162  }
163 
172  public function updateLastAction(\ElggRiverItem $item, int $last_action = null): int {
173  if ($last_action === null) {
174  $last_action = $this->getCurrentTime()->getTimestamp();
175  }
176 
177  $update = Update::table(self::TABLE_NAME);
178  $update->set('last_action', $update->param($last_action, ELGG_VALUE_TIMESTAMP))
179  ->where($update->compare('id', '=', $item->id, ELGG_VALUE_ID));
180 
181  $this->db->updateData($update);
182 
183  return (int) $last_action;
184  }
185 }
if(!$items) $item
Definition: delete.php:13
EventsService $events
Definition: RiverTable.php:31
create(\ElggRiverItem $item)
Save a river item to the database.
Definition: RiverTable.php:76
static table($table, $alias=null)
{}
Definition: Update.php:13
The Elgg database.
Definition: Database.php:25
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
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:25
static intoTable($table)
{}
Definition: Insert.php:13
EntityTable $entityTable
Definition: RiverTable.php:29
Views service.
updateLastAction(\ElggRiverItem $item, int $last_action=null)
Update the last_action column in the river table for $item.
Definition: RiverTable.php:172
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:115
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
const ELGG_VALUE_STRING
Definition: constants.php:112
Interfaces with the database to perform CRUD operations on annotations.
River table database action.
Definition: RiverTable.php:16
__construct(Database $db, AnnotationsTable $annotationsTable, EntityTable $entityTable, EventsService $events, ViewsService $views)
Create the river table service.
Definition: RiverTable.php:44
$id
Generic annotation delete action.
Definition: delete.php:6
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13
AnnotationsTable $annotationsTable
Definition: RiverTable.php:25
Entity table database service.
Definition: EntityTable.php:26