Elgg  Version 5.1
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 
23  protected $session;
24 
30  public function __construct(\ElggSession $session) {
31  $this->session = $session;
32  }
33 
43  public function dumpRegister(string $register_name = ''): array {
44  $set = $this->loadRegisters();
45  $return = [];
46 
47  foreach ($set as $prop => $values) {
48  if ($register_name === $prop || $register_name === '') {
49  if ($values || $register_name === $prop) {
50  $return[$prop] = $values;
51  }
52 
53  $set[$prop] = [];
54  }
55  }
56 
57  // support arbitrary registers for 2.0 BC
58  if ($register_name && !isset($return[$register_name])) {
59  $return[$register_name] = [];
60  }
61 
62  $this->saveRegisters($set);
63  return $return;
64  }
65 
73  public function count(string $register_name = ''): int {
74  $set = $this->loadRegisters();
75  $count = 0;
76 
77  foreach ($set as $prop => $values) {
78  if ($register_name === $prop || $register_name === '') {
79  $count += count($values);
80  }
81  }
82 
83  return $count;
84  }
85 
93  public function addSuccessMessage(string $message): void {
94  $this->addMessage(new \ElggSystemMessage($message, 'success'));
95  }
96 
104  public function addErrorMessage(string $message): void {
105  $this->addMessage(new \ElggSystemMessage($message, 'error'));
106  }
107 
121  public function addMessage($message): void {
122  if (is_array($message)) {
124  }
125 
126  if (!$message instanceof \ElggSystemMessage) {
127  throw new InvalidArgumentException(__METHOD__ . ' $message needs to be an \ElggSystemMessage or an array of options');
128  }
129 
130  $set = $this->loadRegisters();
131  $set[$message->getType()][] = $message;
132  $this->saveRegisters($set);
133  }
134 
140  public function loadRegisters(): array {
141  return $this->session->get(self::SESSION_KEY, []);
142  }
143 
157  public function saveRegisters(array $set): void {
158  $filter = function ($el) {
159  return ($el instanceof \ElggSystemMessage) && $el->getMessage() !== '';
160  };
161 
162  $data = [];
163  foreach ($set as $prop => $values) {
164  if (!is_array($values)) {
165  continue;
166  }
167 
168  $arr = array_filter($values, $filter);
169  if (!empty($arr)) {
170  $data[$prop] = array_values($arr);
171  }
172  }
173 
174  $this->session->set(self::SESSION_KEY, $data);
175  }
176 }
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.
$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.
$filter
Layout body.
Definition: body.php:10
saveRegisters(array $set)
Save the registers to the session.
__construct(\ElggSession $session)
Constructor.
addErrorMessage(string $message)
Display an error on next page load.