Elgg  Version 6.1
Annotations.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Traits\Entity;
4 
10 trait Annotations {
11 
18  protected array $temp_annotations = [];
19 
31  public function deleteAnnotations(string $name = null): bool {
32  if ($this->guid) {
34  'guid' => $this->guid,
35  'limit' => false,
36  'annotation_name' => $name,
37  ]);
38  }
39 
40  if ($name) {
41  unset($this->temp_annotations[$name]);
42  } else {
43  $this->temp_annotations = [];
44  }
45 
46  return true;
47  }
48 
58  public function deleteOwnedAnnotations(string $name = null): bool {
59  // access is turned off for this because they might
60  // no longer have access to an entity they created annotations on
61  return elgg_call(ELGG_IGNORE_ACCESS, function() use ($name) {
63  'annotation_owner_guid' => $this->guid,
64  'limit' => false,
65  'annotation_name' => $name,
66  ]);
67  });
68  }
69 
78  private function getAnnotationCalculation(string $name, string $calculation): mixed {
79  return elgg_get_annotations([
80  'guid' => $this->guid,
81  'distinct' => false,
82  'annotation_name' => $name,
83  'annotation_calculation' => $calculation
84  ]);
85  }
86 
106  public function annotate($name, $value, $access_id = ACCESS_PRIVATE, $owner_guid = 0, $value_type = '') {
107  if (!$this->guid) {
108  $this->temp_annotations[$name] = $value;
109  return true;
110  }
111 
112  if (!$owner_guid) {
113  $owner_guid = _elgg_services()->session_manager->getLoggedInUserGuid();
114  }
115 
116  $annotation = new \ElggAnnotation();
117  $annotation->entity_guid = $this->guid;
118  $annotation->name = $name;
119  $annotation->value = $value;
120  $annotation->owner_guid = $owner_guid;
121  $annotation->access_id = $access_id;
122 
123  if (!empty($value_type)) {
124  $annotation->value_type = $value_type;
125  }
126 
127  if ($annotation->save()) {
128  return $annotation->id;
129  }
130 
131  return false;
132  }
133 
145  public function getAnnotations(array $options = []) {
146  if ($this->guid) {
147  $options['guid'] = $this->guid;
148 
150  } else {
151  $name = elgg_extract('annotation_name', $options, '');
152 
153  if (isset($this->temp_annotations[$name])) {
154  return [$this->temp_annotations[$name]];
155  }
156  }
157 
158  return [];
159  }
160 
168  public function countAnnotations(string $name = ''): int {
169  return $this->getAnnotationCalculation($name, 'count');
170  }
171 
179  public function getAnnotationsAvg(string $name) {
180  return $this->getAnnotationCalculation($name, 'avg');
181  }
182 
190  public function getAnnotationsSum(string $name) {
191  return $this->getAnnotationCalculation($name, 'sum');
192  }
193 
201  public function getAnnotationsMin(string $name) {
202  return $this->getAnnotationCalculation($name, 'min');
203  }
204 
212  public function getAnnotationsMax(string $name) {
213  return $this->getAnnotationCalculation($name, 'max');
214  }
215 }
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags...
Definition: elgglib.php:306
if($id< 1) $annotation
Definition: delete.php:11
elgg_delete_annotations(array $options)
Deletes annotations based on $options.
Definition: annotations.php:85
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
elgg_get_annotations(array $options=[])
Fetch annotations or perform a calculation on them.
Definition: annotations.php:50
countAnnotations(string $name= '')
Count annotations.
getAnnotationsMin(string $name)
Get the minimum of integer type annotations of given name.
$value
Definition: generic.php:51
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:256
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:121
$owner_guid
const ACCESS_PRIVATE
Definition: constants.php:10
annotate($name, $value, $access_id=ACCESS_PRIVATE, $owner_guid=0, $value_type= '')
Adds an annotation to an entity.
getAnnotationsMax(string $name)
Get the maximum of integer type annotations of a given name.
getAnnotationsAvg(string $name)
Get the average of an integer type annotation.
getAnnotations(array $options=[])
Gets an array of annotations.
getAnnotationsSum(string $name)
Get the sum of integer type annotations of a given name.
$access_id
Definition: access.php:10
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
deleteAnnotations(string $name=null)
Deletes all annotations on this object (annotations.entity_guid = $this->guid).
Definition: Annotations.php:31
deleteOwnedAnnotations(string $name=null)
Deletes all annotations owned by this object (annotations.owner_guid = $this->guid).
Definition: Annotations.php:58
$guid
Reset an ElggUpgrade.
Definition: reset.php:6