Пример #1
0
 function loadController($controller = null)
 {
     if (isset($controller)) {
         $this->contorllerFile = $controller;
     }
     $class = SweetFramework::className($this->contorllerFile);
     if (!SweetFramework::loadFileType('controller', $class)) {
         D::error('No Controller Found');
     }
     $page = $this->loadUrl($class::$urlPattern, $this->count);
     if (is_array(f_last($page))) {
         if (is_array(f_first(f_last($page)))) {
             return $this->loadController(f_first(f_first(f_last($page))), $this->count += 1);
         }
         $page[$this->count] = f_first(f_last($page));
         //D::log($page[$part], 'page o parts');
     }
     D::log($page, 'Initing Controller…');
     $this->controller = new $class();
     //$this->controller->getLibrary('Databases/Query.php');
     /*@todo make "shortcuts" more dynamic */
     //$this->controller->template =& $this->controller->lib->Template;
     if (empty($page[$this->count])) {
         return f_callable(array($this->controller, 'index'));
     } else {
         if (method_exists($class, $page[$this->count])) {
             return f_callable(array($this->controller, $page[$this->count]));
         }
     }
     if (method_exists($class, '__DudeWheresMyCar')) {
         return f_callable(array($this->controller, '__DudeWheresMyCar'));
     }
     return function () {
         header("HTTP/1.0 404 Not Found");
         echo '<h1>404 error</h1>';
         //todo check for some sort of custom 404…
         return false;
     };
 }
Пример #2
0
	function _buildPulls($pulls, $on=null, $with=array()) {
		//D::log($pulls, '$pulls');
		$builtPulls = array();
		foreach($pulls as $k => $pull) {
			if(is_string($k)) {
				//sub join?
				
				$pullRel = $this->relationships[$k];
				
				if(is_string($fKey = f_first(array_keys($pullRel)) )) {
					$flName = $fKey;
					$model = $this->model(f_first($pullRel[$fKey]));
				} else {
					$flName = $k;
					$model = $this->model(f_first($pullRel));
				}
				
				if(is_array($rfName = f_last($pullRel))) {
					$rfName = f_last(f_last($pullRel));
				}
				
				//$builtPulls[] = $model->_buildPull($k, $pullRel, $on, $flName, $rfName);
				$builtPulls[] = $model->_buildPull(join('$', f_push($k, $with)), $pullRel, $on, $flName, $rfName);
				
				
				$builtPulls = array_merge($builtPulls, $model->_buildPulls((array)$pull, join('$', f_push($k, $with)), f_push($k, (array)$with) ));
				
			} else {
				if(is_array($pull)) {
					$builtPulls = array_merge($builtPulls, $this->_buildPulls($pull, $on, $with));
					continue;
				}
				//regular join
				if(!array_key_exists($pull, $this->relationships)) {
					//D::show($this->relationships, 'Current relationships');
					D::warn($pull . ' can\'t be found in the ' . get_class($this) . ' model');
				}
				$pullRel = $this->relationships[$pull];
				
				if(is_string($fKey = f_first(array_keys($pullRel)) )) {
					$flName = $fKey;
					$model = $this->model(f_first($pullRel[$fKey]));
				} else {
					$flName = $pull;
					$model = $this->model(f_first($pullRel));
				}
				
				if(is_array($rfName = f_last($pullRel))) {
					$rfName = f_last(f_last($pullRel));
				}
				$builtPulls[] = $model->_buildPull(join('$', f_push($pull, $with)), $pullRel, $on, $flName, $rfName);
			}
		}	
		return $builtPulls;
	}
Пример #3
0
	function helper($helper) {
		if(is_array($helper)) {
			return f_last(array_map(f_callable(array($this, 'helper')), $helper));
		}
		return SweetFramework::loadFileType('helper', $helper);
	}
Пример #4
0
	function flash($key, $value=null) {
		//if($this->checkCookie()) {
			if(!isset($value)) {
				return @$this->_flash[$key];
			}
			if(is_array($key)) {
				foreach($key as $k => $v) {
					$this->flash($k, $v);
				}
				return $this->flash(f_last($key));
			}
			$this->_flash[$key] = $value;
			$this->libs->Query->insert(array('name' => $key, 'value' => serialize($this->_flash[$key]), 'session' => $this->_id, 'flash' => 1))->into($this->libs->Config->get('Session', 'dataTableName'))->go();
		//}
	}
Пример #5
0
	static function _buildWhere($group, $groupOperator='AND', $escape=true) {
		//"Bitch I'll pick the world up and I'ma drop it on your f*ckin' head" - Lil Wayne.
		$keys = array_keys($group);
		if(is_int(f_last($keys)) && is_string(f_last($group))) {
			$operator = f_last($group);
			$group = f_chop($group);
		} else {
			$operator = '=';
		}
		if(is_int(f_first($keys)) && is_string(f_first($group))) {
			$groupOperator = f_first($group);
			$group = f_rest($group);
		}
		$builtArray = f_keyMap(
			function($value, $key) use($groupOperator, $operator, $escape) {
				if(is_int($key) && is_array($value)) {
					$bWhere = Query::_buildWhere($value, $groupOperator, $escape);
					if(!empty($bWhere)) {
						return '(' . "\n\t" . $bWhere . "\n" .')';
					} else {
						return null;
					}
				}
				if(is_string($key)) {
					static $escapeFunc = array('Query' , 'nullEscape');
					if(!$escape) {
						$escapeFunc = 'nothing';
					} else {
						$escapeFunc = f_callable($escapeFunc);
					}
					if(is_array($value)) {
						$key = $escapeFunc($key, '');
						if(f_first(array_keys($value)) !== 0) {
							return join(' ' . $groupOperator . ' ', f_keyMap(function($v, $k) use($key, $escapeFunc) {
								return $key . ' BETWEEN ' . $escapeFunc($k) . ' AND ' . $escapeFunc($v);
							}, $value));
						}
						return $key . ' IN (' . join(', ', array_map($escapeFunc, $value)) . ')'; 
					} else {
						$value = call_user_func($escapeFunc, $value);
						if($value === 'null') {
							if($operator == '=') {
								$operator = 'IS';
							} else {
								$operator = 'IS NOT';
							}
						}
						return Query::escape($key) . ' ' . $operator . ' ' . $value;
					}
				}
			},
			$group
		);
		if(!empty($builtArray)) {
			return join(' ' . $groupOperator . ' ', array_filter($builtArray));
		}
	}
	function loadController($fileName, $part=0) {
		D::log($fileName, 'Loading Controller…');
		
		//$fileName = Events::callEvent('loadController', $fileName);

		require(LOC . 'Controllers/' . $fileName);
		
		//print_r($this);
		static $partCount = 0;

		$class = substr(strrchr('/' . $fileName, '/'), 1, -4);

		$page = $this->lib->Uri->loadUrl($class::$urlPattern, $part);
		
		if(is_array(f_last($page))) {
			if(is_array( f_first(f_last($page)) )) {
				$this->loadController(f_first(f_first(f_last($page))), $part+1);
				return true;
			}
			$page[$part] = f_first(f_last($page));
			//D::log($page[$part], 'page o parts');
		}


		D::log($page, 'Loading Controller…');
		$this->controller = new $class();
		
		$this->controller->getLibrary('Databases/Query.php');
		
		/*@todo make "shortcuts" more dynamic */
		$this->controller->template =& $this->controller->lib->Template;
		
		if(empty($page[$part])) {
			echo $this->controller->index();
		} else {
			if(method_exists($class, $page[$part])) {
				echo f_call(array(
					$this->controller,
					$page[$part]
				));
				return true;
			} else {
				return f_function(function() {
					header("HTTP/1.0 404 Not Found");
					echo '<h1>404 error</h1>'; //todo check for some sort of custom 404…
				});
			}
		}
		D::log($page, 'controller method array');
	}
Пример #7
0
	function loadController($controller=null) {
		if(isset($controller)) {
			$this->contorllerFile = $controller;
		}
		if(empty($this->contorllerFile)) {
			$this->contorllerFile = $this->lib('Config')->get('site', 'mainController');
		}
		
		$class = SweetFramework::className($this->contorllerFile);
		D::log($class, 'Controller Loaded');
		
		if(!SweetFramework::loadFileType('controller', $this->contorllerFile)) {
			D::error('No Controller Found');
		}
		if(!empty($class::$urlPattern)) {
			$page = $this->loadUrl($class::$urlPattern);
		} else {
			$page = $this->loadUrl(array());
		}
		
		D::log($this->count, 'COUNT');
		D::log($page, 'page');
		
		if(is_array(f_last($page))) {
			if(is_array( f_first(f_last($page)) )) {
				$this->request = f_first($page);
				D::log($this->request, 'Request Reduced');
				if(method_exists($class, 'enRoute')) {
					$class::enRoute();
				}
				return $this->loadController(f_first(f_first(f_last($page))) );
			}
			$page[$this->count] = f_first(f_last($page));
		}
		
		$fpage = f_first($page);
		$this->controller = new $class();
		if(empty($fpage)) {
			return f_callable(array($this->controller, D::log('index', 'Controller Function')));
		} else {
			if(method_exists($class, $fpage)) {
				return f_callable(array($this->controller, D::log($fpage, 'Controller Function')));
			}
		}
		
		//D::show($class, 'controller');
		if(method_exists($class, '__DudeWheresMyCar')) {
			return f_callable(array(
				$this->controller,
				'__DudeWheresMyCar'
			));
		}
		//@todo find a way to check for __DudeWheresMyCar functions higher up the controller tree.
		
		return function() {
			header('HTTP/1.0 404 Not Found');
			echo '<h1>404 error</h1>'; //todo check for some sort of custom 404…
			return false;
		};
	}
Пример #8
0
	static function _buildWhere($group, $groupOperator='AND', $escape=true) {
		//"Bitch I'll pick the world up and I'ma drop it on your f*ckin' head" - Lil Wayne.
		$keys = array_keys($group);
		if(is_int(f_last($keys)) && is_string(f_last($group))) {
			$operator = f_last($group);
			$group = f_chop($group);
		} else {
			$operator = '=';
		}
		if(is_int(f_first($keys)) && is_string(f_first($group))) {
			$groupOperator = f_first($group);
			$group = f_rest($group);
		}
		$builtArray = f_keyMap(
			function($value, $key) use($groupOperator, $operator, $escape) {
				if(is_int($key) && is_array($value)) {
					//Group? @todo double check to make sure OR is working
					$bWhere = Query::_buildWhere($value, $groupOperator, $escape);
					if(!empty($bWhere)) {
						return '(' . "\n" . $bWhere . ')';
					} else {
						return null;
					}
				}
				if(is_string($key)) {
					static $escapeFunc = 'Query::nullEscape';
					if(!$escape) {
						$escapeFunc = 'nothing';
					}
					//column
					if(is_array($value)) {
						//IN or group
						return Query::escape($key) . ' IN (' . join(', ', array_map($escapeFunc, $value)) . ')'; 
					} else {
						$value = call_user_func($escapeFunc, $value);
						if($value === 'null') {
							if($operator == '=') {
								$operator = 'IS';
							} else {
								$operator = 'IS NOT';
							}
							
						}
						return Query::escape($key) . ' ' . $operator . ' ' . $value;
					}
				}
			},
			$group
		);
//		D::log($builtArray, 'built array');
		
		if(!empty($builtArray)) {
			return join(' ' . $groupOperator . ' ', array_filter($builtArray));
		}
	}