public static function __callStatic($reallyHopeNoOneNamesThereVaribleThis, $values=array()) { extract((array) f_first($values)); ob_start(); include(LOC . '/sweet-framework/blocks/' . $reallyHopeNoOneNamesThereVaribleThis . '.php' ); return ob_get_clean(); }
function chain($baseItem, $items=array()) { if(!empty($items) && !empty($baseItem)) { return chain($baseItem->{f_first($items)}, f_rest($items)); //D::show('$baseItem->' . join('->', $items) . ';'); //return eval('$baseItem->' . join('->', $items) . ';') ?: null; } else { return $baseItem; } }
function f_qsort($in) { if(!empty($in)) { return array_merge( f_qsort( f_filter( function($a) use($in) { return $a < f_first($in); }, f_rest($in) ) ), array( f_first($in) ), f_qsort( f_filter( function($a) use($in) { return $a >= f_first($in); }, f_rest($in) ) ) ); } else { return array(); } }
function __get($var) { /* ORM TODO: - is this where i tell if i have a m2m relation ship? - does it make sense for Forigen Keys to exist like this? - not really, if its how m2m relationships are defined. - do foreign keys always have a field in the current model? - yes. - if it is a forigen key do i only need to return one sweet row item? - yes. - do m2m always need to return an array? - yes. - what advantages do i have for detecting m2m relationships - the differnces between fk and m2m code? - what does the pk mean? - //? the pk is used so you dont get an array of all the same item. -if it is the same item it passes it to the sweetRow obj - do you need it on m2m? - shouldn't matter. ?no ?not always. - the comments example on the pages models proves that it can be avaible. - do you need it on fk? yes. //in order for a fk to point to something, that something needs a pk. // for the most part the fk is gonna be the same for each row. - when is it differnent? //if its differnt does that mean there are 2 items? //this shouldn't be possible correct? //?on m2m? - how do i handle backwards relationships? - how were they defined before? - how were they handeled before? - do they even need to be defined? -yes. - use cases for backwards relationships? - m2m relationships are backwards fk relationships. they already work. */ //) if(!empty($this->__pull) && ( isset($this->__pull[$var]) || in_array($var, $this->__pull)) ) { ////// KEYS: $varL = strlen($var); /* $keys = array_filter( array_keys((array)f_first($this->__data)), function($k) use($var, $varL) { return ($var == substr($k, 0, $varL)); } ); */ $keys = array(); foreach(array_keys((array)f_first($this->__data)) as $k) { if($var == substr($k, 0, $varL)) { $keys[] = $k; } } //D::log($keys, 'keys'); $varL++; $pull = isset($this->__pull[$var]) ? $this->__pull[$var] : array(); //!meh $pullRel = $this->__model->relationships[$var]; if(is_string($fKey = f_first(array_keys($pullRel)) )) { //m2m //$model = SweetFramework::getClass('model', f_first($pullRel[$fKey])); $model = f_first($pullRel[$fKey]); $model = isset($this->__model->__relationships[$model]) ? $this->__model->__relationships[$model] : ($this->__model->__relationships[$model] = SweetFramework::getClass('model', $model)); $returnItems = array(); if(!isset($model->pk)) { foreach($this->__data as $row) { if(!empty($row)) { $item = self::subRow2Item($keys, $row, $varL); if(!empty($item)) { $returnItems[] = new SweetRow($model, $item, $pull); } } } } else { foreach($this->__data as $row) { if(!empty($row)) { $item = self::subRow2Item($keys, $row, $varL); if(!empty($item)) { if(isset($returnItems[$item[$model->pk]])) { f_call(array($returnItems[$item[$model->pk]], 'pass'), array($item)); } else { $returnItems[$item[$model->pk]] = new SweetRow($model, $item, $pull); } } } } } return $returnItems; } else { //$model = SweetFramework::getClass('model', f_first($pullRel)); $model = f_first($pullRel); $model = isset($this->__model->__relationships[$model]) ? $this->__model->__relationships[$model] : ($this->__model->__relationships[$model] = SweetFramework::getClass('model', $model)); if(!isset($model->pk)) { foreach($this->__data as $row) { if(!empty($row)) { $item = self::subRow2Item($keys, $row, $varL); if(!empty($item)) { if(isset($returnItem)) { $returnItem->pass($item); } else { $returnItem = new SweetRow($model, $item, $pull); } } } } } else { foreach($this->__data as $row) { if(!empty($row)) { $item = self::subRow2Item($keys, $row, $varL); if(!empty($item)) { if(isset($returnItem) && $returnItem->{$model->pk} == $item[$model->pk]) { $returnItem->pass($item); } else { $returnItem = new SweetRow($model, $item, $pull); } } } } } } return isset($returnItem) ? $returnItem : null; } else if(isset($this->__model->fields[$var])) { //basicly this @ is here to make sure you call any field on a SweetRow and it will just return null unless it's been set. return !empty($this->__data[0][$var]) ? $this->__data[0][$var] : null; //return @f_first($this->__data)->$var; } }
function generateCookie($checkString, $uid) { //@todo remove the mssql depencdy here. return f_first(f_first( $this->libs->Query->insert(array( 'checkString' => $this->encryptCheckString($checkString), 'created' => time(), 'uid' => $uid ))->into($this->libs->Config->get('Session', 'tableName'))->go()->getDriver()->query('SELECT max(@@IDENTITY) AS \'id\' FROM ' . $this->libs->Config->get('Session', 'tableName'), 'assoc') )) . '_' . $this->encryptCheckString($uid); }
function one() { return f_first($this->libs->Query->select('*')->limit(0, 1)->from($this->tableName)->results()); }
function get($index) { return f_first((array) $this->getPart($index)); }
function _build() { //puts all the stuff together in a magic happy fashion. $sqlString = ''; switch ($this->_mode) { case 'select': //adds in our select values //@todo Make the second parameter in `form()` actaully be a real "sub query" //$this-> if(isset(self::$_fromLimit)) { self::$_fromValue = '(SELECT * FROM ' . Query::$_fromValue . $this->_buildLimit(Query::$_fromLimit) . ') AS ' . Query::$_fromValue; } $sqlString = 'SELECT ' . $this->_buildSelect() . "\n" . ' FROM ' . join(', ', (array)Query::$_fromValue) . $this->_buildJoins() . $this->_buildWhereString($this->_whereValue) . $this->_buildGroupBy() . $this->_buildOrderBy() . $this->_buildLimit($this->_limit); break; case 'update': $sqlString = 'UPDATE ' . f_first(Query::$_fromValue) . "\n" . ' SET ' . $this->_buildSet($this->_setValue) . $this->_buildWhereString($this->_whereValue); break; case 'insert': /* f_reduce( function($a, $b) { return array_merge(array_keys((array)$b), array_keys((array)$a)); }, $this->_insert ); */ if(!is_array(f_first($this->_insert) )) { $this->_insert = array($this->_insert); }; $cols = array_map(function($v) { return Query::nullEscape($v, '`');}, array_keys(array_reduce($this->_insert, 'array_merge_recursive', array()))); $sqlString = 'INSERT INTO ' . f_first((array) Query::$_fromValue) . ' (' . join(', ', $cols) . ') VALUES ' . join(', ', f_map( function($v) use($cols) { return '(' . join(',', f_map( function ($i) use ($v) { $i = substr($i, 1, -1); if(isset($v[$i])) { return Query::nullEscape($v[$i]); } else { return 'null'; } }, $cols )) . ')'; }, D::log($this->_insert, 'Insert Data') )); break; case 'delete': $sqlString = 'DELETE FROM ' . join(', ', (array)Query::$_fromValue) . $this->_buildWhereString($this->_whereValue); break; } $this->sql = $sqlString; return $this->sql; }
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'); }
private function regexArray($regexs) { $matches = array(); foreach($regexs as $regex => $func) { preg_match_all($regex, $this->request, $matches); if(f_first($matches)) { D::log($regex, 'regex'); return f_push( array($func), array_map('f_first', f_rest($matches)) ); } } return false; }
function _build() { //puts all the stuff together in a magic happy fashion. $sqlString = ''; switch ($this->_mode) { case 'select': //adds in our select values D::log('hello'); $sqlString = 'SELECT ' . $this->_buildSelect() . "\n" . ' FROM ' . join(', ', (array)Query::$_fromValue) . $this->_buildJoins() . "\n" . $this->_buildWhereString($this->_whereValue) . $this->_buildOrderBy() . $this->_buildLimit(); break; case 'update': $sqlString = 'UPDATE ' . f_first(Query::$_fromValue) . "\n" . ' SET ' . $this->_buildSet($this->_setValue) . $this->_buildWhereString($this->_whereValue); break; case 'insert': /* f_reduce( function($a, $b) { return array_merge(array_keys((array)$b), array_keys((array)$a)); }, $this->_insert ); */ if(!is_array(f_first($this->_insert) )) { $this->_insert = array($this->_insert); } $cols = array_map(function($v) { return Query::nullEscape($v, '`');}, array_keys(array_reduce($this->_insert, 'array_merge_recursive', array()))); $sqlString = 'INSERT INTO ' . f_first((array) Query::$_fromValue) . ' (' . join(', ', $cols) . ') VALUES ' . join(', ', f_map( function($v) use($cols) { return '(' . join(',', f_map( function ($i) use ($v) { $i = substr($i, 1, -1); if(isset($v[$i])) { return Query::nullEscape($v[$i]); } else { return 'null'; } }, $cols )) . ')'; }, D::log($this->_insert, 'raw incert') )); break; case 'delete': $sqlString = 'DELETE FROM ' . join(', ', Query::$_fromValue) . $this->_buildWhereString($this->_whereValue); break; } $this->sql = $sqlString; D::log($this->sql, 'SQL Build'); return $this->sql; }