/** * {@inheritdoc} * * Replaces the default FlashComponent::set * behavior to use twitter bootstrap alerts. * All them use the same `flash` layout. * * @param string|\Exception $message Message to be flashed. If an instance * of \Exception the exception message will be used and code will be set * in params. * @param array $options An array of options * @return void */ public function set($message, array $options = []) { parent::set($message, $options); $options += $this->config(); $flash = $this->_session->read("Flash.{$options['key']}"); if (array_key_exists($flash['element'], $this->aliases) || in_array(str_replace('Flash/', null, $flash['element']), $this->aliases)) { $flash = array_replace_recursive($flash, array('element' => 'CakeBootstrap3.flash', 'class' => $this->getFlashClassName($flash['element']))); $this->_session->write("Flash.{$options['key']}", $flash); } }
/** * Magic method for verbose flash methods based on element names. * @param string $name Element name to use * @param array $args Parameters to pass * @return void */ public function __call($name, $args) { if (!isset($args[1]['plugin']) && in_array($name, ['alert', 'error', 'notice', 'success'])) { if (!isset($args[1]['params']['class'])) { if ($name === 'alert') { $args[1]['params']['class'] = 'alert-warning'; } elseif ($name === 'error') { $args[1]['params']['class'] = 'alert-danger'; } elseif ($name === 'notice') { $args[1]['params']['class'] = 'alert-info'; } else { $args[1]['params']['class'] = sprintf('alert-%s', $name); } } $name = 'flash'; $args[1]['plugin'] = METOOLS; } parent::__call($name, $args); }
/** * Magic method for verbose flash methods. * * @param string $name The type of the flash message, e.g. success. * @param array $args Parameters to pass when calling `FlashComponent::set()`. * @return void * @throws \Cake\Network\Exception\InternalErrorException If missing the flash message. */ public function __call($name, $args) { if (count($args) < 1) { throw new InternalErrorException('Flash message missing.'); } $message = $args[0]; $options = ['element' => 'default', 'key' => isset($args[1]) && !empty($args[1]) ? $args[1] : 'flash', 'params' => ['class' => '', 'type' => $name, 'dismiss' => true]]; $customOptions = isset($args[2]) ? (array) $args[2] : []; if (isset($customOptions['element'])) { $options['element'] = $customOptions['element']; unset($customOptions['element']); } $options['params'] = array_merge($options['params'], $customOptions); if (!$options['params']['dismiss']) { $class = 'flash-message--no-dismiss'; if (isset($options['params']['class']) && $options['params']['class'] !== '') { $class = ' ' . $class; } $options['params']['class'] .= $class; } parent::set($message, $options); }
/** * Set error flash messages. * * @param FlashComponent $Flash * @return void */ public function setFlashErrors(FlashComponent $Flash) { foreach ($this->_errors as $types) { if (is_array($types)) { foreach ($types as $message) { $Flash->error($message); } } } }