/** * We want to use our own little helper so that everything is shorter to write and * so we can use fancier messages with JavaScript. * * @param $options * @return HTML String */ public function flash($options = array()) { $defaults = array('key' => 'default', 'options' => array('type' => 'growl', 'fade_delay' => '8000', 'pnotify_opacity' => '.8')); $options += $defaults; $message = ''; $flash = FlashMessage::read($options['key']); if (!empty($flash)) { $message = $flash['message']; FlashMessage::clear($options['key']); } $view = new View(array('paths' => array('template' => '{:library}/views/elements/{:template}.{:type}.php', 'layout' => '{:library}/views/layouts/{:layout}.{:type}.php'))); return $view->render('all', array('options' => $options['options'], 'message' => $message), array('library' => 'li3b_core', 'template' => 'flash_message', 'type' => 'html', 'layout' => 'blank')); }
public function testClear() { FlashMessage::write('Foo'); FlashMessage::clear(); $result = Session::read('message.default', array('name' => 'default')); $this->assertNull($result); FlashMessage::write('Foo 2', 'test1'); FlashMessage::clear('test1'); $result = Session::read('message.test1', array('name' => 'default')); $this->assertNull($result); FlashMessage::write('Foo 3', 'test2'); FlashMessage::write('Foo 4', 'test3'); FlashMessage::clear(null); $result = Session::read('message', array('name' => 'default')); $this->assertNull($result); }
public function testClear() { FlashMessage::set('Foo'); FlashMessage::clear(); $result = Session::read('FlashMessage.default', array('name' => 'flash_message')); $this->assertNull($result); FlashMessage::set('Foo 2', array(), 'TestKey'); FlashMessage::clear('TestKey'); $result = Session::read('FlashMessage.TestKey', array('name' => 'flash_message')); $this->assertNull($result); FlashMessage::set('Foo 3', array(), 'TestKey2'); FlashMessage::set('Foo 4', array(), 'TestKey3'); FlashMessage::clear(null); $result = Session::read('FlashMessage', array('name' => 'flash_message')); $this->assertNull($result); }
/** * Binds the messaging system to a controller to enable `'message'` option flags in various * controller methods, such as `render()` and `redirect()`. * * @param object $controller An instance of `lithium\action\Controller`. * @param array $options Options. * @return object Returns the passed `$controller` instance. */ public static function bindTo($controller, array $options = array()) { $controller->applyFilter('redirect', function ($self, $params, $chain) use($options) { $options =& $params['options']; if (isset($params['options']['message'])) { FlashMessage::write($params['options']['message']); unset($params['options']['message']); } return $chain->next($self, $params, $chain); }); return $controller; }
protected function flashMessage($msg, $viewAttribs = array(), $namespace = 'global') { return FlashMessage::write($msg, $viewAttribs, $namespace); }
/** * Read a page (like "view()" but retrieves page data from the database). * Also, like other methods, extra data is bridged in from an optional associated page type library on the record itself. */ public function read($url=null) { // We can get the URL from the named parameter or from the arg passed if((isset($this->request->params['url'])) && (empty($url))) { $url = $this->request->params['url']; } $document = $this->getDocument(array( 'action' => __METHOD__, 'request' => $this->request, 'find_type' => 'first', 'conditions' => array('url' => $url) )); if(!$document) { FlashMessage::set('Page not found.', array('options' => array('type' => 'error', 'pnotify_title' => 'Error', 'pnotify_opacity' => '.8'))); $this->redirect(array('controller' => 'pages', 'action' => 'index')); } $this->set(compact('document')); }
/** * Public view action, for user profiles and such. * * @param $url The user's pretty URL */ public function read($url = null) { $conditions = array('url' => $url); /** * If nothing is passed, get the currently logged in user's profile. * This is safer to use for logged in users, because if they update * their profile and change their name...The pretty URL changes. */ if (empty($url) && isset($this->request->user)) { $conditions = array('_id' => $this->request->user['_id']); } $user = User::find('first', array('conditions' => $conditions)); if (empty($user)) { FlashMessage::write('Sorry, that user does not exist.', 'default'); return $this->redirect('/'); } /** * Protect the password in case changes are made where this action * could be called with a handler like JSON or XML, etc. This way, * even if the user document is returned, it won't contain any * sensitive password information. Not even the _id. */ $user->set(array('password' => null, '_id' => null)); $this->set(compact('user')); }
public function logout() { Auth::clear('minerva_user'); FlashMessage::set('You\'ve successfully logged out.'); $this->redirect(array('action' => 'login')); }
public function testMessageTranslation() { $testApp = Libraries::get(true, 'resources') . '/tmp/tests/test_app'; mkdir($testApp . '/config', 0777, true); $body = <<<EOD <?php return array( \t'hello' => 'Hello World.', \t'advice' => 'Whatever advice you give, be short.', \t'error' => 'To err is human, but for a real disaster you need a computer.' ); ?> EOD; $filepath = $testApp . '/config/messages.php'; file_put_contents($filepath, $body); Libraries::add('test_app', array('path' => $testApp)); FlashMessage::config(array('library' => 'test_app')); $messages = array('hello', 'advice', 'error'); FlashMessage::write($messages); $expected = array('message' => array('Hello World.', 'Whatever advice you give, be short.', 'To err is human, but for a real disaster you need a computer.'), 'attrs' => array()); $result = FlashMessage::read('flash_message'); $this->assertEqual($expected, $result); $message = 'hello'; FlashMessage::write($message); $expected = array('message' => 'Hello World.', 'attrs' => array()); $result = FlashMessage::read('flash_message'); $this->assertEqual($expected, $result); $this->_cleanUp(); }
<?php use li3_flash_message\extensions\storage\FlashMessage; use lithium\action\Dispatcher; Dispatcher::applyFilter('_callable', function ($self, $params, $chain) { $object = $chain->next($self, $params, $chain); if (is_a($object, 'lithium\\action\\Controller')) { return FlashMessage::bindTo($object); } return $object; });
// Adding the library here if it hasn't already been added. if (!class_exists('li3_access\\security\\Access')) { Libraries::add('li3_access'); } Dispatcher::applyFilter('_callable', function ($self, $params, $chain) { // Run other filters first. This allows this one to not exactly be overwritten or excluded...But it does allow for a different login action to be used... // TODO: Perhaps allow this to be skipped... $next = $chain->next($self, $params, $chain); $request = $params['request']; $action = $request->action; $user = Auth::check('li3b_user'); // Protect all admin methods except for login and logout. if ($request->admin === true && $action != 'login' && $action != 'logout') { $action_access = Access::check('default', $user, $request, array('rules' => array('allowManagers'))); if (!empty($action_access)) { FlashMessage::write($action_access['message'], 'default'); if ($user) { header('Location: ' . Router::match($action_access['redirect'])); } else { header('Location: ' . Router::match(array('library' => 'li3b_users', 'controller' => 'users', 'action' => 'login'))); } // None shall pass. exit; } } // Sets the current user in each request for convenience. $params['request']->user = $user; return $next; // return $chain->next($self, $params, $chain); }); Access::config(array('default' => array('adapter' => 'Rules', 'filters' => array())));
<?php use lithium\action\Dispatcher; use li3_flash_message\extensions\storage\FlashMessage; Dispatcher::applyFilter('_callable', function ($self, $params, $chain) { return FlashMessage::bindTo($chain->next($self, $params, $chain)); });
/** * Generic delete() action. * The trick here is that $this->calling_class and $this->calling_method will hold a string * reference for which extended class called this delete() method. We need that in order to * get the proper records and access. */ public function delete() { // get the "_type" ... page_type, user_type, or block_type $model = Inflector::classify(Inflector::singularize($this->request->params['controller'])); $modelClass = 'minerva\models\\'.$model; $conditions = array(); if(isset($this->request->params['id'])) { $conditions = array('id' => $this->request->params['id']); } if(isset($this->request->params['url'])) { $conditions = array('url' => $this->request->params['url']); } if(empty($conditions)) { $this->redirect(array('controller' => $this->request->params['controller'], 'action' => 'index')); } $document = $this->getDocument(array( 'action' => __METHOD__, 'request' => $this->request, 'find_type' => 'first', 'conditions' => $conditions )); if($document->delete()) { FlashMessage::set('The content has been deleted.', array('options' => array('type' => 'success', 'pnotify_title' => 'Success', 'pnotify_opacity' => .8))); $this->redirect(array('controller' => $this->request->params['controller'], 'action' => 'index')); } else { FlashMessage::set('The content could not be deleted, please try again.', array('options' => array('type' => 'error', 'pnotify_title' => 'Error', 'pnotify_opacity' => .8))); $this->redirect(array('controller' => $this->request->params['controller'], 'action' => 'index')); } }