Elgg  Version 1.11
Annotations.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Database;
3 
13 class Annotations {
19  private $CONFIG;
20 
24  public function __construct() {
25  global $CONFIG;
26  $this->CONFIG = $CONFIG;
27  }
28 
38  function get($id) {
39  return _elgg_get_metastring_based_object_from_id($id, 'annotation');
40  }
41 
48  function delete($id) {
50  if (!$annotation) {
51  return false;
52  }
53  return $annotation->delete();
54  }
55 
68  function create($entity_guid, $name, $value, $value_type = '', $owner_guid = 0, $access_id = ACCESS_PRIVATE) {
69 
70 
71  $result = false;
72 
74  $value_type = detect_extender_valuetype($value, $value_type);
75 
76  $owner_guid = (int)$owner_guid;
77  if ($owner_guid == 0) {
78  $owner_guid = _elgg_services()->session->getLoggedInUserGuid();
79  }
80 
81  $access_id = (int)$access_id;
82  $time = time();
83 
84  $value_id = elgg_get_metastring_id($value);
85  if (!$value_id) {
86  return false;
87  }
88 
89  $name_id = elgg_get_metastring_id($name);
90  if (!$name_id) {
91  return false;
92  }
93 
94  // @todo we don't check that the entity is loaded which means the user may
95  // not have access to the entity
97 
98  if (_elgg_services()->events->trigger('annotate', $entity->type, $entity)) {
99  $result = _elgg_services()->db->insertData("INSERT INTO {$this->CONFIG->dbprefix}annotations
100  (entity_guid, name_id, value_id, value_type, owner_guid, time_created, access_id) VALUES
101  ($entity_guid, $name_id, $value_id, '$value_type', $owner_guid, $time, $access_id)");
102 
103  if ($result !== false) {
105  if (_elgg_services()->events->trigger('create', 'annotation', $obj)) {
106  return $result;
107  } else {
108  // plugin returned false to reject annotation
110  return false;
111  }
112  }
113  }
114 
115  return $result;
116  }
117 
130  function update($annotation_id, $name, $value, $value_type, $owner_guid, $access_id) {
131 
132 
133  $annotation_id = (int)$annotation_id;
134 
135  $annotation = elgg_get_annotation_from_id($annotation_id);
136  if (!$annotation) {
137  return false;
138  }
139  if (!$annotation->canEdit()) {
140  return false;
141  }
142 
143  $name = trim($name);
144  $value_type = detect_extender_valuetype($value, $value_type);
145 
146  $owner_guid = (int)$owner_guid;
147  if ($owner_guid == 0) {
148  $owner_guid = _elgg_services()->session->getLoggedInUserGuid();
149  }
150 
151  $access_id = (int)$access_id;
152 
153  $value_id = elgg_get_metastring_id($value);
154  if (!$value_id) {
155  return false;
156  }
157 
158  $name_id = elgg_get_metastring_id($name);
159  if (!$name_id) {
160  return false;
161  }
162 
163  $result = _elgg_services()->db->updateData("UPDATE {$this->CONFIG->dbprefix}annotations
164  SET name_id = $name_id, value_id = $value_id, value_type = '$value_type',
165  access_id = $access_id, owner_guid = $owner_guid
166  WHERE id = $annotation_id");
167 
168  if ($result !== false) {
169  // @todo add plugin hook that sends old and new annotation information before db access
170  $obj = elgg_get_annotation_from_id($annotation_id);
171  _elgg_services()->events->trigger('update', 'annotation', $obj);
172  }
173 
174  return $result;
175  }
176 
205  function find(array $options = array()) {
206 
207  // support shortcut of 'count' => true for 'annotation_calculation' => 'count'
208  if (isset($options['count']) && $options['count']) {
209  $options['annotation_calculation'] = 'count';
210  unset($options['count']);
211  }
212 
213  $options['metastring_type'] = 'annotations';
215  }
216 
227  function deleteAll(array $options) {
228  if (!_elgg_is_valid_options_for_batch_operation($options, 'annotation')) {
229  return false;
230  }
231 
232  $options['metastring_type'] = 'annotations';
233  return _elgg_batch_metastring_based_objects($options, 'elgg_batch_delete_callback', false);
234  }
235 
244  function disableAll(array $options) {
245  if (!_elgg_is_valid_options_for_batch_operation($options, 'annotation')) {
246  return false;
247  }
248 
249  // if we can see hidden (disabled) we need to use the offset
250  // otherwise we risk an infinite loop if there are more than 50
251  $inc_offset = access_get_show_hidden_status();
252 
253  $options['metastring_type'] = 'annotations';
254  return _elgg_batch_metastring_based_objects($options, 'elgg_batch_disable_callback', $inc_offset);
255  }
256 
268  function enableAll(array $options) {
269  if (!$options || !is_array($options)) {
270  return false;
271  }
272 
273  $options['metastring_type'] = 'annotations';
274  return _elgg_batch_metastring_based_objects($options, 'elgg_batch_enable_callback');
275  }
276 
309  function getEntities(array $options = array()) {
310  $defaults = array(
311  'annotation_names' => ELGG_ENTITIES_ANY_VALUE,
312  'annotation_values' => ELGG_ENTITIES_ANY_VALUE,
313  'annotation_name_value_pairs' => ELGG_ENTITIES_ANY_VALUE,
314 
315  'annotation_name_value_pairs_operator' => 'AND',
316  'annotation_case_sensitive' => true,
317  'order_by_annotation' => array(),
318 
319  'annotation_created_time_lower' => ELGG_ENTITIES_ANY_VALUE,
320  'annotation_created_time_upper' => ELGG_ENTITIES_ANY_VALUE,
321 
322  'annotation_owner_guids' => ELGG_ENTITIES_ANY_VALUE,
323  );
324 
325  $options = array_merge($defaults, $options);
326 
327  $singulars = array('annotation_name', 'annotation_value',
328  'annotation_name_value_pair', 'annotation_owner_guid');
329 
332 
333  if (!$options) {
334  return false;
335  }
336 
337  // because of performance issues support for ordering by maxtime has been dropped
338  // @see https://github.com/Elgg/Elgg/issues/6638
339  if (isset($options['order_by']) && preg_match('~\bmaxtime\b~i', $options['order_by'])) {
340  // check if the user provided maxtime
341  $deprecated = true;
342  if (isset($options['selects'])) {
343  $selects = $options['selects'];
344  if (!is_array($selects)) {
345  $selects = array($selects);
346  }
347 
348  foreach ($selects as $select) {
349  if (preg_match('~\bmaxtime\b~i', $options['order_by'])) {
350  $deprecated = false;
351  break;
352  }
353  }
354  }
355 
356  // the user didn't provide maxtime
357  if ($deprecated) {
358  // special sorting for annotations
359  elgg_deprecated_notice(__FUNCTION__ . ": no longer orders by annotations by default. If you order"
360  . " by maxtime, you must provide that column via \$options['selects']. See"
361  . " https://github.com/Elgg/Elgg/issues/6638#issuecomment-41562034", "1.10");
362 
363  $options['selects'][] = "MAX(n_table.time_created) AS maxtime";
364  $options['group_by'] = 'n_table.entity_guid';
365  }
366  }
367 
368  $time_wheres = _elgg_get_entity_time_where_sql('a', $options['annotation_created_time_upper'],
369  $options['annotation_created_time_lower']);
370 
371  if ($time_wheres) {
372  $options['wheres'] = array_merge($options['wheres'], $time_wheres);
373  }
374 
376  }
377 
411 
412  if (isset($options['count']) && $options['count']) {
414  }
415 
416  $db_prefix = _elgg_services()->config->get('dbprefix');
417  $defaults = array(
418  'calculation' => 'sum',
419  'order_by' => 'annotation_calculation desc'
420  );
421 
422  $options = array_merge($defaults, $options);
423 
424  $function = sanitize_string(elgg_extract('calculation', $options, 'sum', false));
425 
426  // you must cast this as an int or it sorts wrong.
427  $options['selects'][] = 'e.*';
428  $options['selects'][] = "$function(CAST(a_msv.string AS signed)) AS annotation_calculation";
429 
430  // need our own join to get the values because the lower level functions don't
431  // add all the joins if it's a different callback.
432  $options['joins'][] = "JOIN {$db_prefix}metastrings a_msv ON n_table.value_id = a_msv.id";
433 
434  // don't need access control because it's taken care of by elgg_get_annotations.
435  $options['group_by'] = 'n_table.entity_guid';
436 
437  // do not default to a callback function used in elgg_get_annotation()
438  if (!isset($options['callback'])) {
439  $options['callback'] = 'entity_row_to_elggstar';
440  }
441 
443  }
444 
454  function exists($entity_guid, $annotation_type, $owner_guid = null) {
455 
456 
457  if (!$owner_guid && !($owner_guid = _elgg_services()->session->getLoggedInUserGuid())) {
458  return false;
459  }
460 
463  $annotation_type = sanitize_string($annotation_type);
464 
465  $sql = "SELECT a.id FROM {$this->CONFIG->dbprefix}annotations a" .
466  " JOIN {$this->CONFIG->dbprefix}metastrings m ON a.name_id = m.id" .
467  " WHERE a.owner_guid = $owner_guid AND a.entity_guid = $entity_guid" .
468  " AND m.string = '$annotation_type'";
469 
470  if (_elgg_services()->db->getDataRow($sql)) {
471  return true;
472  }
473 
474  return false;
475  }
476 }
_elgg_entities_get_metastrings_options($type, $options)
Returns options to pass to elgg_get_entities() for metastrings operations.
_elgg_batch_metastring_based_objects(array $options, $callback, $inc_offset=true)
Runs metastrings-based objects found using $options through $callback.
elgg_get_entities_from_metadata(array $options=array())
interfaces
Definition: metadata.php:255
getEntities(array $options=array())
Returns entities based upon annotations.
elgg_get_metastring_id($string, $case_sensitive=true)
Gets the metastring identifier for a value.
Definition: metastrings.php:25
_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:1687
$annotation
Elgg default annotation view.
Definition: default.php:10
deleteAll(array $options)
Deletes annotations based on $options.
__construct()
Constructor.
Definition: Annotations.php:24
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
$defaults
elgg_get_entities_from_annotations(array $options=array())
Returns entities based upon annotations.
_elgg_get_entity_time_where_sql($table, $time_created_upper=null, $time_created_lower=null, $time_updated_upper=null, $time_updated_lower=null)
Returns SQL where clause for entity time limits.
Definition: entities.php:553
create($entity_guid, $name, $value, $value_type= '', $owner_guid=0, $access_id=ACCESS_PRIVATE)
Create a new annotation.
Definition: Annotations.php:68
$value
Definition: longtext.php:26
elgg_extract($key, array $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1246
events($event="", $object_type="", $function="", $priority=500, $call=false, $object=null)
Deprecated events core function.
disableAll(array $options)
Disables annotations based on $options.
sanitize_string($string)
Sanitize a string for database use.
Definition: database.php:140
$entity_guid
Definition: save.php:9
detect_extender_valuetype($value, $value_type="")
Detect the value_type for a given value.
Definition: extender.php:21
$options
Definition: index.php:14
$owner_guid
elgg_get_annotation_from_id($id)
Get a specific annotation by its id.
Definition: annotations.php:36
exists($entity_guid, $annotation_type, $owner_guid=null)
Check to see if a user has already created an annotation on an object.
getEntitiesFromCalculation($options)
Get entities ordered by a mathematical calculation on annotation values.
elgg_delete_annotation_by_id($id)
Deletes an annotation using its ID.
Definition: annotations.php:46
_elgg_services()
Definition: autoloader.php:14
_elgg_get_metastring_based_object_from_id($id, $type)
Returns a singular metastring-based object by its ID.
_elgg_get_metastring_based_objects($options)
Returns an array of either or objects.
Definition: metastrings.php:76
const ACCESS_PRIVATE
Definition: elgglib.php:1954
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:1967
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1006
elgg global
Pointer to the global context.
Definition: elgglib.js:12
find(array $options=array())
Returns annotations.
access_get_show_hidden_status()
Return current status of showing disabled entities.
Definition: access.php:172
elgg_get_annotations(array $options=array())
Returns annotations.
update($annotation_id, $name, $value, $value_type, $owner_guid, $access_id)
Update an annotation.
_elgg_normalize_plural_options_array($options, $singulars)
Normalise the singular keys in an options array to plural keys.
Definition: elgglib.php:1376
sanitize_int($int, $signed=true)
Sanitizes an integer for database use.
Definition: database.php:161
$entity
Definition: delete.php:10
if(!$collection_name) $id
Definition: add.php:17
if(!$num_display) $db_prefix
Definition: content.php:12
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:382
enableAll(array $options)
Enables annotations based on $options.