PageRenderTime 26ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/core/model/modx/error/moderror.class.php

https://github.com/francisreboucas/revolution
PHP | 324 lines | 153 code | 19 blank | 152 comment | 40 complexity | 9fb9a41b5531b6d9fdc7bf4277b5ac7c MD5 | raw file
  1. <?php
  2. /*
  3. * MODX Revolution
  4. *
  5. * Copyright 2006-2011 by MODX, LLC.
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it under
  9. * the terms of the GNU General Public License as published by the Free Software
  10. * Foundation; either version 2 of the License, or (at your option) any later
  11. * version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  20. * Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /**
  23. * Error handler for request/response processing.
  24. *
  25. * @package modx
  26. * @subpackage error
  27. */
  28. /**
  29. * Abstract class for error handling and validation for requests.
  30. *
  31. * @abstract Implement a derivative of this class for error handling and
  32. * validation of any kind of MODX request.
  33. * @package modx
  34. * @subpackage error
  35. */
  36. class modError {
  37. /**
  38. * @var array The array of errors
  39. */
  40. public $errors;
  41. /**
  42. * @var string The error message to output.
  43. */
  44. public $message;
  45. /**
  46. * @var modX A reference to the $modx object.
  47. */
  48. public $modx;
  49. /**
  50. * @var integer The total number of errors.
  51. */
  52. public $total = 0;
  53. /**
  54. * @var boolean Indicates failure or success.
  55. */
  56. public $status = false;
  57. /**
  58. * @var array An array of objects to validate against
  59. */
  60. protected $_objects = array();
  61. /**
  62. * @param modX $modx A reference to the modX instance
  63. * @param string $message The default message to send as an error response
  64. */
  65. function __construct(modX &$modx, $message = '') {
  66. $this->modx =& $modx;
  67. $this->message = $message;
  68. $this->errors = array ();
  69. }
  70. /**
  71. * Adds an object to the validation queue.
  72. *
  73. * @access public
  74. * @param xPDOObject $obj An xPDOObject to validate.
  75. */
  76. public function addObjectToValidate(xPDOObject &$obj) {
  77. if (is_object($obj) && $obj instanceof xPDOObject) {
  78. $this->_objects[]= $obj;
  79. }
  80. }
  81. /**
  82. * Checks validation, and if any errors are found, returns them. Error
  83. * handlers that derive from this can determine their own behaviour should
  84. * errors be found.
  85. * @access public
  86. * @param xPDOObject|array $objs An xPDOObject or array of xPDOObjects to
  87. * add to the validation queue.
  88. * @return string The validation message returned.
  89. */
  90. public function checkValidation($objs= array()) {
  91. if (is_object($objs)) {
  92. $this->addObjectToValidate($objs);
  93. }
  94. else if (is_array($objs) && !empty($objs)) {
  95. foreach ($objs as $obj) {
  96. $this->addObjectToValidate($obj);
  97. }
  98. }
  99. return $this->_validate();
  100. }
  101. /**
  102. * Grabs formatted validation messages for all objects in the validation
  103. * queue.
  104. * @access protected
  105. * @return string The compiled validation message returned.
  106. */
  107. protected function _validate() {
  108. $s = '';
  109. /** @var xPDOObject $obj */
  110. foreach ($this->_objects as $obj) {
  111. /** @var modValidator $validator */
  112. if ($validator= $obj->getValidator()) {
  113. $messages= $validator->getMessages();
  114. if (!empty($messages)) {
  115. $fields = array();
  116. foreach ($messages as $message) {
  117. $s .= $message['message'].'<br />'."\n";
  118. if (!isset($fields[$message['field']])) $fields[$message['field']] = array();
  119. $fields[$message['field']][$message['name']] = $message['message'];
  120. }
  121. foreach ($fields as $fieldKey => $field) {
  122. foreach ($field as $fieldMsgName => $fieldMsg) {
  123. $this->addField($fieldKey, $fieldMsg);
  124. }
  125. }
  126. }
  127. }
  128. }
  129. return $s;
  130. }
  131. /**
  132. * Process errors and return a proper output value.
  133. *
  134. * @param string $message The error message to output.
  135. * @param boolean $status Whether or not the action is a success or failure.
  136. * @param object|array $object The object to send back to output.
  137. * @return string|object|array The transformed object data array.
  138. */
  139. public function process($message = '', $status = false, $object = null) {
  140. if (isset($this->modx->registry) && $this->modx->registry->isLogging()) {
  141. $this->modx->registry->resetLogging();
  142. }
  143. if ($status === true) {
  144. $s = $this->_validate();
  145. if ($s !== '') {
  146. $status = false;
  147. $message = $s;
  148. }
  149. }
  150. $this->status = (boolean) $status;
  151. if ($message != '') {
  152. $this->message = $message;
  153. }
  154. $objarray = array ();
  155. if (is_array($object)) {
  156. $obj = reset($object);
  157. if (is_object($obj) && $obj instanceof xPDOObject) {
  158. $this->total = count($object);
  159. }
  160. unset ($obj);
  161. }
  162. $objarray = $this->toArray($object);
  163. return array (
  164. 'success' => $status,
  165. 'message' => $this->message,
  166. 'total' => isset ($this->total) && $this->total != 0 ? $this->total : count($this->errors),
  167. 'errors' => $this->errors,
  168. 'object' => $objarray,
  169. );
  170. }
  171. /**
  172. * Add a specific field error to the error queue.
  173. *
  174. * @param string $name The id of the field.
  175. * @param string $error The error message.
  176. */
  177. public function addField($name, $error) {
  178. $this->errors[] = array (
  179. 'id' => $name,
  180. 'msg' => $error
  181. );
  182. }
  183. /**
  184. * Return the fields added as errors.
  185. *
  186. * @return array An array of errors for specific fields.
  187. */
  188. public function getFields() {
  189. $f = array ();
  190. $errors = array_values(array_filter($this->errors, array($this, 'isFieldError')));
  191. foreach ($errors as $fi) {
  192. $f[] = $fi['msg'];
  193. }
  194. return $f;
  195. }
  196. /**
  197. * Add an error to the error queue.
  198. *
  199. * @param string|array $msg An error message string or custom error array.
  200. */
  201. public function addError($msg) {
  202. $this->errors[] = $msg;
  203. }
  204. /**
  205. * Return all of the errors in the error queue.
  206. *
  207. * @param boolean $includeFields Whether or not to include the fields in the error response
  208. * @return array An array of errors
  209. */
  210. public function getErrors($includeFields = false) {
  211. $errors = $this->errors;
  212. if (!$includeFields) {
  213. $errors = array_values(array_filter($this->errors, array($this, 'isNotFieldError')));
  214. }
  215. return $errors;
  216. }
  217. /**
  218. * Returns true if the error passed to it represents a field error.
  219. *
  220. * @param mixed $error An element of modError::errors.
  221. * @return boolean True if the error is a field error.
  222. */
  223. public function isFieldError($error) {
  224. return (is_array($error) && isset($error['msg']) && isset($error['id']) && count($error) == 2);
  225. }
  226. /**
  227. * Returns true if the error passed to it does not represent a field error.
  228. *
  229. * @param mixed $error An element of modError::errors.
  230. * @return boolean True if the error is not a field error.
  231. */
  232. public function isNotFieldError($error) {
  233. return (!is_array($error) || !(isset($error['msg']) && isset($error['id']) && count($error) == 2));
  234. }
  235. /**
  236. * Check to see if there is any errors on the queue.
  237. *
  238. * @return boolean True if there are errors or a message has been specified.
  239. */
  240. public function hasError() {
  241. return count($this->errors) > 0 || $this->message != '';
  242. }
  243. /**
  244. * Send a failure error message.
  245. *
  246. * @param string $message The error message to send.
  247. * @param object|array|string $object An object to send back to the output.
  248. * @return string|array The failure response
  249. */
  250. public function failure($message = '', $object = null) {
  251. return $this->process($message, false, $object);
  252. }
  253. /**
  254. * Send a success error message.
  255. *
  256. * @param string $message The error message to send.
  257. * @param object|array|string $object An object to send back to the output.
  258. * @return string|array The success response
  259. */
  260. public function success($message = '', $object = null) {
  261. return $this->process($message, true, $object);
  262. }
  263. /**
  264. * Converts an object or objects embedded in an array, to arrays.
  265. *
  266. * This function also makes sure that any members of the array are not PHP
  267. * resource types (e.g. database connections, file handles, etc.).
  268. *
  269. * @param array|xPDOObject|object $object An array or object to process.
  270. * @return array Returns an array representation of the object(s).
  271. */
  272. public function toArray($object) {
  273. $array = array ();
  274. if (is_array($object)) {
  275. while (list ($key, $value) = each($object)) {
  276. if (!is_resource($value)) {
  277. if (is_object($value) || is_array($value)) {
  278. $array[$key] = $this->toArray($value);
  279. } else {
  280. $array[$key] = $value;
  281. }
  282. }
  283. }
  284. }
  285. elseif (is_object($object)) {
  286. if ($object instanceof xPDOObject) {
  287. $array = $this->toArray($object->toArray());
  288. } else {
  289. $array = $this->toArray(get_object_vars($object));
  290. }
  291. }
  292. if ($this->modx->getDebug() === true)
  293. $this->modx->log(xPDO::LOG_LEVEL_DEBUG, "modError::toArray() -- " . print_r($array, true));
  294. return $array;
  295. }
  296. /**
  297. * Resets the error messages.
  298. */
  299. public function reset() {
  300. $this->errors = array();
  301. $this->message = '';
  302. $this->total = 0;
  303. $this->status = true;
  304. }
  305. }