Elgg  Version 2.3
SubtypeTable.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Database;
3 
5 
15 class SubtypeTable {
16 
20  protected $cache = null;
21 
25  protected $db;
26 
32  public function __construct(Database $db) {
33  $this->db = $db;
34  }
35 
42  public function setCachedValues(array $values) {
43  $this->cache = $values;
44  }
45 
66  public function getId($type, $subtype) {
67  if (!$subtype) {
68  return false;
69  }
70 
71  $obj = $this->retrieveFromCache($type, $subtype);
72 
73  return $obj ? $obj->id : false;
74  }
75 
84  public function getSubtype($subtype_id) {
85  if (!$subtype_id) {
86  return '';
87  }
88 
89  $cache = $this->getPopulatedCache();
90 
91  return isset($cache[$subtype_id]) ? $cache[$subtype_id]->subtype : false;
92  }
93 
103  public function retrieveFromCache($type, $subtype) {
104  foreach ($this->getPopulatedCache() as $obj) {
105  if ($obj->type === $type && $obj->subtype === $subtype) {
106  return $obj;
107  }
108  }
109 
110  return null;
111  }
112 
128  public function getClass($type, $subtype) {
129  $obj = $this->retrieveFromCache($type, $subtype);
130 
131  return $obj ? $obj->class : null;
132  }
133 
144  public function getClassFromId($subtype_id) {
145  if (!$subtype_id) {
146  return null;
147  }
148 
149  $cache = $this->getPopulatedCache();
150 
151  return isset($cache[$subtype_id]) ? $cache[$subtype_id]->class : null;
152  }
153 
174  public function add($type, $subtype, $class = "") {
175  if (!$subtype) {
176  return 0;
177  }
178 
179  $id = $this->getId($type, $subtype);
180 
181  if (!$id) {
182  $sql = "
183  INSERT INTO {$this->db->prefix}entity_subtypes
184  (type, subtype, class) VALUES
185  (:type, :subtype, :class)
186  ";
187  $params = [
188  ':type' => $type,
189  ':subtype' => $subtype,
190  ':class' => $class,
191  ];
192  $id = $this->db->insertData($sql, $params);
193 
194  $this->invalidateCache();
195  }
196 
197  return $id;
198  }
199 
214  public function remove($type, $subtype) {
215  $sql = "
216  DELETE FROM {$this->db->prefix}entity_subtypes
217  WHERE type = :type AND subtype = :subtype
218  ";
219  $params = [
220  ':type' => $type,
221  ':subtype' => $subtype,
222  ];
223  if (!$this->db->deleteData($sql, $params)) {
224  return false;
225  }
226 
227  $this->invalidateCache();
228 
229  return true;
230  }
231 
241  public function update($type, $subtype, $class = '') {
242  $id = $this->getId($type, $subtype);
243  if (!$id) {
244  return false;
245  }
246 
247  $sql = "
248  UPDATE {$this->db->prefix}entity_subtypes
249  SET type = :type, subtype = :subtype, class = :class
250  WHERE id = :id
251  ";
252  $params = [
253  ':type' => $type,
254  ':subtype' => $subtype,
255  ':class' => $class,
256  ':id' => $id,
257  ];
258  if (!$this->db->updateData($sql, false, $params)) {
259  return false;
260  }
261 
262  $this->invalidateCache();
263 
264  return true;
265  }
266 
272  protected function invalidateCache() {
273  $this->cache = null;
274  _elgg_services()->boot->invalidateCache();
275  _elgg_services()->entityCache->clear();
276  }
277 
283  protected function getPopulatedCache() {
284  if ($this->cache === null) {
285  $rows = $this->db->getData("
286  SELECT *
287  FROM {$this->db->prefix}entity_subtypes
288  ");
289 
290  $this->cache = [];
291  foreach ($rows as $row) {
292  $this->cache[$row->id] = $row;
293  }
294  }
295 
296  return $this->cache;
297  }
298 }
update($type, $subtype, $class= '')
Update a registered type, subtype, and class name.
setCachedValues(array $values)
Set the cached values from the boot data.
The Elgg database.
Definition: Database.php:17
retrieveFromCache($type, $subtype)
Retrieve subtype from the cache.
$class
Definition: field.php:20
$subtype
Definition: delete.php:28
getPopulatedCache()
Get a populated cache object.
$params
Definition: login.php:72
__construct(Database $db)
Constructor.
invalidateCache()
Empty the cache.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
add($type, $subtype, $class="")
Register with a certain type and subtype to be loaded as a specific class.
getClassFromId($subtype_id)
Returns the class name for a subtype id.
$row
$rows
getSubtype($subtype_id)
Gets the denormalized string for a given subtype ID.
getId($type, $subtype)
Return the id for a given subtype.
if(!$collection_name) $id
Definition: add.php:17
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5
if(!$display_name) $type
Definition: delete.php:27
getClass($type, $subtype)
Return the class name for a registered type and subtype.