PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jaspion/Controllers/Controller.php

https://github.com/gilmario-kpslow/jaspion
PHP | 242 lines | 166 code | 30 blank | 46 comment | 31 complexity | e74e288002976d73f78a316ffbab6509 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. namespace jaspion\Controllers;
  3. use jaspion\Util\MensagemUtil;
  4. /**
  5. * Description of Action
  6. *
  7. * @author gilmario
  8. */
  9. class Controller {
  10. /**
  11. *
  12. * @var Guardar variáveis que vão sobre a pagina
  13. */
  14. protected $view;
  15. protected $action;
  16. private $script;
  17. private $_css;
  18. private $mensagemService;
  19. function __construct() {
  20. $this->script = '';
  21. $this->view = new \stdClass();
  22. $this->getGlobais();
  23. $this->view->mensagem = "";
  24. $this->mensagemService = new MensagemUtil();
  25. }
  26. /**
  27. * Renderizar uma página dentro do layout
  28. * @param $action
  29. * @param $layout
  30. */
  31. public function render($action, $layout = "layout") {
  32. $this->mensagemSessao();
  33. $this->action = $action;
  34. if ($layout && file_exists("../App/Views/" . $layout . ".phtml")) {
  35. include_once '../App/Views/' . $layout . '.phtml';
  36. } else {
  37. $this->content();
  38. }
  39. }
  40. /**
  41. * Renderizar uma pagina sem layout
  42. * @param $action
  43. */
  44. public function simpleRender($action) {
  45. $this->action = $action;
  46. $this->content();
  47. }
  48. /**
  49. * Carregar o conteudo da view pela página
  50. */
  51. public function content() {
  52. $atual = get_class($this);
  53. $singleClassName = strtolower(str_replace("Controller", "", str_replace("App\\Controllers\\", "", $atual)));
  54. $singleClassName = str_replace("\\", "/", $singleClassName);
  55. include_once '../App/Views/' . $singleClassName . "/" . $this->action . '.phtml';
  56. }
  57. /*
  58. * implementada para ser usada com 1 ou 2 parametro
  59. * caso 1 parametro : adicionar css que esta no resouces/css
  60. * caso 2 parametro : adicionar css que esta no na pasta que corresponde ao primero parametro
  61. */
  62. public function css() {
  63. switch (func_num_args()) {
  64. case 2:
  65. return '<link href="' . DIR_ROOT . '/resources/' . func_get_arg(0) . '/css/' . func_get_arg(1) . '.css" rel="stylesheet"/>';
  66. default:
  67. return '<link href="' . DIR_ROOT . '/resources/css/' . func_get_arg(0) . '.css" rel="stylesheet"/>';
  68. }
  69. }
  70. /*
  71. * implementada para ser usada com 1 ou 2 parametro
  72. * caso 1 parametro : adicionar js que esta no resouces/js
  73. * caso 2 parametro : adicionar js que esta no na pasta que corresponde ao primero parametro
  74. */
  75. public function js() {
  76. switch (func_num_args()) {
  77. case 2:
  78. return '<script src="' . DIR_ROOT . '/resources/' . func_get_arg(0) . '/js/' . func_get_arg(1) . '.js" type="text/javascript"></script>';
  79. default:
  80. return '<script src="' . DIR_ROOT . '/resources/js/' . func_get_arg(0) . '.js" type="text/javascript"></script>';
  81. }
  82. }
  83. /*
  84. * implementada para ser usada com 1 ou 2 parametro
  85. * caso 1 parametro : adicionar imagem que esta no resouces/images
  86. * caso 2 parametro : adicionar imagem que esta no na pasta que corresponde ao primero parametro
  87. */
  88. public function img() {
  89. switch (func_num_args()) {
  90. case 2:
  91. return DIR_ROOT . '/resources/' . func_get_arg(0) . '/images/' . func_get_arg(1);
  92. default:
  93. return DIR_ROOT . '/resources/images/' . func_get_arg(0);
  94. }
  95. }
  96. public function download() {
  97. switch (func_num_args()) {
  98. case 2:
  99. return DIR_ROOT . '/resources/' . func_get_arg(0) . '/downloads/' . func_get_arg(1);
  100. default:
  101. return DIR_ROOT . '/resources/downloads/' . func_get_arg(0);
  102. }
  103. }
  104. public function link($link) {
  105. return DIR_ROOT . $link;
  106. }
  107. public function scripts() {
  108. echo $this->script;
  109. }
  110. public function cssBlock() {
  111. echo $this->_css;
  112. }
  113. public function addScript($script) {
  114. if ($this->script == '') {
  115. $this->script = $this->js($script);
  116. } else {
  117. $this->script .= "\r\n" . $this->js($script);
  118. }
  119. }
  120. public function addAdd($css) {
  121. switch (func_num_args()) {
  122. case 2:
  123. if ($this->_css == '') {
  124. $this->_css = $this->css(func_get_arg(0), func_get_arg(1));
  125. } else {
  126. $this->_css .= "\r\n" . $this->css(func_get_arg(0), func_get_arg(1));
  127. }
  128. break;
  129. default :
  130. if ($this->_css == '') {
  131. $this->_css = $this->css(func_get_arg(0));
  132. } else {
  133. $this->_css .= "\r\n" . $this->css(func_get_arg(0));
  134. }
  135. break;
  136. }
  137. }
  138. public function getGlobais() {
  139. $global = \jaspion\Init\Jaspion::getGlobais();
  140. if (!is_null($global)) {
  141. foreach ($global as $globais) {
  142. $this->criarGlobais($globais->nome, isset($globais->valor) ? $globais->valor : null, isset($globais->use) ? $globais->use : null);
  143. }
  144. }
  145. }
  146. public function criarGlobais($nome, $valor = null, $use = null) {
  147. if (!is_null($use) && !is_null($valor)) {
  148. $objeto = new $use();
  149. $this->view->$nome = $objeto->$valor();
  150. } else if (!is_null($use) && is_null($valor)) {
  151. $objeto = new $use();
  152. $this->view->$nome = $objeto;
  153. } else {
  154. $this->view->$nome = $valor;
  155. }
  156. }
  157. /**
  158. *
  159. * @param type $men
  160. * @param type $tipo (def)
  161. */
  162. public function mensagem($men, $tipo = null) {
  163. switch ($tipo) {
  164. case 0:return $this->view->mensagem = "<div id='alerta' class='alert alert-success' style='text-align:center;'><button type='button' class='close' data-dismiss='alert'>×</button><span class='glyphicon glyphicon-exclamation-sign'></span> " . $men . "</div>";
  165. case 1: return $this->view->mensagem = "<div id='alerta' class='alert alert-danger' style='text-align:center;'> <button type='button' class='close' data-dismiss='alert'>×</button><span class='glyphicon glyphicon-remove-sign'></span> " . $men . "</div>";
  166. case 2:return $this->view->mensagem = "<div id='alerta' class='alert alert-warning' style='text-align:center;'> <button type='button' class='close' data-dismiss='alert'>×</button><span class='glyphicon glyphicon-warning-sign'></span> " . $men . "</div>";
  167. default :return $this->view->mensagem = "<div id='alerta' class='alert alert-info' style='text-align:center;'><button type='button' class='close' data-dismiss='alert'>×</button><span class='glyphicon glyphicon-info-sign'></span> " . $men . "</div>";
  168. }
  169. }
  170. /**
  171. *
  172. * @param type $nome
  173. * @param type $tipo (def)
  174. */
  175. public function mensagemCreate($nome, $tipo = null) {
  176. $men = $this->mensagemService->mensagem($nome);
  177. switch ($tipo) {
  178. case 0:return $this->view->mensagem = "<div id='alerta' class='alert alert-success' style='text-align:center;'><button type='button' class='close' data-dismiss='alert'>×</button><span class='glyphicon glyphicon-exclamation-sign'></span> " . $men . "</div>";
  179. case 1: return $this->view->mensagem = "<div id='alerta' class='alert alert-danger' style='text-align:center;'> <button type='button' class='close' data-dismiss='alert'>×</button><span class='glyphicon glyphicon-remove-sign'></span> " . $men . "</div>";
  180. case 2:return $this->view->mensagem = "<div id='alerta' class='alert alert-warning' style='text-align:center;'> <button type='button' class='close' data-dismiss='alert'>×</button><span class='glyphicon glyphicon-warning-sign'></span> " . $men . "</div>";
  181. default :return $this->view->mensagem = "<div id='alerta' class='alert alert-info' style='text-align:center;'><button type='button' class='close' data-dismiss='alert'>×</button><span class='glyphicon glyphicon-info-sign'></span> " . $men . "</div>";
  182. }
  183. }
  184. public function mensagemSessao() {
  185. $session = new \jaspion\Util\RegistrySession();
  186. if ($session->success) {
  187. $this->view->mensagem = "<div id='alerta' class='alert alert-success' style='text-align:center;'><button type='button' class='close' data-dismiss='alert'>×</button><span class='glyphicon glyphicon-exclamation-sign'></span> " . $session->success . "</div>";
  188. $session->unSetRegistry('success');
  189. }
  190. if ($session->danger) {
  191. $this->view->mensagem = "<div id='alerta' class='alert alert-danger' style='text-align:center;'> <button type='button' class='close' data-dismiss='alert'>×</button><span class='glyphicon glyphicon-remove-sign'></span> " . $session->danger . "</div>";
  192. $session->unSetRegistry('danger');
  193. }
  194. if ($session->warning) {
  195. $this->view->mensagem = "<div id='alerta' class='alert alert-warning' style='text-align:center;'> <button type='button' class='close' data-dismiss='alert'>×</button><span class='glyphicon glyphicon-warning-sign'></span> " . $session->warning . "</div>";
  196. $session->unSetRegistry('warning');
  197. }
  198. if ($session->info) {
  199. $this->view->mensagem = "<div id='alerta' class='alert alert-info' style='text-align:center;'><button type='button' class='close' data-dismiss='alert'>×</button><span class='glyphicon glyphicon-info-sign'></span> " . $session->info . "</div>";
  200. $session->unSetRegistry('info');
  201. }
  202. }
  203. public function mensagemCreateSessao($men, $tipo = null) {
  204. $session = new \jaspion\Util\RegistrySession();
  205. switch ($tipo) {
  206. case 0:return $session->success = $men;
  207. case 1: return $session->danger = $men;
  208. case 2:return $session->warning = $men;
  209. default :return $session->info = $men;
  210. }
  211. }
  212. }