2008-05-30 11:40:08 +00:00
< ? php
/**
* The View Tasks handles creating and updating view files .
*
2010-10-03 16:38:58 +00:00
* PHP 5
2008-05-30 11:40:08 +00:00
*
2009-11-06 06:46:59 +00:00
* CakePHP ( tm ) : Rapid Development Framework ( http :// cakephp . org )
2013-02-08 11:59:49 +00:00
* Copyright ( c ) Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2008-05-30 11:40:08 +00:00
*
* Licensed under The MIT License
2013-02-08 12:22:51 +00:00
* For full copyright and license information , please see the LICENSE . txt
2008-05-30 11:40:08 +00:00
* Redistributions of files must retain the above copyright notice .
*
2013-02-08 11:59:49 +00:00
* @ copyright Copyright ( c ) Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2009-11-06 06:00:11 +00:00
* @ link http :// cakephp . org CakePHP ( tm ) Project
2008-10-30 17:30:26 +00:00
* @ since CakePHP ( tm ) v 1.2
2009-11-06 06:51:51 +00:00
* @ license MIT License ( http :// www . opensource . org / licenses / mit - license . php )
2008-05-30 11:40:08 +00:00
*/
2010-12-08 00:20:56 +00:00
2011-11-25 21:05:00 +00:00
App :: uses ( 'AppShell' , 'Console/Command' );
2010-12-08 00:20:56 +00:00
App :: uses ( 'Controller' , 'Controller' );
App :: uses ( 'BakeTask' , 'Console/Command/Task' );
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Task class for creating and updating view files .
*
2011-07-26 06:16:14 +00:00
* @ package Cake . Console . Command . Task
2008-05-30 11:40:08 +00:00
*/
2010-03-05 01:52:25 +00:00
class ViewTask extends BakeTask {
2008-05-30 11:40:08 +00:00
/**
* Tasks to be loaded by this Task
*
* @ var array
*/
2010-04-04 07:14:00 +00:00
public $tasks = array ( 'Project' , 'Controller' , 'DbConfig' , 'Template' );
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
2011-04-17 10:09:32 +00:00
* path to View directory
2008-05-30 11:40:08 +00:00
*
* @ var array
*/
2011-04-17 10:09:32 +00:00
public $path = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Name of the controller being used
*
* @ var string
*/
2010-04-04 07:14:00 +00:00
public $controllerName = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* The template file to use
*
* @ var string
*/
2010-04-04 07:14:00 +00:00
public $template = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Actions to use for scaffolding
*
* @ var array
*/
2010-04-04 07:14:00 +00:00
public $scaffoldActions = array ( 'index' , 'view' , 'add' , 'edit' );
2009-07-24 19:18:37 +00:00
2010-02-13 22:51:49 +00:00
/**
2012-12-22 22:48:15 +00:00
* An array of action names that don ' t require templates . These
2010-02-13 22:51:49 +00:00
* actions will not emit errors when doing bakeActions ()
*
* @ var array
*/
2010-04-04 07:14:00 +00:00
public $noTemplateActions = array ( 'delete' );
2010-02-13 22:51:49 +00:00
2008-05-30 11:40:08 +00:00
/**
* Override initialize
*
2011-07-29 02:03:44 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
*/
2010-04-05 03:19:38 +00:00
public function initialize () {
2011-04-17 11:28:45 +00:00
$this -> path = current ( App :: path ( 'View' ));
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Execution method always used for tasks
*
2011-07-29 02:03:44 +00:00
* @ return mixed
2008-05-30 11:40:08 +00:00
*/
2010-04-05 03:19:38 +00:00
public function execute () {
2010-10-19 03:09:23 +00:00
parent :: execute ();
2008-05-30 11:40:08 +00:00
if ( empty ( $this -> args )) {
2010-04-24 01:57:59 +00:00
$this -> _interactive ();
2008-05-30 11:40:08 +00:00
}
2009-07-28 21:49:04 +00:00
if ( empty ( $this -> args [ 0 ])) {
return ;
}
if ( ! isset ( $this -> connection )) {
$this -> connection = 'default' ;
}
2011-04-22 19:37:31 +00:00
$action = null ;
2010-03-06 02:30:58 +00:00
$this -> controllerName = $this -> _controllerName ( $this -> args [ 0 ]);
2008-05-30 11:40:08 +00:00
2009-11-11 14:56:35 +00:00
$this -> Project -> interactive = false ;
2013-02-12 02:38:08 +00:00
if ( strtolower ( $this -> args [ 0 ]) === 'all' ) {
2009-07-28 21:49:04 +00:00
return $this -> all ();
}
2008-05-30 11:40:08 +00:00
2009-07-28 21:49:04 +00:00
if ( isset ( $this -> args [ 1 ])) {
$this -> template = $this -> args [ 1 ];
}
if ( isset ( $this -> args [ 2 ])) {
$action = $this -> args [ 2 ];
}
if ( ! $action ) {
$action = $this -> template ;
}
if ( $action ) {
return $this -> bake ( $action , true );
}
2009-06-04 03:18:14 +00:00
2011-08-20 04:43:34 +00:00
$vars = $this -> _loadController ();
2009-07-28 21:49:04 +00:00
$methods = $this -> _methodsToBake ();
2008-05-30 11:40:08 +00:00
2009-07-28 21:49:04 +00:00
foreach ( $methods as $method ) {
$content = $this -> getContent ( $method , $vars );
if ( $content ) {
$this -> bake ( $method , $content );
2008-05-30 11:40:08 +00:00
}
}
}
2009-07-24 19:18:37 +00:00
2009-07-17 03:12:47 +00:00
/**
* Get a list of actions that can / should have views baked for them .
*
* @ return array Array of action names that should be baked
2009-11-14 12:19:25 +00:00
*/
2010-04-24 01:57:59 +00:00
protected function _methodsToBake () {
2012-03-03 23:55:07 +00:00
$methods = array_diff (
2009-07-17 03:12:47 +00:00
array_map ( 'strtolower' , get_class_methods ( $this -> controllerName . 'Controller' )),
2011-03-07 05:15:00 +00:00
array_map ( 'strtolower' , get_class_methods ( 'AppController' ))
2009-07-17 03:12:47 +00:00
);
2009-07-28 21:49:04 +00:00
$scaffoldActions = false ;
2009-07-17 03:12:47 +00:00
if ( empty ( $methods )) {
2009-07-28 21:49:04 +00:00
$scaffoldActions = true ;
2009-07-17 03:12:47 +00:00
$methods = $this -> scaffoldActions ;
}
2009-10-07 05:35:29 +00:00
$adminRoute = $this -> Project -> getPrefix ();
2009-07-17 03:12:47 +00:00
foreach ( $methods as $i => $method ) {
2011-11-06 15:14:55 +00:00
if ( $adminRoute && ! empty ( $this -> params [ 'admin' ])) {
2009-07-28 21:49:04 +00:00
if ( $scaffoldActions ) {
2009-10-07 05:35:29 +00:00
$methods [ $i ] = $adminRoute . $method ;
2009-07-28 21:49:04 +00:00
continue ;
2009-10-07 05:35:29 +00:00
} elseif ( strpos ( $method , $adminRoute ) === false ) {
2009-07-28 21:49:04 +00:00
unset ( $methods [ $i ]);
}
2009-07-17 03:12:47 +00:00
}
2009-08-02 22:17:31 +00:00
if ( $method [ 0 ] === '_' || $method == strtolower ( $this -> controllerName . 'Controller' )) {
2009-07-17 03:12:47 +00:00
unset ( $methods [ $i ]);
}
}
return $methods ;
}
2009-07-24 19:18:37 +00:00
2009-04-28 03:55:34 +00:00
/**
* Bake All views for All controllers .
*
* @ return void
2009-11-14 12:19:25 +00:00
*/
2010-04-24 01:57:59 +00:00
public function all () {
2009-06-05 03:42:06 +00:00
$this -> Controller -> interactive = false ;
2009-06-04 03:18:14 +00:00
$tables = $this -> Controller -> listAll ( $this -> connection , false );
2009-07-29 10:48:31 +00:00
2010-02-10 02:13:20 +00:00
$actions = null ;
if ( isset ( $this -> args [ 1 ])) {
$actions = array ( $this -> args [ 1 ]);
}
2009-04-28 03:58:36 +00:00
$this -> interactive = false ;
2009-04-28 03:55:34 +00:00
foreach ( $tables as $table ) {
$model = $this -> _modelName ( $table );
$this -> controllerName = $this -> _controllerName ( $model );
2010-12-09 03:45:18 +00:00
App :: uses ( $model , 'Model' );
if ( class_exists ( $model )) {
2011-08-20 04:43:34 +00:00
$vars = $this -> _loadController ();
2010-02-10 02:13:20 +00:00
if ( ! $actions ) {
$actions = $this -> _methodsToBake ();
}
2009-06-05 03:42:06 +00:00
$this -> bakeActions ( $actions , $vars );
2010-05-02 23:28:18 +00:00
$actions = null ;
2009-04-28 03:55:34 +00:00
}
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Handles interactive baking
*
2011-07-29 02:03:44 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
*/
2010-04-24 01:57:59 +00:00
protected function _interactive () {
2008-05-30 11:40:08 +00:00
$this -> hr ();
2012-02-05 17:00:30 +00:00
$this -> out ( sprintf ( " Bake View \n Path: %s " , $this -> getPath ()));
2008-05-30 11:40:08 +00:00
$this -> hr ();
2009-06-04 01:59:46 +00:00
2010-03-09 02:18:53 +00:00
$this -> DbConfig -> interactive = $this -> Controller -> interactive = $this -> interactive = true ;
2009-06-04 03:18:14 +00:00
if ( empty ( $this -> connection )) {
$this -> connection = $this -> DbConfig -> getConfig ();
}
$this -> Controller -> connection = $this -> connection ;
2008-05-30 11:40:08 +00:00
$this -> controllerName = $this -> Controller -> getName ();
2012-11-21 14:39:03 +00:00
$prompt = __d ( 'cake_console' , " Would you like bake to build your views interactively? \n Warning: Choosing no will overwrite %s views if it exist. " , $this -> controllerName );
2009-06-04 03:18:14 +00:00
$interactive = $this -> in ( $prompt , array ( 'y' , 'n' ), 'n' );
2008-05-30 11:40:08 +00:00
2013-02-12 02:38:08 +00:00
if ( strtolower ( $interactive ) === 'n' ) {
2009-06-04 03:18:14 +00:00
$this -> interactive = false ;
2008-05-30 11:40:08 +00:00
}
2011-03-19 17:32:35 +00:00
$prompt = __d ( 'cake_console' , " Would you like to create some CRUD views \n (index, add, view, edit) for this controller? \n NOTE: Before doing so, you'll need to create your controller \n and model classes (including associated models). " );
2011-12-01 07:21:31 +00:00
$wannaDoScaffold = $this -> in ( $prompt , array ( 'y' , 'n' ), 'y' );
2009-06-04 03:18:14 +00:00
2011-12-01 07:21:31 +00:00
$wannaDoAdmin = $this -> in ( __d ( 'cake_console' , " Would you like to create the views for admin routing? " ), array ( 'y' , 'n' ), 'n' );
2008-05-30 11:40:08 +00:00
2013-02-12 02:38:08 +00:00
if ( strtolower ( $wannaDoScaffold ) === 'y' || strtolower ( $wannaDoAdmin ) === 'y' ) {
2011-08-20 04:43:34 +00:00
$vars = $this -> _loadController ();
2013-02-12 02:38:08 +00:00
if ( strtolower ( $wannaDoScaffold ) === 'y' ) {
2009-06-05 03:35:36 +00:00
$actions = $this -> scaffoldActions ;
2009-06-04 02:03:57 +00:00
$this -> bakeActions ( $actions , $vars );
2008-05-30 11:40:08 +00:00
}
2013-02-12 02:38:08 +00:00
if ( strtolower ( $wannaDoAdmin ) === 'y' ) {
2009-10-07 04:46:13 +00:00
$admin = $this -> Project -> getPrefix ();
2009-06-05 03:35:36 +00:00
$regularActions = $this -> scaffoldActions ;
$adminActions = array ();
foreach ( $regularActions as $action ) {
$adminActions [] = $admin . $action ;
}
$this -> bakeActions ( $adminActions , $vars );
}
2008-05-30 11:40:08 +00:00
$this -> hr ();
2009-09-26 21:08:37 +00:00
$this -> out ();
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , " View Scaffolding Complete. \n " ));
2008-05-30 11:40:08 +00:00
} else {
2009-06-04 03:18:14 +00:00
$this -> customAction ();
2008-05-30 11:40:08 +00:00
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Loads Controller and sets variables for the template
* Available template variables
* 'modelClass' , 'primaryKey' , 'displayField' , 'singularVar' , 'pluralVar' ,
* 'singularHumanName' , 'pluralHumanName' , 'fields' , 'foreignKeys' ,
* 'belongsTo' , 'hasOne' , 'hasMany' , 'hasAndBelongsToMany'
*
* @ return array Returns an variables to be made available to a view template
*/
2011-08-20 04:43:34 +00:00
protected function _loadController () {
2008-05-30 11:40:08 +00:00
if ( ! $this -> controllerName ) {
2011-03-19 17:32:35 +00:00
$this -> err ( __d ( 'cake_console' , 'Controller not found' ));
2008-05-30 11:40:08 +00:00
}
2010-12-09 03:45:18 +00:00
$plugin = null ;
2008-05-30 11:40:08 +00:00
if ( $this -> plugin ) {
2010-12-09 03:45:18 +00:00
$plugin = $this -> plugin . '.' ;
2008-05-30 11:40:08 +00:00
}
2010-12-09 03:45:18 +00:00
$controllerClassName = $this -> controllerName . 'Controller' ;
2011-03-07 05:15:00 +00:00
App :: uses ( $controllerClassName , $plugin . 'Controller' );
2010-12-09 03:45:18 +00:00
if ( ! class_exists ( $controllerClassName )) {
$file = $controllerClassName . '.php' ;
2011-03-19 17:32:35 +00:00
$this -> err ( __d ( 'cake_console' , " The file '%s' could not be found. \n In order to bake a view, you'll need to first create the controller. " , $file ));
2008-06-04 19:04:58 +00:00
$this -> _stop ();
2008-05-30 11:40:08 +00:00
}
2010-11-13 04:05:44 +00:00
$controllerObj = new $controllerClassName ();
2010-02-28 01:28:04 +00:00
$controllerObj -> plugin = $this -> plugin ;
2008-05-30 11:40:08 +00:00
$controllerObj -> constructClasses ();
$modelClass = $controllerObj -> modelClass ;
2010-11-13 04:05:44 +00:00
$modelObj = $controllerObj -> { $controllerObj -> modelClass };
2008-05-30 11:40:08 +00:00
if ( $modelObj ) {
$primaryKey = $modelObj -> primaryKey ;
$displayField = $modelObj -> displayField ;
$singularVar = Inflector :: variable ( $modelClass );
2010-05-07 01:38:14 +00:00
$singularHumanName = $this -> _singularHumanName ( $this -> controllerName );
2009-12-30 03:23:52 +00:00
$schema = $modelObj -> schema ( true );
2008-05-30 11:40:08 +00:00
$fields = array_keys ( $schema );
2011-08-20 04:43:34 +00:00
$associations = $this -> _associations ( $modelObj );
2008-05-30 11:40:08 +00:00
} else {
2010-02-28 01:28:04 +00:00
$primaryKey = $displayField = null ;
2008-05-30 11:40:08 +00:00
$singularVar = Inflector :: variable ( Inflector :: singularize ( $this -> controllerName ));
2009-08-18 08:29:54 +00:00
$singularHumanName = $this -> _singularHumanName ( $this -> controllerName );
2010-02-28 01:28:04 +00:00
$fields = $schema = $associations = array ();
2008-05-30 11:40:08 +00:00
}
2009-08-18 08:29:54 +00:00
$pluralVar = Inflector :: variable ( $this -> controllerName );
$pluralHumanName = $this -> _pluralHumanName ( $this -> controllerName );
2008-05-30 11:40:08 +00:00
return compact ( 'modelClass' , 'schema' , 'primaryKey' , 'displayField' , 'singularVar' , 'pluralVar' ,
2011-12-01 07:21:31 +00:00
'singularHumanName' , 'pluralHumanName' , 'fields' , 'associations' );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2009-06-04 01:59:46 +00:00
/**
* Bake a view file for each of the supplied actions
*
* @ param array $actions Array of actions to make files for .
2011-07-29 02:03:44 +00:00
* @ param array $vars
2009-06-04 01:59:46 +00:00
* @ return void
2009-11-14 12:19:25 +00:00
*/
2010-04-24 01:57:59 +00:00
public function bakeActions ( $actions , $vars ) {
2009-06-04 01:59:46 +00:00
foreach ( $actions as $action ) {
$content = $this -> getContent ( $action , $vars );
$this -> bake ( $action , $content );
}
}
2009-07-24 19:18:37 +00:00
2009-06-04 03:18:14 +00:00
/**
* handle creation of baking a custom action view file
*
* @ return void
2009-11-14 12:19:25 +00:00
*/
2010-04-24 01:57:59 +00:00
public function customAction () {
2009-06-04 03:18:14 +00:00
$action = '' ;
2012-09-14 17:42:25 +00:00
while ( ! $action ) {
2011-03-19 17:32:35 +00:00
$action = $this -> in ( __d ( 'cake_console' , 'Action Name? (use lowercase_underscored function name)' ));
2012-09-14 17:42:25 +00:00
if ( ! $action ) {
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'The action name you supplied was empty. Please try again.' ));
2009-06-04 03:18:14 +00:00
}
}
2009-09-26 21:08:37 +00:00
$this -> out ();
2009-06-04 03:18:14 +00:00
$this -> hr ();
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'The following view will be created:' ));
2009-06-04 03:18:14 +00:00
$this -> hr ();
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Controller Name: %s' , $this -> controllerName ));
$this -> out ( __d ( 'cake_console' , 'Action Name: %s' , $action ));
2011-11-05 18:54:21 +00:00
$this -> out ( __d ( 'cake_console' , 'Path: %s' , $this -> getPath () . $this -> controllerName . DS . Inflector :: underscore ( $action ) . " .ctp " ));
2009-06-04 03:18:14 +00:00
$this -> hr ();
2011-12-01 07:21:31 +00:00
$looksGood = $this -> in ( __d ( 'cake_console' , 'Look okay?' ), array ( 'y' , 'n' ), 'y' );
2013-02-12 02:38:08 +00:00
if ( strtolower ( $looksGood ) === 'y' ) {
2010-09-21 03:42:35 +00:00
$this -> bake ( $action , ' ' );
2009-06-04 03:18:14 +00:00
$this -> _stop ();
} else {
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Bake Aborted.' ));
2009-06-04 03:18:14 +00:00
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Assembles and writes bakes the view file .
*
* @ param string $action Action to bake
* @ param string $content Content to write
* @ return boolean Success
*/
2010-04-05 03:19:38 +00:00
public function bake ( $action , $content = '' ) {
2008-05-30 11:40:08 +00:00
if ( $content === true ) {
2009-06-05 03:35:36 +00:00
$content = $this -> getContent ( $action );
2008-05-30 11:40:08 +00:00
}
2010-08-24 01:49:30 +00:00
if ( empty ( $content )) {
return false ;
}
2011-04-25 19:17:59 +00:00
$this -> out ( " \n " . __d ( 'cake_console' , 'Baking `%s` view file...' , $action ), 1 , Shell :: QUIET );
2010-03-05 01:52:25 +00:00
$path = $this -> getPath ();
2011-05-22 18:42:43 +00:00
$filename = $path . $this -> controllerName . DS . Inflector :: underscore ( $action ) . '.ctp' ;
2009-06-07 01:18:34 +00:00
return $this -> createFile ( $filename , $content );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Builds content from template and variables
*
2009-09-23 15:16:18 +00:00
* @ param string $action name to generate content to
2008-05-30 11:40:08 +00:00
* @ param array $vars passed for use in templates
* @ return string content from template
*/
2010-04-05 03:19:38 +00:00
public function getContent ( $action , $vars = null ) {
2008-05-30 11:40:08 +00:00
if ( ! $vars ) {
2011-08-20 04:43:34 +00:00
$vars = $this -> _loadController ();
2008-05-30 11:40:08 +00:00
}
2009-05-30 04:32:57 +00:00
2009-06-04 02:03:57 +00:00
$this -> Template -> set ( 'action' , $action );
$this -> Template -> set ( 'plugin' , $this -> plugin );
2009-05-30 04:32:57 +00:00
$this -> Template -> set ( $vars );
2010-02-13 22:51:49 +00:00
$template = $this -> getTemplate ( $action );
if ( $template ) {
return $this -> Template -> generate ( 'views' , $template );
2008-05-30 11:40:08 +00:00
}
return false ;
}
2009-09-23 17:27:47 +00:00
2009-09-23 15:16:18 +00:00
/**
* Gets the template name based on the action name
*
* @ param string $action name
* @ return string template name
*/
2010-04-05 03:19:38 +00:00
public function getTemplate ( $action ) {
2010-02-13 22:51:49 +00:00
if ( $action != $this -> template && in_array ( $action , $this -> noTemplateActions )) {
return false ;
}
2009-09-23 15:16:18 +00:00
if ( ! empty ( $this -> template ) && $action != $this -> template ) {
return $this -> template ;
2011-04-17 10:09:32 +00:00
}
2011-11-07 00:01:56 +00:00
$themePath = $this -> Template -> getThemePath ();
if ( file_exists ( $themePath . 'views' . DS . $action . '.ctp' )) {
return $action ;
}
2009-09-23 15:16:18 +00:00
$template = $action ;
2009-10-07 05:35:29 +00:00
$prefixes = Configure :: read ( 'Routing.prefixes' );
foreach (( array ) $prefixes as $prefix ) {
if ( strpos ( $template , $prefix ) !== false ) {
$template = str_replace ( $prefix . '_' , '' , $template );
}
2009-09-23 15:16:18 +00:00
}
if ( in_array ( $template , array ( 'add' , 'edit' ))) {
$template = 'form' ;
} elseif ( preg_match ( '@(_add|_edit)$@' , $template )) {
$template = str_replace ( array ( '_add' , '_edit' ), '_form' , $template );
}
return $template ;
}
2009-07-24 19:18:37 +00:00
2010-10-13 02:36:51 +00:00
/**
* get the option parser for this task
*
* @ return ConsoleOptionParser
*/
public function getOptionParser () {
$parser = parent :: getOptionParser ();
return $parser -> description (
2011-03-19 17:32:35 +00:00
__d ( 'cake_console' , 'Bake views for a controller, using built-in or custom templates.' )
2010-10-13 02:36:51 +00:00
) -> addArgument ( 'controller' , array (
2012-12-22 22:48:15 +00:00
'help' => __d ( 'cake_console' , 'Name of the controller views to bake. Can be Plugin.name as a shortcut for plugin baking.' )
2010-10-13 02:36:51 +00:00
)) -> addArgument ( 'action' , array (
2011-03-19 17:32:35 +00:00
'help' => __d ( 'cake_console' , " Will bake a single action's file. core templates are (index, add, edit, view) " )
2010-10-13 02:36:51 +00:00
)) -> addArgument ( 'alias' , array (
2011-03-19 17:32:35 +00:00
'help' => __d ( 'cake_console' , 'Will bake the template in <action> but create the filename after <alias>.' )
2010-10-13 02:36:51 +00:00
)) -> addOption ( 'plugin' , array (
2010-10-13 03:06:07 +00:00
'short' => 'p' ,
2011-03-19 17:32:35 +00:00
'help' => __d ( 'cake_console' , 'Plugin to bake the view into.' )
2010-10-13 02:36:51 +00:00
)) -> addOption ( 'admin' , array (
2011-03-19 17:32:35 +00:00
'help' => __d ( 'cake_console' , 'Set to only bake views for a prefix in Routing.prefixes' ),
2010-10-13 02:36:51 +00:00
'boolean' => true
)) -> addOption ( 'connection' , array (
2010-10-13 03:06:07 +00:00
'short' => 'c' ,
2011-03-19 17:32:35 +00:00
'help' => __d ( 'cake_console' , 'The connection the connected model is on.' )
2010-10-13 02:36:51 +00:00
)) -> addSubcommand ( 'all' , array (
2011-03-19 17:32:35 +00:00
'help' => __d ( 'cake_console' , 'Bake all CRUD action views for all controllers. Requires models and controllers to exist.' )
)) -> epilog ( __d ( 'cake_console' , 'Omitting all arguments and options will enter into an interactive mode.' ));
2010-10-13 02:36:51 +00:00
}
2008-05-30 11:40:08 +00:00
/**
* Returns associations for controllers models .
*
2011-07-29 02:03:44 +00:00
* @ param Model $model
* @ return array $associations
2008-05-30 11:40:08 +00:00
*/
2012-02-23 14:06:25 +00:00
protected function _associations ( Model $model ) {
2008-06-20 20:17:23 +00:00
$keys = array ( 'belongsTo' , 'hasOne' , 'hasMany' , 'hasAndBelongsToMany' );
$associations = array ();
2008-05-30 11:40:08 +00:00
2012-12-20 12:47:03 +00:00
foreach ( $keys as $type ) {
2008-06-20 20:17:23 +00:00
foreach ( $model -> { $type } as $assocKey => $assocData ) {
2012-12-20 12:47:03 +00:00
list (, $modelClass ) = pluginSplit ( $assocData [ 'className' ]);
2008-06-20 20:17:23 +00:00
$associations [ $type ][ $assocKey ][ 'primaryKey' ] = $model -> { $assocKey } -> primaryKey ;
$associations [ $type ][ $assocKey ][ 'displayField' ] = $model -> { $assocKey } -> displayField ;
$associations [ $type ][ $assocKey ][ 'foreignKey' ] = $assocData [ 'foreignKey' ];
2011-10-13 01:09:19 +00:00
$associations [ $type ][ $assocKey ][ 'controller' ] = Inflector :: pluralize ( Inflector :: underscore ( $modelClass ));
2012-03-03 23:55:07 +00:00
$associations [ $type ][ $assocKey ][ 'fields' ] = array_keys ( $model -> { $assocKey } -> schema ( true ));
2008-06-20 20:17:23 +00:00
}
}
return $associations ;
}
2012-03-03 23:55:07 +00:00
2008-05-30 11:40:08 +00:00
}