PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Supernova/View.php

https://gitlab.com/madkoding/supernova-php
PHP | 233 lines | 119 code | 24 blank | 90 comment | 8 complexity | 70e46adc3e289422877c33c42efa6c50 MD5 | raw file
  1. <?php
  2. namespace Supernova;
  3. class View extends \Supernova\Controller
  4. {
  5. /**
  6. * Values for template
  7. * @var array
  8. */
  9. public static $values = array();
  10. /**
  11. * Default Template name
  12. * @var string
  13. */
  14. public static $prefix = "Default";
  15. /**
  16. * Default Layout name
  17. * @var string
  18. */
  19. public static $layout = "default";
  20. /**
  21. * Stores model name
  22. * @var string
  23. */
  24. public static $model;
  25. /**
  26. * Stores namespace
  27. * @var string
  28. */
  29. public static $namespace;
  30. /**
  31. * Stores view name
  32. * @var string
  33. */
  34. public static $viewName;
  35. /**
  36. * Stores view layout files
  37. * @var array
  38. */
  39. public static $viewFiles = array();
  40. /**
  41. * Stores HTTP error number to show
  42. * @var integer
  43. */
  44. public static $errorNumber = 0;
  45. /**
  46. * Set values for the template
  47. * @param string $name Field name
  48. * @param mixed $value Values
  49. */
  50. public static function set($name, $value = null)
  51. {
  52. self::$values[$name] = $value;
  53. }
  54. /**
  55. * Set session message
  56. * @param string $msg Message
  57. * @param string $key Key name
  58. */
  59. public static function setMessage($msg, $key = 'message')
  60. {
  61. \Supernova\Session::create($key, $msg);
  62. }
  63. /**
  64. * Get session message
  65. * @param String $key Key name
  66. */
  67. public static function getMessage($key = 'message')
  68. {
  69. //if (empty($this->errors)) {
  70. $msg = \Supernova\Session::read($key);
  71. if (!empty($msg)) {
  72. \Supernova\Session::destroy($key);
  73. return $msg;
  74. }
  75. }
  76. /**
  77. * Generate view file if does not exist
  78. * @param string $model Model name
  79. * @return null
  80. */
  81. private static function generateIfNotExist($model)
  82. {
  83. preg_match("/(index|add|edit|delete)/i", \Supernova\Core::$elements['action'], $matches);
  84. $actionName = ucFirst(current($matches));
  85. $functionName = "view".$actionName;
  86. $Generator = new \Supernova\Blackhole\Create($model);
  87. if (method_exists($Generator, $functionName)) {
  88. $Generator->$functionName();
  89. }
  90. }
  91. /**
  92. * Initialize view parameters
  93. * @return null
  94. */
  95. private static function initialize()
  96. {
  97. self::$model = \Supernova\Inflector\Singularize::word(\Supernova\Core::$elements['controller']);
  98. if (!empty(\Supernova\Core::$elements['prefix'])) {
  99. self::$prefix = \Supernova\Core::$elements['prefix'];
  100. }
  101. self::$namespace = "\App\Model\\".self::$model;
  102. self::$viewName = \Supernova\Inflector\Underscore::word(
  103. \Supernova\Core::$elements['prefix'].
  104. \Supernova\Core::$elements['action']
  105. );
  106. }
  107. /**
  108. * Load view in memory
  109. * @return null
  110. */
  111. private static function loadView()
  112. {
  113. $viewFile = "View".DS.self::$model.DS.self::$viewName.".php";
  114. self::$viewFiles[] = ROOT.DS."App".DS.$viewFile;
  115. self::$viewFiles[] = ROOT.DS."Plugins".DS.self::$model.DS.$viewFile;
  116. self::generateIfNotExist(self::$model);
  117. }
  118. /**
  119. * Show error if view file not found
  120. * @return null
  121. */
  122. private static function viewNotFound()
  123. {
  124. $actionFile = \Supernova\Core::$elements['prefix'].\Supernova\Core::$elements['action'];
  125. $actionFile = \Supernova\Inflector\Underscore::word($actionFile).".php";
  126. $controllerMsg = \Supernova\Core::$elements['controller'];
  127. trigger_error(__("View not found:")." ".$actionFile." ".__('in')." /App/View/".$controllerMsg);
  128. \Supernova\View::setError(404);
  129. }
  130. /**
  131. * Show template and layout
  132. */
  133. public static function render()
  134. {
  135. self::initialize();
  136. self::loadView();
  137. $content_for_layout = self::getContent(self::$viewFiles);
  138. if ($content_for_layout === false) {
  139. self::viewNotFound();
  140. return;
  141. }
  142. $layoutFile = ROOT.DS."Public".DS.self::$prefix.DS.self::$layout.".php";
  143. if (is_readable($layoutFile)) {
  144. include($layoutFile);
  145. } else {
  146. trigger_error(__("Layout")." ".self::$layout.".php ".__("not found in prefix")." ".$prefix);
  147. }
  148. }
  149. /**
  150. * Get template content
  151. * @param array $views Array with template names
  152. * @return mixed Return the first content found or false
  153. */
  154. public static function getContent($views = array())
  155. {
  156. extract(self::$values);
  157. ob_start((ini_get("zlib.output_compression") == 'On') ? "ob_gzhandler" : null);
  158. foreach ($views as $view) {
  159. if (file_exists($view)) {
  160. include($view);
  161. $content = ob_get_contents();
  162. ob_end_clean();
  163. return $content;
  164. }
  165. }
  166. return false;
  167. }
  168. /**
  169. * Include CSS into the layout
  170. * @param string $cssFile CSS file without extension
  171. * @return string HTML Link to CSS
  172. */
  173. public static function includeCss($cssFile)
  174. {
  175. $publicUrl = \Supernova\Route::getPublicUrl();
  176. $prefix = (empty(\Supernova\Core::$elements['prefix'])) ? "Default" : \Supernova\Core::$elements['prefix'];
  177. $output = "<link rel='stylesheet' href='$publicUrl/$prefix/css/$cssFile.css' />";
  178. return $output;
  179. }
  180. /**
  181. * Include JS into the layout
  182. * @param string $jsFile JS file without extension
  183. * @return string HTML link to JS
  184. */
  185. public static function includeJs($jsFile)
  186. {
  187. $publicUrl = \Supernova\Route::getPublicUrl();
  188. $prefix = (empty(\Supernova\Core::$elements['prefix'])) ? "Default" : \Supernova\Core::$elements['prefix'];
  189. $output = "<script type='text/javascript' src='$publicUrl/$prefix/js/$jsFile.js' ></script>";
  190. return $output;
  191. }
  192. /**
  193. * Set HTTP error number
  194. * @param integer $num HTTP Number error
  195. */
  196. public static function setError($num = 404)
  197. {
  198. self::$errorNumber = $num;
  199. }
  200. /**
  201. * Call to the error page
  202. * @param string $encodedError Encripted error string
  203. */
  204. public static function callError($encodedError = "")
  205. {
  206. include(ROOT.DS.\Supernova\Route::$params['publicFolder'].DS."errors".DS.self::$errorNumber.'.php');
  207. die();
  208. }
  209. }