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 }
dumpRegister(string $register_name= '')
Empty and return the given register or all registers.
loadRegisters()
Load the registers from the session.
addSuccessMessage(string $message)
Display a system message on next page load.
Exception thrown if an argument is not of the expected type.
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
addMessage($message)
Adds a message to the registry.
Elgg System Message.
Elgg Session Management.
Definition: ElggSession.php:19
if(!$entity instanceof\ElggUser) $data
Definition: attributes.php:13
count(string $register_name= '')
Counts the number of messages, either globally or in a particular register.
$filter
Layout content filter.
Definition: filter.php:18
$count
Definition: ban.php:24
if(isset($_COOKIE['elggperm'])) $session
Definition: login_as.php:29
static factory(array $options)
Create an ElggSystemMessage from an associative array.
System messages service.
saveRegisters(array $set)
Save the registers to the session.
__construct(protected\ElggSession $session)
Constructor.
addErrorMessage(string $message)
Display an error on next page load.