1 <?php 2 /* Copyright (c) 2013, Geert Bergman (geert@scrivo.nl) 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. Neither the name of "Scrivo" nor the names of its contributors may be 14 * used to endorse or promote products derived from this software without 15 * specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $Id: Action.php 866 2013-08-25 16:22:35Z geert $ 30 */ 31 32 /** 33 * Implementation of the \Scrivo\Action class. 34 */ 35 36 namespace Scrivo; 37 38 class Action { 39 40 const VIEW = 1; 41 const FORWARD = 2; 42 const TYPE = 3; 43 const FILE = 4; 44 const SUCCESS = 5; 45 const FAIL = 6; 46 const WARN = 7; 47 const AUTH = 8; 48 const PARAMS = 9; 49 const ACTION = 10; 50 const DOWNLOAD = 11; 51 const XHR = 12; 52 53 protected $context = null; 54 protected $session = null; 55 protected $parameters = null; 56 57 private $auth = \Scrivo\User::STATUS_MEMBER; 58 private $type = null; 59 private $file = null; 60 private $action = null; 61 private $forward = array(); 62 private $result = self::FAIL; 63 private $resultData = null; 64 65 public static function create($context, $action, $userStatus, $session) { 66 if (isset($action[self::ACTION])) { 67 $act = $action[self::ACTION]; 68 return new $act($context, $action, $userStatus, $session); 69 } else { 70 return new Action($context, $action, $userStatus, $session); 71 } 72 } 73 74 function __construct($context, $action, $userStatus, $session=null) { 75 76 $this->auth = $action[self::AUTH]; 77 if ($userStatus) { 78 if ($userStatus > $this->auth) { 79 die("Authorization Error"); 80 } 81 } 82 83 $this->context = $context; 84 $this->session = $session; 85 $this->type = $action[self::TYPE]; 86 87 if (isset($action[self::FILE])) { 88 $this->file = $action[self::FILE]; 89 } 90 if (isset($action[self::ACTION])) { 91 $this->action = $action[self::ACTION]; 92 } 93 if (isset($action[self::FORWARD])) { 94 $this->forward = $action[self::FORWARD]; 95 } 96 if (isset($action[self::PARAMS])) { 97 $this->parameters = $action[self::PARAMS]; 98 } 99 100 if (isset($session->clearError)) { 101 unset($session->clearError); 102 103 unset($session->forwardResult); 104 unset($session->errorActionId); 105 unset($session->errorCode); 106 unset($session->formData); 107 } 108 if (isset($session->errorActionId)) { 109 $session->clearError = true; 110 } 111 } 112 113 /** 114 * Implementation of the readable properties using the PHP magic 115 * method __get(). 116 * 117 * @param string $name The name of the property to get. 118 * 119 * @return mixed The value of the requested property. 120 */ 121 public function __get($name) { 122 switch($name) { 123 case "type": return $this->type; 124 } 125 throw new \Scrivo\SystemException("No such property-get '$name'."); 126 } 127 128 function setParameters($arr) { 129 $this->parameters = $arr; 130 } 131 132 function setResult($res, $result = null, $form_data = null) { 133 if ($result instanceof \Exception) { 134 $result = $result->getMessage(); 135 } 136 $this->result = $res; 137 $this->session->forwardResult = $res; 138 if ($res == self::FAIL) { 139 $this->session->errorCode = $result!==null ? $result : null; 140 } 141 $this->session->formData = $form_data ? serialize($form_data) : null; 142 $this->resultData = $result; 143 144 $this->session->errorActionId = 145 ($result && isset($this->forward[$res])) 146 ? $this->forward[$res] : null; 147 } 148 149 function forward() { 150 $p = array("a" => $this->forward[$this->result]); 151 if ($this->parameters) { 152 $p = $p + $this->parameters; 153 } 154 array_walk_recursive($p, function(&$a){ 155 if ($a instanceof \Scrivo\String) { 156 $a = (string)$a; 157 } 158 }); 159 $loc = "Location: ?".http_build_query($p, "&"); 160 header($loc); 161 } 162 163 function getView() { 164 ob_start(); 165 include $this->getLayout(); 166 $output = ob_get_contents(); 167 ob_end_clean(); 168 return $output; 169 } 170 171 function getXhr() { 172 $res = array(); 173 if ($this->result == self::FAIL) { 174 $res["result"] = "ERROR"; 175 } else { 176 $res["result"] = "OK"; 177 } 178 $res["data"] = $this->resultData ? $this->resultData : ""; 179 $res = $this->prepareXhr($res); 180 return json_encode($res); 181 } 182 183 function prepareXhr($data) { 184 if (is_array($data)) { 185 foreach ($data as $k=>$v) { 186 $data[$k] = $this->prepareXhr($v); 187 } 188 } else if ($data instanceof \stdClass) { 189 $data = (array)$data; 190 foreach ($data as $k=>$v) { 191 $data[$k] = $this->prepareXhr($v); 192 } 193 } else if ($data instanceof \Scrivo\String) { 194 $data = (string)$data; 195 } 196 return $data; 197 } 198 199 } 200 201 ?>
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013