2005-08-21 06:49:02 +00:00
< ? php
/* SVN FILE: $Id$ */
2005-07-21 04:02:32 +00:00
/**
2005-08-21 06:49:02 +00:00
* Short description for file .
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP : Rapid Development Framework < http :// www . cakephp . org />
* Copyright ( c ) 2005 , CakePHP Authors / Developers
*
* 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 >
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice .
*
* @ filesource
* @ 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 Cake v 1.0 . 0.172
* @ version $Revision $
* @ modifiedby $LastChangedBy $
* @ lastmodified $Date $
* @ license http :// www . opensource . org / licenses / mit - license . php The MIT License
*/
2005-07-21 04:02:32 +00:00
/**
* Enter description here ...
*/
2005-10-03 04:48:00 +00:00
uses ( '/model/model' , 'inflector' , 'object' );
2005-07-21 04:02:32 +00:00
/**
2005-08-25 16:40:50 +00:00
* Scaffolding is a set of automatic views , forms and controllers for starting web development work faster .
2005-08-21 06:49:02 +00:00
*
2005-08-25 16:40:50 +00:00
* Scaffold inspects your database tables , and making educated guesses , sets up a
* number of pages for each of your Models . These pages have data forms that work ,
* and afford the web developer an early look at the data , and the possibility to over - ride
* scaffolded actions with custom - made ones .
2005-08-21 06:49:02 +00:00
*
* @ package cake
* @ subpackage cake . libs
* @ since Cake v 1.0 . 0.172
*/
2005-07-21 04:02:32 +00:00
class Scaffold extends Object {
2005-08-25 16:40:50 +00:00
2005-07-21 04:02:32 +00:00
/**
2005-08-25 16:40:50 +00:00
* Name of controller class
2005-07-21 04:02:32 +00:00
*
2005-08-25 16:40:50 +00:00
* @ var string
2005-07-21 04:02:32 +00:00
*/
var $clazz = null ;
2005-08-25 16:40:50 +00:00
2005-07-21 04:02:32 +00:00
/**
* Enter description here ...
*
2005-08-25 16:40:50 +00:00
* @ var string
2005-07-21 04:02:32 +00:00
*/
var $actionView = null ;
2005-08-25 16:40:50 +00:00
2005-07-21 04:02:32 +00:00
/**
* Enter description here ...
*
* @ var unknown_type
*/
2005-09-17 02:22:07 +00:00
var $modelKey = null ;
2005-08-25 16:40:50 +00:00
2005-07-21 04:02:32 +00:00
/**
* Enter description here ...
*
2005-08-25 16:40:50 +00:00
* @ var Object
2005-07-21 04:02:32 +00:00
*/
var $controllerClass = null ;
2005-08-25 16:40:50 +00:00
2005-07-21 04:02:32 +00:00
/**
2005-08-25 16:40:50 +00:00
* Name of scaffolded Model
2005-07-21 04:02:32 +00:00
*
2005-08-25 16:40:50 +00:00
* @ var string
2005-07-21 04:02:32 +00:00
*/
var $modelName = null ;
2005-08-25 16:40:50 +00:00
2005-07-21 04:02:32 +00:00
/**
2005-08-25 16:40:50 +00:00
* Title HTML element for current scaffolded view
2005-07-21 04:02:32 +00:00
*
2005-08-25 16:40:50 +00:00
* @ var string
2005-07-21 04:02:32 +00:00
*/
var $scaffoldTitle = null ;
2005-08-25 16:40:50 +00:00
/**
* Base URL
2005-07-21 04:02:32 +00:00
*
2005-08-25 16:40:50 +00:00
* @ var string
2005-07-21 04:02:32 +00:00
*/
var $base = false ;
2005-08-25 16:40:50 +00:00
2005-07-21 04:02:32 +00:00
/**
2005-08-25 16:40:50 +00:00
* Construct and set up given controller with given parameters .
2005-07-21 04:02:32 +00:00
*
2005-08-25 16:40:50 +00:00
* @ param string $controller_class Name of controller
* @ param array $params
2005-07-21 04:02:32 +00:00
*/
2005-09-19 22:59:06 +00:00
function __construct ( & $controller , $params )
2005-08-21 06:49:02 +00:00
{
2005-09-19 22:59:06 +00:00
$this -> controllerClass =& $controller ;
$this -> clazz = $controller -> name ;
$this -> actionView = $controller -> action ;
2005-09-17 07:56:32 +00:00
$this -> modelKey = Inflector :: underscore ( Inflector :: singularize ( $this -> clazz ));
2005-09-17 02:22:07 +00:00
$this -> scaffoldTitle = Inflector :: humanize ( $this -> modelKey );
2005-08-25 16:40:50 +00:00
$this -> controllerClass -> layout = 'scaffold' ;
$this -> controllerClass -> pageTitle = $this -> scaffoldTitle ;
2005-09-19 22:59:06 +00:00
$this -> _renderScaffold ( $params );
2005-07-21 04:02:32 +00:00
}
2005-09-19 22:59:06 +00:00
function _renderScaffold ( $params )
{
$this -> _scaffoldView ( $params );
}
2005-08-25 16:40:50 +00:00
2005-07-21 04:02:32 +00:00
/**
2005-08-25 16:40:50 +00:00
* Renders the List view as the default action ( index ) .
2005-07-21 04:02:32 +00:00
*
2005-08-25 16:40:50 +00:00
* @ param array $params
* @ return boolean Success
2005-07-21 04:02:32 +00:00
*/
2005-09-19 22:59:06 +00:00
function _scaffoldIndex ( $params )
2005-08-21 06:49:02 +00:00
{
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> pageTitle = Inflector :: humanize ( Inflector :: pluralize ( $this -> modelKey ));
2005-09-19 22:59:06 +00:00
return $this -> _scaffoldList ( $params );
2005-07-21 04:02:32 +00:00
}
2005-08-25 16:40:50 +00:00
2005-07-21 04:02:32 +00:00
/**
2005-08-25 16:40:50 +00:00
* Renders a Show view of scaffolded Model .
2005-07-21 04:02:32 +00:00
*
2005-08-25 16:40:50 +00:00
* @ param array $params
2005-07-21 04:02:32 +00:00
*/
2005-09-19 22:59:06 +00:00
function _scaffoldShow ( $params )
2005-08-21 06:49:02 +00:00
{
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> params [ 'data' ] = $this -> controllerClass -> models [ $this -> modelKey ] -> read ();
2005-08-25 16:40:50 +00:00
$this -> controllerClass -> set ( 'data' , $this -> controllerClass -> params [ 'data' ] );
$this -> controllerClass -> set ( 'fieldNames' , $this -> controllerClass -> generateFieldNames ( $this -> controllerClass -> params [ 'data' ], false ) );
2005-10-03 04:48:00 +00:00
$this -> controllerClass -> render ( $this -> actionView , '' , LIBS . 'controller' . DS . 'templates' . DS . 'scaffolds' . DS . 'show.thtml' );
2005-07-21 04:02:32 +00:00
}
2005-08-25 16:40:50 +00:00
2005-07-21 04:02:32 +00:00
/**
2005-08-25 16:40:50 +00:00
* Renders List view of scaffolded Model .
2005-07-21 04:02:32 +00:00
*
2005-08-25 16:40:50 +00:00
* @ param array $params
2005-07-21 04:02:32 +00:00
*/
2005-09-19 22:59:06 +00:00
function _scaffoldList ( $params )
2005-08-21 06:49:02 +00:00
{
2005-08-25 16:40:50 +00:00
$this -> controllerClass -> set ( 'fieldNames' , $this -> controllerClass -> generateFieldNames ( null , false ) );
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> set ( 'data' , $this -> controllerClass -> models [ $this -> modelKey ] -> findAll ());
2005-10-03 04:48:00 +00:00
$this -> controllerClass -> render ( $this -> actionView , '' , LIBS . 'controller' . DS . 'templates' . DS . 'scaffolds' . DS . 'list.thtml' );
2005-07-21 04:02:32 +00:00
}
2005-08-25 16:40:50 +00:00
2005-07-21 04:02:32 +00:00
/**
2005-08-25 16:40:50 +00:00
* Creates a new scaffold .
2005-07-21 04:02:32 +00:00
*
2005-08-25 16:40:50 +00:00
* @ param array $params
2005-07-21 04:02:32 +00:00
*/
2005-09-19 22:59:06 +00:00
function _scaffoldNew ( $params )
2005-08-21 06:49:02 +00:00
{
2005-08-25 16:40:50 +00:00
$this -> controllerClass -> set ( 'fieldNames' , $this -> controllerClass -> generateFieldNames () );
2005-10-03 04:48:00 +00:00
$this -> controllerClass -> render ( $this -> actionView , '' , LIBS . 'controller' . DS . 'templates' . DS . 'scaffolds' . DS . 'new.thtml' );
2005-07-21 04:02:32 +00:00
}
2005-08-25 16:40:50 +00:00
2005-07-21 04:02:32 +00:00
/**
2005-08-25 16:40:50 +00:00
* Renders an Edit view for scaffolded Model .
2005-07-21 04:02:32 +00:00
*
2005-08-25 16:40:50 +00:00
* @ param array $params
2005-07-21 04:02:32 +00:00
*/
2005-09-19 22:59:06 +00:00
function _scaffoldEdit ( $params )
2005-08-21 06:49:02 +00:00
{
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> params [ 'data' ] = $this -> controllerClass -> models [ $this -> modelKey ] -> read ();
2005-08-25 16:40:50 +00:00
// generate the field names.
$this -> controllerClass -> set ( 'fieldNames' , $this -> controllerClass -> generateFieldNames ( $this -> controllerClass -> params [ 'data' ]) );
$this -> controllerClass -> set ( 'data' , $this -> controllerClass -> params [ 'data' ]);
2005-10-03 04:48:00 +00:00
$this -> controllerClass -> render ( $this -> actionView , '' , LIBS . 'controller' . DS . 'templates' . DS . 'scaffolds' . DS . 'edit.thtml' );
2005-08-21 06:49:02 +00:00
}
2005-08-25 16:40:50 +00:00
/**
* Renders a " create new " view for scaffolded Model .
*
* @ param array $params
*/
2005-09-19 22:59:06 +00:00
function _scaffoldCreate ( $params )
2005-08-21 06:49:02 +00:00
{
2005-08-25 16:40:50 +00:00
$this -> controllerClass -> set ( 'fieldNames' , $this -> controllerClass -> generateFieldNames () );
2005-09-21 05:48:09 +00:00
$this -> _cleanUpFields ();
2005-08-25 16:40:50 +00:00
2005-09-17 02:22:07 +00:00
if ( $this -> controllerClass -> models [ $this -> modelKey ] -> save ( $this -> controllerClass -> params [ 'data' ]))
2005-08-25 16:40:50 +00:00
{
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> flash ( 'Your ' . $this -> modelKey . ' has been saved.' , '/' . Inflector :: underscore ( $this -> controllerClass -> viewPath ) );
2005-08-25 16:40:50 +00:00
}
else
{
$this -> controllerClass -> set ( 'data' , $this -> controllerClass -> params [ 'data' ]);
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> validateErrors ( $this -> controllerClass -> models [ $this -> modelKey ]);
2005-10-03 04:48:00 +00:00
$this -> controllerClass -> render ( $this -> actionView , '' , LIBS . 'controller' . DS . 'templates' . DS . 'scaffolds' . DS . 'new.thtml' );
2005-08-25 16:40:50 +00:00
}
2005-07-21 04:02:32 +00:00
}
2005-08-25 16:40:50 +00:00
2005-07-21 04:02:32 +00:00
/**
2005-08-25 16:40:50 +00:00
* Renders an update view for scaffolded Model .
2005-07-21 04:02:32 +00:00
*
2005-08-25 16:40:50 +00:00
* @ param array $params
2005-07-21 04:02:32 +00:00
*/
2005-09-19 22:59:06 +00:00
function _scaffoldUpdate ( $params = array ())
2005-08-21 06:49:02 +00:00
{
2005-08-25 16:40:50 +00:00
// clean up the date fields
2005-09-19 22:59:06 +00:00
$this -> _cleanUpFields ();
2005-08-25 16:40:50 +00:00
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> models [ $this -> modelKey ] -> set ( $this -> controllerClass -> params [ 'data' ]);
if ( $this -> controllerClass -> models [ $this -> modelKey ] -> save ())
2005-08-25 16:40:50 +00:00
{
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> flash ( 'The ' . Inflector :: humanize ( $this -> modelKey ) . ' has been updated.' , '/' . Inflector :: underscore ( $this -> controllerClass -> viewPath ));
2005-08-25 16:40:50 +00:00
}
else
{
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> flash ( 'There was an error updating the ' . Inflector :: humanize ( $this -> modelKey ), '/' . Inflector :: underscore ( $this -> controllerClass -> viewPath ));
2005-08-25 16:40:50 +00:00
}
2005-07-21 04:02:32 +00:00
}
2005-08-25 16:40:50 +00:00
2005-07-21 04:02:32 +00:00
/**
2005-08-25 16:40:50 +00:00
* Performs a delete on given scaffolded Model .
2005-07-21 04:02:32 +00:00
*
2005-08-25 16:40:50 +00:00
* @ param array $params
2005-07-21 04:02:32 +00:00
*/
2005-09-19 22:59:06 +00:00
function _scaffoldDestroy ( $params = array ())
2005-08-21 06:49:02 +00:00
{
2005-08-25 16:40:50 +00:00
$id = $params [ 'pass' ][ 0 ];
// figure out what model and table we are working with
2005-09-17 02:22:07 +00:00
if ( $this -> controllerClass -> models [ $this -> modelKey ] -> del ( $id ))
2005-08-25 16:40:50 +00:00
{
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> flash ( 'The ' . Inflector :: humanize ( $this -> modelKey ) . ' with id: ' . $id . ' has been deleted.' , '/' . Inflector :: underscore ( $this -> controllerClass -> viewPath ));
2005-08-25 16:40:50 +00:00
}
else
{
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> flash ( 'There was an error deleting the ' . Inflector :: humanize ( $this -> modelKey ) . ' with the id ' . $id , '/' . Inflector :: underscore ( $this -> controllerClass -> viewPath ));
2005-08-25 16:40:50 +00:00
}
2005-07-21 04:02:32 +00:00
}
2005-09-19 22:59:06 +00:00
/**
* When methods are now present in a controller
* scaffoldView is used to call default Scaffold methods if :
* < code >
* var $scaffold ;
* </ code >
* is placed in the controller ' s class definition .
*
* @ param string $url
* @ param string $controller_class
* @ param array $params
* @ since Cake v 0.10 . 0.172
*/
function _scaffoldView ( $params )
{
2005-10-03 04:48:00 +00:00
if ( ! in_array ( 'form' , $this -> controllerClass -> helpers ))
{
$this -> controllerClass -> helpers [] = 'form' ;
}
2005-09-19 22:59:06 +00:00
$isDataBaseSet = DboFactory :: getInstance ( $this -> controllerClass -> useDbConfig );
if ( ! empty ( $isDataBaseSet ))
{
$this -> controllerClass -> constructClasses ();
if ( $params [ 'action' ] === 'index' || $params [ 'action' ] === 'list' ||
$params [ 'action' ] === 'show' || $params [ 'action' ] === 'new' ||
$params [ 'action' ] === 'create' || $params [ 'action' ] === 'edit' ||
$params [ 'action' ] === 'update' || $params [ 'action' ] === 'destroy' )
{
switch ( $params [ 'action' ])
{
case 'index' :
$this -> _scaffoldIndex ( $params );
break ;
case 'show' :
$this -> _scaffoldShow ( $params );
break ;
case 'list' :
$this -> _scaffoldList ( $params );
break ;
case 'new' :
$this -> _scaffoldNew ( $params );
break ;
case 'edit' :
$this -> _scaffoldEdit ( $params );
break ;
case 'create' :
$this -> _scaffoldCreate ( $params );
break ;
case 'update' :
$this -> _scaffoldUpdate ( $params );
break ;
case 'destroy' :
$this -> _scaffoldDestroy ( $params );
break ;
}
}
else
{
$this -> controllerClass -> layout = 'default' ;
$this -> controllerClass -> missingAction = $params [ 'action' ];
call_user_func_array ( array ( $this -> controllerClass , 'missingAction' ), null );
exit ;
}
}
else
{
$this -> controllerClass -> layout = 'default' ;
call_user_func_array ( array ( $this -> controllerClass , 'missingDatabase' ), null );
exit ;
}
}
2005-08-25 16:40:50 +00:00
/**
* Cleans up the date fields of current Model .
*
*
*/
2005-09-19 22:59:06 +00:00
function _cleanUpFields ()
2005-08-21 06:49:02 +00:00
{
2005-08-25 16:40:50 +00:00
// clean up the date fields
2005-09-17 02:22:07 +00:00
$objModel = $this -> controllerClass -> models [ $this -> modelKey ];
2005-09-18 13:25:20 +00:00
foreach ( $objModel -> _tableInfo as $table )
2005-08-25 16:40:50 +00:00
{
foreach ( $table as $field )
{
2005-09-17 02:22:07 +00:00
if ( 'date' == $field [ 'type' ] && isset ( $this -> controllerClass -> params [ 'data' ][ $this -> modelKey ][ $field [ 'name' ] . '_year' ] ) )
2005-08-25 16:40:50 +00:00
{
$newDate = mktime ( 0 , 0 , 0 ,
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> params [ 'data' ][ $this -> modelKey ][ $field [ 'name' ] . '_month' ],
$this -> controllerClass -> params [ 'data' ][ $this -> modelKey ][ $field [ 'name' ] . '_day' ],
$this -> controllerClass -> params [ 'data' ][ $this -> modelKey ][ $field [ 'name' ] . '_year' ] );
2005-08-25 16:40:50 +00:00
$newDate = date ( 'Y-m-d' , $newDate );
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> params [ 'data' ][ $this -> modelKey ][ $field [ 'name' ]] = $newDate ;
2005-08-25 16:40:50 +00:00
}
2005-09-17 02:22:07 +00:00
else if ( 'datetime' == $field [ 'type' ] && isset ( $this -> controllerClass -> params [ 'data' ][ $this -> modelKey ][ $field [ 'name' ] . '_year' ] ) )
2005-08-25 16:40:50 +00:00
{
2005-09-17 02:22:07 +00:00
$hour = $this -> controllerClass -> params [ 'data' ][ $this -> modelKey ][ $field [ 'name' ] . '_hour' ];
if ( $hour != 12 && 'pm' == $this -> controllerClass -> params [ 'data' ][ $this -> modelKey ][ $field [ 'name' ] . '_meridian' ] )
2005-08-25 16:40:50 +00:00
{
$hour = $hour + 12 ;
}
$newDate = mktime ( $hour ,
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> params [ 'data' ][ $this -> modelKey ][ $field [ 'name' ] . '_min' ],
2005-08-25 16:40:50 +00:00
0 ,
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> params [ 'data' ][ $this -> modelKey ][ $field [ 'name' ] . '_month' ],
$this -> controllerClass -> params [ 'data' ][ $this -> modelKey ][ $field [ 'name' ] . '_day' ],
$this -> controllerClass -> params [ 'data' ][ $this -> modelKey ][ $field [ 'name' ] . '_year' ] );
2005-08-25 16:40:50 +00:00
$newDate = date ( 'Y-m-d' , $newDate );
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> params [ 'data' ][ $this -> modelKey ][ $field [ 'name' ]] = $newDate ;
2005-08-25 16:40:50 +00:00
}
else if ( 'tinyint(1)' == $field [ 'type' ] )
{
2005-09-17 02:22:07 +00:00
if ( isset ( $this -> controllerClass -> params [ 'data' ][ $this -> modelKey ][ $field [ 'name' ]]) &&
" on " == $this -> controllerClass -> params [ 'data' ][ $this -> modelKey ][ $field [ 'name' ]] )
2005-08-25 16:40:50 +00:00
{
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> params [ 'data' ][ $this -> modelKey ][ $field [ 'name' ]] = true ;
2005-08-25 16:40:50 +00:00
}
else
{
2005-09-17 02:22:07 +00:00
$this -> controllerClass -> params [ 'data' ][ $this -> modelKey ][ $field [ 'name' ]] = false ;
2005-08-25 16:40:50 +00:00
}
}
}
}
2005-08-21 06:49:02 +00:00
}
2005-07-21 04:02:32 +00:00
}
?>