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

/libs/controller.php

http://j3-php-framework.googlecode.com/
PHP | 289 lines | 189 code | 47 blank | 53 comment | 55 complexity | d86ee6107b87e9056f2cd82c441b2853 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. include("libs/log4php/Logger.php");
  3. require_once("libs/FirePHPCore/FirePHP.class.php");
  4. require_once("libs/localuser.php");
  5. require_once("libs/DomPDF/dompdf_config.inc.php");
  6. /** Clase principal de los controladores.
  7. Incluye metodos utilizados frecuentemente.
  8. */
  9. class Controller {
  10. protected $layout;
  11. protected $contentPage;
  12. protected $pageTitle;
  13. protected $titleShowed;
  14. static public $instancia = null;
  15. protected $myFirePhp;
  16. protected $logger;
  17. protected $continuar;
  18. protected $usuariosPermitidos;
  19. protected $nivelesPermitidos = null;
  20. /** Constructor
  21. */
  22. public function __construct() {
  23. $this->layout = "";
  24. $this->contentPage = "";
  25. $this->pageTitle = "";
  26. $this->titleShowed = "";
  27. $this->usuariosPermitidos = "*";
  28. }
  29. /** Toma la instancia actual para no tener que reinstanciar la clase.
  30. param clase Nombre de la clase.
  31. */
  32. public static function getInstance($clase){
  33. if (self::$instancia == null) {
  34. self::$instancia = new $clase;
  35. } else {
  36. echo "Existente";
  37. }
  38. return self::$instancia;
  39. }
  40. /** Metodo que redirecciona a la pagina dada.
  41. @param page Pagina a la que se desea redireccionar
  42. @param permanent hace la redireccion permanente
  43. */
  44. public function redirect($page, $permanent = false) {
  45. if($permanent) {
  46. header( "HTTP/1.1 301 Moved Permanently" );
  47. }
  48. header("location:".$page);
  49. exit(0);
  50. }
  51. /** Metodo que ejecuta una vista especifica
  52. @param name Nombre del controlador
  53. @param vista Nombre de la vista
  54. */
  55. public function ejecutar($name,$vista) {
  56. require_once 'conf/app.php';
  57. //Firebug
  58. ob_start();
  59. $this->myFirePhp = FirePHP::getInstance(true);
  60. //Logger
  61. Logger::configure('conf/log.xml');
  62. $this->logger = Logger::getRootLogger();
  63. $whatsB = $vista;
  64. $vista = strtolower($vista);
  65. $this->continuar = true;
  66. session_start();
  67. if ($vista) {
  68. if (method_exists($this, $vista) == true) {
  69. call_user_func(array($this, $vista));
  70. if ($this->usuarioPermitido() == false && strcmp($vista,"notificacion") != 0) {
  71. $this->continuar = false;
  72. $this->userNotAllowed();
  73. } else {
  74. if ($this->sesionExpirada() == true) {
  75. $this->continuar = false;
  76. $this->sessionExpired();
  77. }
  78. }
  79. if (strcmp($vista[0], "x") != 0) {
  80. if ($this->continuar == true) {
  81. if (strcmp($vista,"notificacion") == 0) {
  82. $this->showView("vistas/defaults/notificacion.php");
  83. } else
  84. if (file_exists("vistas/$name/$vista.php")) {
  85. $this->showView("vistas/$name/$vista.php");
  86. } else {
  87. echo "ERROR: Vista <strong>$vista.php</strong> no definida. :(";
  88. }
  89. }
  90. }
  91. } else {
  92. $cc = get_class($this);
  93. echo "ERROR: Metodo <strong>$cc/$whatsB</strong> no definido. :(";
  94. }
  95. } else if ($this->continuar == true){
  96. $this->index();
  97. if ($this->usuarioPermitido() == false) {
  98. $this->continuar = false;
  99. $this->userNotAllowed();
  100. } else {
  101. if ($this->sesionExpirada() == true) {
  102. $this->continuar = false;
  103. $this->sessionExpired();
  104. }
  105. }
  106. if (file_exists("vistas/$name/index.php")) {
  107. if ($this->continuar == true) {
  108. $this->showView("vistas/$name/index.php");
  109. }
  110. } else {
  111. echo "ERROR: Vista <strong>index.php</strong> no definida. :(";
  112. }
  113. }
  114. }
  115. /** Metodo que muestra el contenido de la pagina dada en "contentPage".
  116. Usado principalmente en layouts.
  117. */
  118. public function showContent() {
  119. require($this->contentPage);
  120. }
  121. /** Metodo que muestra el contenido de una pagina dada
  122. @param page Pagina a mostrar
  123. */
  124. public function showView($page) {
  125. if (strcmp($this->layout, "") == 0 ) {
  126. require($page);
  127. } else {
  128. if (file_exists("vistas/layouts/".$this->layout.".php")) {
  129. $this->contentPage = $page;
  130. require("vistas/layouts/".$this->layout.".php");
  131. } else {
  132. echo "ERROR: Layout <strong>".$this->layout.".php</strong> no definido. :(";
  133. }
  134. }
  135. }
  136. /** Metodo con la vista por default (index).
  137. */
  138. public function index() {
  139. }
  140. /** Vista por defecto de notificaciones
  141. */
  142. public function notificacion() {
  143. $this->titulo = $_SESSION["notifTitle"];
  144. $this->mensaje = $_SESSION["notifMessage"];
  145. $this->boton = $_SESSION["notifButton"];
  146. $this->enlace = $_SESSION["notifLink"];
  147. $this->titleShowed = $_SESSION["notifBarra"];
  148. $this->layout = $_SESSION["notifLayout"];
  149. unset($_SESSION["notifTitle"]);
  150. unset($_SESSION["notifMessage"]);
  151. unset($_SESSION["notifButton"]);
  152. unset($_SESSION["notifLink"]);
  153. unset($_SESSION["notifBarra"]);
  154. unset($_SESSION["notifLayout"]);
  155. }
  156. /** Funcion que muestra la vista de notificaciones
  157. */
  158. public function showNotificacion($titulo, $mensaje, $boton, $enlace, $layout = "default", $barra = "") {
  159. $_SESSION["notifTitle"] = $titulo;
  160. $_SESSION["notifMessage"] = $mensaje;
  161. $_SESSION["notifButton"] = $boton;
  162. $_SESSION["notifLink"] = $enlace;
  163. $_SESSION["notifBarra"] = $barra;
  164. $_SESSION["notifLayout"] = $layout;
  165. $clase = strtolower(get_class($this));
  166. $this->redirect(substr($clase, 0, strlen($clase)-10)."/notificacion");
  167. }
  168. /** Funcion que verifica si el usuario actualestá permitido para acceder a la funcionalidad
  169. @return bool
  170. */
  171. public function usuarioPermitido() {
  172. $nivel = LocalUser::getcurrentUser()->getNivel();
  173. /*if ($nivel == -1) {
  174. return true;
  175. }*/
  176. if (is_array($this->nivelesPermitidos)) {
  177. foreach ($this->nivelesPermitidos as $niv) {
  178. if ($nivel == $niv)
  179. return true;
  180. }
  181. } else if ($this->nivelesPermitidos == $nivel) {
  182. return true;
  183. } else if ($this->nivelesPermitidos == null) {
  184. return true;
  185. }
  186. return false;
  187. }
  188. /** Funcion que se ejecuta cuando un usuario no está permitido en la funcionalidad
  189. */
  190. public function userNotAllowed() {
  191. echo "Usuario no permitido";
  192. }
  193. /** Funcion que verifica si la sesión ya ha expirado
  194. @return bool
  195. */
  196. public function sesionExpirada() {
  197. $nivel = LocalUser::getcurrentUser()->getNivel();
  198. if ($nivel == -1) {
  199. return false;
  200. }
  201. if ($this->nivelesPermitidos == null) {
  202. return false;
  203. }
  204. $tiempo = LocalUser::getcurrentUser()->getTime();
  205. $actual = time();
  206. //echo "<script type='text/javascript'>alert('$tiempo : $actual');</script>";
  207. if ($actual > $tiempo + SESSION_TIME) {
  208. $this->logger->debug("SESSION: $actual > $tiempo + ".SESSION_TIME." = TRUE");
  209. return true;
  210. } else {
  211. $this->logger->debug("SESSION: $actual > $tiempo + ".SESSION_TIME." = FALSE");
  212. LocalUser::getcurrentUser()->setTime( time() );
  213. return false;
  214. }
  215. }
  216. /** Funcion que se ejecuta cuando la sesión ha expirado (En caso de MANUAL)
  217. */
  218. public function sessionExpired() {
  219. echo "Sesion expirada";
  220. }
  221. /** Funcion que crea un PDF a partir del código HTML que se le indique
  222. @param filename Nombre del archivo PDF
  223. @param html Código HTML a escribir en el PDF
  224. */
  225. public function createPDF($filename, $html) {
  226. $dompdf = new DOMPDF();
  227. $dompdf->load_html($html);
  228. $dompdf->render();
  229. $dompdf->stream($filename . ".pdf");
  230. }
  231. /** Funcion que crea un PDF a partir de la URL indicada (no funciona completamente)
  232. @param filename Nombre del archivo PDF
  233. @param url URL de la página a ser convertida en PDF
  234. */
  235. public function createPDFfromURL($filename, $url) {
  236. $_GET["input_file"] = $url;
  237. $_GET["output_file"] = $filename;
  238. //$_GET["base_path"] = APP_URL;
  239. require_once("libs/DomPDF/dompdf.php");
  240. }
  241. }
  242. ?>