Elgg  Version 1.11
SubtypeTable.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Database;
3 
12 
13 
23 class SubtypeTable {
29  private $CONFIG;
30 
34  public function __construct() {
35  global $CONFIG;
36  $this->CONFIG = $CONFIG;
37  }
38 
59  function getId($type, $subtype) {
61 
62  if (!$subtype) {
63  return false;
64  }
65 
66  if ($SUBTYPE_CACHE === null) {
68  }
69 
70  // use the cache before hitting database
72  if ($result !== null) {
73  return $result->id;
74  }
75 
76  return false;
77  }
78 
87  function getSubtype($subtype_id) {
89 
90  if (!$subtype_id) {
91  return '';
92  }
93 
94  if ($SUBTYPE_CACHE === null) {
96  }
97 
98  if (isset($SUBTYPE_CACHE[$subtype_id])) {
99  return $SUBTYPE_CACHE[$subtype_id]->subtype;
100  }
101 
102  return false;
103  }
104 
116 
117  if ($SUBTYPE_CACHE === null) {
119  }
120 
121  foreach ($SUBTYPE_CACHE as $obj) {
122  if ($obj->type === $type && $obj->subtype === $subtype) {
123  return $obj;
124  }
125  }
126  return null;
127  }
128 
134  function populateCache() {
136 
137  $results = _elgg_services()->db->getData("SELECT * FROM {$this->CONFIG->dbprefix}entity_subtypes");
138 
139  $SUBTYPE_CACHE = array();
140  foreach ($results as $row) {
141  $SUBTYPE_CACHE[$row->id] = $row;
142  }
143  }
144 
160  function getClass($type, $subtype) {
162 
163  if ($SUBTYPE_CACHE === null) {
165  }
166 
167  // use the cache before going to the database
169  if ($obj) {
170  return $obj->class;
171  }
172 
173  return null;
174  }
175 
186  function getClassFromId($subtype_id) {
188 
189  if (!$subtype_id) {
190  return null;
191  }
192 
193  if ($SUBTYPE_CACHE === null) {
195  }
196 
197  if (isset($SUBTYPE_CACHE[$subtype_id])) {
198  return $SUBTYPE_CACHE[$subtype_id]->class;
199  }
200 
201  return null;
202  }
203 
224  function add($type, $subtype, $class = "") {
226 
227  if (!$subtype) {
228  return 0;
229  }
230 
232 
233  if (!$id) {
234  // In cache we store non-SQL-escaped strings because that's what's returned by query
235  $cache_obj = (object) array(
236  'type' => $type,
237  'subtype' => $subtype,
238  'class' => $class,
239  );
240 
244 
245  $id = _elgg_services()->db->insertData("INSERT INTO {$this->CONFIG->dbprefix}entity_subtypes"
246  . " (type, subtype, class) VALUES ('$type', '$subtype', '$class')");
247 
248  // add entry to cache
249  $cache_obj->id = $id;
250  $SUBTYPE_CACHE[$id] = $cache_obj;
251  }
252 
253  return $id;
254  }
255 
270  function remove($type, $subtype) {
272 
275 
276  $success = _elgg_services()->db->deleteData("DELETE FROM {$this->CONFIG->dbprefix}entity_subtypes"
277  . " WHERE type = '$type' AND subtype = '$subtype'");
278 
279  if ($success) {
280  // invalidate the cache
281  $SUBTYPE_CACHE = null;
282  }
283 
284  return (bool) $success;
285  }
286 
296  function update($type, $subtype, $class = '') {
298 
300  if (!$id) {
301  return false;
302  }
303 
304  if ($SUBTYPE_CACHE === null) {
306  }
307 
308  $unescaped_class = $class;
309 
313 
314  $success = _elgg_services()->db->updateData("UPDATE {$this->CONFIG->dbprefix}entity_subtypes
315  SET type = '$type', subtype = '$subtype', class = '$class'
316  WHERE id = $id
317  ");
318 
319  if ($success && isset($SUBTYPE_CACHE[$id])) {
320  $SUBTYPE_CACHE[$id]->class = $unescaped_class;
321  }
322 
323  return $success;
324  }
325 }
update($type, $subtype, $class= '')
Update a registered type, subtype, and class name.
$success
Definition: view.php:29
_elgg_populate_subtype_cache()
Fetch all suptypes from DB to local cache.
Definition: entities.php:191
_elgg_retrieve_cached_subtype($type, $subtype)
Retrieve subtype from the cache.
Definition: entities.php:182
get_subtype_id($type, $subtype)
Return the id for a given subtype.
Definition: entities.php:157
retrieveFromCache($type, $subtype)
Retrieve subtype from the cache.
populateCache()
Fetch all suptypes from DB to local cache.
if(isset($vars['id'])) $class
Definition: ajax_loader.php:19
_elgg_services()
Definition: autoloader.php:14
sanitise_string($string)
Wrapper function for alternate English spelling (.
Definition: database.php:150
elgg global
Pointer to the global context.
Definition: elgglib.js:12
$type
Definition: add.php:8
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.
$subtype
Definition: river.php:12
global $SUBTYPE_CACHE
Cache subtypes and related class names.
if(!$collection_name) $id
Definition: add.php:17
__construct()
Constructor.
getClass($type, $subtype)
Return the class name for a registered type and subtype.