PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/zan/classes/templates.php

https://github.com/triartdesign/ZanPHP
PHP | 467 lines | 452 code | 1 blank | 14 comment | 1 complexity | 2bba283c64031121e6f1d44824b8b0f6 MD5 | raw file
  1. <?php
  2. /**
  3. * ZanPHP
  4. *
  5. * An open source agile and rapid development framework for PHP 5
  6. *
  7. * @package ZanPHP
  8. * @author MilkZoft Developer Team
  9. * @copyright Copyright (c) 2011, MilkZoft, Inc.
  10. * @license http://www.zanphp.com/documentation/en/license/
  11. * @link http://www.zanphp.com
  12. */
  13. /**
  14. * Access from index.php:
  15. */
  16. if(!defined("_access")) {
  17. die("Error: You don't have permission to access here...");
  18. }
  19. /**
  20. * ZanPHP Templates Class
  21. *
  22. * This class is responsible for controlling design templates
  23. *
  24. * @package ZanPHP
  25. * @subpackage core
  26. * @category classes
  27. * @author MilkZoft Developer Team
  28. * @link http://www.zanphp.com/documentation/en/classes/templates_class
  29. */
  30. class ZP_Templates extends ZP_Load {
  31. /**
  32. * Contains the CSS style from an specific application
  33. *
  34. * @var private $CSS = NULL
  35. */
  36. private $CSS = NULL;
  37. /**
  38. *
  39. *
  40. *
  41. */
  42. private $js = NULL;
  43. /**
  44. * Contains the name of the current theme
  45. *
  46. * @var private $theme = NULL
  47. */
  48. private $theme = NULL;
  49. /**
  50. * Contains the path of the theme
  51. *
  52. * @var public $themePath
  53. */
  54. public $themePath;
  55. /**
  56. * Contains the title for the header template
  57. *
  58. * @var private $title = get("webNam"]e
  59. */
  60. private $title;
  61. /**
  62. * Contains the meta tags for the header template
  63. *
  64. * @var private $meta = get("tagsMeta"]
  65. */
  66. private $meta;
  67. /**
  68. * Contains the array of vars
  69. *
  70. * @var public $vars = array()
  71. */
  72. private $vars = array();
  73. /**
  74. * Load helpers: array, browser, debugging, forms, html and web
  75. *
  76. * @return void
  77. */
  78. public function __construct() {
  79. }
  80. /**
  81. * Set the CSS style
  82. *
  83. * @return void
  84. */
  85. public function CSS($CSS = NULL, $application = NULL, $print = FALSE) {
  86. if(file_exists($CSS)) {
  87. if($print) {
  88. print '<link rel="stylesheet" href="'. get("webURL") . _sh . $CSS .'" type="text/css" />' . "\n";
  89. } else {
  90. $this->CSS .= '<link rel="stylesheet" href="'. get("webURL") . _sh . $CSS .'" type="text/css" />' . "\n";
  91. }
  92. }
  93. if($CSS === "bootstrap") {
  94. if(is_null($this->CSS)) {
  95. if($print) {
  96. print '<link rel="stylesheet" href="'. get("webURL") .'/zan/vendors/css/frameworks/bootstrap/bootstrap.min.css" type="text/css" />' . "\n";
  97. } else {
  98. $this->CSS = '<link rel="stylesheet" href="'. get("webURL") .'/zan/vendors/css/frameworks/bootstrap/bootstrap.min.css" type="text/css" />' . "\n";
  99. }
  100. } else {
  101. if($print) {
  102. print '<link rel="stylesheet" href="'. get("webURL") .'/zan/vendors/css/frameworks/bootstrap/bootstrap.min.css" type="text/css" />' . "\n";
  103. } else {
  104. $this->CSS .= '<link rel="stylesheet" href="'. get("webURL") .'/zan/vendors/css/frameworks/bootstrap/bootstrap.min.css" type="text/css" />' . "\n";
  105. }
  106. }
  107. } elseif($CSS === "prettyphoto") {
  108. if(is_null($this->CSS)) {
  109. if($print) {
  110. print '<link rel="stylesheet" href="'. path("vendors/js/lightbox/prettyphoto/css/prettyPhoto.css", "zan") .'" type="text/css" />' . "\n";
  111. } else {
  112. $this->CSS = '<link rel="stylesheet" href="'. path("vendors/js/lightbox/prettyphoto/css/prettyPhoto.css", "zan") .'" type="text/css" />' . "\n";
  113. }
  114. } else {
  115. if($print) {
  116. print '<link rel="stylesheet" href="'. path("vendors/js/lightbox/prettyphoto/css/prettyPhoto.css", "zan") .'" type="text/css" />' . "\n";
  117. } else {
  118. $this->CSS .= '<link rel="stylesheet" href="'. path("vendors/js/lightbox/prettyphoto/css/prettyPhoto.css", "zan") .'" type="text/css" />' . "\n";
  119. }
  120. }
  121. } elseif($CSS === "codemirror") {
  122. if ($print) {
  123. print '<link rel="stylesheet" href="'. path("vendors/js/codemirror/codemirror.css", "zan") .'" type="text/css" />' . "\n";
  124. } else {
  125. if (is_null($this->CSS)) {
  126. $this->CSS = '<link rel="stylesheet" href="'. path("vendors/js/codemirror/codemirror.css", "zan") .'" type="text/css" />' . "\n";
  127. } else {
  128. $this->CSS .= '<link rel="stylesheet" href="'. path("vendors/js/codemirror/codemirror.css", "zan") .'" type="text/css" />' . "\n";
  129. }
  130. }
  131. } elseif($CSS === "redactorjs") {
  132. if ($print) {
  133. print '<link rel="stylesheet" href="'. path("vendors/js/editors/redactorjs/css/redactor.css", "zan") .'" type="text/css" />' . "\n";
  134. } else {
  135. if (is_null($this->CSS)) {
  136. $this->CSS = '<link rel="stylesheet" href="'. path("vendors/js/editors/redactorjs/css/redactor.css", "zan") .'" type="text/css" />' . "\n";
  137. } else {
  138. $this->CSS .= '<link rel="stylesheet" href="'. path("vendors/js/editors/redactorjs/css/redactor.css", "zan") .'" type="text/css" />' . "\n";
  139. }
  140. }
  141. }
  142. $file = is_null($application) ? "www/lib/css/$CSS.css" : "www/applications/$application/views/css/$CSS.css";
  143. if(is_null($this->CSS)) {
  144. if($print) {
  145. print '<link rel="stylesheet" href="'. get("webURL") .'/www/lib/css/default.css" type="text/css" />' . "\n";
  146. } else {
  147. $this->CSS = '<link rel="stylesheet" href="'. get("webURL") .'/www/lib/css/default.css" type="text/css" />' . "\n";
  148. }
  149. }
  150. if(file_exists($file)) {
  151. if($print) {
  152. print '<link rel="stylesheet" href="'. get("webURL") .'/'. $file .'" type="text/css" />' . "\n";
  153. } else {
  154. $this->CSS .= '<link rel="stylesheet" href="'. get("webURL") .'/'. $file .'" type="text/css" />' . "\n";
  155. }
  156. }
  157. }
  158. /**
  159. * Verify if a template exists
  160. *
  161. * @return boolean value
  162. */
  163. public function exists($template, $view = FALSE) {
  164. if(!$view) {
  165. if(file_exists("www/lib/themes/$this->theme/$template.php")) {
  166. return TRUE;
  167. }
  168. } elseif(file_exists("www/applications/$view/views/$template.php")) {
  169. return TRUE;
  170. }
  171. return FALSE;
  172. }
  173. /**
  174. * Get the CSS style
  175. *
  176. * @return void
  177. */
  178. public function getCSS() {
  179. return $this->CSS;
  180. }
  181. /**
  182. * Get the Js
  183. *
  184. * @return void
  185. */
  186. public function getJs() {
  187. return $this->js;
  188. }
  189. /**
  190. * Gets the list of available designs
  191. *
  192. * @return array value
  193. */
  194. public function getThemes($theme) {
  195. $path = "www/lib/themes/";
  196. $dir = dir($path);
  197. $options = FALSE;
  198. $i = 0;
  199. while($element = $dir->read()) {
  200. $directory = $path . $element . _sh;
  201. if($element !== ".." and $element !== "." and is_dir($directory) and $element !== "cpanel") {
  202. if($element === $theme) {
  203. $options[$i]["value"] = $element;
  204. $options[$i]["option"] = $element;
  205. $options[$i]["selected"] = TRUE;
  206. } else {
  207. $options[$i]["value"] = $element;
  208. $options[$i]["option"] = $element;
  209. $options[$i]["selected"] = FALSE;
  210. }
  211. $i++;
  212. }
  213. }
  214. $dir->close();
  215. return $options;
  216. }
  217. /**
  218. * Get the header title
  219. *
  220. * @return void
  221. */
  222. public function getTitle() {
  223. return (is_null($this->title)) ? get("webName") ." - ". get("webSlogan") : encode($this->title);
  224. }
  225. /**
  226. * Get the meta tags
  227. *
  228. * @return void
  229. */
  230. public function getMeta() {
  231. return (is_null($this->meta) ? "" : ltrim($this->meta));
  232. }
  233. /**
  234. * Verify if a theme exists
  235. *
  236. * @return boolean value
  237. */
  238. public function isTheme() {
  239. $this->path = (!is_null($this->theme)) ? "www/lib/themes/$this->theme" : FALSE;
  240. $this->directory = @dir($this->path);
  241. return ($this->directory) ? TRUE : FALSE;
  242. }
  243. /**
  244. *
  245. *
  246. *
  247. */
  248. public function js($js, $application = NULL, $getJs = FALSE) {
  249. if($js == "prettyphoto") {
  250. $js = '<script type="text/javascript" src="'. path("vendors/js/lightbox/prettyphoto/js/jquery.prettyphoto.js", "zan") .'"></script>';
  251. $this->CSS("prettyphoto");
  252. } elseif($js === "jquery") {
  253. $js = '<script type="text/javascript" src="'. path("vendors/js/jquery/jquery.js", "zan") .'"></script>';
  254. } elseif (preg_match("/^jquery\\..+\\.js$/i", $js)){ # Plugin jQuery
  255. $js = '<script type="text/javascript" src="'. path("vendors/js/jquery/$js", "zan") .'"></script>';
  256. } elseif($js === "redactorjs") {
  257. $js = '<script type="text/javascript" src="'. path("vendors/js/editors/redactorjs/redactor.min.js", "zan") .'"></script>';
  258. $this->CSS("redactorjs");
  259. } elseif($js === "lesscss") {
  260. $js = '<script type="text/javascript" src="'. path("vendors/js/less/less.js", "zan") .'"></script>';
  261. } elseif($js === "angular") {
  262. $js = '<script type="text/javascript" src="'. path("vendors/js/angular/angular-1.0.1.min.js", "zan") .'"></script>';
  263. } elseif($js === "codemirror") {
  264. $js = '<script type="text/javascript" src="'. path("vendors/js/codemirror/codemirror.js", "zan") .'"></script>';
  265. $js .= '<script type="text/javascript" src="'. path("vendors/js/codemirror/util/loadmode.js", "zan") .'"></script>';
  266. $this->CSS("codemirror", NULL, TRUE);
  267. } elseif(file_exists($js)) {
  268. $js = '<script type="text/javascript" src="'. path($js, TRUE) .'"></script>';
  269. } elseif(file_exists(path($js, "zan"))) {
  270. $js = '<script type="text/javascript" src="'. path($js, "zan") .'"></script>';
  271. } elseif(file_exists("www/applications/$application/views/js/$js")) {
  272. $js = '<script type="text/javascript" src="'. get("webURL") .'/www/applications/' . $application . '/views/js/' . $js . '"></script>';
  273. } elseif(file_exists("www/applications/$application/views/js/$js.js")) {
  274. $js = '<script type="text/javascript" src="'. get("webURL") .'/www/applications/' . $application . '/views/js/' . $js . '.js"></script>';
  275. } else {
  276. return FALSE;
  277. }
  278. if($getJs) {
  279. return $js;
  280. } else {
  281. $this->js .= $js;
  282. }
  283. }
  284. /**
  285. * Load template
  286. *
  287. * @return void
  288. */
  289. public function load($template, $direct = FALSE) {
  290. if(is_array($this->vars)) {
  291. $key = array_keys($this->vars);
  292. $size = sizeof($key);
  293. for($i = 0; $i < $size; $i++) {
  294. $$key[$i] = $this->vars[$key[$i]];
  295. }
  296. }
  297. if($direct) {
  298. if(is_array($template)) {
  299. $count = count($template);
  300. if($count === 1) {
  301. include $template[0];
  302. } elseif($count === 2) {
  303. include $template[0];
  304. include $template[1];
  305. } elseif($count === 3) {
  306. include $template[0];
  307. include $template[1];
  308. include $template[2];
  309. } else {
  310. include $template[0];
  311. include $template[1];
  312. include $template[2];
  313. include $template[3];
  314. }
  315. } else {
  316. if(!file_exists($template)) {
  317. getException("Error 404: Theme Not Found: " . $template);
  318. }
  319. include $template;
  320. }
  321. } else {
  322. $template = "www/lib/themes/$this->theme/$template.php";
  323. if(!file_exists($template)) {
  324. getException("Error 404: Theme Not Found: " . $template);
  325. }
  326. include $template;
  327. }
  328. }
  329. /**
  330. * Set the current theme
  331. *
  332. * @return void
  333. */
  334. public function theme($theme = NULL) {
  335. $this->theme = (is_null($theme)) ? get("webTheme") : $theme;
  336. $this->themePath = get("webURL") ."/www/lib/themes/$this->theme";
  337. if(!$this->isTheme()) {
  338. die("You need to create a valid theme");
  339. }
  340. }
  341. /**
  342. *
  343. *
  344. *
  345. */
  346. public function themeCSS($theme = NULL) {
  347. $this->helper("browser");
  348. $theme = is_null($theme) ? get("webTheme") : $theme;
  349. $file = "www/lib/themes/". $theme ."/css/style.css";
  350. $browser = browser();
  351. if($browser === "Internet Explorer") {
  352. $style = "www/lib/themes/". $theme ."/css/ie.style.css";
  353. if(file_exists($style)) {
  354. return '<link rel="stylesheet" href="'. $this->themePath .'/css/ie.style.css" type="text/css">';
  355. }
  356. return '<link rel="stylesheet" href="'. $this->themePath .'/css/style.css" type="text/css">';
  357. } else {
  358. return '<link rel="stylesheet" href="'. $this->themePath .'/css/style.css" type="text/css">';
  359. }
  360. }
  361. /**
  362. * Set header title
  363. *
  364. * @return void
  365. */
  366. public function title($title = NULL) {
  367. $this->title = is_null($title) ? get("webName") ." - ". get("webSlogan") : stripslashes($title) ." - ". get("webName");
  368. $this->setMeta("title", $this->title);
  369. }
  370. /**
  371. * Set header meta tag
  372. *
  373. * @return void
  374. */
  375. public function setMeta($tag, $value) {
  376. switch ($tag) {
  377. case "title":
  378. $value = encode(stripslashes($value));
  379. $this->meta .= "\t<meta name=\"$tag\" content=\"$value\" />\n";
  380. break;
  381. case "language":
  382. $this->meta .= "\t<meta http-equiv=\"content-language\" content=\"$value\" />\n";
  383. break;
  384. case "description":
  385. $value = preg_replace("/\r\n+/", " ", strip_tags($value));
  386. if(strlen($value) > 250) {
  387. $abstract = stripslashes(substr($value, 0, strrpos(substr($value, 0, 100), " ")));
  388. $value = stripslashes(substr($value, 0, strrpos(substr($value, 0, 250), " ")));
  389. } else {
  390. $abstract = $value;
  391. }
  392. $this->meta .= "\t<meta name=\"abstract\" content=\"" . $abstract . "\" />\n";
  393. default:
  394. $this->meta .= "\t<meta name=\"$tag\" content=\"$value\" />\n";
  395. break;
  396. }
  397. }
  398. /**
  399. * Set vars
  400. *
  401. * @return void
  402. */
  403. public function vars($vars) {
  404. $this->vars = $vars;
  405. }
  406. }