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 === '') {
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($value);
87  } else {
88  $value = [$value];
89  }
90 
91  // strip null values from array
92  $value = array_filter($value, function($var) {
93  return !is_null($var);
94  });
95 
96  if (empty($this->guid)) {
97  // unsaved entity. store in temp array
98  return $this->setTempMetadata($name, $value, $multiple);
99  }
100 
101  // saved entity. persist md to db.
102  if (!$multiple) {
103  $current_metadata = $this->getMetadata($name);
104 
105  if ((is_array($current_metadata) || count($value) > 1 || $value === []) && isset($current_metadata)) {
106  // remove current metadata if needed
107  // need to remove access restrictions right now to delete
108  // because this is the expected behavior
109  $delete_result = elgg_call(ELGG_IGNORE_ACCESS, function() use ($name) {
110  return elgg_delete_metadata([
111  'guid' => $this->guid,
112  'metadata_name' => $name,
113  'limit' => false,
114  ]);
115  });
116 
117  if ($delete_result === false) {
118  return false;
119  }
120  }
121 
122  if (count($value) > 1) {
123  // new value is a multiple valued metadata
124  $multiple = true;
125  }
126  }
127 
128  // create new metadata
129  foreach ($value as $value_tmp) {
130  $metadata = new \ElggMetadata();
131  $metadata->entity_guid = $this->guid;
132  $metadata->name = $name;
133  $metadata->value = $value_tmp;
134 
135  if (!empty($value_type)) {
136  $metadata->value_type = $value_type;
137  }
138 
139  $md_id = _elgg_services()->metadataTable->create($metadata, $multiple);
140  if ($md_id === false) {
141  return false;
142  }
143  }
144 
145  return true;
146  }
147 
158  protected function setTempMetadata(string $name, mixed $value, bool $multiple = false): bool {
159  // if overwrite, delete first
160  if (!$multiple) {
161  unset($this->temp_metadata[$name]);
162  if (count($value)) {
163  // only save if value array contains data
164  $this->temp_metadata[$name] = $value;
165  }
166 
167  return true;
168  }
169 
170  if (!isset($this->temp_metadata[$name])) {
171  $this->temp_metadata[$name] = [];
172  }
173 
174  $this->temp_metadata[$name] = array_merge($this->temp_metadata[$name], $value);
175 
176  return true;
177  }
178 
190  public function deleteMetadata(string $name = null): bool {
191  if (!$this->guid) {
192  // remove from temp_metadata
193  if (isset($name)) {
194  if (isset($this->temp_metadata[$name])) {
195  unset($this->temp_metadata[$name]);
196  }
197  } else {
198  $this->temp_metadata = [];
199  }
200 
201  return true;
202  }
203 
204  return elgg_delete_metadata([
205  'guid' => $this->guid,
206  'limit' => false,
207  'metadata_name' => $name,
208  ]);
209  }
210 }
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:304
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:190
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
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:121
setTempMetadata(string $name, mixed $value, bool $multiple=false)
Set temp metadata on this entity.
Definition: Metadata.php:158
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