2005-07-04 01:07:14 +00:00
|
|
|
<?php
|
2005-08-21 06:49:02 +00:00
|
|
|
/* SVN FILE: $Id$ */
|
2005-06-19 23:30:36 +00:00
|
|
|
|
|
|
|
/**
|
2005-08-25 03:45:14 +00:00
|
|
|
* Base controller class.
|
2005-06-19 23:30:36 +00:00
|
|
|
*
|
2005-08-21 06:49:02 +00:00
|
|
|
* PHP versions 4 and 5
|
2005-06-19 23:30:36 +00:00
|
|
|
*
|
2005-08-21 06:49:02 +00:00
|
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright (c) 2005, CakePHP Authors/Developers
|
2005-07-10 05:08:19 +00:00
|
|
|
*
|
2005-08-21 06:49:02 +00:00
|
|
|
* Author(s): Michal Tatarynowicz aka Pies <tatarynowicz@gmail.com>
|
|
|
|
* Larry E. Masters aka PhpNut <nut@phpnut.com>
|
|
|
|
* Kamil Dzielinski aka Brego <brego.dk@gmail.com>
|
2005-07-10 05:08:19 +00:00
|
|
|
*
|
2005-08-21 06:49:02 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
2005-07-10 05:08:19 +00:00
|
|
|
*
|
2005-08-25 03:45:14 +00:00
|
|
|
* @filesource
|
2005-08-21 06:49:02 +00:00
|
|
|
* @author CakePHP Authors/Developers
|
|
|
|
* @copyright Copyright (c) 2005, CakePHP Authors/Developers
|
|
|
|
* @link https://trac.cakephp.org/wiki/Authors Authors/Developers
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.libs
|
|
|
|
* @since CakePHP v 0.2.9
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2005-07-10 05:08:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2005-08-25 03:45:14 +00:00
|
|
|
* Include files
|
2005-07-10 05:08:19 +00:00
|
|
|
*/
|
2005-08-21 06:49:02 +00:00
|
|
|
uses('model', 'inflector', 'folder', 'view', 'helper');
|
2005-07-10 05:08:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2005-08-21 06:49:02 +00:00
|
|
|
* Controller
|
2005-07-10 05:08:19 +00:00
|
|
|
*
|
2005-08-25 03:45:14 +00:00
|
|
|
* Application controller (controllers are where you put all the actual code)
|
2005-08-21 06:49:02 +00:00
|
|
|
* Provides basic functionality, such as rendering views (aka displaying templates).
|
2005-08-25 03:45:14 +00:00
|
|
|
* Automatically selects model name from on singularized object class name
|
2005-08-21 06:49:02 +00:00
|
|
|
* and creates the model object if proper class exists.
|
2005-07-10 05:08:19 +00:00
|
|
|
*
|
2005-08-21 06:49:02 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.libs
|
|
|
|
* @since CakePHP v 0.2.9
|
2005-07-10 05:08:19 +00:00
|
|
|
*
|
|
|
|
*/
|
2005-08-21 06:49:02 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $parent = 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');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
|
|
|
var $viewPath;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Variables for the view
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $_viewVars = array();
|
|
|
|
|
|
|
|
/**
|
2005-08-25 03:45:14 +00:00
|
|
|
* Web page title
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $pageTitle = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of model objects.
|
|
|
|
*
|
|
|
|
* @var array Array of model objects.
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $models = array();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $base = null;
|
|
|
|
|
|
|
|
/**
|
2005-08-25 03:45:14 +00:00
|
|
|
* Layout file to use (see /app/views/layouts/default.thtml)
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $layout = 'default';
|
|
|
|
|
|
|
|
/**
|
2005-08-25 03:45:14 +00:00
|
|
|
* Automatically render the view (the dispatcher checks for this variable before running render())
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $autoRender = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $autoLayout = true;
|
|
|
|
|
|
|
|
/**
|
2005-08-25 03:45:14 +00:00
|
|
|
* Database configuration to use (see /config/database.php)
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $useDbConfig = 'default';
|
2005-08-25 03:45:14 +00:00
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $beforeFilter = null;
|
|
|
|
|
|
|
|
/**
|
2005-08-25 03:45:14 +00:00
|
|
|
* Constructor.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
|
|
|
*/
|
2005-09-17 02:22:07 +00:00
|
|
|
function __construct ()
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
|
|
|
// parent::__construct();
|
|
|
|
$r = null;
|
|
|
|
if (!preg_match('/(.*)Controller/i', get_class($this), $r))
|
|
|
|
{
|
|
|
|
die("Controller::__construct() : Can't get or parse my own class name, exiting.");
|
|
|
|
}
|
|
|
|
|
2005-09-17 02:22:07 +00:00
|
|
|
$this->name = $r[1];
|
2005-08-21 06:49:02 +00:00
|
|
|
$this->viewPath = Inflector::underscore($r[1]);
|
|
|
|
|
|
|
|
//Adding Before Filter check
|
|
|
|
if (!empty($this->beforeFilter))
|
|
|
|
{
|
|
|
|
if(is_array($this->beforeFilter))
|
|
|
|
{
|
|
|
|
// If is an array we will use a foreach and run these methods
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Run a single before filter
|
2005-08-25 03:45:14 +00:00
|
|
|
}
|
2005-08-21 06:49:02 +00:00
|
|
|
}
|
2005-08-25 03:45:14 +00:00
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function contructClasses(){
|
|
|
|
|
|
|
|
if(empty($this->params['pass']))
|
|
|
|
{
|
|
|
|
$id = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$id = $this->params['pass'];
|
|
|
|
}
|
|
|
|
|
2005-08-23 23:21:33 +00:00
|
|
|
$dboFactory = DboFactory::getInstance($this->useDbConfig);
|
|
|
|
$this->db =& $dboFactory;
|
|
|
|
|
2005-09-17 02:22:07 +00:00
|
|
|
$match = array();
|
|
|
|
preg_match('/(.*)Controller/i', get_class($this), $match);
|
|
|
|
|
|
|
|
$model_class = Inflector::singularize($match[1]);
|
|
|
|
$modelKey = strtolower(Inflector::underscore($model_class));
|
2005-08-21 06:49:02 +00:00
|
|
|
|
|
|
|
if (class_exists($model_class) && ($this->uses === false))
|
|
|
|
{
|
2005-09-17 02:22:07 +00:00
|
|
|
$this->models[$modelKey] = new $model_class($id);
|
2005-08-21 06:49:02 +00:00
|
|
|
}
|
|
|
|
elseif ($this->uses)
|
|
|
|
{
|
|
|
|
if (!$this->db)
|
2005-07-10 05:08:19 +00:00
|
|
|
{
|
2005-08-21 06:49:02 +00:00
|
|
|
die("Controller::__construct() : ".$this->name." controller needs database access, exiting.");
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
$uses = is_array($this->uses)? $this->uses: array($this->uses);
|
2005-07-10 05:08:19 +00:00
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
foreach ($uses as $model_name)
|
2005-07-10 05:08:19 +00:00
|
|
|
{
|
2005-08-21 06:49:02 +00:00
|
|
|
$model_class = ucfirst(strtolower($model_name));
|
2005-09-17 02:22:07 +00:00
|
|
|
$modelKey = strtolower(Inflector::underscore($model_class));
|
2005-08-21 06:49:02 +00:00
|
|
|
|
|
|
|
if (class_exists($model_class))
|
|
|
|
{
|
2005-09-17 02:22:07 +00:00
|
|
|
$this->models[$modelKey] = new $model_class($id);
|
2005-08-21 06:49:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
die("Controller::__construct() : ".ucfirst($this->name)." requires missing model {$model_class}, exiting.");
|
|
|
|
}
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
2005-08-21 06:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirects to given $url, after turning off $this->autoRender.
|
|
|
|
*
|
|
|
|
* @param unknown_type $url
|
|
|
|
*/
|
|
|
|
function redirect ($url)
|
|
|
|
{
|
|
|
|
$this->autoRender = false;
|
|
|
|
header ('Location: '.$this->base.$url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves a variable to use inside a template.
|
|
|
|
*
|
|
|
|
* @param mixed $one A string or an array of data.
|
|
|
|
* @param string $two Value in case $one is a string (which then works as the key), otherwise unused.
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function set($one, $two=null)
|
|
|
|
{
|
|
|
|
return $this->_setArray(is_array($one)? $one: array($one=>$two));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $action
|
|
|
|
*/
|
|
|
|
function setAction ($action)
|
|
|
|
{
|
|
|
|
$this->action = $action;
|
|
|
|
|
|
|
|
$args = func_get_args();
|
|
|
|
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);
|
|
|
|
|
|
|
|
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, $object->invalidFields($object->data));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->validationErrors = (count($errors)? $errors: false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-08-25 03:45:14 +00:00
|
|
|
* Gets an instance of the view object & prepares it for rendering the output, then
|
|
|
|
* asks the view to actualy do the job.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
|
|
|
* @param unknown_type $action
|
|
|
|
* @param unknown_type $layout
|
|
|
|
* @param unknown_type $file
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function render($action=null, $layout=null, $file=null)
|
|
|
|
{
|
2005-09-17 07:56:32 +00:00
|
|
|
$view =& new View($this);
|
2005-08-21 06:49:02 +00:00
|
|
|
|
|
|
|
if(!empty($this->models))
|
|
|
|
{
|
2005-09-17 02:22:07 +00:00
|
|
|
foreach ($this->models as $key)
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
2005-09-17 02:22:07 +00:00
|
|
|
$key = Inflector::underscore(get_class($key));
|
2005-08-21 06:49:02 +00:00
|
|
|
if(!empty($this->models[$key]->validationErrors))
|
|
|
|
{
|
|
|
|
$view->validationErrors[$key] =& $this->models[$key]->validationErrors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $view->render($action, $layout, $file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function missingController()
|
|
|
|
{
|
|
|
|
//We are simulating action call below, this is not a filename!
|
|
|
|
$this->render('../errors/missingController');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function missingAction()
|
|
|
|
{
|
|
|
|
//We are simulating action call below, this is not a filename!
|
|
|
|
$this->render('../errors/missingAction');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function missingView()
|
|
|
|
{
|
|
|
|
//We are simulating action call below, this is not a filename!
|
|
|
|
$this->render('../errors/missingView');
|
|
|
|
}
|
2005-09-17 07:56:32 +00:00
|
|
|
|
|
|
|
function missingDatabase()
|
|
|
|
{
|
|
|
|
//We are simulating action call below, this is not a filename!
|
|
|
|
$this->render('../errors/missingDatabase');
|
|
|
|
}
|
2005-08-21 06:49:02 +00:00
|
|
|
|
|
|
|
// /**
|
|
|
|
// * Displays an error page to the user. Uses layouts/error.html to render the page.
|
|
|
|
// *
|
|
|
|
// * @param int $code Error code (for instance: 404)
|
|
|
|
// * @param string $name Name of the error (for instance: Not Found)
|
|
|
|
// * @param string $message Error message
|
|
|
|
// */
|
|
|
|
// function error ($code, $name, $message)
|
|
|
|
// {
|
|
|
|
// header ("HTTP/1.0 {$code} {$name}");
|
|
|
|
// print ($this->_render(VIEWS.'layouts/error.thtml', array('code'=>$code,'name'=>$name,'message'=>$message)));
|
|
|
|
// }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets data for this view. Will set title if the key "title" is in given $data array.
|
|
|
|
*
|
2005-08-25 03:45:14 +00:00
|
|
|
* @param array $data Array of
|
2005-08-21 06:49:02 +00:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-08-25 03:45:14 +00:00
|
|
|
* Shows a message to the user $time seconds, then redirects to $url
|
|
|
|
* Uses flash.thtml as a layout for the messages
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2005-08-25 03:45:14 +00:00
|
|
|
* @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
|
2005-08-21 06:49:02 +00:00
|
|
|
*/
|
2005-09-17 02:22:07 +00:00
|
|
|
function flash($message, $url, $pause=1)
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
|
|
|
$this->autoRender = false;
|
|
|
|
$this->autoLayout = false;
|
|
|
|
|
|
|
|
$this->set('url', $this->base.$url);
|
|
|
|
$this->set('message', $message);
|
2005-09-17 02:22:07 +00:00
|
|
|
$this->set('pause', $pause);
|
|
|
|
$this->set('page_title', $message);
|
2005-08-21 06:49:02 +00:00
|
|
|
|
|
|
|
$this->render(null,false,VIEWS.'layouts'.DS.'flash.thtml');
|
|
|
|
}
|
2005-08-25 03:45:14 +00:00
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
/**
|
2005-08-25 03:45:14 +00:00
|
|
|
* 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
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
|
|
|
* @param unknown_type $message
|
|
|
|
* @param unknown_type $url
|
|
|
|
* @param unknown_type $time
|
|
|
|
*/
|
|
|
|
function flashOut($message, $url, $time=1)
|
|
|
|
{
|
|
|
|
$this->autoRender = false;
|
|
|
|
$this->autoLayout = false;
|
|
|
|
|
|
|
|
$this->set('url', $url);
|
|
|
|
$this->set('message', $message);
|
|
|
|
$this->set('time', $time);
|
|
|
|
|
|
|
|
$this->render(null,false,VIEWS.'layouts'.DS.'flash.thtml');
|
|
|
|
}
|
2005-08-25 03:45:14 +00:00
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2005-08-25 03:45:14 +00:00
|
|
|
function generateFieldNames( $data = null, $doCreateOptions = true )
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
|
|
|
// Initialize the list array
|
|
|
|
$fieldNames = array();
|
2005-08-25 03:45:14 +00:00
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
// figure out what model and table we are working with
|
|
|
|
$model = Inflector::pluralize($this->name);
|
2005-09-17 02:22:07 +00:00
|
|
|
$table = Inflector::underscore(Inflector::singularize($this->name));
|
2005-08-21 06:49:02 +00:00
|
|
|
|
|
|
|
// get all of the column names.
|
2005-08-23 23:21:33 +00:00
|
|
|
$classRegistry =& ClassRegistry::getInstance();
|
2005-09-17 02:22:07 +00:00
|
|
|
$objRegistryModel = $classRegistry->getObject(Inflector::singularize($model));
|
2005-08-25 03:45:14 +00:00
|
|
|
|
2005-08-21 20:01:32 +00:00
|
|
|
foreach ($objRegistryModel->_table_info as $tables)
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
|
|
|
foreach ($tables as $tabl)
|
|
|
|
{
|
|
|
|
// set up the prompt
|
2005-08-25 16:40:50 +00:00
|
|
|
if( $objRegistryModel->isForeignKey($tabl['name']) )
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
|
|
|
$niceName = substr( $tabl['name'], 0, strpos( $tabl['name'], "_id" ) );
|
|
|
|
$fieldNames[ $tabl['name'] ]['prompt'] = Inflector::humanize($niceName);
|
|
|
|
// this is a foreign key, also set up the other controller
|
|
|
|
$fieldNames[ $tabl['name'] ]['model'] = Inflector::singularize($niceName);
|
|
|
|
$fieldNames[ $tabl['name'] ]['controller'] = Inflector::pluralize($niceName);
|
|
|
|
$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';
|
|
|
|
}
|
2005-08-25 03:45:14 +00:00
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
// Now, set up some other attributes that will be useful for auto generating a form.
|
|
|
|
//tagName is in the format table/field "post/title"
|
|
|
|
$fieldNames[ $tabl['name']]['tagName'] = $table.'/'.$tabl['name'];
|
|
|
|
|
|
|
|
// Now, find out if this is a required field.
|
2005-08-21 20:01:32 +00:00
|
|
|
//$validationFields = $classRegistry->getObject($table)->validate;
|
|
|
|
$validationFields = $objRegistryModel->validate;
|
2005-08-21 06:49:02 +00:00
|
|
|
if( isset( $validationFields[ $tabl['name'] ] ) )
|
|
|
|
{
|
|
|
|
// Now, we know that this field has some validation set.
|
|
|
|
// find out if it is a required field.
|
2005-08-25 03:45:14 +00:00
|
|
|
if( VALID_NOT_EMPTY == $validationFields[ $tabl['name'] ] )
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
|
|
|
// this is a required field.
|
|
|
|
$fieldNames[$tabl['name']]['required'] = true;
|
|
|
|
$fieldNames[$tabl['name']]['errorMsg'] = "Required Field";
|
|
|
|
}
|
|
|
|
}
|
2005-08-25 03:45:14 +00:00
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
// now, determine what the input type should be for this database 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":
|
2005-08-25 03:45:14 +00:00
|
|
|
{
|
2005-08-21 06:49:02 +00:00
|
|
|
$fieldNames[ $tabl['name']]['type'] = 'area';
|
|
|
|
//$fieldNames[ $tabl['name']]['size'] = $fieldLength;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "varchar":
|
|
|
|
{
|
|
|
|
if( isset( $fieldNames[ $tabl['name']]['foreignKey'] ) )
|
|
|
|
{
|
|
|
|
$fieldNames[ $tabl['name']]['type'] = 'select';
|
|
|
|
// This is a foreign key select dropdown box. now, we have to add the options.
|
|
|
|
$fieldNames[ $tabl['name']]['options'] = array();
|
2005-08-25 03:45:14 +00:00
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
// get the list of options from the other model.
|
2005-08-23 23:21:33 +00:00
|
|
|
$registry =& ClassRegistry::getInstance();
|
2005-08-21 06:49:02 +00:00
|
|
|
$otherModel = $registry->getObject($fieldNames[ $tabl['name']]['model']);
|
2005-08-25 03:45:14 +00:00
|
|
|
|
|
|
|
if( is_object($otherModel) )
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
|
|
|
if( $doCreateOptions )
|
|
|
|
{
|
|
|
|
$otherDisplayField = $otherModel->getDisplayField();
|
|
|
|
foreach( $otherModel->findAll() as $pass )
|
|
|
|
{
|
2005-08-25 03:45:14 +00:00
|
|
|
foreach( $pass as $key=>$value )
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
2005-08-25 03:45:14 +00:00
|
|
|
if( $key == $fieldNames[ $tabl['name']]['model'] && isset( $value['id'] ) && isset( $value[$otherDisplayField] ) )
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
|
|
|
$fieldNames[ $tabl['name']]['options'][$value['id']] = $value[$otherDisplayField];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$fieldNames[ $tabl['name']]['selected'] = $data[$table][$tabl['name']];
|
|
|
|
}
|
|
|
|
}
|
2005-08-25 03:45:14 +00:00
|
|
|
else
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
|
|
|
$fieldNames[ $tabl['name']]['type'] = 'input';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2005-08-25 03:45:14 +00:00
|
|
|
case "tinyint":
|
|
|
|
{
|
|
|
|
if( $fieldLength > 1 )
|
|
|
|
{
|
|
|
|
$fieldNames[ $tabl['name']]['type'] = 'input';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$fieldNames[ $tabl['name']]['type'] = 'checkbox';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2005-08-21 06:49:02 +00:00
|
|
|
case "int":
|
2005-08-25 16:40:50 +00:00
|
|
|
case "smallint":
|
|
|
|
case "mediumint":
|
|
|
|
case "bigint":
|
2005-08-21 06:49:02 +00:00
|
|
|
case "decimal":
|
2005-08-21 20:01:32 +00:00
|
|
|
case "float":
|
|
|
|
case "double":
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
|
|
|
//BUGBUG: Need a better way to determine if this field is an auto increment foreign key.
|
|
|
|
// If it is a number, and it is a foreign key, we'll make a HUGE assumption that it is an auto increment field.
|
|
|
|
// for foreign key autonumber fields, we'll set the type to 'key' so that it does not display in the input form.
|
|
|
|
if( 0 == strncmp($tabl['name'], 'id', 2) )
|
|
|
|
{
|
|
|
|
$fieldNames[ $tabl['name']]['type'] = 'hidden';
|
|
|
|
}
|
|
|
|
else if( isset( $fieldNames[ $tabl['name']]['foreignKey'] ) )
|
|
|
|
{
|
|
|
|
$fieldNames[ $tabl['name']]['type'] = 'select';
|
|
|
|
// This is a foreign key select dropdown box. now, we have to add the options.
|
|
|
|
$fieldNames[ $tabl['name']]['options'] = array();
|
2005-08-25 03:45:14 +00:00
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
// get the list of options from the other model.
|
2005-08-23 23:21:33 +00:00
|
|
|
$registry =& ClassRegistry::getInstance();
|
2005-08-21 06:49:02 +00:00
|
|
|
$otherModel = $registry->getObject($fieldNames[ $tabl['name']]['model']);
|
2005-08-25 03:45:14 +00:00
|
|
|
|
|
|
|
if( is_object($otherModel) )
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
2005-08-25 03:45:14 +00:00
|
|
|
if( $doCreateOptions )
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
|
|
|
$otherDisplayField = $otherModel->getDisplayField();
|
|
|
|
foreach( $otherModel->findAll() as $pass )
|
|
|
|
{
|
2005-08-25 03:45:14 +00:00
|
|
|
foreach( $pass as $key=>$value )
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
2005-08-25 03:45:14 +00:00
|
|
|
if( $key == $fieldNames[ $tabl['name']]['model'] && isset( $value['id'] ) && isset( $value[$otherDisplayField] ) )
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
|
|
|
$fieldNames[ $tabl['name']]['options'][$value['id']] = $value[$otherDisplayField];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$fieldNames[ $tabl['name']]['selected'] = $data[$table][$tabl['name']];
|
|
|
|
}
|
|
|
|
}
|
2005-08-25 03:45:14 +00:00
|
|
|
else
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
|
|
|
$fieldNames[ $tabl['name']]['type'] = 'input';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "enum":
|
|
|
|
{
|
|
|
|
// for enums, the $fieldLength variable is actually the list of enums.
|
|
|
|
$fieldNames[ $tabl['name']]['type'] = 'select';
|
|
|
|
// This is a foreign key select dropdown box. now, we have to add the options.
|
|
|
|
$fieldNames[ $tabl['name']]['options'] = array();
|
2005-08-25 03:45:14 +00:00
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
$enumValues = split(',', $fieldLength );
|
2005-08-25 03:45:14 +00:00
|
|
|
foreach ($enumValues as $enum )
|
2005-08-21 06:49:02 +00:00
|
|
|
{
|
|
|
|
$enum = trim( $enum, "'" );
|
|
|
|
$fieldNames[$tabl['name']]['options'][$enum] = $enum;
|
|
|
|
}
|
|
|
|
$fieldNames[ $tabl['name']]['selected'] = $data[$table][$tabl['name']];
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "date":
|
|
|
|
case "datetime":
|
|
|
|
{
|
2005-08-21 20:01:32 +00:00
|
|
|
if( 0 != strncmp( "created", $tabl['name'], 6 ) && 0 != strncmp("modified",$tabl['name'], 8) )
|
2005-08-21 06:49:02 +00:00
|
|
|
$fieldNames[ $tabl['name']]['type'] = $type;
|
2005-08-25 03:45:14 +00:00
|
|
|
}
|
2005-08-21 06:49:02 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
//sorry, this database field type is not yet set up.
|
2005-08-25 03:45:14 +00:00
|
|
|
break;
|
2005-08-21 06:49:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // end switch
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
2005-08-21 20:01:32 +00:00
|
|
|
// now, add any necessary hasAndBelongsToMany list boxes
|
|
|
|
// loop through the many to many relations to make a list box.
|
2005-08-25 03:45:14 +00:00
|
|
|
foreach( $objRegistryModel->_manyToMany as $relation )
|
2005-08-21 20:01:32 +00:00
|
|
|
{
|
2005-09-17 02:22:07 +00:00
|
|
|
list($tableName) = $relation;
|
2005-08-21 20:01:32 +00:00
|
|
|
|
|
|
|
$otherModelName = Inflector::singularize($tableName);
|
|
|
|
$otherModel = new $otherModelName();
|
2005-08-25 03:45:14 +00:00
|
|
|
|
|
|
|
if( $doCreateOptions )
|
2005-08-21 20:01:32 +00:00
|
|
|
{
|
|
|
|
$otherDisplayField = $otherModel->getDisplayField();
|
|
|
|
$fieldNames[$tableName]['model'] = $tableName;
|
|
|
|
$fieldNames[$tableName]['prompt'] = "Related ".Inflector::humanize($tableName);
|
|
|
|
$fieldNames[$tableName]['type'] = "selectMultiple";
|
2005-08-22 02:48:37 +00:00
|
|
|
$fieldNames[$tableName]['tagName'] = $otherModelName.'/'.$tableName;
|
2005-08-25 03:45:14 +00:00
|
|
|
|
2005-08-21 20:01:32 +00:00
|
|
|
foreach( $otherModel->findAll() as $pass )
|
|
|
|
{
|
2005-08-25 03:45:14 +00:00
|
|
|
foreach( $pass as $key=>$value )
|
2005-08-21 20:01:32 +00:00
|
|
|
{
|
2005-08-25 03:45:14 +00:00
|
|
|
if( $key == $otherModelName && isset( $value['id'] ) && isset( $value[$otherDisplayField] ) )
|
2005-08-21 20:01:32 +00:00
|
|
|
{
|
|
|
|
$fieldNames[$tableName]['options'][$value['id']] = $value[$otherDisplayField];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( isset( $data[$tableName] ) )
|
|
|
|
{
|
|
|
|
foreach( $data[$tableName] as $row )
|
|
|
|
{
|
|
|
|
$fieldNames[$tableName]['selected'][$row['id']] = $row['id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // end loop through manytomany relations.
|
2005-08-21 06:49:02 +00:00
|
|
|
}
|
2005-08-21 20:01:32 +00:00
|
|
|
|
2005-08-25 03:45:14 +00:00
|
|
|
|
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
return $fieldNames;
|
2005-08-25 03:45:14 +00:00
|
|
|
}
|
2005-06-19 23:30:36 +00:00
|
|
|
}
|
|
|
|
|
2005-07-03 07:07:47 +00:00
|
|
|
?>
|