Controller = $Controller; } /** * Called after the Controller::beforeRender(), after the view class is loaded, and before the * Controller::render() * * @param object $Controller Controller with components to beforeRender * @return void */ public function beforeRender(Controller $Controller) { if (Configure::read('Common.messages') !== false && $messages = $this->Session->read('Message')) { foreach ($messages as $message) { $this->message($message['message'], 'error'); } $this->Session->delete('Message'); } if ($this->Controller->request->is('ajax')) { $ajaxMessages = array_merge( (array)$this->Session->read('messages'), (array)Configure::read('messages') ); // The header can be read with JavaScript and a custom Message can be displayed $this->Controller->response->header('X-Ajax-Flashmessage', json_encode($ajaxMessages)); $this->Session->delete('messages'); } } /** * Adds a flash message. * Updates "messages" session content (to enable multiple messages of one type). * * @param string $message Message to output. * @param string $type Type ('error', 'warning', 'success', 'info' or custom class). * @return void */ public function message($message, $type = null) { if (!$type) { $type = 'info'; } $old = (array)$this->Session->read('messages'); if (isset($old[$type]) && count($old[$type]) > 99) { array_shift($old[$type]); } $old[$type][] = $message; $this->Session->write('messages', $old); } /** * Adds a transient flash message. * These flash messages that are not saved (only available for current view), * will be merged into the session flash ones prior to output. * * @param string $message Message to output. * @param string $type Type ('error', 'warning', 'success', 'info' or custom class). * @return void */ public static function transientMessage($message, $type = null) { if (!$type) { $type = 'info'; } $old = (array)Configure::read('messages'); if (isset($old[$type]) && count($old[$type]) > 99) { array_shift($old[$type]); } $old[$type][] = $message; Configure::write('messages', $old); } /** * Magic method for verbose flash methods based on types. * * For example: $this->Flash->success('My message') * * @param string $name Element name to use. * @param array $args Parameters to pass when calling `FlashComponent::set()`. * @return void * @throws InternalErrorException If missing the flash message. */ public function __call($name, $args) { if (count($args) < 1) { throw new InternalErrorException('Flash message missing.'); } $this->message($args[0], $name); } }