From fc0854e3979b4d624e42a015f3802410e2b333f3 Mon Sep 17 00:00:00 2001 From: phpnut Date: Tue, 14 Mar 2006 02:26:08 +0000 Subject: [PATCH] Merging fixes and enhancements into trunk. Revision: [2287] Merging changes from model_php4.php Revision: [2286] Added serialized object data to the cached file. Instances of the view helpers are available in the views now. You also have access to the Controller::, example $this->controller->Session; Revision: [2285] Adding Controller::postConditions() to convert a POST'ed data array to a Model conditions array Revision: [2284] Adding Model::invalidate() and refactoring Model::invalidFields(). Adding a fix for RequestHandler::accepts() git-svn-id: https://svn.cakephp.org/repo/trunk/cake@2288 3807eeeb-6ff5-0310-8944-8be069107fe0 --- VERSION.txt | 2 +- cake/bootstrap.php | 26 ++----- cake/dispatcher.php | 1 + .../controller/components/request_handler.php | 6 ++ cake/libs/controller/controller.php | 34 +++++++-- cake/libs/model/model_php4.php | 75 ++++++++++--------- cake/libs/model/model_php5.php | 75 ++++++++++--------- cake/libs/view/helpers/cache.php | 20 ++++- cake/libs/view/view.php | 32 +++++++- 9 files changed, 173 insertions(+), 98 deletions(-) diff --git a/VERSION.txt b/VERSION.txt index f39d66361..91eaf1671 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -6,4 +6,4 @@ // +---------------------------------------------------------------------------------------------------+ // /////////////////////////////////////////////////////////////////////////////////////////////////////////// -1.0.0.2283 \ No newline at end of file +1.0.0.2288 \ No newline at end of file diff --git a/cake/bootstrap.php b/cake/bootstrap.php index 32479337a..efff13088 100644 --- a/cake/bootstrap.php +++ b/cake/bootstrap.php @@ -107,6 +107,8 @@ else } $TIME_START = getMicrotime(); + +require CAKE.'dispatcher.php'; if(defined('CACHE_CHECK') && CACHE_CHECK === true) { if (empty($uri)) @@ -118,29 +120,11 @@ if(defined('CACHE_CHECK') && CACHE_CHECK === true) if (file_exists($filename)) { - ob_start(); - include($filename); - if (DEBUG) - { - echo ""; - } - $out = ob_get_clean(); - if (preg_match('/^/', $out, $match)) - { - if(time() >= $match['1']) - { - @unlink($filename); - unset($out); - } - else - { - die(e($out)); - } - } + uses(DS.'controller'.DS.'component', DS.'view'.DS.'view'); + $view = new View(); + $view->renderCache($filename, $TIME_START); } } - -require CAKE.'dispatcher.php'; require LIBS.'model'.DS.'connection_manager.php'; config('database'); diff --git a/cake/dispatcher.php b/cake/dispatcher.php index 6be01946f..fcb0e18e9 100644 --- a/cake/dispatcher.php +++ b/cake/dispatcher.php @@ -213,6 +213,7 @@ class Dispatcher extends Object { array_push($controller->components, $controller->webservices); array_push($controller->helpers, $controller->webservices); + $component =& new Component($controller); } if((in_array('scaffold', array_keys($classVars))) && ($missingAction === true)) diff --git a/cake/libs/controller/components/request_handler.php b/cake/libs/controller/components/request_handler.php index 64fa4fec6..21c664bd4 100644 --- a/cake/libs/controller/components/request_handler.php +++ b/cake/libs/controller/components/request_handler.php @@ -388,6 +388,12 @@ class RequestHandlerComponent extends Object } else if (is_string($type)) { + // If client only accepts */*, then assume default HTML browser + if ($type == 'html' && $this->__acceptTypes === array('*/*')) + { + return true; + } + if (!in_array($type, array_keys($this->__requestContent))) { return false; diff --git a/cake/libs/controller/controller.php b/cake/libs/controller/controller.php index 81b3a1122..a6808c0ae 100644 --- a/cake/libs/controller/controller.php +++ b/cake/libs/controller/controller.php @@ -241,6 +241,10 @@ class Controller extends Object } } } + if (!empty($this->components)) + { + $component =& new Component($this); + } parent::__construct(); } @@ -251,11 +255,6 @@ class Controller extends Object */ function constructClasses() { - if (!empty($this->components)) - { - $component =& new Component($this); - } - if(empty($this->params['pass'])) { $id = false; @@ -840,6 +839,31 @@ class Controller extends Object return $fieldNames; } +/** + * Converts POST'ed model data to a model conditions array, suitable for a find + * or findAll Model query + * + * @param array $data POST'ed data organized by model and field + * @return array An array of model conditions + */ + function postConditions ($data) + { + if (!is_array($data) || empty($data)) + { + return null; + } + + $conditions = array(); + foreach ($data as $model => $fields) + { + foreach ($fields as $field => $value) + { + $conditions[$model.'.'.$field] = $value; + } + } + return $conditions; + } + /** * Cleans up the date fields of current Model. * diff --git a/cake/libs/model/model_php4.php b/cake/libs/model/model_php4.php index 1cfa7dddd..02303f9e4 100644 --- a/cake/libs/model/model_php4.php +++ b/cake/libs/model/model_php4.php @@ -1399,45 +1399,52 @@ class Model extends Object * @param array $data * @return array Array of invalid fields */ - function invalidFields ($data=null) + function invalidFields ($data = array()) { - if (!isset($this->validate) || is_array($this->validationErrors)) - { - if (!isset($this->validate)) - { - return true; - } - else - { - return $this->validationErrors; - } - } + if (!isset($this->validate)) + { + return true; + } - if ($data == null) - { - if (isset($this->data)) - { - $data = $this->data; - } - else - { - $data = array(); - } - } + if (is_array($this->validationErrors)) + { + return $this->validationErrors; + } - $errors = array(); - foreach ($data as $table => $field) - { - foreach ($this->validate as $field_name => $validator) - { - if (isset($data[$table][$field_name]) && !preg_match($validator, $data[$table][$field_name])) + if (empty($data) && isset($this->data)) + { + $data = $this->data; + } + + if (isset($data[$this->name])) + { + $data = $data[$this->name]; + } + + $this->validationErrors = array(); + foreach ($this->validate as $field_name => $validator) + { + if (isset($data[$field_name]) && !preg_match($validator, $data[$field_name])) { - $errors[$field_name] = 1; + $this->validationErrors[$field_name] = 1; } - } - $this->validationErrors = $errors; - return $errors; - } + } + return $this->validationErrors; + } + +/** + * Sets a field as invalid + * + * @param string $field The name of the field to invalidate + * @return void + */ + function invalidate ($field) + { + if (!is_array($this->validationErrors)) + { + $this->validationErrors = array(); + } + $this->validationErrors[$field] = 1; } /** diff --git a/cake/libs/model/model_php5.php b/cake/libs/model/model_php5.php index 4a74926d8..240819193 100644 --- a/cake/libs/model/model_php5.php +++ b/cake/libs/model/model_php5.php @@ -1395,45 +1395,52 @@ class Model extends Object * @param array $data * @return array Array of invalid fields */ - function invalidFields ($data=null) + function invalidFields ($data = array()) { - if (!isset($this->validate) || is_array($this->validationErrors)) - { - if (!isset($this->validate)) - { - return true; - } - else - { - return $this->validationErrors; - } - } + if (!isset($this->validate)) + { + return true; + } - if ($data == null) - { - if (isset($this->data)) - { - $data = $this->data; - } - else - { - $data = array(); - } - } + if (is_array($this->validationErrors)) + { + return $this->validationErrors; + } - $errors = array(); - foreach ($data as $table => $field) - { - foreach ($this->validate as $field_name => $validator) - { - if (isset($data[$table][$field_name]) && !preg_match($validator, $data[$table][$field_name])) + if (empty($data) && isset($this->data)) + { + $data = $this->data; + } + + if (isset($data[$this->name])) + { + $data = $data[$this->name]; + } + + $this->validationErrors = array(); + foreach ($this->validate as $field_name => $validator) + { + if (isset($data[$field_name]) && !preg_match($validator, $data[$field_name])) { - $errors[$field_name] = 1; + $this->validationErrors[$field_name] = 1; } - } - $this->validationErrors = $errors; - return $errors; - } + } + return $this->validationErrors; + } + +/** + * Sets a field as invalid + * + * @param string $field The name of the field to invalidate + * @return void + */ + function invalidate ($field) + { + if (!is_array($this->validationErrors)) + { + $this->validationErrors = array(); + } + $this->validationErrors[$field] = 1; } /** diff --git a/cake/libs/view/helpers/cache.php b/cake/libs/view/helpers/cache.php index af9f58e3c..9e8acfb34 100644 --- a/cake/libs/view/helpers/cache.php +++ b/cake/libs/view/helpers/cache.php @@ -43,6 +43,7 @@ class CacheHelper extends Helper { var $replace = array(); var $match = array(); + var $view; function cache($file, $out, $cache = false) { @@ -106,7 +107,15 @@ class CacheHelper extends Helper function __parseFile($file, $cache) { - $file = file_get_contents($file); + if(is_file($file)) + { + $file = file_get_contents($file); + } + else if($file = fileExistsInPath($file)) + { + $file = file_get_contents($file); + } + preg_match_all('/(?P(?:.*|(?:[\\S\\s]*[^\\S]))<\/cake:nocache>)/i', $cache, $oresult, PREG_PATTERN_ORDER); preg_match_all('/(?P(?:.*|(?:[\\S\\s]*[^\\S])))<\/cake:nocache>/i', $file, $result, PREG_PATTERN_ORDER); @@ -151,7 +160,14 @@ class CacheHelper extends Helper $result = preg_replace('/\/\//', '/', $this->here); $cache = str_replace('/', '_', $result.'.php'); $cache = str_replace('favicon.ico', '', $cache); - $file = ''.$file; + $file = ''. + 'view->name.'\'); ?>'. + 'controller = new '.$this->view->name.'Controller(); ?>'. + 'helpers = unserialize(\''. serialize($this->view->helpers).'\'); ?>'. + 'webroot = \''. $this->view->webroot.'\'; ?>'. + 'data = unserialize(\''. serialize($this->view->data).'\'); ?>'. + ''. + '_loadHelpers($loaded, $this->helpers); ?>'.$file; return cache('views'.DS.$cache, $file, $timestamp); } } diff --git a/cake/libs/view/view.php b/cake/libs/view/view.php index bc1b0839e..7b0b86bd9 100644 --- a/cake/libs/view/view.php +++ b/cake/libs/view/view.php @@ -214,7 +214,7 @@ class View extends Object * * @return View */ - function __construct (&$controller) + function __construct (&$controller = null) { if ($controller != null) { @@ -664,8 +664,13 @@ class View extends Object if (is_a($this->loaded['cache'], 'CacheHelper')) { $cache =& $this->loaded['cache']; + if($cached === true) + { + $cache->view =& $this; + } $cache->base = $this->base; $cache->here = $this->here; + $cache->helpers = $this->loaded; $cache->controllerName = $this->params['controller']; $cache->cacheAction = $this->controller->cacheAction; $cache->cache($___viewFn, $out, $cached); @@ -783,5 +788,30 @@ class View extends Object 'file' => $viewFileName))); } } + + function renderCache($filename, $time) + { + ob_start(); + include($filename); + if (DEBUG) + { + echo ""; + } + $out = ob_get_clean(); + if (preg_match('/^/', $out, $match)) + { + if(time() >= $match['1']) + { + @unlink($filename); + unset($out); + return; + } + else + { + e($out); + die(); + } + } + } } ?> \ No newline at end of file