Elgg  Version 4.3
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 
22  protected $db;
23 
27  protected $events;
28 
36  $this->db = $db;
37  $this->events = $events;
38  }
39 
47  public function get(int $id) {
48  $qb = Select::fromTable('annotations');
49  $qb->select('*');
50 
51  $where = new AnnotationWhereClause();
52  $where->ids = $id;
53  $qb->addClause($where);
54 
55  $row = $this->db->getDataRow($qb);
56  if (!empty($row)) {
57  return new \ElggAnnotation($row);
58  }
59 
60  return false;
61  }
62 
70  public function delete(\ElggAnnotation $annotation) {
71  if (!$annotation->canEdit()) {
72  return false;
73  }
74 
75  if (!$this->events->trigger('delete', 'annotation', $annotation)) {
76  return false;
77  }
78 
79  $qb = Delete::fromTable('annotations');
80  $qb->where($qb->compare('id', '=', $annotation->id, ELGG_VALUE_INTEGER));
81  $deleted = $this->db->deleteData($qb);
82 
83  if ($deleted) {
85  'annotation_id' => $annotation->id,
86  'limit' => false,
87  ]);
88  }
89 
90  return $deleted !== false;
91  }
92 
102  if ($annotation->id) {
103  return $this->update($annotation);
104  }
105 
106  if (is_null($annotation->owner_guid) || is_null($annotation->name) || is_null($annotation->value)) {
107  return false;
108  }
109 
110  $annotation->entity_guid = $entity->guid;
111 
112  // @todo It looks like annotations permissions are not being checked anywhere...
113  // Uncomment once tests have been updated
114  // See #11418
115  //if (!$entity->canAnnotate(0, $annotation->name)) {
116  // return false;
117  //}
118 
119  if (!$this->events->triggerDeprecated('annotate', $entity->getType(), $entity, "The 'annotate', '{$entity->getType()}' event is deprecated. Use the 'create', 'annotation' event instead.", '4.3')) {
120  return false;
121  }
122 
123  if (!$this->events->triggerBefore('create', 'annotation', $annotation)) {
124  return false;
125  }
126 
127  $time_created = $this->getCurrentTime()->getTimestamp();
128 
129  $qb = Insert::intoTable('annotations');
130  $qb->values([
131  'entity_guid' => $qb->param($annotation->entity_guid, ELGG_VALUE_INTEGER),
132  'name' => $qb->param($annotation->name, ELGG_VALUE_STRING),
133  'value' => $qb->param($annotation->value, $annotation->value_type === 'text' ? ELGG_VALUE_STRING : ELGG_VALUE_INTEGER),
134  'value_type' => $qb->param($annotation->value_type, ELGG_VALUE_STRING),
135  'owner_guid' => $qb->param($annotation->owner_guid, ELGG_VALUE_INTEGER),
136  'time_created' => $qb->param($time_created, ELGG_VALUE_INTEGER),
137  'access_id' => $qb->param($annotation->access_id, ELGG_VALUE_INTEGER),
138  ]);
139 
140  $result = $this->db->insertData($qb);
141  if ($result === false) {
142  return false;
143  }
144 
145  $annotation->id = $result;
146  $annotation->time_created = $time_created;
147 
148  if (!$this->events->trigger('create', 'annotation', $annotation)) {
150 
151  return false;
152  }
153 
154  $this->events->triggerAfter('create', 'annotation', $annotation);
155 
156  return $result;
157  }
158 
168  public function update(\ElggAnnotation $annotation) {
169  if (!$annotation->canEdit()) {
170  return false;
171  }
172 
173  if (is_null($annotation->owner_guid) || is_null($annotation->name) || is_null($annotation->value)) {
174  return false;
175  }
176 
177  if (!$this->events->triggerBefore('update', 'annotation', $annotation)) {
178  return false;
179  }
180 
181  $qb = Update::table('annotations');
182  $qb->set('name', $qb->param($annotation->name, ELGG_VALUE_STRING))
183  ->set('value', $qb->param($annotation->value, $annotation->value_type === 'integer' ? ELGG_VALUE_INTEGER : ELGG_VALUE_STRING))
184  ->set('value_type', $qb->param($annotation->value_type, ELGG_VALUE_STRING))
185  ->set('access_id', $qb->param($annotation->access_id, ELGG_VALUE_INTEGER))
186  ->set('owner_guid', $qb->param($annotation->owner_guid, ELGG_VALUE_INTEGER))
187  ->where($qb->compare('id', '=', $annotation->id, ELGG_VALUE_INTEGER));
188 
189  $result = $this->db->updateData($qb);
190 
191  if ($result === false) {
192  return false;
193  }
194 
195  $this->events->trigger('update', 'annotation', $annotation);
196  $this->events->triggerAfter('update', 'annotation', $annotation);
197 
198  return $result;
199  }
200 
209  public function disable(\ElggAnnotation $annotation) {
210  if ($annotation->enabled == 'no') {
211  return true;
212  }
213 
214  if (!$annotation->canEdit()) {
215  return false;
216  }
217 
218  if (!_elgg_services()->events->trigger('disable', $annotation->getType(), $annotation)) {
219  return false;
220  }
221 
222  if ($annotation->id) {
223  $qb = Update::table('annotations');
224  $qb->set('enabled', $qb->param('no', ELGG_VALUE_STRING))
225  ->where($qb->compare('id', '=', $annotation->id, ELGG_VALUE_INTEGER));
226 
227  if (!$this->db->updateData($qb)) {
228  return false;
229  }
230  }
231 
232  $annotation->enabled = 'no';
233 
234  return true;
235  }
236 
245  public function enable(\ElggAnnotation $annotation) {
246  if ($annotation->enabled == 'yes') {
247  return true;
248  }
249 
250  if (!$annotation->canEdit()) {
251  return false;
252  }
253 
254  if (!$this->events->trigger('enable', $annotation->getType(), $annotation)) {
255  return false;
256  }
257 
258  if ($annotation->id) {
259  $qb = Update::table('annotations');
260  $qb->set('enabled', $qb->param('yes', ELGG_VALUE_STRING))
261  ->where($qb->compare('id', '=', $annotation->id, ELGG_VALUE_INTEGER));
262 
263  if (!$this->db->updateData($qb)) {
264  return false;
265  }
266  }
267 
268  $annotation->enabled = 'yes';
269 
270  return true;
271  }
272 
282  public function find(array $options = []) {
283  $options['metastring_type'] = 'annotations';
284  $options = QueryOptions::normalizeMetastringOptions($options);
285 
286  return Annotations::find($options);
287  }
288 
303  public function deleteAll(array $options) {
304  if (!$this->isValidOptionsForBatchOperation($options)) {
305  return false;
306  }
307 
308  $options['batch'] = true;
309  $options['batch_size'] = 50;
310  $options['batch_inc_offset'] = false;
311 
312  $annotations = Annotations::find($options);
313  $count = $annotations->count();
314 
315  if (!$count) {
316  return;
317  }
318 
319  $success = 0;
320  foreach ($annotations as $annotation) {
321  if ($annotation->delete()) {
322  $success++;
323  }
324  }
325 
326  return $success == $count;
327  }
328 
337  public function disableAll(array $options) {
338  if (!$this->isValidOptionsForBatchOperation($options)) {
339  return false;
340  }
341 
342  // if we can see hidden (disabled) we need to use the offset
343  // otherwise we risk an infinite loop if there are more than 50
344  $inc_offset = _elgg_services()->session->getDisabledEntityVisibility();
345 
346  $options['batch'] = true;
347  $options['batch_size'] = 50;
348  $options['batch_inc_offset'] = $inc_offset;
349 
350  $annotations = Annotations::find($options);
351  $count = $annotations->count();
352 
353  if (!$count) {
354  return;
355  }
356 
357  $success = 0;
358  foreach ($annotations as $annotation) {
359  if ($annotation->disable()) {
360  $success++;
361  }
362  }
363 
364  return $success == $count;
365  }
366 
375  public function enableAll(array $options) {
376  if (!$this->isValidOptionsForBatchOperation($options)) {
377  return false;
378  }
379 
380  $options['batch'] = true;
381  $options['batch_size'] = 50;
382 
383  $annotations = Annotations::find($options);
384  $count = $annotations->count();
385 
386  if (!$count) {
387  return;
388  }
389 
390  $success = 0;
391  foreach ($annotations as $annotation) {
392  if ($annotation->enable()) {
393  $success++;
394  }
395  }
396 
397  return $success == $count;
398  }
399 
407  protected function isValidOptionsForBatchOperation(array $options): bool {
408  $required = [
409  'guid', 'guids',
410  'annotation_owner_guid', 'annotation_owner_guids',
411  'annotation_name', 'annotation_names',
412  'annotation_value', 'annotation_values',
413  ];
414 
415  foreach ($required as $key) {
416  // check that it exists and is something.
417  if (isset($options[$key]) && !elgg_is_empty($options[$key])) {
418  return true;
419  }
420  }
421 
422  return false;
423  }
424 
434  public function exists(int $entity_guid, string $name, int $owner_guid) {
435  if (!$owner_guid) {
436  return false;
437  }
438 
439  $qb = Select::fromTable('annotations');
440  $qb->select('id');
441  $qb->where($qb->compare('owner_guid', '=', $owner_guid, ELGG_VALUE_INTEGER))
442  ->andWhere($qb->compare('entity_guid', '=', $entity_guid, ELGG_VALUE_INTEGER))
443  ->andWhere($qb->compare('name', '=', $name, ELGG_VALUE_STRING));
444 
445  $result = $this->db->getDataRow($qb);
446 
447  return !empty($result) && $result->id;
448  }
449 }
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:86
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...
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:126
elgg_delete_river(array $options=[])
Delete river items based on $options.
Definition: river.php:133
Entity Annotation.
__construct(Database $db, EventsService $events)
Constructor.
Events service.
Builds queries for matching annotations against their properties.
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:179
$entity_guid
Action for adding and editing comments.
Definition: save.php:6
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:25
canEdit($user_guid=0)
Determines whether or not the user can edit this annotation.
$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.
$time_created
Definition: online.php:18
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:127
getType()
Returns the entity type.
$required
Definition: label.php:12
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
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