Commit: 59641ca94a20cd212b40569963753a75877aca77

Author: Jon Adams | Date: 2010-01-24 00:30:43 -0800
Sessions almost complete. Deleting cookie does not appear to work.
diff --git a/controllers/AnologueController.php b/controllers/AnologueController.php index e4db6c5..6f187a7 100644 --- a/controllers/AnologueController.php +++ b/controllers/AnologueController.php @@ -3,6 +3,8 @@ namespace app\controllers; use \app\models\Anologue; +use \lithium\storage\Session; +use \lithium\storage\session\adapter\Cookie; /** * The core controller for Anologue. @@ -10,7 +12,17 @@ use \app\models\Anologue; * @see lithium\action\controller */ class AnologueController extends \lithium\action\Controller { - + + /** + * Constructor + */ + public function __construct($config = null) { + Session::config(array( + 'default' => array('adapter' => new Cookie()) + )); + parent::__construct($config); + } + /** * This action is used to render the index view, which is essentially a static page. */ @@ -27,7 +39,7 @@ class AnologueController extends \lithium\action\Controller { public function view() { $status = 'error'; $data = null; - $result = array(); + $result = $user = array(); if (!empty($this->request->params['id'])) { $data = Anologue::find($this->request->params['id']); @@ -45,7 +57,12 @@ class AnologueController extends \lithium\action\Controller { ); } - $this->set(compact('data')); + $user = Session::read('user'); + if (!empty($user)) { + $user = unserialize($user); + } + + $this->set(compact('data', 'user')); $this->render($result); } @@ -67,6 +84,9 @@ class AnologueController extends \lithium\action\Controller { $status = 'error'; if (!empty($this->request->params['id'])) { $data = $this->request->data; + + $this->_manageCookie($data); + $data['ip'] = $this->request->env('REMOTE_ADDR'); if (!empty($data)) { $status = 'fail'; @@ -76,6 +96,36 @@ class AnologueController extends \lithium\action\Controller { } $this->render(array('json' => (object) compact('status', 'data'))); } + + /** + * Internal method to create or delete data in cookie. + * + * This method is currently intended to be called from within `AnologueController::say()`. + * + * @param array $data associative array of user data and options to be saved + * @see app\controllers\AnologueController::say() + */ + private function _manageCookie($data = array()) { + $cookieKeys = array('author','email','scrolling','sounds', 'cookies'); + + if ($data['cookies'] == 'true') { + $user = array(); + + array_walk($cookieKeys, function($key) use (&$data, &$user) { + if (!empty($data[$key])) { + $user[$key] = $data[$key]; + if ($key == 'author' && $data[$key] == 'anonymous') { + unset($user[$key]); + } + } + }); + + Session::write('user', serialize($user)); + } else { + Session::delete('user'); + } + } + } ?> diff --git a/views/anologue/view.html.php b/views/anologue/view.html.php index 05f3b79..4ff3cdf 100644 --- a/views/anologue/view.html.php +++ b/views/anologue/view.html.php @@ -67,20 +67,20 @@ <div class="anologue-settings"> <div class="input name"> <label class="icon" for="anologue-author" title="Your name"><span>Your name</span></label> - <input type="text" name="anologue-author" id="anologue-author" /> + <input type="text" name="anologue-author" id="anologue-author" value="<?php echo ($user['author']) ?: ''; ?>" /> </div> <div class="input email"> <label class="icon" for="anologue-email" title="Your e-mail address"><span>Your e-mail</span></label> - <input type="text" name="anologue-email" id="anologue-email" /> + <input type="text" name="anologue-email" id="anologue-email" value="<?php echo ($user['email']) ?: ''; ?>" /> </div> <div class="checkbox first sound"> - <label class="icon" title="Toggle sounds"><span>Toggle sounds</span></label> + <label class="icon <?php echo ($user['sounds'] == 'false') ? 'disabled' : ''; ?>" title="Toggle sounds"><span>Toggle sounds</span></label> </div> <div class="checkbox auto-scroll"> - <label class="icon" title="Toggle auto-scrolling"><span>Toggle auto-scrolling</span></label> + <label class="icon <?php echo ($user['scrolling'] == 'false') ? 'disabled' : ''; ?>" title="Toggle auto-scrolling"><span>Toggle auto-scrolling</span></label> </div> <div class="checkbox cookie"> - <label class="icon disabled" title="Toggle cookies"><span>Toggle cookies</span></label> + <label class="icon" title="Toggle cookies"><span>Toggle cookies</span></label> </div> <div class="about"> <?php diff --git a/webroot/js/anologue.js b/webroot/js/anologue.js index 80a36db..6d4028e 100644 --- a/webroot/js/anologue.js +++ b/webroot/js/anologue.js @@ -15,6 +15,9 @@ var anologue = { $(".auto-scroll label").click(function() { anologue.toggleIcon('.auto-scroll'); }); + $(".cookie label").click(function() { + anologue.toggleIcon('.cookie'); + }); $("#anologue-close-help").click(function() { anologue.closeHelp(); return false; @@ -89,7 +92,10 @@ var anologue = { var data = { author: $('#anologue-author').val(), email: $('#anologue-email').val(), - text: $('#anologue-text').val() + text: $('#anologue-text').val(), + scrolling: this.getOption('.auto-scroll'), + sounds: this.getOption('.sound'), + cookies: this.getOption('.cookie') } if (data.text == '') { anologue.listener(); @@ -132,8 +138,8 @@ var anologue = { $('#'+id).animate({ opacity: 'show' }, 1000); - var scrollDisabled = $('.anologue-settings .auto-scroll .icon').hasClass('disabled'); - if (!scrollDisabled) { + var scrollEnabled = this.getOption('.auto-scroll'); + if (scrollEnabled) { $('html, body').animate({ scrollTop: $('#'+id).offset().top }, 'normal'); @@ -192,6 +198,11 @@ var anologue = { $("#anologue-help").addClass('closed'); } return false; + }, + + getOption: function(parentClass) { + var disabled = $('.anologue-settings '+parentClass+' .icon').hasClass('disabled'); + return !disabled; } }