Elgg  Version master
SystemMessagesService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
6 
15 
16  const SUCCESS = 'success';
17  const ERROR = 'error';
18  const SESSION_KEY = '_elgg_msgs';
19 
25  public function __construct(protected \ElggSession $session) {
26  }
27 
37  public function dumpRegister(string $register_name = ''): array {
38  $set = $this->loadRegisters();
39  $return = [];
40 
41  foreach ($set as $prop => $values) {
42  if ($register_name === $prop || $register_name === '') {
43  if ($values || $register_name === $prop) {
44  $return[$prop] = $values;
45  }
46 
47  $set[$prop] = [];
48  }
49  }
50 
51  // support arbitrary registers for 2.0 BC
52  if ($register_name && !isset($return[$register_name])) {
53  $return[$register_name] = [];
54  }
55 
56  $this->saveRegisters($set);
57  return $return;
58  }
59 
67  public function count(string $register_name = ''): int {
68  $set = $this->loadRegisters();
69  $count = 0;
70 
71  foreach ($set as $prop => $values) {
72  if ($register_name === $prop || $register_name === '') {
73  $count += count($values);
74  }
75  }
76 
77  return $count;
78  }
79 
87  public function addSuccessMessage(string $message): void {
88  $this->addMessage(new \ElggSystemMessage($message, 'success'));
89  }
90 
98  public function addErrorMessage(string $message): void {
99  $this->addMessage(new \ElggSystemMessage($message, 'error'));
100  }
101 
115  public function addMessage($message): void {
116  if (is_array($message)) {
118  }
119 
120  if (!$message instanceof \ElggSystemMessage) {
121  throw new InvalidArgumentException(__METHOD__ . ' $message needs to be an \ElggSystemMessage or an array of options');
122  }
123 
124  $set = $this->loadRegisters();
125  $set[$message->getType()][] = $message;
126  $this->saveRegisters($set);
127  }
128 
134  public function loadRegisters(): array {
135  return $this->session->get(self::SESSION_KEY, []);
136  }
137 
151  public function saveRegisters(array $set): void {
152  $filter = function ($el) {
153  return ($el instanceof \ElggSystemMessage) && $el->getMessage() !== '';
154  };
155 
156  $data = [];
157  foreach ($set as $prop => $values) {
158  if (!is_array($values)) {
159  continue;
160  }
161 
162  $arr = array_filter($values, $filter);
163  if (!empty($arr)) {
164  $data[$prop] = array_values($arr);
165  }
166  }
167 
168  $this->session->set(self::SESSION_KEY, $data);
169  }
170 }
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
if(! $entity instanceof \ElggUser) $data
Definition: attributes.php:13
$count
Definition: ban.php:24
Elgg Session Management.
Definition: ElggSession.php:19
Elgg System Message.
static factory(array $options)
Create an ElggSystemMessage from an associative array.
Exception thrown if an argument is not of the expected type.
System messages service.
saveRegisters(array $set)
Save the registers to the session.
loadRegisters()
Load the registers from the session.
addSuccessMessage(string $message)
Display a system message on next page load.
addMessage($message)
Adds a message to the registry.
count(string $register_name='')
Counts the number of messages, either globally or in a particular register.
dumpRegister(string $register_name='')
Empty and return the given register or all registers.
addErrorMessage(string $message)
Display an error on next page load.
__construct(protected \ElggSession $session)
Constructor.
if(isset($_COOKIE['elggperm'])) $session
Definition: login_as.php:29
$filter
Layout content filter.
Definition: filter.php:18
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10