PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/zan/classes/load.php

https://github.com/triartdesign/ZanPHP
PHP | 644 lines | 525 code | 30 blank | 89 comment | 51 complexity | 947752af942e2c8d2a1587e4cfcfb8dc 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. * @version 1.0
  13. */
  14. /**
  15. * Access from index.php
  16. */
  17. if(!defined("_access")) {
  18. die("Error: You don't have permission to access here...");
  19. }
  20. /**
  21. * Includes Singleton
  22. */
  23. include "singleton.php";
  24. /**
  25. * ZanPHP Load Class
  26. *
  27. * This class is used to load models, views, controllers, classes, libraries, helpers as well as interact directly with templates
  28. *
  29. * @package ZanPHP
  30. * @subpackage core
  31. * @category classes
  32. * @author MilkZoft Developer Team
  33. * @link http://www.zanphp.com/documentation/en/classes/load_class
  34. */
  35. class ZP_Load {
  36. /**
  37. *
  38. *
  39. *
  40. */
  41. public $application = FALSE;
  42. /**
  43. * Contains an Instance (object) of the Templates Class
  44. *
  45. * @object public $Templates
  46. */
  47. public $Templates;
  48. /**
  49. * Contains the array of views
  50. *
  51. * @var private $views = array()
  52. */
  53. private $views = array();
  54. public $ZP;
  55. /**
  56. * Loads helper autoload, database config and class templates
  57. *
  58. * @return void
  59. */
  60. public function __construct() {
  61. $this->helper(array("autoload", "config", "validations"));
  62. }
  63. /**
  64. *
  65. *
  66. * @param string
  67. * @return
  68. */
  69. public function app($application) {
  70. $this->application = $application;
  71. return $application;
  72. }
  73. /**
  74. * Loads an application class
  75. *
  76. * @param string $class = NULL
  77. * @param string $application = NULL
  78. * @return object value
  79. */
  80. public function classes($name, $className = NULL, $params = array(), $application = NULL) {
  81. if(file_exists("www/applications/$application/classes/$name.php")) {
  82. include_once "www/applications/$application/classes/$name.php";
  83. } elseif(file_exists("www/classes/$name.php")) {
  84. include_once "www/classes/$name.php";
  85. } else {
  86. getException("$name class doesn't exists");
  87. }
  88. return ($className) ? ZP_Singleton::instance($className, $params) : TRUE;
  89. }
  90. /**
  91. * Loads a config file
  92. *
  93. * @param string $name
  94. * @param bool $application = FALSE
  95. * @return void
  96. */
  97. public function config($name, $application = FALSE) {
  98. if($application) {
  99. if(file_exists("www/applications/$application/config/$name.php")) {
  100. include_once "www/applications/$application/config/$name.php";
  101. }
  102. } elseif(file_exists("www/config/$name.php")) {
  103. include_once "www/config/$name.php";
  104. } else {
  105. if(file_exists("www/applications/$name/config/$name.php")) {
  106. include_once "www/applications/$name/config/$name.php";
  107. }
  108. }
  109. }
  110. /**
  111. * Loads a controller
  112. *
  113. * @param string $name
  114. * @return object value
  115. */
  116. public function controller($controller, $application = NULL) {
  117. $parts = explode("_", $controller);
  118. if(!$this->application) {
  119. if(file_exists("www/applications/$application/controllers/". strtolower($parts[0]) .".php")) {
  120. $file = "www/applications/$application/controllers/". strtolower($parts[0]) .".php";
  121. } elseif(count($parts) === 2) {
  122. $file = "www/applications/". strtolower($parts[0]) ."/controllers/". strtolower($parts[0]) .".php";
  123. }
  124. } else {
  125. if(file_exists("www/applications/$application/controllers/". strtolower($parts[0]) .".php")) {
  126. $file = "www/applications/$application/controllers/". strtolower($parts[0]) .".php";
  127. } elseif(file_exists("www/applications/$this->application/controllers/". strtolower($parts[0]) .".php")) {
  128. $file = "www/applications/$this->application/controllers/". strtolower($parts[0]) .".php";
  129. } else {
  130. $file = "www/applications/". strtolower($parts[0]) ."/controllers/". strtolower($parts[0]) .".php";
  131. }
  132. }
  133. if(file_exists($file)) {
  134. if(class_exists($controller)) {
  135. return ZP_Singleton::instance($controller);
  136. }
  137. include $file;
  138. return ZP_Singleton::instance($controller);
  139. }
  140. return FALSE;
  141. }
  142. /**
  143. * Loads a core class
  144. *
  145. * @param string $class
  146. * @return object value
  147. */
  148. public function core($core) {
  149. return ZP_Singleton::instance("ZP_$core");
  150. }
  151. /**
  152. * Sets a CSS file from an specific application
  153. *
  154. * @param string $CSS = NULL
  155. * @param string $application = NULL
  156. * @param bool $print = FALSE
  157. * @return void
  158. */
  159. public function CSS($CSS = NULL, $application = NULL, $print = FALSE) {
  160. $this->Templates = $this->core("Templates");
  161. $this->Templates->CSS($CSS, $application, $print);
  162. }
  163. public function driver($driver = NULL, $type = "db") {
  164. if(file_exists(_corePath ."/drivers/$type/". strtolower($driver) .".php")) {
  165. $file = _corePath ."/drivers/$type/". strtolower($driver) .".php";
  166. } else {
  167. $file = FALSE;
  168. }
  169. if(file_exists($file)) {
  170. include $file;
  171. return ZP_Singleton::instance("ZP_". $driver);
  172. } else {
  173. getException("$driver driver does not exists");
  174. }
  175. }
  176. public function exception($exception) {
  177. $exception = strtolower($exception);
  178. if(file_exists("www/lib/exceptions/$exception.php")) {
  179. include_once "www/lib/exceptions/$exception.php";
  180. } else {
  181. return FALSE;
  182. }
  183. }
  184. public function execute($Class, $method, $params = array(), $type = "controller") {
  185. if($type === "controller") {
  186. $this->$Class = $this->controller($Class);
  187. } elseif($type === "model") {
  188. $this->$Class = $this->model($Class);
  189. }
  190. return ($this->$Class) ? call_user_func_array(array($this->$Class, $method), is_array($params) ? $params : array()) : FALSE;
  191. }
  192. /**
  193. * Loads a footer template
  194. *
  195. * @return void
  196. */
  197. private function footer() {
  198. if($this->Templates->exists("footer")) {
  199. if(count($this->views) > 0) {
  200. for($i = 0; $i <= count($this->views) - 1; $i++) {
  201. if($this->views[$i]["vars"] !== FALSE) {
  202. $this->Templates->vars($this->views[$i]["vars"]);
  203. }
  204. }
  205. }
  206. $this->Templates->load("footer");
  207. }
  208. }
  209. /**
  210. * Load header template
  211. *
  212. * @return void
  213. */
  214. private function header() {
  215. if($this->Templates->exists("header")) {
  216. if(count($this->views) > 0) {
  217. for($i = 0; $i <= count($this->views) - 1; $i++) {
  218. if($this->views[$i]["vars"] !== FALSE) {
  219. $this->Templates->vars($this->views[$i]["vars"]);
  220. }
  221. }
  222. }
  223. $this->Templates->load("header");
  224. }
  225. }
  226. /**
  227. * Loads a helper or multiple helper files
  228. *
  229. * @param mixed $helper
  230. * @param string $application
  231. * @return void
  232. */
  233. public function helper($helper, $application = NULL) {
  234. if(is_array($helper)) {
  235. for($i = 0; $i <= count($helper) - 1; $i++) {
  236. if($application === NULL) {
  237. if(file_exists(_corePath . "/helpers/". $helper[$i] .".php")) {
  238. include_once _corePath . "/helpers/". $helper[$i] .".php";
  239. } elseif(file_exists("www/helpers/". $helper[$i] .".php")) {
  240. include_once "www/helpers/". $helper[$i] .".php";
  241. } else {
  242. getException($helper[$i] ." helper doesn't exists");
  243. }
  244. } else {
  245. if(file_exists("www/applications/$application/helpers/". $helper[$i] .".php")) {
  246. include_once "www/applications/$application/helpers/". $helper[$i] .".php";
  247. } else {
  248. getException($helper[$i] ." helper doesn't exists");
  249. }
  250. }
  251. }
  252. } else {
  253. if(is_null($application)) {
  254. if(file_exists(_corePath ."/helpers/$helper.php")) {
  255. include_once _corePath ."/helpers/$helper.php";
  256. } elseif(file_exists("www/helpers/$helper.php")) {
  257. include_once "www/helpers/$helper.php";
  258. } else {
  259. getException("$helper helper doesn't exists");
  260. }
  261. } else {
  262. if(file_exists("www/applications/$application/helpers/$helper.php")) {
  263. include_once "www/applications/$application/helpers/$helper.php";
  264. } else {
  265. getException("$helper helper doesn't exists");
  266. }
  267. }
  268. }
  269. }
  270. /**
  271. * Loads a hook or multiple hook files
  272. *
  273. * @param string $hook
  274. * @param string $application = NULL
  275. * @return void
  276. */
  277. public function hook($hook, $application = NULL) {
  278. if(is_array($hook)) {
  279. for($i = 0; $i <= count($hook) - 1; $i++) {
  280. if(is_null($application)) {
  281. if(file_exists(_corePath ."/hooks/". $hook[$i] .".php")) {
  282. include_once _corePath ."/hooks/". $hook[$i] .".php";
  283. } else {
  284. getException("$name hook doesn't exists");
  285. }
  286. } else {
  287. if(file_exists("www/applications/$application/hooks/". $hook[$i] .".php")) {
  288. include_once "www/applications/$application/hooks/". $hook[$i] .".php";
  289. } else {
  290. getException("$name hook doesn't exists");
  291. }
  292. }
  293. }
  294. } else {
  295. if(is_null($application)) {
  296. if(file_exists(_corePath ."hooks/$hook.php")) {
  297. include_once _corePath ."hooks/$hook.php";
  298. } else {
  299. getException("$name hook doesn't exists");
  300. }
  301. } else {
  302. if(file_exists("www/applications/$application/hooks/$hook.php")) {
  303. include_once "www/applications/$application/hooks/$hook.php";
  304. } else {
  305. getException("$name hook doesn't exists");
  306. }
  307. }
  308. }
  309. }
  310. /**
  311. * Sets a JS file from an specific application
  312. *
  313. * @param string $script
  314. * @param string $application = NULL
  315. * @param bool $extra = NULL
  316. * @param bool $getJs = FALSE
  317. * @return void
  318. */
  319. public function js($script, $application = NULL, $extra = NULL, $getJs = FALSE) {
  320. $this->Templates = $this->core("Templates");
  321. $this->Templates->js($script, $application, $extra, $getJs);
  322. }
  323. /**
  324. * Loads a left template
  325. *
  326. * @return void
  327. */
  328. private function left() {
  329. if($this->Templates->exists("left")) {
  330. if(count($this->views) > 0) {
  331. for($i = 0; $i <= count($this->views) - 1; $i++) {
  332. if($this->views[$i]["vars"] !== FALSE) {
  333. $this->Templates->vars($this->views[$i]["vars"]);
  334. }
  335. }
  336. }
  337. $this->Templates->load("left");
  338. }
  339. }
  340. /**
  341. * Loads a library file
  342. *
  343. * @param string $name
  344. * @param string $className
  345. * @param array $params
  346. * @param string $application
  347. * @return void
  348. */
  349. public function library($name, $className = NULL, $params = array(), $application = NULL) {
  350. if(file_exists(_corePath ."/libraries/$application/$name.php")) {
  351. include_once _corePath ."/libraries/$application/$name.php";
  352. } elseif(file_exists(_corePath ."/libraries/$className/$name.php")) {
  353. include_once _corePath ."/libraries/$lib/$name.php";
  354. } elseif(file_exists("www/applications/$application/libraries/$name.php")) {
  355. include_once "www/applications/$application/libraries/$name.php";
  356. } else {
  357. getException("$name library doesn't exists");
  358. }
  359. return ($className) ? ZP_Singleton::instance($className, $params) : TRUE;
  360. }
  361. /**
  362. * Loads a model file
  363. *
  364. * @param string $name
  365. * @return object value
  366. */
  367. public function model($model) {
  368. $parts = explode("_", $model);
  369. if(!$this->application) {
  370. if(count($parts) === 2) {
  371. $file = "www/applications/". strtolower($parts[0]) ."/models/". strtolower($parts[0]) .".php";
  372. }
  373. } else {
  374. if(count($parts) === 2) {
  375. if(file_exists("www/applications/$this->application/models/". strtolower($parts[0]) .".php")) {
  376. $file = "www/applications/$this->application/models/". strtolower($parts[0]) .".php";
  377. } elseif(file_exists("www/applications/$this->application/models/". strtolower($parts[0]) .".php")) {
  378. $file = "www/applications/$this->application/models/". strtolower($parts[0]) .".php";
  379. } else {
  380. $file = "www/applications/". strtolower($parts[0]) ."/models/". strtolower($parts[0]) .".php";
  381. }
  382. }
  383. }
  384. if(file_exists($file)) {
  385. if(class_exists($model)) {
  386. return ZP_Singleton::instance($model);
  387. }
  388. require $file;
  389. return ZP_Singleton::instance($model);
  390. }
  391. return FALSE;
  392. }
  393. /**
  394. * Render and output templates
  395. *
  396. * @return void
  397. */
  398. public function rendering() {
  399. $numArgs = func_num_args();
  400. $args = func_get_args();
  401. if($numArgs > 0) {
  402. for($i = 0; $i <= $numArgs - 1; $i++) {
  403. if($this->views[$i]["vars"]) {
  404. $this->Templates->vars($this->views[$i]["vars"]);
  405. }
  406. if(isset($args[$i]) and $args[$i] === "header") {
  407. $this->header();
  408. }
  409. if($args[$i] !== "header" and $args[$i] !== "left" and $args[$i] !== "right" and $args[$i] !== "footer") {
  410. if($this->Templates->exists($args[$i])) {
  411. $this->Templates->load($args[$i]);
  412. continue;
  413. }
  414. }
  415. if(isset($args[$i]) and $args[$i] === "left") {
  416. $this->left();
  417. }
  418. if(count($this->views) > 0) {
  419. for($i = 0; $i <= count($this->views) - 1; $i++) {
  420. $this->Templates->load($this->views[$i]["name"]);
  421. }
  422. }
  423. if(isset($args[$i]) and $args[$i] === "right") {
  424. $this->right();
  425. }
  426. if(isset($args[$i]) and $args[$i] === "footer") {
  427. $this->footer();
  428. }
  429. }
  430. } else {
  431. for($i = 0; $i <= count($this->views) - 1; $i++) {
  432. if($this->views[$i]["vars"]) {
  433. $this->Templates->vars($this->views[$i]["vars"]);
  434. }
  435. }
  436. $this->header();
  437. $this->left();
  438. if(count($this->views) > 0) {
  439. for($i = 0; $i <= count($this->views) - 1; $i++) {
  440. $this->Templates->load($this->views[$i]["name"]);
  441. }
  442. }
  443. $this->right();
  444. $this->footer();
  445. }
  446. }
  447. /**
  448. * Loads a right template
  449. *
  450. * @return void
  451. */
  452. private function right() {
  453. if($this->Templates->exists("right")) {
  454. if(count($this->views) > 0) {
  455. for($i = 0; $i <= count($this->views) - 1; $i++) {
  456. if($this->views[$i]["vars"] !== FALSE) {
  457. $this->Templates->vars($this->views[$i]["vars"]);
  458. }
  459. }
  460. }
  461. $this->Templates->load("right");
  462. }
  463. }
  464. /**
  465. * Loads templates
  466. *
  467. * @param string $name
  468. * @param string $vars
  469. * @return string value / void
  470. */
  471. public function render($name, $vars = NULL) {
  472. if(is_array($vars)) {
  473. if(count($this->views) === 0) {
  474. $this->views[0]["name"] = $name;
  475. $this->views[0]["vars"] = $vars;
  476. } else {
  477. $i = count($this->views);
  478. $this->views[$i]["name"] = $name;
  479. $this->views[$i]["vars"] = $vars;
  480. }
  481. } else {
  482. $i = count($this->views);
  483. $this->views[$i]["name"] = $name;
  484. $this->views[$i]["vars"] = FALSE;
  485. }
  486. if($name !== "include" and get("autoRender")) {
  487. $this->rendering();
  488. } elseif($name === "include") {
  489. $this->rendering();
  490. }
  491. }
  492. /**
  493. * Set the current theme
  494. *
  495. * @return void
  496. */
  497. public function theme($theme) {
  498. $this->Templates = $this->core("Templates");
  499. $this->Templates->theme($theme);
  500. }
  501. /**
  502. * Set title for header template
  503. *
  504. * @param string $title = NULL
  505. * @return void
  506. */
  507. public function title($title = NULL) {
  508. $this->Templates = $this->core("Templates");
  509. $this->Templates->title($title);
  510. }
  511. public function vars($vars) {
  512. $this->Templates->vars($vars);
  513. }
  514. public function meta($title = NULL, $description = NULL, $keywords = NULL, $language = NULL) {
  515. $this->Templates = $this->core("Templates");
  516. $this->Templates->meta($title, $description, $keywords, $language);
  517. }
  518. public function setMeta($tag, $value) {
  519. $this->Templates = $this->core("Templates");
  520. $this->Templates->setMeta($tag, $value);
  521. }
  522. /**
  523. * Loads a view
  524. *
  525. * @param string $name
  526. * @param string $application = NULL
  527. * @param string $vars = NULL
  528. * @return string value / void
  529. */
  530. public function view($name, $vars = NULL, $application = NULL, $return = FALSE) {
  531. if(is_null($application)) {
  532. $application = whichApplication();
  533. }
  534. if(!is_null($application)) {
  535. $view = "www/applications/$application/views/$name.php";
  536. if(is_array($vars)) {
  537. $key = array_keys($vars);
  538. $size = sizeof($key);
  539. for($i = 0; $i < $size; $i++) {
  540. $$key[$i] = $vars[$key[$i]];
  541. }
  542. } elseif($vars) {
  543. return $view;
  544. }
  545. if(file_exists($view)) {
  546. ob_start();
  547. include $view;
  548. if($return) {
  549. $output = ob_get_contents();
  550. ob_end_clean();
  551. return $output;
  552. }
  553. } else {
  554. getException("Error 404: $view view not found");
  555. }
  556. } else {
  557. return FALSE;
  558. }
  559. }
  560. }