View.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * MINZ - Copyright 2011 Marien Fressinaud
  4. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  5. */
  6. /**
  7. * La classe View représente la vue de l'application
  8. */
  9. class View {
  10. const VIEWS_PATH_NAME = '/views';
  11. const LAYOUT_PATH_NAME = '/layout';
  12. const LAYOUT_FILENAME = '/layout.phtml';
  13. private $view_filename = '';
  14. private $use_layout = false;
  15. private static $title = '';
  16. private static $styles = array ();
  17. private static $scripts = array ();
  18. private static $params = array ();
  19. /**
  20. * Constructeur
  21. * Détermine si on utilise un layout ou non
  22. */
  23. public function __construct () {
  24. $this->view_filename = APP_PATH
  25. . self::VIEWS_PATH_NAME . '/'
  26. . Request::controllerName () . '/'
  27. . Request::actionName () . '.phtml';
  28. if (file_exists (APP_PATH
  29. . self::LAYOUT_PATH_NAME
  30. . self::LAYOUT_FILENAME)) {
  31. $this->use_layout = true;
  32. }
  33. self::$title = Configuration::title ();
  34. }
  35. /**
  36. * Construit la vue
  37. */
  38. public function build () {
  39. if ($this->use_layout) {
  40. $this->buildLayout ();
  41. } else {
  42. $this->render ();
  43. }
  44. }
  45. /**
  46. * Construit le layout
  47. */
  48. public function buildLayout () {
  49. include (
  50. APP_PATH
  51. . self::LAYOUT_PATH_NAME
  52. . self::LAYOUT_FILENAME
  53. );
  54. }
  55. /**
  56. * Affiche la Vue en elle-même
  57. */
  58. public function render () {
  59. if (file_exists ($this->view_filename)) {
  60. include ($this->view_filename);
  61. } else {
  62. Log::record ('File doesn\'t exist : `'
  63. . $this->view_filename . '`',
  64. Log::NOTICE);
  65. }
  66. }
  67. /**
  68. * Ajoute un élément du layout
  69. * @param $part l'élément partial à ajouter
  70. */
  71. public function partial ($part) {
  72. $fic_partial = APP_PATH
  73. . self::LAYOUT_PATH_NAME . '/'
  74. . $part . '.phtml';
  75. if (file_exists ($fic_partial)) {
  76. include ($fic_partial);
  77. } else {
  78. Log::record ('File doesn\'t exist : `'
  79. . $fic_partial . '`',
  80. Log::WARNING);
  81. }
  82. }
  83. /**
  84. * Affiche un élément graphique situé dans APP./views/helpers/
  85. * @param $helper l'élément à afficher
  86. */
  87. public function renderHelper ($helper) {
  88. $fic_helper = APP_PATH
  89. . '/views/helpers/'
  90. . $helper . '.phtml';
  91. if (file_exists ($fic_helper)) {
  92. include ($fic_helper);
  93. } else {
  94. Log::record ('File doesn\'t exist : `'
  95. . $fic_helper . '`',
  96. Log::WARNING);
  97. }
  98. }
  99. /**
  100. * Permet de choisir si on souhaite utiliser le layout
  101. * @param $use true si on souhaite utiliser le layout, false sinon
  102. */
  103. public function _useLayout ($use) {
  104. $this->use_layout = $use;
  105. }
  106. /**
  107. * Gestion du titre
  108. */
  109. public static function title () {
  110. return self::$title;
  111. }
  112. public static function headTitle () {
  113. return '<title>' . self::$title . '</title>' . "\n";
  114. }
  115. public static function _title ($title) {
  116. self::$title = $title;
  117. }
  118. public static function prependTitle ($title) {
  119. self::$title = $title . self::$title;
  120. }
  121. public static function appendTitle ($title) {
  122. self::$title = self::$title . $title;
  123. }
  124. /**
  125. * Gestion des feuilles de style
  126. */
  127. public static function headStyle () {
  128. $styles = '';
  129. foreach(self::$styles as $style) {
  130. $cond = $style['cond'];
  131. if ($cond) {
  132. $styles .= '<!--[if ' . $cond . ']>';
  133. }
  134. $styles .= '<link rel="stylesheet" type="text/css"';
  135. $styles .= ' media="' . $style['media'] . '"';
  136. $styles .= ' href="' . $style['url'] . '" />';
  137. if ($cond) {
  138. $styles .= '<![endif]-->';
  139. }
  140. $styles .= "\n";
  141. }
  142. return $styles;
  143. }
  144. public static function prependStyle ($url, $media = 'all', $cond = false) {
  145. array_unshift (self::$styles, array (
  146. 'url' => $url,
  147. 'media' => $media,
  148. 'cond' => $cond
  149. ));
  150. }
  151. public static function appendStyle ($url, $media = 'all', $cond = false) {
  152. self::$styles[] = array (
  153. 'url' => $url,
  154. 'media' => $media,
  155. 'cond' => $cond
  156. );
  157. }
  158. /**
  159. * Gestion des scripts JS
  160. */
  161. public static function headScript () {
  162. $scripts = '';
  163. foreach (self::$scripts as $script) {
  164. $cond = $script['cond'];
  165. if ($cond) {
  166. $scripts .= '<!--[if ' . $cond . ']>';
  167. }
  168. $scripts .= '<script type="text/javascript"';
  169. $scripts .= ' src="' . $script['url'] . '">';
  170. $scripts .= '</script>';
  171. if ($cond) {
  172. $scripts .= '<![endif]-->';
  173. }
  174. $scripts .= "\n";
  175. }
  176. return $scripts;
  177. }
  178. public static function prependScript ($url, $cond = false) {
  179. array_unshift(self::$scripts, array (
  180. 'url' => $url,
  181. 'cond' => $cond
  182. ));
  183. }
  184. public static function appendScript ($url, $cond = false) {
  185. self::$scripts[] = array (
  186. 'url' => $url,
  187. 'cond' => $cond
  188. );
  189. }
  190. /**
  191. * Gestion des paramètres ajoutés à la vue
  192. */
  193. public static function _param ($key, $value) {
  194. self::$params[$key] = $value;
  195. }
  196. public function attributeParams () {
  197. foreach (View::$params as $key => $value) {
  198. $this->$key = $value;
  199. }
  200. }
  201. }