Elgg  Version master
Metadata.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Traits\Entity;
4 
10 trait Metadata {
11 
18  protected array $temp_metadata = [];
19 
27  public function getMetadata(string $name): mixed {
28  $metadata = $this->getAllMetadata();
29  return elgg_extract($name, $metadata);
30  }
31 
37  public function getAllMetadata(): array {
38  if (!$this->guid) {
39  return array_map(function($values) {
40  return count($values) > 1 ? $values : $values[0];
41  }, $this->temp_metadata);
42  }
43 
44  $metadata = _elgg_services()->metadataCache->load($this->guid);
45  if ($metadata === null) {
46  $metadata = elgg_extract($this->guid, _elgg_services()->metadataCache->populateFromEntities($this->guid));
47  }
48 
49  if (empty($metadata)) {
50  return [];
51  }
52 
53  $metadata_values = [];
54  foreach ($metadata as $md) {
55  $metadata_values[$md->name][] = $md->value;
56  }
57 
58  return array_map(function($values) {
59  return count($values) > 1 ? $values : $values[0];
60  }, $metadata_values);
61  }
62 
78  public function setMetadata(string $name, mixed $value, string $value_type = '', bool $multiple = false): bool {
79  if ($value === null || $value === '' || $value === []) {
80  return $this->deleteMetadata($name);
81  }
82 
83  // normalize value to an array that we will loop over
84  // remove indexes if value already an array.
85  if (is_array($value)) {
86  $value = array_values(array_filter($value, function($var) {
87  // strip null and '' values from array
88  return !is_null($var) && $var !== '';
89  }));
90  } else {
91  $value = [$value];
92  }
93 
94  if (count($value) === 0) {
95  return $this->deleteMetadata($name);
96  }
97 
98  if (empty($this->guid)) {
99  // unsaved entity. store in temp array
100  return $this->setTempMetadata($name, $value, $multiple);
101  }
102 
103  // saved entity. persist md to db.
104  // disable metadatacache to always check the database to prevent racing conditions
105  $md_cache = _elgg_services()->metadataCache;
106  $md_cache_enabled = $md_cache->isEnabled();
107  $md_cache->disable();
108  $restore_md_cache = function() use ($md_cache, $md_cache_enabled) {
109  if ($md_cache_enabled) {
110  $md_cache->enable();
111  }
112  };
113 
114  if (!$multiple) {
115  // using getIDsByName to prevent populating the metadata cache
116  $existing_ids = _elgg_services()->metadataTable->getIDsByName($this->guid, $name);
117 
118  if ((is_array($existing_ids) || count($value) > 1) && isset($existing_ids)) {
119  // remove current metadata if needed
120  if (!$this->deleteMetadata($name)) {
121  $restore_md_cache();
122 
123  return false;
124  }
125  }
126  }
127 
128  if (count($value) > 1) {
129  // new value is a multiple valued metadata
130  $multiple = true;
131  }
132 
133  // create new metadata
134  foreach ($value as $value_tmp) {
135  $metadata = new \ElggMetadata();
136  $metadata->entity_guid = $this->guid;
137  $metadata->name = $name;
138  $metadata->value = $value_tmp;
139 
140  if (!empty($value_type)) {
141  $metadata->value_type = $value_type;
142  }
143 
144  if (_elgg_services()->metadataTable->create($metadata, $multiple) === false) {
145  $restore_md_cache();
146 
147  return false;
148  }
149  }
150 
151  $restore_md_cache();
152 
153  return true;
154  }
155 
166  protected function setTempMetadata(string $name, mixed $value, bool $multiple = false): bool {
167  // if overwrite, delete first
168  if (!$multiple) {
169  unset($this->temp_metadata[$name]);
170  if (count($value)) {
171  // only save if value array contains data
172  $this->temp_metadata[$name] = $value;
173  }
174 
175  return true;
176  }
177 
178  if (!isset($this->temp_metadata[$name])) {
179  $this->temp_metadata[$name] = [];
180  }
181 
182  $this->temp_metadata[$name] = array_merge($this->temp_metadata[$name], $value);
183 
184  return true;
185  }
186 
198  public function deleteMetadata(string $name = null): bool {
199  if (!$this->guid) {
200  // remove from temp_metadata
201  if (isset($name)) {
202  if (isset($this->temp_metadata[$name])) {
203  unset($this->temp_metadata[$name]);
204  }
205  } else {
206  $this->temp_metadata = [];
207  }
208 
209  return true;
210  }
211 
212  return elgg_delete_metadata([
213  'guid' => $this->guid,
214  'limit' => false,
215  'metadata_name' => $name,
216  ]);
217  }
218 }
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
deleteMetadata(string $name=null)
Deletes all metadata on this object (metadata.entity_guid = $this->guid).
Definition: Metadata.php:198
getAllMetadata()
Get all entity metadata.
Definition: Metadata.php:37
$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:254
setTempMetadata(string $name, mixed $value, bool $multiple=false)
Set temp metadata on this entity.
Definition: Metadata.php:166
setMetadata(string $name, mixed $value, string $value_type= '', bool $multiple=false)
Set metadata on this entity.
Definition: Metadata.php:78
elgg_delete_metadata(array $options)
Deletes metadata based on $options.
Definition: metadata.php:49
$metadata
Output annotation metadata.
Definition: metadata.php:9
_elgg_services()
Get the global service provider.
Definition: elgglib.php:351
getMetadata(string $name)
Return the value of a piece of metadata.
Definition: Metadata.php:27
$guid
Reset an ElggUpgrade.
Definition: reset.php:6