PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/core/classes/Post.php

http://github.com/caferrari/vorticephp
PHP | 307 lines | 153 code | 32 blank | 122 comment | 33 complexity | 3ad3768bf963b80c789fbd40e03d9682 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 (!is_array($_POST)) return false;
  135. if ($class == '')
  136. $class = controller;
  137. $class = camelize($class);
  138. if (!class_exists($class)) $class = 'DTO';
  139. $obj = new $class();
  140. foreach ($_POST as $k => $v) $obj->$k = p($k);
  141. return $obj;
  142. }
  143. /**
  144. * Inject posted data into an existing object
  145. *
  146. * @return dto
  147. */
  148. public static function intoObject($obj)
  149. {
  150. if (!is_array($_POST)) return $obj;
  151. foreach ($_POST as $k => $v) $obj->$k = p($k);
  152. return $obj;
  153. }
  154. /**
  155. * Load a object as post data if dont have data sent back by a post error
  156. *
  157. * @return void
  158. */
  159. public static function load($obj, $prefix='')
  160. {
  161. if (self::$hasData) return;
  162. if (is_object($obj))
  163. foreach (get_object_vars($obj) as $c => $v)
  164. self::setVal((isset($prefix[$c]) ? $prefix[$c] : '') . $c, stripslashes($v));
  165. }
  166. /**
  167. * Load a object as post data forced!
  168. *
  169. * @return void
  170. */
  171. public static function forceLoad($obj, $prefix='')
  172. {
  173. if (is_object($obj)) foreach (get_object_vars($obj) as $c => $v) self::setVal((isset($prefix[$c]) ? $prefix[$c] : '') . $c, stripslashes($v));
  174. }
  175. /**
  176. * Set form field value
  177. *
  178. * @param string $c Form field name
  179. * @param string $v Form field value
  180. * @return void
  181. */
  182. public static function setVal($c,$v)
  183. {
  184. self::$form[$c] = $v;
  185. }
  186. /**
  187. * Render message to html
  188. *
  189. * @return string
  190. */
  191. public static function render(){
  192. $tmp = '';
  193. switch (self::$type){
  194. case POST_OK:
  195. $tmp = '<div id="message" class="ok">';
  196. $tmp .='<p>' . self::$message . '</p>';
  197. $tmp .= '</div>';
  198. break;
  199. case POST_ERROR:
  200. if (strlen(self::$message)===0) return;
  201. if (!is_array(self::$errors)) self::$errors = array();
  202. $tmp = '<div id="message" class="error">';
  203. $tmp .= '<p>' . self::$message . '</p>';
  204. $tmp .= '<ul>';
  205. foreach (self::$errors as $error)
  206. $tmp .= is_array($error) ? '<li>' . $error[1] . '</li>' : '<li>' . $error . '</li>';
  207. $tmp .= '</ul>';
  208. $tmp .= '</div>';
  209. break;
  210. default:
  211. return '';
  212. }
  213. return $tmp;
  214. }
  215. /**
  216. * Auto render error/success messages to <!--message--> html comment
  217. *
  218. * @return void
  219. */
  220. public static function autoRender()
  221. {
  222. Vortice::setVar('message', self::render());
  223. }
  224. /**
  225. * Put validation errors on a session and redirect to previews page
  226. *
  227. * @param string $message Errors message
  228. * @param array $erros Errors array
  229. * @return void
  230. */
  231. public static function error($message, $errors=''){
  232. if ($errors=='') $errors = array();
  233. if (!is_array($errors)) throw (new ArrayRequiredException($errors));
  234. foreach($errors as $k => $v) is_array($errors[$k]) ? $errors[$k][1] = e($v[1]) : $errors[$k] = e($v);
  235. $message = e($message);
  236. if (ajax || !isset($_SERVER['HTTP_REFERER']) || Vortice::$rendermode == 'json'){
  237. $tmp = array();
  238. foreach ($errors as $k => $v)
  239. $tmp[] = array('key' => $k, 'value' => $v);
  240. $json = Json::getInstance();
  241. $json->set(0, $message, $tmp);
  242. foreach(DAO::getAll() as $k => $d)
  243. $json->addPackage($k, $d);
  244. if (ajax) header('Content-type: text/json');
  245. exit($json->render());
  246. }else{
  247. Session::set('form_errors', serialize($errors));
  248. Session::set('form_type', POST_ERROR);
  249. Session::set('form_message' , $message);
  250. exit ('<html><head><meta http-equiv="refresh" content="0;URL=' . $_SERVER['HTTP_REFERER'] . '"></head><body></body></html>');
  251. }
  252. }
  253. /**
  254. * Sucess post
  255. *
  256. * @param string $message Sucess message
  257. * @param string $redirec Redirect URL encoded with Link class
  258. * @return void
  259. */
  260. public static function success($message, $redirect=false){
  261. $message = e($message);
  262. if (ajax || Vortice::$rendermode=='json'){
  263. $json = Json::getInstance();
  264. foreach(DAO::getAll() as $k => $d)
  265. $json->addPackage($k, $d);
  266. $json->set(1, $message);
  267. if (ajax) header('Content-type: text/json');
  268. if (!$redirect) exit($json->render());
  269. }else{
  270. Session::set('form_type', POST_OK);
  271. Session::set('form_message' , $message);
  272. }
  273. if ($redirect) redirect($redirect);
  274. }
  275. }