Elgg  Version 3.0
AnnotationsTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Database;
9 use ElggEntity;
10 
19 
21 
25  protected $db;
26 
30  protected $events;
31 
39  $this->db = $db;
40  $this->events = $events;
41  }
42 
50  public function get($id) {
51  $qb = Select::fromTable('annotations');
52  $qb->select('*');
53 
54  $where = new AnnotationWhereClause();
55  $where->ids = $id;
56  $qb->addClause($where);
57 
58  $row = $this->db->getDataRow($qb);
59  if (!empty($row)) {
60  return new ElggAnnotation($row);
61  }
62 
63  return false;
64  }
65 
73  public function delete(ElggAnnotation $annotation) {
74  if (!$annotation->canEdit()) {
75  return false;
76  }
77 
78  if (!$this->events->trigger('delete', 'annotation', $annotation)) {
79  return false;
80  }
81 
82  $qb = Delete::fromTable('annotations');
83  $qb->where($qb->compare('id', '=', $annotation->id, ELGG_VALUE_INTEGER));
84  $deleted = $this->db->deleteData($qb);
85 
86  if ($deleted) {
88  'annotation_id' => $annotation->id,
89  'limit' => false,
90  ]);
91  }
92 
93  return $deleted !== false;
94  }
95 
105  if ($annotation->id) {
106  return $this->update($annotation);
107  }
108 
109  $annotation->entity_guid = $entity->guid;
110 
111  // @todo It looks like annotations permissions are not being checked anywhere...
112  // Uncomment once tests have been updated
113  // See #11418
114  //if (!$entity->canAnnotate(0, $annotation->name)) {
115  // return false;
116  //}
117 
118  if (!$this->events->trigger('annotate', $entity->getType(), $entity)) {
119  return false;
120  }
121 
122  if (!$this->events->triggerBefore('create', 'annotation', $annotation)) {
123  return false;
124  }
125 
126  $time_created = $this->getCurrentTime()->getTimestamp();
127 
128  $qb = Insert::intoTable('annotations');
129  $qb->values([
130  'entity_guid' => $qb->param($annotation->entity_guid, ELGG_VALUE_INTEGER),
131  'name' => $qb->param($annotation->name, ELGG_VALUE_STRING),
132  'value' => $qb->param($annotation->value, $annotation->value_type === 'integer' ? ELGG_VALUE_INTEGER : ELGG_VALUE_STRING),
133  'value_type' => $qb->param($annotation->value_type, ELGG_VALUE_STRING),
134  'owner_guid' => $qb->param($annotation->owner_guid, ELGG_VALUE_INTEGER),
135  'time_created' => $qb->param($time_created, ELGG_VALUE_INTEGER),
136  'access_id' => $qb->param($annotation->access_id, ELGG_VALUE_INTEGER),
137  ]);
138 
139  $result = $this->db->insertData($qb);
140  if ($result === false) {
141  return false;
142  }
143 
144  $annotation->id = $result;
145  $annotation->time_created = $time_created;
146 
147  if (!$this->events->trigger('create', 'annotation', $annotation)) {
149 
150  return false;
151  }
152 
153  $this->events->triggerAfter('create', 'annotation', $annotation);
154 
155  return $result;
156  }
157 
167  public function update(ElggAnnotation $annotation) {
168  if (!$annotation->canEdit()) {
169  return false;
170  }
171 
172  if (!$this->events->triggerBefore('update', 'annotation', $annotation)) {
173  return false;
174  }
175 
176  $qb = Update::table('annotations');
177  $qb->set('name', $qb->param($annotation->name, ELGG_VALUE_STRING))
178  ->set('value', $qb->param($annotation->value, $annotation->value_type === 'integer' ? ELGG_VALUE_INTEGER : ELGG_VALUE_STRING))
179  ->set('value_type', $qb->param($annotation->value_type, ELGG_VALUE_STRING))
180  ->set('access_id', $qb->param($annotation->access_id, ELGG_VALUE_INTEGER))
181  ->set('owner_guid', $qb->param($annotation->owner_guid, ELGG_VALUE_INTEGER))
182  ->where($qb->compare('id', '=', $annotation->id, ELGG_VALUE_INTEGER));
183 
184  $result = $this->db->updateData($qb);
185 
186  if ($result === false) {
187  return false;
188  }
189 
190  $this->events->trigger('update', 'annotation', $annotation);
191  $this->events->triggerAfter('update', 'annotation', $annotation);
192 
193  return $result;
194  }
195 
204  public function disable(ElggAnnotation $annotation) {
205  if ($annotation->enabled == 'no') {
206  return true;
207  }
208 
209  if (!$annotation->canEdit()) {
210  return false;
211  }
212 
213  if (!_elgg_services()->events->trigger('disable', $annotation->getType(), $annotation)) {
214  return false;
215  }
216 
217  if ($annotation->id) {
218  $qb = Update::table('annotations');
219  $qb->set('enabled', $qb->param('no', ELGG_VALUE_STRING))
220  ->where($qb->compare('id', '=', $annotation->id, ELGG_VALUE_INTEGER));
221 
222  if (!$this->db->updateData($qb)) {
223  return false;
224  }
225  }
226 
227  $annotation->enabled = 'no';
228 
229  return true;
230  }
231 
240  public function enable(ElggAnnotation $annotation) {
241  if ($annotation->enabled == 'yes') {
242  return true;
243  }
244 
245  if (!$annotation->canEdit()) {
246  return false;
247  }
248 
249  if (!$this->events->trigger('enable', $annotation->getType(), $annotation)) {
250  return false;
251  }
252 
253  if ($annotation->id) {
254  $qb = Update::table('annotations');
255  $qb->set('enabled', $qb->param('yes', ELGG_VALUE_STRING))
256  ->where($qb->compare('id', '=', $annotation->id, ELGG_VALUE_INTEGER));
257 
258  if (!$this->db->updateData($qb)) {
259  return false;
260  }
261  }
262 
263  $annotation->enabled = 'yes';
264 
265  return true;
266  }
267 
277  public function find(array $options = []) {
278  $options['metastring_type'] = 'annotations';
279  $options = LegacyQueryOptionsAdapter::normalizeMetastringOptions($options);
280 
281  return Annotations::find($options);
282  }
283 
298  public function deleteAll(array $options) {
299  if (!_elgg_is_valid_options_for_batch_operation($options, 'annotation')) {
300  return false;
301  }
302 
303  $options['batch'] = true;
304  $options['batch_size'] = 50;
305  $options['batch_inc_offset'] = false;
306 
307  $annotations = Annotations::find($options);
308  $count = $annotations->count();
309 
310  if (!$count) {
311  return;
312  }
313 
314  $success = 0;
315  foreach ($annotations as $annotation) {
316  if ($annotation->delete()) {
317  $success++;
318  }
319  }
320 
321  return $success == $count;
322  }
323 
332  public function disableAll(array $options) {
333  if (!_elgg_is_valid_options_for_batch_operation($options, 'annotation')) {
334  return false;
335  }
336 
337  // if we can see hidden (disabled) we need to use the offset
338  // otherwise we risk an infinite loop if there are more than 50
339  $inc_offset = _elgg_services()->session->getDisabledEntityVisibility();
340 
341  $options['batch'] = true;
342  $options['batch_size'] = 50;
343  $options['batch_inc_offset'] = $inc_offset;
344 
345  $annotations = Annotations::find($options);
346  $count = $annotations->count();
347 
348  if (!$count) {
349  return;
350  }
351 
352  $success = 0;
353  foreach ($annotations as $annotation) {
354  if ($annotation->disable()) {
355  $success++;
356  }
357  }
358 
359  return $success == $count;
360  }
361 
373  public function enableAll(array $options) {
374  if (!_elgg_is_valid_options_for_batch_operation($options, 'annotation')) {
375  return false;
376  }
377 
378  $options['batch'] = true;
379  $options['batch_size'] = 50;
380 
381  $annotations = Annotations::find($options);
382  $count = $annotations->count();
383 
384  if (!$count) {
385  return;
386  }
387 
388  $success = 0;
389  foreach ($annotations as $annotation) {
390  if ($annotation->enable()) {
391  $success++;
392  }
393  }
394 
395  return $success == $count;
396  }
397 
407  public function exists($entity_guid, $name, $owner_guid) {
408  if (!$owner_guid) {
409  return false;
410  }
411 
412  $qb = Select::fromTable('annotations');
413  $qb->select('id');
414  $qb->where($qb->compare('owner_guid', '=', $owner_guid, ELGG_VALUE_INTEGER))
415  ->andWhere($qb->compare('entity_guid', '=', $entity_guid, ELGG_VALUE_INTEGER))
416  ->andWhere($qb->compare('name', '=', $name, ELGG_VALUE_STRING));
417 
418  $result = $this->db->getDataRow($qb);
419 
420  return !empty($result) && $result->id;
421  }
422 }
getType()
Return a type of extension.
$deleted
Definition: delete.php:25
disable(ElggAnnotation $annotation)
Disable the annotation.
disableAll(array $options)
Disables annotations based on $options.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
exists($entity_guid, $name, $owner_guid)
Check to see if a user has already created an annotation on an object.
_elgg_is_valid_options_for_batch_operation($options, $type)
Checks if there are some constraints on the options array for potentially dangerous operations...
Definition: elgglib.php:1401
$annotation
Elgg default annotation view.
Definition: default.php:10
static find(array $options=[])
Build and execute a new query from an array of legacy options.
Definition: Repository.php:85
static table($table, $alias=null)
{}
Definition: Update.php:13
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:138
enable(ElggAnnotation $annotation)
Enable the annotation.
elgg_delete_river(array $options=[])
Delete river items based on $options.
Definition: river.php:220
__construct(Database $db, EventsService $events)
Constructor.
Events service.
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:27
Builds queries for matching annotations against their properties.
$options
Elgg admin footer.
Definition: footer.php:6
$entity_guid
Definition: save.php:9
create(ElggAnnotation $annotation, ElggEntity $entity)
Create a new annotation and return its ID.
update(ElggAnnotation $annotation)
Store updated annotation in the database.
canEdit($user_guid=0)
Determines whether or not the user can edit this annotation.
$owner_guid
static intoTable($table)
{}
Definition: Insert.php:13
$id
River item delete action.
Definition: delete.php:6
$entity
Definition: reset.php:8
elgg_delete_annotation_by_id($id)
Deletes an annotation using its ID.
Definition: annotations.php:27
deleteAll(array $options)
Deletes annotations based on $options.
$time_created
Definition: online.php:19
find(array $options=[])
Returns annotations.
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
const ELGG_VALUE_STRING
Definition: constants.php:139
getType()
Returns the entity type.
if(elgg_in_context('widget')) $count
Definition: pagination.php:21
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
Interfaces with the database to perform CRUD operations on annotations.
enableAll(array $options)
Enables annotations based on $options.
trait TimeUsing
Adds methods for setting the current time (for testing)
Definition: TimeUsing.php:12
elgg ElggEntity
Definition: ElggEntity.js:15
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13