mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Merging changes from revisions [353] [354] [355] [356]
git-svn-id: https://svn.cakephp.org/repo/trunk/cake@358 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
e0733f4749
commit
36320ff8e4
13 changed files with 563 additions and 215 deletions
111
libs/bake.php
111
libs/bake.php
|
@ -92,8 +92,10 @@ class Bake extends Object {
|
|||
* @return string
|
||||
* @access private
|
||||
*/
|
||||
function template ($type) {
|
||||
switch ($type) {
|
||||
function template ($type)
|
||||
{
|
||||
switch ($type)
|
||||
{
|
||||
case 'view': return "%s";
|
||||
case 'model': return "<?php\n\nclass %s extends AppModel {\n}\n\n?>";
|
||||
case 'action': return "\n\tfunction %s () {\n\t}\n";
|
||||
|
@ -105,17 +107,20 @@ class %sTest extends TestCase {
|
|||
var $abc;
|
||||
|
||||
// called before the tests
|
||||
function setUp() {
|
||||
function setUp()
|
||||
{
|
||||
$this->abc = new %s ();
|
||||
}
|
||||
|
||||
// called after the tests
|
||||
function tearDown() {
|
||||
function tearDown()
|
||||
{
|
||||
unset($this->abc);
|
||||
}
|
||||
|
||||
/*
|
||||
function testFoo () {
|
||||
function testFoo ()
|
||||
{
|
||||
$result = $this->abc->Foo();
|
||||
$expected = \'\';
|
||||
$this->assertEquals($result, $expected);
|
||||
|
@ -143,7 +148,8 @@ class %sTest extends TestCase {
|
|||
* @uses Bake::newView() Depending on the case, can create a new view.
|
||||
* @uses Bake::newController() Depending on the case, can create a new controller.
|
||||
*/
|
||||
function __construct ($type, $names) {
|
||||
function __construct ($type, $names)
|
||||
{
|
||||
|
||||
$this->stdin = fopen('php://stdin', 'r');
|
||||
$this->stdout = fopen('php://stdout', 'w');
|
||||
|
@ -152,7 +158,8 @@ class %sTest extends TestCase {
|
|||
// Output directory name
|
||||
fwrite($this->stderr, "\n".substr(ROOT,0,strlen(ROOT)-1).":\n".str_repeat('-',strlen(ROOT)+1)."\n");
|
||||
|
||||
switch ($type) {
|
||||
switch ($type)
|
||||
{
|
||||
|
||||
case 'model':
|
||||
case 'models':
|
||||
|
@ -165,7 +172,8 @@ class %sTest extends TestCase {
|
|||
$controller = array_shift($names);
|
||||
|
||||
$add_actions = array();
|
||||
foreach ($names as $action) {
|
||||
foreach ($names as $action)
|
||||
{
|
||||
$add_actions[] = $action;
|
||||
$this->newView($controller, $action);
|
||||
}
|
||||
|
@ -176,8 +184,10 @@ class %sTest extends TestCase {
|
|||
case 'view':
|
||||
case 'views':
|
||||
$r = null;
|
||||
foreach ($names as $model_name) {
|
||||
if (preg_match('/^([a-z0-9_]+(?:\/[a-z0-9_]+)*)\/([a-z0-9_]+)$/i', $model_name, $r)) {
|
||||
foreach ($names as $model_name)
|
||||
{
|
||||
if (preg_match('/^([a-z0-9_]+(?:\/[a-z0-9_]+)*)\/([a-z0-9_]+)$/i', $model_name, $r))
|
||||
{
|
||||
$this->newView($r[1], $r[2]);
|
||||
}
|
||||
}
|
||||
|
@ -202,7 +212,8 @@ class %sTest extends TestCase {
|
|||
* @uses Bake::template() Collects view template.
|
||||
* @uses Bake::actions Adds one action for each run.
|
||||
*/
|
||||
function newView ($controller, $name) {
|
||||
function newView ($controller, $name)
|
||||
{
|
||||
// $controller = Inflector::pluralize($controller);
|
||||
$dir = Inflector::underscore($controller);
|
||||
$path = $dir.DS.strtolower($name).".thtml";
|
||||
|
@ -226,7 +237,8 @@ class %sTest extends TestCase {
|
|||
* @uses Bake::makeHelperTest()
|
||||
* @uses Bake::actions Adds one action for each run.
|
||||
*/
|
||||
function newController ($name, $actions=array()) {
|
||||
function newController ($name, $actions=array())
|
||||
{
|
||||
// $name = Inflector::pluralize($name);
|
||||
$this->makeController($name, $actions);
|
||||
$this->makeControllerTest($name);
|
||||
|
@ -249,7 +261,8 @@ class %sTest extends TestCase {
|
|||
* @uses Bake::createFile() Creates controller's file.
|
||||
* @uses Bake::makeControllerFn() Underscored name for controller's filename.
|
||||
*/
|
||||
function makeController ($name, $actions) {
|
||||
function makeController ($name, $actions)
|
||||
{
|
||||
$ctrl = $this->makeControllerName($name);
|
||||
$helper = $this->makeHelperName($name);
|
||||
$body = sprintf($this->template('ctrl'), $ctrl, $helper, join('', $this->getActions($actions)));
|
||||
|
@ -264,7 +277,8 @@ class %sTest extends TestCase {
|
|||
* @access private
|
||||
* @uses Inflector::camelize CamelCase for controller name.
|
||||
*/
|
||||
function makeControllerName ($name) {
|
||||
function makeControllerName ($name)
|
||||
{
|
||||
return Inflector::camelize($name).'Controller';
|
||||
}
|
||||
|
||||
|
@ -276,7 +290,8 @@ class %sTest extends TestCase {
|
|||
* @access private
|
||||
* @uses Inflector::underscore() Underscore for controller's file name.
|
||||
*/
|
||||
function makeControllerFn ($name) {
|
||||
function makeControllerFn ($name)
|
||||
{
|
||||
return CONTROLLERS.Inflector::underscore($name).'_controller.php';
|
||||
}
|
||||
|
||||
|
@ -292,7 +307,8 @@ class %sTest extends TestCase {
|
|||
* @uses Bake::makeControllerName()
|
||||
* @uses Bake::createFile()
|
||||
*/
|
||||
function makeControllerTest ($name) {
|
||||
function makeControllerTest ($name)
|
||||
{
|
||||
$fn = CONTROLLER_TESTS.Inflector::underscore($name).'_controller_test.php';
|
||||
$body = $this->getTestBody($this->makeControllerName($name));
|
||||
return $this->createFile($fn, $body);
|
||||
|
@ -309,7 +325,8 @@ class %sTest extends TestCase {
|
|||
* @uses Bake::createFile()
|
||||
* @uses Bake::makeHelperFn()
|
||||
*/
|
||||
function makeHelper ($name) {
|
||||
function makeHelper ($name)
|
||||
{
|
||||
$body = sprintf($this->template('helper'), $this->makeHelperName($name));
|
||||
return $this->createFile($this->makeHelperFn($name), $body);
|
||||
}
|
||||
|
@ -322,7 +339,8 @@ class %sTest extends TestCase {
|
|||
* @access private
|
||||
* @uses Inflector::camelize()
|
||||
*/
|
||||
function makeHelperName ($name) {
|
||||
function makeHelperName ($name)
|
||||
{
|
||||
return Inflector::camelize($name).'Helper';
|
||||
}
|
||||
|
||||
|
@ -335,7 +353,8 @@ class %sTest extends TestCase {
|
|||
* @uses HELPERS
|
||||
* @uses Inflector::underscore()
|
||||
*/
|
||||
function makeHelperFn ($name) {
|
||||
function makeHelperFn ($name)
|
||||
{
|
||||
return HELPERS.Inflector::underscore($name).'_helper.php';
|
||||
}
|
||||
|
||||
|
@ -351,7 +370,8 @@ class %sTest extends TestCase {
|
|||
* @uses Bake::makeHelperName()
|
||||
* @uses Bake::createFile()
|
||||
*/
|
||||
function makeHelperTest ($name) {
|
||||
function makeHelperTest ($name)
|
||||
{
|
||||
$fn = HELPER_TESTS.Inflector::underscore($name).'_helper_test.php';
|
||||
$body = $this->getTestBody($this->makeHelperName($name));
|
||||
return $this->createFile($fn, $body);
|
||||
|
@ -365,7 +385,8 @@ class %sTest extends TestCase {
|
|||
* @access private
|
||||
* @uses Bake::template()
|
||||
*/
|
||||
function getActions ($as) {
|
||||
function getActions ($as)
|
||||
{
|
||||
$out = array();
|
||||
foreach ($as as $a)
|
||||
$out[] = sprintf($this->template('action'), $a);
|
||||
|
@ -380,7 +401,8 @@ class %sTest extends TestCase {
|
|||
* @access private
|
||||
* @uses Bake::template()
|
||||
*/
|
||||
function getTestBody ($class) {
|
||||
function getTestBody ($class)
|
||||
{
|
||||
return sprintf($this->template('test'), $class, $class);
|
||||
}
|
||||
|
||||
|
@ -396,7 +418,8 @@ class %sTest extends TestCase {
|
|||
* @uses Bake::makeModelTest()
|
||||
* @uses Bake::actions
|
||||
*/
|
||||
function newModel ($name) {
|
||||
function newModel ($name)
|
||||
{
|
||||
$this->createFile($this->getModelFn($name), sprintf($this->template('model'), $this->getModelName($name)));
|
||||
$this->makeModelTest ($name);
|
||||
$this->actions++;
|
||||
|
@ -411,7 +434,8 @@ class %sTest extends TestCase {
|
|||
* @uses MODELS
|
||||
* @uses Inflector::underscore()
|
||||
*/
|
||||
function getModelFn ($name) {
|
||||
function getModelFn ($name)
|
||||
{
|
||||
return MODELS.Inflector::underscore($name).'.php';
|
||||
}
|
||||
|
||||
|
@ -427,7 +451,8 @@ class %sTest extends TestCase {
|
|||
* @uses Bake::getModelName()
|
||||
* @uses Bake::createFile()
|
||||
*/
|
||||
function makeModelTest ($name) {
|
||||
function makeModelTest ($name)
|
||||
{
|
||||
$fn = MODEL_TESTS.Inflector::underscore($name).'_test.php';
|
||||
$body = $this->getTestBody($this->getModelName($name));
|
||||
return $this->createFile($fn, $body);
|
||||
|
@ -441,7 +466,8 @@ class %sTest extends TestCase {
|
|||
* @access private
|
||||
* @uses Inflector::camelize()
|
||||
*/
|
||||
function getModelName ($name) {
|
||||
function getModelName ($name)
|
||||
{
|
||||
return Inflector::camelize($name);
|
||||
}
|
||||
|
||||
|
@ -457,36 +483,44 @@ class %sTest extends TestCase {
|
|||
* @uses Bake::stdout
|
||||
* @uses Bake::stderr
|
||||
*/
|
||||
function createFile ($path, $contents) {
|
||||
function createFile ($path, $contents)
|
||||
{
|
||||
$shortPath = str_replace(ROOT,null,$path);
|
||||
|
||||
if (is_file($path) && !$this->dontAsk) {
|
||||
if (is_file($path) && !$this->dontAsk)
|
||||
{
|
||||
fwrite($this->stdout, "File {$shortPath} exists, overwrite? (yNaq) ");
|
||||
$key = trim(fgets($this->stdin));
|
||||
|
||||
if ($key=='q') {
|
||||
if ($key=='q')
|
||||
{
|
||||
fwrite($this->stdout, "Quitting.\n");
|
||||
exit;
|
||||
}
|
||||
elseif ($key=='a') {
|
||||
elseif ($key=='a')
|
||||
{
|
||||
$this->dont_ask = true;
|
||||
}
|
||||
elseif ($key=='y') {
|
||||
elseif ($key=='y')
|
||||
{
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
fwrite($this->stdout, "Skip {$shortPath}\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ($f = fopen($path, 'w')) {
|
||||
if ($f = fopen($path, 'w'))
|
||||
{
|
||||
fwrite($f, $contents);
|
||||
fclose($f);
|
||||
fwrite($this->stdout, "Wrote {$shortPath}\n");
|
||||
// debug ("Wrote {$path}");
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
fwrite($this->stderr, "Error! Couldn't open {$shortPath} for writing.\n");
|
||||
// debug ("Error! Couldn't open {$path} for writing.");
|
||||
return false;
|
||||
|
@ -502,18 +536,21 @@ class %sTest extends TestCase {
|
|||
* @uses Bake::stdin
|
||||
* @uses Bake::stdout
|
||||
*/
|
||||
function createDir ($path) {
|
||||
function createDir ($path)
|
||||
{
|
||||
if (is_dir($path))
|
||||
return true;
|
||||
|
||||
$shortPath = str_replace(ROOT, null, $path);
|
||||
|
||||
if (mkdir($path)) {
|
||||
if (mkdir($path))
|
||||
{
|
||||
fwrite($this->stdout, "Created {$shortPath}\n");
|
||||
// debug ("Created {$path}");
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
fwrite($this->stderr, "Error! Couldn't create dir {$shortPath}\n");
|
||||
// debug ("Error! Couldn't create dir {$path}");
|
||||
return false;
|
||||
|
|
|
@ -78,7 +78,8 @@ class Cache extends Model {
|
|||
*
|
||||
* @param unknown_type $id
|
||||
*/
|
||||
function __construct ($id) {
|
||||
function __construct ($id)
|
||||
{
|
||||
$this->id = (md5($id));
|
||||
parent::__construct($this->id);
|
||||
}
|
||||
|
@ -89,7 +90,8 @@ class Cache extends Model {
|
|||
* @param unknown_type $id
|
||||
* @return unknown
|
||||
*/
|
||||
function id ($id=null) {
|
||||
function id ($id=null)
|
||||
{
|
||||
if (!$id) return $this->id;
|
||||
return ($this->id = $id);
|
||||
}
|
||||
|
@ -101,7 +103,8 @@ class Cache extends Model {
|
|||
* @param int $keep_for Number of seconds to keep data in cache.
|
||||
* @return unknown
|
||||
*/
|
||||
function remember ($content, $keep_for=CACHE_PAGES_FOR) {
|
||||
function remember ($content, $keep_for=CACHE_PAGES_FOR)
|
||||
{
|
||||
$data = addslashes($this->for_caching.$content);
|
||||
$expire = date("Y-m-d H:i:s",time()+($keep_for>0? $keep_for: 999999999));
|
||||
return $this->query("REPLACE {$this->use_table} (id,data,expire) VALUES ('{$this->id}', '{$data}', '{$expire}')");
|
||||
|
@ -112,7 +115,8 @@ class Cache extends Model {
|
|||
*
|
||||
* @return unknown
|
||||
*/
|
||||
function restore() {
|
||||
function restore()
|
||||
{
|
||||
if (empty($this->data['data']))
|
||||
return $this->find("id='{$this->id}' AND expire>NOW()");
|
||||
|
||||
|
@ -124,7 +128,8 @@ class Cache extends Model {
|
|||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function has() {
|
||||
function has()
|
||||
{
|
||||
return is_array($this->data = $this->find("id='{$this->id}' AND expire>NOW()"));
|
||||
}
|
||||
|
||||
|
@ -133,7 +138,8 @@ class Cache extends Model {
|
|||
*
|
||||
* @param string $string
|
||||
*/
|
||||
function append($string) {
|
||||
function append($string)
|
||||
{
|
||||
$this->for_caching .= $string;
|
||||
}
|
||||
|
||||
|
@ -142,7 +148,8 @@ class Cache extends Model {
|
|||
*
|
||||
* @return unknown
|
||||
*/
|
||||
function clear() {
|
||||
function clear()
|
||||
{
|
||||
return $this->query("DELETE FROM {$this->use_table}");
|
||||
}
|
||||
}
|
||||
|
|
10
libs/dbo.php
10
libs/dbo.php
|
@ -39,11 +39,13 @@
|
|||
* $first_row_only = $db->one("SELECT a,b,c FROM table WHERE a=1", TRUE);
|
||||
*
|
||||
* // emulate the usual way of reading query results
|
||||
* if ($db->query("SELECT a,b,c FROM table")) {
|
||||
* while ( $row = $db->farr() ) {
|
||||
* print $row['a'].$row['b'].$row['c'];
|
||||
* if ($db->query("SELECT a,b,c FROM table"))
|
||||
* {
|
||||
* while ( $row = $db->farr() )
|
||||
* {
|
||||
* print $row['a'].$row['b'].$row['c'];
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* // show a log of all queries, sorted by execution time
|
||||
* $db->showLog(TRUE);
|
||||
|
|
|
@ -64,7 +64,8 @@ class Flay extends Object
|
|||
*
|
||||
* @param unknown_type $text
|
||||
*/
|
||||
function __construct ($text=null) {
|
||||
function __construct ($text=null)
|
||||
{
|
||||
$this->text = $text;
|
||||
parent::__construct();
|
||||
}
|
||||
|
@ -77,7 +78,8 @@ class Flay extends Object
|
|||
* @param boolean $allowHtml Set this to trim whitespace and disable all HTML
|
||||
* @return string Formatted text
|
||||
*/
|
||||
function toHtml ($text=null, $bare=false, $allowHtml=false) {
|
||||
function toHtml ($text=null, $bare=false, $allowHtml=false)
|
||||
{
|
||||
|
||||
if (empty($text) && empty($this->text))
|
||||
return false;
|
||||
|
@ -90,7 +92,8 @@ class Flay extends Object
|
|||
else
|
||||
$text = str_replace('<', '<', str_replace('>', '>', trim($text)));
|
||||
|
||||
if (!$bare) {
|
||||
if (!$bare)
|
||||
{
|
||||
// multi-paragraph functions
|
||||
$text = preg_replace('#(?:[\n]{0,2})"""(.*)"""(?:[\n]{0,2})#s', "\n\n%BLOCKQUOTE%\n\n\\1\n\n%ENDBLOCKQUOTE%\n\n", $text);
|
||||
$text = preg_replace('#(?:[\n]{0,2})===(.*)===(?:[\n]{0,2})#s', "\n\n%CENTER%\n\n\\1\n\n%ENDCENTER%\n\n", $text);
|
||||
|
@ -103,16 +106,21 @@ class Flay extends Object
|
|||
|
||||
// split into paragraphs and parse
|
||||
$out = '';
|
||||
foreach (split('%PARAGRAPH%', $text) as $line) {
|
||||
foreach (split('%PARAGRAPH%', $text) as $line)
|
||||
{
|
||||
|
||||
if ($line) {
|
||||
if ($line)
|
||||
{
|
||||
|
||||
if (!$bare) {
|
||||
if (!$bare)
|
||||
{
|
||||
// pre-parse links
|
||||
$links = array();
|
||||
$regs = null;
|
||||
if (preg_match_all('#\[([^\[]{4,})\]#', $line, $regs)) {
|
||||
foreach ($regs[1] as $reg) {
|
||||
if (preg_match_all('#\[([^\[]{4,})\]#', $line, $regs))
|
||||
{
|
||||
foreach ($regs[1] as $reg)
|
||||
{
|
||||
$links[] = $reg;
|
||||
$line = str_replace("[{$reg}]",'%LINK'.(count($links)-1).'%', $line);
|
||||
}
|
||||
|
@ -134,13 +142,16 @@ class Flay extends Object
|
|||
|
||||
// guess e-mails
|
||||
$emails = null;
|
||||
if (preg_match_all("#([_A-Za-z0-9+-+]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#", $line, $emails)) {
|
||||
foreach ($emails[1] as $email) {
|
||||
if (preg_match_all("#([_A-Za-z0-9+-+]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#", $line, $emails))
|
||||
{
|
||||
foreach ($emails[1] as $email)
|
||||
{
|
||||
$line = str_replace($email, "<a href=\"mailto:{$email}\">{$email}</a>", $line);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$bare) {
|
||||
if (!$bare)
|
||||
{
|
||||
// guess links
|
||||
$urls = null;
|
||||
if (preg_match_all("#((?:http|https|ftp|nntp)://[^ ]+)#", $line, $urls))
|
||||
|
@ -159,10 +170,10 @@ class Flay extends Object
|
|||
}
|
||||
|
||||
// re-parse links
|
||||
if (count($links)) {
|
||||
|
||||
for ($ii=0; $ii<count($links); $ii++) {
|
||||
|
||||
if (count($links))
|
||||
{
|
||||
for ($ii=0; $ii<count($links); $ii++)
|
||||
{
|
||||
if (preg_match("#^(http|https|ftp|nntp)://#", $links[$ii]))
|
||||
{
|
||||
$prefix = null;
|
||||
|
@ -190,7 +201,8 @@ class Flay extends Object
|
|||
{
|
||||
$body = $links[$ii];
|
||||
}
|
||||
$with = "<a href=\"{$prefix}{$regs[1]}\" target=\"_blank\">{$body}</a>";
|
||||
|
||||
$with = "<a href=\"{$prefix}{$regs[1]}\" target=\"_blank\">{$body}</a>";
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -207,7 +219,8 @@ class Flay extends Object
|
|||
}
|
||||
}
|
||||
|
||||
if (!$bare) {
|
||||
if (!$bare)
|
||||
{
|
||||
// re-parse multilines
|
||||
$out = str_replace('<p>%BLOCKQUOTE%</p>', "<blockquote>", $out);
|
||||
$out = str_replace('<p>%ENDBLOCKQUOTE%</p>', "</blockquote>", $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(' <b>...</b> ', $snips);
|
||||
$snips = $joined? "<b>...</b> {$joined} <b>...</b>": substr($string, 0, 80).'<b>...</b>';
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
224
libs/model.php
224
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;
|
||||
|
|
|
@ -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<sizeof($this->value); $ii++) {
|
||||
if ($this->value[$ii][$parentIdKey] == $root) {
|
||||
for ($ii=0; $ii<sizeof($this->value); $ii++)
|
||||
{
|
||||
if ($this->value[$ii][$parentIdKey] == $root)
|
||||
{
|
||||
$tmp = $this->value[$ii];
|
||||
$tmp[$childrenKey] = isset($this->value[$ii][$idKey])?
|
||||
$this->threaded($this->value[$ii][$idKey], $idKey, $parentIdKey, $childrenKey):
|
||||
|
|
|
@ -67,12 +67,16 @@ class Object
|
|||
/**
|
||||
* Class constructor, overridden in descendant classes.
|
||||
*/
|
||||
function __construct() {}
|
||||
function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Class destructor, overridden in descendant classes.
|
||||
*/
|
||||
function __destruct() {}
|
||||
function __destruct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Object-to-string conversion.
|
||||
|
|
|
@ -57,38 +57,48 @@ class Router extends Object {
|
|||
* @see routes
|
||||
* @return array Array of routes
|
||||
*/
|
||||
function connect ($route, $default=null) {
|
||||
function connect ($route, $default=null)
|
||||
{
|
||||
$parsed = $names = array ();
|
||||
|
||||
$r = null;
|
||||
if (($route == '') || ($route == '/')) {
|
||||
if (($route == '') || ($route == '/'))
|
||||
{
|
||||
$regexp = '/^[\/]*$/';
|
||||
$this->routes[] = array($route, $regexp, array(), $default);
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
$elements = array();
|
||||
foreach (explode('/', $route) as $element)
|
||||
{
|
||||
if (trim($element)) $elements[] = $element;
|
||||
|
||||
if (!count($elements))
|
||||
return false;
|
||||
|
||||
foreach ($elements as $element) {
|
||||
if (preg_match('/^:(.+)$/', $element, $r)) {
|
||||
$parsed[] = '(?:\/([^\/]+))?';
|
||||
$names[] = $r[1];
|
||||
}
|
||||
elseif (preg_match('/^\*$/', $element, $r)) {
|
||||
$parsed[] = '(?:\/(.*))?';
|
||||
}
|
||||
else {
|
||||
$parsed[] = '/'.$element;
|
||||
}
|
||||
}
|
||||
$regexp = '#^'.join('', $parsed).'[\/]*$#';
|
||||
$this->routes[] = array($route, $regexp, $names, $default);
|
||||
}
|
||||
|
||||
if (!count($elements))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($elements as $element)
|
||||
{
|
||||
if (preg_match('/^:(.+)$/', $element, $r))
|
||||
{
|
||||
$parsed[] = '(?:\/([^\/]+))?';
|
||||
$names[] = $r[1];
|
||||
}
|
||||
elseif (preg_match('/^\*$/', $element, $r))
|
||||
{
|
||||
$parsed[] = '(?:\/(.*))?';
|
||||
}
|
||||
else
|
||||
{
|
||||
$parsed[] = '/'.$element;
|
||||
}
|
||||
}
|
||||
$regexp = '#^'.join('', $parsed).'[\/]*$#';
|
||||
$this->routes[] = array($route, $regexp, $names, $default);
|
||||
}
|
||||
return $this->routes;
|
||||
}
|
||||
|
||||
|
@ -117,9 +127,19 @@ class Router extends Object {
|
|||
array('controller', 'action'),
|
||||
array()
|
||||
);
|
||||
|
||||
$admin_route = array
|
||||
(
|
||||
'/:controller/:admin/:action/* (default)',
|
||||
"#^(?:\/(?:([a-z0-9_\-]+)(?:\/(admin)(?:\/([a-z0-9_\-]+)(?:\/(.*))?)?)?))[\/]*$#",
|
||||
array('controller', 'admin', 'action'),
|
||||
array()
|
||||
);
|
||||
|
||||
|
||||
$this->routes[] = $admin_route;
|
||||
$this->routes[] = $default_route;
|
||||
|
||||
|
||||
foreach ($this->routes as $route)
|
||||
{
|
||||
list($route, $regexp, $names, $defaults) = $route;
|
||||
|
@ -141,13 +161,18 @@ class Router extends Object {
|
|||
foreach ($defaults as $name=>$value)
|
||||
{
|
||||
if (preg_match('#[a-z_\-]#i', $name))
|
||||
{
|
||||
$out[$name] = $value;
|
||||
}
|
||||
else
|
||||
{
|
||||
$out['pass'][] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($r as $found) {
|
||||
foreach ($r as $found)
|
||||
{
|
||||
// if $found is a named url element (i.e. ':action')
|
||||
if (isset($names[$ii]))
|
||||
{
|
||||
|
|
|
@ -40,7 +40,8 @@ uses ('object');
|
|||
* @since CakePHP v 0.2.9
|
||||
*
|
||||
*/
|
||||
class Time extends Object {
|
||||
class Time extends Object
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns a formatted date string for given Datetime string.
|
||||
|
@ -48,7 +49,8 @@ class Time extends Object {
|
|||
* @param string $date_string Datetime string
|
||||
* @return string Formatted date string
|
||||
*/
|
||||
function nice ($date_string=null) {
|
||||
function nice ($date_string=null)
|
||||
{
|
||||
$date = $date_string? strtotime($date_string): time();
|
||||
return date("D, M jS Y, H:i", $date);
|
||||
}
|
||||
|
@ -63,17 +65,24 @@ class Time extends Object {
|
|||
* @param string $date_string Datetime string
|
||||
* @return string Described, relative date string
|
||||
*/
|
||||
function niceShort ($date_string=null) {
|
||||
function niceShort ($date_string=null)
|
||||
{
|
||||
$date = $date_string? Time::fromString($date_string): time();
|
||||
|
||||
$y = Time::isThisYear($date)? '': ' Y';
|
||||
|
||||
if (Time::isToday($date))
|
||||
{
|
||||
return "Today, ".date("H:i", $date);
|
||||
}
|
||||
elseif (Time::wasYesterday($date))
|
||||
{
|
||||
return "Yesterday, ".date("H:i", $date);
|
||||
}
|
||||
else
|
||||
{
|
||||
return date("M jS{$y}, H:i", $date);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -82,7 +91,8 @@ class Time extends Object {
|
|||
* @param string $date Datetime string
|
||||
* @return boolean True if datetime string is today
|
||||
*/
|
||||
function isToday ($date) {
|
||||
function isToday ($date)
|
||||
{
|
||||
return date('Y-m-d', $date) == date('Y-m-d', time());
|
||||
}
|
||||
|
||||
|
@ -105,7 +115,8 @@ class Time extends Object {
|
|||
* @param string $date Datetime string
|
||||
* @return boolean True if datetime string is within current year
|
||||
*/
|
||||
function isThisYear ($date) {
|
||||
function isThisYear ($date)
|
||||
{
|
||||
return date('Y', $date) == date('Y', time());
|
||||
}
|
||||
|
||||
|
@ -115,7 +126,8 @@ class Time extends Object {
|
|||
* @param string $date Datetime string
|
||||
* @return boolean True if datetime string was yesterday
|
||||
*/
|
||||
function wasYesterday ($date) {
|
||||
function wasYesterday ($date)
|
||||
{
|
||||
return date('Y-m-d', $date) == date('Y-m-d', strtotime('yesterday'));
|
||||
}
|
||||
|
||||
|
@ -125,7 +137,8 @@ class Time extends Object {
|
|||
* @param string $date_string Datetime string to be represented as a Unix timestamp
|
||||
* @return int Unix timestamp
|
||||
*/
|
||||
function fromString ($date_string) {
|
||||
function fromString ($date_string)
|
||||
{
|
||||
return strtotime($date_string);
|
||||
}
|
||||
|
||||
|
@ -135,7 +148,8 @@ class Time extends Object {
|
|||
* @param string $date Datetime string
|
||||
* @return string Formatted date string
|
||||
*/
|
||||
function toAtom ($date) {
|
||||
function toAtom ($date)
|
||||
{
|
||||
return date('Y-m-d\TH:i:s\Z', $date);
|
||||
}
|
||||
|
||||
|
@ -145,7 +159,8 @@ class Time extends Object {
|
|||
* @param datetime $date Datetime string
|
||||
* @return string Formatted date string
|
||||
*/
|
||||
function toRSS ($date) {
|
||||
function toRSS ($date)
|
||||
{
|
||||
return date('D, d M Y H:i:s O', $date);
|
||||
}
|
||||
|
||||
|
@ -183,27 +198,39 @@ class Time extends Object {
|
|||
$diff -= $minutes*60;
|
||||
$seconds = $diff;
|
||||
|
||||
if ($months>0) {
|
||||
if ($months>0)
|
||||
{
|
||||
// over a month old, just show date (mm/dd/yyyy format)
|
||||
return 'on '.date("j/n/Y", $in_seconds);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
$relative_date='';
|
||||
if ($weeks>0) {
|
||||
if ($weeks>0)
|
||||
{
|
||||
// weeks and days
|
||||
$relative_date .= ($relative_date?', ':'').$weeks.' week'.($weeks>1?'s':'');
|
||||
$relative_date .= $days>0?($relative_date?', ':'').$days.' day'.($days>1?'s':''):'';
|
||||
} elseif ($days>0) {
|
||||
}
|
||||
elseif ($days>0)
|
||||
{
|
||||
// days and hours
|
||||
$relative_date .= ($relative_date?', ':'').$days.' day'.($days>1?'s':'');
|
||||
$relative_date .= $hours>0?($relative_date?', ':'').$hours.' hour'.($hours>1?'s':''):'';
|
||||
} elseif ($hours>0) {
|
||||
}
|
||||
elseif ($hours>0)
|
||||
{
|
||||
// hours and minutes
|
||||
$relative_date .= ($relative_date?', ':'').$hours.' hour'.($hours>1?'s':'');
|
||||
$relative_date .= $minutes>0?($relative_date?', ':'').$minutes.' minute'.($minutes>1?'s':''):'';
|
||||
} elseif ($minutes>0) {
|
||||
}
|
||||
elseif ($minutes>0)
|
||||
{
|
||||
// minutes only
|
||||
$relative_date .= ($relative_date?', ':'').$minutes.' minute'.($minutes>1?'s':'');
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
// seconds only
|
||||
$relative_date .= ($relative_date?', ':'').$seconds.' second'.($seconds>1?'s':'');
|
||||
}
|
||||
|
|
|
@ -87,7 +87,7 @@ if (class_exists('DATABASE_CONFIG'))
|
|||
}
|
||||
|
||||
//RUN THE SCRIPT
|
||||
if($url === 'favicon.ico')
|
||||
if(isset($_GET['url']) && $_GET['url'] === 'favicon.ico')
|
||||
{
|
||||
}else{
|
||||
$DISPATCHER = new Dispatcher ();
|
||||
|
|
Loading…
Reference in a new issue