ZanPHP /zan/classes/load.php

Language PHP Lines 644
MD5 Hash 947752af942e2c8d2a1587e4cfcfb8dc
Repository https://github.com/triartdesign/ZanPHP.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
<?php
/**
 * ZanPHP
 *
 * An open source agile and rapid development framework for PHP 5
 *
 * @package		ZanPHP
 * @author		MilkZoft Developer Team
 * @copyright	Copyright (c) 2011, MilkZoft, Inc.
 * @license		http://www.zanphp.com/documentation/en/license/
 * @link		http://www.zanphp.com
 * @version		1.0
 */
 
/**
 * Access from index.php
 */
if(!defined("_access")) {
	die("Error: You don't have permission to access here...");
}

/**
 * Includes Singleton 
 */
include "singleton.php";

/**
 * ZanPHP Load Class
 *
 * This class is used to load models, views, controllers, classes, libraries, helpers as well as interact directly with templates
 *
 * @package		ZanPHP
 * @subpackage	core
 * @category	classes
 * @author		MilkZoft Developer Team
 * @link		http://www.zanphp.com/documentation/en/classes/load_class
 */
class ZP_Load {

	/**
	 * 
	 * 
	 * 
	 */
	public $application = FALSE;
	
	/**
	 * Contains an Instance (object) of the Templates Class
	 * 
	 * @object public $Templates
	 */
	public $Templates;
	
	/**
	 * Contains the array of views
	 * 
	 * @var private $views = array()
	 */
	private $views = array();

	public $ZP;
	
    /**
     * Loads helper autoload, database config and class templates
     *
     * @return void
     */
	public function __construct() {
		$this->helper(array("autoload", "config", "validations"));
	}
	
    /**
     * 
     *
     * @param string 
     * @return 
     */
	public function app($application) {
		$this->application = $application;
		
		return $application;	
	}
	
	/**
     * Loads an application class
     * 
     * @param string $class = NULL
     * @param string $application = NULL
     * @return object value
     */
	public function classes($name, $className = NULL, $params = array(), $application = NULL) {			
		if(file_exists("www/applications/$application/classes/$name.php")) {
			include_once "www/applications/$application/classes/$name.php";	
		} elseif(file_exists("www/classes/$name.php")) {
			include_once "www/classes/$name.php";	
		} else {
			getException("$name class doesn't exists");
		}

		return ($className) ? ZP_Singleton::instance($className, $params) : TRUE;
	}
	
    /**
     * Loads a config file
     * 
     * @param string $name
     * @param bool $application = FALSE
     * @return void
     */
	public function config($name, $application = FALSE) {
		if($application) {
			if(file_exists("www/applications/$application/config/$name.php")) {
				include_once "www/applications/$application/config/$name.php";
			}
		} elseif(file_exists("www/config/$name.php")) {
			include_once "www/config/$name.php";
		} else {
			if(file_exists("www/applications/$name/config/$name.php")) {
				include_once "www/applications/$name/config/$name.php";
			}
		}
	}
	
    /**
     * Loads a controller
     * 
     * @param string $name
     * @return object value
     */
	public function controller($controller, $application = NULL) {
		$parts = explode("_", $controller);
		
		if(!$this->application) { 
			if(file_exists("www/applications/$application/controllers/". strtolower($parts[0]) .".php")) {
				$file = "www/applications/$application/controllers/". strtolower($parts[0]) .".php";
			} elseif(count($parts) === 2) {
				$file = "www/applications/". strtolower($parts[0]) ."/controllers/". strtolower($parts[0]) .".php";
			}		
		} else { 
			if(file_exists("www/applications/$application/controllers/". strtolower($parts[0]) .".php")) {
				$file = "www/applications/$application/controllers/". strtolower($parts[0]) .".php";
			} elseif(file_exists("www/applications/$this->application/controllers/". strtolower($parts[0]) .".php")) {
				$file = "www/applications/$this->application/controllers/". strtolower($parts[0]) .".php";
			} else {
				$file = "www/applications/". strtolower($parts[0]) ."/controllers/". strtolower($parts[0]) .".php";
			}
		}
		
		if(file_exists($file)) {							
			if(class_exists($controller)) {
				return ZP_Singleton::instance($controller);
			}
		
			include $file;
			
			return ZP_Singleton::instance($controller);
		}	
		
		return FALSE;
	}
	
    /**
     * Loads a core class
     * 
     * @param string $class
     * @return object value
     */
	public function core($core) {
		return ZP_Singleton::instance("ZP_$core");		
	}
	
    /**
     * Sets a CSS file from an specific application
     * 
     * @param string $CSS = NULL
     * @param string $application = NULL
     * @param bool $print = FALSE
     * @return void
     */
	public function CSS($CSS = NULL, $application = NULL, $print = FALSE) {
		$this->Templates = $this->core("Templates");
	
		$this->Templates->CSS($CSS, $application, $print);
	}

	public function driver($driver = NULL, $type = "db") {
		if(file_exists(_corePath ."/drivers/$type/". strtolower($driver) .".php")) {
			$file = _corePath ."/drivers/$type/". strtolower($driver) .".php";	
		} else {
			$file = FALSE;	
		}

		if(file_exists($file)) {	
			include $file;
				
			return ZP_Singleton::instance("ZP_". $driver);
		} else {
			getException("$driver driver does not exists");
		}	
	}
	
	public function exception($exception) {
		$exception = strtolower($exception);

		if(file_exists("www/lib/exceptions/$exception.php")) {
			include_once "www/lib/exceptions/$exception.php";
		} else {
			return FALSE;
		}
	}

	public function execute($Class, $method, $params = array(), $type = "controller") {
		if($type === "controller") {
			$this->$Class = $this->controller($Class);	
		} elseif($type === "model") {
			$this->$Class = $this->model($Class);
		}
		
		return ($this->$Class) ? call_user_func_array(array($this->$Class, $method), is_array($params) ? $params : array()) : FALSE;
	}
	
    /**
     * Loads a footer template
     *
     * @return void
     */
	private function footer() {
		if($this->Templates->exists("footer")) {
			if(count($this->views) > 0) {
				for($i = 0; $i <= count($this->views) - 1; $i++) {
					if($this->views[$i]["vars"] !== FALSE) {
						$this->Templates->vars($this->views[$i]["vars"]);
					}
				}
			}
			
			$this->Templates->load("footer");				
		}
	}
	
    /**
     * Load header template
     *
     * @return void
     */
	private function header() {
		if($this->Templates->exists("header")) {
			if(count($this->views) > 0) {
				for($i = 0; $i <= count($this->views) - 1; $i++) {
					if($this->views[$i]["vars"] !== FALSE) {
						$this->Templates->vars($this->views[$i]["vars"]);
					}
				}
			}
			
			$this->Templates->load("header");
		}		
	}
	
    /**
     * Loads a helper or multiple helper files
     * 
     * @param mixed  $helper
     * @param string $application
     * @return void
     */	
	public function helper($helper, $application = NULL) {
		if(is_array($helper)) { 
			for($i = 0; $i <= count($helper) - 1; $i++) {
				if($application === NULL) {
					if(file_exists(_corePath . "/helpers/". $helper[$i] .".php")) {
						include_once _corePath . "/helpers/". $helper[$i] .".php";
					} elseif(file_exists("www/helpers/". $helper[$i] .".php")) {
						include_once "www/helpers/". $helper[$i] .".php";
					} else {
						getException($helper[$i] ." helper doesn't exists");		
					}			
				} else {
					if(file_exists("www/applications/$application/helpers/". $helper[$i] .".php")) {
						include_once "www/applications/$application/helpers/". $helper[$i] .".php";
					} else {			
						getException($helper[$i] ." helper doesn't exists");
					}				
				}
			}
		} else { 
			if(is_null($application)) {
				if(file_exists(_corePath ."/helpers/$helper.php")) {
					include_once _corePath ."/helpers/$helper.php";
				} elseif(file_exists("www/helpers/$helper.php")) {
					include_once "www/helpers/$helper.php";
				}  else {			
					getException("$helper helper doesn't exists");
				}
			} else {
				if(file_exists("www/applications/$application/helpers/$helper.php")) {
					include_once "www/applications/$application/helpers/$helper.php";
				} else {			
					getException("$helper helper doesn't exists");
				}			
			}
		}
	}
	
    /**
     * Loads a hook or multiple hook files
     * 
     * @param string $hook
     * @param string $application = NULL
     * @return void
     */	
	public function hook($hook, $application = NULL) {
		if(is_array($hook)) {
			for($i = 0; $i <= count($hook) - 1; $i++) {
				if(is_null($application)) {
					if(file_exists(_corePath ."/hooks/". $hook[$i] .".php")) {
						include_once _corePath ."/hooks/". $hook[$i] .".php";
					} else {			
						getException("$name hook doesn't exists");
					}			
				} else {
					if(file_exists("www/applications/$application/hooks/". $hook[$i] .".php")) {
						include_once "www/applications/$application/hooks/". $hook[$i] .".php";
					} else {			
						getException("$name hook doesn't exists");
					}				
				}
			}
		} else {
			if(is_null($application)) {
				if(file_exists(_corePath ."hooks/$hook.php")) {
					include_once _corePath ."hooks/$hook.php";
				} else {			
					getException("$name hook doesn't exists");
				}
			} else {
				if(file_exists("www/applications/$application/hooks/$hook.php")) {
					include_once "www/applications/$application/hooks/$hook.php";
				} else {			
					getException("$name hook doesn't exists");
				}			
			}
		}
	}
	
	/**
     * Sets a JS file from an specific application
     * 
     * @param string $script
     * @param string $application = NULL
     * @param bool   $extra       = NULL
     * @param bool   $getJs       = FALSE
     * @return void
     */
	public function js($script, $application = NULL, $extra = NULL, $getJs = FALSE) {
		$this->Templates = $this->core("Templates");
		
		$this->Templates->js($script, $application, $extra, $getJs);
	}
	
    /**
     * Loads a left template
     *
     * @return void
     */
	private function left() {
		if($this->Templates->exists("left")) {
			if(count($this->views) > 0) {
				for($i = 0; $i <= count($this->views) - 1; $i++) {
					if($this->views[$i]["vars"] !== FALSE) {
						$this->Templates->vars($this->views[$i]["vars"]);
					}
				}
			}
			
			$this->Templates->load("left");
		}
	}
	
    /**
     * Loads a library file
     * 
     * @param string $name
     * @param string $className
     * @param array  $params
     * @param string $application
     * @return void
     */
	public function library($name, $className = NULL, $params = array(), $application = NULL) {	
		if(file_exists(_corePath ."/libraries/$application/$name.php")) {
			include_once _corePath ."/libraries/$application/$name.php";	
		} elseif(file_exists(_corePath ."/libraries/$className/$name.php")) {
			include_once _corePath ."/libraries/$lib/$name.php";	
		} elseif(file_exists("www/applications/$application/libraries/$name.php")) {
			include_once "www/applications/$application/libraries/$name.php";				
		} else {
			getException("$name library doesn't exists");
		}

		return ($className) ? ZP_Singleton::instance($className, $params) : TRUE;
	}

    /**
     * Loads a model file
     *
     * @param string $name
     * @return object value
     */
	public function model($model) {
		$parts = explode("_", $model);
	
		if(!$this->application) {
			if(count($parts) === 2) { 
				$file = "www/applications/". strtolower($parts[0]) ."/models/". strtolower($parts[0]) .".php";	
			}		
		} else { 
			if(count($parts) === 2) {
				if(file_exists("www/applications/$this->application/models/". strtolower($parts[0]) .".php")) {
					$file = "www/applications/$this->application/models/". strtolower($parts[0]) .".php";
				} elseif(file_exists("www/applications/$this->application/models/". strtolower($parts[0]) .".php")) {
					$file = "www/applications/$this->application/models/". strtolower($parts[0]) .".php";
				} else {
					$file = "www/applications/". strtolower($parts[0]) ."/models/". strtolower($parts[0]) .".php";
				}
			}
		}

		if(file_exists($file)) { 					
			if(class_exists($model)) { 
				return ZP_Singleton::instance($model);
			}
	
			require $file;
			
			return ZP_Singleton::instance($model);
		}	
		
		return FALSE;
	}
	
    /**
     * Render and output templates
     *
     * @return void
     */
	public function rendering() {
		$numArgs = func_num_args();
		$args    = func_get_args();
		
		if($numArgs > 0) {
			for($i = 0; $i <= $numArgs - 1; $i++) {
				if($this->views[$i]["vars"]) {
					$this->Templates->vars($this->views[$i]["vars"]);
				}
				
				if(isset($args[$i]) and $args[$i] === "header") {
					$this->header();
				}
				
				if($args[$i] !== "header" and $args[$i] !== "left" and $args[$i] !== "right" and $args[$i] !== "footer") {
					if($this->Templates->exists($args[$i])) {
						$this->Templates->load($args[$i]);
						
						continue;
					} 
				}
				
				if(isset($args[$i]) and $args[$i] === "left") {
					$this->left();
				} 
				
				if(count($this->views) > 0) {
					for($i = 0; $i <= count($this->views) - 1; $i++) {						
						$this->Templates->load($this->views[$i]["name"]);						
					}
				}
				
				if(isset($args[$i]) and $args[$i] === "right") {
					$this->right();
				} 
				
				if(isset($args[$i]) and $args[$i] === "footer") {
					$this->footer();
				}
			}
		} else { 
			for($i = 0; $i <= count($this->views) - 1; $i++) {
				if($this->views[$i]["vars"]) {
					$this->Templates->vars($this->views[$i]["vars"]);
				}
			}
			
			$this->header();
			$this->left();
			
			if(count($this->views) > 0) {
				for($i = 0; $i <= count($this->views) - 1; $i++) {
					$this->Templates->load($this->views[$i]["name"]);					
				}
			}
			
			$this->right();
			$this->footer();
		}
	}
	
    /**
     * Loads a right template
     *
     * @return void
     */
	private function right() {
		if($this->Templates->exists("right")) {
			if(count($this->views) > 0) {
				for($i = 0; $i <= count($this->views) - 1; $i++) {
					if($this->views[$i]["vars"] !== FALSE) {
						$this->Templates->vars($this->views[$i]["vars"]);
					}
				}
			}
			
			$this->Templates->load("right");
		}
	}	
	
    /**
     * Loads templates
     *
     * @param string $name
     * @param string $vars
     * @return string value / void
     */	
	public function render($name, $vars = NULL) {	
		if(is_array($vars)) { 
			if(count($this->views) === 0) {
				$this->views[0]["name"] = $name;
				$this->views[0]["vars"] = $vars;
			} else {
				$i = count($this->views);
					
				$this->views[$i]["name"] = $name;
				$this->views[$i]["vars"] = $vars;				
			}
		} else {
			$i = count($this->views);
				
			$this->views[$i]["name"] = $name;
			$this->views[$i]["vars"] = FALSE;		
		}
		
		if($name !== "include" and get("autoRender")) {
			$this->rendering();
		} elseif($name === "include") {
			$this->rendering();
		}
	}
	
    /**
     * Set the current theme
     *
     * @return void
     */
	public function theme($theme) {
		$this->Templates = $this->core("Templates");
		
		$this->Templates->theme($theme);
	}
	
    /**
     * Set title for header template
     *
     * @param string $title = NULL
     * @return void
     */
	public function title($title = NULL) {
		$this->Templates = $this->core("Templates");
		
		$this->Templates->title($title);
	}
        
	public function vars($vars) {
		$this->Templates->vars($vars);
	}
        
        public function meta($title = NULL, $description = NULL, $keywords = NULL, $language = NULL) {
            $this->Templates = $this->core("Templates");
            
            $this->Templates->meta($title, $description, $keywords, $language);
        }
        
        public function setMeta($tag, $value) {
            $this->Templates = $this->core("Templates");
            
            $this->Templates->setMeta($tag, $value);
        }
	
    /**
     * Loads a view
     *
     * @param string $name
     * @param string $application = NULL
     * @param string $vars        = NULL
     * @return string value / void
     */	
	public function view($name, $vars = NULL, $application = NULL, $return = FALSE) {
		if(is_null($application)) {
			$application = whichApplication();
		} 

		if(!is_null($application)) {
			$view = "www/applications/$application/views/$name.php";
			
			if(is_array($vars)) {
				$key  = array_keys($vars);
				$size = sizeof($key);			
			
				for($i = 0; $i < $size; $i++) {
					$$key[$i] = $vars[$key[$i]];
				}
			} elseif($vars) {
				return $view;
			}

			if(file_exists($view)) {
				ob_start();

				include $view;
				
				if($return) {
					$output = ob_get_contents();

					ob_end_clean();

					return $output;
				}
			} else {
				getException("Error 404: $view view not found");	
			}
		} else {
			return FALSE;
		}
	}
	
}
Back to Top