Elgg  Version 6.3
AnnotationsTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Database;
8 use Elgg\Traits\TimeUsing;
9 
16 
17  use TimeUsing;
18 
19  public const TABLE_NAME = 'annotations';
20 
21  public const DEFAULT_JOIN_ALIAS = 'a_table';
22 
29  public function __construct(protected Database $db, protected EventsService $events) {
30  }
31 
39  public function get(int $id): ?\ElggAnnotation {
40  $qb = Select::fromTable(self::TABLE_NAME);
41  $qb->select('*');
42 
43  $where = AnnotationWhereClause::factory(['ids' => $id]);
44  $qb->addClause($where);
45 
46  $row = $this->db->getDataRow($qb);
47  return !empty($row) ? new \ElggAnnotation($row) : null;
48  }
49 
57  public function delete(\ElggAnnotation $annotation): bool {
58  if (!$annotation->canEdit()) {
59  return false;
60  }
61 
62  if (!$this->events->trigger('delete', 'annotation', $annotation)) {
63  return false;
64  }
65 
66  $qb = Delete::fromTable(self::TABLE_NAME);
67  $qb->where($qb->compare('id', '=', $annotation->id, ELGG_VALUE_INTEGER));
68  $deleted = $this->db->deleteData($qb);
69 
70  if ($deleted) {
72  'annotation_id' => $annotation->id,
73  'limit' => false,
74  ]);
75  }
76 
77  return $deleted !== false;
78  }
79 
88  public function create(\ElggAnnotation $annotation, \ElggEntity $entity): int|bool {
89  if ($annotation->id) {
90  return $this->update($annotation);
91  }
92 
93  if (is_null($annotation->owner_guid) || is_null($annotation->name) || is_null($annotation->value)) {
94  return false;
95  }
96 
97  $annotation->entity_guid = $entity->guid;
98 
99  // @todo It looks like annotations permissions are not being checked anywhere...
100  // Uncomment once tests have been updated
101  // See #11418
102  //if (!$entity->canAnnotate(0, $annotation->name)) {
103  // return false;
104  //}
105 
106  if (!$this->events->triggerBefore('create', 'annotation', $annotation)) {
107  return false;
108  }
109 
110  $time_created = $this->getCurrentTime()->getTimestamp();
111 
112  $qb = Insert::intoTable(self::TABLE_NAME);
113  $qb->values([
114  'entity_guid' => $qb->param($annotation->entity_guid, ELGG_VALUE_INTEGER),
115  'name' => $qb->param($annotation->name, ELGG_VALUE_STRING),
116  'value' => $qb->param($annotation->value, $annotation->value_type === 'text' ? ELGG_VALUE_STRING : ELGG_VALUE_INTEGER),
117  'value_type' => $qb->param($annotation->value_type, ELGG_VALUE_STRING),
118  'owner_guid' => $qb->param($annotation->owner_guid, ELGG_VALUE_INTEGER),
119  'time_created' => $qb->param($time_created, ELGG_VALUE_INTEGER),
120  'access_id' => $qb->param($annotation->access_id, ELGG_VALUE_INTEGER),
121  ]);
122 
123  $result = $this->db->insertData($qb);
124  if ($result === false) {
125  return false;
126  }
127 
128  $annotation->id = $result;
129  $annotation->time_created = $time_created;
130 
131  if (!$this->events->trigger('create', 'annotation', $annotation)) {
133 
134  return false;
135  }
136 
137  $this->events->triggerAfter('create', 'annotation', $annotation);
138 
139  return $result;
140  }
141 
151  public function update(\ElggAnnotation $annotation): bool {
152  if (!$annotation->canEdit()) {
153  return false;
154  }
155 
156  if (is_null($annotation->owner_guid) || is_null($annotation->name) || is_null($annotation->value)) {
157  return false;
158  }
159 
160  if (!$this->events->triggerBefore('update', 'annotation', $annotation)) {
161  return false;
162  }
163 
164  $qb = Update::table(self::TABLE_NAME);
165  $qb->set('name', $qb->param($annotation->name, ELGG_VALUE_STRING))
166  ->set('value', $qb->param($annotation->value, $annotation->value_type === 'text' ? ELGG_VALUE_STRING : ELGG_VALUE_INTEGER))
167  ->set('value_type', $qb->param($annotation->value_type, ELGG_VALUE_STRING))
168  ->set('access_id', $qb->param($annotation->access_id, ELGG_VALUE_INTEGER))
169  ->set('owner_guid', $qb->param($annotation->owner_guid, ELGG_VALUE_INTEGER))
170  ->where($qb->compare('id', '=', $annotation->id, ELGG_VALUE_INTEGER));
171 
172  $result = $this->db->updateData($qb);
173 
174  if ($result === false) {
175  return false;
176  }
177 
178  $this->events->trigger('update', 'annotation', $annotation);
179  $this->events->triggerAfter('update', 'annotation', $annotation);
180 
181  return $result;
182  }
183 
193  public function find(array $options = []) {
194  return Annotations::find($options);
195  }
196 
211  public function deleteAll(array $options): bool {
212  if (!$this->isValidOptionsForBatchOperation($options)) {
213  return false;
214  }
215 
216  $options['batch'] = true;
217  $options['batch_size'] = 50;
218  $options['batch_inc_offset'] = false;
219 
220  $annotations = Annotations::find($options);
221  $count = $annotations->count();
222 
223  if (!$count) {
224  return true;
225  }
226 
227  $success = 0;
228  foreach ($annotations as $annotation) {
229  if ($annotation->delete()) {
230  $success++;
231  }
232  }
233 
234  return $success === $count;
235  }
236 
244  protected function isValidOptionsForBatchOperation(array $options): bool {
245  $required = [
246  'guid', 'guids',
247  'annotation_owner_guid', 'annotation_owner_guids',
248  'annotation_name', 'annotation_names',
249  'annotation_value', 'annotation_values',
250  ];
251 
252  foreach ($required as $key) {
253  // check that it exists and is something.
254  if (isset($options[$key]) && !elgg_is_empty($options[$key])) {
255  return true;
256  }
257  }
258 
259  return false;
260  }
261 
271  public function exists(int $entity_guid, string $name, int $owner_guid): bool {
272  if (!$owner_guid) {
273  return false;
274  }
275 
276  $qb = Select::fromTable(self::TABLE_NAME);
277  $qb->select('id');
278  $qb->where($qb->compare('owner_guid', '=', $owner_guid, ELGG_VALUE_INTEGER))
279  ->andWhere($qb->compare('entity_guid', '=', $entity_guid, ELGG_VALUE_INTEGER))
280  ->andWhere($qb->compare('name', '=', $name, ELGG_VALUE_STRING));
281 
282  $result = $this->db->getDataRow($qb);
283 
284  return !empty($result) && $result->id;
285  }
286 }
$entity
Definition: reset.php:8
$deleted
Definition: delete.php:25
if(! $user||! $user->canDelete()) $name
Definition: delete.php:22
if($id< 1) $annotation
Definition: delete.php:11
$id
Generic annotation delete action.
Definition: delete.php:6
$entity_guid
Action for adding and editing comments.
Definition: save.php:6
elgg_delete_annotation_by_id(int $id)
Deletes an annotation using its ID.
Definition: annotations.php:27
$count
Definition: ban.php:24
Entity Annotation.
Interfaces with the database to perform CRUD operations on annotations.
__construct(protected Database $db, protected EventsService $events)
Constructor.
deleteAll(array $options)
Deletes annotations based on $options.
find(array $options=[])
Returns annotations.
create(\ElggAnnotation $annotation, \ElggEntity $entity)
Create a new annotation and return its ID.
isValidOptionsForBatchOperation(array $options)
Checks if there are some constraints on the options array for potentially dangerous operations.
update(\ElggAnnotation $annotation)
Store updated annotation in the database.
exists(int $entity_guid, string $name, int $owner_guid)
Check to see if a user has already created an annotation on an object.
Builds queries for matching annotations against their properties.
static fromTable(string $table)
Returns a QueryBuilder for deleting data from a given table.
Definition: Delete.php:17
static intoTable(string $table)
Returns a QueryBuilder for inserting data in a given table.
Definition: Insert.php:17
static fromTable(string $table, ?string $alias=null)
Returns a QueryBuilder for selecting data from a given table.
Definition: Select.php:18
static table(string $table)
Returns a QueryBuilder for updating data in a given table.
Definition: Update.php:17
The Elgg database.
Definition: Database.php:26
Events service.
$owner_guid
const ELGG_VALUE_STRING
Definition: constants.php:112
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:111
if($who_can_change_language==='nobody') elseif($who_can_change_language==='admin_only' &&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
if(! $user instanceof \ElggUser) $time_created
Definition: online.php:13
foreach($recommendedExtensions as $extension) if(empty(ini_get('session.gc_probability'))||empty(ini_get('session.gc_divisor'))) $db
elgg_is_empty($value)
Check if a value isn't empty, but allow 0 and '0'.
Definition: input.php:176
$required
Definition: label.php:12
$qb
Definition: queue.php:14
if($container instanceof ElggGroup && $container->guid !=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
elgg_delete_river(array $options=[])
Delete river items based on $options.
Definition: river.php:135
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10