Elgg  Version 6.1
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 
60  if (empty($this->guid)) {
61  // no way to return all temp annotations for an unsaved entity
62  $annotations = $this->getAnnotations([
63  'annotation_name' => "profile:{$profile_field_name}",
64  'limit' => false,
65  ]);
66  } else {
67  $annotations = elgg_extract("profile:{$profile_field_name}", $this->getAllProfileAnnotations());
68  }
69 
70  if (empty($annotations)) {
71  return null;
72  }
73 
74  if (!is_array($annotations)) {
75  $annotations = [$annotations];
76  }
77 
78  $result = [];
79  foreach ($annotations as $annotation) {
80  if ($annotation instanceof \ElggAnnotation) {
81  $result[] = $annotation->value;
82  continue;
83  }
84 
85  // non saved entity has annotation as pure value
86  $result[] = $annotation;
87  }
88 
89  if (count($result) === 1) {
90  return $result[0];
91  }
92 
93  return $result;
94  }
95 
101  protected function getAllProfileAnnotations(): array {
102  // store logged in user guid to prevent unwanted access to annotations when switching logged in user during script run (e.g. ElggCoreUserTest)
103  $logged_in_user_guid = elgg_get_logged_in_user_guid();
104  if (!isset($this->_profile_data[$logged_in_user_guid])) {
105  $annotations = $this->getAnnotations([
106  'limit' => false,
107  'wheres' => function(\Elgg\Database\QueryBuilder $qb, $main_alias) {
108  return $qb->compare("{$main_alias}.name", 'LIKE', 'profile:%', ELGG_VALUE_STRING);
109  },
110  ]);
111 
112  $profile_data = [];
113  foreach ($annotations as $annotation) {
114  if (!isset($profile_data[$annotation->name])) {
115  $profile_data[$annotation->name] = [];
116  }
117 
118  $profile_data[$annotation->name][] = $annotation;
119  }
120 
121  $this->_profile_data[$logged_in_user_guid] = $profile_data;
122  }
123 
124  return $this->_profile_data[$logged_in_user_guid];
125  }
126 
134  public function deleteProfileData(string $profile_field_name) {
135  $result = $this->deleteAnnotations("profile:{$profile_field_name}");
136  $result &= $this->deleteMetadata($profile_field_name);
137 
138  $this->_profile_data = null;
139 
140  return $result;
141  }
142 }
getAllProfileAnnotations()
Returns all profile annotations.
if($id< 1) $annotation
Definition: delete.php:11
setProfileData(string $profile_field_name, $value, int $access_id=ACCESS_PRIVATE)
Store profile data.
Definition: ProfileData.php:27
Entity Annotation.
Database abstraction query builder.
deleteMetadata(string $name=null)
Deletes all metadata on this object (metadata.entity_guid = $this->guid).
Definition: Metadata.php:198
$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
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.
getProfileData(string $profile_field_name)
Get profile data.
Definition: ProfileData.php:58
deleteProfileData(string $profile_field_name)
Remove profile data.
getAnnotations(array $options=[])
Gets an array of annotations.
const ELGG_VALUE_STRING
Definition: constants.php:112
$access_id
Definition: access.php:10
deleteAnnotations(string $name=null)
Deletes all annotations on this object (annotations.entity_guid = $this->guid).
Definition: Annotations.php:31
$qb
Definition: queue.php:12
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:34