Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
SubtypeTable.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Database;
3 
4 use Elgg\Database;
5 
15 class SubtypeTable {
16 
20  private $cache = null;
21 
25  private $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  function getId($type, $subtype) {
67  if (!$subtype) {
68  return false;
69  }
70 
71  if ($this->cache === null) {
72  $this->populateCache();
73  }
74 
75  // use the cache before hitting database
77  if ($result !== null) {
78  return $result->id;
79  }
80 
81  return false;
82  }
83 
92  function getSubtype($subtype_id) {
93  if (!$subtype_id) {
94  return '';
95  }
96 
97  if ($this->cache === null) {
98  $this->populateCache();
99  }
100 
101  if (isset($this->cache[$subtype_id])) {
102  return $this->cache[$subtype_id]->subtype;
103  }
104 
105  return false;
106  }
107 
118  if ($this->cache === null) {
119  $this->populateCache();
120  }
121 
122  foreach ($this->cache as $obj) {
123  if ($obj->type === $type && $obj->subtype === $subtype) {
124  return $obj;
125  }
126  }
127  return null;
128  }
129 
135  function populateCache() {
136  $results = $this->db->getData("
137  SELECT *
138  FROM {$this->db->getTablePrefix()}entity_subtypes
139  ");
140 
141  $this->cache = array();
142  foreach ($results as $row) {
143  $this->cache[$row->id] = $row;
144  }
145  }
146 
162  function getClass($type, $subtype) {
163  if ($this->cache === null) {
164  $this->populateCache();
165  }
166 
167  // use the cache before going to the database
168  $obj = $this->retrieveFromCache($type, $subtype);
169  if ($obj) {
170  return $obj->class;
171  }
172 
173  return null;
174  }
175 
186  function getClassFromId($subtype_id) {
187  if (!$subtype_id) {
188  return null;
189  }
190 
191  if ($this->cache === null) {
192  $this->populateCache();
193  }
194 
195  if (isset($this->cache[$subtype_id])) {
196  return $this->cache[$subtype_id]->class;
197  }
198 
199  return null;
200  }
201 
222  function add($type, $subtype, $class = "") {
223  if (!$subtype) {
224  return 0;
225  }
226 
227  $id = $this->getId($type, $subtype);
228 
229  if (!$id) {
230  // In cache we store non-SQL-escaped strings because that's what's returned by query
231  $cache_obj = (object) array(
232  'type' => $type,
233  'subtype' => $subtype,
234  'class' => $class,
235  );
236 
237  $sql = "
238  INSERT INTO {$this->db->getTablePrefix()}entity_subtypes
239  (type, subtype, class) VALUES
240  (:type, :subtype, :class)
241  ";
242  $params = [
243  ':type' => $type,
244  ':subtype' => $subtype,
245  ':class' => $class,
246  ];
247  $id = $this->db->insertData($sql, $params);
248  _elgg_services()->boot->invalidateCache();
249 
250  // add entry to cache
251  $cache_obj->id = $id;
252  $this->cache[$id] = $cache_obj;
253  }
254 
255  return $id;
256  }
257 
272  function remove($type, $subtype) {
273  $sql = "
274  DELETE FROM {$this->db->getTablePrefix()}entity_subtypes
275  WHERE type = :type AND subtype = :subtype
276  ";
277  $params = [
278  ':type' => $type,
279  ':subtype' => $subtype,
280  ];
281  $success = $this->db->deleteData($sql, $params);
282  _elgg_services()->boot->invalidateCache();
283 
284  if ($success) {
285  // invalidate the cache
286  $this->cache = null;
287  }
288 
289  return (bool) $success;
290  }
291 
301  function update($type, $subtype, $class = '') {
302  $id = $this->getId($type, $subtype);
303  if (!$id) {
304  return false;
305  }
306 
307  if ($this->cache === null) {
308  $this->populateCache();
309  }
310 
311  $sql = "
312  UPDATE {$this->db->getTablePrefix()}entity_subtypes
313  SET type = :type, subtype = :subtype, class = :class
314  WHERE id = :id
315  ";
316  $params = [
317  ':type' => $type,
318  ':subtype' => $subtype,
319  ':class' => $class,
320  ':id' => $id,
321  ];
322  $success = $this->db->updateData($sql, false, $params);
323  _elgg_services()->boot->invalidateCache();
324 
325  if ($success && isset($this->cache[$id])) {
326  $this->cache[$id]->class = $class;
327  }
328 
329  return $success;
330  }
331 }
update($type, $subtype, $class= '')
Update a registered type, subtype, and class name.
setCachedValues(array $values)
Set the cached values from the boot data.
retrieveFromCache($type, $subtype)
Retrieve subtype from the cache.
populateCache()
Fetch all suptypes from DB to local cache.
$subtype
Definition: delete.php:28
if(isset($vars['id'])) $class
Definition: ajax_loader.php:19
$params
Definition: login.php:72
__construct(Database $db)
Constructor.
_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
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
if(!$display_name) $type
Definition: delete.php:27
getClass($type, $subtype)
Return the class name for a registered type and subtype.