2007-12-19 23:03:52 +00:00
< ? php
2007-05-14 12:24:40 +00:00
/* SVN FILE: $Id$ */
/**
* The View Tasks handles creating and updating view files .
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP ( tm ) : Rapid Development Framework < http :// www . cakephp . org />
* Copyright 2005 - 2007 , Cake Software Foundation , Inc .
* 1785 E . Sahara Avenue , Suite 490 - 204
* Las Vegas , Nevada 89104
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice .
*
* @ filesource
* @ copyright Copyright 2005 - 2007 , Cake Software Foundation , Inc .
* @ 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
*/
2007-08-18 15:13:54 +00:00
uses ( 'controller' . DS . 'controller' );
2007-05-14 12:24:40 +00:00
/**
* Task class for creating and updating view files .
*
* @ package cake
* @ subpackage cake . cake . console . libs . tasks
*/
2007-06-03 23:56:56 +00:00
class ViewTask extends Shell {
/**
* Tasks to be loaded by this Task
*
* @ var array
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-06 07:31:55 +00:00
*/
2007-06-03 23:56:56 +00:00
var $tasks = array ( 'Project' , 'Controller' );
/**
2007-10-22 05:52:20 +00:00
* Name of the controller being used
2007-06-03 23:56:56 +00:00
*
* @ var string
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-03 23:56:56 +00:00
*/
var $controllerName = null ;
/**
2007-10-22 05:52:20 +00:00
* Path to controller to put views
2007-06-03 23:56:56 +00:00
*
* @ var string
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-03 23:56:56 +00:00
*/
var $controllerPath = null ;
/**
2007-10-22 05:52:20 +00:00
* The template file to use
2007-06-03 23:56:56 +00:00
*
* @ var string
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-03 23:56:56 +00:00
*/
var $template = null ;
/**
* Actions to use for scaffolding
*
* @ var array
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-03 23:56:56 +00:00
*/
var $scaffoldActions = array ( 'index' , 'view' , 'add' , 'edit' );
/**
* Override initialize
*
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-03 23:56:56 +00:00
*/
2007-10-22 05:52:20 +00:00
function initialize () {
}
2007-06-03 23:56:56 +00:00
/**
* Execution method always used for tasks
*
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-03 23:56:56 +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-20 06:15:35 +00:00
if ( isset ( $this -> args [ 0 ])) {
2007-06-21 14:38:46 +00:00
$controller = $action = $alias = null ;
2007-06-03 23:56:56 +00:00
$this -> controllerName = Inflector :: camelize ( $this -> args [ 0 ]);
$this -> controllerPath = Inflector :: underscore ( $this -> controllerName );
2007-06-21 14:38:46 +00:00
if ( isset ( $this -> args [ 1 ])) {
$this -> template = $this -> args [ 1 ];
}
2007-06-03 23:56:56 +00:00
2007-06-21 14:38:46 +00:00
if ( isset ( $this -> args [ 2 ])) {
$action = $this -> args [ 2 ];
}
2007-06-03 23:56:56 +00:00
2007-06-21 14:38:46 +00:00
if ( ! $action ) {
$action = $this -> template ;
}
2007-06-03 23:56:56 +00:00
2007-06-21 14:38:46 +00:00
if ( in_array ( $action , $this -> scaffoldActions )) {
$this -> bake ( $action , true );
} elseif ( $action ) {
$this -> bake ( $action , true );
} else {
$vars = $this -> __loadController ();
if ( $vars ) {
2007-08-27 20:54:11 +00:00
$protected = array_map ( 'strtolower' , get_class_methods ( 'appcontroller' ));
2007-06-21 14:38:46 +00:00
$classVars = get_class_vars ( $this -> controllerName . 'Controller' );
if ( array_key_exists ( 'scaffold' , $classVars )) {
$methods = $this -> scaffoldActions ;
} else {
$methods = get_class_methods ( $this -> controllerName . 'Controller' );
}
$adminDelete = null ;
2007-08-25 15:29:44 +00:00
$adminRoute = Configure :: read ( 'Routing.admin' );
if ( ! empty ( $adminRoute )) {
$adminDelete = $adminRoute . '_delete' ;
2007-06-21 14:38:46 +00:00
}
foreach ( $methods as $method ) {
if ( $method { 0 } != '_' && ! in_array ( low ( $method ), am ( $protected , array ( 'delete' , $adminDelete )))) {
$content = $this -> getContent ( $method , $vars );
$this -> bake ( $method , $content );
}
2007-06-03 23:56:56 +00:00
}
}
}
}
}
/**
* Handles interactive baking
*
* @ access private
*/
2007-05-14 12:24:40 +00:00
function __interactive () {
$this -> hr ();
$this -> out ( 'View Bake:' );
$this -> hr ();
2007-06-03 23:56:56 +00:00
$wannaDoAdmin = 'n' ;
2007-05-14 12:24:40 +00:00
$wannaDoScaffold = 'y' ;
2007-06-21 14:38:46 +00:00
$this -> interactive = false ;
2007-05-14 12:24:40 +00:00
2007-06-03 23:56:56 +00:00
$this -> controllerName = $this -> Controller -> getName ();
$this -> controllerPath = low ( Inflector :: underscore ( $this -> controllerName ));
2007-05-14 12:24:40 +00:00
2007-06-03 23:56:56 +00:00
$interactive = $this -> in ( " Would you like bake to build your views interactively? \n Warning: Choosing no will overwrite { $this -> controllerName } views if it exist. " , array ( 'y' , 'n' ), 'y' );
2007-05-14 12:24:40 +00:00
2007-06-03 23:56:56 +00:00
if ( low ( $interactive ) == 'y' || low ( $interactive ) == 'yes' ) {
2007-06-21 14:38:46 +00:00
$this -> interactive = true ;
2007-05-14 12:24:40 +00:00
$wannaDoScaffold = $this -> in ( " Would you like to create some scaffolded views (index, add, view, edit) for this controller? \n NOTE: Before doing so, you'll need to create your controller and model classes (including associated models). " , array ( 'y' , 'n' ), 'n' );
}
if ( low ( $wannaDoScaffold ) == 'y' || low ( $wannaDoScaffold ) == 'yes' ) {
$wannaDoAdmin = $this -> in ( " Would you like to create the views for admin routing? " , array ( 'y' , 'n' ), 'y' );
}
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-21 14:38:46 +00:00
$admin = $this -> getAdmin ();
2007-05-14 12:24:40 +00:00
}
2007-06-21 14:38:46 +00:00
2007-05-14 12:24:40 +00:00
if ( low ( $wannaDoScaffold ) == 'y' || low ( $wannaDoScaffold ) == 'yes' ) {
2007-08-03 03:11:01 +00:00
$actions = $this -> scaffoldActions ;
2007-06-20 06:15:35 +00:00
if ( $admin ) {
2007-08-03 03:11:01 +00:00
foreach ( $actions as $action ) {
$actions [] = $admin . $action ;
2007-05-14 12:24:40 +00:00
}
}
2007-06-03 23:56:56 +00:00
$vars = $this -> __loadController ();
2007-06-20 06:15:35 +00:00
if ( $vars ) {
2007-08-03 03:11:01 +00:00
foreach ( $actions as $action ) {
2007-06-03 23:56:56 +00:00
$content = $this -> getContent ( $action , $vars );
$this -> bake ( $action , $content );
}
}
$this -> hr ();
$this -> out ( '' );
$this -> out ( 'View Scaffolding Complete.' . " \n " );
2007-05-14 12:24:40 +00:00
} else {
2007-06-03 23:56:56 +00:00
$action = '' ;
while ( $action == '' ) {
$action = $this -> in ( 'Action Name? (use camelCased function name)' );
if ( $action == '' ) {
2007-05-14 12:24:40 +00:00
$this -> out ( 'The action name you supplied was empty. Please try again.' );
}
}
$this -> out ( '' );
$this -> hr ();
$this -> out ( 'The following view will be created:' );
$this -> hr ();
2007-06-03 23:56:56 +00:00
$this -> out ( " Controller Name: { $this -> controllerName } " );
$this -> out ( " Action Name: { $action } " );
$this -> out ( " Path: " . $this -> params [ 'app' ] . DS . $this -> controllerPath . DS . Inflector :: underscore ( $action ) . " .ctp " );
2007-05-14 12:24:40 +00:00
$this -> hr ();
$looksGood = $this -> in ( 'Look okay?' , array ( 'y' , 'n' ), 'y' );
if ( low ( $looksGood ) == 'y' || low ( $looksGood ) == 'yes' ) {
2007-06-03 23:56:56 +00:00
$this -> bake ( $action );
exit ();
2007-05-14 12:24:40 +00:00
} else {
$this -> out ( 'Bake Aborted.' );
2007-06-03 23:56:56 +00:00
exit ();
2007-05-14 12:24:40 +00:00
}
}
}
2007-06-03 23:56:56 +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
2007-10-22 05:52:20 +00:00
* @ access private
2007-06-03 23:56:56 +00:00
*/
function __loadController () {
2007-06-20 06:15:35 +00:00
if ( ! $this -> controllerName ) {
2007-06-03 23:56:56 +00:00
$this -> err ( 'could not find the controller' );
2007-05-14 12:24:40 +00:00
}
2007-08-25 18:00:19 +00:00
2007-08-18 15:13:54 +00:00
$controllerClassName = $this -> controllerName . 'Controller' ;
2007-11-25 18:49:41 +00:00
if ( ! class_exists ( $this -> controllerName . 'Controller' ) && ! App :: import ( 'Controller' , $this -> controllerName )) {
2007-06-03 23:56:56 +00:00
$file = CONTROLLERS . $this -> controllerPath . '_controller.php' ;
$shortPath = $this -> shortPath ( $file );
$this -> err ( " The file ' { $shortPath } ' could not be found. \n In order to bake a view, you'll need to first create the controller. " );
2007-06-05 08:49:36 +00:00
exit ();
2007-05-14 12:24:40 +00:00
}
2007-06-03 23:56:56 +00:00
$controllerObj = & new $controllerClassName ();
2007-05-14 12:24:40 +00:00
$controllerObj -> constructClasses ();
2007-10-29 03:14:07 +00:00
$modelClass = $controllerObj -> modelClass ;
$modelObj =& ClassRegistry :: getObject ( $controllerObj -> modelKey );
$primaryKey = $modelObj -> primaryKey ;
$displayField = $modelObj -> displayField ;
$singularVar = Inflector :: variable ( $modelClass );
$pluralVar = Inflector :: variable ( $this -> controllerName );
$singularHumanName = Inflector :: humanize ( $modelClass );
$pluralHumanName = Inflector :: humanize ( $this -> controllerName );
2007-10-28 04:18:18 +00:00
$fields = array_keys ( $modelObj -> schema ());
2007-10-29 03:04:08 +00:00
$associations = $this -> __associations ( $modelObj );
2007-06-03 23:56:56 +00:00
2007-10-29 03:14:07 +00:00
return compact ( 'modelClass' , 'primaryKey' , 'displayField' , 'singularVar' , 'pluralVar' ,
'singularHumanName' , 'pluralHumanName' , 'fields' , 'associations' );
2007-05-14 12:24:40 +00:00
}
/**
2007-06-03 23:56:56 +00:00
* Assembles and writes bakes the view file .
2007-05-14 12:24:40 +00:00
*
2007-10-22 05:52:20 +00:00
* @ param string $action Action to bake
* @ param string $content Content to write
2007-10-22 16:09:35 +00:00
* @ return boolean Success
2007-10-22 05:52:20 +00:00
* @ access public
2007-05-14 12:24:40 +00:00
*/
2007-06-03 23:56:56 +00:00
function bake ( $action , $content = '' ) {
2007-06-20 06:15:35 +00:00
if ( $content === true ) {
2007-06-03 23:56:56 +00:00
$content = $this -> getContent ();
2007-05-14 12:24:40 +00:00
}
2007-06-03 23:56:56 +00:00
$filename = VIEWS . $this -> controllerPath . DS . Inflector :: underscore ( $action ) . '.ctp' ;
$Folder =& new Folder ( VIEWS . $this -> controllerPath , true );
$errors = $Folder -> errors ();
2007-06-20 06:15:35 +00:00
if ( empty ( $errors )) {
2007-06-03 23:56:56 +00:00
$path = $Folder -> slashTerm ( $Folder -> pwd ());
return $this -> createFile ( $filename , $content );
2007-05-14 12:24:40 +00:00
} else {
2007-06-20 06:15:35 +00:00
foreach ( $errors as $error ) {
2007-06-03 23:56:56 +00:00
$this -> err ( $error );
2007-05-14 12:24:40 +00:00
}
}
2007-06-03 23:56:56 +00:00
return false ;
2007-05-14 12:24:40 +00:00
}
/**
2007-06-03 23:56:56 +00:00
* Builds content from template and variables
2007-05-14 12:24:40 +00:00
*
2007-06-03 23:56:56 +00:00
* @ param string $template file to use
* @ param array $vars passed for use in templates
* @ return string content from template
2007-10-22 05:52:20 +00:00
* @ access public
2007-05-14 12:24:40 +00:00
*/
2007-06-03 23:56:56 +00:00
function getContent ( $template = null , $vars = null ) {
2007-06-20 06:15:35 +00:00
if ( ! $template ) {
2007-06-03 23:56:56 +00:00
$template = $this -> template ;
}
$action = $template ;
2007-06-06 07:31:55 +00:00
2007-08-25 15:29:44 +00:00
$adminRoute = Configure :: read ( 'Routing.admin' );
if ( ! empty ( $adminRoute ) && strpos ( $template , $adminRoute ) !== false ) {
$template = str_replace ( $adminRoute . '_' , '' , $template );
2007-06-06 07:31:55 +00:00
}
2007-06-20 06:15:35 +00:00
if ( in_array ( $template , array ( 'add' , 'edit' ))) {
2007-06-03 23:56:56 +00:00
$action = $template ;
$template = 'form' ;
}
$loaded = false ;
2007-06-20 06:15:35 +00:00
foreach ( $this -> Dispatch -> shellPaths as $path ) {
2007-06-03 23:56:56 +00:00
$templatePath = $path . 'templates' . DS . 'views' . DS . Inflector :: underscore ( $template ) . '.ctp' ;
if ( file_exists ( $templatePath ) && is_file ( $templatePath )) {
$loaded = true ;
break ;
2007-05-14 12:24:40 +00:00
}
}
2007-06-20 06:15:35 +00:00
if ( ! $vars ) {
2007-06-03 23:56:56 +00:00
$vars = $this -> __loadController ();
}
2007-06-20 06:15:35 +00:00
if ( $loaded ) {
2007-06-03 23:56:56 +00:00
extract ( $vars );
ob_start ();
ob_implicit_flush ( 0 );
include ( $templatePath );
$content = ob_get_clean ();
return $content ;
2007-05-14 12:24:40 +00:00
}
2007-06-03 23:56:56 +00:00
$this -> err ( 'Template for ' . $template . ' could not be found' );
return false ;
2007-05-14 12:24:40 +00:00
}
2007-05-26 09:00:54 +00:00
/**
2007-06-08 08:44:34 +00:00
* Displays help contents
2007-05-26 09:00:54 +00:00
*
2007-10-22 05:52:20 +00:00
* @ access public
2007-05-26 09:00:54 +00:00
*/
2007-06-03 23:56:56 +00:00
function help () {
2007-06-08 08:44:34 +00:00
$this -> hr ();
$this -> out ( " Usage: cake bake view <arg1> <arg2>... " );
$this -> hr ();
$this -> out ( 'Commands:' );
$this -> out ( " \n \t view <controller> \n \t \t will read the given controller for methods \n \t \t and bake corresponding views. \n \t \t If var scaffold is found it will bake the scaffolded actions \n \t \t (index,view,add,edit) " );
$this -> out ( " \n \t view <controller> <action> \n \t \t will bake a template. core templates: (index, add, edit, view) " );
$this -> out ( " \n \t view <controller> <template> <alias> \n \t \t will use the template specified but name the file based on the alias " );
$this -> out ( " " );
2007-06-03 23:56:56 +00:00
exit ();
2007-05-26 09:00:54 +00:00
}
2007-10-29 03:04:08 +00:00
/**
* Returns associations for controllers models .
*
* @ return array $associations
* @ access private
*/
function __associations ( $model ) {
$keys = array ( 'belongsTo' , 'hasOne' , 'hasMany' , 'hasAndBelongsToMany' );
2007-10-29 04:00:28 +00:00
$associations = array ();
2007-10-29 03:04:08 +00:00
foreach ( $keys as $key => $type ){
foreach ( $model -> { $type } as $assocKey => $assocData ) {
$associations [ $type ][ $assocKey ][ 'primaryKey' ] = $model -> { $assocKey } -> primaryKey ;
$associations [ $type ][ $assocKey ][ 'displayField' ] = $model -> { $assocKey } -> displayField ;
$associations [ $type ][ $assocKey ][ 'foreignKey' ] = $assocData [ 'foreignKey' ];
2007-10-29 19:32:53 +00:00
$associations [ $type ][ $assocKey ][ 'controller' ] = Inflector :: pluralize ( Inflector :: underscore ( $assocData [ 'className' ]));
$associations [ $type ][ $assocKey ][ 'fields' ] = array_keys ( $model -> { $assocKey } -> schema ());
2007-10-29 03:04:08 +00:00
}
}
return $associations ;
}
2007-06-21 14:38:46 +00:00
}
2007-08-25 18:00:19 +00:00
?>