/admin/engine/classes/multipleError_handler.php

https://gitlab.com/Nightprince/Warcry-CMS · PHP · 297 lines · 212 code · 35 blank · 50 comment · 20 complexity · e560c7833d68684e3678343eea220e27 MD5 · raw file

  1. <?php
  2. if (!defined('init_engine'))
  3. {
  4. header('HTTP/1.0 404 not found');
  5. exit;
  6. }
  7. class multipleErrors
  8. {
  9. private $multipleErrors;
  10. public $currentMultipleError = NULL;
  11. private $errorKeys = array();
  12. public $success = array();
  13. public function __construct($key)
  14. {
  15. $this->multipleErrors[$key] = NULL;
  16. $this->currentMultipleError = $key;
  17. $this->errorKeys[] = $key;
  18. }
  19. public function NewInstance($key)
  20. {
  21. $this->multipleErrors[$key] = NULL;
  22. $this->currentMultipleError = $key;
  23. $this->errorKeys[] = $key;
  24. }
  25. /**
  26. ** onSuccess set's up the success parameters
  27. **
  28. ** Parameters:
  29. ** ----------------------------------------------------------
  30. ** $message - The message witch will be returned on success trigger
  31. ** $redirect - URL to redirect to on success trigger
  32. ** $key (optional) - Set's up the parameters for the given key
  33. **
  34. **/
  35. public function onSuccess($message, $redirect, $key = false)
  36. {
  37. if (!$key)
  38. {
  39. $this->success[$this->currentMultipleError]['message'] = $message;
  40. $this->success[$this->currentMultipleError]['redirect'] = $redirect;
  41. }
  42. else
  43. {
  44. $this->success[$key]['message'] = $message;
  45. $this->success[$key]['redirect'] = $redirect;
  46. }
  47. }
  48. public function Add($text, $key = false)
  49. {
  50. if (!$key)
  51. {
  52. $this->multipleErrors[$this->currentMultipleError][] = $text;
  53. }
  54. else
  55. {
  56. $this->multipleErrors[$key][] = $text;
  57. }
  58. }
  59. public function multipleError_saveFormData($data, $key = false)
  60. {
  61. if (!$key)
  62. {
  63. $_SESSION['multipleErrors'.$this->currentMultipleError.'FormData'] = $data;
  64. }
  65. else
  66. {
  67. $_SESSION['multipleErrors'.$key.'FormData'] = $data;
  68. }
  69. }
  70. public function multipleError_accessFormData($key)
  71. {
  72. if (isset($_SESSION['multipleErrors'.$key.'FormData']) and !empty($_SESSION['multipleErrors'.$key.'FormData']))
  73. {
  74. $data = $_SESSION['multipleErrors'.$key.'FormData'];
  75. unset($_SESSION['multipleErrors'.$key.'FormData']);
  76. return $data;
  77. }
  78. return false;
  79. }
  80. public function Check($redirect = false, $key = false)
  81. {
  82. global $config, $_POST;
  83. if (!$key)
  84. {
  85. $key = $this->currentMultipleError;
  86. }
  87. //check if we got any errors
  88. if (isset($this->multipleErrors[$key]) and is_array($this->multipleErrors[$key]))
  89. {
  90. //if no redirect is passed just print
  91. if (!$redirect)
  92. {
  93. foreach($this->multipleErrors[$key] as $val)
  94. {
  95. echo $val, '<br>';
  96. }
  97. }
  98. else
  99. {
  100. //save the captured errors to our session
  101. $_SESSION['multipleErrors'.$key] = $this->multipleErrors[$key];
  102. //save the form data if we got some
  103. if (isset($_POST) and !empty($_POST))
  104. {
  105. $this->multipleError_saveFormData($_POST, $key);
  106. }
  107. //redirect
  108. header("Location: ".$config['BaseURL'] . '/admin' . $redirect);
  109. }
  110. exit;
  111. }
  112. }
  113. /**
  114. ** Prints the error(s) passed instantly
  115. **
  116. ** Parameters:
  117. ** --------------------------------------------------
  118. ** $message - The error that will be printed, string or array
  119. ** $print - If set to false the errors will be returned as string
  120. **
  121. **/
  122. public function iPrint($message = false, $print = true)
  123. {
  124. if ($message)
  125. {
  126. $errors = '';
  127. if (is_array($message))
  128. {
  129. //handle array
  130. foreach($message as $val)
  131. {
  132. $errors .= $val . '<br>';
  133. }
  134. $message = $errors;
  135. }
  136. $errors = '<div class="container_3 red" align="left"><span class="error_icons atention"></span><p>'.$message.'</p></div>';
  137. if ($print)
  138. {
  139. echo $errors;
  140. }
  141. else
  142. {
  143. return $errors;
  144. }
  145. }
  146. else
  147. {
  148. return false;
  149. }
  150. }
  151. public function DoPrint($key = false)
  152. {
  153. if (!$key)
  154. {
  155. $key = $this->currentMultipleError;
  156. }
  157. else
  158. {
  159. //check if we want to get multiple keys
  160. if (is_array($key))
  161. {
  162. $string = '';
  163. //loop through the keys and collect the errors
  164. foreach ($key as $k)
  165. {
  166. $string .= $this->DoPrint($k);
  167. }
  168. //return all the errors
  169. return $string;
  170. }
  171. }
  172. //if we got errors, print em
  173. if (isset($_SESSION['multipleErrors'.$key]))
  174. {
  175. $errors = '';
  176. foreach($_SESSION['multipleErrors'.$key] as $val)
  177. {
  178. $errors .= '<script>
  179. $(function()
  180. {
  181. new Notification(\''.$val.'\', \'error\', \'urgent\');
  182. });
  183. </script>';
  184. }
  185. //unset the session data
  186. unset($_SESSION['multipleErrors'.$key]);
  187. return $errors;
  188. }
  189. return false;
  190. }
  191. /**
  192. ** triggerSuccess, parameters must be setup by onSuccess() function
  193. **
  194. ** Parameters:
  195. ** ------------------------------------------------------------------------------------------------------------
  196. ** $key (optional) - The key for witch the success should be triggerd, if none specifed current will be used
  197. **
  198. ** Returns:
  199. ** ------------------------------------------------------------------------------------------------------------
  200. ** success - Returns nothing
  201. ** error - Returns echos error string and returns false
  202. **
  203. **/
  204. public function triggerSuccess($key = false)
  205. {
  206. global $config;
  207. if (!$key)
  208. {
  209. $key = $this->currentMultipleError;
  210. }
  211. if (isset($this->success[$key]))
  212. {
  213. //save the success message
  214. $_SESSION['multipleErrors'.$key.'_success'] = $this->success[$key]['message'];
  215. //redirect
  216. header("Location: ".$config['BaseURL'] . '/admin' . $this->success[$key]['redirect']);
  217. exit;
  218. }
  219. else
  220. {
  221. echo '<br> triggerSuccess() has no record with key: '. $key . '.<br>';
  222. return false;
  223. }
  224. }
  225. public function successPrint($key = false)
  226. {
  227. if (!$key)
  228. {
  229. $key = $this->currentMultipleError;
  230. }
  231. else
  232. {
  233. //check if we want to get multiple keys
  234. if (is_array($key))
  235. {
  236. $string = '';
  237. //loop through the keys and collect the success msgs
  238. foreach ($key as $k)
  239. {
  240. $string .= $this->successPrint($k);
  241. }
  242. //return all the success msgs
  243. return $string;
  244. }
  245. }
  246. //if we got errors, print em
  247. if (isset($_SESSION['multipleErrors'.$key.'_success']))
  248. {
  249. $message = $_SESSION['multipleErrors'.$key.'_success'];
  250. $message = '<script>
  251. $(function()
  252. {
  253. new Notification("'.$message.'", \'success\');
  254. });
  255. </script>';
  256. //unset the session data
  257. unset($_SESSION['multipleErrors'.$key.'_success']);
  258. return $message;
  259. }
  260. return false;
  261. }
  262. }