cakephp2-php8/cake/libs/controller/controller.php

942 lines
28 KiB
PHP
Raw Normal View History

<?php
/* SVN FILE: $Id$ */
/**
* Base controller class.
*
* PHP versions 4 and 5
*
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
* Copyright (c) 2006, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright (c) 2006, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
* @package cake
* @subpackage cake.cake.libs.controller
* @since CakePHP v 0.2.9
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Include files
*/
uses(DS . 'controller' . DS . 'component', DS . 'view' . DS . 'view');
/**
* Controller
*
* Application controller (controllers are where you put all the actual code)
* Provides basic functionality, such as rendering views (aka displaying templates).
* Automatically selects model name from on singularized object class name
* and creates the model object if proper class exists.
*
* @package cake
* @subpackage cake.cake.libs.controller
*
*/
class Controller extends Object{
/**
* Name of the controller.
*
* @var unknown_type
* @access public
*/
var $name = null;
/**
* Stores the current URL (for links etc.)
*
* @var string Current URL
*/
var $here = null;
/**
* The webroot of the application
*
* @var string
*/
var $webroot = null;
/**
* Action to be performed.
*
* @var string
* @access public
*/
var $action = null;
/**
* An array of names of models the particular controller wants to use.
*
* @var mixed A single name as a string or a list of names as an array.
* @access protected
*/
var $uses = false;
/**
* An array of names of built-in helpers to include.
*
* @var mixed A single name as a string or a list of names as an array.
* @access protected
*/
var $helpers = array('Html');
/**
* Parameters received in the current request, i.e. GET and POST data
*
* @var array
* @access public
*/
var $params = array();
/**
* POST'ed model data
*
* @var array
* @access public
*/
var $data = array();
/**
* Enter description here...
*
* @var unknown_type
*/
var $viewPath = null;
/**
* Variables for the view
*
* @var array
* @access private
*/
var $_viewVars = array();
/**
* Web page title
*
* @var boolean
* @access private
*/
var $pageTitle = false;
/**
* An array of model objects.
*
* @var array Array of model objects.
* @access public
*/
var $modelNames = array();
/**
* Enter description here...
*
* @var unknown_type
* @access public
*/
var $base = null;
/**
* Layout file to use (see /app/views/layouts/default.thtml)
*
* @var string
* @access public
*/
var $layout = 'default';
/**
* Automatically render the view (the dispatcher checks for this variable before running render())
*
* @var boolean
* @access public
*/
var $autoRender = true;
/**
* Enter description here...
*
* @var boolean
* @access public
*/
var $autoLayout = true;
/**
* Enter description here...
*
* @var string
* @access public
*/
var $beforeFilter = null;
/**
* Enter description here...
*
* @var unknown_type
*/
var $components = array();
/**
* Enter description here...
*
* @var unknown_type
*/
var $view = 'View';
/**
* Enter description here...
*
* @var unknown_type
*/
var $_viewClass = null;
/**
* The output of the requested action. Contains either a variable
* returned from the action, or the data of the rendered view;
*
* @var unknown_type
*/
var $output = null;
/**
* Enter description here...
*
* @var unknown_type
*/
var $plugin = null;
Merging fixes and enhancements into trunk. Changing version number to 1.x.x.x, 0.10.x.x code base had been changed to the version number 1.x.x.x, and what was planned for 1.x.x.x code has now been moved to 2.x.x.x, and 2.x.x.x moved to 3.x.x.x. This will give us easier to track version numbers from now on. Revision: [2248] Merging changes from model_php5.php Revision: [2247] "Removing test code from view class" Revision: [2246] Removed cache time define from core.php. Modified the __() function in basics.php to echo string like it will in later versions of cake with translations. Refactored the cache checking in bootstrap.php to read the files embedded time stamp and delete or output the cached version. Added View::cacheView() for caching pages. Revision: [2245] Moving column formatting from DBO to Sanitize Revision: [2244] Adding beforeValidate() Model callback, and allowing query data to be modified in beforeFind() Revision: [2243] "Adding caching changes to Controller class " Revision: [2242] "Added check to delete cached version if it has expired" Revision: [2241] Adding app/cache/views directory Revision: [2240] "Fixed missing variable" Revision: [2239] "Adding full page caching to view class." Revision: [2238] "Adding defines for caching" Revision: [2237] "Adding caching check too bootstrap.php" Revision: [2236] Adding ClassRegistry::removeObject from Ticket #477 Revision: [2235] "Correcting setting in DATABASE_CONFIG class" Revision: [2231] Adding convenience function am(), which allows merging an infinite number of arrays merged into one Revision: [2207] Change Model::save() to call beforeSave() before validations Revision: [2199] Removing conditions method call in Model::field() Revision: [2196] Setting proper mime type again git-svn-id: https://svn.cakephp.org/repo/trunk/cake@2250 3807eeeb-6ff5-0310-8944-8be069107fe0
2006-03-12 00:11:40 +00:00
/**
* Enter description here...
*
* @var unknown_type
*/
var $cacheAction = false;
Merging fixes and enhancements into trunk. Revision: [2369] Added missing doc comments to classes Revision: [2368] Removing core view paths from Configure class Revision: [2367] Updating View class to use new Configure class to search paths for view files Revision:[ 2366] Corrected loadControllers() in basics.php Revision: [2365] Updating function in basics.php to use new Configure class Revision: [2364] removed duplicate loading of app/config/bootstrap.php Revision: [2363] Added new Configure class to hold paths to models, views, and controllers related to Ticket #470. Moved loading of app/config/bootstrap.php to Configure class from app/webroot/index.php. Added creating instance of Configure instance in cake/bootstrap.php Added example of setting custom paths in app/config/bootstrap.php Corrected error in Object::_savePersistent(). Revision: [2362] Added fix for Ticket #534 Revision: [2361] Refactoring persistent code a little more. File now holds a variable with the serialized class and is include vs. reading file contents. Revision: [2360] Refactored persistent methods to use cache() Revision: [2359] Fixing array_combine() Warning in Model::generateList() Revision: [2358] Set var $persistModel to false by default Revision: [2357] Moved persistent code to Object class. Moved $TIME_START variable to top of file. added __sleep methods for models Revision: [2356] Reverting persistent changes to Model class Revision: [2355] Adding fix for Ticket #550 Revision: [2354] Corrected errors in persistent code Revision: [2353] Corrected overwrite in the model/model_php4.php file. Revision: [2352] Adding persistent methods to Model class. This will allow caching of classes. Added app/tmp/persistent directory. Revision: [2351] Reverting changes to dbo_source.php in [2350] Revision: [2350] Removed name pattern matches related to Ticket #534 Revision: [2349] Adding fix for Ticket #548 Revision: [2348] Adding fixes from Ticket #547. Revision: [2347] Adding fixes from Ticket #546. Revision: [2346] Adding fix for Ticket #527 Revision: [2345] Refactored Html::url() Revision: [2344] Last fix for Ticket #483 Revision: [2343] Updating last commit Revision: [2342] Adding fix for Ticket #483 Revision: [2341] Adding fix for Ticket #543, DBO will now only cache SELECT queries Revision: [2340] Adding session_write_close() to the CakeSession::__regenerateId() Revision: [2339] Adding patch from Ticket #544 Revision: [2338] Adding patch from Ticket #529 Revision: [2337] Adding patch from Ticket #528 Revision: [2336] Removing the converting of \n to <br /> in Sanitize::html() Revision: [2335] Added bash script from Ticket #533 Revision: [2334] Added enhancement for Ticket #474 Revision: [2333] Correcting errors introduced with prior fix Revision: [2332] Correcting errors introduced with prior fix Revision: [2331] Performance optimization for NeatArray::findIn(): replaced foreach loop with for Revision: [2330] Minor performance optimization: Replacing all occurrences of low() with strtolower() git-svn-id: https://svn.cakephp.org/repo/trunk/cake@2370 3807eeeb-6ff5-0310-8944-8be069107fe0
2006-03-19 03:26:43 +00:00
/**
* Enter description here...
*
* @var boolean
*/
var $persistModel = false;
/**
* Enter description here...
*
* @var unknown_type
*/
var $webservices = null;
/**
* Enter description here...
*
* @var mixed
*/
var $namedArgs = false;
/**
* Constructor.
*
*/
function __construct() {
if ($this->name === null) {
$r = null;
if (!preg_match('/(.*)Controller/i', get_class($this), $r)) {
die ("Controller::__construct() : Can't get or parse my own class name, exiting.");
}
$this->name = $r[1];
}
if ($this->viewPath == null) {
$this->viewPath = Inflector::underscore($this->name);
}
$this->modelClass = ucwords(Inflector::singularize($this->name));
$this->modelKey = Inflector::underscore($this->modelClass);
if (!defined('AUTO_SESSION') || AUTO_SESSION == true) {
$this->components[] = 'Session';
}
if (is_subclass_of($this, 'AppController')) {
$appVars = get_class_vars('AppController');
foreach(array('components', 'helpers', 'uses') as $var) {
if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
$diff = array_diff($appVars[$var], $this->{$var});
$this->{$var} = array_merge($this->{$var}, $diff);
}
}
}
if (!empty($this->components)) {
$component = new Component();
$component->init($this);
}
parent::__construct();
}
/**
* Loads and instantiates classes required by this controller,
* including components and models
*
*/
function constructClasses() {
if (empty($this->params['pass'])) {
$id = false;
} else {
$id = $this->params['pass'];
}
$cached = false;
$object = null;
if (class_exists($this->modelClass) && ($this->uses === false)) {
if ($this->persistModel === true) {
$cached = $this->_persist($this->modelClass, null, $object);
}
if (($cached === false)) {
$model =& new $this->modelClass($id);
$this->modelNames[] = $this->modelClass;
$this->{$this->modelClass} =& $model;
if ($this->persistModel === true) {
$this->_persist($this->modelClass, true, $model);
$registry = ClassRegistry::getInstance();
$this->_persist($this->modelClass . 'registry', true, $registry->_objects, 'registry');
}
} else {
$this->_persist($this->modelClass . 'registry', true, $object, 'registry');
$this->_persist($this->modelClass, true, $object);
$this->modelNames[] = $this->modelClass;
return true;
}
} elseif ($this->uses === false) {
return $this->cakeError('missingModel', array(array('className' => $this->modelClass, 'webroot' => '', 'base' => $this->base)));
}
if ($this->uses) {
$uses = is_array($this->uses) ? $this->uses : array($this->uses);
foreach($uses as $modelClass) {
$cached = false;
$object = null;
$modelKey = Inflector::underscore($modelClass);
if (class_exists($modelClass)) {
if ($this->persistModel === true) {
$cached = $this->_persist($modelClass, null, $object);
}
if (($cached === false)) {
$model =& new $modelClass($id);
$this->modelNames[] = $modelClass;
$this->{$modelClass} =& $model;
if ($this->persistModel === true) {
$this->_persist($modelClass, true, $model);
$registry = ClassRegistry::getInstance();
$this->_persist($modelClass . 'registry', true, $registry->_objects, 'registry');
}
} else {
$this->_persist($modelClass . 'registry', true, $object, 'registry');
$this->_persist($modelClass, true, $object);
$this->modelNames[] = $modelClass;
return true;
}
} else {
return $this->cakeError('missingModel', array(array('className' => $modelClass, 'webroot' => '', 'base' => $this->base)));
}
}
}
}
/**
* Redirects to given $url, after turning off $this->autoRender.
*
Merging fixes and enhancements into trunk Revision: [1891] Added patch from Ticket #278 Revision: [1890] Adding patch from Ticket #227 Revision: [1889] Adding fix from Changeset [1631]. This fixes Ticket #319 Revision: [1888] Added fix for Ticket #315 Revision: [1887] Adding patch from Ticket #312 Revision: [1886] Adding fix that was committed in [1304] back. Closing Ticket #77 again Revision: [1885] Fix added for Ticket #306 Added patch from Ticket #318 Added patch from Ticket #322 Revision: [1884] Adding fix to Ticket #332 Revision: [1883] Adding patch from Ticket #330 Revision: [1882] Adding fix for Ticket #170 back to HtmlHelper::selectTag(). Was lost in a previous merge Revision: [1881] Adding fix for Ticket #336 Revision: [1880] Adding fix from Ticket #307 Revision: [1879] Plugins will use their own helpers and components if present Revision: [1878] Basic implementation of plugins within app/plugins working. Revision: [1877] Starting plugin code for multiple apps within one app. Revision: [1876] Added Ticket #345. Revision: [1875] Added check to AcoAction class that would not attempt to load AppModel Class if it is already defined in memory Added fixes for Ticket #317, Ticket #333, Ticket #343, Ticket #337 Revision: [1874] Adding fix for Ticket #340 Revision: [1873] Added themeWeb var to helpers that will be used if a theme class overrides the view class Revision: [1872] Adding $format to timeAgo and relativeTime, for gwoo Revision: [1871] Docstrings changes. One char at a time we map out Cake. Revision: [1870] Docstrings for Session, and corrections to tabbing on datasource. Revision: [1869] Docstrings for the core database classes. Revision: [1868] Adding patch for Ticket #131 Revision: [1867] Allowing ajax link titles to not be escaped Revision: [1866] Changed error class so calls to ErrorHandler::error() in production setting will work. git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1892 3807eeeb-6ff5-0310-8944-8be069107fe0
2006-02-01 13:26:23 +00:00
* @param string $url
* @param integer $status
*/
function redirect($url, $status = null) {
$this->autoRender = false;
$pos = strpos($url, '://');
$base = strip_plugin($this->base, $this->plugin);
if ($pos === false) {
if (strpos($url, '/') !== 0) {
$url = '/' . $url;
}
$url = $base . $url;
}
if (function_exists('session_write_close')) {
session_write_close();
}
if ($status != null) {
$codes = array(
100 => "HTTP/1.1 100 Continue",
101 => "HTTP/1.1 101 Switching Protocols",
200 => "HTTP/1.1 200 OK",
201 => "HTTP/1.1 201 Created",
202 => "HTTP/1.1 202 Accepted",
203 => "HTTP/1.1 203 Non-Authoritative Information",
204 => "HTTP/1.1 204 No Content",
205 => "HTTP/1.1 205 Reset Content",
206 => "HTTP/1.1 206 Partial Content",
300 => "HTTP/1.1 300 Multiple Choices",
301 => "HTTP/1.1 301 Moved Permanently",
302 => "HTTP/1.1 302 Found",
303 => "HTTP/1.1 303 See Other",
304 => "HTTP/1.1 304 Not Modified",
305 => "HTTP/1.1 305 Use Proxy",
307 => "HTTP/1.1 307 Temporary Redirect",
400 => "HTTP/1.1 400 Bad Request",
401 => "HTTP/1.1 401 Unauthorized",
402 => "HTTP/1.1 402 Payment Required",
403 => "HTTP/1.1 403 Forbidden",
404 => "HTTP/1.1 404 Not Found",
405 => "HTTP/1.1 405 Method Not Allowed",
406 => "HTTP/1.1 406 Not Acceptable",
407 => "HTTP/1.1 407 Proxy Authentication Required",
408 => "HTTP/1.1 408 Request Time-out",
409 => "HTTP/1.1 409 Conflict",
410 => "HTTP/1.1 410 Gone",
411 => "HTTP/1.1 411 Length Required",
412 => "HTTP/1.1 412 Precondition Failed",
413 => "HTTP/1.1 413 Request Entity Too Large",
414 => "HTTP/1.1 414 Request-URI Too Large",
415 => "HTTP/1.1 415 Unsupported Media Type",
416 => "HTTP/1.1 416 Requested range not satisfiable",
417 => "HTTP/1.1 417 Expectation Failed",
500 => "HTTP/1.1 500 Internal Server Error",
501 => "HTTP/1.1 501 Not Implemented",
502 => "HTTP/1.1 502 Bad Gateway",
503 => "HTTP/1.1 503 Service Unavailable",
504 => "HTTP/1.1 504 Gateway Time-out"
);
if (isset($codes[$status])) {
header($codes[$status]);
}
}
if (defined('SERVER_IIS')) {
header('Location: ' . FULL_BASE_URL . $url);
} else {
header('Location: ' . $url);
}
}
/**
* Saves a variable to use inside a template.
*
* @param mixed $one A string or an array of data.
* @param mixed $two Value in case $one is a string (which then works as the key). Unused if $one is an associative array, otherwise serves as the values to $one's keys.
* @return unknown
*/
function set($one, $two = null) {
if (is_array($one)) {
if (is_array($two)) {
return $this->_setArray(array_combine($one, $two));
} else {
return $this->_setArray($one);
}
} else {
return $this->_setArray(array($one => $two));
}
}
/**
* Enter description here...
*
* @param unknown_type $action
*/
function setAction($action) {
$this->action = $action;
$args = func_get_args();
unset($args[0]);
call_user_func_array(array(&$this, $action), $args);
}
/**
* Returns number of errors in a submitted FORM.
*
* @return int Number of errors
*/
function validate() {
$args = func_get_args();
$errors = call_user_func_array(array(&$this, 'validateErrors'), $args);
if ($errors === false) {
return 0;
}
return count($errors);
}
/**
* Validates a FORM according to the rules set up in the Model.
*
* @return int Number of errors
*/
function validateErrors() {
$objects = func_get_args();
if (!count($objects)) {
return false;
}
$errors = array();
foreach($objects as $object) {
$errors = array_merge($errors, $this->{$object->name}->invalidFields($object->data));
}
return $this->validationErrors = (count($errors) ? $errors : false);
}
/**
* Gets an instance of the view object & prepares it for rendering the output, then
* asks the view to actualy do the job.
*
* @param unknown_type $action
* @param unknown_type $layout
* @param unknown_type $file
* @return unknown
*/
function render($action = null, $layout = null, $file = null) {
$viewClass = $this->view;
if ($this->view != 'View') {
$viewClass = $this->view . 'View';
loadView($this->view);
}
$this->beforeRender();
$this->_viewClass =& new $viewClass($this);
if (!empty($this->modelNames)) {
foreach($this->modelNames as $model) {
if (!empty($this->{$model}->validationErrors)) {
$this->_viewClass->validationErrors[$model] = &$this->{$model}->validationErrors;
}
}
}
$this->autoRender = false;
return $this->_viewClass->render($action, $layout, $file);
}
/**
* Gets the referring URL of this request
*
* @param string $default Default URL to use if HTTP_REFERER cannot be read from headers
* @param boolean $local If true, restrict referring URLs to local server
* @access public
*/
function referer($default = null, $local = false) {
$ref = env('HTTP_REFERER');
$base = FULL_BASE_URL . $this->webroot;
if ($ref != null && (defined(FULL_BASE_URL) || FULL_BASE_URL)) {
if (strpos($ref, $base) === 0) {
return substr($ref, strlen($base) - 1);
} elseif(!$local) {
return $ref;
}
}
if ($default != null) {
return $default;
} else {
return '/';
}
}
/**
* Sets data for this view. Will set title if the key "title" is in given $data array.
*
* @param array $data Array of
* @access private
*/
function _setArray($data) {
foreach($data as $name => $value) {
if ($name == 'title') {
$this->_setTitle($value);
} else {
$this->_viewVars[$name] = $value;
}
}
}
/**
* Set the title element of the page.
*
* @param string $pageTitle Text for the title
* @access private
*/
function _setTitle($pageTitle) {
$this->pageTitle = $pageTitle;
}
/**
* Shows a message to the user $time seconds, then redirects to $url
* Uses flash.thtml as a layout for the messages
*
* @param string $message Message to display to the user
* @param string $url Relative URL to redirect to after the time expires
* @param int $time Time to show the message
*/
function flash($message, $url, $pause = 1) {
$this->autoRender = false;
$this->autoLayout = false;
$this->set('url', $this->base . $url);
$this->set('message', $message);
$this->set('pause', $pause);
$this->set('page_title', $message);
if (file_exists(VIEWS . 'layouts' . DS . 'flash.thtml')) {
$flash = VIEWS . 'layouts' . DS . 'flash.thtml';
} elseif ($flash = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . "layouts" . DS . 'flash.thtml')) {
}
$this->render(null, false, $flash);
}
/**
* Shows a message to the user $time seconds, then redirects to $url
* Uses flash.thtml as a layout for the messages
*
* @param string $message Message to display to the user
* @param string $url URL to redirect to after the time expires
* @param int $time Time to show the message
*
* @param unknown_type $message
* @param unknown_type $url
* @param unknown_type $time
*/
function flashOut($message, $url, $pause = 1) {
$this->autoRender = false;
$this->autoLayout = false;
$this->set('url', $url);
$this->set('message', $message);
$this->set('pause', $pause);
$this->set('page_title', $message);
if (file_exists(VIEWS . 'layouts' . DS . 'flash.thtml')) {
$flash = VIEWS . 'layouts' . DS . 'flash.thtml';
} elseif($flash = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . "layouts" . DS . 'flash.thtml')) {
}
$this->render(null, false, $flash);
}
/**
* This function creates a $fieldNames array for the view to use.
* @todo Map more database field types to html form fields.
* @todo View the database field types from all the supported databases.
*
*/
function generateFieldNames($data = null, $doCreateOptions = true) {
$fieldNames = array();
$model = $this->modelClass;
$modelKey = $this->modelKey;
$table = $this->{$model}->table;
$association = array_search($table, $this->{$model}->alias);
$objRegistryModel =& ClassRegistry::getObject($modelKey);
foreach($objRegistryModel->_tableInfo as $tables) {
foreach($tables as $tabl) {
if ($objRegistryModel->isForeignKey($tabl['name'])) {
if(false !== strpos($tabl['name'], "_id")) {
$niceName = substr($tabl['name'], 0, strpos($tabl['name'], "_id" ));
} else {
$niceName = $niceName = $tabl['name'];
}
$fkNames = $this->{$model}->keyToTable[$tabl['name']];
$fieldNames[$tabl['name']]['table'] = $fkNames[0];
$association = array_search($fieldNames[$tabl['name']]['table'], $this->{$model}->alias);
$fieldNames[$tabl['name']]['prompt'] = Inflector::humanize($niceName);
$fieldNames[$tabl['name']]['model'] = $fkNames[1];
$fieldNames[$tabl['name']]['modelKey'] = $this->{$model}->tableToModel[$fieldNames[$tabl['name']]['table']];
$fieldNames[$tabl['name']]['controller'] = Inflector::pluralize($this->{$model}->tableToModel[$fkNames[0]]);
$fieldNames[$tabl['name']]['foreignKey'] = true;
} else if('created' != $tabl['name'] && 'updated' != $tabl['name']) {
$fieldNames[$tabl['name']]['prompt'] = Inflector::humanize($tabl['name']);
} else if('created' == $tabl['name']) {
$fieldNames[$tabl['name']]['prompt'] = 'Created';
} else if('updated' == $tabl['name']) {
$fieldNames[$tabl['name']]['prompt'] = 'Modified';
}
$fieldNames[$tabl['name']]['tagName'] = $model . '/' . $tabl['name'];
$validationFields = $objRegistryModel->validate;
if (isset($validationFields[$tabl['name']])) {
if (VALID_NOT_EMPTY == $validationFields[$tabl['name']]) {
$fieldNames[$tabl['name']]['required'] = true;
$fieldNames[$tabl['name']]['errorMsg'] = "Required Field";
}
}
$lParenPos = strpos($tabl['type'], '(');
$rParenPos = strpos($tabl['type'], ')');
if (false != $lParenPos) {
$type = substr($tabl['type'], 0, $lParenPos);
$fieldLength = substr($tabl['type'], $lParenPos + 1, $rParenPos - $lParenPos - 1);
} else {
$type = $tabl['type'];
}
switch($type) {
case "text":
$fieldNames[$tabl['name']]['type'] = 'area';
break;
case "string":
if (isset($fieldNames[$tabl['name']]['foreignKey'])) {
$fieldNames[$tabl['name']]['type'] = 'select';
$fieldNames[$tabl['name']]['options'] = array();
$otherModel =& ClassRegistry::getObject(Inflector::underscore($fieldNames[$tabl['name']]['modelKey']));
if (is_object($otherModel)) {
if ($doCreateOptions) {
$otherDisplayField = $otherModel->getDisplayField();
$otherModel->recursive = 0;
$rec = $otherModel->findAll();
foreach($rec as $pass) {
foreach($pass as $key => $value) {
if ($key == $this->{$model}->tableToModel[$fieldNames[$tabl['name']]['table']] && isset($value['id']) && isset($value[$otherDisplayField])) {
$fieldNames[$tabl['name']]['options'][$value['id']] = $value[$otherDisplayField];
}
}
}
}
$fieldNames[$tabl['name']]['selected'] = $data[$association][$tabl['name']];
}
} else {
$fieldNames[$tabl['name']]['type'] = 'input';
}
break;
case "boolean":
$fieldNames[$tabl['name']]['type'] = 'checkbox';
break;
case "integer":
case "float":
if (strcmp($tabl['name'], $this->$model->primaryKey) == 0) {
$fieldNames[$tabl['name']]['type'] = 'hidden';
} else if(isset($fieldNames[$tabl['name']]['foreignKey'])) {
$fieldNames[$tabl['name']]['type'] = 'select';
$fieldNames[$tabl['name']]['options'] = array();
$otherModel =& ClassRegistry::getObject(Inflector::underscore($fieldNames[$tabl['name']]['modelKey']));
if (is_object($otherModel)) {
if ($doCreateOptions) {
$otherDisplayField = $otherModel->getDisplayField();
$otherModel->recursive = 0;
$rec = $otherModel->findAll();
foreach($rec as $pass) {
foreach($pass as $key => $value) {
if ($key == $this->{$model}->tableToModel[$fieldNames[$tabl['name']]['table']] && isset($value[$otherModel->primaryKey]) && isset($value[$otherDisplayField])) {
$fieldNames[$tabl['name']]['options'][$value[$otherModel->primaryKey]] = $value[$otherDisplayField];
}
}
}
}
$fieldNames[$tabl['name']]['selected'] = $data[$model][$tabl['name']];
}
} else {
$fieldNames[$tabl['name']]['type'] = 'input';
}
break;
case "enum":
$fieldNames[$tabl['name']]['type'] = 'select';
$fieldNames[$tabl['name']]['options'] = array();
$enumValues = split(',', $fieldLength);
foreach($enumValues as $enum) {
$enum = trim($enum, "'");
$fieldNames[$tabl['name']]['options'][$enum] = $enum;
}
$fieldNames[$tabl['name']]['selected'] = $data[$model][$tabl['name']];
break;
case "date":
case "datetime":
if (0 != strncmp("created", $tabl['name'], 7) && 0 != strncmp("modified", $tabl['name'], 8)) {
$fieldNames[$tabl['name']]['type'] = $type;
}
if (isset($data[$model][$tabl['name']])) {
$fieldNames[$tabl['name']]['selected'] = $data[$model][$tabl['name']];
} else {
$fieldNames[$tabl['name']]['selected'] = null;
}
break;
default:
break;
}
}
foreach($objRegistryModel->hasAndBelongsToMany as $relation => $relData) {
$modelName = $relData['className'];
$manyAssociation = $relation;
$modelKeyM = Inflector::underscore($modelName);
$modelObject =& new $modelName();
if ($doCreateOptions) {
$otherDisplayField = $modelObject->getDisplayField();
$fieldNames[$modelKeyM]['model'] = $modelName;
$fieldNames[$modelKeyM]['prompt'] = "Related " . Inflector::humanize(Inflector::pluralize($modelName));
$fieldNames[$modelKeyM]['type'] = "selectMultiple";
$fieldNames[$modelKeyM]['tagName'] = $manyAssociation . '/' . $manyAssociation;
$modelObject->recursive = 0;
$rec = $modelObject->findAll();
foreach($rec as $pass) {
foreach($pass as $key => $value) {
if ($key == $modelName && isset($value[$modelObject->primaryKey]) && isset($value[$otherDisplayField])) {
$fieldNames[$modelKeyM]['options'][$value[$modelObject->primaryKey]] = $value[$otherDisplayField];
}
}
}
if (isset($data[$manyAssociation])) {
foreach($data[$manyAssociation] as $key => $row) {
$fieldNames[$modelKeyM]['selected'][$row[$modelObject->primaryKey]] = $row[$modelObject->primaryKey];
}
}
}
}
}
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
* @param mixed $op A string containing an SQL comparison operator, or an array matching operators to fields
* @param string $bool SQL boolean operator: AND, OR, XOR, etc.
* @param boolean $exclusive If true, and $op is an array, fields not included in $op will not be included in the returned conditions
* @return array An array of model conditions
*/
function postConditions($data, $op = '', $bool = 'AND', $exclusive = false) {
if (!is_array($data) || empty($data)) {
return null;
}
$cond = array();
foreach($data as $model => $fields) {
foreach($fields as $field => $value) {
$key = $model . '.' . $field;
if (is_string($op)) {
$cond[$key] = $this->__postConditionMatch($op, $value);
} elseif (is_array($op)) {
$opFields = array_keys($op);
if (in_array($key, $opFields) || in_array($field, $opFields)) {
if (in_array($key, $opFields)) {
$cond[$key] = $this->__postConditionMatch($op[$key], $value);
} else {
$cond[$key] = $this->__postConditionMatch($op[$field], $value);
}
} elseif (!$exclusive) {
$cond[$key] = $this->__postConditionMatch(null, $value);
}
}
}
}
if ($bool != null && up($bool) != 'AND') {
$cond = array($bool => $cond);
}
return $cond;
}
/**
* Private method used by postConditions
*
*/
function __postConditionMatch($op, $value) {
if (is_string($op)) {
$op = up(trim($op));
}
switch($op) {
case '':
case '=':
case null:
return $value;
break;
case 'LIKE':
return 'LIKE %' . $value . '%';
break;
default:
return $op . ' ' . $value;
break;
}
}
Merging fixes and enhancements into trunk. Revision: [2104] Correct changes of names in scaffold. Revision: [2103] Changed methods to protected methods Controller::_beforeScaffold($method) Controller::_scaffoldError($method) Revision: [2102] Added 2 methods to Controller class that must be overridden in child classes if you place to use Scaffold in production applications. This will allow you to perform checks before displaying the forms, and also before performing any Scaffold methods. Each of these methods are passed the name of the method they represent in Scaffold. Controller::beforeScaffold($method) Controller::scaffoldError($method) These 3 name are passed to the methods above, but are not view related 'create' when creating a new record in the database 'update' when updating a record in the database. 'delete' name says what it does These are view related names passed to the methods above 'index' the list of all records shown in scaffold. 'edit' the edit record form. 'show' when displaying a specific record. 'add' add record form. Scaffold can now be used to generate forms and customize these on a per app or per controller basis. Scaffold will first look: Example: app/views/posts/ Then: app/views/scaffold/ There are also specific naming used for these views each related to a scaffold method. scaffold.list.thtml scaffold.edit.thtml scaffold.new.thtml scaffold.show.thtml Added scaffold_error.thtml that is used if a controller does not implement Controller::scaffoldError() when using scaffold and Controller::beforeScaffold() is implemented and returns false Revision: [2101] Corrected some bugs found in View that would cause errors when using scaffold. Revision: [2100] Added fix for Ticket #427 git-svn-id: https://svn.cakephp.org/repo/trunk/cake@2105 3807eeeb-6ff5-0310-8944-8be069107fe0
2006-02-23 11:40:52 +00:00
/**
* Cleans up the date fields of current Model.
*
*/
function cleanUpFields() {
foreach($this->{$this->modelClass}->_tableInfo as $table) {
foreach($table as $field) {
if ('date' == $field['type'] && isset($this->params['data'][$this->modelClass][$field['name'] . '_year'])) {
$newDate = $this->params['data'][$this->modelClass][$field['name'] . '_year'] . '-';
$newDate .= $this->params['data'][$this->modelClass][$field['name'] . '_month'] . '-';
$newDate .= $this->params['data'][$this->modelClass][$field['name'] . '_day'];
unset($this->params['data'][$this->modelClass][$field['name'] . '_year']);
unset($this->params['data'][$this->modelClass][$field['name'] . '_month']);
unset($this->params['data'][$this->modelClass][$field['name'] . '_day']);
unset($this->params['data'][$this->modelClass][$field['name'] . '_hour']);
unset($this->params['data'][$this->modelClass][$field['name'] . '_min']);
unset($this->params['data'][$this->modelClass][$field['name'] . '_meridian']);
$this->params['data'][$this->modelClass][$field['name']] = $newDate;
$this->data[$this->modelClass][$field['name']] = $newDate;
} else if('datetime' == $field['type'] && isset($this->params['data'][$this->modelClass][$field['name'] . '_year'])) {
$hour = $this->params['data'][$this->modelClass][$field['name'] . '_hour'];
if ($hour != 12 && (isset($this->params['data'][$this->modelClass][$field['name'] . '_meridian']) && 'pm' == $this->params['data'][$this->modelClass][$field['name'] . '_meridian'])) {
$hour = $hour + 12;
}
$newDate = $this->params['data'][$this->modelClass][$field['name'] . '_year'] . '-';
$newDate .= $this->params['data'][$this->modelClass][$field['name'] . '_month'] . '-';
$newDate .= $this->params['data'][$this->modelClass][$field['name'] . '_day'] . ' ';
$newDate .= $hour . ':' . $this->params['data'][$this->modelClass][$field['name'] . '_min'] . ':00';
unset($this->params['data'][$this->modelClass][$field['name'] . '_year']);
unset($this->params['data'][$this->modelClass][$field['name'] . '_month']);
unset($this->params['data'][$this->modelClass][$field['name'] . '_day']);
unset($this->params['data'][$this->modelClass][$field['name'] . '_hour']);
unset($this->params['data'][$this->modelClass][$field['name'] . '_min']);
unset($this->params['data'][$this->modelClass][$field['name'] . '_meridian']);
$this->params['data'][$this->modelClass][$field['name']] = $newDate;
$this->data[$this->modelClass][$field['name']] = $newDate;
}
}
}
}
/**
* Called before the controller action. Overridden in subclasses.
*
*/
function beforeFilter() {
}
/**
* Called after the controller action is run, but before the view is rendered. Overridden in subclasses.
*
*/
function beforeRender() {
}
/**
* Called after the controller action is run and rendered. Overridden in subclasses.
*
*/
function afterFilter() {
}
Merging fixes and enhancements into trunk. Revision: [2104] Correct changes of names in scaffold. Revision: [2103] Changed methods to protected methods Controller::_beforeScaffold($method) Controller::_scaffoldError($method) Revision: [2102] Added 2 methods to Controller class that must be overridden in child classes if you place to use Scaffold in production applications. This will allow you to perform checks before displaying the forms, and also before performing any Scaffold methods. Each of these methods are passed the name of the method they represent in Scaffold. Controller::beforeScaffold($method) Controller::scaffoldError($method) These 3 name are passed to the methods above, but are not view related 'create' when creating a new record in the database 'update' when updating a record in the database. 'delete' name says what it does These are view related names passed to the methods above 'index' the list of all records shown in scaffold. 'edit' the edit record form. 'show' when displaying a specific record. 'add' add record form. Scaffold can now be used to generate forms and customize these on a per app or per controller basis. Scaffold will first look: Example: app/views/posts/ Then: app/views/scaffold/ There are also specific naming used for these views each related to a scaffold method. scaffold.list.thtml scaffold.edit.thtml scaffold.new.thtml scaffold.show.thtml Added scaffold_error.thtml that is used if a controller does not implement Controller::scaffoldError() when using scaffold and Controller::beforeScaffold() is implemented and returns false Revision: [2101] Corrected some bugs found in View that would cause errors when using scaffold. Revision: [2100] Added fix for Ticket #427 git-svn-id: https://svn.cakephp.org/repo/trunk/cake@2105 3807eeeb-6ff5-0310-8944-8be069107fe0
2006-02-23 11:40:52 +00:00
/**
* This method should be overridden in child classes.
*
* @param string $method name of method called example index, edit, etc.
* @return boolean
*/
function _beforeScaffold($method) {
return true;
}
/**
* This method should be overridden in child classes.
*
* @param string $method name of method called either edit or update.
* @return boolean
*/
function _afterScaffoldSave($method) {
return true;
}
/**
* This method should be overridden in child classes.
*
* @param string $method name of method called either edit or update.
* @return boolean
*/
function _afterScaffoldSaveError($method) {
return true;
}
Merging fixes and enhancements into trunk. Revision: [2104] Correct changes of names in scaffold. Revision: [2103] Changed methods to protected methods Controller::_beforeScaffold($method) Controller::_scaffoldError($method) Revision: [2102] Added 2 methods to Controller class that must be overridden in child classes if you place to use Scaffold in production applications. This will allow you to perform checks before displaying the forms, and also before performing any Scaffold methods. Each of these methods are passed the name of the method they represent in Scaffold. Controller::beforeScaffold($method) Controller::scaffoldError($method) These 3 name are passed to the methods above, but are not view related 'create' when creating a new record in the database 'update' when updating a record in the database. 'delete' name says what it does These are view related names passed to the methods above 'index' the list of all records shown in scaffold. 'edit' the edit record form. 'show' when displaying a specific record. 'add' add record form. Scaffold can now be used to generate forms and customize these on a per app or per controller basis. Scaffold will first look: Example: app/views/posts/ Then: app/views/scaffold/ There are also specific naming used for these views each related to a scaffold method. scaffold.list.thtml scaffold.edit.thtml scaffold.new.thtml scaffold.show.thtml Added scaffold_error.thtml that is used if a controller does not implement Controller::scaffoldError() when using scaffold and Controller::beforeScaffold() is implemented and returns false Revision: [2101] Corrected some bugs found in View that would cause errors when using scaffold. Revision: [2100] Added fix for Ticket #427 git-svn-id: https://svn.cakephp.org/repo/trunk/cake@2105 3807eeeb-6ff5-0310-8944-8be069107fe0
2006-02-23 11:40:52 +00:00
/**
* This method should be overridden in child classes.
* If not it will render a scaffold error.
* Method MUST return true in child classes
*
* @param string $method name of method called example index, edit, etc.
* @return boolean
*/
function _scaffoldError($method) {
return false;
}
Merging changes to trunk: Revision: [1761] Removing old db_acl.sql Revision: [1759] Removed unneeded calls to uses(). Changed basics.php listClasses() no longer using folder class. Starting corrections in DboPostgres class. Adding missing DboPostgres::query(). Added missing doc blocks to AjaxHelper. Fixed undefined keys in FormHelper::generateFields() Reformatted FormHelper::generateFields() adding open and close brackets where needed Revision: [1758] Fixed typo Revision: [1757] Fixed errors found when using PHP 4. Fixed a scaffold error Revision: [1756] Merging changes to model_php4.php Revision: [1755] Fixed scaffolding for the changes made to the model. Fixed Model::isForeignKey(), replaced array_key_exists with in_array, other function was failing Revision: [1754] Committing changes from bundt model to beta. DataSources will not be in the beta release Revision: [1751] Cleaning up a little more in the code. Removing loading of log.php unless it is really needed. Refactored dispatcher to speed up the stripslashes code if it is called Revision: [1748] removing all references to error_messages and deleting the file Revision: [1747] updated more error messages Revision: [1746] removing all error message defines Revision: [1745] added _() method from 1.0 to basics.php only used to return string right now Revision: [1744] Adding fix for ticket #220 Revision: [1743] More work on ErrorHandler class Revision: [1742] Renaming error view for missing database connection Revision: [1741] More work on ErrorHandler class Revision: [1740] More work on error class Revision: [1739] Replacing all $_SERVER variable check with env() in basics.php Revision: [1738] Adding env() to basic Revision: [1737] Updated session to use env() Revision: [1736] Removing ternary operators from Dispatcher Revision: [1735] Per nates request I am rolling back ACL to [1373] Revision: [1734] Removed the IP in the session class this was not very reliable. Added a time setting that generates current time adding the Security::inactiveMins() to the session Removed code that was added to basics.php to replace gethostbyaddr(). Added CAKE_SESSION_STRING define to core.php which is used in the by the Session class to generate a hashed key. Revision: [1733] Moving errors messages to ErrorHandler class. Updating errors view for use with new class. Updating Scaffold to use new class. Updated Dispatcher to use new class. Removing methods from Object class Revision: [1732] Adding ErrorHandler class Revision: [1731] Adding fix for Ticket #223 git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1762 3807eeeb-6ff5-0310-8944-8be069107fe0
2006-01-12 02:10:47 +00:00
}
?>