Elgg  Version master
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 {
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 }
$guid
Reset an ElggUpgrade.
Definition: reset.php:6
if(! $user||! $user->canDelete()) $name
Definition: delete.php:22
if($id< 1) $annotation
Definition: delete.php:11
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
elgg_delete_annotations(array $options)
Deletes annotations based on $options.
Definition: annotations.php:85
elgg_get_annotations(array $options=[])
Fetch annotations or perform a calculation on them.
Definition: annotations.php:50
$owner_guid
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:121
const ACCESS_PRIVATE
Definition: constants.php:10
if($who_can_change_language==='nobody') elseif($who_can_change_language==='admin_only' &&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
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
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
$value
Definition: generic.php:51
getAnnotationsAvg(string $name)
Get the average of an integer type annotation.
annotate($name, $value, $access_id=ACCESS_PRIVATE, $owner_guid=0, $value_type='')
Adds an annotation to an entity.
getAnnotations(array $options=[])
Gets an array of annotations.
getAnnotationsSum(string $name)
Get the sum of integer type annotations of a given name.
deleteAnnotations(?string $name=null)
Deletes all annotations on this object (annotations.entity_guid = $this->guid).
Definition: Annotations.php:31
countAnnotations(string $name='')
Count annotations.
trait Annotations
Bundle all annotations related functions for an \ElggEntity.
Definition: Annotations.php:10
getAnnotationsMin(string $name)
Get the minimum of integer type annotations of given name.
deleteOwnedAnnotations(?string $name=null)
Deletes all annotations owned by this object (annotations.owner_guid = $this->guid).
Definition: Annotations.php:58
getAnnotationsMax(string $name)
Get the maximum of integer type annotations of a given name.
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10
$access_id
Definition: access.php:10