Elgg  Version 4.3
PrivateSettingsTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Database;
6 use ElggEntity;
8 use Elgg\Values;
9 
19 
23  protected $db;
24 
28  protected $entities;
29 
33  protected $cache;
34 
43  $this->db = $db;
44  $this->entities = $entities;
45  $this->cache = $cache;
46  }
47 
59  public function get(ElggEntity $entity, $name) {
60  return elgg_extract($name, $this->getAllForEntity($entity));
61  }
62 
70  public function getAllForEntity(ElggEntity $entity) {
71  $values = $this->cache->load($entity->guid);
72  if (isset($values)) {
73  return $values;
74  }
75 
76  $qb = Select::fromTable('private_settings');
77  $qb->select('name')
78  ->addSelect('value')
79  ->where($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER));
80 
81  $result = $this->db->getData($qb);
82 
83  $return = [];
84 
85  if (!empty($result)) {
86  foreach ($result as $r) {
87  $return[$r->name] = $r->value;
88  }
89  }
90 
91  $this->cache->save($entity->guid, $return);
92 
93  return $return;
94  }
95 
109  public function getAllForGUIDs($guids) {
111 
112  if (!is_array($guids) || empty($guids)) {
113  return [];
114  }
115 
116  $qb = Select::fromTable('private_settings');
117  $qb->select('entity_guid')
118  ->addSelect('name')
119  ->addSelect('value')
120  ->where($qb->compare('entity_guid', 'IN', $guids));
121 
122  $result = $this->db->getData($qb);
123  if (empty($result)) {
124  return [];
125  }
126 
127  $return = [];
128 
129  foreach ($result as $r) {
130  $return[$r->entity_guid][$r->name] = $r->value;
131  }
132 
133  return $return;
134  }
135 
145  public function set(ElggEntity $entity, $name, $value) {
146  $entity->invalidateCache();
147 
148  $value_type = is_int($value) ? ELGG_VALUE_INTEGER : ELGG_VALUE_STRING;
149 
150  $qb = Select::fromTable('private_settings');
151  $qb->select('id')
152  ->where($qb->compare('name', '=', $name, ELGG_VALUE_STRING))
153  ->andWhere($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER));
154 
155  $row = $this->db->getDataRow($qb);
156 
157  if (!empty($row)) {
158  $qb = Update::table('private_settings');
159  $qb->set('value', $qb->param($value, $value_type))
160  ->where($qb->compare('id', '=', $row->id, ELGG_VALUE_INTEGER));
161 
162  $result = $this->db->updateData($qb);
163  } else {
164  $qb = Insert::intoTable('private_settings');
165  $qb->values([
166  'entity_guid' => $qb->param($entity->guid, ELGG_VALUE_INTEGER),
167  'name' => $qb->param($name, ELGG_VALUE_STRING),
168  'value' => $qb->param($value, $value_type),
169  ]);
170 
171  $result = $this->db->insertData($qb);
172  }
173 
174  return $result !== false;
175  }
176 
185  public function remove(ElggEntity $entity, $name) {
186  $entity->invalidateCache();
187 
188  $qb = Delete::fromTable('private_settings');
189  $qb->where($qb->compare('name', '=', $name, ELGG_VALUE_STRING))
190  ->andWhere($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER));
191 
192  return (bool) $this->db->deleteData($qb);
193  }
194 
202  public function removeAllForEntity(ElggEntity $entity) {
203  $entity->invalidateCache();
204 
205  $qb = Delete::fromTable('private_settings');
206  $qb->where($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER));
207 
208  return (bool) $this->db->deleteData($qb);
209  }
210 
211 }
static normalizeGuids(...$args)
Flatten an array of data into an array of GUIDs.
Definition: Values.php:141
removeAllForEntity(ElggEntity $entity)
Deletes all private settings for an entity.
Saves user notification settings.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
static table($table, $alias=null)
{}
Definition: Update.php:13
invalidateCache()
Invalidate cache for entity.
The Elgg database.
Definition: Database.php:25
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:126
getAllForGUIDs($guids)
Return an array of all private settings for the requested guids.
$value
Definition: generic.php:51
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 ElggEntity
Definition: deprecated.js:437
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:547
__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
const ELGG_VALUE_STRING
Definition: constants.php:127
$qb
Definition: queue.php:11
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13
Entity table database service.
Definition: EntityTable.php:26