Elgg  Version 3.0
PrivateSettingsTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
6 use Elgg\Database;
7 use ElggEntity;
9 use Elgg\Values;
10 
23 
27  protected $db;
28 
32  protected $entities;
33 
37  protected $cache;
38 
47  $this->db = $db;
48  $this->entities = $entities;
49  $this->cache = $cache;
50  }
51 
64  public function get(ElggEntity $entity, $name) {
65  return elgg_extract($name, $this->getAllForEntity($entity));
66  }
67 
76  public function getAllForEntity(ElggEntity $entity) {
77  $values = $this->cache->load($entity->guid);
78  if (isset($values)) {
79  return $values;
80  }
81 
82  $qb = Select::fromTable('private_settings');
83  $qb->select('name')
84  ->addSelect('value')
85  ->where($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER));
86 
87  $result = $this->db->getData($qb);
88 
89  $return = [];
90 
91  if (!empty($result)) {
92  foreach ($result as $r) {
93  $return[$r->name] = $r->value;
94  }
95  }
96 
97  $this->cache->save($entity->guid, $return);
98 
99  return $return;
100  }
101 
116  public function getAllForGUIDs($guids) {
118 
119  if (!is_array($guids) || empty($guids)) {
120  return [];
121  }
122 
123  $qb = Select::fromTable('private_settings');
124  $qb->select('entity_guid')
125  ->addSelect('name')
126  ->addSelect('value')
127  ->where($qb->compare('entity_guid', 'IN', $guids));
128 
129  $result = $this->db->getData($qb);
130  if (empty($result)) {
131  return [];
132  }
133 
134  $return = [];
135 
136  foreach ($result as $r) {
137  $return[$r->entity_guid][$r->name] = $r->value;
138  }
139 
140  return $return;
141  }
142 
153  public function set(ElggEntity $entity, $name, $value) {
154  $entity->invalidateCache();
155 
156  $value_type = is_int($value) ? ELGG_VALUE_INTEGER : ELGG_VALUE_STRING;
157 
158  $qb = Select::fromTable('private_settings');
159  $qb->select('id')
160  ->where($qb->compare('name', '=', $name, ELGG_VALUE_STRING))
161  ->andWhere($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER));
162 
163  $row = $this->db->getDataRow($qb);
164 
165  if (!empty($row)) {
166  $qb = Update::table('private_settings');
167  $qb->set('value', $qb->param($value, $value_type))
168  ->where($qb->compare('id', '=', $row->id, ELGG_VALUE_INTEGER));
169 
170  $result = $this->db->updateData($qb);
171  } else {
172  $qb = Insert::intoTable('private_settings');
173  $qb->values([
174  'entity_guid' => $qb->param($entity->guid, ELGG_VALUE_INTEGER),
175  'name' => $qb->param($name, ELGG_VALUE_STRING),
176  'value' => $qb->param($value, $value_type),
177  ]);
178 
179  $result = $this->db->insertData($qb);
180  }
181 
182  return $result !== false;
183  }
184 
194  public function remove(ElggEntity $entity, $name) {
195  $entity->invalidateCache();
196 
197  $qb = Delete::fromTable('private_settings');
198  $qb->where($qb->compare('name', '=', $name, ELGG_VALUE_STRING))
199  ->andWhere($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER));
200 
201  return $this->db->deleteData($qb);
202  }
203 
212  public function removeAllForEntity(ElggEntity $entity) {
213  $entity->invalidateCache();
214 
215  $qb = Delete::fromTable('private_settings');
216  $qb->where($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER));
217 
218  return $this->db->deleteData($qb);
219  }
220 
221 }
static normalizeGuids(...$args)
Flatten an array of data into an array of GUIDs.
Definition: Values.php:139
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
removeAllForEntity(ElggEntity $entity)
Deletes all private settings for an entity.
static table($table, $alias=null)
{}
Definition: Update.php:13
invalidateCache()
Invalidate cache for entity.
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:138
getAllForGUIDs($guids)
Return an array of all private settings for the requested guids.
Private settings for entities.
static intoTable($table)
{}
Definition: Insert.php:13
$entity
Definition: reset.php:8
In memory cache of known private settings values stored by entity.
getAllForEntity(ElggEntity $entity)
Return an array of all private settings.
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1131
__construct(Database $db, EntityTable $entities, PrivateSettingsCache $cache)
Constructor.
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
$value
Definition: debugging.php:7
const ELGG_VALUE_STRING
Definition: constants.php:139
elgg ElggEntity
Definition: ElggEntity.js:15
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13
WARNING: API IN FLUX.
Definition: EntityTable.php:38