      1: <?php
      2:
      3: /**
      4:  *
      5:  * A singleton class that maintains the view state in the session
      6:  *
      7:  * @package alpha::view
      8:  * @since 1.0
      9:  * @author John Collins <dev@alphaframework.org>
     10:  * @version $Id: ViewState.inc 1497 2012-02-14 20:28:45Z alphadev $
     11:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     12:  * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
     13:  * All rights reserved.
     14:  *
     15:  * <pre>
     16:  * Redistribution and use in source and binary forms, with or
     17:  * without modification, are permitted provided that the
     18:  * following conditions are met:
     19:  *
     20:  * * Redistributions of source code must retain the above
     21:  *   copyright notice, this list of conditions and the
     22:  *   following disclaimer.
     23:  * * Redistributions in binary form must reproduce the above
     24:  *   copyright notice, this list of conditions and the
     25:  *   following disclaimer in the documentation and/or other
     26:  *   materials provided with the distribution.
     27:  * * Neither the name of the Alpha Framework nor the names
     28:  *   of its contributors may be used to endorse or promote
     29:  *   products derived from this software without specific
     30:  *   prior written permission.
     31:  *
     32:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     33:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     34:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     35:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     36:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     37:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     38:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     39:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     40:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     41:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     42:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     43:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     44:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     45:  * </pre>
     46:  *
     47:  */
     48: class ViewState {
     49:     /**
     50:      * The name of the last selected tab by the user
     51:      *
     52:      * @var string
     53:      * @since 1.0
     54:      */
     55:     protected $selectedTab;
     56:
     57:     /**
     58:      * The amount of rows to expand the Markdown edit TextBox by
     59:      *
     60:      * @var string
     61:      * @since 1.0
     62:      */
     63:     protected $markdownTextBoxRows;
     64:
     65:     /**
     66:      * The view state object singleton
     67:      *
     68:      * @var ViewState
     69:      * @since 1.0
     70:      */
     71:     protected static $instance;
     72:
     73:     /**
     74:      * Private constructor means the class cannot be instantiated from elsewhere
     75:      *
     76:      * @since 1.0
     77:      */
     78:     private function __construct () {}
     79:
     80:     /**
     81:      * Get the ViewState instance.  Loads from $_SESSION if its not already in memory, otherwise
     82:      * a new instance will be returned with empty properties.
     83:      *
     84:      * @var ViewState
     85:      * @since 1.0
     86:      */
     87:     public static function getInstance() {
     88:         // if we don't already have the object in memory...
     89:         if (!isset(self::$instance)) {
     90:             // load from the session, otherwise return a new object
     91:             if(isset($_SESSION['ViewState'])) {
     92:                 return unserialize($_SESSION['ViewState']);
     93:             }else{
     94:                 self::$instance = new ViewState();
     95:                 return self::$instance;
     96:             }
     97:         }else{
     98:             return self::$instance;
     99:         }
    100:     }
    101:
    102:     /**
    103:      * Get the attribute value indicated by the key
    104:      *
    105:      * @param string $key
    106:      * @throws IllegalArguementException
    107:      * @return string
    108:      * @since 1.0
    109:      */
    110:     public function get($key) {
    111:         $attribute = new ReflectionProperty(get_class($this), $key);
    112:
    113:         if($attribute != null)
    114:             return $this->$key;
    115:         else
    116:             throw new IllegalArguementException('The property ['.$key.'] does not exist on the ['.get_class($this).'] class');
    117:     }
    118:
    119:     /**
    120:      * Sets the attribute value indicated by the key.  The ViewState instance will be serialized and saved back to the $_SESSION.
    121:      *
    122:      * @param string $key
    123:      * @param string $value
    124:      * @throws IllegalArguementException
    125:      * @since 1.0
    126:      */
    127:     public function set($key, $value) {
    128:         $attribute = new ReflectionProperty(get_class($this), $key);
    129:
    130:         if($attribute != null) {
    131:             $this->$key = $value;
    132:             $_SESSION[get_class($this)] = serialize($this);
    133:         }else{
    134:             throw new IllegalArguementException('The property ['.$key.'] does not exist on the ['.get_class($this).'] class');
    135:         }
    136:     }
    137: }
    138:
    139: ?>