Elgg  Version 2.3
SystemMessagesService.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
3 
5 
18 
19  const SUCCESS = 'success';
20  const ERROR = 'error';
21  const SESSION_KEY = 'msg';
22 
26  protected $session;
27 
33  public function __construct(\ElggSession $session) {
34  $this->session = $session;
35  }
36 
45  public function dumpRegister($register_name = '') {
46  $set = $this->loadRegisters();
47  $return = [];
48 
49  foreach ($set as $prop => $values) {
50  if ($register_name === $prop || $register_name === '') {
51  if ($values || $register_name === $prop) {
52  $return[$prop] = $values;
53  }
54 
55  $set->{$prop} = [];
56  }
57  }
58 
59  // support arbitrary registers for 2.0 BC
60  if ($register_name && !isset($return[$register_name])) {
61  $return[$register_name] = [];
62  }
63 
64  $this->saveRegisters($set);
65  return $return;
66  }
67 
75  public function count($register_name = "") {
76  $set = $this->loadRegisters();
77  $count = 0;
78 
79  foreach ($set as $prop => $values) {
80  if ($register_name === $prop || $register_name === '') {
81  $count += count($values);
82  }
83  }
84 
85  return $count;
86  }
87 
97  public function addSuccessMessage($message) {
98  $set = $this->loadRegisters();
99  foreach ((array)$message as $str) {
100  $set->success[] = $str;
101  }
102  $this->saveRegisters($set);
103  }
104 
114  public function addErrorMessage($error) {
115  $set = $this->loadRegisters();
116  foreach ((array)$error as $str) {
117  $set->error[] = $str;
118  }
119  $this->saveRegisters($set);
120  }
121 
127  public function loadRegisters() {
128  $registers = $this->session->get(self::SESSION_KEY, array());
129  $set = new RegisterSet();
130  foreach ($registers as $key => $register) {
131  $set->{$key} = $register;
132  }
133  return $set;
134  }
135 
149  public function saveRegisters(RegisterSet $set) {
150  $filter = function ($el) {
151  return is_string($el) && $el !== "";
152  };
153 
154  $data = [];
155  foreach ($set as $prop => $values) {
156  if (!is_array($values)) {
157  continue;
158  }
159  $arr = array_filter($values, $filter);
160  if ($arr) {
161  $data[$prop] = array_values($arr);
162  }
163  }
164 
165  $this->session->set(self::SESSION_KEY, $data);
166  }
167 }
loadRegisters()
Load the registers from the session.
if(!$owner||!($owner instanceof ElggUser)||!$owner->canEdit()) $error
Definition: upload.php:14
$data
Definition: opendd.php:13
$return
Definition: opendd.php:15
saveRegisters(RegisterSet $set)
Save the registers to the session.
addErrorMessage($error)
Display an error on next page load.
Save menu items.
$key
Definition: summary.php:34
elgg ajax SUCCESS
Definition: ajax.js:28
if(!$vars['title']&&$vars['title']!==false) if(isset($vars['filter_override'])) if(!isset($vars['filter'])&&elgg_is_logged_in()&&$context) $filter
Definition: content.php:62
addSuccessMessage($message)
Display a system message on next page load.
dumpRegister($register_name= '')
Empty and return the given register or all registers.
if(elgg_in_context('widget')) $count
Definition: pagination.php:21
elgg ajax ERROR
Definition: ajax.js:33
Represents the state of system messages and errors.
Definition: RegisterSet.php:13
$session
Definition: login.php:9
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5
__construct(\ElggSession $session)
Constructor.
count($register_name="")
Counts the number of messages, either globally or in a particular register.