/gulliver/system/class.controller.php

https://bitbucket.org/ferOnti/processmaker · PHP · 296 lines · 145 code · 33 blank · 118 comment · 14 complexity · 520c5dfe89787f62c910cd75b82ee469 MD5 · raw file

  1. <?php
  2. /**
  3. * Controller Class
  4. * Implementing MVC Pattern
  5. *
  6. * @author Erik Amaru Ortiz <erik@colosa.com, aortiz.erik@gmail.com>
  7. * @package gulliver.system
  8. * @access private
  9. */
  10. class Controller
  11. {
  12. /**
  13. *
  14. * @var boolean debug switch for general purpose
  15. */
  16. public $debug = null;
  17. /**
  18. *
  19. * @var array - private array to store proxy data
  20. */
  21. private $__data__ = array ();
  22. /**
  23. *
  24. * @var object - private object to store the http request data
  25. */
  26. private $__request__;
  27. /**
  28. *
  29. * @var object - headPublisher object to handle the output
  30. */
  31. private $headPublisher = null;
  32. /**
  33. *
  34. * @var string - response type var. possibles values: json|plain
  35. */
  36. private $responseType = '';
  37. /**
  38. *
  39. * @var string - layout to pass skinEngine
  40. */
  41. private $layout = '';
  42. /**
  43. * Magic setter method
  44. *
  45. * @param string $name
  46. * @param string $value
  47. */
  48. public function __set ($name, $value)
  49. {
  50. $this->__data__[$name] = $value;
  51. }
  52. /**
  53. * Magic getter method
  54. *
  55. * @param string $name
  56. * @return string or NULL if the internal var doesn't exist
  57. */
  58. public function __get ($name)
  59. {
  60. if (array_key_exists( $name, $this->__data__ )) {
  61. return $this->__data__[$name];
  62. }
  63. $trace = debug_backtrace();
  64. trigger_error( 'Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE );
  65. return null;
  66. }
  67. /**
  68. * Magic isset method
  69. *
  70. * @param string $name
  71. */
  72. public function __isset ($name)
  73. {
  74. return isset( $this->__data__[$name] );
  75. }
  76. /**
  77. * Magic unset method
  78. *
  79. * @param string $name
  80. */
  81. public function __unset ($name)
  82. {
  83. unset( $this->__data__[$name] );
  84. }
  85. /**
  86. * Set Response type method
  87. *
  88. * @param string $type contains : json|plain
  89. */
  90. public function setResponseType ($type)
  91. {
  92. $this->responseType = $type;
  93. }
  94. /**
  95. * call to execute a internal proxy method and handle its exceptions
  96. *
  97. * @param string $name
  98. */
  99. public function call ($name)
  100. {
  101. try {
  102. $result = $this->$name( $this->__request__ );
  103. if ($this->responseType == 'json') {
  104. print G::json_encode( $result );
  105. }
  106. } catch (Exception $e) {
  107. if ($this->responseType != 'json') {
  108. $result->exception->class = get_class( $e );
  109. $result->exception->code = $e->getCode();
  110. $template = new TemplatePower( PATH_TEMPLATE . 'controller.exception.tpl' );
  111. $template->prepare();
  112. $template->assign( 'controller', (function_exists( 'get_called_class' ) ? get_called_class() : 'Controller') );
  113. $template->assign( 'message', $e->getMessage() );
  114. $template->assign( 'file', $e->getFile() );
  115. $template->assign( 'line', $e->getLine() );
  116. $template->assign( 'trace', $e->getTraceAsString() );
  117. echo $template->getOutputContent();
  118. } else {
  119. $result->success = false;
  120. $result->msg = $e->getMessage();
  121. switch (get_class( $e )) {
  122. case 'Exception':
  123. $error = "SYSTEM ERROR";
  124. break;
  125. case 'PMException':
  126. $error = "PROCESSMAKER ERROR";
  127. break;
  128. case 'PropelException':
  129. $error = "DATABASE ERROR";
  130. break;
  131. case 'UserException':
  132. $error = "USER ERROR";
  133. break;
  134. }
  135. $result->error = $error;
  136. $result->exception->class = get_class( $e );
  137. $result->exception->code = $e->getCode();
  138. print G::json_encode( $result );
  139. }
  140. }
  141. }
  142. /**
  143. * Set the http request data
  144. *
  145. * @param array $data
  146. */
  147. public function setHttpRequestData ($data)
  148. {
  149. if (! is_object( $this->__request__ )) {
  150. $this->__request__ = new stdclass();
  151. }
  152. if (is_array( $data )) {
  153. while ($var = each( $data )) {
  154. $this->__request__->$var['key'] = $var['value'];
  155. }
  156. } else {
  157. $this->__request__ = $data;
  158. }
  159. }
  160. /**
  161. * Get debug var.
  162. * method
  163. *
  164. * @param boolan $val boolean value for debug var.
  165. */
  166. public function setDebug ($val)
  167. {
  168. $this->debug = $val;
  169. }
  170. /**
  171. * Get debug var.
  172. * method
  173. */
  174. public function getDebug ()
  175. {
  176. if ($this->debug === null) {
  177. $this->debug = defined( 'DEBUG' ) && DEBUG ? true : false;
  178. }
  179. return $this->debug;
  180. }
  181. /**
  182. * * HeadPublisher Functions Binding **
  183. */
  184. /**
  185. * Include a particular extjs library or extension to the main output
  186. *
  187. * @param string $srcFile path of a extjs library or extension
  188. * @param boolean $debug debug flag to indicate if the js output will be minifield or not
  189. * $debug: true -> the js content will be not minified (readable)
  190. * false -> the js content will be minified
  191. */
  192. public function includeExtJSLib ($srcFile, $debug = false)
  193. {
  194. $this->getHeadPublisher()->usingExtJs( $srcFile, ($debug ? $debug : $this->getDebug()) );
  195. }
  196. /**
  197. * Include a javascript file that is using extjs framework to the main output
  198. *
  199. * @param string $srcFile path of javascrit file to include
  200. * @param boolean $debug debug flag to indicate if the js output will be minifield or not
  201. * $debug: true -> the js content will be not minified (readable)
  202. * false -> the js content will be minified
  203. */
  204. public function includeExtJS ($srcFile, $debug = false)
  205. {
  206. $this->getHeadPublisher()->addExtJsScript( $srcFile, ($debug ? $debug : $this->getDebug()) );
  207. }
  208. /**
  209. * Include a Html file to the main output
  210. *
  211. * @param string $file path of html file to include to the main output
  212. */
  213. public function setView ($file)
  214. {
  215. $this->getHeadPublisher()->addContent( $file );
  216. }
  217. /**
  218. * Set variables to be accesible by javascripts
  219. *
  220. * @param string $name contains var. name
  221. * @param string $value conatins var. value
  222. */
  223. public function setJSVar ($name, $value)
  224. {
  225. $this->getHeadPublisher()->assign( $name, $value );
  226. }
  227. /**
  228. * Set variables to be accesible by the extjs layout template
  229. *
  230. * @param string $name contains var. name
  231. * @param string $value conatins var. value
  232. */
  233. public function setVar ($name, $value)
  234. {
  235. $this->getHeadPublisher()->assignVar( $name, $value );
  236. }
  237. /**
  238. * method to get the local getHeadPublisher object
  239. */
  240. public function getHeadPublisher ()
  241. {
  242. if (! is_object( $this->headPublisher )) {
  243. $this->headPublisher = headPublisher::getSingleton();
  244. }
  245. return $this->headPublisher;
  246. }
  247. public function setLayout ($layout)
  248. {
  249. $this->layout = $layout;
  250. }
  251. public function render ($type = 'mvc')
  252. {
  253. G::RenderPage( 'publish', $type, null, $this->layout );
  254. }
  255. public function header ($header)
  256. {
  257. G::header( $header );
  258. }
  259. public function redirect ($url)
  260. {
  261. G::header( "Location: $url" );
  262. }
  263. }