Elgg  Version 1.10
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  $name = trim($name);
75  $value_type = detect_extender_valuetype($value, $value_type);
76 
77  $owner_guid = (int)$owner_guid;
78  if ($owner_guid == 0) {
79  $owner_guid = _elgg_services()->session->getLoggedInUserGuid();
80  }
81 
82  $access_id = (int)$access_id;
83  $time = time();
84 
85  $value_id = elgg_get_metastring_id($value);
86  if (!$value_id) {
87  return false;
88  }
89 
90  $name_id = elgg_get_metastring_id($name);
91  if (!$name_id) {
92  return false;
93  }
94 
95  // @todo we don't check that the entity is loaded which means the user may
96  // not have access to the entity
98 
99  if (_elgg_services()->events->trigger('annotate', $entity->type, $entity)) {
100  $result = _elgg_services()->db->insertData("INSERT INTO {$this->CONFIG->dbprefix}annotations
101  (entity_guid, name_id, value_id, value_type, owner_guid, time_created, access_id) VALUES
102  ($entity_guid, $name_id, $value_id, '$value_type', $owner_guid, $time, $access_id)");
103 
104  if ($result !== false) {
106  if (_elgg_services()->events->trigger('create', 'annotation', $obj)) {
107  return $result;
108  } else {
109  // plugin returned false to reject annotation
111  return false;
112  }
113  }
114  }
115 
116  return $result;
117  }
118 
131  function update($annotation_id, $name, $value, $value_type, $owner_guid, $access_id) {
132 
133 
134  $annotation_id = (int)$annotation_id;
135 
136  $annotation = elgg_get_annotation_from_id($annotation_id);
137  if (!$annotation) {
138  return false;
139  }
140  if (!$annotation->canEdit()) {
141  return false;
142  }
143 
144  $name = trim($name);
145  $value_type = detect_extender_valuetype($value, $value_type);
146 
147  $owner_guid = (int)$owner_guid;
148  if ($owner_guid == 0) {
149  $owner_guid = _elgg_services()->session->getLoggedInUserGuid();
150  }
151 
152  $access_id = (int)$access_id;
153 
154  $value_id = elgg_get_metastring_id($value);
155  if (!$value_id) {
156  return false;
157  }
158 
159  $name_id = elgg_get_metastring_id($name);
160  if (!$name_id) {
161  return false;
162  }
163 
164  $result = _elgg_services()->db->updateData("UPDATE {$this->CONFIG->dbprefix}annotations
165  SET name_id = $name_id, value_id = $value_id, value_type = '$value_type',
166  access_id = $access_id, owner_guid = $owner_guid
167  WHERE id = $annotation_id");
168 
169  if ($result !== false) {
170  // @todo add plugin hook that sends old and new annotation information before db access
171  $obj = elgg_get_annotation_from_id($annotation_id);
172  _elgg_services()->events->trigger('update', 'annotation', $obj);
173  }
174 
175  return $result;
176  }
177 
206  function find(array $options = array()) {
207 
208  // @todo remove support for count shortcut - see #4393
209  if (isset($options['__egefac']) && $options['__egefac']) {
210  unset($options['__egefac']);
211  } else {
212  // support shortcut of 'count' => true for 'annotation_calculation' => 'count'
213  if (isset($options['count']) && $options['count']) {
214  $options['annotation_calculation'] = 'count';
215  unset($options['count']);
216  }
217  }
218 
219  $options['metastring_type'] = 'annotations';
221  }
222 
233  function deleteAll(array $options) {
234  if (!_elgg_is_valid_options_for_batch_operation($options, 'annotation')) {
235  return false;
236  }
237 
238  $options['metastring_type'] = 'annotations';
239  return _elgg_batch_metastring_based_objects($options, 'elgg_batch_delete_callback', false);
240  }
241 
250  function disableAll(array $options) {
251  if (!_elgg_is_valid_options_for_batch_operation($options, 'annotation')) {
252  return false;
253  }
254 
255  // if we can see hidden (disabled) we need to use the offset
256  // otherwise we risk an infinite loop if there are more than 50
257  $inc_offset = access_get_show_hidden_status();
258 
259  $options['metastring_type'] = 'annotations';
260  return _elgg_batch_metastring_based_objects($options, 'elgg_batch_disable_callback', $inc_offset);
261  }
262 
274  function enableAll(array $options) {
275  if (!$options || !is_array($options)) {
276  return false;
277  }
278 
279  $options['metastring_type'] = 'annotations';
280  return _elgg_batch_metastring_based_objects($options, 'elgg_batch_enable_callback');
281  }
282 
315  function getEntities(array $options = array()) {
316  $defaults = array(
317  'annotation_names' => ELGG_ENTITIES_ANY_VALUE,
318  'annotation_values' => ELGG_ENTITIES_ANY_VALUE,
319  'annotation_name_value_pairs' => ELGG_ENTITIES_ANY_VALUE,
320 
321  'annotation_name_value_pairs_operator' => 'AND',
322  'annotation_case_sensitive' => true,
323  'order_by_annotation' => array(),
324 
325  'annotation_created_time_lower' => ELGG_ENTITIES_ANY_VALUE,
326  'annotation_created_time_upper' => ELGG_ENTITIES_ANY_VALUE,
327 
328  'annotation_owner_guids' => ELGG_ENTITIES_ANY_VALUE,
329  );
330 
331  $options = array_merge($defaults, $options);
332 
333  $singulars = array('annotation_name', 'annotation_value',
334  'annotation_name_value_pair', 'annotation_owner_guid');
335 
338 
339  if (!$options) {
340  return false;
341  }
342 
343  // because of performance issues support for ordering by maxtime has been dropped
344  // @see https://github.com/Elgg/Elgg/issues/6638
345  if (isset($options['order_by']) && preg_match('~\bmaxtime\b~i', $options['order_by'])) {
346  // check if the user provided maxtime
347  $deprecated = true;
348  if (isset($options['selects'])) {
349  $selects = $options['selects'];
350  if (!is_array($selects)) {
351  $selects = array($selects);
352  }
353 
354  foreach ($selects as $select) {
355  if (preg_match('~\bmaxtime\b~i', $options['order_by'])) {
356  $deprecated = false;
357  break;
358  }
359  }
360  }
361 
362  // the user didn't provide maxtime
363  if ($deprecated) {
364  // special sorting for annotations
365  elgg_deprecated_notice(__FUNCTION__ . ": no longer orders by annotations by default. If you order"
366  . " by maxtime, you must provide that column via \$options['selects']. See"
367  . " https://github.com/Elgg/Elgg/issues/6638#issuecomment-41562034", "1.10");
368 
369  $options['selects'][] = "MAX(n_table.time_created) AS maxtime";
370  $options['group_by'] = 'n_table.entity_guid';
371  }
372  }
373 
374  $time_wheres = _elgg_get_entity_time_where_sql('a', $options['annotation_created_time_upper'],
375  $options['annotation_created_time_lower']);
376 
377  if ($time_wheres) {
378  $options['wheres'] = array_merge($options['wheres'], $time_wheres);
379  }
380 
382  }
383 
406  $db_prefix = _elgg_services()->config->get('dbprefix');
407  $defaults = array(
408  'calculation' => 'sum',
409  'order_by' => 'annotation_calculation desc'
410  );
411 
412  $options = array_merge($defaults, $options);
413 
414  $function = sanitize_string(elgg_extract('calculation', $options, 'sum', false));
415 
416  // you must cast this as an int or it sorts wrong.
417  $options['selects'][] = 'e.*';
418  $options['selects'][] = "$function(CAST(a_msv.string AS signed)) AS annotation_calculation";
419 
420  // need our own join to get the values because the lower level functions don't
421  // add all the joins if it's a different callback.
422  $options['joins'][] = "JOIN {$db_prefix}metastrings a_msv ON n_table.value_id = a_msv.id";
423 
424  // don't need access control because it's taken care of by elgg_get_annotations.
425  $options['group_by'] = 'n_table.entity_guid';
426 
427  $options['callback'] = 'entity_row_to_elggstar';
428 
429  // see #4393
430  // @todo remove after the 'count' shortcut is removed from elgg_get_annotations()
431  $options['__egefac'] = true;
432 
434  }
435 
445  function exists($entity_guid, $annotation_type, $owner_guid = null) {
446 
447 
448  if (!$owner_guid && !($owner_guid = _elgg_services()->session->getLoggedInUserGuid())) {
449  return false;
450  }
451 
454  $annotation_type = sanitize_string($annotation_type);
455 
456  $sql = "SELECT a.id FROM {$this->CONFIG->dbprefix}annotations a" .
457  " JOIN {$this->CONFIG->dbprefix}metastrings m ON a.name_id = m.id" .
458  " WHERE a.owner_guid = $owner_guid AND a.entity_guid = $entity_guid" .
459  " AND m.string = '$annotation_type'";
460 
461  if (_elgg_services()->db->getDataRow($sql)) {
462  return true;
463  }
464 
465  return false;
466  }
467 }
_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:1784
$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
_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:29
if(!$autoload_available) _elgg_services()
Definition: autoloader.php:20
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:1349
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_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:2046
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:2059
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Sends a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1055
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:170
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:1479
sanitize_int($int, $signed=true)
Sanitizes an integer for database use.
Definition: database.php:161
$defaults
Definition: access.php:19
$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.