Elgg  Version 1.11
SystemMessagesService.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
3 
16 
20  protected $session;
21 
27  public function __construct(\ElggSession $session) {
28  $this->session = $session;
29  }
30 
39  function dumpRegister($name = '') {
40  $registers = $this->loadRegisters($name);
41 
42  if ($name !== "") {
43  $return = array();
44  $return[$name] = empty($registers[$name]) ? [] : $registers[$name];
45  unset($registers[$name]);
46  } else {
47  $return = $registers;
48  $registers = array();
49  }
50 
51  $this->saveRegisters($registers);
52  return $return;
53  }
54 
62  function count($register_name = "") {
63  $registers = $this->loadRegisters($register_name);
64 
65  if ($register_name !== '') {
66  return empty($registers[$register_name]) ? 0 : count($registers[$register_name]);
67  }
68 
69  $count = 0;
70  foreach ($registers as $register) {
71  $count += count($register);
72  }
73  return $count;
74  }
75 
86  return $this->addMessageToRegister($message, "success");
87  }
88 
98  function addErrorMessage($error) {
99  return $this->addMessageToRegister($error, "error");
100  }
101 
121  function addMessageToRegister($message, $register_name = '') {
122  $registers = $this->loadRegisters($register_name);
123 
124  if (is_string($message)) {
125  $message = array($message);
126  }
127  if (!isset($registers[$register_name])) {
128  $registers[$register_name] = [];
129  }
130  $registers[$register_name] = array_merge($registers[$register_name], $message);
131 
132  $this->saveRegisters($registers);
133  return true;
134  }
135 
143  protected function loadRegisters($accessed_register = '') {
144  $registers = $this->session->get('msg', array());
145 
146  if (!isset($registers[$accessed_register]) && $accessed_register !== '') {
147  $registers[$accessed_register] = array();
148  }
149 
150  return $registers;
151  }
152 
159  protected function saveRegisters(array $registers) {
160  $this->session->set('msg', $registers);
161  }
162 }
if(!$owner||!($owner instanceof ElggUser)||!$owner->canEdit()) $error
Definition: upload.php:14
loadRegisters($accessed_register= '')
Load the registers from the session.
dumpRegister($name= '')
Empty and return the given register or all registers.
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
$return
Definition: opendd.php:15
addErrorMessage($error)
Display an error on next page load.
saveRegisters(array $registers)
Save the registers to the session.
Save menu items.
addSuccessMessage($message)
Display a system message on next page load.
if(elgg_in_context('widget')) $count
Definition: pagination.php:20
addMessageToRegister($message, $register_name= '')
Add a message(s) to a named register to be displayed.
$session
Definition: login.php:9
__construct(\ElggSession $session)
Constructor.
count($register_name="")
Counts the number of messages, either globally or in a particular register.