Elgg  Version 1.9
annotations.php
Go to the documentation of this file.
1 <?php
19  if (!($row instanceof stdClass)) {
20  // @todo should throw in this case?
21  return $row;
22  }
23 
24  return new ElggAnnotation($row);
25 }
26 
37  return _elgg_get_metastring_based_object_from_id($id, 'annotation');
38 }
39 
48  if (!$annotation) {
49  return false;
50  }
51  return $annotation->delete();
52 }
53 
66 function create_annotation($entity_guid, $name, $value, $value_type = '',
67 $owner_guid = 0, $access_id = ACCESS_PRIVATE) {
69 
70  $result = false;
71 
73  $name = trim($name);
74  $value_type = detect_extender_valuetype($value, $value_type);
75 
76  $owner_guid = (int)$owner_guid;
77  if ($owner_guid == 0) {
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_trigger_event('annotate', $entity->type, $entity)) {
99  $result = insert_data("INSERT INTO {$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_trigger_event('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($annotation_id, $name, $value, $value_type, $owner_guid, $access_id) {
131  global $CONFIG;
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) {
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 = update_data("UPDATE {$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_trigger_event('update', 'annotation', $obj);
172  }
173 
174  return $result;
175 }
176 
206 function elgg_get_annotations(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  $defaults = array(
234  'limit' => 25,
235  'offset' => (int) max(get_input('annoff', 0), 0),
236  'no_results' => '',
237  );
238 
239  $options = array_merge($defaults, $options);
240 
241  return elgg_list_entities($options, 'elgg_get_annotations', 'elgg_view_annotation_list');
242 }
243 
256  if (!_elgg_is_valid_options_for_batch_operation($options, 'annotation')) {
257  return false;
258  }
259 
260  $options['metastring_type'] = 'annotations';
261  return _elgg_batch_metastring_based_objects($options, 'elgg_batch_delete_callback', false);
262 }
263 
274  if (!_elgg_is_valid_options_for_batch_operation($options, 'annotation')) {
275  return false;
276  }
277 
278  // if we can see hidden (disabled) we need to use the offset
279  // otherwise we risk an infinite loop if there are more than 50
280  $inc_offset = access_get_show_hidden_status();
281 
282  $options['metastring_type'] = 'annotations';
283  return _elgg_batch_metastring_based_objects($options, 'elgg_batch_disable_callback', $inc_offset);
284 }
285 
299  if (!$options || !is_array($options)) {
300  return false;
301  }
302 
303  $options['metastring_type'] = 'annotations';
304  return _elgg_batch_metastring_based_objects($options, 'elgg_batch_enable_callback');
305 }
306 
343 function elgg_get_entities_from_annotations(array $options = array()) {
344  $defaults = array(
345  'annotation_names' => ELGG_ENTITIES_ANY_VALUE,
346  'annotation_values' => ELGG_ENTITIES_ANY_VALUE,
347  'annotation_name_value_pairs' => ELGG_ENTITIES_ANY_VALUE,
348 
349  'annotation_name_value_pairs_operator' => 'AND',
350  'annotation_case_sensitive' => true,
351  'order_by_annotation' => array(),
352 
353  'annotation_created_time_lower' => ELGG_ENTITIES_ANY_VALUE,
354  'annotation_created_time_upper' => ELGG_ENTITIES_ANY_VALUE,
355 
356  'annotation_owner_guids' => ELGG_ENTITIES_ANY_VALUE,
357 
358  'order_by' => 'maxtime DESC',
359  'group_by' => 'a.entity_guid'
360  );
361 
362  $options = array_merge($defaults, $options);
363 
364  $singulars = array('annotation_name', 'annotation_value',
365  'annotation_name_value_pair', 'annotation_owner_guid');
366 
369 
370  if (!$options) {
371  return false;
372  }
373 
374  // special sorting for annotations
375  //@todo overrides other sorting
376  $options['selects'][] = "MAX(n_table.time_created) AS maxtime";
377  $options['group_by'] = 'n_table.entity_guid';
378 
379  $time_wheres = _elgg_get_entity_time_where_sql('a', $options['annotation_created_time_upper'],
380  $options['annotation_created_time_lower']);
381 
382  if ($time_wheres) {
383  $options['wheres'] = array_merge($options['wheres'], $time_wheres);
384  }
385 
387 }
388 
400  return elgg_list_entities($options, 'elgg_get_entities_from_annotations');
401 }
402 
425  $db_prefix = elgg_get_config('dbprefix');
426  $defaults = array(
427  'calculation' => 'sum',
428  'order_by' => 'annotation_calculation desc'
429  );
430 
431  $options = array_merge($defaults, $options);
432 
433  $function = sanitize_string(elgg_extract('calculation', $options, 'sum', false));
434 
435  // you must cast this as an int or it sorts wrong.
436  $options['selects'][] = 'e.*';
437  $options['selects'][] = "$function(CAST(a_msv.string AS signed)) AS annotation_calculation";
438 
439  // need our own join to get the values because the lower level functions don't
440  // add all the joins if it's a different callback.
441  $options['joins'][] = "JOIN {$db_prefix}metastrings a_msv ON n_table.value_id = a_msv.id";
442 
443  // don't need access control because it's taken care of by elgg_get_annotations.
444  $options['group_by'] = 'n_table.entity_guid';
445 
446  $options['callback'] = 'entity_row_to_elggstar';
447 
448  // see #4393
449  // @todo remove after the 'count' shortcut is removed from elgg_get_annotations()
450  $options['__egefac'] = true;
451 
453 }
454 
465  $defaults = array(
466  'calculation' => 'sum',
467  'order_by' => 'annotation_calculation desc'
468  );
469  $options = array_merge($defaults, $options);
470 
471  return elgg_list_entities($options, 'elgg_get_entities_from_annotation_calculation');
472 }
473 
484 function elgg_annotation_exists($entity_guid, $annotation_type, $owner_guid = null) {
485  global $CONFIG;
486 
488  return false;
489  }
490 
493  $annotation_type = sanitize_string($annotation_type);
494 
495  $sql = "SELECT a.id FROM {$CONFIG->dbprefix}annotations a" .
496  " JOIN {$CONFIG->dbprefix}metastrings m ON a.name_id = m.id" .
497  " WHERE a.owner_guid = $owner_guid AND a.entity_guid = $entity_guid" .
498  " AND m.string = '$annotation_type'";
499 
500  if (get_data_row($sql)) {
501  return true;
502  }
503 
504  return false;
505 }
506 
518  $annotation = $params['extender'];
519  /* @var ElggExtender $annotation */
520  if ($annotation->getSubtype() == 'generic_comment') {
521  $entity = $annotation->getEntity();
522  if ($entity) {
523  return $entity->getURL() . '#item-annotation-' . $annotation->id;
524  }
525  }
526 }
527 
537 function _elgg_annotations_test($hook, $type, $tests) {
538  global $CONFIG;
539  $tests[] = $CONFIG->path . 'engine/tests/ElggCoreAnnotationAPITest.php';
540  $tests[] = $CONFIG->path . 'engine/tests/ElggAnnotationTest.php';
541  return $tests;
542 }
543 
549  elgg_register_plugin_hook_handler('extender:url', 'annotation', '_elgg_set_comment_url');
550  elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_annotations_test');
551 }
552 
553 elgg_register_event_handler('init', 'system', '_elgg_annotations_init');
_elgg_entities_get_metastrings_options($type, $options)
Returns options to pass to elgg_get_entities() for metastrings operations.
_elgg_annotations_init()
Initialize the annotation library private.
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
_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())
ElggEntities interfaces.
Definition: metadata.php:431
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
elgg_get_metastring_id($string, $case_sensitive=true)
Gets the metastring identifier for a value.
Definition: metastrings.php:34
get_data_row($query, $callback="")
Retrieve a single row from the database.
Definition: database.php:66
_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:1877
$annotation
Elgg default annotation view.
Definition: default.php:10
create_annotation($entity_guid, $name, $value, $value_type= '', $owner_guid=0, $access_id=ACCESS_PRIVATE)
Create a new annotation.
Definition: annotations.php:66
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
elgg_delete_annotations(array $options)
Deletes annotations based on $options.
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:1287
$value
Definition: longtext.php:29
Elgg Annotations.
row_to_elggannotation($row)
Convert a database row to a new ElggAnnotation.
Definition: annotations.php:18
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:1464
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Register a callback as a plugin hook handler.
Definition: elgglib.php:853
update_annotation($annotation_id, $name, $value, $value_type, $owner_guid, $access_id)
Update an annotation.
$url
Definition: exceptions.php:24
update_data($query)
Update a row in the database.
Definition: database.php:93
_elgg_annotations_test($hook, $type, $tests)
Register annotation unit tests.
sanitize_string($string)
Sanitize a string for database use.
Definition: database.php:140
_elgg_set_comment_url($hook, $type, $url, $params)
Set the URL for a comment when called from a plugin hook.
insert_data($query)
Insert a row into the database.
Definition: database.php:80
$params
Definition: login.php:72
$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
elgg_disable_annotations(array $options)
Disables annotations based on $options.
$owner_guid
elgg_list_annotations($options)
Returns a rendered list of annotations with pagination.
elgg_get_annotation_from_id($id)
Get a specific annotation by its id.
Definition: annotations.php:36
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.
global $CONFIG
elgg_get_entities_from_annotation_calculation($options)
Get entities ordered by a mathematical calculation on annotation values.
_elgg_get_metastring_based_objects($options)
Returns an array of either ElggAnnotation or ElggMetadata objects.
const ACCESS_PRIVATE
Definition: elgglib.php:2121
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:2134
elgg_enable_annotations(array $options)
Enables annotations based on $options.
elgg global
Pointer to the global context.
Definition: elgglib.js:12
$type
Definition: add.php:8
access_get_show_hidden_status()
Return current status of showing disabled entities.
Definition: access.php:299
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Register a callback as an Elgg event handler.
Definition: elgglib.php:669
elgg_list_entities(array $options=array(), $getter= 'elgg_get_entities', $viewer= 'elgg_view_entity_list')
Returns a string of rendered entities.
Definition: entities.php:1343
elgg_list_entities_from_annotations($options=array())
Returns a viewable list of entities from annotations.
elgg_get_annotations(array $options=array())
Returns annotations.
$row
_elgg_normalize_plural_options_array($options, $singulars)
Normalise the singular keys in an options array to plural keys.
Definition: elgglib.php:1594
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
elgg_trigger_event($event, $object_type, $object=null)
Trigger an Elgg Event and attempt to run all handler callbacks registered to that event...
Definition: elgglib.php:720
elgg_annotation_exists($entity_guid, $annotation_type, $owner_guid=null)
Check to see if a user has already created an annotation on an object.
if(!$collection_name) $id
Definition: add.php:17
elgg_list_entities_from_annotation_calculation($options)
List entities from an annotation calculation.
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:42
if(!$num_display) $db_prefix
Definition: content.php:12
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:604