2006-04-24 16:38:16 +00:00
#!/usr/bin/php -q
2005-07-04 01:07:14 +00:00
< ? php
2005-08-21 06:49:02 +00:00
/* SVN FILE: $Id$ */
2005-05-22 23:24:09 +00:00
/**
2006-07-18 11:11:44 +00:00
* Command - line code generation utility to automate programmer chores .
2006-07-23 18:59:52 +00:00
*
2006-07-18 11:11:44 +00:00
* Bake is CakePHP ' s code generation script , which can help you kickstart
* application development by writing fully functional skeleton controllers ,
* models , and views . Going further , Bake can also write Unit Tests for you .
2005-08-21 06:49:02 +00:00
*
* PHP versions 4 and 5
*
* CakePHP : Rapid Development Framework < http :// www . cakephp . org />
2006-05-26 05:29:17 +00:00
* Copyright ( c ) 2005 , Cake Software Foundation , Inc .
* 1785 E . Sahara Avenue , Suite 490 - 204
* Las Vegas , Nevada 89104
2005-08-21 06:49:02 +00:00
*
2005-12-23 21:57:26 +00:00
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice .
2005-08-21 06:49:02 +00:00
*
2006-01-13 23:53:23 +00:00
* @ filesource
2006-05-26 05:29:17 +00:00
* @ copyright Copyright ( c ) 2005 , Cake Software Foundation , Inc .
* @ link http :// www . cakefoundation . org / projects / info / cakephp CakePHP Project
* @ package cake
* @ subpackage cake . cake . scripts . bake
* @ since CakePHP v 0.10 . 0.1232
* @ version $Revision $
* @ modifiedby $LastChangedBy $
* @ lastmodified $Date $
* @ license http :// www . opensource . org / licenses / mit - license . php The MIT License
*/
2006-06-14 18:02:37 +00:00
define ( 'DS' , DIRECTORY_SEPARATOR );
if ( function_exists ( 'ini_set' )) {
ini_set ( 'display_errors' , '1' );
ini_set ( 'error_reporting' , '7' );
}
2006-10-10 18:24:25 +00:00
$app = null ;
2006-05-26 05:29:17 +00:00
$root = dirname ( dirname ( dirname ( __FILE__ )));
$core = null ;
$here = $argv [ 0 ];
$help = null ;
$project = null ;
for ( $i = 1 ; $i < count ( $argv ); $i += 2 ) {
switch ( $argv [ $i ]) {
case '-a' :
case '-app' :
$app = $argv [ $i + 1 ];
break ;
case '-c' :
case '-core' :
$core = $argv [ $i + 1 ];
break ;
case '-r' :
case '-root' :
$root = $argv [ $i + 1 ];
break ;
case '-h' :
case '-help' :
$help = true ;
break ;
case '-p' :
case '-project' :
$project = true ;
$projectPath = $argv [ $i + 1 ];
2006-10-10 22:05:41 +00:00
$app = $argv [ $i + 1 ];
2006-05-26 05:29:17 +00:00
break ;
}
}
2006-11-23 06:46:10 +00:00
if ( ! $app && isset ( $argv [ 1 ])) {
2006-10-10 22:05:41 +00:00
$app = $argv [ 1 ];
2006-11-23 06:46:10 +00:00
} elseif ( ! $app ) {
$app = 'app' ;
2006-10-10 22:05:41 +00:00
}
if ( ! is_dir ( $app )) {
$project = true ;
$projectPath = $app ;
2006-10-14 18:07:52 +00:00
2006-10-10 22:05:41 +00:00
}
2006-10-14 18:07:52 +00:00
2006-10-10 22:05:41 +00:00
if ( $project ) {
$app = $projectPath ;
}
2006-10-14 18:07:52 +00:00
2006-10-09 20:27:28 +00:00
$shortPath = str_replace ( $root , '' , $app );
2006-11-23 06:46:10 +00:00
$shortPath = str_replace ( '..' . DS , '' , $shortPath );
$shortPath = str_replace ( DS . DS , DS , $shortPath );
2006-10-14 18:07:52 +00:00
2006-11-23 06:46:10 +00:00
$pathArray = explode ( DS , $shortPath );
if ( end ( $pathArray ) != '' ) {
$appDir = array_pop ( $pathArray );
} else {
array_pop ( $pathArray );
$appDir = array_pop ( $pathArray );
}
$rootDir = implode ( DS , $pathArray );
$rootDir = str_replace ( DS . DS , DS , $rootDir );
2006-10-14 18:07:52 +00:00
2006-10-13 03:46:47 +00:00
if ( ! $rootDir ) {
$rootDir = $root ;
$projectPath = $root . DS . $appDir ;
}
2006-10-14 18:07:52 +00:00
2006-10-09 20:27:28 +00:00
define ( 'ROOT' , $rootDir );
define ( 'APP_DIR' , $appDir );
2006-10-13 04:45:12 +00:00
2006-06-19 20:02:36 +00:00
define ( 'DEBUG' , 1 );;
2006-10-09 20:27:28 +00:00
define ( 'CAKE_CORE_INCLUDE_PATH' , $root );
2006-10-14 18:07:52 +00:00
2006-05-26 05:29:17 +00:00
if ( function_exists ( 'ini_set' )) {
2006-06-19 20:02:36 +00:00
ini_set ( 'include_path' , ini_get ( 'include_path' ) .
PATH_SEPARATOR . CAKE_CORE_INCLUDE_PATH . DS .
2006-10-09 20:27:28 +00:00
PATH_SEPARATOR . ROOT . DS . APP_DIR . DS );
2006-06-19 20:02:36 +00:00
define ( 'APP_PATH' , null );
define ( 'CORE_PATH' , null );
2006-06-14 18:02:37 +00:00
} else {
define ( 'APP_PATH' , ROOT . DS . APP_DIR . DS );
define ( 'CORE_PATH' , CAKE_CORE_INCLUDE_PATH . DS );
2006-05-26 05:29:17 +00:00
}
2006-10-09 20:27:28 +00:00
require_once ( CORE_PATH . 'cake' . DS . 'basics.php' );
require_once ( CORE_PATH . 'cake' . DS . 'config' . DS . 'paths.php' );
require_once ( CORE_PATH . 'cake' . DS . 'dispatcher.php' );
require_once ( CORE_PATH . 'cake' . DS . 'scripts' . DS . 'templates' . DS . 'skel' . DS . 'config' . DS . 'core.php' );
2006-11-28 09:34:25 +00:00
/* uses ( 'inflector' , 'model' . DS . 'model' );
2006-10-09 20:27:28 +00:00
require_once ( CORE_PATH . 'cake' . DS . 'app_model.php' );
2006-11-28 09:34:25 +00:00
require_once ( CORE_PATH . 'cake' . DS . 'app_controller.php' ); */
/*uses ('inflector', 'model'.DS.'model');*/
/* uses ( 'neat_array' , 'model' . DS . 'connection_manager' , 'controller' . DS . 'controller' , 'session' ,
'configure' , 'security' , DS . 'controller' . DS . 'scaffold' ); */
uses ( 'session' , 'configure' , 'inflector' , 'model' . DS . 'connection_manager' );
2006-05-26 05:29:17 +00:00
$pattyCake = new Bake ();
if ( $help === true )
{
$pattyCake -> help ();
exit ();
}
if ( $project === true )
{
$pattyCake -> project ( $projectPath );
exit ();
}
$pattyCake -> main ();
/**
2006-07-18 11:11:44 +00:00
* Bake is a command - line code generation utility for automating programmer chores .
2006-05-26 05:29:17 +00:00
*
* @ package cake
* @ subpackage cake . cake . scripts
2005-08-21 06:49:02 +00:00
*/
2006-04-24 16:38:16 +00:00
class Bake {
2006-04-27 10:04:08 +00:00
2006-05-26 05:29:17 +00:00
/**
2006-07-18 11:11:44 +00:00
* Standard input stream .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ var filehandle
2006-05-26 05:29:17 +00:00
*/
var $stdin ;
/**
2006-07-18 11:11:44 +00:00
* Standard output stream .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ var filehandle
2006-05-26 05:29:17 +00:00
*/
var $stdout ;
/**
2006-07-18 11:11:44 +00:00
* Standard error stream .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ var filehandle
2006-05-26 05:29:17 +00:00
*/
var $stderr ;
/**
2006-07-18 11:11:44 +00:00
* Associated controller name .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ var string
2006-05-26 05:29:17 +00:00
*/
2006-10-09 20:27:28 +00:00
var $controllerName = null ;
2006-05-26 05:29:17 +00:00
/**
2006-07-18 11:11:44 +00:00
* If true , Bake will ask for permission to perform actions .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ var boolean
2006-05-26 05:29:17 +00:00
*/
var $interactive = false ;
2006-10-14 18:07:52 +00:00
2006-10-10 18:24:25 +00:00
var $__modelAlias = false ;
2006-05-26 05:29:17 +00:00
/**
2006-07-18 11:11:44 +00:00
* Private helper function for constructor
* @ access private
2006-05-26 05:29:17 +00:00
*/
function __construct () {
$this -> stdin = fopen ( 'php://stdin' , 'r' );
$this -> stdout = fopen ( 'php://stdout' , 'w' );
$this -> stderr = fopen ( 'php://stderr' , 'w' );
$this -> welcome ();
}
/**
2006-07-18 11:11:44 +00:00
* Constructor .
2006-05-26 05:29:17 +00:00
*
* @ return Bake
*/
function Bake () {
return $this -> __construct ();
}
/**
2006-07-18 11:11:44 +00:00
* Main - loop method .
2006-05-26 05:29:17 +00:00
*
*/
function main () {
2006-11-23 06:46:10 +00:00
$this -> stdout ( '' );
$this -> stdout ( '' );
$this -> stdout ( 'Baking...' );
$this -> hr ();
$this -> stdout ( 'Name: ' . APP_DIR );
$this -> stdout ( 'Path: ' . ROOT . DS . APP_DIR );
$this -> hr ();
2006-05-26 05:29:17 +00:00
if ( ! file_exists ( CONFIGS . 'database.php' )) {
$this -> stdout ( '' );
$this -> stdout ( 'Your database configuration was not found. Take a moment to create one:' );
$this -> doDbConfig ();
}
require_once ( CONFIGS . 'database.php' );
$this -> stdout ( '[M]odel' );
$this -> stdout ( '[C]ontroller' );
$this -> stdout ( '[V]iew' );
$invalidSelection = true ;
while ( $invalidSelection ) {
2006-11-23 06:46:10 +00:00
$classToBake = strtoupper ( $this -> getInput ( 'What would you like to Bake?' , array ( 'M' , 'V' , 'C' )));
2006-05-26 05:29:17 +00:00
switch ( $classToBake ) {
case 'M' :
$invalidSelection = false ;
$this -> doModel ();
break ;
case 'V' :
$invalidSelection = false ;
$this -> doView ();
break ;
case 'C' :
$invalidSelection = false ;
$this -> doController ();
break ;
default :
$this -> stdout ( 'You have made an invalid selection. Please choose a type of class to Bake by entering M, V, or C.' );
}
}
}
/**
2006-07-18 11:11:44 +00:00
* Database configuration setup .
2006-05-26 05:29:17 +00:00
*
*/
function doDbConfig () {
$this -> hr ();
2006-11-23 06:46:10 +00:00
$this -> stdout ( 'Database Configuration:' );
2006-05-26 05:29:17 +00:00
$this -> hr ();
2006-10-14 18:07:52 +00:00
2006-10-09 20:27:28 +00:00
$driver = '' ;
while ( $driver == '' ) {
$driver = $this -> getInput ( 'What database driver would you like to use?' , array ( 'mysql' , 'mysqli' , 'mssql' , 'sqlite' , 'postgres' , 'odbc' ), 'mysql' );
if ( $driver == '' ) {
$this -> stdout ( 'The database driver supplied was empty. Please supply a database driver.' );
}
}
2006-10-14 18:07:52 +00:00
2006-10-09 20:27:28 +00:00
switch ( $driver ) {
case 'mysql' :
2006-10-14 19:05:23 +00:00
$connect = 'mysql_connect' ;
2006-10-09 20:27:28 +00:00
break ;
case 'mysqli' :
$connect = 'mysqli_connect' ;
break ;
case 'mssql' :
2006-10-14 19:05:23 +00:00
$connect = 'mssql_connect' ;
2006-10-09 20:27:28 +00:00
break ;
case 'sqlite' :
2006-10-14 19:05:23 +00:00
$connect = 'sqlite_open' ;
2006-10-09 20:27:28 +00:00
break ;
case 'postgres' :
2006-10-14 19:05:23 +00:00
$connect = 'pg_connect' ;
2006-10-09 20:27:28 +00:00
break ;
case 'odbc' :
2006-10-14 19:05:23 +00:00
$connect = 'odbc_connect' ;
2006-10-09 20:27:28 +00:00
break ;
default :
$this -> stdout ( 'The connection parameter could not be set.' );
break ;
}
2006-10-14 18:07:52 +00:00
2006-05-26 05:29:17 +00:00
$host = '' ;
while ( $host == '' ) {
$host = $this -> getInput ( 'What is the hostname for the database server?' , null , 'localhost' );
if ( $host == '' ) {
$this -> stdout ( 'The host name you supplied was empty. Please supply a hostname.' );
}
}
$login = '' ;
while ( $login == '' ) {
2006-10-31 19:57:17 +00:00
$login = $this -> getInput ( 'What is the database username?' , null , 'root' );
2006-05-26 05:29:17 +00:00
if ( $login == '' ) {
$this -> stdout ( 'The database username you supplied was empty. Please try again.' );
}
}
$password = '' ;
2006-06-14 18:02:37 +00:00
$blankPassword = false ;
2006-05-26 05:29:17 +00:00
2006-06-14 18:02:37 +00:00
while ( $password == '' && $blankPassword == false ) {
2006-11-23 06:46:10 +00:00
$password = $this -> getInput ( 'What is the database password?' );
2006-05-26 05:29:17 +00:00
if ( $password == '' ) {
2006-06-14 18:02:37 +00:00
$blank = $this -> getInput ( 'The password you supplied was empty. Use an empty password?' , array ( 'y' , 'n' ), 'n' );
if ( $blank == 'y' )
{
$blankPassword = true ;
}
2006-05-26 05:29:17 +00:00
}
}
$database = '' ;
while ( $database == '' ) {
2006-10-31 19:57:17 +00:00
$database = $this -> getInput ( 'What is the name of the database you will be using?' , null , 'cake' );
2006-05-26 05:29:17 +00:00
if ( $database == '' ) {
$this -> stdout ( 'The database name you supplied was empty. Please try again.' );
}
}
2006-10-14 18:07:52 +00:00
2006-10-09 20:27:28 +00:00
$prefix = '' ;
while ( $prefix == '' ) {
$prefix = $this -> getInput ( 'Enter a table prefix?' , null , 'n' );
}
2006-10-10 20:00:35 +00:00
if ( low ( $prefix ) == 'n' ) {
$prefix = '' ;
}
2006-05-26 05:29:17 +00:00
$this -> stdout ( '' );
$this -> hr ();
$this -> stdout ( 'The following database configuration will be created:' );
$this -> hr ();
2006-10-09 20:27:28 +00:00
$this -> stdout ( " Driver: $driver " );
$this -> stdout ( " Connection: $connect " );
$this -> stdout ( " Host: $host " );
$this -> stdout ( " User: $login " );
$this -> stdout ( " Pass: " . str_repeat ( '*' , strlen ( $password )));
$this -> stdout ( " Database: $database " );
$this -> stdout ( " Table prefix: $prefix " );
2006-05-26 05:29:17 +00:00
$this -> hr ();
$looksGood = $this -> getInput ( 'Look okay?' , array ( 'y' , 'n' ), 'y' );
2006-10-31 19:57:17 +00:00
if ( low ( $looksGood ) == 'y' || low ( $looksGood ) == 'yes' ) {
2006-10-09 20:27:28 +00:00
$this -> bakeDbConfig ( $driver , $connect , $host , $login , $password , $database , $prefix );
2006-05-26 05:29:17 +00:00
} else {
$this -> stdout ( 'Bake Aborted.' );
}
}
/**
2006-07-18 11:11:44 +00:00
* Action to create a Model .
2006-05-26 05:29:17 +00:00
*
*/
function doModel ()
{
$this -> hr ();
$this -> stdout ( 'Model Bake:' );
$this -> hr ();
$this -> interactive = true ;
2006-10-18 11:22:34 +00:00
2006-10-15 11:19:39 +00:00
$useTable = null ;
$primaryKey = 'id' ;
$validate = array ();
$associations = array ();
2006-05-26 05:29:17 +00:00
/* $usingDefault = $this -> getInput ( 'Will your model be using a database connection setting other than the default?' );
2006-10-31 19:57:17 +00:00
if ( low ( $usingDefault ) == 'y' || low ( $usingDefault ) == 'yes' )
2006-05-26 05:29:17 +00:00
{
2006-10-15 11:19:39 +00:00
$useDbConfig = $this -> getInput ( 'Please provide the name of the connection you wish to use.' );
2006-05-26 05:29:17 +00:00
} */
2006-10-17 19:37:55 +00:00
$useDbConfig = 'default' ;
2006-10-31 19:57:17 +00:00
$this -> __doList ( $useDbConfig );
2006-10-18 11:22:34 +00:00
2006-10-14 18:07:52 +00:00
2006-10-09 20:27:28 +00:00
$enteredModel = '' ;
2006-10-14 18:07:52 +00:00
2006-10-09 20:27:28 +00:00
while ( $enteredModel == '' ) {
$enteredModel = $this -> getInput ( 'Enter a number from the list above, or type in the name of another model.' );
2006-05-26 05:29:17 +00:00
2006-10-18 00:16:09 +00:00
if ( $enteredModel == '' || intval ( $enteredModel ) > count ( $this -> __modelNames )) {
2006-05-26 05:29:17 +00:00
$this -> stdout ( 'Error:' );
$this -> stdout ( " The model name you supplied was empty, or the number \n you selected was not an option. Please try again. " );
2006-10-09 20:27:28 +00:00
$enteredModel = '' ;
2006-05-26 05:29:17 +00:00
}
}
2006-10-18 00:16:09 +00:00
if ( intval ( $enteredModel ) > 0 && intval ( $enteredModel ) <= count ( $this -> __modelNames )) {
$currentModelName = $this -> __modelNames [ intval ( $enteredModel ) - 1 ];
2006-05-26 05:29:17 +00:00
} else {
2006-10-15 08:28:26 +00:00
$currentModelName = $enteredModel ;
2006-10-09 20:27:28 +00:00
}
2006-10-14 18:07:52 +00:00
2006-10-15 11:19:39 +00:00
$useTable = Inflector :: tableize ( $currentModelName );
2006-10-18 00:16:09 +00:00
if ( array_search ( $useTable , $this -> __tables ) === false ) {
2006-10-15 11:19:39 +00:00
$this -> stdout ( " \n Given your model named ' $currentModelName ', Cake would expect a database table named ' " . $useTable . " '. " );
2006-10-10 20:00:35 +00:00
$tableIsGood = $this -> getInput ( 'Is this correct?' , array ( 'y' , 'n' ), 'y' );
}
2006-05-26 05:29:17 +00:00
2006-10-31 19:57:17 +00:00
if ( low ( $tableIsGood ) == 'n' || low ( $tableIsGood ) == 'no' ) {
2006-10-15 11:19:39 +00:00
$useTable = $this -> getInput ( 'What is the name of the table (enter "null" to use NO table)?' );
2006-05-26 05:29:17 +00:00
}
2006-10-14 18:07:52 +00:00
2006-05-26 05:29:17 +00:00
$wannaDoValidation = $this -> getInput ( 'Would you like to supply validation criteria for the fields in your model?' , array ( 'y' , 'n' ), 'y' );
2006-11-29 04:19:50 +00:00
loadModel ();
2006-10-15 11:19:39 +00:00
$tempModel = new Model ( false , $useTable );
2006-10-18 00:16:09 +00:00
$db =& ConnectionManager :: getDataSource ( $useDbConfig );
2006-05-26 05:29:17 +00:00
$modelFields = $db -> describe ( $tempModel );
2006-10-15 11:19:39 +00:00
if ( ! isset ( $modelFields [ 0 ][ 'name' ]) && $modelFields [ 0 ][ 'name' ] != 'id' ) {
$primaryKey = $this -> getInput ( 'What is the primaryKey' , null , 'id' );
}
2006-10-09 20:27:28 +00:00
$validate = array ();
2006-05-26 05:29:17 +00:00
2006-10-31 19:57:17 +00:00
if ( array_search ( $useTable , $this -> __tables ) !== false && ( low ( $wannaDoValidation ) == 'y' || low ( $wannaDoValidation ) == 'yes' )) {
2006-05-26 05:29:17 +00:00
foreach ( $modelFields as $field ) {
$this -> stdout ( '' );
$prompt .= 'Name: ' . $field [ 'name' ] . " \n " ;
$prompt .= 'Type: ' . $field [ 'type' ] . " \n " ;
$prompt .= '---------------------------------------------------------------' . " \n " ;
$prompt .= 'Please select one of the following validation options:' . " \n " ;
$prompt .= '---------------------------------------------------------------' . " \n " ;
$prompt .= " 1- VALID_NOT_EMPTY \n " ;
$prompt .= " 2- VALID_EMAIL \n " ;
$prompt .= " 3- VALID_NUMBER \n " ;
$prompt .= " 4- VALID_YEAR \n " ;
$prompt .= " 5- Do not do any validation on this field. \n \n " ;
$prompt .= " ... or enter in a valid regex validation string. \n \n " ;
if ( $field [ 'name' ] == 'id' || $field [ 'name' ] == 'created' || $field [ 'name' ] == 'modified' ) {
$validation = $this -> getInput ( $prompt , null , '5' );
} else {
$validation = $this -> getInput ( $prompt , null , '1' );
}
switch ( $validation ) {
case '1' :
$validate [ $field [ 'name' ]] = 'VALID_NOT_EMPTY' ;
break ;
case '2' :
$validate [ $field [ 'name' ]] = 'VALID_EMAIL' ;
break ;
case '3' :
$validate [ $field [ 'name' ]] = 'VALID_NUMBER' ;
break ;
case '4' :
$validate [ $field [ 'name' ]] = 'VALID_YEAR' ;
break ;
case '5' :
break ;
default :
$validate [ $field [ 'name' ]] = $validation ;
2006-10-09 20:27:28 +00:00
break ;
2006-05-26 05:29:17 +00:00
}
}
}
2006-06-20 12:24:48 +00:00
$wannaDoAssoc = $this -> getInput ( 'Would you like to define model associations (hasMany, hasOne, belongsTo, etc.)?' , array ( 'y' , 'n' ), 'y' );
2006-05-26 05:29:17 +00:00
2006-10-31 19:57:17 +00:00
if (( low ( $wannaDoAssoc ) == 'y' || low ( $wannaDoAssoc ) == 'yes' )) {
2006-05-26 05:29:17 +00:00
$this -> stdout ( 'One moment while I try to detect any associations...' );
2006-10-15 06:45:13 +00:00
$possibleKeys = array ();
2006-05-26 05:29:17 +00:00
//Look for belongsTo
2006-10-15 05:58:13 +00:00
$i = 0 ;
2006-05-26 05:29:17 +00:00
foreach ( $modelFields as $field ) {
$offset = strpos ( $field [ 'name' ], '_id' );
if ( $offset !== false ) {
2006-10-15 05:58:13 +00:00
$tmpModelName = $this -> __modelNameFromKey ( $field [ 'name' ]);
2006-10-15 11:19:39 +00:00
$associations [ 'belongsTo' ][ $i ][ 'alias' ] = $tmpModelName ;
$associations [ 'belongsTo' ][ $i ][ 'className' ] = $tmpModelName ;
$associations [ 'belongsTo' ][ $i ][ 'foreignKey' ] = $field [ 'name' ];
2006-10-15 05:58:13 +00:00
$i ++ ;
2006-05-26 05:29:17 +00:00
}
}
//Look for hasOne and hasMany and hasAndBelongsToMany
2006-10-15 05:58:13 +00:00
$i = 0 ;
2006-10-15 11:19:39 +00:00
$j = 0 ;
2006-10-18 00:16:09 +00:00
foreach ( $this -> __tables as $otherTable ) {
2006-10-15 11:19:39 +00:00
$tempOtherModel = & new Model ( false , $otherTable );
2006-10-09 20:27:28 +00:00
$modelFieldsTemp = $db -> describe ( $tempOtherModel );
2006-05-26 05:29:17 +00:00
foreach ( $modelFieldsTemp as $field ) {
2006-10-15 08:13:41 +00:00
if ( $field [ 'type' ] == 'integer' || $field [ 'type' ] == 'string' ) {
2006-10-18 11:22:34 +00:00
$possibleKeys [ $otherTable ][] = $field [ 'name' ];
2006-10-15 08:13:41 +00:00
}
2006-10-09 20:27:28 +00:00
if ( $field [ 'name' ] == $this -> __modelKey ( $currentModelName )) {
2006-10-15 05:58:13 +00:00
$tmpModelName = $this -> __modelName ( $otherTable );
2006-10-15 11:19:39 +00:00
$associations [ 'hasOne' ][ $j ][ 'alias' ] = $tmpModelName ;
$associations [ 'hasOne' ][ $j ][ 'className' ] = $tmpModelName ;
$associations [ 'hasOne' ][ $j ][ 'foreignKey' ] = $field [ 'name' ];
2006-10-18 11:22:34 +00:00
2006-10-15 11:19:39 +00:00
$associations [ 'hasMany' ][ $j ][ 'alias' ] = $tmpModelName ;
$associations [ 'hasMany' ][ $j ][ 'className' ] = $tmpModelName ;
$associations [ 'hasMany' ][ $j ][ 'foreignKey' ] = $field [ 'name' ];
2006-10-15 05:58:13 +00:00
$j ++ ;
2006-05-26 05:29:17 +00:00
}
}
2006-10-15 11:19:39 +00:00
$offset = strpos ( $otherTable , $useTable . '_' );
2006-05-26 05:29:17 +00:00
if ( $offset !== false ) {
2006-10-15 11:19:39 +00:00
$offset = strlen ( $useTable . '_' );
2006-10-15 05:58:13 +00:00
$tmpModelName = $this -> __modelName ( substr ( $otherTable , $offset ));
2006-10-15 11:19:39 +00:00
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ] = $tmpModelName ;
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'className' ] = $tmpModelName ;
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'foreignKey' ] = $this -> __modelKey ( $currentModelName );
2006-10-18 11:22:34 +00:00
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'associationForeignKey' ] = $this -> __modelKey ( $tmpModelName );
2006-10-15 11:19:39 +00:00
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'joinTable' ] = $otherTable ;
2006-10-15 05:58:13 +00:00
$i ++ ;
2006-05-26 05:29:17 +00:00
}
2006-10-15 11:19:39 +00:00
$offset = strpos ( $otherTable , '_' . $useTable );
2006-05-26 05:29:17 +00:00
if ( $offset !== false ) {
2006-10-15 05:58:13 +00:00
$tmpModelName = $this -> __modelName ( substr ( $otherTable , 0 , $offset ));
2006-10-15 11:19:39 +00:00
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ] = $tmpModelName ;
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'className' ] = $tmpModelName ;
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'foreignKey' ] = $this -> __modelKey ( $currentModelName );
2006-10-18 11:22:34 +00:00
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'associationForeignKey' ] = $this -> __modelKey ( $tmpModelName );
2006-10-15 11:19:39 +00:00
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'joinTable' ] = $otherTable ;
2006-10-15 05:58:13 +00:00
$i ++ ;
2006-05-26 05:29:17 +00:00
}
2006-10-18 11:22:34 +00:00
}
2006-05-26 05:29:17 +00:00
$this -> stdout ( 'Done.' );
$this -> hr ();
//if none found...
2006-10-15 11:19:39 +00:00
if ( empty ( $associations )) {
2006-05-26 05:29:17 +00:00
$this -> stdout ( 'None found.' );
} else {
$this -> stdout ( 'Please confirm the following associations:' );
$this -> hr ();
2006-10-15 11:19:39 +00:00
if ( ! empty ( $associations [ 'belongsTo' ])) {
$count = count ( $associations [ 'belongsTo' ]);
for ( $i = 0 ; $i < $count ; $i ++ ) {
if ( $currentModelName == $associations [ 'belongsTo' ][ $i ][ 'alias' ]) {
$response = $this -> getInput ( " { $currentModelName } belongsTo { $associations [ 'belongsTo' ][ $i ][ 'alias' ] } \n This looks like a self join. Do you want to specify an alternate association alias? " , array ( 'y' , 'n' ), 'y' );
2006-10-31 19:57:17 +00:00
if ( 'y' == low ( $response ) || 'yes' == low ( $response )) {
2006-10-15 11:19:39 +00:00
$associations [ 'belongsTo' ][ $i ][ 'alias' ] = $this -> getInput ( " So what is the alias? " , null , $associations [ 'belongsTo' ][ $i ][ 'alias' ]);
2006-10-15 05:58:13 +00:00
}
2006-10-15 11:19:39 +00:00
if ( $currentModelName != $associations [ 'belongsTo' ][ $i ][ 'alias' ]) {
$response = $this -> getInput ( " $currentModelName belongsTo { $associations [ 'belongsTo' ][ $i ][ 'alias' ] } ? " , array ( 'y' , 'n' ), 'y' );
2006-10-15 05:58:13 +00:00
} else {
2006-10-15 11:19:39 +00:00
$response = 'n' ;
2006-10-15 05:58:13 +00:00
}
} else {
2006-10-15 11:19:39 +00:00
$response = $this -> getInput ( " $currentModelName belongsTo { $associations [ 'belongsTo' ][ $i ][ 'alias' ] } ? " , array ( 'y' , 'n' ), 'y' );
2006-10-15 05:58:13 +00:00
}
2006-10-31 19:57:17 +00:00
if ( 'n' == low ( $response ) || 'no' == low ( $response )) {
2006-10-15 11:19:39 +00:00
unset ( $associations [ 'belongsTo' ][ $i ]);
2006-05-26 05:29:17 +00:00
}
}
2006-10-18 11:22:34 +00:00
$associations [ 'belongsTo' ] = array_merge ( $associations [ 'belongsTo' ]);
2006-05-26 05:29:17 +00:00
}
2006-10-15 11:19:39 +00:00
if ( ! empty ( $associations [ 'hasOne' ])) {
$count = count ( $associations [ 'hasOne' ]);
for ( $i = 0 ; $i < $count ; $i ++ ) {
if ( $currentModelName == $associations [ 'hasOne' ][ $i ][ 'alias' ]) {
$response = $this -> getInput ( " { $currentModelName } hasOne { $associations [ 'hasOne' ][ $i ][ 'alias' ] } \n This looks like a self join. Do you want to specify an alternate association alias? " , array ( 'y' , 'n' ), 'y' );
2006-10-31 19:57:17 +00:00
if ( 'y' == low ( $response ) || 'yes' == low ( $response )) {
2006-10-15 11:19:39 +00:00
$associations [ 'hasOne' ][ $i ][ 'alias' ] = $this -> getInput ( " So what is the alias? " , null , $associations [ 'hasOne' ][ $i ][ 'alias' ]);
2006-10-15 05:58:13 +00:00
}
2006-10-15 11:19:39 +00:00
if ( $currentModelName != $associations [ 'hasOne' ][ $i ][ 'alias' ]) {
$response = $this -> getInput ( " $currentModelName hasOne { $associations [ 'hasOne' ][ $i ][ 'alias' ] } ? " , array ( 'y' , 'n' ), 'y' );
2006-10-15 05:58:13 +00:00
} else {
2006-10-15 11:19:39 +00:00
$response = 'n' ;
2006-10-15 05:58:13 +00:00
}
} else {
2006-10-15 11:19:39 +00:00
$response = $this -> getInput ( " $currentModelName hasOne { $associations [ 'hasOne' ][ $i ][ 'alias' ] } ? " , array ( 'y' , 'n' ), 'y' );
2006-10-15 05:58:13 +00:00
}
2006-10-31 19:57:17 +00:00
if ( 'n' == low ( $response ) || 'no' == low ( $response )) {
2006-10-15 11:19:39 +00:00
unset ( $associations [ 'hasOne' ][ $i ]);
2006-05-26 05:29:17 +00:00
}
}
2006-10-18 11:22:34 +00:00
$associations [ 'hasOne' ] = array_merge ( $associations [ 'hasOne' ]);
2006-05-26 05:29:17 +00:00
}
2006-10-15 11:19:39 +00:00
if ( ! empty ( $associations [ 'hasMany' ])) {
$count = count ( $associations [ 'hasMany' ]);
for ( $i = 0 ; $i < $count ; $i ++ ) {
if ( $currentModelName == $associations [ 'hasMany' ][ $i ][ 'alias' ]) {
$response = $this -> getInput ( " { $currentModelName } hasMany { $associations [ 'hasMany' ][ $i ][ 'alias' ] } \n This looks like a self join. Do you want to specify an alternate association alias? " , array ( 'y' , 'n' ), 'y' );
2006-10-31 19:57:17 +00:00
if ( 'y' == low ( $response ) || 'yes' == low ( $response )) {
2006-10-15 11:19:39 +00:00
$associations [ 'hasMany' ][ $i ][ 'alias' ] = $this -> getInput ( " So what is the alias? " , null , $associations [ 'hasMany' ][ $i ][ 'alias' ]);
2006-10-15 05:58:13 +00:00
}
2006-10-15 11:19:39 +00:00
if ( $currentModelName != $associations [ 'hasMany' ][ $i ][ 'alias' ]) {
$response = $this -> getInput ( " $currentModelName hasMany { $associations [ 'hasMany' ][ $i ][ 'alias' ] } ? " , array ( 'y' , 'n' ), 'y' );
2006-10-15 05:58:13 +00:00
} else {
2006-10-15 11:19:39 +00:00
$response = 'n' ;
2006-10-15 05:58:13 +00:00
}
} else {
2006-10-15 11:19:39 +00:00
$response = $this -> getInput ( " $currentModelName hasMany { $associations [ 'hasMany' ][ $i ][ 'alias' ] } ? " , array ( 'y' , 'n' ), 'y' );
2006-10-15 05:58:13 +00:00
}
2006-10-31 19:57:17 +00:00
if ( 'n' == low ( $response ) || 'no' == low ( $response )) {
2006-10-15 11:19:39 +00:00
unset ( $associations [ 'hasMany' ][ $i ]);
2006-05-26 05:29:17 +00:00
}
}
2006-10-18 11:22:34 +00:00
$associations [ 'hasMany' ] = array_merge ( $associations [ 'hasMany' ]);
2006-05-26 05:29:17 +00:00
}
2006-10-15 11:19:39 +00:00
if ( ! empty ( $associations [ 'hasAndBelongsToMany' ])) {
$count = count ( $associations [ 'hasAndBelongsToMany' ]);
for ( $i = 0 ; $i < $count ; $i ++ ) {
if ( $currentModelName == $associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ]) {
$response = $this -> getInput ( " { $currentModelName } hasAndBelongsToMany { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ] } \n This looks like a self join. Do you want to specify an alternate association alias? " , array ( 'y' , 'n' ), 'y' );
2006-10-31 19:57:17 +00:00
if ( 'y' == low ( $response ) || 'yes' == low ( $response )) {
2006-10-15 11:19:39 +00:00
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ] = $this -> getInput ( " So what is the alias? " , null , $associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ]);
2006-10-15 05:58:13 +00:00
}
2006-10-15 11:19:39 +00:00
if ( $currentModelName != $associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ]) {
$response = $this -> getInput ( " $currentModelName hasAndBelongsToMany { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ] } ? " , array ( 'y' , 'n' ), 'y' );
2006-10-15 05:58:13 +00:00
} else {
2006-10-15 11:19:39 +00:00
$response = 'n' ;
2006-10-15 05:58:13 +00:00
}
} else {
2006-10-15 11:19:39 +00:00
$response = $this -> getInput ( " $currentModelName hasAndBelongsToMany { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ] } ? " , array ( 'y' , 'n' ), 'y' );
2006-10-15 05:58:13 +00:00
}
2006-10-31 19:57:17 +00:00
if ( 'n' == low ( $response ) || 'no' == low ( $response )) {
2006-10-15 11:19:39 +00:00
unset ( $associations [ 'hasAndBelongsToMany' ][ $i ]);
2006-05-26 05:29:17 +00:00
}
}
2006-10-18 11:22:34 +00:00
$associations [ 'hasAndBelongsToMany' ] = array_merge ( $associations [ 'hasAndBelongsToMany' ]);
2006-05-26 05:29:17 +00:00
}
}
$wannaDoMoreAssoc = $this -> getInput ( 'Would you like to define some additional model associations?' , array ( 'y' , 'n' ), 'y' );
2006-10-31 19:57:17 +00:00
while (( low ( $wannaDoMoreAssoc ) == 'y' || low ( $wannaDoMoreAssoc ) == 'yes' )) {
2006-05-26 05:29:17 +00:00
$assocs = array ( 1 => 'belongsTo' , 2 => 'hasOne' , 3 => 'hasMany' , 4 => 'hasAndBelongsToMany' );
$bad = true ;
while ( $bad ) {
$this -> stdout ( 'What is the association type?' );
$prompt = " 1- belongsTo \n " ;
$prompt .= " 2- hasOne \n " ;
$prompt .= " 3- hasMany \n " ;
$prompt .= " 4- hasAndBelongsToMany \n " ;
$assocType = intval ( $this -> getInput ( $prompt , null , null ));
if ( intval ( $assocType ) < 1 || intval ( $assocType ) > 4 ) {
$this -> stdout ( 'The selection you entered was invalid. Please enter a number between 1 and 4.' );
} else {
$bad = false ;
}
}
2006-10-31 19:57:17 +00:00
$this -> stdout ( 'For the following options be very careful to match your setup exactly. Any spelling mistakes will cause errors.' );
2006-10-15 20:30:27 +00:00
$this -> hr ();
2006-10-15 05:58:13 +00:00
$associationName = $this -> getInput ( 'What is the name of this association?' );
2006-10-15 20:30:27 +00:00
$className = $this -> getInput ( 'What className will ' . $associationName . ' use?' , null , $associationName );
$suggestedForeignKey = null ;
2006-10-15 08:13:41 +00:00
if ( $assocType == '1' ) {
2006-10-15 11:19:39 +00:00
$showKeys = $possibleKeys [ $useTable ];
2006-10-15 20:30:27 +00:00
$suggestedForeignKey = $this -> __modelKey ( $associationName );
2006-10-15 07:30:01 +00:00
} else {
2006-10-15 08:13:41 +00:00
$otherTable = Inflector :: tableize ( $className );
2006-10-18 00:16:09 +00:00
if ( in_array ( $otherTable , $this -> __tables )) {
2006-10-15 08:13:41 +00:00
if ( $assocType < '4' ) {
$showKeys = $possibleKeys [ $otherTable ];
} else {
$showKeys = null ;
}
2006-10-15 20:30:27 +00:00
} else {
$otherTable = $this -> getInput ( 'What is the table for this class?' );
$showKeys = $possibleKeys [ $otherTable ];
2006-10-15 08:13:41 +00:00
}
2006-10-15 20:30:27 +00:00
$suggestedForeignKey = $this -> __modelKey ( $currentModelName );
2006-10-15 06:45:13 +00:00
}
2006-10-15 08:13:41 +00:00
if ( ! empty ( $showKeys )) {
$this -> stdout ( 'A helpful List of possible keys' );
for ( $i = 0 ; $i < count ( $showKeys ); $i ++ ) {
$this -> stdout ( $i + 1 . " . " . $showKeys [ $i ]);
}
$foreignKey = $this -> getInput ( 'What is the foreignKey? Choose a number.' );
if ( intval ( $foreignKey ) > 0 && intval ( $foreignKey ) <= $i ) {
$foreignKey = $showKeys [ intval ( $foreignKey ) - 1 ];
2006-10-18 11:22:34 +00:00
}
}
2006-10-15 08:13:41 +00:00
if ( ! isset ( $foreignKey )) {
2006-10-15 20:30:27 +00:00
$foreignKey = $this -> getInput ( 'What is the foreignKey? Specify your own.' , null , $suggestedForeignKey );
2006-10-15 08:13:41 +00:00
}
2006-10-15 05:58:13 +00:00
if ( $assocType == '4' ) {
$associationForeignKey = $this -> getInput ( 'What is the associationForeignKey?' , null , $this -> __modelKey ( $currentModelName ));
$joinTable = $this -> getInput ( 'What is the joinTable?' );
}
2006-10-15 20:30:27 +00:00
$associations [ $assocs [ $assocType ]] = array_values ( $associations [ $assocs [ $assocType ]]);
2006-10-15 11:19:39 +00:00
$count = count ( $associations [ $assocs [ $assocType ]]);
2006-10-15 07:30:01 +00:00
$i = ( $count > 0 ) ? $count : 0 ;
2006-10-15 11:19:39 +00:00
$associations [ $assocs [ $assocType ]][ $i ][ 'alias' ] = $associationName ;
$associations [ $assocs [ $assocType ]][ $i ][ 'className' ] = $className ;
$associations [ $assocs [ $assocType ]][ $i ][ 'foreignKey' ] = $foreignKey ;
2006-10-15 05:58:13 +00:00
if ( $assocType == '4' ) {
2006-10-15 11:19:39 +00:00
$associations [ $assocs [ $assocType ]][ $i ][ 'associationForeignKey' ] = $associationForeignKey ;
$associations [ $assocs [ $assocType ]][ $i ][ 'joinTable' ] = $joinTable ;
2006-10-15 05:58:13 +00:00
}
2006-05-26 05:29:17 +00:00
$wannaDoMoreAssoc = $this -> getInput ( 'Define another association?' , array ( 'y' , 'n' ), 'y' );
}
}
$this -> stdout ( '' );
$this -> hr ();
$this -> stdout ( 'The following model will be created:' );
$this -> hr ();
2006-10-09 20:27:28 +00:00
$this -> stdout ( " Model Name: $currentModelName " );
2006-10-15 11:19:39 +00:00
$this -> stdout ( " DB Connection: " . ( $usingDefault ? 'default' : $useDbConfig ));
$this -> stdout ( " Model Table: " . $useTable );
2006-05-26 05:29:17 +00:00
$this -> stdout ( " Validation: " . print_r ( $validate , true ));
2006-10-15 11:19:39 +00:00
if ( ! empty ( $associations )) {
2006-05-26 05:29:17 +00:00
$this -> stdout ( " Associations: " );
2006-10-15 11:19:39 +00:00
if ( count ( $associations [ 'belongsTo' ])) {
for ( $i = 0 ; $i < count ( $associations [ 'belongsTo' ]); $i ++ ) {
$this -> stdout ( " $currentModelName belongsTo { $associations [ 'belongsTo' ][ $i ][ 'alias' ] } " );
2006-05-26 05:29:17 +00:00
}
}
2006-10-15 11:19:39 +00:00
if ( count ( $associations [ 'hasOne' ])) {
for ( $i = 0 ; $i < count ( $associations [ 'hasOne' ]); $i ++ ) {
$this -> stdout ( " $currentModelName hasOne { $associations [ 'hasOne' ][ $i ][ 'alias' ] } " );
2006-05-26 05:29:17 +00:00
}
}
2006-10-15 11:19:39 +00:00
if ( count ( $associations [ 'hasMany' ])) {
for ( $i = 0 ; $i < count ( $associations [ 'hasMany' ]); $i ++ ) {
$this -> stdout ( " $currentModelName hasMany { $associations [ 'hasMany' ][ $i ][ 'alias' ] } " );
2006-05-26 05:29:17 +00:00
}
}
2006-10-15 11:19:39 +00:00
if ( count ( $associations [ 'hasAndBelongsToMany' ])) {
for ( $i = 0 ; $i < count ( $associations [ 'hasAndBelongsToMany' ]); $i ++ ) {
$this -> stdout ( " $currentModelName hasAndBelongsToMany { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ] } " );
2006-05-26 05:29:17 +00:00
}
}
}
$this -> hr ();
$looksGood = $this -> getInput ( 'Look okay?' , array ( 'y' , 'n' ), 'y' );
2006-10-31 19:57:17 +00:00
if ( low ( $looksGood ) == 'y' || low ( $looksGood ) == 'yes' ) {
2006-10-15 11:19:39 +00:00
if ( $useTable == Inflector :: tableize ( $currentModelName )) {
2006-05-26 05:29:17 +00:00
// set it to null...
// putting $useTable in the model
// is unnecessary.
2006-10-15 11:19:39 +00:00
$useTable = null ;
2006-05-26 05:29:17 +00:00
}
2006-10-15 11:19:39 +00:00
$this -> bakeModel ( $currentModelName , $useDbConfig , $useTable , $primaryKey , $validate , $associations );
2006-05-26 05:29:17 +00:00
if ( $this -> doUnitTest ()) {
2006-10-09 20:27:28 +00:00
$this -> bakeUnitTest ( 'model' , $currentModelName );
2006-05-26 05:29:17 +00:00
}
} else {
$this -> stdout ( 'Bake Aborted.' );
}
}
/**
2006-07-18 11:11:44 +00:00
* Action to create a View .
2006-05-26 05:29:17 +00:00
*
*/
function doView () {
$this -> hr ();
$this -> stdout ( 'View Bake:' );
$this -> hr ();
$uses = array ();
2006-07-29 05:58:31 +00:00
$wannaUseSession = 'y' ;
2006-05-26 05:29:17 +00:00
$wannaDoScaffold = 'y' ;
2006-10-18 11:22:34 +00:00
2006-10-17 19:37:55 +00:00
$useDbConfig = 'default' ;
2006-10-31 19:57:17 +00:00
$this -> __doList ( $useDbConfig , 'Controllers' );
2006-10-18 11:22:34 +00:00
2006-10-17 19:37:55 +00:00
$enteredController = '' ;
2006-10-14 18:07:52 +00:00
2006-10-17 19:37:55 +00:00
while ( $enteredController == '' ) {
$enteredController = $this -> getInput ( 'Enter a number from the list above, or type in the name of another controller.' );
2006-05-26 05:29:17 +00:00
2006-10-18 00:16:09 +00:00
if ( $enteredController == '' || intval ( $enteredController ) > count ( $this -> __controllerNames )) {
2006-10-17 19:37:55 +00:00
$this -> stdout ( 'Error:' );
$this -> stdout ( " The Controller name you supplied was empty, or the number \n you selected was not an option. Please try again. " );
$enteredController = '' ;
2006-05-26 05:29:17 +00:00
}
}
2006-10-17 19:37:55 +00:00
2006-10-18 00:16:09 +00:00
if ( intval ( $enteredController ) > 0 && intval ( $enteredController ) <= count ( $this -> __controllerNames ) ) {
$controllerName = $this -> __controllerNames [ intval ( $enteredController ) - 1 ];
2006-10-17 19:37:55 +00:00
} else {
$controllerName = $enteredController ;
}
2006-10-10 18:24:25 +00:00
$controllerPath = $this -> __controllerPath ( $controllerName );
2006-10-14 18:07:52 +00:00
2006-05-26 05:29:17 +00:00
$doItInteractive = $this -> getInput ( " Would you like bake to build your views interactively? \n Warning: Choosing no will overwrite { $controllerClassName } views if it exist. " , array ( 'y' , 'n' ), 'y' );
2006-10-31 19:57:17 +00:00
if ( low ( $doItInteractive ) == 'y' || low ( $doItInteractive ) == 'yes' ) {
2006-05-26 05:29:17 +00:00
$this -> interactive = true ;
$wannaDoScaffold = $this -> getInput ( " 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' );
}
2006-10-14 18:07:52 +00:00
2006-10-09 20:27:28 +00:00
$admin = null ;
2006-10-20 01:19:46 +00:00
$admin_url = null ;
2006-10-31 19:57:17 +00:00
if ( low ( $wannaDoScaffold ) == 'y' || low ( $wannaDoScaffold ) == 'yes' ) {
2006-10-09 20:27:28 +00:00
$wannaDoAdmin = $this -> getInput ( " Would you like to create the views for admin routing? " , array ( 'y' , 'n' ), 'n' );
}
2006-10-14 18:07:52 +00:00
2006-10-31 19:57:17 +00:00
if (( low ( $wannaDoAdmin ) == 'y' || low ( $wannaDoAdmin ) == 'yes' )) {
2006-10-13 09:31:20 +00:00
require ( CONFIGS . 'core.php' );
2006-10-09 20:27:28 +00:00
if ( defined ( 'CAKE_ADMIN' )) {
2006-10-14 18:07:52 +00:00
$admin = CAKE_ADMIN . '_' ;
2006-10-20 01:19:46 +00:00
$admin_url = '/' . CAKE_ADMIN ;
2006-10-09 20:27:28 +00:00
} else {
2006-10-14 18:07:52 +00:00
$adminRoute = '' ;
2006-10-09 20:27:28 +00:00
$this -> stdout ( 'You need to enable CAKE_ADMIN in /app/config/core.php to use admin routing.' );
2006-10-14 18:07:52 +00:00
$this -> stdout ( 'What would you like the admin route to be?' );
$this -> stdout ( 'Example: www.example.com/admin/controller' );
while ( $adminRoute == '' ) {
$adminRoute = $this -> getInput ( " What would you like the admin route to be? " , null , 'admin' );
}
if ( $this -> __addAdminRoute ( $adminRoute ) !== true ){
$this -> stdout ( 'Unable to write to /app/config/core.php.' );
$this -> stdout ( 'You need to enable CAKE_ADMIN in /app/config/core.php to use admin routing.' );
exit ();
} else {
$admin = $adminRoute . '_' ;
2006-10-20 01:19:46 +00:00
$admin_url = '/' . $adminRoute ;
2006-10-14 18:07:52 +00:00
}
2006-10-09 20:27:28 +00:00
}
}
2006-10-31 19:57:17 +00:00
if ( low ( $wannaDoScaffold ) == 'y' || low ( $wannaDoScaffold ) == 'yes' ) {
2006-10-10 18:24:25 +00:00
$file = CONTROLLERS . $controllerPath . '_controller.php' ;
2006-05-26 05:29:17 +00:00
if ( ! file_exists ( $file )) {
2006-07-29 05:58:31 +00:00
$shortPath = str_replace ( ROOT , null , $file );
$shortPath = str_replace ( '../' , '' , $shortPath );
$shortPath = str_replace ( '//' , '/' , $shortPath );
2006-05-26 05:29:17 +00:00
$this -> stdout ( '' );
2006-07-29 05:58:31 +00:00
$this -> stdout ( " The file ' $shortPath ' could not be found. \n In order to scaffold, you'll need to first create the controller. " );
2006-05-26 05:29:17 +00:00
$this -> stdout ( '' );
die ();
} else {
2006-10-10 18:24:25 +00:00
loadController ( $controllerName );
2006-05-26 05:29:17 +00:00
loadModels ();
2006-10-13 07:41:10 +00:00
if ( $admin ) {
2006-10-28 20:15:41 +00:00
$this -> __bakeViews ( $controllerName , $controllerPath , $admin , $admin_url );
2006-10-13 07:41:10 +00:00
}
2006-10-28 20:15:41 +00:00
$this -> __bakeViews ( $controllerName , $controllerPath , null , null );
2006-11-04 07:08:17 +00:00
2006-05-26 05:29:17 +00:00
$this -> hr ();
$this -> stdout ( '' );
$this -> stdout ( 'View Scaffolding Complete.' . " \n " );
}
} else {
$actionName = '' ;
while ( $actionName == '' ) {
$actionName = $this -> getInput ( 'Action Name? (use camelCased function name)' );
if ( $actionName == '' ) {
$this -> stdout ( 'The action name you supplied was empty. Please try again.' );
}
}
$this -> stdout ( '' );
$this -> hr ();
$this -> stdout ( 'The following view will be created:' );
$this -> hr ();
2006-10-10 18:24:25 +00:00
$this -> stdout ( " Controller Name: $controllerName " );
2006-05-26 05:29:17 +00:00
$this -> stdout ( " Action Name: $actionName " );
2006-11-28 09:34:25 +00:00
$this -> stdout ( " Path: app/views/ " . $controllerPath . DS . Inflector :: underscore ( $actionName ) . '.ctp' );
2006-05-26 05:29:17 +00:00
$this -> hr ();
$looksGood = $this -> getInput ( 'Look okay?' , array ( 'y' , 'n' ), 'y' );
2006-10-31 19:57:17 +00:00
if ( low ( $looksGood ) == 'y' || low ( $looksGood ) == 'yes' ) {
2006-10-10 18:24:25 +00:00
$this -> bakeView ( $controllerName , $actionName );
2006-05-26 05:29:17 +00:00
} else {
$this -> stdout ( 'Bake Aborted.' );
}
}
}
2006-11-04 07:08:17 +00:00
function __bakeViews ( $controllerName , $controllerPath , $admin = null , $admin_url = null ) {
2006-10-28 20:15:41 +00:00
$controllerClassName = $controllerName . 'Controller' ;
$controllerObj = & new $controllerClassName ();
2006-11-28 09:34:25 +00:00
2006-10-28 20:15:41 +00:00
if ( ! in_array ( 'Html' , $controllerObj -> helpers )) {
$controllerObj -> helpers [] = 'Html' ;
}
if ( ! in_array ( 'Form' , $controllerObj -> helpers )) {
$controllerObj -> helpers [] = 'Form' ;
}
$controllerObj -> constructClasses ();
$currentModelName = $controllerObj -> modelClass ;
$this -> __modelClass = $currentModelName ;
2006-11-28 09:34:25 +00:00
$modelKey = $controllerObj -> modelKey ;
2006-10-28 20:15:41 +00:00
$modelObj =& ClassRegistry :: getObject ( $modelKey );
$singularName = $this -> __singularName ( $currentModelName );
$pluralName = $this -> __pluralName ( $currentModelName );
$singularHumanName = $this -> __singularHumanName ( $currentModelName );
$pluralHumanName = $this -> __pluralHumanName ( $controllerName );
$fieldNames = $controllerObj -> generateFieldNames ( null , false );
//-------------------------[INDEX]-------------------------//
$indexView = null ;
if ( ! empty ( $modelObj -> alias )) {
foreach ( $modelObj -> alias as $key => $value ) {
$alias [] = $key ;
}
}
$indexView .= " <div class= \" { $pluralName } \" > \n " ;
$indexView .= " <h2>List " . $pluralHumanName . " </h2> \n \n " ;
$indexView .= " <table cellpadding= \" 0 \" cellspacing= \" 0 \" > \n " ;
$indexView .= " <tr> \n " ;
foreach ( $fieldNames as $fieldName ) {
$indexView .= " \t <th> " . $fieldName [ 'prompt' ] . " </th> \n " ;
}
$indexView .= " \t <th>Actions</th> \n " ;
$indexView .= " </tr> \n " ;
$indexView .= " <?php foreach ( \$ { $pluralName } as \$ { $singularName } ): ?> \n " ;
$indexView .= " <tr> \n " ;
$count = 0 ;
foreach ( $fieldNames as $field => $value ) {
if ( isset ( $value [ 'foreignKey' ])) {
$otherModelName = $this -> __modelName ( $value [ 'model' ]);
2006-11-28 09:34:25 +00:00
$otherModelKey = Inflector :: underscore ( $value [ 'modelKey' ]);
2006-10-28 20:15:41 +00:00
$otherModelObj =& ClassRegistry :: getObject ( $otherModelKey );
2006-11-28 09:34:25 +00:00
$otherControllerName = $this -> __controllerName ( $value [ 'modelKey' ]);
2006-10-28 20:15:41 +00:00
$otherControllerPath = $this -> __controllerPath ( $otherControllerName );
if ( is_object ( $otherModelObj )) {
$displayField = $otherModelObj -> getDisplayField ();
$indexView .= " \t <td> <?php echo \$ html->link( \$ " . $singularName . " [' { $alias [ $count ] } '][' { $displayField } '], ' { $admin_url } / " . $otherControllerPath . " /view/' . \$ " . $singularName . " [' { $alias [ $count ] } '][' { $otherModelObj -> primaryKey } '])?></td> \n " ;
} else {
$indexView .= " \t <td><?php echo \$ " . $singularName . " [' { $modelObj -> name } '][' { $field } ']; ?></td> \n " ;
}
$count ++ ;
} else {
$indexView .= " \t <td><?php echo \$ " . $singularName . " [' { $modelObj -> name } '][' { $field } ']; ?></td> \n " ;
}
}
$indexView .= " \t <td nowrap> \n " ;
$indexView .= " \t \t <?php echo \$ html->link('View',' { $admin_url } / { $controllerPath } /view/' . \$ " . $singularName . " [' { $modelObj -> name } '][' { $modelObj -> primaryKey } '])?> \n " ;
$indexView .= " \t \t <?php echo \$ html->link('Edit',' { $admin_url } / { $controllerPath } /edit/' . \$ " . $singularName . " [' { $modelObj -> name } '][' { $modelObj -> primaryKey } '])?> \n " ;
$indexView .= " \t \t <?php echo \$ html->link('Delete',' { $admin_url } / { $controllerPath } /delete/' . \$ " . $singularName . " [' { $modelObj -> name } '][' { $modelObj -> primaryKey } '], null, 'Are you sure you want to delete id ' . \$ " . $singularName . " [' { $modelObj -> name } '][' { $modelObj -> primaryKey } '])?> \n " ;
$indexView .= " \t </td> \n " ;
$indexView .= " </tr> \n " ;
$indexView .= " <?php endforeach; ?> \n " ;
$indexView .= " </table> \n \n " ;
$indexView .= " <ul class= \" actions \" > \n " ;
$indexView .= " \t <li><?php echo \$ html->link('New { $singularHumanName } ', ' { $admin_url } / { $controllerPath } /add'); ?></li> \n " ;
$indexView .= " </ul> \n " ;
$indexView .= " </div> " ;
//-------------------------[VIEW]-------------------------//
$viewView = null ;
$viewView .= " <div class= \" { $singularName } \" > \n " ;
$viewView .= " <h2>View " . $singularHumanName . " </h2> \n \n " ;
$viewView .= " <dl> \n " ;
$count = 0 ;
foreach ( $fieldNames as $field => $value ) {
$viewView .= " \t <dt> " . $value [ 'prompt' ] . " </dt> \n " ;
if ( isset ( $value [ 'foreignKey' ])) {
$otherModelName = $this -> __modelName ( $value [ 'model' ]);
2006-11-28 09:34:25 +00:00
$otherModelKey = Inflector :: underscore ( $value [ 'modelKey' ]);
$otherModelObj =& ClassRegistry :: getObject ( $value [ 'modelKey' ]);
$otherControllerName = $this -> __controllerName ( $value [ 'modelKey' ]);
2006-10-28 20:15:41 +00:00
$otherControllerPath = $this -> __controllerPath ( $otherControllerName );
$displayField = $otherModelObj -> getDisplayField ();
$viewView .= " \t <dd> <?php echo \$ html->link( \$ " . $singularName . " [' { $alias [ $count ] } '][' { $displayField } '], ' { $admin_url } / " . $otherControllerPath . " /view/' . \$ " . $singularName . " [' { $alias [ $count ] } '][' { $otherModelObj -> primaryKey } '])?></dd> \n " ;
$count ++ ;
} else {
$viewView .= " \t <dd> <?php echo \$ " . $singularName . " [' { $modelObj -> name } '][' { $field } ']?></dd> \n " ;
}
}
$viewView .= " </dl> \n " ;
$viewView .= " <ul class= \" actions \" > \n " ;
$viewView .= " \t <li><?php echo \$ html->link('Edit " . $singularHumanName . " ', ' { $admin_url } / { $controllerPath } /edit/' . \$ " . $singularName . " [' { $modelObj -> name } '][' { $modelObj -> primaryKey } ']) ?> </li> \n " ;
$viewView .= " \t <li><?php echo \$ html->link('Delete " . $singularHumanName . " ', ' { $admin_url } / { $controllerPath } /delete/' . \$ " . $singularName . " [' { $modelObj -> name } '][' { $modelObj -> primaryKey } '], null, 'Are you sure you want to delete: id ' . \$ " . $singularName . " [' { $modelObj -> name } '][' { $modelObj -> primaryKey } '] . '?') ?> </li> \n " ;
$viewView .= " \t <li><?php echo \$ html->link('List " . $pluralHumanName . " ', ' { $admin_url } / { $controllerPath } /index') ?> </li> \n " ;
$viewView .= " \t <li><?php echo \$ html->link('New " . $singularHumanName . " ', ' { $admin_url } / { $controllerPath } /add') ?> </li> \n " ;
foreach ( $fieldNames as $field => $value ) {
if ( isset ( $value [ 'foreignKey' ] ) ) {
$otherModelName = $this -> __modelName ( $value [ 'model' ]);
if ( $otherModelName != $currentModelName ) {
$otherControllerName = $this -> __controllerName ( $otherModelName );
$otherControllerPath = $this -> __controllerPath ( $otherControllerName );
$otherSingularHumanName = $this -> __singularHumanName ( $value [ 'controller' ]);
$otherPluralHumanName = $this -> __pluralHumanName ( $value [ 'controller' ]);
$viewView .= " \t <li><?php echo \$ html->link('List " . $otherSingularHumanName . " ', ' { $admin_url } / " . $otherControllerPath . " /index/')?> </li> \n " ;
$viewView .= " \t <li><?php echo \$ html->link('New " . $otherPluralHumanName . " ', ' { $admin_url } / " . $otherControllerPath . " /add/')?> </li> \n " ;
}
}
}
$viewView .= " </ul> \n \n " ;
$viewView .= " </div> \n " ;
foreach ( $modelObj -> hasOne as $associationName => $relation ) {
$new = true ;
$otherModelName = $this -> __modelName ( $relation [ 'className' ]);
$otherControllerName = $this -> __controllerName ( $otherModelName );
$otherControllerPath = $this -> __controllerPath ( $otherModelName );
$otherSingularName = $this -> __singularName ( $associationName );
$otherPluralHumanName = $this -> __pluralHumanName ( $associationName );
$otherSingularHumanName = $this -> __singularHumanName ( $associationName );
$viewView .= " <div class= \" related \" > \n " ;
$viewView .= " <h3>Related " . $otherPluralHumanName . " </h3> \n " ;
$viewView .= " <?php if(!empty( \$ " . $singularName . " [' { $associationName } '])): ?> \n " ;
$viewView .= " <dl> \n " ;
$viewView .= " \t <?php foreach( \$ " . $singularName . " [' { $associationName } '] as \$ field => \$ value): ?> \n " ;
$viewView .= " \t \t <dt><?php echo \$ field ?></dt> \n " ;
$viewView .= " \t \t <dd> <?php echo \$ value ?></dd> \n " ;
$viewView .= " \t <?php endforeach; ?> \n " ;
$viewView .= " </dl> \n " ;
$viewView .= " <?php endif; ?> \n " ;
$viewView .= " <ul class= \" actions \" > \n " ;
$viewView .= " \t <li><?php echo \$ html->link('Edit " . $otherSingularHumanName . " ', ' { $admin_url } / " . $otherControllerPath . " /edit/' . \$ " . $singularName . " [' { $associationName } '][' " . $modelObj -> { $otherModelName } -> primaryKey . " ']);?></li> \n " ;
$viewView .= " \t <li><?php echo \$ html->link('New " . $otherSingularHumanName . " ', ' { $admin_url } / " . $otherControllerPath . " /add/');?> </li> \n " ;
$viewView .= " </ul> \n " ;
$viewView .= " </div> \n " ;
}
$relations = array_merge ( $modelObj -> hasMany , $modelObj -> hasAndBelongsToMany );
foreach ( $relations as $associationName => $relation ) {
2006-11-28 09:34:25 +00:00
$otherModelName = $associationName ;
$otherControllerName = $this -> __controllerName ( $relation [ 'className' ]);
$otherControllerPath = $this -> __controllerPath ( $otherControllerName );
2006-10-28 20:15:41 +00:00
$otherSingularName = $this -> __singularName ( $associationName );
$otherPluralHumanName = $this -> __pluralHumanName ( $associationName );
$otherSingularHumanName = $this -> __singularHumanName ( $associationName );
2006-11-28 09:34:25 +00:00
$otherModelKey = Inflector :: underscore ( $relation [ 'className' ]);
2006-10-28 20:15:41 +00:00
$otherModelObj =& ClassRegistry :: getObject ( $otherModelKey );
$viewView .= " <div class= \" related \" > \n " ;
$viewView .= " <h3>Related " . $otherPluralHumanName . " </h3> \n " ;
$viewView .= " <?php if(!empty( \$ " . $singularName . " [' { $associationName } '])):?> \n " ;
$viewView .= " <table cellpadding= \" 0 \" cellspacing= \" 0 \" > \n " ;
$viewView .= " <tr> \n " ;
$viewView .= " <?php foreach( \$ " . $singularName . " [' { $associationName } ']['0'] as \$ column => \$ value): ?> \n " ;
$viewView .= " <th><?php echo \$ column?></th> \n " ;
$viewView .= " <?php endforeach; ?> \n " ;
$viewView .= " <th>Actions</th> \n " ;
$viewView .= " </tr> \n " ;
$viewView .= " <?php foreach( \$ " . $singularName . " [' { $associationName } '] as \$ " . $otherSingularName . " ):?> \n " ;
$viewView .= " <tr> \n " ;
$viewView .= " \t <?php foreach( \$ " . $otherSingularName . " as \$ column => \$ value):?> \n " ;
$viewView .= " \t \t <td><?php echo \$ value;?></td> \n " ;
$viewView .= " \t <?php endforeach;?> \n " ;
$viewView .= " \t <td nowrap> \n " ;
$viewView .= " \t \t <?php echo \$ html->link('View', ' { $admin_url } / " . $otherControllerPath . " /view/' . \$ " . $otherSingularName . " [' { $otherModelObj -> primaryKey } ']);?> \n " ;
$viewView .= " \t \t <?php echo \$ html->link('Edit', ' { $admin_url } / " . $otherControllerPath . " /edit/' . \$ " . $otherSingularName . " [' { $otherModelObj -> primaryKey } ']);?> \n " ;
$viewView .= " \t \t <?php echo \$ html->link('Delete', ' { $admin_url } / " . $otherControllerPath . " /delete/' . \$ " . $otherSingularName . " [' { $otherModelObj -> primaryKey } '], null, 'Are you sure you want to delete: id ' . \$ " . $otherSingularName . " [' { $otherModelObj -> primaryKey } '] . '?');?> \n " ;
$viewView .= " \t </td> \n " ;
$viewView .= " </tr> \n " ;
$viewView .= " <?php endforeach; ?> \n " ;
$viewView .= " </table> \n " ;
$viewView .= " <?php endif; ?> \n \n " ;
$viewView .= " <ul class= \" actions \" > \n " ;
$viewView .= " \t <li><?php echo \$ html->link('New " . $otherSingularHumanName . " ', ' { $admin_url } / " . $otherControllerPath . " /add/');?> </li> \n " ;
$viewView .= " </ul> \n " ;
$viewView .= " </div> \n " ;
}
2006-11-28 09:34:25 +00:00
$fields = $controllerObj -> generateFieldNames ( null , true );
2006-10-28 20:15:41 +00:00
//-------------------------[ADD]-------------------------//
$addView = null ;
$addView .= " <h2>New " . $singularHumanName . " </h2> \n " ;
2006-11-29 05:45:46 +00:00
$addView .= " <?php echo \$ form->create(' { $currentModelName } ', array('action'=>'add'));?> \n " ;
2006-11-28 09:34:25 +00:00
$addView .= $this -> displayFields ( $fields );
2006-11-29 05:45:46 +00:00
$addView .= " \t <?php echo \$ form->generateSubmitDiv('Add');?> \n " ;
2006-10-28 20:15:41 +00:00
$addView .= " </form> \n " ;
$addView .= " <ul class= \" actions \" > \n " ;
$addView .= " <li><?php echo \$ html->link('List { $pluralHumanName } ', ' { $admin_url } / { $controllerPath } /index')?></li> \n " ;
foreach ( $modelObj -> belongsTo as $associationName => $relation ) {
2006-11-23 06:46:10 +00:00
$otherModelName = $this -> __modelName ( $associationName );
2006-10-28 20:15:41 +00:00
if ( $otherModelName != $currentModelName ) {
$otherControllerName = $this -> __controllerName ( $otherModelName );
2006-11-28 09:34:25 +00:00
$otherControllerPath = $this -> __controllerPath ( $otherControllerName );
2006-10-28 20:15:41 +00:00
$otherSingularName = $this -> __singularName ( $associationName );
$otherPluralName = $this -> __pluralHumanName ( $associationName );
$addView .= " <li><?php echo \$ html->link('View " . $otherPluralName . " ', ' { $admin_url } / " . $otherControllerPath . " /index/');?></li> \n " ;
$addView .= " <li><?php echo \$ html->link('Add " . $otherPluralName . " ', ' { $admin_url } / " . $otherControllerPath . " /add/');?></li> \n " ;
}
}
$addView .= " </ul> \n " ;
//-------------------------[EDIT]-------------------------//
$editView = null ;
$editView .= " <h2>Edit " . $singularHumanName . " </h2> \n " ;
2006-11-29 05:45:46 +00:00
$addView .= " <?php echo \$ form->create(' { $currentModelName } ', array('action'=>'edit'));?> \n " ;
$addView .= $this -> displayFields ( $fields );
$addView .= " \t <?php echo \$ form->generateSubmitDiv('Add');?> \n " ;
2006-10-28 20:15:41 +00:00
$editView .= " </form> \n " ;
$editView .= " <ul class= \" actions \" > \n " ;
$editView .= " <li><?php echo \$ html->link('Delete',' { $admin_url } / { $controllerPath } /delete/' . \$ html->tagValue(' { $modelObj -> name } / { $modelObj -> primaryKey } '), null, 'Are you sure you want to delete: id ' . \$ html->tagValue(' { $modelObj -> name } / { $modelObj -> primaryKey } '));?> \n " ;
$editView .= " <li><?php echo \$ html->link('List { $pluralHumanName } ', ' { $admin_url } / { $controllerPath } /index')?></li> \n " ;
foreach ( $modelObj -> belongsTo as $associationName => $relation ) {
2006-11-23 06:46:10 +00:00
$otherModelName = $this -> __modelName ( $associationName );
2006-10-28 20:15:41 +00:00
if ( $otherModelName != $currentModelName ) {
$otherControllerName = $this -> __controllerName ( $otherModelName );
2006-11-28 09:34:25 +00:00
$otherControllerPath = $this -> __controllerPath ( $otherControllerName );
2006-10-28 20:15:41 +00:00
$otherSingularName = $this -> __singularName ( $associationName );
$otherPluralName = $this -> __pluralHumanName ( $associationName );
$editView .= " <li><?php echo \$ html->link('View " . $otherPluralName . " ', ' { $admin_url } / " . $otherControllerPath . " /index/');?></li> \n " ;
$editView .= " <li><?php echo \$ html->link('Add " . $otherPluralName . " ', ' { $admin_url } / " . $otherControllerPath . " /add/');?></li> \n " ;
}
}
$editView .= " </ul> \n " ;
//------------------------------------------------------------------------------------//
2006-11-04 07:08:17 +00:00
2006-10-28 20:15:41 +00:00
if ( ! file_exists ( VIEWS . $controllerPath )) {
mkdir ( VIEWS . $controllerPath );
}
2006-11-28 09:34:25 +00:00
$filename = VIEWS . $controllerPath . DS . $admin . 'index.ctp' ;
2006-10-31 19:57:17 +00:00
$this -> __createFile ( $filename , $indexView );
2006-11-28 09:34:25 +00:00
$filename = VIEWS . $controllerPath . DS . $admin . 'view.ctp' ;
2006-10-31 19:57:17 +00:00
$this -> __createFile ( $filename , $viewView );
2006-11-28 09:34:25 +00:00
$filename = VIEWS . $controllerPath . DS . $admin . 'add.ctp' ;
2006-10-31 19:57:17 +00:00
$this -> __createFile ( $filename , $addView );
2006-11-28 09:34:25 +00:00
$filename = VIEWS . $controllerPath . DS . $admin . 'edit.ctp' ;
2006-11-04 07:08:17 +00:00
$this -> __createFile ( $filename , $editView );
2006-10-28 20:15:41 +00:00
}
2006-11-28 09:34:25 +00:00
/**
* returns the fields to be display in the baked forms .
*
* @ param array $fields
*/
function displayFields ( $fields = array ()) {
foreach ( $fields as $name => $options ) {
if ( isset ( $options [ 'tagName' ])){
$tagName = $options [ 'tagName' ];
unset ( $options [ 'tagName' ]);
}
$formOptions = array ();
if ( isset ( $options [ 'type' ])){
$type = $options [ 'type' ];
unset ( $options [ 'type' ]);
$formOptions [] = " 'type' => ' { $type } ' " ;
}
if ( isset ( $options [ 'class' ]) && $options [ 'class' ] == 'required' ){
$class = $options [ 'class' ];
unset ( $options [ 'class' ]);
$formOptions [] = " 'class' => ' { $class } ' " ;
}
if ( isset ( $options [ 'options' ])){
$fieldOptions = $this -> __pluralName ( $options [ 'model' ]);
unset ( $options [ 'options' ]);
$formOptions [] = " 'options' => \$ { $fieldOptions } " ;
if ( isset ( $options [ 'multiple' ])){
$selected = 'selected' . ucfirst ( $fieldOptions );
$formOptions [] = " 'selected' => \$ { $selected } " ;
$formOptions [] = " 'multiple' => 'multiple' " ;
}
}
if ( isset ( $options [ 'size' ])){
$size = $options [ 'size' ];
unset ( $options [ 'class' ]);
$formOptions [] = " 'size' => ' { $size } ' " ;
}
if ( isset ( $options [ 'cols' ])){
$cols = $options [ 'cols' ];
unset ( $options [ 'cols' ]);
$formOptions [] = " 'cols' => ' { $cols } ' " ;
}
if ( isset ( $options [ 'rows' ])){
$rows = $options [ 'rows' ];
unset ( $options [ 'rows' ]);
$formOptions [] = " 'rows' => ' { $rows } ' " ;
}
if ( ! empty ( $formOptions )) {
$formOptions = " , array( " . join ( ', ' , $formOptions ) . " ) " ;
} else {
$formOptions = null ;
}
$displayFields .= " \t <?php echo \$ form->input(' { $tagName } ' { $formOptions } );?> \n " ;
}
return $displayFields ;
}
2006-05-26 05:29:17 +00:00
/**
2006-07-18 11:11:44 +00:00
* Action to create a Controller .
2006-05-26 05:29:17 +00:00
*
*/
function doController () {
$this -> hr ();
$this -> stdout ( 'Controller Bake:' );
$this -> hr ();
$uses = array ();
$helpers = array ();
$components = array ();
2006-07-29 05:58:31 +00:00
$wannaUseSession = 'y' ;
2006-05-26 05:29:17 +00:00
$wannaDoScaffolding = 'y' ;
2006-07-29 05:58:31 +00:00
2006-10-17 19:37:55 +00:00
$useDbConfig = 'default' ;
2006-10-31 19:57:17 +00:00
$this -> __doList ( $useDbConfig , 'Controllers' );
2006-10-18 11:22:34 +00:00
2006-10-17 19:37:55 +00:00
$enteredController = '' ;
while ( $enteredController == '' ) {
$enteredController = $this -> getInput ( 'Enter a number from the list above, or type in the name of another controller.' );
2006-05-26 05:29:17 +00:00
2006-10-18 00:16:09 +00:00
if ( $enteredController == '' || intval ( $enteredController ) > count ( $this -> __controllerNames )) {
2006-10-17 19:37:55 +00:00
$this -> stdout ( 'Error:' );
$this -> stdout ( " The Controller name you supplied was empty, or the number \n you selected was not an option. Please try again. " );
$enteredController = '' ;
2006-05-26 05:29:17 +00:00
}
}
2006-10-17 19:37:55 +00:00
2006-10-18 00:16:09 +00:00
if ( intval ( $enteredController ) > 0 && intval ( $enteredController ) <= count ( $this -> __controllerNames ) ) {
$controllerName = $this -> __controllerNames [ intval ( $enteredController ) - 1 ];
2006-10-17 19:37:55 +00:00
} else {
$controllerName = $enteredController ;
}
2006-10-10 18:24:25 +00:00
$controllerPath = $this -> __controllerPath ( $controllerName );
2006-10-14 18:07:52 +00:00
2006-05-26 05:29:17 +00:00
$doItInteractive = $this -> getInput ( " Would you like bake to build your controller interactively? \n Warning: Choosing no will overwrite { $controllerClassName } controller if it exist. " , array ( 'y' , 'n' ), 'y' );
2006-10-31 19:57:17 +00:00
if ( low ( $doItInteractive ) == 'y' || low ( $doItInteractive ) == 'yes' ) {
2006-05-26 05:29:17 +00:00
$this -> interactive = true ;
2006-10-10 18:24:25 +00:00
$wannaDoUses = $this -> getInput ( " Would you like this controller to use other models besides ' " . $this -> __modelName ( $controllerName ) . " '? " , array ( 'y' , 'n' ), 'n' );
2006-05-26 05:29:17 +00:00
2006-10-31 19:57:17 +00:00
if ( low ( $wannaDoUses ) == 'y' || low ( $wannaDoUses ) == 'yes' ) {
2006-05-26 05:29:17 +00:00
$usesList = $this -> getInput ( " Please provide a comma separated list of the classnames of other models you'd like to use. \n Example: 'Author, Article, Book' " );
$usesListTrimmed = str_replace ( ' ' , '' , $usesList );
$uses = explode ( ',' , $usesListTrimmed );
}
2006-07-29 05:58:31 +00:00
$wannaDoHelpers = $this -> getInput ( " Would you like this controller to use other helpers besides HtmlHelper and FormHelper? " , array ( 'y' , 'n' ), 'n' );
2006-05-26 05:29:17 +00:00
2006-10-31 19:57:17 +00:00
if ( low ( $wannaDoHelpers ) == 'y' || low ( $wannaDoHelpers ) == 'yes' ) {
2006-05-26 05:29:17 +00:00
$helpersList = $this -> getInput ( " Please provide a comma separated list of the other helper names you'd like to use. \n Example: 'Ajax, Javascript, Time' " );
$helpersListTrimmed = str_replace ( ' ' , '' , $helpersList );
$helpers = explode ( ',' , $helpersListTrimmed );
}
$wannaDoComponents = $this -> getInput ( " Would you like this controller to use any components? " , array ( 'y' , 'n' ), 'n' );
2006-10-31 19:57:17 +00:00
if ( low ( $wannaDoComponents ) == 'y' || low ( $wannaDoComponents ) == 'yes' ) {
2006-05-26 05:29:17 +00:00
$componentsList = $this -> getInput ( " Please provide a comma separated list of the component names you'd like to use. \n Example: 'Acl, MyNiftyHelper' " );
$componentsListTrimmed = str_replace ( ' ' , '' , $componentsList );
$components = explode ( ',' , $componentsListTrimmed );
}
2006-07-29 05:58:31 +00:00
$wannaUseSession = $this -> getInput ( " Would you like to use Sessions? " , array ( 'y' , 'n' ), 'y' );
2006-06-19 15:00:17 +00:00
$wannaDoScaffolding = $this -> getInput ( " Would you like to include some basic class methods (index(), add(), view(), edit())? " , array ( 'y' , 'n' ), 'n' );
2006-07-29 05:58:31 +00:00
2006-05-26 05:29:17 +00:00
}
2006-10-14 18:07:52 +00:00
2006-10-31 19:57:17 +00:00
if ( low ( $wannaDoScaffolding ) == 'y' || low ( $wannaDoScaffolding ) == 'yes' ) {
2006-10-09 20:27:28 +00:00
$wannaDoAdmin = $this -> getInput ( " Would you like to create the methods for admin routing? " , array ( 'y' , 'n' ), 'n' );
2006-10-17 18:01:58 +00:00
} else {
2006-11-06 09:12:38 +00:00
$wannaUseScaffold = $this -> getInput ( " Would you like to use scaffolding? " , array ( 'y' , 'n' ), 'y' );
2006-10-18 11:22:34 +00:00
}
2006-10-14 18:07:52 +00:00
2006-10-13 09:31:20 +00:00
$admin = null ;
2006-10-20 01:19:46 +00:00
$admin_url = null ;
2006-10-31 19:57:17 +00:00
if (( low ( $wannaDoAdmin ) == 'y' || low ( $wannaDoAdmin ) == 'yes' )) {
2006-10-13 09:31:20 +00:00
require ( CONFIGS . 'core.php' );
2006-10-09 20:27:28 +00:00
if ( defined ( 'CAKE_ADMIN' )) {
$admin = CAKE_ADMIN . '_' ;
2006-10-20 01:19:46 +00:00
$admin_url = '/' . CAKE_ADMIN ;
2006-10-09 20:27:28 +00:00
} else {
2006-10-14 18:07:52 +00:00
$adminRoute = '' ;
2006-10-09 20:27:28 +00:00
$this -> stdout ( 'You need to enable CAKE_ADMIN in /app/config/core.php to use admin routing.' );
2006-10-14 18:07:52 +00:00
$this -> stdout ( 'What would you like the admin route to be?' );
$this -> stdout ( 'Example: www.example.com/admin/controller' );
while ( $adminRoute == '' ) {
$adminRoute = $this -> getInput ( " What would you like the admin route to be? " , null , 'admin' );
}
if ( $this -> __addAdminRoute ( $adminRoute ) !== true ){
$this -> stdout ( 'Unable to write to /app/config/core.php.' );
$this -> stdout ( 'You need to enable CAKE_ADMIN in /app/config/core.php to use admin routing.' );
exit ();
} else {
$admin = $adminRoute . '_' ;
2006-10-20 01:19:46 +00:00
$admin_url = '/' . $adminRoute ;
2006-10-14 18:07:52 +00:00
}
2006-10-09 20:27:28 +00:00
}
}
2006-05-26 05:29:17 +00:00
2006-10-31 19:57:17 +00:00
if ( low ( $wannaDoScaffolding ) == 'y' || low ( $wannaDoScaffolding ) == 'yes' ) {
2006-10-15 11:19:39 +00:00
loadModels ();
2006-10-20 20:58:14 +00:00
$actions = $this -> __bakeActions ( $controllerName , null , null , $wannaUseSession );
2006-10-13 09:31:20 +00:00
if ( $admin ) {
2006-10-20 20:58:14 +00:00
$actions .= $this -> __bakeActions ( $controllerName , $admin , $admin_url , $wannaUseSession );
2006-07-29 05:58:31 +00:00
}
2006-05-26 05:29:17 +00:00
}
if ( $this -> interactive === true ) {
$this -> stdout ( '' );
$this -> hr ();
$this -> stdout ( 'The following controller will be created:' );
$this -> hr ();
$this -> stdout ( " Controller Name: $controllerName " );
if ( count ( $uses )) {
$this -> stdout ( " Uses: " , false );
foreach ( $uses as $use ) {
if ( $use != $uses [ count ( $uses ) - 1 ]) {
$this -> stdout ( ucfirst ( $use ) . " , " , false );
} else {
$this -> stdout ( ucfirst ( $use ));
}
}
}
if ( count ( $helpers )) {
$this -> stdout ( " Helpers: " , false );
foreach ( $helpers as $help ) {
if ( $help != $helpers [ count ( $helpers ) - 1 ]) {
$this -> stdout ( ucfirst ( $help ) . " , " , false );
} else {
$this -> stdout ( ucfirst ( $help ));
}
}
}
if ( count ( $components )) {
$this -> stdout ( " Components: " , false );
foreach ( $components as $comp ) {
if ( $comp != $components [ count ( $components ) - 1 ]) {
$this -> stdout ( ucfirst ( $comp ) . " , " , false );
} else {
$this -> stdout ( ucfirst ( $comp ));
}
}
}
$this -> hr ();
$looksGood = $this -> getInput ( 'Look okay?' , array ( 'y' , 'n' ), 'y' );
2006-10-31 19:57:17 +00:00
if ( low ( $looksGood ) == 'y' || low ( $looksGood ) == 'yes' ) {
2006-10-17 18:01:58 +00:00
$this -> bakeController ( $controllerName , $uses , $helpers , $components , $actions , $wannaUseScaffold );
2006-05-26 05:29:17 +00:00
if ( $this -> doUnitTest ()) {
2006-10-09 20:27:28 +00:00
$this -> bakeUnitTest ( 'controller' , $controllerName );
2006-05-26 05:29:17 +00:00
}
} else {
$this -> stdout ( 'Bake Aborted.' );
}
} else {
2006-10-17 18:01:58 +00:00
$this -> bakeController ( $controllerName , $uses , $helpers , $components , $actions , $wannaUseScaffold );
if ( $this -> doUnitTest ()) {
$this -> bakeUnitTest ( 'controller' , $controllerName );
}
2006-05-26 05:29:17 +00:00
exit ();
}
}
2006-10-14 18:07:52 +00:00
2006-10-20 20:58:14 +00:00
function __bakeActions ( $controllerName , $admin = null , $admin_url = null , $wannaUseSession = 'y' ) {
2006-10-13 09:31:20 +00:00
$currentModelName = $this -> __modelName ( $controllerName );
2006-10-17 18:01:58 +00:00
$modelObj =& new $currentModelName ();
$controllerPath = $this -> __controllerPath ( $currentModelName );
$pluralName = $this -> __pluralName ( $currentModelName );
2006-10-13 09:31:20 +00:00
$singularName = $this -> __singularName ( $currentModelName );
$singularHumanName = $this -> __singularHumanName ( $currentModelName );
$pluralHumanName = $this -> __pluralHumanName ( $controllerName );
if ( ! class_exists ( $currentModelName )) {
$this -> stdout ( 'You must have a model for this class to build scaffold methods. Please try again.' );
exit ;
}
$actions .= " \n " ;
$actions .= " \t function { $admin } index() { \n " ;
$actions .= " \t \t \$ this-> { $currentModelName } ->recursive = 0; \n " ;
2006-10-17 17:39:48 +00:00
$actions .= " \t \t \$ this->set(' { $pluralName } ', \$ this-> { $currentModelName } ->findAll()); \n " ;
2006-10-13 09:31:20 +00:00
$actions .= " \t } \n " ;
$actions .= " \n " ;
$actions .= " \t function { $admin } view( \$ id = null) { \n " ;
$actions .= " \t \t if(! \$ id) { \n " ;
2006-10-31 19:57:17 +00:00
if ( low ( $wannaUseSession ) == 'y' || low ( $wannaUseSession ) == 'yes' ) {
2006-10-20 01:19:46 +00:00
$actions .= " \t \t \t \$ this->Session->setFlash('Invalid id for { $singularHumanName } .'); \n " ;
$actions .= " \t \t \t \$ this->redirect(' { $admin_url } / { $controllerPath } /index'); \n " ;
2006-10-17 18:01:58 +00:00
} else {
2006-10-20 01:19:46 +00:00
$actions .= " \t \t \t \$ this->flash('Invalid id for { $singularHumanName } ', ' { $admin_url } / { $controllerPath } /index'); \n " ;
2006-10-17 18:01:58 +00:00
}
2006-10-13 09:31:20 +00:00
$actions .= " \t \t } \n " ;
$actions .= " \t \t \$ this->set(' " . $singularName . " ', \$ this-> { $currentModelName } ->read(null, \$ id)); \n " ;
$actions .= " \t } \n " ;
$actions .= " \n " ;
2006-10-28 20:15:41 +00:00
/* ADD ACTION */
2006-10-13 09:31:20 +00:00
$actions .= " \t function { $admin } add() { \n " ;
$actions .= " \t \t if(empty( \$ this->data)) { \n " ;
foreach ( $modelObj -> hasAndBelongsToMany as $associationName => $relation ) {
2006-11-03 22:47:43 +00:00
if ( ! empty ( $associationName )) {
$otherModelName = $this -> __modelName ( $associationName );
2006-10-13 09:31:20 +00:00
$otherSingularName = $this -> __singularName ( $associationName );
$otherPluralName = $this -> __pluralName ( $associationName );
2006-10-31 19:57:17 +00:00
$selectedOtherPluralName = 'selected' . ucfirst ( $otherPluralName );
2006-10-13 09:31:20 +00:00
$actions .= " \t \t \t \$ this->set(' { $otherPluralName } ', \$ this-> { $currentModelName } -> { $otherModelName } ->generateList()); \n " ;
2006-10-31 19:57:17 +00:00
$actions .= " \t \t \t \$ this->set(' { $selectedOtherPluralName } ', null); \n " ;
2006-10-13 09:31:20 +00:00
}
}
foreach ( $modelObj -> belongsTo as $associationName => $relation ) {
2006-11-03 22:47:43 +00:00
if ( ! empty ( $associationName )) {
$otherModelName = $this -> __modelName ( $associationName );
2006-10-13 09:31:20 +00:00
$otherSingularName = $this -> __singularName ( $associationName );
$otherPluralName = $this -> __pluralName ( $associationName );
2006-11-03 22:47:43 +00:00
$actions .= " \t \t \t \$ this->set(' { $otherPluralName } ', \$ this-> { $currentModelName } -> { $otherModelName } ->generateList()); \n " ;
2006-10-13 09:31:20 +00:00
}
}
$actions .= " \t \t \t \$ this->render(); \n " ;
$actions .= " \t \t } else { \n " ;
$actions .= " \t \t \t \$ this->cleanUpFields(); \n " ;
$actions .= " \t \t \t if( \$ this-> { $currentModelName } ->save( \$ this->data)) { \n " ;
2006-10-31 19:57:17 +00:00
if ( low ( $wannaUseSession ) == 'y' || low ( $wannaUseSession ) == 'yes' ) {
2006-10-28 20:15:41 +00:00
$actions .= " \t \t \t \t \$ this->Session->setFlash('The " . $this -> __singularHumanName ( $currentModelName ) . " has been saved'); \n " ;
2006-10-20 01:19:46 +00:00
$actions .= " \t \t \t \t \$ this->redirect(' { $admin_url } / { $controllerPath } /index'); \n " ;
2006-10-13 09:31:20 +00:00
} else {
2006-10-20 01:19:46 +00:00
$actions .= " \t \t \t \t \$ this->flash(' { $currentModelName } saved.', ' { $admin_url } / { $controllerPath } /index'); \n " ;
2006-10-13 09:31:20 +00:00
}
$actions .= " \t \t \t } else { \n " ;
2006-10-31 19:57:17 +00:00
if ( low ( $wannaUseSession ) == 'y' || low ( $wannaUseSession ) == 'yes' ) {
2006-10-20 01:19:46 +00:00
$actions .= " \t \t \t \t \$ this->Session->setFlash('Please correct errors below.'); \n " ;
2006-10-13 09:31:20 +00:00
}
foreach ( $modelObj -> hasAndBelongsToMany as $associationName => $relation ) {
2006-11-03 22:47:43 +00:00
if ( ! empty ( $associationName )) {
$otherModelName = $this -> __modelName ( $associationName );
2006-10-13 09:31:20 +00:00
$otherSingularName = $this -> __singularName ( $associationName );
$otherPluralName = $this -> __pluralName ( $associationName );
2006-10-31 19:57:17 +00:00
$selectedOtherPluralName = 'selected' . ucfirst ( $otherPluralName );
2006-10-13 09:31:20 +00:00
$actions .= " \t \t \t \t \$ this->set(' { $otherPluralName } ', \$ this-> { $currentModelName } -> { $otherModelName } ->generateList()); \n " ;
$actions .= " \t \t \t \t if(empty( \$ this->data[' { $associationName } '][' { $associationName } '])) { \$ this->data[' { $associationName } '][' { $associationName } '] = null; } \n " ;
2006-10-31 19:57:17 +00:00
$actions .= " \t \t \t \t \$ this->set(' { $selectedOtherPluralName } ', \$ this->data[' { $associationName } '][' { $associationName } ']); \n " ;
2006-10-13 09:31:20 +00:00
}
}
foreach ( $modelObj -> belongsTo as $associationName => $relation ) {
2006-11-03 22:47:43 +00:00
if ( ! empty ( $associationName )) {
$otherModelName = $this -> __modelName ( $associationName );
2006-10-13 09:31:20 +00:00
$otherSingularName = $this -> __singularName ( $associationName );
$otherPluralName = $this -> __pluralName ( $associationName );
2006-11-03 22:47:43 +00:00
$actions .= " \t \t \t \t \$ this->set(' { $otherPluralName } ', \$ this-> { $currentModelName } -> { $otherModelName } ->generateList()); \n " ;
2006-10-13 09:31:20 +00:00
}
}
$actions .= " \t \t \t } \n " ;
$actions .= " \t \t } \n " ;
$actions .= " \t } \n " ;
$actions .= " \n " ;
2006-10-28 20:15:41 +00:00
/* EDIT ACTION */
2006-10-17 18:01:58 +00:00
$actions .= " \t function { $admin } edit( \$ id = null) { \n " ;
2006-10-13 09:31:20 +00:00
$actions .= " \t \t if(empty( \$ this->data)) { \n " ;
2006-10-17 18:01:58 +00:00
$actions .= " \t \t \t if(! \$ id) { \n " ;
2006-10-31 19:57:17 +00:00
if ( low ( $wannaUseSession ) == 'y' || low ( $wannaUseSession ) == 'yes' ) {
2006-10-20 01:19:46 +00:00
$actions .= " \t \t \t \t \$ this->Session->setFlash('Invalid id for { $singularHumanName } '); \n " ;
$actions .= " \t \t \t \t \$ this->redirect(' { $admin_url } / { $controllerPath } /index'); \n " ;
2006-10-17 18:01:58 +00:00
} else {
2006-10-20 01:19:46 +00:00
$actions .= " \t \t \t \t \$ this->flash('Invalid id for { $singularHumanName } ', ' { $admin_url } / { $controllerPath } /index'); \n " ;
2006-10-17 18:01:58 +00:00
}
$actions .= " \t \t \t } \n " ;
2006-10-13 09:31:20 +00:00
$actions .= " \t \t \t \$ this->data = \$ this-> { $currentModelName } ->read(null, \$ id); \n " ;
foreach ( $modelObj -> hasAndBelongsToMany as $associationName => $relation ) {
2006-11-03 22:47:43 +00:00
if ( ! empty ( $associationName )) {
$otherModelName = $this -> __modelName ( $associationName );
2006-11-28 09:34:25 +00:00
$otherSingularName = $this -> __singularName ( $relation [ 'className' ]);
$otherPluralName = $this -> __pluralName ( $relation [ 'className' ]);
$otherModelKey = Inflector :: underscore ( $relation [ 'className' ]);
2006-10-13 09:31:20 +00:00
$otherModelObj =& ClassRegistry :: getObject ( $otherModelKey );
2006-10-31 19:57:17 +00:00
$selectedOtherPluralName = 'selected' . ucfirst ( $otherPluralName );
2006-10-13 09:31:20 +00:00
$actions .= " \t \t \t \$ this->set(' { $otherPluralName } ', \$ this-> { $currentModelName } -> { $otherModelName } ->generateList()); \n " ;
2006-10-28 20:15:41 +00:00
$actions .= " \t \t \t if(empty( \$ this->data[' { $associationName } '])) { \$ this->data[' { $associationName } '] = null; } \n " ;
2006-10-31 19:57:17 +00:00
$actions .= " \t \t \t \$ this->set(' { $selectedOtherPluralName } ', \$ this->_selectedArray( \$ this->data[' { $associationName } '])); \n " ;
2006-10-13 09:31:20 +00:00
}
}
foreach ( $modelObj -> belongsTo as $associationName => $relation ) {
2006-11-03 22:47:43 +00:00
if ( ! empty ( $associationName )) {
$otherModelName = $this -> __modelName ( $associationName );
2006-10-13 09:31:20 +00:00
$otherSingularName = $this -> __singularName ( $associationName );
$otherPluralName = $this -> __pluralName ( $associationName );
2006-11-03 22:47:43 +00:00
$actions .= " \t \t \t \$ this->set(' { $otherPluralName } ', \$ this-> { $currentModelName } -> { $otherModelName } ->generateList()); \n " ;
2006-10-13 09:31:20 +00:00
}
}
$actions .= " \t \t } else { \n " ;
$actions .= " \t \t \t \$ this->cleanUpFields(); \n " ;
$actions .= " \t \t \t if( \$ this-> { $currentModelName } ->save( \$ this->data)) { \n " ;
2006-10-31 19:57:17 +00:00
if ( low ( $wannaUseSession ) == 'y' || low ( $wannaUseSession ) == 'yes' ) {
2006-10-20 01:19:46 +00:00
$actions .= " \t \t \t \t \$ this->Session->setFlash('The " . Inflector :: humanize ( $currentModelName ) . " has been saved'); \n " ;
$actions .= " \t \t \t \t \$ this->redirect(' { $admin_url } / { $controllerPath } /index'); \n " ;
2006-10-13 09:31:20 +00:00
} else {
2006-10-20 01:19:46 +00:00
$actions .= " \t \t \t \t \$ this->flash(' { $currentModelName } saved.', ' { $admin_url } / { $controllerPath } /index'); \n " ;
2006-10-13 09:31:20 +00:00
}
$actions .= " \t \t \t } else { \n " ;
2006-10-31 19:57:17 +00:00
if ( low ( $wannaUseSession ) == 'y' || low ( $wannaUseSession ) == 'yes' ) {
2006-10-20 01:19:46 +00:00
$actions .= " \t \t \t \t \$ this->Session->setFlash('Please correct errors below.'); \n " ;
2006-10-13 09:31:20 +00:00
}
foreach ( $modelObj -> hasAndBelongsToMany as $associationName => $relation ) {
2006-11-03 22:47:43 +00:00
if ( ! empty ( $associationName )) {
$otherModelName = $this -> __modelName ( $associationName );
2006-10-13 09:31:20 +00:00
$otherSingularName = $this -> __singularName ( $associationName );
$otherPluralName = $this -> __pluralName ( $associationName );
2006-10-31 19:57:17 +00:00
$selectedOtherPluralName = 'selected' . ucfirst ( $otherPluralName );
2006-10-13 09:31:20 +00:00
$actions .= " \t \t \t \t \$ this->set(' { $otherPluralName } ', \$ this-> { $currentModelName } -> { $otherModelName } ->generateList()); \n " ;
$actions .= " \t \t \t \t if(empty( \$ this->data[' { $associationName } '][' { $associationName } '])) { \$ this->data[' { $associationName } '][' { $associationName } '] = null; } \n " ;
2006-10-31 19:57:17 +00:00
$actions .= " \t \t \t \t \$ this->set(' { $selectedOtherPluralName } ', \$ this->data[' { $associationName } '][' { $associationName } ']); \n " ;
2006-10-13 09:31:20 +00:00
}
}
foreach ( $modelObj -> belongsTo as $associationName => $relation ) {
2006-11-03 22:47:43 +00:00
if ( ! empty ( $associationName )) {
$otherModelName = $this -> __modelName ( $associationName );
2006-10-13 09:31:20 +00:00
$otherSingularName = $this -> __singularName ( $associationName );
$otherPluralName = $this -> __pluralName ( $associationName );
2006-11-03 22:47:43 +00:00
$actions .= " \t \t \t \t \$ this->set(' { $otherPluralName } ', \$ this-> { $currentModelName } -> { $otherModelName } ->generateList()); \n " ;
2006-10-13 09:31:20 +00:00
}
}
$actions .= " \t \t \t } \n " ;
$actions .= " \t \t } \n " ;
$actions .= " \t } \n " ;
$actions .= " \n " ;
$actions .= " \t function { $admin } delete( \$ id = null) { \n " ;
$actions .= " \t \t if(! \$ id) { \n " ;
2006-10-31 19:57:17 +00:00
if ( low ( $wannaUseSession ) == 'y' || low ( $wannaUseSession ) == 'yes' ) {
2006-10-20 01:19:46 +00:00
$actions .= " \t \t \t \$ this->Session->setFlash('Invalid id for { $singularHumanName } '); \n " ;
$actions .= " \t \t \t \$ this->redirect(' { $admin_url } / { $controllerPath } /index'); \n " ;
2006-10-17 18:01:58 +00:00
} else {
2006-10-20 01:19:46 +00:00
$actions .= " \t \t \t \$ this->flash('Invalid id for { $singularHumanName } ', ' { $admin_url } / { $controllerPath } /index'); \n " ;
2006-10-17 18:01:58 +00:00
}
2006-10-13 09:31:20 +00:00
$actions .= " \t \t } \n " ;
$actions .= " \t \t if( \$ this-> { $currentModelName } ->del( \$ id)) { \n " ;
2006-10-31 19:57:17 +00:00
if ( low ( $wannaUseSession ) == 'y' || low ( $wannaUseSession ) == 'yes' ) {
2006-10-20 01:19:46 +00:00
$actions .= " \t \t \t \$ this->Session->setFlash('The " . $this -> __singularHumanName ( $currentModelName ) . " deleted: id '. \$ id.''); \n " ;
$actions .= " \t \t \t \$ this->redirect(' { $admin_url } / { $controllerPath } /index'); \n " ;
2006-10-13 09:31:20 +00:00
} else {
2006-10-20 01:19:46 +00:00
$actions .= " \t \t \t \$ this->flash(' { $currentModelName } deleted: id '. \$ id.'.', ' { $admin_url } / { $controllerPath } /index'); \n " ;
2006-10-13 09:31:20 +00:00
}
$actions .= " \t \t } \n " ;
$actions .= " \t } \n " ;
$actions .= " \n " ;
return $actions ;
}
2006-05-26 05:29:17 +00:00
/**
2006-07-18 11:11:44 +00:00
* Action to create a Unit Test .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ return Success
2006-05-26 05:29:17 +00:00
*/
function doUnitTest () {
2006-11-06 09:12:38 +00:00
if ( is_dir ( VENDORS . 'simpletest' ) || is_dir ( ROOT . DS . APP_DIR . DS . 'vendors' . DS . 'simpletest' )) {
2006-05-26 05:29:17 +00:00
return true ;
}
$unitTest = $this -> getInput ( 'Cake test suite not installed. Do you want to bake unit test files anyway?' , array ( 'y' , 'n' ), 'y' );
2006-10-31 19:57:17 +00:00
$result = low ( $unitTest ) == 'y' || low ( $unitTest ) == 'yes' ;
2006-05-26 05:29:17 +00:00
if ( $result ) {
2006-11-23 06:51:50 +00:00
$this -> stdout ( " \n You can download the Cake test suite from http://cakeforge.org/projects/testsuite/ " , true );
2006-05-26 05:29:17 +00:00
}
return $result ;
}
/**
2006-07-18 11:11:44 +00:00
* Creates a database configuration file for Bake .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ param string $host
* @ param string $login
* @ param string $password
* @ param string $database
2006-05-26 05:29:17 +00:00
*/
2006-10-09 20:27:28 +00:00
function bakeDbConfig ( $driver , $connect , $host , $login , $password , $database , $prefix ) {
2006-05-26 05:29:17 +00:00
$out = " <?php \n " ;
2006-10-15 11:19:39 +00:00
$out .= " class DATABASE_CONFIG { \n \n " ;
2006-05-26 05:29:17 +00:00
$out .= " \t var \$ default = array( \n " ;
2006-10-09 20:27:28 +00:00
$out .= " \t \t 'driver' => ' { $driver } ', \n " ;
$out .= " \t \t 'connect' => ' { $connect } ', \n " ;
$out .= " \t \t 'host' => ' { $host } ', \n " ;
$out .= " \t \t 'login' => ' { $login } ', \n " ;
$out .= " \t \t 'password' => ' { $password } ', \n " ;
2006-10-10 20:00:35 +00:00
$out .= " \t \t 'database' => ' { $database } ', \n " ;
2006-10-09 20:27:28 +00:00
$out .= " \t \t 'prefix' => ' { $prefix } ' \n " ;
2006-05-26 05:29:17 +00:00
$out .= " \t ); \n " ;
$out .= " } \n " ;
$out .= " ?> " ;
$filename = CONFIGS . 'database.php' ;
2006-10-31 19:57:17 +00:00
$this -> __createFile ( $filename , $out );
2006-05-26 05:29:17 +00:00
}
/**
2006-07-18 11:11:44 +00:00
* Assembles and writes a Model file .
2006-05-26 05:29:17 +00:00
*
2006-10-15 11:19:39 +00:00
* @ param string $name
* @ param object $useDbConfig
* @ param string $useTable
* @ param string $primaryKey
2006-07-18 11:11:44 +00:00
* @ param array $validate
2006-10-15 11:19:39 +00:00
* @ param array $associations
2006-05-26 05:29:17 +00:00
*/
2006-10-15 11:19:39 +00:00
function bakeModel ( $name , $useDbConfig = 'default' , $useTable = null , $primaryKey = 'id' , $validate = array (), $associations = array ()) {
2006-05-26 05:29:17 +00:00
$out = " <?php \n " ;
2006-10-15 11:19:39 +00:00
$out .= " class { $name } extends AppModel { \n \n " ;
$out .= " \t var \$ name = ' { $name } '; \n " ;
2006-05-26 05:29:17 +00:00
2006-10-15 11:19:39 +00:00
if ( $useDbConfig != 'default' ) {
$out .= " \t var \$ useDbConfig = ' $useDbConfig '; \n " ;
2006-05-26 05:29:17 +00:00
}
2006-10-15 11:19:39 +00:00
if ( $useTable != null ) {
$out .= " \t var \$ useTable = ' $useTable '; \n " ;
}
2006-10-18 11:22:34 +00:00
2006-10-17 16:49:09 +00:00
if ( $primaryKey != 'id' ) {
2006-10-15 11:19:39 +00:00
$out .= " \t var \$ primaryKey = ' $primaryKey '; \n " ;
2006-05-26 05:29:17 +00:00
}
2006-10-18 11:22:34 +00:00
2006-05-26 05:29:17 +00:00
if ( count ( $validate )) {
$out .= " \t var \$ validate = array( \n " ;
$keys = array_keys ( $validate );
for ( $i = 0 ; $i < count ( $validate ); $i ++ ) {
$out .= " \t \t ' " . $keys [ $i ] . " ' => " . $validate [ $keys [ $i ]] . " , \n " ;
}
$out .= " \t ); \n " ;
}
$out .= " \n " ;
2006-10-15 11:19:39 +00:00
if ( ! empty ( $associations )) {
2006-05-26 05:29:17 +00:00
$out .= " \t //The Associations below have been created with all possible keys, those that are not needed can be removed \n " ;
2006-10-15 11:19:39 +00:00
if ( ! empty ( $associations [ 'belongsTo' ])) {
2006-05-26 05:29:17 +00:00
$out .= " \t var \$ belongsTo = array( \n " ;
2006-10-15 11:19:39 +00:00
for ( $i = 0 ; $i < count ( $associations [ 'belongsTo' ]); $i ++ ) {
2006-11-29 06:44:45 +00:00
$out .= " \t \t \t ' { $associations [ 'belongsTo' ][ $i ][ 'alias' ] } ' => " ;
$out .= " array('className' => ' { $associations [ 'belongsTo' ][ $i ][ 'className' ] } ', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'foreignKey' => ' { $associations [ 'belongsTo' ][ $i ][ 'foreignKey' ] } ', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'conditions' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'fields' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'order' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'counterCache' => '' " ;
$out .= " ), \n " ;
2006-05-26 05:29:17 +00:00
}
$out .= " \t ); \n \n " ;
}
2006-10-15 11:19:39 +00:00
if ( ! empty ( $associations [ 'hasOne' ])) {
2006-05-26 05:29:17 +00:00
$out .= " \t var \$ hasOne = array( \n " ;
2006-10-15 11:19:39 +00:00
for ( $i = 0 ; $i < count ( $associations [ 'hasOne' ]); $i ++ ) {
2006-11-29 06:44:45 +00:00
$out .= " \t \t \t ' { $associations [ 'hasOne' ][ $i ][ 'alias' ] } ' => " ;
$out .= " array('className' => ' { $associations [ 'hasOne' ][ $i ][ 'className' ] } ', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'foreignKey' => ' { $associations [ 'hasOne' ][ $i ][ 'foreignKey' ] } ', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'conditions' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'fields' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'order' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'dependent' => '' " ;
$out .= " ), \n " ;
2006-05-26 05:29:17 +00:00
}
$out .= " \t ); \n \n " ;
}
2006-10-15 11:19:39 +00:00
if ( ! empty ( $associations [ 'hasMany' ])) {
2006-05-26 05:29:17 +00:00
$out .= " \t var \$ hasMany = array( \n " ;
2006-10-15 11:19:39 +00:00
for ( $i = 0 ; $i < count ( $associations [ 'hasMany' ]); $i ++ ) {
2006-11-29 06:44:45 +00:00
$out .= " \t \t \t ' { $associations [ 'hasMany' ][ $i ][ 'alias' ] } ' => " ;
$out .= " array('className' => ' { $associations [ 'hasMany' ][ $i ][ 'className' ] } ', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'foreignKey' => ' { $associations [ 'hasMany' ][ $i ][ 'foreignKey' ] } ', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'conditions' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'fields' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'order' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'limit' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'offset' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'dependent' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'exclusive' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'finderQuery' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'counterQuery' => '' " ;
$out .= " ), \n " ;
2006-05-26 05:29:17 +00:00
}
$out .= " \t ); \n \n " ;
}
2006-10-15 11:19:39 +00:00
if ( ! empty ( $associations [ 'hasAndBelongsToMany' ])) {
2006-05-26 05:29:17 +00:00
$out .= " \t var \$ hasAndBelongsToMany = array( \n " ;
2006-10-15 11:19:39 +00:00
for ( $i = 0 ; $i < count ( $associations [ 'hasAndBelongsToMany' ]); $i ++ ) {
2006-11-29 06:44:45 +00:00
$out .= " \t \t \t ' { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ] } ' => " ;
$out .= " array('className' => ' { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'className' ] } ', \n " ;
2006-10-31 19:57:17 +00:00
$out .= " \t \t \t \t \t \t 'joinTable' => ' { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'joinTable' ] } ', \n " ;
$out .= " \t \t \t \t \t \t 'foreignKey' => ' { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'foreignKey' ] } ', \n " ;
$out .= " \t \t \t \t \t \t 'associationForeignKey' => ' { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'associationForeignKey' ] } ', \n " ;
$out .= " \t \t \t \t \t \t 'conditions' => '', \n " ;
$out .= " \t \t \t \t \t \t 'fields' => '', \n " ;
$out .= " \t \t \t \t \t \t 'order' => '', \n " ;
$out .= " \t \t \t \t \t \t 'limit' => '', \n " ;
$out .= " \t \t \t \t \t \t 'offset' => '', \n " ;
$out .= " \t \t \t \t \t \t 'uniq' => '', \n " ;
$out .= " \t \t \t \t \t \t 'finderQuery' => '', \n " ;
$out .= " \t \t \t \t \t \t 'deleteQuery' => '', \n " ;
2006-11-29 06:44:45 +00:00
$out .= " \t \t \t \t \t \t 'insertQuery' => '' " ;
$out .= " ), \n " ;
2006-05-26 05:29:17 +00:00
}
$out .= " \t ); \n \n " ;
}
}
$out .= " } \n " ;
$out .= " ?> " ;
2006-10-15 11:19:39 +00:00
$filename = MODELS . Inflector :: underscore ( $name ) . '.php' ;
2006-10-31 19:57:17 +00:00
$this -> __createFile ( $filename , $out );
2006-05-26 05:29:17 +00:00
}
/**
2006-07-18 11:11:44 +00:00
* Assembles and writes a View file .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ param string $controllerName
* @ param string $actionName
* @ param string $content
2006-05-26 05:29:17 +00:00
*/
function bakeView ( $controllerName , $actionName , $content = '' ) {
2006-11-28 09:34:25 +00:00
$out = " <h2> { $actionName } </h2> \n " ;
2006-05-26 05:29:17 +00:00
$out .= $content ;
2006-10-10 18:24:25 +00:00
if ( ! file_exists ( VIEWS . $this -> __controllerPath ( $controllerName ))) {
mkdir ( VIEWS . $this -> __controllerPath ( $controllerName ));
2006-05-26 05:29:17 +00:00
}
2006-11-28 09:34:25 +00:00
$filename = VIEWS . $this -> __controllerPath ( $controllerName ) . DS . Inflector :: underscore ( $actionName ) . '.ctp' ;
2006-10-31 19:57:17 +00:00
$this -> __createFile ( $filename , $out );
2006-05-26 05:29:17 +00:00
}
/**
2006-07-18 11:11:44 +00:00
* Assembles and writes a Controller file .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ param string $controllerName
* @ param array $uses
* @ param array $helpers
* @ param array $components
* @ param string $actions
2006-05-26 05:29:17 +00:00
*/
2006-10-17 18:01:58 +00:00
function bakeController ( $controllerName , $uses , $helpers , $components , $actions = '' , $wannaUseScaffold = 'y' ) {
2006-05-26 05:29:17 +00:00
$out = " <?php \n " ;
2006-10-15 11:19:39 +00:00
$out .= " class $controllerName " . " Controller extends AppController { \n \n " ;
2006-10-31 19:57:17 +00:00
if ( low ( $wannaUseScaffold ) == 'y' || low ( $wannaUseScaffold ) == 'yes' ) {
2006-10-17 18:01:58 +00:00
$out .= " \t var \$ scaffold; \n " ;
}
2006-05-26 05:29:17 +00:00
$out .= " \t var \$ name = ' $controllerName '; \n " ;
if ( count ( $uses )) {
2006-10-09 20:27:28 +00:00
$out .= " \t var \$ uses = array(' " . $this -> __modelName ( $controllerName ) . " ', " ;
2006-05-26 05:29:17 +00:00
foreach ( $uses as $use ) {
if ( $use != $uses [ count ( $uses ) - 1 ]) {
2006-10-09 20:27:28 +00:00
$out .= " ' " . $this -> __modelName ( $use ) . " ', " ;
2006-05-26 05:29:17 +00:00
} else {
2006-10-09 20:27:28 +00:00
$out .= " ' " . $this -> __modelName ( $use ) . " ' " ;
2006-05-26 05:29:17 +00:00
}
}
$out .= " ); \n " ;
}
2006-07-29 05:58:31 +00:00
$out .= " \t var \$ helpers = array('Html', 'Form' " ;
if ( count ( $helpers )) {
foreach ( $helpers as $help ) {
if ( $help != $helpers [ count ( $helpers ) - 1 ]) {
2006-10-09 20:27:28 +00:00
$out .= " , ' " . Inflector :: camelize ( $help ) . " ' " ;
2006-07-29 05:58:31 +00:00
} else {
2006-10-09 20:27:28 +00:00
$out .= " , ' " . Inflector :: camelize ( $help ) . " ' " ;
2006-07-29 05:58:31 +00:00
}
2006-05-26 05:29:17 +00:00
}
}
$out .= " ); \n " ;
if ( count ( $components )) {
$out .= " \t var \$ components = array( " ;
foreach ( $components as $comp ) {
if ( $comp != $components [ count ( $components ) - 1 ]) {
2006-10-09 20:27:28 +00:00
$out .= " ' " . Inflector :: camelize ( $comp ) . " ', " ;
2006-05-26 05:29:17 +00:00
} else {
2006-10-09 20:27:28 +00:00
$out .= " ' " . Inflector :: camelize ( $comp ) . " ' " ;
2006-05-26 05:29:17 +00:00
}
}
$out .= " ); \n " ;
}
$out .= $actions ;
$out .= " } \n " ;
$out .= " ?> " ;
2006-10-10 18:24:25 +00:00
$filename = CONTROLLERS . $this -> __controllerPath ( $controllerName ) . '_controller.php' ;
2006-10-31 19:57:17 +00:00
$this -> __createFile ( $filename , $out );
2006-05-26 05:29:17 +00:00
}
/**
2006-07-18 11:11:44 +00:00
* Assembles and writes a unit test file .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ param string $type One of " model " , and " controller " .
* @ param string $className
2006-05-26 05:29:17 +00:00
*/
function bakeUnitTest ( $type , $className ) {
$out = '<?php ' . " \n \n " ;
$error = false ;
switch ( $type ) {
case 'model' :
2006-11-23 06:46:10 +00:00
$out .= " loadModel(' $className '); \n \n " ;
2006-10-15 11:19:39 +00:00
$out .= " class { $className } TestCase extends UnitTestCase { \n " ;
2006-05-26 05:29:17 +00:00
$out .= " \t var \$ object = null; \n \n " ;
2006-11-23 06:51:50 +00:00
$out .= " \t function setUp() { \n \t \t \$ this->object = new { $className } (); \n " ;
$out .= " \t } \n \n \t function tearDown() { \n \t \t unset( \$ this->object); \n \t } \n " ;
$out .= " \n \t /* \n \t function testMe() { \n " ;
2006-05-26 05:29:17 +00:00
$out .= " \t \t \$ result = \$ this->object->doSomething(); \n " ;
$out .= " \t \t \$ expected = 1; \n " ;
2006-11-06 09:12:38 +00:00
$out .= " \t \t \$ this->assertEqual( \$ result, \$ expected); \n \t } \n \t */ \n } " ;
2006-05-26 05:29:17 +00:00
$path = MODEL_TESTS ;
2006-10-09 20:27:28 +00:00
$filename = $this -> __singularName ( $className ) . '.test.php' ;
2006-05-26 05:29:17 +00:00
break ;
case 'controller' :
2006-11-06 09:12:38 +00:00
$out .= " loadController(' $className '); \n \n " ;
2006-10-15 11:19:39 +00:00
$out .= " class { $className } ControllerTestCase extends UnitTestCase { \n " ;
2006-05-26 05:29:17 +00:00
$out .= " \t var \$ object = null; \n \n " ;
2006-11-23 06:51:50 +00:00
$out .= " \t function setUp() { \n \t \t \$ this->object = new { $className } Controller(); \n " ;
$out .= " \t } \n \n \t function tearDown() { \n \t \t unset( \$ this->object); \n \t } \n " ;
$out .= " \n \t /* \n \t function testMe() { \n " ;
2006-05-26 05:29:17 +00:00
$out .= " \t \t \$ result = \$ this->object->doSomething(); \n " ;
$out .= " \t \t \$ expected = 1; \n " ;
2006-11-06 09:12:38 +00:00
$out .= " \t \t \$ this->assertEqual( \$ result, \$ expected); \n \t } \n \t */ \n } " ;
2006-05-26 05:29:17 +00:00
$path = CONTROLLER_TESTS ;
2006-11-23 06:51:50 +00:00
$filename = $this -> __pluralName ( $className ) . '_controller.test.php' ;
2006-05-26 05:29:17 +00:00
break ;
default :
$error = true ;
break ;
}
$out .= " \n ?> " ;
if ( ! $error ) {
$this -> stdout ( " Baking unit test for $className ... " );
$path = explode ( DS , $path );
foreach ( $path as $i => $val ) {
2006-10-09 20:27:28 +00:00
if ( $val == '' || $val == '../' ) {
2006-05-26 05:29:17 +00:00
unset ( $path [ $i ]);
}
}
$path = implode ( DS , $path );
2006-06-19 01:44:18 +00:00
$unixPath = DS ;
2006-06-19 01:47:08 +00:00
if ( strpos ( PHP_OS , 'WIN' ) === 0 ){
2006-06-19 01:44:18 +00:00
$unixPath = null ;
}
if ( ! is_dir ( $unixPath . $path )) {
2006-05-26 05:29:17 +00:00
$create = $this -> getInput ( " Unit test directory does not exist. Create it? " , array ( 'y' , 'n' ), 'y' );
if ( low ( $create ) == 'y' || low ( $create ) == 'yes' ) {
$build = array ();
2006-06-19 01:44:18 +00:00
2006-05-26 05:29:17 +00:00
foreach ( explode ( DS , $path ) as $i => $dir ) {
$build [] = $dir ;
2006-06-19 01:44:18 +00:00
if ( ! is_dir ( $unixPath . implode ( DS , $build ))) {
mkdir ( $unixPath . implode ( DS , $build ));
2006-05-26 05:29:17 +00:00
}
}
2006-10-15 06:45:13 +00:00
} else {
return false ;
2006-05-26 05:29:17 +00:00
}
2006-10-18 11:22:34 +00:00
}
2006-10-31 19:57:17 +00:00
$this -> __createFile ( $unixPath . $path . DS . $filename , $out );
2006-05-26 05:29:17 +00:00
}
}
/**
2006-07-18 11:11:44 +00:00
* Prompts the user for input , and returns it .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ param string $prompt Prompt text .
* @ param mixed $options Array or string of options .
* @ param string $default Default input value .
* @ return Either the default value , or the user - provided input .
2006-05-26 05:29:17 +00:00
*/
function getInput ( $prompt , $options = null , $default = null ) {
if ( ! is_array ( $options )) {
$print_options = '' ;
} else {
$print_options = '(' . implode ( '/' , $options ) . ')' ;
}
if ( $default == null ) {
$this -> stdout ( '' );
$this -> stdout ( $prompt . " $print_options \n " . '> ' , false );
} else {
$this -> stdout ( '' );
$this -> stdout ( $prompt . " $print_options \n " . " [ $default ] > " , false );
}
$result = trim ( fgets ( $this -> stdin ));
if ( $default != null && empty ( $result )) {
return $default ;
} else {
return $result ;
}
}
/**
2006-07-18 11:11:44 +00:00
* Outputs to the stdout filehandle .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ param string $string String to output .
* @ param boolean $newline If true , the outputs gets an added newline .
2006-05-26 05:29:17 +00:00
*/
function stdout ( $string , $newline = true ) {
if ( $newline ) {
2006-11-28 09:34:25 +00:00
fwrite ( $this -> stdout , $string . " \n " );
2006-05-26 05:29:17 +00:00
} else {
2006-11-28 09:34:25 +00:00
fwrite ( $this -> stdout , $string );
2006-05-26 05:29:17 +00:00
}
}
/**
2006-07-18 11:11:44 +00:00
* Outputs to the stderr filehandle .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ param string $string Error text to output .
2006-05-26 05:29:17 +00:00
*/
function stderr ( $string ) {
2006-10-28 20:15:41 +00:00
fwrite ( $this -> stderr , __ ( $string , true ));
2006-05-26 05:29:17 +00:00
}
/**
2006-07-18 11:11:44 +00:00
* Outputs a series of minus characters to the standard output , acts as a visual separator .
2006-05-26 05:29:17 +00:00
*
*/
function hr () {
$this -> stdout ( '---------------------------------------------------------------' );
}
/**
2006-07-18 11:11:44 +00:00
* Creates a file at given path .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ param string $path Where to put the file .
* @ param string $contents Content to put in the file .
* @ return Success
2006-05-26 05:29:17 +00:00
*/
2006-10-31 19:57:17 +00:00
function __createFile ( $path , $contents ) {
2006-07-23 18:59:52 +00:00
$path = str_replace ( '//' , '/' , $path );
2006-10-31 19:57:17 +00:00
echo " \n Creating file $path\n " ;
2006-05-26 05:29:17 +00:00
if ( is_file ( $path ) && $this -> interactive === true ) {
2006-10-31 19:57:17 +00:00
fwrite ( $this -> stdout , __ ( " File exists, overwrite? " , true ) . " { $path } (y/n/q): " );
2006-05-26 05:29:17 +00:00
$key = trim ( fgets ( $this -> stdin ));
if ( $key == 'q' ) {
2006-10-28 20:15:41 +00:00
fwrite ( $this -> stdout , __ ( " Quitting. " , true ) . " \n " );
2006-05-26 05:29:17 +00:00
exit ;
} elseif ( $key == 'a' ) {
$this -> dont_ask = true ;
} elseif ( $key == 'y' ) {
} else {
2006-10-31 19:57:17 +00:00
fwrite ( $this -> stdout , __ ( " Skip " , true ) . " { $path } \n " );
2006-05-26 05:29:17 +00:00
return false ;
}
}
if ( $f = fopen ( $path , 'w' )) {
fwrite ( $f , $contents );
fclose ( $f );
2006-10-31 19:57:17 +00:00
fwrite ( $this -> stdout , __ ( " Wrote " , true ) . " { $path } \n " );
2006-05-26 05:29:17 +00:00
return true ;
} else {
2006-10-31 19:57:17 +00:00
fwrite ( $this -> stderr , __ ( " Error! Could not write to " , true ) . " { $path } . \n " );
2006-05-26 05:29:17 +00:00
return false ;
}
}
2006-11-28 09:34:25 +00:00
2006-05-26 05:29:17 +00:00
/**
2006-07-18 11:11:44 +00:00
* Outputs usage text on the standard output .
2006-05-26 05:29:17 +00:00
*
*/
function help () {
$this -> stdout ( 'CakePHP Bake:' );
$this -> hr ();
$this -> stdout ( 'The Bake script generates controllers, views and models for your application.' );
$this -> stdout ( 'If run with no command line arguments, Bake guides the user through the class' );
2006-06-19 07:23:27 +00:00
$this -> stdout ( 'creation process. You can customize the generation process by telling Bake' );
2006-05-26 05:29:17 +00:00
$this -> stdout ( 'where different parts of your application are using command line arguments.' );
$this -> stdout ( '' );
$this -> hr ( '' );
$this -> stdout ( 'usage: php bake.php [command] [path...]' );
$this -> stdout ( '' );
$this -> stdout ( 'commands:' );
$this -> stdout ( ' -app [path...] Absolute path to Cake\'s app Folder.' );
$this -> stdout ( ' -core [path...] Absolute path to Cake\'s cake Folder.' );
$this -> stdout ( ' -help Shows this help message.' );
$this -> stdout ( ' -project [path...] Generates a new app folder in the path supplied.' );
$this -> stdout ( ' -root [path...] Absolute path to Cake\'s \app\webroot Folder.' );
$this -> stdout ( '' );
}
/**
2006-07-23 18:59:52 +00:00
* Checks that given project path does not already exist , and
2006-07-18 11:11:44 +00:00
* finds the app directory in it . Then it calls __buildDirLayout () with that information .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ param string $projectPath
2006-05-26 05:29:17 +00:00
*/
2006-10-10 22:05:41 +00:00
function project ( $projectPath = null ) {
2006-05-26 05:29:17 +00:00
if ( $projectPath != '' ) {
while ( $this -> __checkPath ( $projectPath ) === true ) {
2006-11-23 06:46:10 +00:00
$response = $this -> getInput ( 'Bake -app in ' . $projectPath , array ( 'y' , 'n' ), 'y' );
if ( low ( $response ) == 'y' ) {
$this -> main ();
} else {
$projectPath = $this -> getInput ( " What is the full path for this app including the app directory name? \n Example: " . ROOT . DS . " myapp " , null , ROOT . DS . 'myapp' );
}
2006-05-26 05:29:17 +00:00
}
} else {
while ( $projectPath == '' ) {
2006-10-31 19:57:17 +00:00
$projectPath = $this -> getInput ( " What is the full path for this app including the app directory name? \n Example: " . ROOT . DS . " myapp " , null , ROOT . DS . 'myapp' );
2006-05-26 05:29:17 +00:00
if ( $projectPath == '' ) {
$this -> stdout ( 'The directory path you supplied was empty. Please try again.' );
}
}
}
while ( $this -> __checkPath ( $projectPath ) === true || $projectPath == '' ) {
2006-11-23 06:46:10 +00:00
$projectPath = $this -> getInput ( 'Directory ' . $projectPath . ' exists please choose another:' );
2006-05-26 05:29:17 +00:00
while ( $projectPath == '' ) {
$projectPath = $this -> getInput ( 'The directory path you supplied was empty. Please try again.' );
}
}
$parentPath = explode ( DS , $projectPath );
$count = count ( $parentPath );
$appName = $parentPath [ $count - 1 ];
2006-10-31 19:57:17 +00:00
if ( $appName == '' ) {
$appName = $parentPath [ $count - 2 ];
}
2006-05-26 05:29:17 +00:00
$this -> __buildDirLayout ( $projectPath , $appName );
exit ();
}
/**
2006-07-18 11:11:44 +00:00
* Returns true if given path is a directory .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ param string $projectPath
* @ return True if given path is a directory .
2006-05-26 05:29:17 +00:00
*/
function __checkPath ( $projectPath ) {
if ( is_dir ( $projectPath )) {
return true ;
} else {
return false ;
}
}
/**
2006-07-18 11:11:44 +00:00
* Looks for a skeleton template of a Cake application ,
* and if not found asks the user for a path . When there is a path
* this method will make a deep copy of the skeleton to the project directory .
* A default home page will be added , and the tmp file storage will be chmod ' ed to 0777.
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ param string $projectPath
* @ param string $appName
2006-05-26 05:29:17 +00:00
*/
function __buildDirLayout ( $projectPath , $appName ) {
$skel = '' ;
2006-10-09 20:27:28 +00:00
if ( $this -> __checkPath ( CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'scripts' . DS . 'templates' . DS . 'skel' ) === true ) {
$skel = CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'scripts' . DS . 'templates' . DS . 'skel' ;
2006-05-26 05:29:17 +00:00
} else {
while ( $skel == '' ) {
2006-10-09 20:27:28 +00:00
$skel = $this -> getInput ( " What is the full path for the cake install app directory? \n Example: " , null , ROOT . 'myapp' . DS );
2006-05-26 05:29:17 +00:00
if ( $skel == '' ) {
$this -> stdout ( 'The directory path you supplied was empty. Please try again.' );
} else {
while ( $this -> __checkPath ( $skel ) === false ) {
$skel = $this -> getInput ( 'Directory path does not exist please choose another:' );
}
}
}
}
$this -> stdout ( '' );
$this -> hr ();
$this -> stdout ( " Skel Directory: $skel " );
$this -> stdout ( " Will be copied to: " );
2006-09-15 20:19:03 +00:00
$this -> stdout ( " New App Directory: $projectPath " );
2006-05-26 05:29:17 +00:00
$this -> hr ();
$looksGood = $this -> getInput ( 'Look okay?' , array ( 'y' , 'n' , 'q' ), 'y' );
2006-10-31 19:57:17 +00:00
if ( low ( $looksGood ) == 'y' || low ( $looksGood ) == 'yes' ) {
2006-05-26 05:29:17 +00:00
$verboseOuptut = $this -> getInput ( 'Do you want verbose output?' , array ( 'y' , 'n' ), 'n' );
$verbose = false ;
2006-10-31 19:57:17 +00:00
if ( low ( $verboseOuptut ) == 'y' || low ( $verboseOuptut ) == 'yes' ) {
2006-05-26 05:29:17 +00:00
$verbose = true ;
}
2006-10-31 19:57:17 +00:00
$this -> __copydirr ( $skel , $projectPath , 0755 , $verbose );
2006-05-26 05:29:17 +00:00
$this -> hr ();
$this -> stdout ( 'Created: ' . $projectPath );
$this -> hr ();
$this -> stdout ( 'Creating welcome page' );
$this -> hr ();
$this -> __defaultHome ( $projectPath , $appName );
$this -> stdout ( 'Welcome page created' );
if ( chmodr ( $projectPath . DS . 'tmp' , 0777 ) === false ) {
$this -> stdout ( 'Could not set permissions on ' . $projectPath . DS . 'tmp' . DS . '*' );
$this -> stdout ( 'You must manually check that these directories can be wrote to by the server' );
}
return ;
2006-10-31 19:57:17 +00:00
} elseif ( low ( $looksGood ) == 'q' || low ( $looksGood ) == 'quit' ) {
2006-05-26 05:29:17 +00:00
$this -> stdout ( 'Bake Aborted.' );
} else {
2006-10-10 22:05:41 +00:00
$this -> project ();
2006-05-26 05:29:17 +00:00
}
}
/**
2006-07-18 11:11:44 +00:00
* Recursive directory copy .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ param string $fromDir
* @ param string $toDir
* @ param octal $chmod
* @ param boolean $verbose
* @ return Success .
2006-05-26 05:29:17 +00:00
*/
2006-10-31 19:57:17 +00:00
function __copydirr ( $fromDir , $toDir , $chmod = 0755 , $verbose = false ) {
2006-05-26 05:29:17 +00:00
$errors = array ();
$messages = array ();
if ( ! is_dir ( $toDir )) {
2006-06-14 18:02:37 +00:00
uses ( 'folder' );
$folder = new Folder ();
$folder -> mkdirr ( $toDir , 0755 );
2006-05-26 05:29:17 +00:00
}
if ( ! is_writable ( $toDir )) {
$errors [] = 'target ' . $toDir . ' is not writable' ;
}
if ( ! is_dir ( $fromDir )) {
$errors [] = 'source ' . $fromDir . ' is not a directory' ;
}
if ( ! empty ( $errors )) {
if ( $verbose ) {
foreach ( $errors as $err ) {
$this -> stdout ( 'Error: ' . $err );
}
}
return false ;
}
2006-06-19 15:00:17 +00:00
$exceptions = array ( '.' , '..' , '.svn' );
2006-05-26 05:29:17 +00:00
$handle = opendir ( $fromDir );
while ( false !== ( $item = readdir ( $handle ))) {
if ( ! in_array ( $item , $exceptions )) {
$from = str_replace ( '//' , '/' , $fromDir . '/' . $item );
$to = str_replace ( '//' , '/' , $toDir . '/' . $item );
if ( is_file ( $from )) {
if ( @ copy ( $from , $to )) {
chmod ( $to , $chmod );
touch ( $to , filemtime ( $from ));
$messages [] = 'File copied from ' . $from . ' to ' . $to ;
} else {
$errors [] = 'cannot copy file from ' . $from . ' to ' . $to ;
}
}
if ( is_dir ( $from )) {
if ( @ mkdir ( $to )) {
chmod ( $to , $chmod );
$messages [] = 'Directory created: ' . $to ;
} else {
$errors [] = 'cannot create directory ' . $to ;
}
2006-10-31 19:57:17 +00:00
$this -> __copydirr ( $from , $to , $chmod , $verbose );
2006-05-26 05:29:17 +00:00
}
}
}
closedir ( $handle );
if ( $verbose ) {
foreach ( $errors as $err ) {
$this -> stdout ( 'Error: ' . $err );
}
foreach ( $messages as $msg ) {
$this -> stdout ( $msg );
}
}
return true ;
}
2006-10-14 18:07:52 +00:00
function __addAdminRoute ( $name ){
$file = file_get_contents ( CONFIGS . 'core.php' );
if ( preg_match ( '%([/\\t\\x20]*define\\(\'CAKE_ADMIN\',[\\t\\x20\'a-z]*\\);)%' , $file , $match )) {
$result = str_replace ( $match [ 0 ], 'define(\'CAKE_ADMIN\', \'' . $name . '\');' , $file );
if ( file_put_contents ( CONFIGS . 'core.php' , $result )){
return true ;
} else {
return false ;
}
} else {
return false ;
2006-10-14 18:45:00 +00:00
}
2006-10-14 18:07:52 +00:00
}
2006-05-26 05:29:17 +00:00
/**
2006-07-18 11:11:44 +00:00
* Outputs an ASCII art banner to standard output .
2006-05-26 05:29:17 +00:00
*
*/
2006-06-14 18:02:37 +00:00
function welcome ()
{
2006-05-26 05:29:17 +00:00
$this -> stdout ( '' );
2006-06-14 18:02:37 +00:00
$this -> stdout ( ' ___ __ _ _ ___ __ _ _ __ __ __ _ _ ___ ' );
$this -> stdout ( '| |__| |_/ |__ |__] |__| |__] |__] |__| |_/ |__ ' );
$this -> stdout ( '|___ | | | \_ |___ | | | | |__] | | | \_ |___ ' );
2006-05-26 05:29:17 +00:00
$this -> hr ();
$this -> stdout ( '' );
}
/**
2006-07-18 11:11:44 +00:00
* Writes a file with a default home page to the project .
2006-05-26 05:29:17 +00:00
*
2006-07-18 11:11:44 +00:00
* @ param string $dir
* @ param string $app
2006-05-26 05:29:17 +00:00
*/
function __defaultHome ( $dir , $app ) {
$path = $dir . DS . 'views' . DS . 'pages' . DS ;
2006-11-28 09:34:25 +00:00
include ( CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'scripts' . DS . 'templates' . DS . 'views' . DS . 'home.ctp' );
$this -> __createFile ( $path . 'home.ctp' , $output );
2006-05-26 05:29:17 +00:00
}
2006-10-09 20:27:28 +00:00
/**
2006-10-10 18:24:25 +00:00
* creates the proper pluralize controller for the url
2006-10-09 20:27:28 +00:00
*
* @ param string $name
* @ return string $name
2006-10-14 18:07:52 +00:00
*/
2006-10-10 18:24:25 +00:00
function __controllerPath ( $name ) {
2006-10-09 20:27:28 +00:00
return low ( Inflector :: tableize ( $name ));
}
/**
* creates the proper pluralize controller class name .
*
* @ param string $name
* @ return string $name
*/
function __controllerName ( $name ) {
return Inflector :: pluralize ( Inflector :: camelize ( $name ));
}
/**
* creates the proper singular model name .
*
* @ param string $name
* @ return string $name
2006-10-14 18:07:52 +00:00
*/
2006-10-09 20:27:28 +00:00
function __modelName ( $name ) {
return Inflector :: camelize ( Inflector :: singularize ( $name ));
}
/**
* creates the proper singular model key for associations .
*
* @ param string $name
* @ return string $name
2006-10-14 18:07:52 +00:00
*/
2006-10-09 20:27:28 +00:00
function __modelKey ( $name ) {
return Inflector :: underscore ( Inflector :: singularize ( $name )) . '_id' ;
}
/**
* creates the proper model name from a foreign key .
*
* @ param string $key
* @ return string $name
2006-10-14 18:07:52 +00:00
*/
2006-10-09 20:27:28 +00:00
function __modelNameFromKey ( $key ) {
$name = str_replace ( '_id' , '' , $key );
return $this -> __modelName ( $name );
}
/**
* creates the singular name for use in views .
*
* @ param string $name
* @ return string $name
2006-10-14 18:07:52 +00:00
*/
2006-10-09 20:27:28 +00:00
function __singularName ( $name ) {
2006-10-31 19:57:17 +00:00
return Inflector :: variable ( Inflector :: singularize ( $name ));
2006-10-09 20:27:28 +00:00
}
/**
* creates the plural name for views .
*
* @ param string $name
* @ return string $name
*/
function __pluralName ( $name ) {
2006-10-31 19:57:17 +00:00
return Inflector :: variable ( Inflector :: pluralize ( $name ));
2006-10-09 20:27:28 +00:00
}
/**
* creates the singular human name used in views
*
* @ param string $name
* @ return string $name
*/
function __singularHumanName ( $name ) {
return Inflector :: humanize ( Inflector :: underscore ( Inflector :: singularize ( $name )));
}
/**
* creates the plural humna name used in views
*
* @ param string $name
* @ return string $name
*/
function __pluralHumanName ( $name ) {
return Inflector :: humanize ( Inflector :: underscore ( Inflector :: pluralize ( $name )));
}
2006-10-17 19:37:55 +00:00
/**
2006-10-31 19:57:17 +00:00
* outputs the a list of possible models or controllers from database
2006-10-17 19:37:55 +00:00
*
* @ param string $useDbConfig
* @ param string $type = Models or Controllers
* @ return output
*/
2006-10-31 19:57:17 +00:00
function __doList ( $useDbConfig = 'default' , $type = 'Models' ) {
2006-10-17 19:37:55 +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 ();
}
2006-10-18 00:16:09 +00:00
$this -> __tables = $tables ;
2006-10-31 19:57:17 +00:00
$this -> stdout ( 'Possible ' . $type . ' based on your current database:' );
2006-10-17 19:37:55 +00:00
$this -> __controllerNames = array ();
$this -> __modelNames = array ();
2006-10-18 00:16:09 +00:00
$count = count ( $tables );
for ( $i = 0 ; $i < $count ; $i ++ ) {
2006-10-31 19:57:17 +00:00
if ( low ( $type ) == 'controllers' ) {
2006-10-17 19:37:55 +00:00
$this -> __controllerNames [] = $this -> __controllerName ( $tables [ $i ]);
$this -> stdout ( $i + 1 . " . " . $this -> __controllerNames [ $i ]);
} else {
$this -> __modelNames [] = $this -> __modelName ( $tables [ $i ]);
$this -> stdout ( $i + 1 . " . " . $this -> __modelNames [ $i ]);
}
}
}
2006-10-14 18:07:52 +00:00
2006-05-09 02:15:47 +00:00
}
2006-11-03 22:47:43 +00:00
?>