Elgg  Version 5.1
AnnotationsTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Database;
9 
16 
17  use TimeUsing;
18 
19  protected Database $db;
20 
22 
29  public function __construct(Database $db, EventsService $events) {
30  $this->db = $db;
31  $this->events = $events;
32  }
33 
41  public function get(int $id): ?\ElggAnnotation {
42  $qb = Select::fromTable('annotations');
43  $qb->select('*');
44 
45  $where = new AnnotationWhereClause();
46  $where->ids = $id;
47  $qb->addClause($where);
48 
49  $row = $this->db->getDataRow($qb);
50  return !empty($row) ? new \ElggAnnotation($row) : null;
51  }
52 
60  public function delete(\ElggAnnotation $annotation): bool {
61  if (!$annotation->canEdit()) {
62  return false;
63  }
64 
65  if (!$this->events->trigger('delete', 'annotation', $annotation)) {
66  return false;
67  }
68 
69  $qb = Delete::fromTable('annotations');
70  $qb->where($qb->compare('id', '=', $annotation->id, ELGG_VALUE_INTEGER));
71  $deleted = $this->db->deleteData($qb);
72 
73  if ($deleted) {
75  'annotation_id' => $annotation->id,
76  'limit' => false,
77  ]);
78  }
79 
80  return $deleted !== false;
81  }
82 
91  public function create(\ElggAnnotation $annotation, \ElggEntity $entity): int|bool {
92  if ($annotation->id) {
93  return $this->update($annotation);
94  }
95 
96  if (is_null($annotation->owner_guid) || is_null($annotation->name) || is_null($annotation->value)) {
97  return false;
98  }
99 
100  $annotation->entity_guid = $entity->guid;
101 
102  // @todo It looks like annotations permissions are not being checked anywhere...
103  // Uncomment once tests have been updated
104  // See #11418
105  //if (!$entity->canAnnotate(0, $annotation->name)) {
106  // return false;
107  //}
108 
109  if (!$this->events->triggerBefore('create', 'annotation', $annotation)) {
110  return false;
111  }
112 
113  $time_created = $this->getCurrentTime()->getTimestamp();
114 
115  $qb = Insert::intoTable('annotations');
116  $qb->values([
117  'entity_guid' => $qb->param($annotation->entity_guid, ELGG_VALUE_INTEGER),
118  'name' => $qb->param($annotation->name, ELGG_VALUE_STRING),
119  'value' => $qb->param($annotation->value, $annotation->value_type === 'text' ? ELGG_VALUE_STRING : ELGG_VALUE_INTEGER),
120  'value_type' => $qb->param($annotation->value_type, ELGG_VALUE_STRING),
121  'owner_guid' => $qb->param($annotation->owner_guid, ELGG_VALUE_INTEGER),
122  'time_created' => $qb->param($time_created, ELGG_VALUE_INTEGER),
123  'access_id' => $qb->param($annotation->access_id, ELGG_VALUE_INTEGER),
124  ]);
125 
126  $result = $this->db->insertData($qb);
127  if ($result === false) {
128  return false;
129  }
130 
131  $annotation->id = $result;
132  $annotation->time_created = $time_created;
133 
134  if (!$this->events->trigger('create', 'annotation', $annotation)) {
136 
137  return false;
138  }
139 
140  $this->events->triggerAfter('create', 'annotation', $annotation);
141 
142  return $result;
143  }
144 
154  public function update(\ElggAnnotation $annotation): bool {
155  if (!$annotation->canEdit()) {
156  return false;
157  }
158 
159  if (is_null($annotation->owner_guid) || is_null($annotation->name) || is_null($annotation->value)) {
160  return false;
161  }
162 
163  if (!$this->events->triggerBefore('update', 'annotation', $annotation)) {
164  return false;
165  }
166 
167  $qb = Update::table('annotations');
168  $qb->set('name', $qb->param($annotation->name, ELGG_VALUE_STRING))
169  ->set('value', $qb->param($annotation->value, $annotation->value_type === 'integer' ? ELGG_VALUE_INTEGER : ELGG_VALUE_STRING))
170  ->set('value_type', $qb->param($annotation->value_type, ELGG_VALUE_STRING))
171  ->set('access_id', $qb->param($annotation->access_id, ELGG_VALUE_INTEGER))
172  ->set('owner_guid', $qb->param($annotation->owner_guid, ELGG_VALUE_INTEGER))
173  ->where($qb->compare('id', '=', $annotation->id, ELGG_VALUE_INTEGER));
174 
175  $result = $this->db->updateData($qb);
176 
177  if ($result === false) {
178  return false;
179  }
180 
181  $this->events->trigger('update', 'annotation', $annotation);
182  $this->events->triggerAfter('update', 'annotation', $annotation);
183 
184  return $result;
185  }
186 
195  public function disable(\ElggAnnotation $annotation): bool {
196  if ($annotation->enabled === 'no') {
197  return true;
198  }
199 
200  if (!$annotation->canEdit()) {
201  return false;
202  }
203 
204  if (!_elgg_services()->events->trigger('disable', $annotation->getType(), $annotation)) {
205  return false;
206  }
207 
208  if ($annotation->id) {
209  $qb = Update::table('annotations');
210  $qb->set('enabled', $qb->param('no', ELGG_VALUE_STRING))
211  ->where($qb->compare('id', '=', $annotation->id, ELGG_VALUE_INTEGER));
212 
213  if (!$this->db->updateData($qb)) {
214  return false;
215  }
216  }
217 
218  $annotation->enabled = 'no';
219 
220  return true;
221  }
222 
231  public function enable(\ElggAnnotation $annotation): bool {
232  if ($annotation->enabled == 'yes') {
233  return true;
234  }
235 
236  if (!$annotation->canEdit()) {
237  return false;
238  }
239 
240  if (!$this->events->trigger('enable', $annotation->getType(), $annotation)) {
241  return false;
242  }
243 
244  if ($annotation->id) {
245  $qb = Update::table('annotations');
246  $qb->set('enabled', $qb->param('yes', ELGG_VALUE_STRING))
247  ->where($qb->compare('id', '=', $annotation->id, ELGG_VALUE_INTEGER));
248 
249  if (!$this->db->updateData($qb)) {
250  return false;
251  }
252  }
253 
254  $annotation->enabled = 'yes';
255 
256  return true;
257  }
258 
268  public function find(array $options = []) {
269  $options['metastring_type'] = 'annotations';
270  $options = QueryOptions::normalizeMetastringOptions($options);
271 
272  return Annotations::find($options);
273  }
274 
289  public function deleteAll(array $options): bool {
290  if (!$this->isValidOptionsForBatchOperation($options)) {
291  return false;
292  }
293 
294  $options['batch'] = true;
295  $options['batch_size'] = 50;
296  $options['batch_inc_offset'] = false;
297 
298  $annotations = Annotations::find($options);
299  $count = $annotations->count();
300 
301  if (!$count) {
302  return true;
303  }
304 
305  $success = 0;
306  foreach ($annotations as $annotation) {
307  if ($annotation->delete()) {
308  $success++;
309  }
310  }
311 
312  return $success === $count;
313  }
314 
323  public function disableAll(array $options): bool {
324  if (!$this->isValidOptionsForBatchOperation($options)) {
325  return false;
326  }
327 
328  // if we can see hidden (disabled) we need to use the offset
329  // otherwise we risk an infinite loop if there are more than 50
330  $inc_offset = _elgg_services()->session_manager->getDisabledEntityVisibility();
331 
332  $options['batch'] = true;
333  $options['batch_size'] = 50;
334  $options['batch_inc_offset'] = $inc_offset;
335 
336  $annotations = Annotations::find($options);
337  $count = $annotations->count();
338 
339  if (!$count) {
340  return true;
341  }
342 
343  $success = 0;
344  foreach ($annotations as $annotation) {
345  if ($annotation->disable()) {
346  $success++;
347  }
348  }
349 
350  return $success === $count;
351  }
352 
361  public function enableAll(array $options): bool {
362  if (!$this->isValidOptionsForBatchOperation($options)) {
363  return false;
364  }
365 
366  $options['batch'] = true;
367  $options['batch_size'] = 50;
368 
369  $annotations = Annotations::find($options);
370  $count = $annotations->count();
371 
372  if (!$count) {
373  return true;
374  }
375 
376  $success = 0;
377  foreach ($annotations as $annotation) {
378  if ($annotation->enable()) {
379  $success++;
380  }
381  }
382 
383  return $success === $count;
384  }
385 
393  protected function isValidOptionsForBatchOperation(array $options): bool {
394  $required = [
395  'guid', 'guids',
396  'annotation_owner_guid', 'annotation_owner_guids',
397  'annotation_name', 'annotation_names',
398  'annotation_value', 'annotation_values',
399  ];
400 
401  foreach ($required as $key) {
402  // check that it exists and is something.
403  if (isset($options[$key]) && !elgg_is_empty($options[$key])) {
404  return true;
405  }
406  }
407 
408  return false;
409  }
410 
420  public function exists(int $entity_guid, string $name, int $owner_guid): bool {
421  if (!$owner_guid) {
422  return false;
423  }
424 
425  $qb = Select::fromTable('annotations');
426  $qb->select('id');
427  $qb->where($qb->compare('owner_guid', '=', $owner_guid, ELGG_VALUE_INTEGER))
428  ->andWhere($qb->compare('entity_guid', '=', $entity_guid, ELGG_VALUE_INTEGER))
429  ->andWhere($qb->compare('name', '=', $name, ELGG_VALUE_STRING));
430 
431  $result = $this->db->getDataRow($qb);
432 
433  return !empty($result) && $result->id;
434  }
435 }
getType()
Return a type of extension.
disableAll(array $options)
Disables annotations based on $options.
$deleted
Definition: delete.php:25
enable(\ElggAnnotation $annotation)
Enable the annotation.
exists(int $entity_guid, string $name, int $owner_guid)
Check to see if a user has already created an annotation on an object.
if($id< 1) $annotation
Definition: delete.php:11
static find(array $options=[])
Build and execute a new query from an array of legacy options.
Definition: Repository.php:110
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
static table($table, $alias=null)
{}
Definition: Update.php:13
The Elgg database.
Definition: Database.php:25
isValidOptionsForBatchOperation(array $options)
Checks if there are some constraints on the options array for potentially dangerous operations...
if(!$user instanceof\ElggUser) $time_created
Definition: online.php:13
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:111
elgg_delete_river(array $options=[])
Delete river items based on $options.
Definition: river.php:135
Entity Annotation.
__construct(Database $db, EventsService $events)
Constructor.
Events service.
Builds queries for matching annotations against their properties.
canEdit(int $user_guid=0)
Determines whether or not the user can edit this annotation.
trait TimeUsing
Adds methods for setting the current time (for testing)
Definition: TimeUsing.php:10
$options
Elgg admin footer.
Definition: footer.php:6
elgg_is_empty($value)
Check if a value isn&#39;t empty, but allow 0 and &#39;0&#39;.
Definition: input.php:176
$entity_guid
Action for adding and editing comments.
Definition: save.php:6
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:25
$owner_guid
static intoTable($table)
{}
Definition: Insert.php:13
$entity
Definition: reset.php:8
disable(\ElggAnnotation $annotation)
Disable the annotation.
deleteAll(array $options)
Deletes annotations based on $options.
create(\ElggAnnotation $annotation,\ElggEntity $entity)
Create a new annotation and return its ID.
$count
Definition: ban.php:24
update(\ElggAnnotation $annotation)
Store updated annotation in the database.
find(array $options=[])
Returns annotations.
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
const ELGG_VALUE_STRING
Definition: constants.php:112
$required
Definition: label.php:12
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
Interfaces with the database to perform CRUD operations on annotations.
enableAll(array $options)
Enables annotations based on $options.
$id
Generic annotation delete action.
Definition: delete.php:6
$qb
Definition: queue.php:11
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13
elgg_delete_annotation_by_id(int $id)
Deletes an annotation using its ID.
Definition: annotations.php:27