Elgg  Version 4.3
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 
28  protected $annotationsTable;
29 
33  protected $db;
34 
38  protected $entityTable;
39 
43  protected $events;
44 
48  protected $views;
49 
60  $this->annotationsTable = $annotationsTable;
61  $this->db = $db;
62  $this->entityTable = $entityTable;
63  $this->events = $events;
64  $this->views = $views;
65  }
66 
74  public function get(int $id): ?\ElggRiverItem {
75  $select = Select::fromTable(self::TABLE_NAME);
76  $select->select('*')
77  ->where($select->compare('id', '=', $id, ELGG_VALUE_ID));
78 
79  $row = $this->db->getDataRow($select);
80  if (empty($row)) {
81  return null;
82  }
83 
84  return new \ElggRiverItem($row);
85  }
86 
94  public function create(\ElggRiverItem $item): bool {
95  if ($item->id) {
96  // already created
97  return false;
98  }
99 
100  if (!empty($item->view) && !$this->views->viewExists($item->view)) {
101  return false;
102  }
103 
104  if (empty($item->action_type)) {
105  return false;
106  }
107 
108  if (empty($item->subject_guid) || !$this->entityTable->exists($item->subject_guid)) {
109  return false;
110  }
111 
112  if (empty($item->object_guid) || !$this->entityTable->exists($item->object_guid)) {
113  return false;
114  }
115 
116  if (!empty($item->target_guid) && !$this->entityTable->exists($item->target_guid)) {
117  return false;
118  }
119 
120  if (!empty($item->annotation_id) && !$this->annotationsTable->get($item->annotation_id)) {
121  return false;
122  }
123 
124  $created = $item->posted ?? $this->getCurrentTime()->getTimestamp();
125 
126  if (!$this->events->triggerBefore('create', 'river', $item)) {
127  return false;
128  }
129 
130  $insert = Insert::intoTable(self::TABLE_NAME);
131  $insert->values([
132  'action_type' => $insert->param($item->action_type, ELGG_VALUE_STRING),
133  'view' => $insert->param($item->view ?? '', ELGG_VALUE_STRING),
134  'subject_guid' => $insert->param($item->subject_guid, ELGG_VALUE_GUID),
135  'object_guid' => $insert->param($item->object_guid, ELGG_VALUE_GUID),
136  'target_guid' => $insert->param($item->target_guid ?? 0, ELGG_VALUE_GUID),
137  'annotation_id' => $insert->param($item->annotation_id ?? 0, ELGG_VALUE_ID),
138  'posted' => $insert->param($created, ELGG_VALUE_TIMESTAMP),
139  ]);
140 
141  $id = $this->db->insertData($insert);
142  if (empty($id)) {
143  return false;
144  }
145 
146  $item->id = $id;
147  $item->posted = $created;
148 
149  $this->events->triggerAfter('create', 'river', $item);
150 
151  return true;
152  }
153 
161  public function delete(\ElggRiverItem $item): bool {
162  if (!$item->id) {
163  return false;
164  }
165 
166  if (!$this->events->triggerBefore('delete', 'river', $item)) {
167  return false;
168  }
169 
170  $delete = Delete::fromTable(self::TABLE_NAME);
171  $delete->where($delete->compare('id', '=', $item->id, ELGG_VALUE_ID));
172 
173  $result = (bool) $this->db->deleteData($delete);
174 
175  $this->events->triggerAfter('delete', 'river', $item);
176 
177  return $result;
178  }
179 }
if(!$items) $item
Definition: delete.php:13
create(\ElggRiverItem $item)
Save a river item to the database.
Definition: RiverTable.php:94
The Elgg database.
Definition: Database.php:25
const ELGG_VALUE_GUID
Definition: constants.php:128
Events service.
$delete
const ELGG_VALUE_ID
Definition: constants.php:129
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
Views service.
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:130
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
const ELGG_VALUE_STRING
Definition: constants.php:127
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:59
$id
Generic annotation delete action.
Definition: delete.php:6
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13
Entity table database service.
Definition: EntityTable.php:26