Elgg  Version 6.3
ProfileData.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Traits\Entity;
4 
11 trait ProfileData {
12 
16  protected $_profile_data = [];
17 
27  public function setProfileData(string $profile_field_name, $value, int $access_id = ACCESS_PRIVATE) {
28  // remove old values
29  $this->deleteProfileData($profile_field_name);
30 
31  if (is_null($value) || $value === '') {
32  // don't try to store empty values
33  return;
34  }
35 
36  // store new value(s)
37  if (!is_array($value)) {
38  $value = [$value];
39  }
40 
41  foreach ($value as $v) {
42  $this->annotate("profile:{$profile_field_name}", $v, $access_id, $this->guid);
43  }
44 
45  $this->_profile_data = null;
46 
47  // for BC, keep storing fields in MD, but we'll read annotations only
48  $this->$profile_field_name = $value;
49  }
50 
58  public function getProfileData(string $profile_field_name) {
59  if (_elgg_services()->userCapabilities->canBypassPermissionsCheck()) {
60  // can use metadata for performance benefits if access is ignored
61  return $this->{$profile_field_name};
62  }
63 
64  if (empty($this->guid)) {
65  // no way to return all temp annotations for an unsaved entity
66  $annotations = $this->getAnnotations([
67  'annotation_name' => "profile:{$profile_field_name}",
68  'limit' => false,
69  ]);
70  } else {
71  $annotations = elgg_extract("profile:{$profile_field_name}", $this->getAllProfileAnnotations());
72  }
73 
74  if (empty($annotations)) {
75  return null;
76  }
77 
78  if (!is_array($annotations)) {
79  $annotations = [$annotations];
80  }
81 
82  $result = [];
83  foreach ($annotations as $annotation) {
84  if ($annotation instanceof \ElggAnnotation) {
85  $result[] = $annotation->value;
86  continue;
87  }
88 
89  // non saved entity has annotation as pure value
90  $result[] = $annotation;
91  }
92 
93  if (count($result) === 1) {
94  return $result[0];
95  }
96 
97  return $result;
98  }
99 
105  protected function getAllProfileAnnotations(): array {
106  // store logged in user guid to prevent unwanted access to annotations when switching logged in user during script run (e.g. ElggCoreUserTest)
107  $logged_in_user_guid = elgg_get_logged_in_user_guid();
108  if (!isset($this->_profile_data[$logged_in_user_guid])) {
109  $annotations = $this->getAnnotations([
110  'limit' => false,
111  'wheres' => function(\Elgg\Database\QueryBuilder $qb, $main_alias) {
112  return $qb->compare("{$main_alias}.name", 'LIKE', 'profile:%', ELGG_VALUE_STRING);
113  },
114  ]);
115 
116  $profile_data = [];
117  foreach ($annotations as $annotation) {
118  if (!isset($profile_data[$annotation->name])) {
119  $profile_data[$annotation->name] = [];
120  }
121 
122  $profile_data[$annotation->name][] = $annotation;
123  }
124 
125  $this->_profile_data[$logged_in_user_guid] = $profile_data;
126  }
127 
128  return $this->_profile_data[$logged_in_user_guid];
129  }
130 
138  public function deleteProfileData(string $profile_field_name) {
139  $result = $this->deleteAnnotations("profile:{$profile_field_name}");
140  $result &= $this->deleteMetadata($profile_field_name);
141 
142  $this->_profile_data = null;
143 
144  return $result;
145  }
146 }
if($id< 1) $annotation
Definition: delete.php:11
Entity Annotation.
Database abstraction query builder.
const ELGG_VALUE_STRING
Definition: constants.php:112
const ACCESS_PRIVATE
Definition: constants.php:10
_elgg_services()
Get the global service provider.
Definition: elgglib.php:337
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:240
$value
Definition: generic.php:51
deleteProfileData(string $profile_field_name)
Remove profile data.
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.
getAllProfileAnnotations()
Returns all profile annotations.
deleteAnnotations(?string $name=null)
Deletes all annotations on this object (annotations.entity_guid = $this->guid).
Definition: Annotations.php:31
getProfileData(string $profile_field_name)
Get profile data.
Definition: ProfileData.php:58
trait ProfileData
Adds methods to save profile data to an ElggEntity.
Definition: ProfileData.php:11
deleteMetadata(?string $name=null)
Deletes all metadata on this object (metadata.entity_guid = $this->guid).
Definition: Metadata.php:198
setProfileData(string $profile_field_name, $value, int $access_id=ACCESS_PRIVATE)
Store profile data.
Definition: ProfileData.php:27
$qb
Definition: queue.php:14
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:34
$access_id
Definition: access.php:10