Elgg  Version 3.0
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($register_name = '') {
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($register_name = "") {
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($message) {
94  $set = $this->loadRegisters();
95  foreach ((array) $message as $str) {
96  $set->success[] = $str;
97  }
98  $this->saveRegisters($set);
99  }
100 
108  public function addErrorMessage($error) {
109  $set = $this->loadRegisters();
110  foreach ((array) $error as $str) {
111  $set->error[] = $str;
112  }
113  $this->saveRegisters($set);
114  }
115 
121  public function loadRegisters() {
122  $registers = $this->session->get(self::SESSION_KEY, []);
123  $set = new RegisterSet();
124  foreach ($registers as $key => $register) {
125  $set->{$key} = $register;
126  }
127  return $set;
128  }
129 
143  public function saveRegisters(RegisterSet $set) {
144  $filter = function ($el) {
145  return is_string($el) && $el !== "";
146  };
147 
148  $data = [];
149  foreach ($set as $prop => $values) {
150  if (!is_array($values)) {
151  continue;
152  }
153  $arr = array_filter($values, $filter);
154  if (!empty($arr)) {
155  $data[$prop] = array_values($arr);
156  }
157  }
158 
159  $this->session->set(self::SESSION_KEY, $data);
160  }
161 }
loadRegisters()
Load the registers from the session.
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:57
saveRegisters(RegisterSet $set)
Save the registers to the session.
addErrorMessage($error)
Display an error on next page load.
$error
Bad request error.
Definition: 400.php:6
Configuration exception.
elgg ajax SUCCESS
Definition: ajax.js:28
addSuccessMessage($message)
Display a system message on next page load.
dumpRegister($register_name= '')
Empty and return the given register or all registers.
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:55
System messages service.
if(elgg_in_context('widget')) $count
Definition: pagination.php:21
$filter
Layout body.
Definition: body.php:18
elgg ajax ERROR
Definition: ajax.js:33
Represents the state of system messages and errors.
Definition: RegisterSet.php:13
__construct(\ElggSession $session)
Constructor.
count($register_name="")
Counts the number of messages, either globally or in a particular register.