/zan/classes/load.php

https://github.com/MilkZoft/ZanPHP · PHP · 491 lines · 465 code · 26 blank · 0 comment · 52 complexity · 6521268764073236a0767aa3cc22e726 MD5 · raw file

  1. <?php
  2. if (!defined("ACCESS")) {
  3. die("Error: You don't have permission to access here...");
  4. }
  5. include "singleton.php";
  6. class ZP_Load
  7. {
  8. public $application = false;
  9. public $Templates;
  10. private $views = array();
  11. public $ZP;
  12. public function __construct()
  13. {
  14. $this->helper(array("autoload", "browser", "config", "validations"));
  15. }
  16. public function app($application)
  17. {
  18. return $this->application = $application;
  19. }
  20. public function classes($name, $className = null, $params = array(), $application = null)
  21. {
  22. $name = strtolower($name);
  23. if (file_exists("www/applications/$application/classes/$name.php")) {
  24. include_once "www/applications/$application/classes/$name.php";
  25. } elseif (file_exists("www/classes/$name.php")) {
  26. include_once "www/classes/$name.php";
  27. } else {
  28. getException("$name class doesn't exists");
  29. }
  30. return ($className) ? ZP_Singleton::instance($className, $params) : true;
  31. }
  32. public function config($name, $application = false)
  33. {
  34. if ($application) {
  35. if (file_exists("www/applications/$application/config/$name.php")) {
  36. include_once "www/applications/$application/config/$name.php";
  37. }
  38. } elseif (file_exists("www/config/$name.php")) {
  39. include_once "www/config/$name.php";
  40. } else {
  41. if (file_exists("www/applications/$name/config/$name.php")) {
  42. include_once "www/applications/$name/config/$name.php";
  43. }
  44. }
  45. }
  46. public function controller($controller, $application = null)
  47. {
  48. $parts = explode("_", $controller);
  49. if (!$this->application) {
  50. if (file_exists("www/applications/$application/controllers/". strtolower($parts[0]) .".php")) {
  51. $file = "www/applications/$application/controllers/". strtolower($parts[0]) .".php";
  52. } elseif (count($parts) === 2) {
  53. $file = "www/applications/". strtolower($parts[0]) ."/controllers/". strtolower($parts[0]) .".php";
  54. }
  55. } else {
  56. if (file_exists("www/applications/$application/controllers/". strtolower($parts[0]) .".php")) {
  57. $file = "www/applications/$application/controllers/". strtolower($parts[0]) .".php";
  58. } elseif (file_exists("www/applications/$this->application/controllers/". strtolower($parts[0]) .".php")) {
  59. $file = "www/applications/$this->application/controllers/". strtolower($parts[0]) .".php";
  60. } else {
  61. $file = "www/applications/". strtolower($parts[0]) ."/controllers/". strtolower($parts[0]) .".php";
  62. }
  63. }
  64. if (file_exists($file)) {
  65. if (class_exists($controller)) {
  66. return ZP_Singleton::instance($controller);
  67. }
  68. include $file;
  69. return ZP_Singleton::instance($controller);
  70. }
  71. return false;
  72. }
  73. public function core($core)
  74. {
  75. return ZP_Singleton::instance("ZP_$core");
  76. }
  77. public function CSS($CSS = null, $application = null, $print = false, $top = false)
  78. {
  79. $this->Templates = $this->core("Templates");
  80. $this->Templates->CSS($CSS, $application, $print, $top);
  81. }
  82. public function driver($driver = null, $type = "db")
  83. {
  84. if (file_exists(CORE_PATH ."/drivers/$type/". strtolower($driver) .".php")) {
  85. $file = CORE_PATH ."/drivers/$type/". strtolower($driver) .".php";
  86. } else {
  87. $file = false;
  88. }
  89. if (file_exists($file)) {
  90. include $file;
  91. return ZP_Singleton::instance("ZP_". $driver);
  92. } else {
  93. getException("$driver driver does not exists");
  94. }
  95. }
  96. public function db($type = "db")
  97. {
  98. if (strtolower($type) === "db") {
  99. return $this->core("Db");
  100. } elseif (strtolower($type) === "mongodb" or strtolower($type) === "mongo") {
  101. return (DB_NOSQL_ACTIVE) ? $this->core("MongoDB") : false;
  102. } elseif (strtolower($type) === "couchdb" or strtolower($type) === "couch") {
  103. return (DB_NOSQL_ACTIVE) ? $this->core("CouchDB") : false;
  104. } elseif (strtolower($type) === "cassandra") {
  105. return (DB_NOSQL_ACTIVE) ? $this->core("Cassandra") : false;
  106. }
  107. }
  108. public function exception($exception)
  109. {
  110. $exception = strtolower($exception);
  111. if (file_exists("www/lib/exceptions/$exception.php")) {
  112. include_once "www/lib/exceptions/$exception.php";
  113. } else {
  114. return false;
  115. }
  116. }
  117. public function execute($Class, $method, $params = array(), $type = "controller")
  118. {
  119. if ($type === "controller") {
  120. $this->$Class = $this->controller($Class);
  121. } elseif ($type === "model") {
  122. $this->$Class = $this->model($Class);
  123. }
  124. return ($this->$Class) ? call_user_func_array(array($this->$Class, $method), is_array($params) ? $params : array()) : false;
  125. }
  126. private function footer()
  127. {
  128. if ($this->Templates->exists("footer")) {
  129. if (count($this->views) > 0) {
  130. for ($i = 0; $i <= count($this->views) - 1; $i++) {
  131. if ($this->views[$i]["vars"] !== false) {
  132. $this->Templates->vars($this->views[$i]["vars"]);
  133. }
  134. }
  135. }
  136. $this->Templates->load("footer");
  137. }
  138. }
  139. private function header()
  140. {
  141. if ($this->Templates->exists("header")) {
  142. if (count($this->views) > 0) {
  143. for ($i = 0; $i <= count($this->views) - 1; $i++) {
  144. if ($this->views[$i]["vars"] !== false) {
  145. $this->Templates->vars($this->views[$i]["vars"]);
  146. }
  147. }
  148. }
  149. $this->Templates->load("header");
  150. }
  151. }
  152. public function helper($helper, $application = null)
  153. {
  154. if (is_array($helper)) {
  155. for ($i = 0; $i <= count($helper) - 1; $i++) {
  156. if ($application === null) {
  157. if (file_exists(CORE_PATH ."/helpers/". $helper[$i] .".php")) {
  158. include_once CORE_PATH ."/helpers/". $helper[$i] .".php";
  159. } elseif (file_exists("www/helpers/". $helper[$i] .".php")) {
  160. include_once "www/helpers/". $helper[$i] .".php";
  161. } else {
  162. getException($helper[$i] ." helper doesn't exists");
  163. }
  164. } else {
  165. if (file_exists("www/applications/$application/helpers/". $helper[$i] .".php")) {
  166. include_once "www/applications/$application/helpers/". $helper[$i] .".php";
  167. } else {
  168. getException($helper[$i] ." helper doesn't exists");
  169. }
  170. }
  171. }
  172. } else {
  173. if (is_null($application)) {
  174. if (file_exists(CORE_PATH ."/helpers/$helper.php")) {
  175. include_once CORE_PATH ."/helpers/$helper.php";
  176. } elseif (file_exists("www/helpers/$helper.php")) {
  177. include_once "www/helpers/$helper.php";
  178. } else {
  179. getException("$helper helper doesn't exists");
  180. }
  181. } else {
  182. if (file_exists("www/applications/$application/helpers/$helper.php")) {
  183. include_once "www/applications/$application/helpers/$helper.php";
  184. } else {
  185. getException("$helper helper doesn't exists");
  186. }
  187. }
  188. }
  189. }
  190. public function hook($hook, $application = null)
  191. {
  192. if (is_array($hook)) {
  193. for ($i = 0; $i <= count($hook) - 1; $i++) {
  194. if (is_null($application)) {
  195. if (file_exists(CORE_PATH ."/hooks/". $hook[$i] .".php")) {
  196. include_once CORE_PATH ."/hooks/". $hook[$i] .".php";
  197. } else {
  198. getException("$name hook doesn't exists");
  199. }
  200. } else {
  201. if (file_exists("www/applications/$application/hooks/". $hook[$i] .".php")) {
  202. include_once "www/applications/$application/hooks/". $hook[$i] .".php";
  203. } else {
  204. getException("$name hook doesn't exists");
  205. }
  206. }
  207. }
  208. } else {
  209. if (is_null($application)) {
  210. if (file_exists(CORE_PATH ."hooks/$hook.php")) {
  211. include_once CORE_PATH ."hooks/$hook.php";
  212. } else {
  213. getException("$name hook doesn't exists");
  214. }
  215. } else {
  216. if (file_exists("www/applications/$application/hooks/$hook.php")) {
  217. include_once "www/applications/$application/hooks/$hook.php";
  218. } else {
  219. getException("$name hook doesn't exists");
  220. }
  221. }
  222. }
  223. }
  224. public function js($script, $application = null, $getJs = false, $top = false)
  225. {
  226. $this->Templates = $this->core("Templates");
  227. return $this->Templates->js($script, $application, $getJs, $top);
  228. }
  229. private function left()
  230. {
  231. if ($this->Templates->exists("left")) {
  232. if (count($this->views) > 0) {
  233. for ($i = 0; $i <= count($this->views) - 1; $i++) {
  234. if ($this->views[$i]["vars"] !== false) {
  235. $this->Templates->vars($this->views[$i]["vars"]);
  236. }
  237. }
  238. }
  239. $this->Templates->load("left");
  240. }
  241. }
  242. public function library($name, $className = null, $params = array(), $application = null)
  243. {
  244. if (file_exists(CORE_PATH ."/libraries/$application/$name.php")) {
  245. include_once CORE_PATH ."/libraries/$application/$name.php";
  246. } elseif (file_exists(CORE_PATH ."/libraries/$className/$name.php")) {
  247. include_once CORE_PATH ."/libraries/$className/$name.php";
  248. } elseif (file_exists(CORE_PATH ."/libraries/$name/$name.php")) {
  249. include_once CORE_PATH ."/libraries/$name/$name.php";
  250. } elseif (file_exists("www/applications/$application/libraries/$name.php")) {
  251. include_once "www/applications/$application/libraries/$name.php";
  252. } else {
  253. getException("$name library doesn't exists");
  254. }
  255. return ($className) ? ZP_Singleton::instance($className, $params) : true;
  256. }
  257. public function model($model) {
  258. $parts = explode("_", $model);
  259. if (!$this->application) {
  260. if (count($parts) === 2) {
  261. $file = "www/applications/". strtolower($parts[0]) ."/models/". strtolower($parts[0]) .".php";
  262. }
  263. } else {
  264. if (count($parts) === 2) {
  265. if (file_exists("www/applications/$this->application/models/". strtolower($parts[0]) .".php")) {
  266. $file = "www/applications/$this->application/models/". strtolower($parts[0]) .".php";
  267. } elseif (file_exists("www/applications/$this->application/models/". strtolower($parts[0]) .".php")) {
  268. $file = "www/applications/$this->application/models/". strtolower($parts[0]) .".php";
  269. } else {
  270. $file = "www/applications/". strtolower($parts[0]) ."/models/". strtolower($parts[0]) .".php";
  271. }
  272. }
  273. }
  274. if (file_exists($file)) {
  275. if (class_exists($model)) {
  276. return ZP_Singleton::instance($model);
  277. }
  278. require $file;
  279. return ZP_Singleton::instance($model);
  280. }
  281. return false;
  282. }
  283. public function rendering()
  284. {
  285. $numArgs = func_num_args();
  286. $args = func_get_args();
  287. if ($numArgs > 0) {
  288. for ($i = 0; $i <= $numArgs - 1; $i++) {
  289. if ($this->views[$i]["vars"]) {
  290. $this->Templates->vars($this->views[$i]["vars"]);
  291. }
  292. if (isset($args[$i]) and $args[$i] === "header") {
  293. $this->header();
  294. }
  295. if ($args[$i] !== "header" and $args[$i] !== "left" and $args[$i] !== "right" and $args[$i] !== "footer") {
  296. if ($this->Templates->exists($args[$i])) {
  297. $this->Templates->load($args[$i]);
  298. continue;
  299. }
  300. }
  301. if (isset($args[$i]) and $args[$i] === "left") {
  302. $this->left();
  303. }
  304. if (count($this->views) > 0) {
  305. for ($i = 0; $i <= count($this->views) - 1; $i++) {
  306. $this->Templates->load($this->views[$i]["name"]);
  307. }
  308. }
  309. if (isset($args[$i]) and $args[$i] === "right") {
  310. $this->right();
  311. }
  312. if (isset($args[$i]) and $args[$i] === "footer") {
  313. $this->footer();
  314. }
  315. }
  316. } else {
  317. for ($i = 0; $i <= count($this->views) - 1; $i++) {
  318. if ($this->views[$i]["vars"]) {
  319. $this->Templates->vars($this->views[$i]["vars"]);
  320. }
  321. }
  322. $this->header();
  323. $this->left();
  324. if (count($this->views) > 0) {
  325. for ($i = 0; $i <= count($this->views) - 1; $i++) {
  326. $this->Templates->load($this->views[$i]["name"]);
  327. }
  328. }
  329. $this->right();
  330. $this->footer();
  331. }
  332. }
  333. private function right()
  334. {
  335. if ($this->Templates->exists("right")) {
  336. if (count($this->views) > 0) {
  337. for ($i = 0; $i <= count($this->views) - 1; $i++) {
  338. if ($this->views[$i]["vars"] !== false) {
  339. $this->Templates->vars($this->views[$i]["vars"]);
  340. }
  341. }
  342. }
  343. $this->Templates->load("right");
  344. }
  345. }
  346. public function render($name, $vars = null)
  347. {
  348. if (is_array($vars)) {
  349. if (count($this->views) === 0) {
  350. $this->views[0]["name"] = $name;
  351. $this->views[0]["vars"] = $vars;
  352. } else {
  353. $i = count($this->views);
  354. $this->views[$i]["name"] = $name;
  355. $this->views[$i]["vars"] = $vars;
  356. }
  357. } else {
  358. $i = count($this->views);
  359. $this->views[$i]["name"] = $name;
  360. $this->views[$i]["vars"] = false;
  361. }
  362. if ($name !== "include" and _get("autoRender")) {
  363. $this->rendering();
  364. }
  365. }
  366. public function theme($theme)
  367. {
  368. $this->Templates = $this->core("Templates");
  369. $this->Templates->theme($theme);
  370. }
  371. public function title($title = null)
  372. {
  373. $this->Templates = $this->core("Templates");
  374. $this->Templates->title($title);
  375. }
  376. public function vars($vars)
  377. {
  378. $this->Templates->vars($vars);
  379. }
  380. public function meta($tag, $value)
  381. {
  382. $this->Templates = $this->core("Templates");
  383. $this->Templates->meta($tag, $value);
  384. }
  385. public function view($name, $vars = null, $application = null, $return = false)
  386. {
  387. if (is_null($application)) {
  388. $application = whichApplication();
  389. }
  390. if (!is_null($application) and is_string($application) and is_string($name)) {
  391. $theme = _get("webTheme");
  392. if (file_exists("www/lib/themes/$theme/views/$application/$name.php")) {
  393. $view = "www/lib/themes/$theme/views/$application/$name.php";
  394. $minView = "www/lib/themes/$theme/views/$application/min/$name.php";
  395. } else {
  396. $view = "www/applications/$application/views/$name.php";
  397. $minView = "www/applications/$application/views/min/$name.php";
  398. }
  399. if (_get("environment") > 2 and file_exists($minView)) {
  400. $view = $minView;
  401. }
  402. if (is_array($vars)) {
  403. $key = array_keys($vars);
  404. $size = sizeof($key);
  405. for ($i = 0; $i < $size; $i++) {
  406. $$key[$i] = $vars[$key[$i]];
  407. }
  408. } elseif ($vars) {
  409. return $view;
  410. }
  411. if (file_exists($view)) {
  412. ob_start();
  413. include $view;
  414. if ($return) {
  415. $output = ob_get_contents();
  416. ob_clean();
  417. return $output;
  418. }
  419. } else {
  420. getException("Error 404: $view view not found");
  421. }
  422. } else {
  423. return false;
  424. }
  425. }
  426. }