Overview

Namespaces

  • Alpha
    • Controller
      • Front
    • Exception
    • Model
      • Type
    • Task
    • Util
      • Backup
      • Cache
      • Code
        • Highlight
        • Metric
      • Config
      • Convertor
      • Email
      • Extension
      • Feed
      • File
      • Graph
      • Helper
      • Http
        • Filter
        • Session
      • Image
      • Logging
      • Search
      • Security
    • View
      • Renderer
        • Html
        • Json
      • Widget

Classes

  • ArticleCommentView
  • ArticleView
  • DEnumView
  • PersonView
  • SequenceView
  • View
  • ViewState
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace Alpha\View;
  4: 
  5: use Alpha\Exception\IllegalArguementException;
  6: use Alpha\Util\Config\ConfigProvider;
  7: use Alpha\Util\Http\Session\SessionProviderFactory;
  8: use ReflectionProperty;
  9: 
 10: /**
 11:  * A singleton class that maintains the view state in the session.
 12:  *
 13:  * @since 1.0
 14:  *
 15:  * @author John Collins <dev@alphaframework.org>
 16:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
 17:  * @copyright Copyright (c) 2015, John Collins (founder of Alpha Framework).
 18:  * All rights reserved.
 19:  *
 20:  * <pre>
 21:  * Redistribution and use in source and binary forms, with or
 22:  * without modification, are permitted provided that the
 23:  * following conditions are met:
 24:  *
 25:  * * Redistributions of source code must retain the above
 26:  *   copyright notice, this list of conditions and the
 27:  *   following disclaimer.
 28:  * * Redistributions in binary form must reproduce the above
 29:  *   copyright notice, this list of conditions and the
 30:  *   following disclaimer in the documentation and/or other
 31:  *   materials provided with the distribution.
 32:  * * Neither the name of the Alpha Framework nor the names
 33:  *   of its contributors may be used to endorse or promote
 34:  *   products derived from this software without specific
 35:  *   prior written permission.
 36:  *
 37:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 38:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 39:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 40:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 41:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 42:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 43:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 44:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 45:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 46:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 47:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 48:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 49:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 50:  * </pre>
 51:  */
 52: class ViewState
 53: {
 54:     /**
 55:      * The name of the last selected tab by the user.
 56:      *
 57:      * @var string
 58:      *
 59:      * @since 1.0
 60:      */
 61:     protected $selectedTab;
 62: 
 63:     /**
 64:      * The amount of rows to expand the Markdown edit TextBox by.
 65:      *
 66:      * @var string
 67:      *
 68:      * @since 1.0
 69:      */
 70:     protected $markdownTextBoxRows;
 71: 
 72:     /**
 73:      * If the backend admin menu should be displayed or not.
 74:      *
 75:      * @var bool
 76:      *
 77:      * @since 2.0
 78:      */
 79:     protected $renderAdminMenu = false;
 80: 
 81:     /**
 82:      * The start position of the last pagination page by the user.
 83:      *
 84:      * @var int
 85:      *
 86:      * @since 2.0
 87:      */
 88:     protected $selectedStart = 0;
 89: 
 90:     /**
 91:      * The view state object singleton.
 92:      *
 93:      * @var Alpha\View\ViewState
 94:      *
 95:      * @since 1.0
 96:      */
 97:     protected static $instance;
 98: 
 99:     /**
100:      * Private constructor means the class cannot be instantiated from elsewhere.
101:      *
102:      * @since 1.0
103:      */
104:     private function __construct()
105:     {
106:     }
107: 
108:     /**
109:      * Get the ViewState instance.  Loads from $_SESSION if its not already in memory, otherwise
110:      * a new instance will be returned with empty properties.
111:      *
112:      * @return Alpha\View\ViewState
113:      *
114:      * @since 1.0
115:      */
116:     public static function getInstance()
117:     {
118:         $config = ConfigProvider::getInstance();
119:         $sessionProvider = $config->get('session.provider.name');
120:         $session = SessionProviderFactory::getInstance($sessionProvider);
121: 
122:         // if we don't already have the object in memory...
123:         if (!isset(self::$instance)) {
124:             // load from the session, otherwise return a new object
125:             if ($session->get('ViewState') !== false) {
126:                 return unserialize($session->get('ViewState'));
127:             } else {
128:                 self::$instance = new self();
129: 
130:                 return self::$instance;
131:             }
132:         } else {
133:             return self::$instance;
134:         }
135:     }
136: 
137:     /**
138:      * Get the attribute value indicated by the key.
139:      *
140:      * @param string $key
141:      *
142:      * @throws Alpha\Exception\IllegalArguementException
143:      *
144:      * @return string
145:      *
146:      * @since 1.0
147:      */
148:     public function get($key)
149:     {
150:         $attribute = new ReflectionProperty(get_class($this), $key);
151: 
152:         if ($attribute != null) {
153:             return $this->$key;
154:         } else {
155:             throw new IllegalArguementException('The property ['.$key.'] does not exist on the ['.get_class($this).'] class');
156:         }
157:     }
158: 
159:     /**
160:      * Sets the attribute value indicated by the key.  The ViewState instance will be serialized and saved back to the $_SESSION.
161:      *
162:      * @param string $key
163:      * @param string $value
164:      *
165:      * @throws Alpha\Exception\IllegalArguementException
166:      *
167:      * @since 1.0
168:      */
169:     public function set($key, $value)
170:     {
171:         $config = ConfigProvider::getInstance();
172:         $sessionProvider = $config->get('session.provider.name');
173:         $session = SessionProviderFactory::getInstance($sessionProvider);
174:         $attribute = new ReflectionProperty(get_class($this), $key);
175: 
176:         if ($attribute != null) {
177:             $this->$key = $value;
178:             $session->set('ViewState', serialize($this));
179:         } else {
180:             throw new IllegalArguementException('The property ['.$key.'] does not exist on the ['.get_class($this).'] class');
181:         }
182:     }
183: }
184: 
Alpha Framework 2.0.4 API Documentation API documentation generated by ApiGen 2.8.0