PageRenderTime 130ms CodeModel.GetById 40ms RepoModel.GetById 3ms app.codeStats 0ms

/sample/core/classes/Post.php

https://github.com/caferrari/vortice_book
PHP | 300 lines | 149 code | 29 blank | 122 comment | 33 complexity | eef004fec92f5032b2c150728e260db9 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * Copyright (c) 2008, Carlos André Ferrari <[carlos@]ferrari.eti.br>; Luan Almeida <[luan@]luan.eti.br>
  4. * All rights reserved.
  5. */
  6. /**
  7. * Post class, Work with Form Posted data
  8. *
  9. * @version 1
  10. * @package Framework
  11. * @author Carlos André Ferrari <carlos@ferrari.eti.br>
  12. */
  13. class Post
  14. {
  15. /**
  16. * Errors array
  17. *
  18. * @staticvar array
  19. * @access private
  20. */
  21. private static $errors = array();
  22. /**
  23. * Form fields array
  24. *
  25. * @staticvar array
  26. * @access private
  27. */
  28. private static $form = array();
  29. /**
  30. * Error/Sucess message
  31. *
  32. * @staticvar string
  33. * @access public
  34. */
  35. public static $message = '';
  36. /**
  37. * Message type
  38. *
  39. * @staticvar int
  40. * @access public
  41. */
  42. public static $type;
  43. /**
  44. * Have data sent back via post error?
  45. *
  46. * @staticvar bool
  47. * @access public
  48. */
  49. public static $hasData = false;
  50. /**
  51. * Constructor
  52. * @return void
  53. */
  54. private function __construct(){
  55. throw (new Exception('Don\'t do that!!'));
  56. }
  57. /**
  58. * Load sucess or form data and error message if exists
  59. *
  60. * @return void
  61. */
  62. public static function start(){
  63. define ('POST_ERROR', 0);
  64. define ('POST_OK', 1);
  65. global $_PAR;
  66. if (!is_array($_PAR)) $_PAR = array();
  67. foreach (array_merge($_POST, $_GET, $_PAR) as $k => $v)
  68. if ($v!='' && ($k=='id' || preg_match('@^id[_\-]@', $k)) && !is_numeric($v)) throw new VorticeException('Integer Required', 'Any parameter started with \'id\' must be an Integer', '403');
  69. self::$form = array();
  70. $tmp = unserialize(Session::get('form_val'));
  71. self::$form = is_array($tmp) ? $tmp : array();
  72. if (count($tmp) != '')
  73. self::$hasData = true;
  74. $tmp = @unserialize(Session::get('form_errors'));
  75. self::$errors = (is_array($tmp) && count($tmp) > 0) ? $tmp : "";
  76. if (Session::get('form_message'))
  77. {
  78. self::$message = Session::get('form_message');
  79. self::$type = Session::get('form_type');
  80. }
  81. foreach ($_POST as $k => $v) self::setVal($k, $v);
  82. Session::del('form_errors');
  83. Session::del('form_val');
  84. Session::del('form_type');
  85. Session::del('form_message');
  86. Session::set('form_val', serialize($_POST));
  87. }
  88. /**
  89. * Return the message type
  90. *
  91. * @return int
  92. */
  93. public static function getType(){
  94. return self::$type;
  95. }
  96. /**
  97. * Return errors
  98. *
  99. * @return array
  100. */
  101. public static function getErrors(){
  102. return self::$errors;
  103. }
  104. /**
  105. * Return errors
  106. *
  107. * @return array
  108. */
  109. public static function getError($field=''){
  110. if (isset(self::$errors[$field])) return self::$errors[$field];
  111. return false;
  112. }
  113. /**
  114. * Return form field value
  115. *
  116. * @param string $c Form field name
  117. * @return string
  118. */
  119. public static function getVal($c){
  120. $v = stripslashes((isset(self::$form[$c])) ? self::$form[$c] : '');
  121. return str_replace(
  122. array('"'),
  123. array('&quot;'),
  124. $v
  125. );
  126. }
  127. /**
  128. * DTO object factory
  129. *
  130. * @return dto
  131. */
  132. public static function toObject($class = '')
  133. {
  134. if ($class == '') $class = ucfirst(controller);
  135. if (!class_exists($class)) $class = 'DTO';
  136. $obj = new $class();
  137. if (!is_array($_POST)) return false;
  138. foreach ($_POST as $k => $v) $obj->$k = p($k);
  139. return $obj;
  140. }
  141. /**
  142. * Inject posted data into an existing object
  143. *
  144. * @return dto
  145. */
  146. public static function intoObject($obj)
  147. {
  148. if (!is_array($_POST)) return $obj;
  149. foreach ($_POST as $k => $v) $obj->$k = p($k);
  150. return $obj;
  151. }
  152. /**
  153. * Load a object as post data if dont have data sent back by a post error
  154. *
  155. * @return void
  156. */
  157. public static function load($obj, $prefix='')
  158. {
  159. if (self::$hasData) return;
  160. if (is_object($obj)) foreach (get_object_vars($obj) as $c => $v) self::setVal((isset($prefix[$c]) ? $prefix[$c] : '') . $c, stripslashes($v));
  161. }
  162. /**
  163. * Load a object as post data forced!
  164. *
  165. * @return void
  166. */
  167. public static function forceLoad($obj, $prefix='')
  168. {
  169. if (is_object($obj)) foreach (get_object_vars($obj) as $c => $v) self::setVal((isset($prefix[$c]) ? $prefix[$c] : '') . $c, stripslashes($v));
  170. }
  171. /**
  172. * Set form field value
  173. *
  174. * @param string $c Form field name
  175. * @param string $v Form field value
  176. * @return void
  177. */
  178. public static function setVal($c,$v)
  179. {
  180. self::$form[$c] = $v;
  181. }
  182. /**
  183. * Render message to html
  184. *
  185. * @return string
  186. */
  187. public static function render(){
  188. $tmp = '';
  189. switch (self::$type){
  190. case POST_OK:
  191. $tmp = '<div id="message" class="ok">';
  192. $tmp .='<p>' . self::$message . '</p>';
  193. $tmp .= '</div>';
  194. break;
  195. case POST_ERROR:
  196. if (strlen(self::$message)===0) return;
  197. if (!is_array(self::$errors)) self::$errors = array();
  198. $tmp = '<div id="message" class="error">';
  199. $tmp .= '<p>' . self::$message . '</p>';
  200. $tmp .= '<ul>';
  201. foreach (self::$errors as $error)
  202. $tmp .= is_array($error) ? '<li>' . $error[1] . '</li>' : '<li>' . $error . '</li>';
  203. $tmp .= '</ul>';
  204. $tmp .= '</div>';
  205. break;
  206. default:
  207. return '';
  208. }
  209. return $tmp;
  210. }
  211. /**
  212. * Auto render error/success messages to <!--message--> html comment
  213. *
  214. * @return void
  215. */
  216. public static function autoRender()
  217. {
  218. Vortice::setVar('message', self::render());
  219. }
  220. /**
  221. * Put validation errors on a session and redirect to previews page
  222. *
  223. * @param string $message Errors message
  224. * @param array $erros Errors array
  225. * @return void
  226. */
  227. public static function error($message, $errors=''){
  228. if ($errors=='') $errors = array();
  229. if (!is_array($errors)) throw (new ArrayRequiredException($errors));
  230. foreach($errors as $k => $v) is_array($errors[$k]) ? $errors[$k][1] = e($v[1]) : $errors[$k] = e($v);
  231. $message = e($message);
  232. if (ajax || !isset($_SERVER['HTTP_REFERER']) || Vortice::$rendermode == 'json'){
  233. $tmp = array();
  234. foreach ($errors as $k => $v)
  235. $tmp[] = array('key' => $k, 'value' => $v);
  236. $json = Json::getInstance();
  237. $json->set(0, $message, $tmp);
  238. foreach(DAO::getAll() as $k => $d)
  239. $json->addPackage($k, $d);
  240. if (ajax) header('Content-type: text/json');
  241. exit($json->render());
  242. }else{
  243. Session::set('form_errors', serialize($errors));
  244. Session::set('form_type', POST_ERROR);
  245. Session::set('form_message' , $message);
  246. exit ('<html><head><meta http-equiv="refresh" content="0;URL=' . $_SERVER['HTTP_REFERER'] . '"></head><body></body></html>');
  247. }
  248. }
  249. /**
  250. * Sucess post
  251. *
  252. * @param string $message Sucess message
  253. * @param string $redirec Redirect URL encoded with Link class
  254. * @return void
  255. */
  256. public static function success($message, $redirect=false){
  257. $message = e($message);
  258. if (ajax || Vortice::$rendermode=='json'){
  259. $json = Json::getInstance();
  260. foreach(DAO::getAll() as $k => $d)
  261. $json->addPackage($k, $d);
  262. $json->set(1, $message);
  263. if (ajax) header('Content-type: text/json');
  264. if (!$redirect) exit($json->render());
  265. }else{
  266. Session::set('form_type', POST_OK);
  267. Session::set('form_message' , $message);
  268. }
  269. if ($redirect) redirect($redirect);
  270. }
  271. }