%BLOCKQUOTE%
', "", $out); $out = str_replace('", $out); @@ -224,7 +237,8 @@ class Flay extends Object * @param unknown_type $string * @return unknown */ - function extractWords ($string) { + function extractWords ($string) + { return preg_split('/[\s,\.:\/="!\(\)<>~\[\]]+/', $string); } @@ -236,21 +250,27 @@ class Flay extends Object * @param unknown_type $max_snippets * @return unknown */ - function markedSnippets ($words, $string, $max_snippets=5) { + function markedSnippets ($words, $string, $max_snippets=5) + { $string = strip_tags($string); $snips = array(); $rest = $string; - foreach ($words as $word) { - if (preg_match_all("/[\s,]+.{0,40}{$word}.{0,40}[\s,]+/i", $rest, $r)) { + foreach ($words as $word) + { + if (preg_match_all("/[\s,]+.{0,40}{$word}.{0,40}[\s,]+/i", $rest, $r)) + { foreach ($r as $result) $rest = str_replace($result, '', $rest); $snips = array_merge($snips, $r[0]); } } - if (count($snips) > $max_snippets) $snips = array_slice($snips, 0, $max_snippets); + if (count($snips) > $max_snippets) + { + $snips = array_slice($snips, 0, $max_snippets); + } $joined = join(' ... ', $snips); $snips = $joined? "... {$joined} ...": substr($string, 0, 80).'...'; @@ -302,14 +322,17 @@ class Flay extends Object * @param unknown_type $elipsis * @return unknown */ - function fragment ($text, $length, $elipsis='...') { + function fragment ($text, $length, $elipsis='...') + { $soft=$length-5; $hard=$length+5; $rx = '/(.{'.$soft.','.$hard.'})[\s,\.:\/="!\(\)<>~\[\]]+.*/'; - if (preg_match($rx, $text, $r)) { + if (preg_match($rx, $text, $r)) + { $out = $r[1]; } - else { + else + { $out = substr($text,0,$length); } diff --git a/libs/folder.php b/libs/folder.php index 0956b2154..279b22d5f 100644 --- a/libs/folder.php +++ b/libs/folder.php @@ -60,9 +60,13 @@ class Folder extends Object { * * @param string $path */ - function __construct ($path=false) { - if (empty($path)) $path = getcwd(); - $this->cd($path); + function __construct ($path=false) + { + if (empty($path)) + { + $path = getcwd(); + } + $this->cd($path); } /** @@ -70,7 +74,8 @@ class Folder extends Object { * * @return string Current path */ - function pwd () { + function pwd () + { return $this->path; } @@ -80,7 +85,8 @@ class Folder extends Object { * @param string $desired_path Path to the directory to change to * @return string The new path. Returns false on failure */ - function cd ($desired_path) { + function cd ($desired_path) + { $desired_path = realpath($desired_path); $new_path = Folder::isAbsolute($desired_path)? $desired_path: @@ -97,28 +103,38 @@ class Folder extends Object { * @param boolean $sort * @return array */ - function ls($sort=true) { + function ls($sort=true) + { $dir = opendir($this->path); - if ($dir) { + if ($dir) + { $dirs = $files = array(); - while (false !== ($n = readdir($dir))) { - if (!preg_match('#^\.+$#', $n)) { + while (false !== ($n = readdir($dir))) + { + if (!preg_match('#^\.+$#', $n)) + { if (is_dir($this->addPathElement($this->path, $n))) + { $dirs[] = $n; - else + } + else + { $files[] = $n; + } } } - if ($sort || $this->sort) { + if ($sort || $this->sort) + { sort($dirs); sort($files); } return array($dirs,$files); } - else { + else + { return false; } } @@ -135,7 +151,9 @@ class Folder extends Object { $data = $this->ls(); if (!is_array($data)) + { return array(); + } list($dirs, $files) = $data; @@ -158,7 +176,8 @@ class Folder extends Object { * @param string $pattern Preg_match pattern (Defaults to: .*) * @return array Files matching $pattern */ - function findRecursive ($pattern='.*') { + function findRecursive ($pattern='.*') + { $starts_on = $this->path; $out = $this->_findRecursive($pattern); $this->cd($starts_on); @@ -172,18 +191,22 @@ class Folder extends Object { * @return array Files matching pattern * @access private */ - function _findRecursive ($pattern) { + function _findRecursive ($pattern) + { list($dirs, $files) = $this->ls(); $found = array(); - foreach ($files as $file) { - if (preg_match("/^{$pattern}$/i", $file)) { + foreach ($files as $file) + { + if (preg_match("/^{$pattern}$/i", $file)) + { $found[] = $this->addPathElement($this->path, $file); } } $start = $this->path; - foreach ($dirs as $dir) { + foreach ($dirs as $dir) + { $this->cd($this->addPathElement($start, $dir)); $found = array_merge($found, $this->findRecursive($pattern)); } @@ -197,7 +220,8 @@ class Folder extends Object { * @param string $path Path to check * @return boolean */ - function isWindowsPath ($path) { + function isWindowsPath ($path) + { return preg_match('#^[A-Z]:\\\#i', $path)? true: false; } @@ -207,7 +231,8 @@ class Folder extends Object { * @param string $path Path to check * @return boolean */ - function isAbsolute ($path) { + function isAbsolute ($path) + { return preg_match('#^\/#', $path) || preg_match('#^[A-Z]:\\\#i', $path); } @@ -217,7 +242,8 @@ class Folder extends Object { * @param string $path Path to check * @return boolean */ - function isSlashTerm ($path) { + function isSlashTerm ($path) + { return preg_match('#[\\\/]$#', $path)? true: false; } @@ -227,7 +253,8 @@ class Folder extends Object { * @param string $path Path to check * @return string Set of slashes ("\\" or "/") */ - function correctSlashFor ($path) { + function correctSlashFor ($path) + { return Folder::isWindowsPath($path)? '\\': '/'; } @@ -237,7 +264,8 @@ class Folder extends Object { * @param string $path Path to check * @return string */ - function slashTerm ($path) { + function slashTerm ($path) + { return $path . (Folder::isSlashTerm($path)? null: Folder::correctSlashFor($path)); } @@ -248,7 +276,8 @@ class Folder extends Object { * @param string $element * @return string */ - function addPathElement ($path, $element) { + function addPathElement ($path, $element) + { return Folder::slashTerm($path).$element; } } diff --git a/libs/inflector.php b/libs/inflector.php index ebc1d717f..936db2d71 100644 --- a/libs/inflector.php +++ b/libs/inflector.php @@ -48,7 +48,8 @@ class Inflector extends Object * Constructor. * */ - function __construct () { + function __construct () + { parent::__construct(); } @@ -58,7 +59,8 @@ class Inflector extends Object * @param string $word Word in singular * @return string Word in plural */ - function pluralize ($word) { + function pluralize ($word) + { $plural_rules = array( '/(x|ch|ss|sh)$/' => '\1es', # search, switch, fix, box, process, address '/series$/' => '\1series', @@ -74,8 +76,10 @@ class Inflector extends Object '/$/' => 's' ); - foreach ($plural_rules as $rule => $replacement) { - if (preg_match($rule, $word)) { + foreach ($plural_rules as $rule => $replacement) + { + if (preg_match($rule, $word)) + { return preg_replace($rule, $replacement, $word); } } @@ -108,8 +112,10 @@ class Inflector extends Object '/s$/' => '' ); - foreach ($singular_rules as $rule => $replacement) { - if (preg_match($rule, $word)) { + foreach ($singular_rules as $rule => $replacement) + { + if (preg_match($rule, $word)) + { return preg_replace($rule, $replacement, $word); } } @@ -123,7 +129,8 @@ class Inflector extends Object * @param string $lower_case_and_underscored_word Word to camelize * @return string Camelized word. likeThis. */ - function camelize($lower_case_and_underscored_word) { + function camelize($lower_case_and_underscored_word) + { return str_replace(" ","",ucwords(str_replace("_"," ",$lower_case_and_underscored_word))); } @@ -133,7 +140,8 @@ class Inflector extends Object * @param string $camel_cased_word Camel-cased word to be "underscorized" * @return string Underscore-syntaxed version of the $camel_cased_word */ - function underscore($camel_cased_word) { + function underscore($camel_cased_word) + { $camel_cased_word = preg_replace('/([A-Z]+)([A-Z])/','\1_\2',$camel_cased_word); return strtolower(preg_replace('/([a-z])([A-Z])/','\1_\2',$camel_cased_word)); } @@ -145,7 +153,8 @@ class Inflector extends Object * @param string $lower_case_and_underscored_word String to be made more readable * @return string Human-readable string */ - function humanize($lower_case_and_underscored_word) { + function humanize($lower_case_and_underscored_word) + { return ucwords(str_replace("_"," ",$lower_case_and_underscored_word)); } @@ -155,7 +164,8 @@ class Inflector extends Object * @param string $class_name Name of class to get database table name for * @return string Name of the database table for given class */ - function tableize($class_name) { + function tableize($class_name) + { return Inflector::pluralize(Inflector::underscore($class_name)); } diff --git a/libs/legacy.php b/libs/legacy.php index a1d2fe598..e8cd3f3a4 100644 --- a/libs/legacy.php +++ b/libs/legacy.php @@ -34,9 +34,11 @@ /** * Enter description here... */ -if (version_compare(phpversion(), '5.0') < 0) { +if (version_compare(phpversion(), '5.0') < 0) +{ eval(' - function clone($object) { + function clone($object) + { return $object; } '); @@ -44,7 +46,8 @@ if (version_compare(phpversion(), '5.0') < 0) { -if (!function_exists('file_get_contents')) { +if (!function_exists('file_get_contents')) +{ /** * Replace file_get_contents() * @@ -60,18 +63,22 @@ if (!function_exists('file_get_contents')) { */ function file_get_contents($filename, $incpath = false) { - if (false === $fh = fopen($filename, 'rb', $incpath)) { + if (false === $fh = fopen($filename, 'rb', $incpath)) + { user_error('file_get_contents() failed to open stream: No such file or directory', E_USER_WARNING); return false; } clearstatcache(); - if ($fsize = @filesize($filename)) { + if ($fsize = @filesize($filename)) + { $data = fread($fh, $fsize); - } else { + } else + { $data = ''; - while (!feof($fh)) { + while (!feof($fh)) + { $data .= fread($fh, 8192); } } diff --git a/libs/model.php b/libs/model.php index 9baa49619..ad19f5702 100644 --- a/libs/model.php +++ b/libs/model.php @@ -193,6 +193,10 @@ class Model extends Object { $this->_hasOneLink(); } + if (!empty($this->hasMany)) + { + return $this->_hasManyLinks(); + } } @@ -292,12 +296,80 @@ class Model extends Object } $association = explode(',', $this->hasOne); - foreach ($association as $modelName) { + foreach ($association as $modelName) + { $this->_constructAssociatedModels($modelName , 'One'); } } } + + function _hasManyLinks() + { + if(is_array($this->hasMany)) + { + $this->_resetCount(); + + foreach ($this->hasMany as $association => $associationValue) + { + $className = $association; + $this->_hasMany = array($className,$association); + + foreach ($associationValue as $option => $optionValue) + { + switch ($option) + { + case 'className': + //$this->__joinedHasMany[$count][$this->table]['className'] = $optionValue; + //$this->__joinedHasMany[$count][$this->table]['association'] = $association; + break; + + case 'conditions': + //$this->__joinedHasMany[$count][$this->table]['conditions'] = $optionValue; + break; + + case 'order': + //$this->__joinedHasMany[$count][$this->table]['order'] = $optionValue; + break; + + case 'foreignKey': + $modelForeignKey = $this->table .'To'. $className . 'ForeignKey'; + $foreignKey = $optionValue; + $this->$modelForeignKey = $foreignKey; + unset($modelForeignKey); + break; + + case 'dependent': + //$this->__joinedHasMany[$count][$this->table]['dependent'] = $optionValue; + break; + + case 'exclusive': + //$this->__joinedHasMany[$count][$this->table]['exclusive'] = $optionValue; + break; + + case 'finderSql': + //$this->__joinedHasMany[$count][$this->table]['finderSql'] = $optionValue; + break; + + case 'counterSql': + //$this->__joinedHasMany[$count][$this->table]['counterSql'] = $optionValue; + break; + } + } + $this->linkManyToOne($className, $this->id[$this->_count]); + } + } + else + { + $this->_hasMany = explode(',', $this->hasMany); + $this->_resetCount(); + + foreach ($this->_hasMany as $modelName) + { + $this->_constructAssociatedModels($modelName , 'Many'); + } + } + } /** @@ -320,6 +392,11 @@ class Model extends Object $joinedHas = 'joinedHasOne'; break; + case 'Many': + $this->linkManyToOne($modelName, $this->id[$this->_count++]); + $joinedHas = 'joinedHasMany'; + break; + default: //nothing break; @@ -327,7 +404,7 @@ class Model extends Object if(!isset($this->$className)) { - $this->$className = new $className(); + $this->$className = &new $className(); } $this->{$joinedHas}[] = $this->$className; $this->relink(); @@ -337,16 +414,22 @@ class Model extends Object * Updates this model's association links, by emptying the links list, and then link"*Association Type" again. * */ - function relink () { + function relink () + { - if(!empty($this->id)){ + if(!empty($this->id)) + { $i = 1; } - foreach ($this->_hasOne as $table) { - if(is_array($table)){ + foreach ($this->_hasOne as $table) + { + if(is_array($table)) + { $names[] = explode(',', $table); - } else { + } + else + { $names[0] = $table; $names[1] = $table; } @@ -355,6 +438,22 @@ class Model extends Object $this->$className->clearLinks(); $this->$className->linkOneToOne($tableName, $this->id[$i]); } + foreach ($this->_hasMany as $table) + { + if(is_array($table)) + { + $names[] = explode(',', $table); + } + else + { + $names[0] = $table; + $names[1] = $table; + } + $className = $names[1]; + $tableName = Inflector::singularize($names[0]); + $this->clearLinks(); + $this->linkManyToOne($tableName, $this->id[0]); + } } @@ -366,11 +465,20 @@ class Model extends Object * @param string $model_name Name of model to link to * @param unknown_type $value Defaults to NULL. */ - function linkManyToOne ($model_name, $value=null) + function linkManyToOne ($tableName, $value=null) { - $table_name = Inflector::tableize($model_name); - $field_name = Inflector::singularize($table_name).'_id'; - $this->_one_to_many[] = array($table_name, $field_name, $value); + $tableName = Inflector::tableize($tableName); + $fieldKey = $this->table .'To'. Inflector::singularize($tableName) . 'ForeignKey'; + + if(!empty($this->$fieldKey)) + { + $field_name = $this->$fieldKey; + } + else + { + $field_name = Inflector::singularize($this->table).'_id'; + } + $this->_oneToMany[] = array($tableName, $field_name, $value); } /** @@ -447,7 +555,8 @@ class Model extends Object foreach ($data as $n => $v) { /* - if (!$this->hasField($n)) { + if (!$this->hasField($n)) + { DEBUG? trigger_error(sprintf(ERROR_NO_FIELD_IN_MODEL_DB, $n, $this->table), E_USER_ERROR): trigger_error('Application error occured, trying to set a field name that doesn\'t exist.', E_USER_WARNING); @@ -455,7 +564,8 @@ class Model extends Object */ //$n == 'id'? $this->setId($v): $this->data[$n] = $v; - foreach ($v as $x => $y){ + foreach ($v as $x => $y) + { //$x == 'id'? $this->id = $y: $this->data[$n][$x] = $y; if($x == 'id') { @@ -486,8 +596,10 @@ class Model extends Object */ function loadInfo () { - if (empty($this->_table_info)) + if (empty($this->_table_info)) + { $this->_table_info = new NeatArray($this->db->fields($this->table)); + } return $this->_table_info; } @@ -500,7 +612,10 @@ class Model extends Object */ function hasField ($name) { - if (empty($this->_table_info)) $this->loadInfo(); + if (empty($this->_table_info)) + { + $this->loadInfo(); + } return $this->_table_info->findIn('name', $name); } @@ -572,10 +687,15 @@ class Model extends Object function save ($data=null, $validate=true) { - if ($data) $this->set($data); + if ($data) + { + $this->set($data); + } if ($validate && !$this->validates()) + { return false; + } $fields = $values = array(); @@ -584,12 +704,12 @@ class Model extends Object foreach ($v as $x => $y) { - if ($this->hasField($x)) - { - $fields[] = $x; - $values[] = $this->db->prepare($y); - } - } + if ($this->hasField($x)) + { + $fields[] = $x; + $values[] = $this->db->prepare($y); + } + } } if (empty($this->id) && $this->hasField('created') && !in_array('created', $fields)) @@ -608,7 +728,8 @@ class Model extends Object } if(count($fields)) { - if(!empty($this->id)){ + if(!empty($this->id)) + { $sql = array(); foreach (array_combine($fields, $values) as $field=>$value) @@ -673,14 +794,19 @@ class Model extends Object */ function del ($id=null) { - if ($id) $this->id = $id; + if ($id) + { + $this->id = $id; + } if ($this->id && $this->db->query("DELETE FROM {$this->table} WHERE id = '{$this->id}'")) { $this->id = false; return true; } else + { return false; + } } /** @@ -759,29 +885,26 @@ class Model extends Object $conditions = $this->parseConditions($conditions); if (is_array($fields)) + { $f = $fields; + } elseif ($fields) + { $f = array($fields); + } else + { $f = array('*'); + } $joins = $whers = array(); foreach ($this->_oneToOne as $rule) { - list($table, $field, $value) = $rule; $joins[] = "LEFT JOIN {$table} ON {$this->table}.{$field} = {$table}.id"; - if(empty($this->id)) - { - $whers[] = "{$table}.id != 'NULL'"; - } - else{ - $whers[] = "{$this->table}.{$field} = '{$value}'"; - } } - $joins = count($joins)? join(' ', $joins): null; $whers = count($whers)? '('.join(' AND ', $whers).')': null; $conditions .= ($conditions && $whers? ' AND ': null).$whers; @@ -800,8 +923,29 @@ class Model extends Object .($order? " ORDER BY {$order}": null) .$limit_str; - $data = $this->db->all($sql); - + $data = $this->db->all($sql); + + if(!empty($this->_oneToMany)) + { + $datacheck = $data; + foreach ($this->_oneToMany as $rule) + { + $count = 0; + list($table, $field, $value) = $rule; + foreach ($datacheck as $key => $value1) + { + foreach ($value1 as $key2 => $value2) + { + $select = $this->db->all("SELECT * FROM {$table} WHERE ($field) = {$value2['id']}"); + $data2 = array_merge_recursive($data[$count],$select); + $data1[$count] = $data2; + } + $count++; + } + $data = $data1; + $this->joinedHasMany[] = new NeatArray($this->db->fields($table)); + } + } return $data; } @@ -927,11 +1071,15 @@ class Model extends Object function _invalidFields ($data=null) { if (!isset($this->validate)) + { return true; + } if (is_array($this->validationErrors)) + { return $this->validationErrors; - + } + $data = ($data? $data: (isset($this->data)? $this->data: array())); $errors = array(); foreach ($this->data as $table => $field) @@ -939,7 +1087,9 @@ class Model extends Object foreach ($this->validate as $field_name=>$validator) { if (!isset($data[$table][$field_name]) || !preg_match($validator, $data[$table][$field_name])) - $errors[$field_name] = 1; + { + $errors[$field_name] = 1; + } } $this->validationErrors = $errors; return $errors; diff --git a/libs/neat_array.php b/libs/neat_array.php index 6ba60ca71..9d70739d0 100644 --- a/libs/neat_array.php +++ b/libs/neat_array.php @@ -52,7 +52,8 @@ class NeatArray { * @access public * @uses NeatArray::value */ - function NeatArray ($value=array()) { + function NeatArray ($value=array()) + { $this->value = $value; } @@ -90,10 +91,13 @@ class NeatArray { * @access public * @uses NeatArray::value */ - function cleanup () { + function cleanup () + { $out = is_array($this->value)? array(): null; - foreach ($this->value as $k=>$v) { - if ($v) { + foreach ($this->value as $k=>$v) + { + if ($v) + { $out[$k] = $v; } } @@ -108,7 +112,8 @@ class NeatArray { * @access public * @uses NeatArray::value */ - function add ($value) { + function add ($value) + { return ($this->value = $this->plus($value))? true: false; } @@ -120,7 +125,8 @@ class NeatArray { * @access public * @uses NeatArray::value */ - function plus ($value) { + function plus ($value) + { return array_merge($this->value, (is_array($value)? $value: array($value))); } @@ -132,16 +138,21 @@ class NeatArray { * @access public * @uses NeatArray::value */ - function totals ($sortedBy=1,$reverse=true) { + function totals ($sortedBy=1,$reverse=true) + { $out = array(); foreach ($this->value as $val) + { isset($out[$val])? $out[$val]++: $out[$val] = 1; + } - if ($sortedBy == 1) { + if ($sortedBy == 1) + { $reverse? arsort($out, SORT_NUMERIC): asort($out, SORT_NUMERIC); } - if ($sortedBy == 2) { + if ($sortedBy == 2) + { $reverse? krsort($out, SORT_STRING): ksort($out, SORT_STRING); } @@ -154,7 +165,8 @@ class NeatArray { * @param unknown_type $with * @return unknown */ - function filter ($with) { + function filter ($with) + { return $this->value = array_filter($this->value, $with); } @@ -165,7 +177,8 @@ class NeatArray { * @access public * @uses NeatArray::value */ - function walk ($with) { + function walk ($with) + { array_walk($this->value, $with); return $this->value; } @@ -193,9 +206,11 @@ class NeatArray { * @access public * @uses NeatArray::value */ - function extract ($name) { + function extract ($name) + { $out = array(); - foreach ($this->value as $val) { + foreach ($this->value as $val) + { if (isset($val[$name])) $out[] = $val[$name]; } @@ -207,7 +222,8 @@ class NeatArray { * * @return array */ - function unique () { + function unique () + { return array_unique($this->value); } @@ -216,7 +232,8 @@ class NeatArray { * * @return array */ - function makeUnique () { + function makeUnique () + { return $this->value = array_unique($this->value); } @@ -253,18 +270,25 @@ class NeatArray { * @return array */ - function joinWith ($his, $onMine, $onHis=null) { - if (empty($onHis)) $onHis = $onMine; + function joinWith ($his, $onMine, $onHis=null) + { + if (empty($onHis)) + { + $onHis = $onMine; + } $his = new NeatArray($his); $out = array(); - foreach ($this->value as $key=>$val) { - if ($fromHis = $his->findIn($onHis, $val[$onMine])) { + foreach ($this->value as $key=>$val) + { + if ($fromHis = $his->findIn($onHis, $val[$onMine])) + { list($fromHis) = array_values($fromHis); $out[$key] = array_merge($val, $fromHis); } - else { + else + { $out[$key] = $val; } } @@ -281,11 +305,14 @@ class NeatArray { * @param unknown_type $childrenKey * @return unknown */ - function threaded ($root=null, $idKey='id', $parentIdKey='parent_id', $childrenKey='children') { + function threaded ($root=null, $idKey='id', $parentIdKey='parent_id', $childrenKey='children') + { $out = array(); - for ($ii=0; $ii%ENDBLOCKQUOTE%
', "