2007-05-14 12:24:40 +00:00
< ? php
/* SVN FILE: $Id$ */
/**
* The ControllerTask handles creating and updating controller files .
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP ( tm ) : Rapid Development Framework < http :// www . cakephp . org />
2008-01-01 22:18:17 +00:00
* Copyright 2005 - 2008 , Cake Software Foundation , Inc .
2007-05-14 12:24:40 +00:00
* 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
2008-01-01 22:18:17 +00:00
* @ copyright Copyright 2005 - 2008 , Cake Software Foundation , Inc .
2007-05-14 12:24:40 +00:00
* @ link http :// www . cakefoundation . org / projects / info / cakephp CakePHP ( tm ) Project
* @ package cake
* @ subpackage cake . cake . console . libs . tasks
* @ since CakePHP ( tm ) v 1.2
* @ version $Revision $
* @ modifiedby $LastChangedBy $
* @ lastmodified $Date $
* @ license http :// www . opensource . org / licenses / mit - license . php The MIT License
*/
/**
* Task class for creating and updating controller files .
*
* @ package cake
* @ subpackage cake . cake . console . libs . tasks
*/
class ControllerTask extends Shell {
2007-06-03 23:56:33 +00:00
/**
* Tasks to be loaded by this Task
*
* @ var array
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-03 23:56:33 +00:00
*/
2007-06-05 15:37:06 +00:00
var $tasks = array ( 'Project' );
2007-12-21 02:54:45 +00:00
/**
* path to CONTROLLERS directory
*
* @ var array
* @ access public
*/
var $path = CONTROLLERS ;
2007-06-03 23:56:33 +00:00
/**
* Override initialize
*
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-03 23:56:33 +00:00
*/
2007-12-21 02:54:45 +00:00
function initialize () {
}
2007-06-03 23:56:33 +00:00
/**
* Execution method always used for tasks
*
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-03 23:56:33 +00:00
*/
2007-05-14 12:24:40 +00:00
function execute () {
2007-06-20 06:15:35 +00:00
if ( empty ( $this -> args )) {
2007-05-14 12:24:40 +00:00
$this -> __interactive ();
}
2007-06-03 23:56:33 +00:00
2007-06-20 06:15:35 +00:00
if ( isset ( $this -> args [ 0 ])) {
2007-06-03 23:56:33 +00:00
$controller = Inflector :: camelize ( $this -> args [ 0 ]);
$actions = null ;
2007-06-20 06:15:35 +00:00
if ( isset ( $this -> args [ 1 ]) && $this -> args [ 1 ] == 'scaffold' ) {
2007-06-03 23:56:33 +00:00
$this -> out ( 'Baking scaffold for ' . $controller );
2007-12-22 19:24:56 +00:00
$actions = $this -> bakeActions ( $controller );
2007-06-03 23:56:33 +00:00
} else {
$actions = 'scaffold' ;
}
2007-08-05 02:03:18 +00:00
if (( isset ( $this -> args [ 1 ]) && $this -> args [ 1 ] == 'admin' ) || ( isset ( $this -> args [ 2 ]) && $this -> args [ 2 ] == 'admin' )) {
2007-06-20 06:15:35 +00:00
if ( $admin = $this -> getAdmin ()) {
2007-08-25 15:29:44 +00:00
$this -> out ( 'Adding ' . Configure :: read ( 'Routing.admin' ) . ' methods' );
2007-06-05 17:06:00 +00:00
if ( $actions == 'scaffold' ) {
2007-12-22 19:24:56 +00:00
$actions = $this -> bakeActions ( $controller , $admin );
2007-06-05 17:06:00 +00:00
} else {
2007-12-22 19:24:56 +00:00
$actions .= $this -> bakeActions ( $controller , $admin );
2007-06-05 17:06:00 +00:00
}
2007-06-03 23:56:33 +00:00
}
}
2007-12-22 19:24:56 +00:00
if ( $this -> bake ( $controller , $actions )) {
2007-11-07 19:34:06 +00:00
if ( $this -> _checkUnitTest ()) {
2007-12-22 19:24:56 +00:00
$this -> bakeTest ( $controller );
2007-11-07 19:34:06 +00:00
}
}
2007-06-03 23:56:33 +00:00
}
2007-05-14 12:24:40 +00:00
}
2007-06-03 23:56:33 +00:00
/**
* Interactive
*
2007-10-22 05:52:20 +00:00
* @ access private
2007-06-03 23:56:33 +00:00
*/
2007-12-19 23:03:52 +00:00
function __interactive ( $controllerName = false ) {
if ( ! $controllerName ) {
$this -> interactive = false ;
$this -> hr ();
2007-12-21 02:54:45 +00:00
$this -> out ( sprintf ( " Bake Controller \n Path: %s " , $this -> path ));
2007-12-19 23:03:52 +00:00
$this -> hr ();
$actions = '' ;
$uses = array ();
$helpers = array ();
$components = array ();
$wannaUseSession = 'y' ;
$wannaDoAdmin = 'n' ;
$wannaUseScaffold = 'n' ;
$wannaDoScaffolding = 'y' ;
$controllerName = $this -> getName ();
}
2007-05-14 12:24:40 +00:00
$this -> hr ();
2007-12-19 23:03:52 +00:00
$this -> out ( " Baking { $controllerName } Controller " );
2007-05-14 12:24:40 +00:00
$this -> hr ();
2007-12-19 23:03:52 +00:00
2007-12-21 02:54:45 +00:00
$controllerFile = low ( Inflector :: underscore ( $controllerName ));
2007-12-19 23:03:52 +00:00
$question [] = __ ( " Would you like to build your controller interactively? " , true );
2007-12-21 02:54:45 +00:00
if ( file_exists ( $this -> path . $controllerFile . '_controller.php' )) {
2007-12-19 23:03:52 +00:00
$question [] = sprintf ( __ ( " Warning: Choosing no will overwrite the %sController. " , true ), $controllerName );
}
$doItInteractive = $this -> in ( join ( " \n " , $question ), array ( 'y' , 'n' ), 'y' );
2007-05-14 12:24:40 +00:00
if ( low ( $doItInteractive ) == 'y' || low ( $doItInteractive ) == 'yes' ) {
$this -> interactive = true ;
2007-12-19 23:03:52 +00:00
$wannaUseScaffold = $this -> in ( __ ( " Would you like to use scaffolding? " , true ), array ( 'y' , 'n' ), 'n' );
2007-05-14 12:24:40 +00:00
if ( low ( $wannaUseScaffold ) == 'n' || low ( $wannaUseScaffold ) == 'no' ) {
2007-12-19 23:03:52 +00:00
$wannaDoScaffolding = $this -> in ( __ ( " Would you like to include some basic class methods (index(), add(), view(), edit())? " , true ), array ( 'y' , 'n' ), 'n' );
2007-05-14 12:24:40 +00:00
if ( low ( $wannaDoScaffolding ) == 'y' || low ( $wannaDoScaffolding ) == 'yes' ) {
2007-12-19 23:03:52 +00:00
$wannaDoAdmin = $this -> in ( __ ( " Would you like to create the methods for admin routing? " , true ), array ( 'y' , 'n' ), 'n' );
2007-05-14 12:24:40 +00:00
}
2007-12-19 23:03:52 +00:00
$wannaDoHelpers = $this -> in ( __ ( " Would you like this controller to use other helpers besides HtmlHelper and FormHelper? " , true ), array ( 'y' , 'n' ), 'n' );
2007-05-14 12:24:40 +00:00
if ( low ( $wannaDoHelpers ) == 'y' || low ( $wannaDoHelpers ) == 'yes' ) {
2007-12-19 23:03:52 +00:00
$helpersList = $this -> in ( __ ( " Please provide a comma separated list of the other helper names you'd like to use. \n Example: 'Ajax, Javascript, Time' " , true ));
2007-05-14 12:24:40 +00:00
$helpersListTrimmed = str_replace ( ' ' , '' , $helpersList );
$helpers = explode ( ',' , $helpersListTrimmed );
}
2007-12-19 23:03:52 +00:00
$wannaDoComponents = $this -> in ( __ ( " Would you like this controller to use any components? " , true ), array ( 'y' , 'n' ), 'n' );
2007-05-14 12:24:40 +00:00
if ( low ( $wannaDoComponents ) == 'y' || low ( $wannaDoComponents ) == 'yes' ) {
2007-12-19 23:03:52 +00:00
$componentsList = $this -> in ( __ ( " Please provide a comma separated list of the component names you'd like to use. \n Example: 'Acl, Security, RequestHandler' " , true ));
2007-05-14 12:24:40 +00:00
$componentsListTrimmed = str_replace ( ' ' , '' , $componentsList );
$components = explode ( ',' , $componentsListTrimmed );
}
2007-12-19 23:03:52 +00:00
$wannaUseSession = $this -> in ( __ ( " Would you like to use Sessions? " , true ), array ( 'y' , 'n' ), 'y' );
2007-05-14 12:24:40 +00:00
} else {
$wannaDoScaffolding = 'n' ;
}
} else {
2007-12-19 23:03:52 +00:00
$wannaDoScaffolding = $this -> in ( __ ( " Would you like to include some basic class methods (index(), add(), view(), edit())? " , true ), array ( 'y' , 'n' ), 'y' );
2007-05-14 12:24:40 +00:00
if ( low ( $wannaDoScaffolding ) == 'y' || low ( $wannaDoScaffolding ) == 'yes' ) {
2007-12-19 23:03:52 +00:00
$wannaDoAdmin = $this -> in ( __ ( " Would you like to create the methods for admin routing? " , true ), array ( 'y' , 'n' ), 'y' );
2007-05-14 12:24:40 +00:00
}
}
2007-06-21 14:38:46 +00:00
$admin = false ;
2007-05-14 12:24:40 +00:00
if (( low ( $wannaDoAdmin ) == 'y' || low ( $wannaDoAdmin ) == 'yes' )) {
2007-06-03 23:56:33 +00:00
$admin = $this -> getAdmin ();
2007-05-14 12:24:40 +00:00
}
if ( low ( $wannaDoScaffolding ) == 'y' || low ( $wannaDoScaffolding ) == 'yes' ) {
2007-12-22 19:24:56 +00:00
$actions = $this -> bakeActions ( $controllerName , null , in_array ( low ( $wannaUseSession ), array ( 'y' , 'yes' )));
2007-06-20 06:15:35 +00:00
if ( $admin ) {
2007-12-22 19:24:56 +00:00
$actions .= $this -> bakeActions ( $controllerName , $admin , in_array ( low ( $wannaUseSession ), array ( 'y' , 'yes' )));
2007-05-14 12:24:40 +00:00
}
}
2007-06-20 06:15:35 +00:00
if ( $this -> interactive === true ) {
2007-05-14 12:24:40 +00:00
$this -> out ( '' );
$this -> hr ();
$this -> out ( 'The following controller will be created:' );
$this -> hr ();
2007-12-19 23:03:52 +00:00
$this -> out ( " Controller Name: $controllerName " );
2007-06-03 23:56:33 +00:00
2007-05-14 12:24:40 +00:00
if ( low ( $wannaUseScaffold ) == 'y' || low ( $wannaUseScaffold ) == 'yes' ) {
2007-12-19 23:03:52 +00:00
$this -> out ( " var \$ scaffold; " );
2007-06-03 23:56:33 +00:00
$actions = 'scaffold' ;
2007-05-14 12:24:40 +00:00
}
2007-06-20 06:15:35 +00:00
if ( count ( $helpers )) {
2007-12-19 23:03:52 +00:00
$this -> out ( " Helpers: " , false );
2007-05-14 12:24:40 +00:00
2007-06-20 06:15:35 +00:00
foreach ( $helpers as $help ) {
2007-05-14 12:24:40 +00:00
if ( $help != $helpers [ count ( $helpers ) - 1 ]) {
$this -> out ( ucfirst ( $help ) . " , " , false );
} else {
$this -> out ( ucfirst ( $help ));
}
}
}
2007-06-20 06:15:35 +00:00
if ( count ( $components )) {
2007-12-19 23:03:52 +00:00
$this -> out ( " Components: " , false );
2007-05-14 12:24:40 +00:00
2007-06-20 06:15:35 +00:00
foreach ( $components as $comp ) {
2007-05-14 12:24:40 +00:00
if ( $comp != $components [ count ( $components ) - 1 ]) {
$this -> out ( ucfirst ( $comp ) . " , " , false );
} else {
$this -> out ( ucfirst ( $comp ));
}
}
}
$this -> hr ();
2007-12-19 23:03:52 +00:00
$looksGood = $this -> in ( __ ( 'Look okay?' , true ), array ( 'y' , 'n' ), 'y' );
2007-05-14 12:24:40 +00:00
if ( low ( $looksGood ) == 'y' || low ( $looksGood ) == 'yes' ) {
2007-12-22 19:24:56 +00:00
$baked = $this -> bake ( $controllerName , $actions , $helpers , $components , $uses );
2007-05-14 12:24:40 +00:00
if ( $baked && $this -> _checkUnitTest ()) {
2007-12-22 19:24:56 +00:00
$this -> bakeTest ( $controllerName );
2007-05-14 12:24:40 +00:00
}
} else {
2007-12-19 23:03:52 +00:00
$this -> __interactive ( $controllerName );
2007-05-14 12:24:40 +00:00
}
} else {
2007-12-22 19:24:56 +00:00
$baked = $this -> bake ( $controllerName , $actions , $helpers , $components , $uses );
2007-05-14 12:24:40 +00:00
if ( $baked && $this -> _checkUnitTest ()) {
2007-12-22 19:24:56 +00:00
$this -> bakeTest ( $controllerName );
2007-05-14 12:24:40 +00:00
}
}
}
2007-10-22 05:52:20 +00:00
/**
* Bake scaffold actions
*
* @ param string $controllerName Controller name
* @ param string $admin Admin route to use
2007-10-22 16:54:36 +00:00
* @ param boolean $wannaUseSession Set to true to use sessions , false otherwise
2007-10-22 05:52:20 +00:00
* @ return string Baked actions
* @ access private
*/
2007-12-22 19:24:56 +00:00
function bakeActions ( $controllerName , $admin = null , $wannaUseSession = true ) {
2007-05-14 12:24:40 +00:00
$currentModelName = $this -> _modelName ( $controllerName );
2007-11-17 17:37:23 +00:00
if ( ! App :: import ( 'Model' , $currentModelName )) {
2007-12-31 22:27:53 +00:00
$this -> err ( __ ( 'You must have a model for this class to build scaffold methods. Please try again.' , true ));
2007-05-14 12:24:40 +00:00
exit ;
}
$actions = null ;
$modelObj =& new $currentModelName ();
$controllerPath = $this -> _controllerPath ( $controllerName );
$pluralName = $this -> _pluralName ( $currentModelName );
2007-11-02 06:43:17 +00:00
$singularName = Inflector :: variable ( $currentModelName );
$singularHumanName = Inflector :: humanize ( $currentModelName );
$pluralHumanName = Inflector :: humanize ( $controllerName );
2007-05-14 12:24:40 +00:00
$actions .= " \n " ;
$actions .= " \t function { $admin } index() { \n " ;
$actions .= " \t \t \$ this-> { $currentModelName } ->recursive = 0; \n " ;
$actions .= " \t \t \$ this->set(' { $pluralName } ', \$ this->paginate()); \n " ;
$actions .= " \t } \n " ;
$actions .= " \n " ;
$actions .= " \t function { $admin } view( \$ id = null) { \n " ;
2007-06-20 06:15:35 +00:00
$actions .= " \t \t if (! \$ id) { \n " ;
2007-10-22 05:52:20 +00:00
if ( $wannaUseSession ) {
2007-12-31 22:27:53 +00:00
$actions .= " \t \t \t \$ this->Session->setFlash(__('Invalid { $singularHumanName } .', true)); \n " ;
2007-12-23 01:17:48 +00:00
$actions .= " \t \t \t \$ this->redirect(array('action'=>'index')); \n " ;
2007-05-14 12:24:40 +00:00
} else {
2007-12-31 22:27:53 +00:00
$actions .= " \t \t \t \$ this->flash(__('Invalid { $singularHumanName } ', true), array('action'=>'index')); \n " ;
2007-05-14 12:24:40 +00:00
}
$actions .= " \t \t } \n " ;
$actions .= " \t \t \$ this->set(' " . $singularName . " ', \$ this-> { $currentModelName } ->read(null, \$ id)); \n " ;
$actions .= " \t } \n " ;
$actions .= " \n " ;
/* ADD ACTION */
$compact = array ();
$actions .= " \t function { $admin } add() { \n " ;
2007-06-20 06:15:35 +00:00
$actions .= " \t \t if (!empty( \$ this->data)) { \n " ;
2007-05-14 12:24:40 +00:00
$actions .= " \t \t \t \$ this-> { $currentModelName } ->create(); \n " ;
2007-06-20 06:15:35 +00:00
$actions .= " \t \t \t if ( \$ this-> { $currentModelName } ->save( \$ this->data)) { \n " ;
2007-10-22 05:52:20 +00:00
if ( $wannaUseSession ) {
2007-12-31 22:27:53 +00:00
$actions .= " \t \t \t \t \$ this->Session->setFlash(__('The " . $singularHumanName . " has been saved', true)); \n " ;
2007-12-23 01:17:48 +00:00
$actions .= " \t \t \t \t \$ this->redirect(array('action'=>'index')); \n " ;
2007-05-14 12:24:40 +00:00
} else {
2007-12-31 22:27:53 +00:00
$actions .= " \t \t \t \t \$ this->flash(__(' { $currentModelName } saved.', true), array('action'=>'index')); \n " ;
2007-05-26 12:13:00 +00:00
$actions .= " \t \t \t \t exit(); \n " ;
2007-05-14 12:24:40 +00:00
}
$actions .= " \t \t \t } else { \n " ;
2007-10-22 05:52:20 +00:00
if ( $wannaUseSession ) {
2007-12-31 22:27:53 +00:00
$actions .= " \t \t \t \t \$ this->Session->setFlash(__('The { $singularHumanName } could not be saved. Please, try again.', true)); \n " ;
2007-05-14 12:24:40 +00:00
}
$actions .= " \t \t \t } \n " ;
$actions .= " \t \t } \n " ;
2007-06-20 06:15:35 +00:00
foreach ( $modelObj -> hasAndBelongsToMany as $associationName => $relation ) {
if ( ! empty ( $associationName )) {
2007-05-14 12:24:40 +00:00
$habtmModelName = $this -> _modelName ( $associationName );
$habtmSingularName = $this -> _singularName ( $associationName );
$habtmPluralName = $this -> _pluralName ( $associationName );
2008-01-01 19:34:40 +00:00
$actions .= " \t \t \$ { $habtmPluralName } = \$ this-> { $currentModelName } -> { $habtmModelName } ->find('list'); \n " ;
2007-05-14 12:24:40 +00:00
$compact [] = " ' { $habtmPluralName } ' " ;
}
}
2007-06-20 06:15:35 +00:00
foreach ( $modelObj -> belongsTo as $associationName => $relation ) {
if ( ! empty ( $associationName )) {
2007-05-14 12:24:40 +00:00
$belongsToModelName = $this -> _modelName ( $associationName );
$belongsToPluralName = $this -> _pluralName ( $associationName );
2008-01-01 19:34:40 +00:00
$actions .= " \t \t \$ { $belongsToPluralName } = \$ this-> { $currentModelName } -> { $belongsToModelName } ->find('list'); \n " ;
2007-05-14 12:24:40 +00:00
$compact [] = " ' { $belongsToPluralName } ' " ;
}
}
2007-06-20 06:15:35 +00:00
if ( ! empty ( $compact )) {
2007-05-14 12:24:40 +00:00
$actions .= " \t \t \$ this->set(compact( " . join ( ', ' , $compact ) . " )); \n " ;
}
$actions .= " \t } \n " ;
$actions .= " \n " ;
/* EDIT ACTION */
$compact = array ();
$actions .= " \t function { $admin } edit( \$ id = null) { \n " ;
2007-06-20 06:15:35 +00:00
$actions .= " \t \t if (! \$ id && empty( \$ this->data)) { \n " ;
2007-10-22 05:52:20 +00:00
if ( $wannaUseSession ) {
2007-12-31 22:27:53 +00:00
$actions .= " \t \t \t \$ this->Session->setFlash(__('Invalid { $singularHumanName } ', true)); \n " ;
2007-12-23 01:17:48 +00:00
$actions .= " \t \t \t \$ this->redirect(array('action'=>'index')); \n " ;
2007-05-14 12:24:40 +00:00
} else {
2007-12-31 22:27:53 +00:00
$actions .= " \t \t \t \$ this->flash(__('Invalid { $singularHumanName } ', true), array('action'=>'index')); \n " ;
2007-05-14 12:24:40 +00:00
$actions .= " \t \t \t exit(); \n " ;
}
$actions .= " \t \t } \n " ;
2007-06-20 06:15:35 +00:00
$actions .= " \t \t if (!empty( \$ this->data)) { \n " ;
$actions .= " \t \t \t if ( \$ this-> { $currentModelName } ->save( \$ this->data)) { \n " ;
2007-10-22 05:52:20 +00:00
if ( $wannaUseSession ) {
2007-12-31 22:27:53 +00:00
$actions .= " \t \t \t \t \$ this->Session->setFlash(__('The " . $singularHumanName . " has been saved', true)); \n " ;
2007-12-23 01:17:48 +00:00
$actions .= " \t \t \t \t \$ this->redirect(array('action'=>'index')); \n " ;
2007-05-14 12:24:40 +00:00
} else {
2007-12-31 22:27:53 +00:00
$actions .= " \t \t \t \t \$ this->flash(__('The " . $singularHumanName . " has been saved.', true), array('action'=>'index')); \n " ;
2007-05-14 12:24:40 +00:00
$actions .= " \t \t \t \t exit(); \n " ;
}
$actions .= " \t \t \t } else { \n " ;
2007-10-22 05:52:20 +00:00
if ( $wannaUseSession ) {
2007-12-31 22:27:53 +00:00
$actions .= " \t \t \t \t \$ this->Session->setFlash(__('The { $singularHumanName } could not be saved. Please, try again.', true)); \n " ;
2007-05-14 12:24:40 +00:00
}
$actions .= " \t \t \t } \n " ;
$actions .= " \t \t } \n " ;
2007-06-20 06:15:35 +00:00
$actions .= " \t \t if (empty( \$ this->data)) { \n " ;
2007-05-14 12:24:40 +00:00
$actions .= " \t \t \t \$ this->data = \$ this-> { $currentModelName } ->read(null, \$ id); \n " ;
$actions .= " \t \t } \n " ;
2007-06-20 06:15:35 +00:00
foreach ( $modelObj -> hasAndBelongsToMany as $associationName => $relation ) {
if ( ! empty ( $associationName )) {
2007-05-14 12:24:40 +00:00
$habtmModelName = $this -> _modelName ( $associationName );
$habtmSingularName = $this -> _singularName ( $associationName );
$habtmPluralName = $this -> _pluralName ( $associationName );
2008-01-01 19:34:40 +00:00
$actions .= " \t \t \$ { $habtmPluralName } = \$ this-> { $currentModelName } -> { $habtmModelName } ->find('list'); \n " ;
2007-05-14 12:24:40 +00:00
$compact [] = " ' { $habtmPluralName } ' " ;
}
}
2007-06-20 06:15:35 +00:00
foreach ( $modelObj -> belongsTo as $associationName => $relation ) {
if ( ! empty ( $associationName )) {
2007-05-14 12:24:40 +00:00
$belongsToModelName = $this -> _modelName ( $associationName );
$belongsToPluralName = $this -> _pluralName ( $associationName );
2008-01-01 19:34:40 +00:00
$actions .= " \t \t \$ { $belongsToPluralName } = \$ this-> { $currentModelName } -> { $belongsToModelName } ->find('list'); \n " ;
2007-05-14 12:24:40 +00:00
$compact [] = " ' { $belongsToPluralName } ' " ;
}
}
2007-06-20 06:15:35 +00:00
if ( ! empty ( $compact )) {
2007-05-14 12:24:40 +00:00
$actions .= " \t \t \$ this->set(compact( " . join ( ',' , $compact ) . " )); \n " ;
}
$actions .= " \t } \n " ;
$actions .= " \n " ;
$actions .= " \t function { $admin } delete( \$ id = null) { \n " ;
2007-06-20 06:15:35 +00:00
$actions .= " \t \t if (! \$ id) { \n " ;
2007-10-22 05:52:20 +00:00
if ( $wannaUseSession ) {
2007-12-31 22:27:53 +00:00
$actions .= " \t \t \t \$ this->Session->setFlash(__('Invalid id for { $singularHumanName } ', true)); \n " ;
2007-12-23 01:17:48 +00:00
$actions .= " \t \t \t \$ this->redirect(array('action'=>'index')); \n " ;
2007-05-14 12:24:40 +00:00
} else {
2007-12-31 22:27:53 +00:00
$actions .= " \t \t \t \$ this->flash(__('Invalid { $singularHumanName } ', true), array('action'=>'index')); \n " ;
2007-05-14 12:24:40 +00:00
}
$actions .= " \t \t } \n " ;
2007-06-20 06:15:35 +00:00
$actions .= " \t \t if ( \$ this-> { $currentModelName } ->del( \$ id)) { \n " ;
2007-10-22 05:52:20 +00:00
if ( $wannaUseSession ) {
2007-12-31 22:27:53 +00:00
$actions .= " \t \t \t \$ this->Session->setFlash(__(' { $singularHumanName } deleted', true)); \n " ;
2007-12-23 01:17:48 +00:00
$actions .= " \t \t \t \$ this->redirect(array('action'=>'index')); \n " ;
2007-05-14 12:24:40 +00:00
} else {
2007-12-31 22:27:53 +00:00
$actions .= " \t \t \t \$ this->flash(__(' { $singularHumanName } deleted', true), array('action'=>'index')); \n " ;
2007-05-14 12:24:40 +00:00
}
$actions .= " \t \t } \n " ;
$actions .= " \t } \n " ;
$actions .= " \n " ;
return $actions ;
2007-06-03 23:56:33 +00:00
}
2007-05-14 12:24:40 +00:00
/**
2007-10-22 05:52:20 +00:00
* Assembles and writes a Controller file
2007-05-14 12:24:40 +00:00
*
2007-10-22 05:52:20 +00:00
* @ param string $controllerName Controller name
* @ param string $actions Actions to add , or set the whole controller to use $scaffold ( set $actions to 'scaffold' )
* @ param array $helpers Helpers to use in controller
* @ param array $components Components to use in controller
* @ param array $uses Models to use in controller
* @ return string Baked controller
* @ access private
2007-05-14 12:24:40 +00:00
*/
2007-12-22 19:24:56 +00:00
function bake ( $controllerName , $actions = '' , $helpers = null , $components = null , $uses = null ) {
2007-05-14 12:24:40 +00:00
$out = " <?php \n " ;
$out .= " class $controllerName " . " Controller extends AppController { \n \n " ;
$out .= " \t var \$ name = ' $controllerName '; \n " ;
2007-05-26 12:13:00 +00:00
2007-06-20 06:15:35 +00:00
if ( low ( $actions ) == 'scaffold' ) {
2007-05-26 12:13:00 +00:00
$out .= " \t var \$ scaffold; \n " ;
2007-05-14 12:24:40 +00:00
} else {
if ( count ( $uses )) {
$out .= " \t var \$ uses = array(' " . $this -> _modelName ( $controllerName ) . " ', " ;
2007-06-20 06:15:35 +00:00
foreach ( $uses as $use ) {
2007-05-14 12:24:40 +00:00
if ( $use != $uses [ count ( $uses ) - 1 ]) {
$out .= " ' " . $this -> _modelName ( $use ) . " ', " ;
} else {
$out .= " ' " . $this -> _modelName ( $use ) . " ' " ;
}
}
$out .= " ); \n " ;
}
2007-12-19 23:03:52 +00:00
$out .= " \t var \$ helpers = array('Html', 'Form' " ;
2007-05-26 12:13:00 +00:00
if ( count ( $helpers )) {
2007-06-20 06:15:35 +00:00
foreach ( $helpers as $help ) {
2007-12-19 23:03:52 +00:00
$out .= " , ' " . Inflector :: camelize ( $help ) . " ' " ;
2007-05-14 12:24:40 +00:00
}
2007-05-26 12:13:00 +00:00
}
$out .= " ); \n " ;
2007-05-14 12:24:40 +00:00
if ( count ( $components )) {
$out .= " \t var \$ components = array( " ;
2007-06-20 06:15:35 +00:00
foreach ( $components as $comp ) {
2007-05-14 12:24:40 +00:00
if ( $comp != $components [ count ( $components ) - 1 ]) {
$out .= " ' " . Inflector :: camelize ( $comp ) . " ', " ;
} else {
$out .= " ' " . Inflector :: camelize ( $comp ) . " ' " ;
}
}
$out .= " ); \n " ;
}
2007-06-03 23:56:33 +00:00
$out .= $actions ;
2007-05-14 12:24:40 +00:00
}
$out .= " } \n " ;
$out .= " ?> " ;
2007-12-21 02:54:45 +00:00
$filename = $this -> path . $this -> _controllerPath ( $controllerName ) . '_controller.php' ;
2007-05-14 12:24:40 +00:00
return $this -> createFile ( $filename , $out );
}
/**
2007-10-22 05:52:20 +00:00
* Assembles and writes a unit test file
2007-05-14 12:24:40 +00:00
*
2007-10-22 05:52:20 +00:00
* @ param string $className Controller class name
* @ return string Baked test
* @ access private
2007-05-14 12:24:40 +00:00
*/
2007-12-22 19:24:56 +00:00
function bakeTest ( $className ) {
2008-01-07 13:35:14 +00:00
$out = " App::import('Controller', ' $className '); \n \n " ;
$out .= " class Test { $className } extends { $className } Controller { \n " ;
$out .= " \t var \$ autoRender = false; \n } \n \n " ;
$out .= " class { $className } ControllerTest extends CakeTestCase { \n " ;
$out .= " \t var \$ { $className } = null; \n \n " ;
$out .= " \t function setUp() { \n \t \t \$ this-> { $className } = new Test { $className } (); \n \t } \n \n " ;
$out .= " \t function test { $className } ControllerInstance() { \n " ;
$out .= " \t \t \$ this->assertTrue(is_a( \$ this-> { $className } , ' { $className } Controller')); \n \t } \n \n " ;
$out .= " \t function tearDown() { \n \t \t unset( \$ this-> { $className } ); \n \t } \n } \n " ;
2007-05-14 12:24:40 +00:00
$path = CONTROLLER_TESTS ;
2007-11-07 19:26:31 +00:00
$filename = Inflector :: underscore ( $className ) . '_controller.test.php' ;
2008-01-07 13:35:14 +00:00
$this -> out ( " \n Baking unit test for $className ... " );
2007-05-14 12:24:40 +00:00
2008-01-07 13:35:14 +00:00
$header = '$Id' ;
$content = " <?php \n /* SVN FILE: $header $ */ \n /* " . $className . " Controller Test cases generated on: " . date ( 'Y-m-d H:m:s' ) . " : " . time () . " */ \n { $out } ?> " ;
return $this -> createFile ( $path . $filename , $content );
2007-05-14 12:24:40 +00:00
}
/**
2007-10-22 05:52:20 +00:00
* Outputs and gets the list of possible models or controllers from database
2007-05-14 12:24:40 +00:00
*
2007-10-22 05:52:20 +00:00
* @ param string $useDbConfig Database configuration name
* @ return array Set of controllers
* @ access public
2007-05-14 12:24:40 +00:00
*/
2007-06-01 17:34:42 +00:00
function listAll ( $useDbConfig = 'default' ) {
2007-05-14 12:24:40 +00:00
$db =& ConnectionManager :: getDataSource ( $useDbConfig );
$usePrefix = empty ( $db -> config [ 'prefix' ]) ? '' : $db -> config [ 'prefix' ];
if ( $usePrefix ) {
$tables = array ();
foreach ( $db -> listSources () as $table ) {
if ( ! strncmp ( $table , $usePrefix , strlen ( $usePrefix ))) {
$tables [] = substr ( $table , strlen ( $usePrefix ));
}
}
} else {
$tables = $db -> listSources ();
}
$this -> __tables = $tables ;
$this -> out ( 'Possible Models based on your current database:' );
$this -> _controllerNames = array ();
$count = count ( $tables );
for ( $i = 0 ; $i < $count ; $i ++ ) {
$this -> _controllerNames [] = $this -> _controllerName ( $this -> _modelName ( $tables [ $i ]));
$this -> out ( $i + 1 . " . " . $this -> _controllerNames [ $i ]);
}
2007-06-03 23:56:33 +00:00
return $this -> _controllerNames ;
2007-05-26 09:00:54 +00:00
}
/**
* Forces the user to specify the controller he wants to bake , and returns the selected controller name .
*
2007-10-22 05:52:20 +00:00
* @ return string Controller name
* @ access public
2007-05-26 09:00:54 +00:00
*/
2007-06-03 23:56:33 +00:00
function getName () {
2007-05-26 09:00:54 +00:00
$useDbConfig = 'default' ;
2007-06-03 23:56:33 +00:00
$controllers = $this -> listAll ( $useDbConfig , 'Controllers' );
2007-05-26 09:00:54 +00:00
$enteredController = '' ;
while ( $enteredController == '' ) {
$enteredController = $this -> in ( 'Enter a number from the list above, or type in the name of another controller.' );
2007-06-03 23:56:33 +00:00
if ( $enteredController == '' || intval ( $enteredController ) > count ( $controllers )) {
2007-05-26 09:00:54 +00:00
$this -> out ( 'Error:' );
$this -> out ( " The Controller name you supplied was empty, or the number \n you selected was not an option. Please try again. " );
$enteredController = '' ;
}
}
2007-06-03 23:56:33 +00:00
if ( intval ( $enteredController ) > 0 && intval ( $enteredController ) <= count ( $controllers ) ) {
$controllerName = $controllers [ intval ( $enteredController ) - 1 ];
2007-05-26 09:00:54 +00:00
} else {
$controllerName = Inflector :: camelize ( $enteredController );
}
2007-06-03 23:56:33 +00:00
2007-05-26 09:00:54 +00:00
return $controllerName ;
}
2007-06-08 08:44:34 +00:00
/**
* Displays help contents
*
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-08 08:44:34 +00:00
*/
function help () {
$this -> hr ();
$this -> out ( " Usage: cake bake controller <arg1> <arg2>... " );
$this -> hr ();
$this -> out ( 'Commands:' );
$this -> out ( " \n \t controller <name> \n \t \t bakes controller with var \$ scaffold " );
$this -> out ( " \n \t controller <name> scaffold \n \t \t bakes controller with scaffold actions. \n \t \t (index, view, add, edit, delete) " );
2007-08-25 15:29:44 +00:00
$this -> out ( " \n \t controller <name> scaffold admin \n \t \t bakes a controller with scaffold actions for both public and Configure::read('Routing.admin') " );
$this -> out ( " \n \t controller <name> admin \n \t \t bakes a controller with scaffold actions only for Configure::read('Routing.admin') " );
2007-06-08 08:44:34 +00:00
$this -> out ( " " );
exit ();
2007-06-21 14:38:46 +00:00
}
}
2007-12-19 23:03:52 +00:00
?>