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-04-24 16:38:16 +00:00
* Short description for file .
2006-01-13 23:53:23 +00:00
*
2006-04-24 16:38:16 +00:00
* Long description for file
2005-08-21 06:49:02 +00:00
*
* PHP versions 4 and 5
*
* CakePHP : Rapid Development Framework < http :// www . cakephp . org />
2006-04-24 16:38:16 +00:00
* Copyright ( c ) 2005 , Cake Software Foundation , Inc .
2005-12-23 21:57:26 +00:00
* 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-04-24 16:38:16 +00:00
* @ copyright Copyright ( c ) 2005 , Cake Software Foundation , Inc .
2005-12-23 21:57:26 +00:00
* @ link http :// www . cakefoundation . org / projects / info / cakephp CakePHP Project
2005-08-21 06:49:02 +00:00
* @ package cake
2006-04-24 16:38:16 +00:00
* @ subpackage cake . cake . scripts . bake
* @ since CakePHP v 0.10 . 0.1232
2005-08-21 06:49:02 +00:00
* @ version $Revision $
* @ modifiedby $LastChangedBy $
* @ lastmodified $Date $
* @ license http :// www . opensource . org / licenses / mit - license . php The MIT License
*/
2006-04-24 16:38:16 +00:00
ini_set ( 'display_errors' , '1' );
ini_set ( 'error_reporting' , '7' );
2005-05-22 23:24:09 +00:00
2006-04-24 21:22:56 +00:00
$app = 'app' ;
$core = null ;
$root = dirname ( dirname ( dirname ( __FILE__ )));
$here = $argv [ 0 ];
for ( $i = 1 ; $i < count ( $argv ); $i += 2 )
{
// Process command-line modifiers here
switch ( $argv [ $i ])
{
case '-app' :
$app = $argv [ $i + 1 ];
break ;
case '-core' :
$core = $argv [ $i + 1 ];
break ;
case '-root' :
$root = $argv [ $i + 1 ];
break ;
}
}
2005-05-22 23:24:09 +00:00
define ( 'DS' , DIRECTORY_SEPARATOR );
2006-04-24 21:22:56 +00:00
define ( 'ROOT' , $root . DS );
define ( 'APP_DIR' , $app );
define ( 'APP_PATH' , $app . DS );
2006-04-24 16:38:16 +00:00
define ( 'DEBUG' , 1 );
2006-04-24 21:22:56 +00:00
define ( 'CORE_PATH' , $core );
2006-04-27 10:04:08 +00:00
define ( 'CAKE_CORE_INCLUDE_PATH' , ROOT );
if ( function_exists ( 'ini_set' ))
{
ini_set ( 'include_path' , ini_get ( 'include_path' ) . PATH_SEPARATOR . CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . ROOT . DS . APP_DIR . DS );
}
2006-04-24 16:38:16 +00:00
require_once ( ROOT . 'cake' . DS . 'basics.php' );
require_once ( ROOT . 'cake' . DS . 'config' . DS . 'paths.php' );
require_once ( ROOT . 'cake' . DS . 'dispatcher.php' );
require_once ( CONFIGS . 'core.php' );
uses ( 'inflector' );
uses ( 'model' . DS . 'model' );
require_once ( ROOT . 'cake' . DS . 'app_model.php' );
require_once ( ROOT . 'cake' . DS . 'app_controller.php' );
uses ( 'neat_array' );
uses ( 'model' . DS . 'connection_manager' );
uses ( 'controller' . DS . 'controller' );
uses ( 'session' );
uses ( 'configure' );
uses ( 'security' );
uses ( DS . 'controller' . DS . 'scaffold' );
$pattyCake = new Bake ();
$pattyCake -> main ();
class Bake {
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
var $stdin ;
var $stdout ;
var $stderr ;
2006-04-27 10:04:08 +00:00
function __construct ()
{
2006-04-24 16:38:16 +00:00
$this -> stdin = fopen ( 'php://stdin' , 'r' );
$this -> stdout = fopen ( 'php://stdout' , 'w' );
$this -> stderr = fopen ( 'php://stderr' , 'w' );
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function Bake ()
{
return $this -> __construct ();
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function main ()
{
$this -> stdout ( '' );
$this -> stdout ( '____ ____ _ _ ____ ___ _ _ ___ ___ ____ _ _ ____ ' );
$this -> stdout ( '| |__| |_/ |___ |__] |__| |__] |__] |__| |_/ |___ ' );
$this -> stdout ( '|___ | | | \_ |___ | | | | |__] | | | \_ |___ ' );
$this -> hr ();
if ( ! file_exists ( CONFIGS . 'database.php' ))
{
$this -> stdout ( '' );
$this -> stdout ( '' );
$this -> stdout ( 'Your database configuration was not found. Take a moment to create one:' );
$this -> stdout ( '' );
$this -> stdout ( '' );
$this -> doDbConfig ();
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
require_once ( CONFIGS . 'database.php' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$this -> stdout ( '[M]odel' );
$this -> stdout ( '[C]ontroller' );
$this -> stdout ( '[V]iew' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$invalidSelection = true ;
2006-04-27 10:04:08 +00:00
while ( $invalidSelection )
2006-04-24 16:38:16 +00:00
{
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$classToBake = strtoupper ( $this -> getInput ( 'Please select a class to Bake:' , array ( 'M' , 'V' , 'C' )));
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +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-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
/*---- ----*/
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function doDbConfig ()
{
$this -> hr ();
$this -> stdout ( 'Database Configuration Bake:' );
$this -> hr ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$driver = 'mysql' ;
$connect = 'mysql_pconnect' ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$host = '' ;
2006-04-27 10:04:08 +00:00
while ( $host == '' )
2006-04-24 16:38:16 +00:00
{
$host = $this -> getInput ( 'What is the hostname for the database server?' , null , 'localhost' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( $host == '' )
{
$this -> stdout ( 'The host name you supplied was empty. Please supply a hostname.' );
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$login = '' ;
2006-04-27 10:04:08 +00:00
while ( $login == '' )
2006-04-24 16:38:16 +00:00
{
$login = $this -> getInput ( 'What is the database username?' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( $login == '' )
{
$this -> stdout ( 'The database username you supplied was empty. Please try again.' );
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$password = '' ;
2006-04-27 10:04:08 +00:00
while ( $password == '' )
2006-04-24 16:38:16 +00:00
{
$password = $this -> getInput ( 'What is the database password?' );
2006-04-27 10:04:08 +00:00
if ( $password == '' )
2006-04-24 16:38:16 +00:00
{
$this -> stdout ( 'The password you supplied was empty. Please try again.' );
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$database = '' ;
2006-04-27 10:04:08 +00:00
while ( $database == '' )
2006-04-24 16:38:16 +00:00
{
$database = $this -> getInput ( 'What is the name of the database you will be using?' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( $database == '' )
{
$this -> stdout ( 'The database name you supplied was empty. Please try again.' );
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$this -> stdout ( '' );
$this -> hr ();
$this -> stdout ( 'The following database configuration will be created:' );
$this -> hr ();
$this -> stdout ( " Host: $host " );
$this -> stdout ( " User: $login " );
$this -> stdout ( " Pass: " . str_repeat ( '*' , strlen ( $password )));
2006-04-27 10:04:08 +00:00
$this -> stdout ( " Database: $database " );
2006-04-24 16:38:16 +00:00
$this -> hr ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$looksGood = $this -> getInput ( 'Look okay?' , array ( 'y' , 'n' ), 'y' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( strtolower ( $looksGood ) == 'y' || strtolower ( $looksGood ) == 'yes' )
{
$this -> bakeDbConfig ( $host , $login , $password , $database );
}
else
{
$this -> stdout ( 'Bake Aborted.' );
}
}
function doModel ()
2006-04-27 10:04:08 +00:00
{
2006-04-24 16:38:16 +00:00
$this -> hr ();
$this -> stdout ( 'Model Bake:' );
$this -> hr ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$dbConnection = 'default' ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
/* $usingDefault = $this -> getInput ( 'Will your model be using a database connection setting other than the default?' );
if ( strtolower ( $usingDefault ) == 'y' || strtolower ( $usingDefault ) == 'yes' )
{
$dbConnection = $this -> getInput ( 'Please provide the name of the connection you wish to use.' );
} */
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$modelName = '' ;
2006-04-27 10:04:08 +00:00
$db =& ConnectionManager :: getDataSource ( $dbConnection );
2006-04-24 16:38:16 +00:00
$tables = $db -> listSources ();
$inflect = new Inflector ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$this -> stdout ( 'Possible models based on your current database:' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
for ( $i = 0 ; $i < count ( $tables ); $i ++ )
{
$this -> stdout ( $i + 1 . " . " . $inflect -> camelize ( $inflect -> singularize ( $tables [ $i ])));
}
2006-04-27 10:04:08 +00:00
while ( $modelName == '' )
2006-04-24 16:38:16 +00:00
{
$modelName = $this -> getInput ( 'Enter a number from the list above, or type in the name of another model.' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( $modelName == '' || intval ( $modelName ) > $i )
{
$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. " );
$modelName = '' ;
}
}
2006-04-27 10:04:08 +00:00
if ( intval ( $modelName ) > 0 && intval ( $modelName ) <= $i )
2006-04-24 16:38:16 +00:00
{
$modelClassName = $inflect -> camelize ( $inflect -> singularize ( $tables [ $modelName - 1 ]));
$modelTableName = $tables [ intval ( $modelName ) - 1 ];
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
$modelClassName = $inflect -> camelize ( $modelName );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$this -> stdout ( " \n Given your model named ' $modelClassName ', Cake would expect a database table named ' " . $inflect -> pluralize ( $modelName ) . " '. " );
$tableIsGood = $this -> getInput ( 'Is this correct?' , array ( 'y' , 'n' ), 'y' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( strtolower ( $tableIsGood ) == 'n' || strtolower ( $tableIsGood ) == 'no' )
{
$modelTableName = $this -> getInput ( 'What is the name of the table (enter "null" to use NO table)?' );
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$wannaDoValidation = $this -> getInput ( 'Would you like to supply validation criteria for the fields in your model?' , array ( 'y' , 'n' ), 'y' );
$validate = array ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$tempModel = new Model ( false , $modelTableName );
$modelFields = $db -> describe ( $tempModel );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( array_search ( $modelTableName , $tables ) !== false && ( strtolower ( $wannaDoValidation ) == 'y' || strtolower ( $wannaDoValidation ) == 'yes' ))
2006-04-27 10:04:08 +00:00
{
2006-04-24 16:38:16 +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 " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( $field [ 'name' ] == 'id' || $field [ 'name' ] == 'created' || $field [ 'name' ] == 'modified' )
{
$validation = $this -> getInput ( $prompt , null , '5' );
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
$validation = $this -> getInput ( $prompt , null , '1' );
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
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 ;
break ;
}
2006-04-27 10:04:08 +00:00
}
2006-04-24 16:38:16 +00:00
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$modelTableName == null ? $modelTableName = $inflect -> pluralize ( $modelName ) : $modelTableName = $modelTableName ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$wannaDoAssoc = $this -> getInput ( 'Would you like define model associations (hasMany, hasOne, belongsTo, etc.)?' , array ( 'y' , 'n' ), 'y' );
if (( strtolower ( $wannaDoAssoc ) == 'y' || strtolower ( $wannaDoAssoc ) == 'yes' ))
{
$this -> stdout ( 'One moment while I try to detect any associations...' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
//Look for belongsTo
foreach ( $modelFields as $field )
2006-04-27 10:04:08 +00:00
{
2006-04-24 16:38:16 +00:00
$offset = strpos ( $field [ 'name' ], '_id' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( $offset !== false )
{
2006-04-27 10:04:08 +00:00
$belongsToClasses [] = $inflect -> camelize ( substr ( $field [ 'name' ], 0 , $offset ));
2006-04-24 16:38:16 +00:00
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
//Look for hasOne and hasMany and hasAndBelongsToMany
foreach ( $tables as $table )
{
$tempModelOthers = new Model ( false , $table );
$modelFieldsTemp = $db -> describe ( $tempModelOthers );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
foreach ( $modelFieldsTemp as $field )
2006-04-27 10:04:08 +00:00
{
2006-04-24 16:38:16 +00:00
if ( $field [ 'name' ] == $inflect -> singularize ( $modelTableName ) . '_id' )
{
2006-04-27 10:04:08 +00:00
$hasOneClasses [] = $inflect -> camelize ( $inflect -> singularize ( $table ));
2006-04-24 16:38:16 +00:00
$hasManyClasses [] = $inflect -> camelize ( $inflect -> singularize ( $table ));
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$offset = strpos ( $table , $modelTableName . '_' );
if ( $offset !== false )
{
$offset = strlen ( $modelTableName . '_' );
$hasAndBelongsToManyClasses [] = $inflect -> camelize ( $inflect -> singularize ( substr ( $table , $offset )));
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$offset = strpos ( $table , '_' . $modelTableName );
if ( $offset !== false )
{
$hasAndBelongsToManyClasses [] = $inflect -> camelize ( $inflect -> singularize ( substr ( $table , 0 , $offset )));
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$this -> stdout ( 'Done.' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$this -> hr ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
//if none found...
if ( count ( $hasOneClasses ) < 1 && count ( $hasManyClasses ) < 1 && count ( $hasAndBelongsToManyClasses ) < 1 && count ( $belongsToClasses ))
{
$this -> stdout ( 'None found.' );
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
2006-04-27 10:04:08 +00:00
$this -> stdout ( 'Please confirm the following associations:' );
2006-04-24 16:38:16 +00:00
$this -> hr ();
if ( count ( $belongsToClasses ))
{
for ( $i = 0 ; $i < count ( $belongsToClasses ); $i ++ )
{
$response = $this -> getInput ( " $modelClassName belongsTo { $belongsToClasses [ $i ] } ? " , array ( 'y' , 'n' ), 'y' );
if ( $response == 'y' )
{
$modelAssociations [ 'belongsTo' ][] = $belongsToClasses [ $i ];
}
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( count ( $hasOneClasses ))
{
for ( $i = 0 ; $i < count ( $hasOneClasses ); $i ++ )
{
$response = $this -> getInput ( " $modelClassName hasOne { $hasOneClasses [ $i ] } ? " , array ( 'y' , 'n' ), 'y' );
if ( $response == 'y' )
{
$modelAssociations [ 'hasOne' ][] = $hasOneClasses [ $i ];
}
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( count ( $hasManyClasses ))
{
for ( $i = 0 ; $i < count ( $hasManyClasses ); $i ++ )
{
$response = $this -> getInput ( " $modelClassName hasMany { $hasManyClasses [ $i ] } ? " , array ( 'y' , 'n' ), 'y' );
if ( $response == 'y' )
{
$modelAssociations [ 'hasMany' ][] = $hasManyClasses [ $i ];
}
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( count ( $hasAndBelongsToManyClasses ))
{
for ( $i = 0 ; $i < count ( $hasAndBelongsToManyClasses ); $i ++ )
{
$response = $this -> getInput ( " $modelClassName hasAndBelongsToMany { $hasAndBelongsToManyClasses [ $i ] } ? " , array ( 'y' , 'n' ), 'y' );
if ( $response == 'y' )
{
$modelAssociations [ 'hasAndBelongsToMany' ][] = $hasAndBelongsToManyClasses [ $i ];
}
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$wannaDoMoreAssoc = $this -> getInput ( 'Would you like to define some additional model associations?' , array ( 'y' , 'n' ), 'y' );
while (( strtolower ( $wannaDoMoreAssoc ) == 'y' || strtolower ( $wannaDoMoreAssoc ) == 'yes' ))
{
$assocs = array ( 1 => 'belongsTo' , 2 => 'hasOne' , 3 => 'hasMany' , 4 => 'hasAndBelongsToMany' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$bad = true ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
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 ));
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( intval ( $assocType ) < 1 || intval ( $assocType ) > 4 )
{
$this -> stdout ( 'The selection you entered was invalid. Please enter a number between 1 and 4.' );
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
$bad = false ;
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$assocClassName = $this -> getInput ( 'Classname of associated Model?' );
2006-04-27 10:04:08 +00:00
$modelAssociations [ $assocs [ $assocType ]][] = $assocClassName ;
2006-04-24 16:38:16 +00:00
$this -> stdout ( " Association ' $modelClassName { $assocs [ $assocType ] } $assocClassName ' defined. " );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$wannaDoMoreAssoc = $this -> getInput ( 'Define another association?' , array ( 'y' , 'n' ), 'y' );
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$this -> stdout ( '' );
$this -> hr ();
$this -> stdout ( 'The following model will be created:' );
$this -> hr ();
$this -> stdout ( " Model Name: $modelClassName " );
$this -> stdout ( " DB Connection: " . ( $usingDefault ? 'default' : $dbConnection ));
$this -> stdout ( " Model Table: " . $modelTableName );
2006-04-27 10:04:08 +00:00
$this -> stdout ( " Validation: " . print_r ( $validate , true ));
2006-04-24 16:38:16 +00:00
if ( count ( $belongsToClasses ) || count ( $hasOneClasses ) || count ( $hasManyClasses ) || count ( $hasAndBelongsToManyClasses ))
{
2006-04-27 10:04:08 +00:00
$this -> stdout ( " Associations: " );
2006-04-24 16:38:16 +00:00
if ( count ( $modelAssociations [ 'belongsTo' ]))
{
for ( $i = 0 ; $i < count ( $modelAssociations [ 'belongsTo' ]); $i ++ )
{
$this -> stdout ( " $modelClassName belongsTo { $modelAssociations [ 'belongsTo' ][ $i ] } " );
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( count ( $modelAssociations [ 'hasOne' ]))
{
for ( $i = 0 ; $i < count ( $modelAssociations [ 'hasOne' ]); $i ++ )
{
$this -> stdout ( " $modelClassName hasOne { $modelAssociations [ 'hasOne' ][ $i ] } " );
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( count ( $modelAssociations [ 'hasMany' ]))
{
for ( $i = 0 ; $i < count ( $modelAssociations [ 'hasMany' ]); $i ++ )
{
$this -> stdout ( " $modelClassName hasMany { $modelAssociations [ 'hasMany' ][ $i ] } " );
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( count ( $modelAssociations [ 'hasAndBelongsToMany' ]))
{
for ( $i = 0 ; $i < count ( $modelAssociations [ 'hasAndBelongsToMany' ]); $i ++ )
{
$this -> stdout ( " $modelClassName hasAndBelongsToMany { $modelAssociations [ 'hasAndBelongsToMany' ][ $i ] } " );
}
2006-04-27 10:04:08 +00:00
}
2006-04-24 16:38:16 +00:00
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$this -> hr ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$looksGood = $this -> getInput ( 'Look okay?' , array ( 'y' , 'n' ), 'y' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( strtolower ( $looksGood ) == 'y' || strtolower ( $looksGood ) == 'yes' )
{
if ( $inflect -> camelize ( $inflect -> singularize ( $modelTableName )) == $modelClassName )
{
2006-04-27 10:04:08 +00:00
// set it to null...
// putting $useTable in the model
2006-04-24 16:38:16 +00:00
// is unnecessary.
$modelTableName = null ;
}
$this -> bakeModel ( $modelClassName , $dbConnection , $modelTableName , $validate , $modelAssociations );
2006-04-24 21:22:56 +00:00
if ( $this -> doUnitTest ())
{
$this -> bakeUnitTest ( 'model' , $modelClassName );
}
2006-04-24 16:38:16 +00:00
}
else
{
$this -> stdout ( 'Bake Aborted.' );
2006-04-27 10:04:08 +00:00
}
2006-04-24 16:38:16 +00:00
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function doView ()
{
$this -> hr ();
$this -> stdout ( 'View Bake:' );
$this -> hr ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$controllerName = '' ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$inflect = new Inflector ();
2006-04-27 10:04:08 +00:00
while ( $controllerName == '' )
2006-04-24 16:38:16 +00:00
{
$controllerName = $this -> getInput ( 'Controller Name? (plural)' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( $controllerName == '' )
{
$this -> stdout ( 'The controller name you supplied was empty. Please try again.' );
}
}
2006-04-27 10:04:08 +00:00
2006-05-07 03:35:58 +00:00
$controllerName = $inflect -> underscore ( $controllerName );
2006-04-24 16:38:16 +00:00
$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' );
$uses = array ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( strtolower ( $wannaDoScaffold ) == 'y' || strtolower ( $wannaDoScaffold ) == 'yes' )
{
$file = CONTROLLERS . $controllerName . '_controller.php' ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( ! file_exists ( $file ))
{
$this -> stdout ( '' );
$this -> stdout ( " The file ' $file ' could not be found. \n In order to scaffold, you'll need to first create the controller. " );
$this -> stdout ( '' );
die ();
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
require_once ( CONTROLLERS . $controllerName . '_controller.php' );
$controller = $inflect -> camelize ( $controllerName . '_controller' );
$temp = new $controller ();
if ( ! in_array ( 'Form' , $temp -> helpers ))
{
$temp -> helpers [] = 'Form' ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
loadModels ();
$temp -> constructClasses ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$fieldNames = $temp -> generateFieldNames ( null , false );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
uses ( 'view' . DS . 'helpers' . DS . 'html' );
uses ( 'view' . DS . 'helpers' . DS . 'form' );
$this -> Html = new HtmlHelper ();
$this -> Html -> tags = $this -> Html -> loadConfig ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( ! empty ( $temp -> { $temp -> modelClass } -> alias ))
{
foreach ( $temp -> { $temp -> modelClass } -> alias as $key => $value )
{
$alias [] = $key ;
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
//-------------------------[INDEX]-------------------------//
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$indexView .= " <h1>List " . $inflect -> pluralize ( $temp -> modelKey ) . " </h1> \n \n " ;
$indexView .= " <table> \n " ;
$indexView .= " <tr> \n " ;
foreach ( $fieldNames as $fieldName )
{
$indexView .= " \t <th> " . $fieldName [ 'prompt' ] . " </th> \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$indexView .= " \t <th>Actions</th> \n " ;
$indexView .= " </tr> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 21:22:56 +00:00
$indexView .= " <?php foreach ( \$ data as \$ row): ?> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$indexView .= " <tr> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$count = 0 ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
foreach ( $fieldNames as $field => $value )
{
if ( isset ( $value [ 'foreignKey' ]))
2006-04-27 10:04:08 +00:00
{
2006-04-24 16:38:16 +00:00
$otherModelKey = Inflector :: underscore ( $value [ 'modelKey' ]);
$otherControllerName = $value [ 'controller' ];
$otherModelObject =& ClassRegistry :: getObject ( $otherModelKey );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( is_object ( $otherModelObject ))
{
$indexView .= " \t <td><?php echo \$ row[' " . $alias [ $count ] . " '][' " . $otherModelObject -> getDisplayField () . " '] ?></td> \n " ;
}
else
{
$indexView .= " \t <td><?php echo \$ row[' " . $alias [ $count ] . " '][' " . $field . " '] ?></td> \n " ;
}
$count ++ ;
}
else
{
$indexView .= " \t <td><?php echo \$ row[' { $temp -> modelClass } '][' { $field } '] ?></td> \n " ;
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$id = $temp -> { $temp -> modelClass } -> primaryKey ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$indexView .= " \t <td> \n " ;
$indexView .= " \t \t <?php echo \$ html->link('View','/ $temp->viewPath /view/' . \$ row[' { $temp -> modelClass } '][' $id '])?> \n " ;
$indexView .= " \t \t <?php echo \$ html->link('Edit','/ $temp->viewPath /edit/' . \$ row[' { $temp -> modelClass } '][' $id '])?> \n " ;
$indexView .= " \t \t <?php echo \$ html->link('Delete','/ $temp->viewPath /delete/' . \$ row[' { $temp -> modelClass } '][' $id '])?> \n " ;
$indexView .= " \t </td> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$indexView .= " </tr> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$indexView .= " <?php endforeach?> \n " ;
$indexView .= " </table> \n \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$indexView .= " <ul> \n " ;
$indexView .= " \t <li><?php echo \$ html->link('New $temp->modelClass ', '/ $temp->viewPath /add'); ?></li> \n " ;
$indexView .= " </ul> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
//-------------------------[VIEW]-------------------------//
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$modelName = $temp -> modelClass ;
$modelKey = Inflector :: underscore ( $modelName );
$objModel =& ClassRegistry :: getObject ( $modelKey );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$viewView .= " <h1>View " . $inflect -> pluralize ( $temp -> modelKey ) . " </h1> \n \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$viewView .= " <table> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$count = 0 ;
foreach ( $fieldNames as $field => $value )
{
$viewView .= " <tr> \n " ;
$viewView .= " \t <td><?php echo ' { $value [ 'prompt' ] } ' ?></td> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( isset ( $value [ 'foreignKey' ]))
{
$otherModelObject =& ClassRegistry :: getObject ( Inflector :: underscore ( $objModel -> tableToModel [ $value [ 'table' ]]));
$displayField = $otherModelObject -> getDisplayField ();
$viewView .= " \t <td><?php echo \$ html->link( \$ data[' { $alias [ $count ] } '][' { $displayField } '], '/ " . $inflect -> underscore ( $value [ 'controller' ]) . " /view/' . \$ data[' { $objModel -> tableToModel [ $objModel -> table ] } '][' { $field } '])?></td> \n " ;
$count ++ ;
}
else
{
$viewView .= " \t <td><?php echo \$ data[' { $objModel -> tableToModel [ $objModel -> table ] } '][' { $field } ']?></td> \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$viewView .= " </tr> \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$viewView .= " </table> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$viewView .= " <ul> \n " ;
$viewView .= " \t <li><?php echo \$ html->link('Edit " . $inflect -> humanize ( $objModel -> name ) . " ', '/ { $temp -> viewPath } /edit/' . \$ data[' { $objModel -> tableToModel [ $objModel -> table ] } '][' $id ']) ?> </li> \n " ;
$viewView .= " \t <li><?php echo \$ html->link('Delete " . $inflect -> humanize ( $objModel -> name ) . " ', '/ { $temp -> viewPath } /delete/' . \$ data[' { $objModel -> tableToModel [ $objModel -> table ] } '][' $id ']) ?> </li> \n " ;
$viewView .= " \t <li><?php echo \$ html->link('List " . $inflect -> humanize ( $objModel -> name ) . " ', '/ { $temp -> viewPath } /index') ?> </li> \n " ;
$viewView .= " \t <li><?php echo \$ html->link('New " . $inflect -> humanize ( $objModel -> name ) . " ', '/ { $temp -> viewPath } /add') ?> </li> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
foreach ( $fieldNames as $field => $value )
{
if ( isset ( $value [ 'foreignKey' ] ) )
{
$viewView .= " \t <li> <?php echo \$ html->link( 'List " . $inflect -> humanize ( $value [ 'controller' ]) . " ', '/ " . $inflect -> underscore ( $value [ 'controller' ]) . " /index/')?> </li> \n " ;
}
}
$viewView .= " </ul> \n \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
foreach ( $objModel -> hasOne as $association => $relation )
{
$model = $relation [ 'className' ];
$otherModelName = $objModel -> tableToModel [ $objModel -> { $model } -> table ];
$controller = $inflect -> pluralize ( $model );
$new = true ;
$viewView .= " <h2>Related " . $inflect -> humanize ( $association ) . " </h2> \n " ;
2006-04-27 10:04:08 +00:00
$viewView .= " <dl> \n " ;
$viewView .= " <?php if(isset( \$ data[' { $association } ']) && is_array( \$ data[' { $association } '])): ?> \n " ;
$viewView .= " \t <?php foreach( \$ data[' { $association } '] as \$ field => \$ value): ?> \n " ;
2006-04-24 16:38:16 +00:00
$viewView .= " \t \t <dt><?php echo \$ field ?></dt> \n " ;
$viewView .= " \t \t <dd><?php echo \$ value ?></dd> \n " ;
2006-04-27 10:04:08 +00:00
$viewView .= " \t <?php endforeach; ?> \n " ;
2006-04-24 16:38:16 +00:00
$viewView .= " \t <ul><li><?php echo \$ html->link('New " . $inflect -> humanize ( $association ) . " ', '/ " . $inflect -> underscore ( $controller ) . " /add/' . \$ data[' { $association } '][' " . $objModel -> { $model } -> primaryKey . " '])?> </li></ul> \n " ;
2006-04-24 21:22:56 +00:00
$viewView .= " <?php endif ?> \n " ;
2006-04-24 16:38:16 +00:00
$viewView .= " </dl> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$relations = array_merge ( $objModel -> hasMany , $objModel -> hasAndBelongsToMany );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
foreach ( $relations as $association => $relation )
{
$model = $relation [ 'className' ];
$count = 0 ;
$otherModelName = $inflect -> singularize ( $model );
$controller = $inflect -> pluralize ( $model );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$viewView .= " \n <h2>Related " . $inflect -> humanize ( $inflect -> pluralize ( $association )) . " </h2> \n " ;
$viewView .= " <?php if(isset( \$ data[' { $association } ']) && is_array( \$ data[' { $association } '])):?> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$viewView .= " <table> \n " ;
$viewView .= " <tr> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$viewView .= " <?php foreach( \$ data[' { $association } '][0] as \$ column => \$ value): ?> \n " ;
$viewView .= " <th><?php echo \$ column?></th> \n " ;
2006-04-24 21:22:56 +00:00
$viewView .= " <?php endforeach; ?> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$viewView .= " <th>Actions</th> \n " ;
$viewView .= " </tr> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$viewView .= " <?php foreach( \$ data[' { $association } '] as \$ row):?> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$viewView .= " <tr> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$viewView .= " \t <?php foreach( \$ row as \$ column => \$ value):?> \n " ;
$viewView .= " \t \t <td><?php echo \$ value?></td> \n " ;
2006-04-24 21:22:56 +00:00
$viewView .= " \t <?php endforeach;?> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 21:22:56 +00:00
$viewView .= " <?php if (isset( \$ this->controller-> { $modelName } -> { $association } )):?> \n " ;
2006-04-24 16:38:16 +00:00
$viewView .= " <td> \n " ;
$viewView .= " \t <?php echo \$ html->link('View', '/ " . $inflect -> underscore ( $controller ) . " /view/' . \$ row[ \$ this->controller-> { $modelName } -> { $association } ->primaryKey])?> \n " ;
$viewView .= " \t <?php echo \$ html->link('Edit', '/ " . $inflect -> underscore ( $controller ) . " /edit/' . \$ row[ \$ this->controller-> { $modelName } -> { $association } ->primaryKey])?> \n " ;
$viewView .= " \t <?php echo \$ html->link('Delete', '/ " . $inflect -> underscore ( $controller ) . " /delete/' . \$ row[ \$ this->controller-> { $modelName } -> { $association } ->primaryKey])?> \n " ;
$viewView .= " </td> \n " ;
2006-04-24 21:22:56 +00:00
$viewView .= " <?php else: ?> \n " ;
2006-04-24 16:38:16 +00:00
$viewView .= " <td> \n " ;
$viewView .= " \t <?php echo \$ html->link('View', '/ " . $inflect -> underscore ( $controller ) . " /view/' . \$ row[ \$ this->controller-> { $modelName } ->primaryKey])?> \n " ;
$viewView .= " \t <?php echo \$ html->link('Edit', '/ " . $inflect -> underscore ( $controller ) . " /edit/' . \$ row[ \$ this->controller-> { $modelName } ->primaryKey])?> \n " ;
$viewView .= " \t <?php echo \$ html->link('Delete', '/ " . $inflect -> underscore ( $controller ) . " /delete/' . \$ row[ \$ this->controller-> { $modelName } ->primaryKey])?> \n " ;
$viewView .= " </td> \n " ;
2006-04-24 21:22:56 +00:00
$viewView .= " <?php endif ?> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$viewView .= " </tr> \n " ;
2006-04-24 21:22:56 +00:00
$viewView .= " <?php endforeach; ?> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$viewView .= " </table> \n " ;
2006-04-24 21:22:56 +00:00
$viewView .= " <?php endif ?> \n \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$viewView .= " <ul> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$viewView .= " <li><?php echo \$ html->link('New " . $inflect -> humanize ( $association ) . " ', '/ " . $inflect -> underscore ( $controller ) . " /add/')?></li> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$viewView .= " </ul> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
//-------------------------[ADD]-------------------------//
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$addView .= " <h1>New " . $temp -> modelKey . " </h1> \n " ;
$addView .= " <?php \$ data = null;?> " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$fields .= $this -> Html -> formTag ( '/' . $temp -> viewPath . '/add' ) . " \n " ;
$fields .= $this -> generateFields ( $temp -> generateFieldNames ( null , true ));
$fields .= $this -> generateSubmitDiv ( 'Add' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$addView .= $fields ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$addView .= " </form> \n " ;
$addView .= " <ul> \n " ;
$addView .= " <li><?php echo \$ html->link('List " . $temp -> viewPath . " ', '/ { $temp -> viewPath } /index')?></li> \n " ;
$addView .= " </ul> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
//-------------------------[EDIT]-------------------------//
$editView .= " <h1>Edit " . $temp -> modelKey . " </h1> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$editView .= " <form action= \" / { $temp -> viewPath } /edit/<?php echo \$ data[' { $objModel -> tableToModel [ $objModel -> table ] } '][' $id '] ?> \" method= \" post \" > \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$fields = $this -> generateFields ( $temp -> generateFieldNames ( null , true ));
2006-04-27 10:04:08 +00:00
$fields .= " <?php echo \$ html->hidden(' { $objModel -> name } / { $id } ', array('value' => \$ data[' { $objModel -> tableToModel [ $objModel -> table ] } '][' $id ']))?> " ;
2006-04-24 16:38:16 +00:00
$fields .= $this -> generateSubmitDiv ( 'Save' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$editView .= $fields ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$editView .= " </form> \n " ;
$editView .= " <ul> \n " ;
$editView .= " \t <li><?php echo \$ html->link('List " . $temp -> viewPath . " ', '/ { $temp -> viewPath } /index')?></li> \n " ;
$editView .= " </ul> \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
//------------------------------------------------------------------------------------//
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( ! file_exists ( VIEWS . strtolower ( $controllerName )))
{
mkdir ( VIEWS . strtolower ( $controllerName ));
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$filename = VIEWS . strtolower ( $controllerName ) . DS . 'index.thtml' ;
$this -> createFile ( $filename , $indexView );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$filename = VIEWS . strtolower ( $controllerName ) . DS . 'view.thtml' ;
$this -> createFile ( $filename , $viewView );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$filename = VIEWS . strtolower ( $controllerName ) . DS . 'add.thtml' ;
$this -> createFile ( $filename , $addView );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$filename = VIEWS . strtolower ( $controllerName ) . DS . 'edit.thtml' ;
$this -> createFile ( $filename , $editView );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$this -> hr ();
$this -> stdout ( '' );
$this -> stdout ( 'Note:' . " \n " );
$this -> stdout ( " \t - If you're using a non-domain install, change URL paths \n \t from /controller/action to /cake_install/controller/action \n " );
$this -> hr ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$this -> stdout ( '' );
$this -> stdout ( 'View Scaffolding Complete.' . " \n " );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
}
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
$actionName = '' ;
2006-04-27 10:04:08 +00:00
while ( $actionName == '' )
2006-04-24 16:38:16 +00:00
{
$actionName = $this -> getInput ( 'Action Name? (use camelCased function name)' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( $actionName == '' )
{
$this -> stdout ( 'The action name you supplied was empty. Please try again.' );
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$this -> stdout ( '' );
$this -> hr ();
$this -> stdout ( 'The following view will be created:' );
$this -> hr ();
$this -> stdout ( " Controller Name: $controllerName " );
$this -> stdout ( " Action Name: $actionName " );
$this -> stdout ( " Path: app/views/ " . strtolower ( $controllerName ) . DS . $inflect -> underscore ( $actionName ) . '.thtml' );
$this -> hr ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$looksGood = $this -> getInput ( 'Look okay?' , array ( 'y' , 'n' ), 'y' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( strtolower ( $looksGood ) == 'y' || strtolower ( $looksGood ) == 'yes' )
{
$this -> bakeView ( $controllerName , $actionName );
}
else
{
$this -> stdout ( 'Bake Aborted.' );
2006-04-27 10:04:08 +00:00
}
}
2006-04-24 16:38:16 +00:00
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function doController ()
{
$this -> hr ();
$this -> stdout ( 'Controller Bake:' );
$this -> hr ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$controllerName = '' ;
2006-04-27 10:04:08 +00:00
while ( $controllerName == '' )
2006-04-24 16:38:16 +00:00
{
$controllerName = $this -> getInput ( 'Controller name? Remember that Cake controller names are plural.' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( $controllerName == '' )
{
$this -> stdout ( 'The controller name you supplied was empty. Please try again.' );
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$inflect = new Inflector ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$controllerClassName = $inflect -> camelize ( $controllerName );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$wannaDoUses = $this -> getInput ( " Would you like this controller to use other models besides ' " . $inflect -> singularize ( $controllerClassName ) . " '? " , array ( 'y' , 'n' ), 'n' );
$uses = array ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( strtolower ( $wannaDoUses ) == 'y' || strtolower ( $wannaDoUses ) == 'yes' )
{
$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-04-27 10:04:08 +00:00
}
2006-04-24 16:38:16 +00:00
$wannaDoHelpers = $this -> getInput ( " Would you like this controller to use other helpers besides HtmlHelper? " , array ( 'y' , 'n' ), 'n' );
$helpers = array ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( strtolower ( $wannaDoHelpers ) == 'y' || strtolower ( $wannaDoHelpers ) == 'yes' )
{
$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 );
2006-04-27 10:04:08 +00:00
}
2006-04-24 16:38:16 +00:00
$wannaDoComponents = $this -> getInput ( " Would you like this controller to use any components? " , array ( 'y' , 'n' ), 'n' );
$components = array ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( strtolower ( $wannaDoComponents ) == 'y' || strtolower ( $wannaDoComponents ) == 'yes' )
{
$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-04-27 10:04:08 +00:00
$wannaDoScaffolding = $this -> getInput ( " Would to include some basic class methods (index(), add(), view(), edit())? " , array ( 'y' , 'n' ), 'n' );
2006-04-24 16:38:16 +00:00
if ( strtolower ( $wannaDoScaffolding ) == 'y' || strtolower ( $wannaDoScaffolding ) == 'yes' )
{
$controllerModel = $inflect -> singularize ( $controllerClassName );
2006-05-07 03:35:58 +00:00
loadModels ();
$tempModel = new $controllerModel ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$actions .= " \n " ;
2006-04-27 10:04:08 +00:00
$actions .= " \t function index() \n " ;
$actions .= " \t { \n " ;
2006-05-07 03:35:58 +00:00
$actions .= " \t \t \$ this-> { $controllerModel } ->recursive = 0; \n " ;
2006-04-27 10:04:08 +00:00
$actions .= " \t \t \$ this->set('data', \$ this-> { $controllerModel } ->findAll()); \n " ;
2006-04-24 16:38:16 +00:00
$actions .= " \t } \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$actions .= " \n " ;
2006-04-27 10:04:08 +00:00
$actions .= " \t function add() \n " ;
$actions .= " \t { \n " ;
2006-05-07 03:35:58 +00:00
$actions .= " \t \t if(empty( \$ this->data)) \n " ;
2006-04-24 16:38:16 +00:00
$actions .= " \t \t { \n " ;
$actions .= " \t \t \t \$ this->render(); \n " ;
$actions .= " \t \t } \n " ;
$actions .= " \t \t else \n " ;
$actions .= " \t \t { \n " ;
2006-05-07 03:35:58 +00:00
$actions .= " \t \t \t if( \$ this-> { $controllerModel } ->save( \$ this->data)) \n " ;
2006-04-24 16:38:16 +00:00
$actions .= " \t \t \t { \n " ;
$actions .= " \t \t \t \t \$ this->flash(' { $controllerModel } saved.', '/ { $controllerName } /index'); \n " ;
$actions .= " \t \t \t } \n " ;
$actions .= " \t \t \t else \n " ;
$actions .= " \t \t \t { \n " ;
$actions .= " \t \t \t \t \$ this->render(); \n " ;
$actions .= " \t \t \t } \n " ;
$actions .= " \t \t } \n " ;
$actions .= " \t } \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$actions .= " \n " ;
2006-04-27 10:04:08 +00:00
$actions .= " \t function edit( \$ id) \n " ;
$actions .= " \t { \n " ;
2006-05-07 03:35:58 +00:00
$actions .= " \t \t if(empty( \$ this->data)) \n " ;
2006-04-24 16:38:16 +00:00
$actions .= " \t \t { \n " ;
2006-05-07 03:35:58 +00:00
$actions .= " \t \t \t \$ this->set('data', \$ this-> { $controllerModel } ->find(' { $controllerModel } . { $tempModel -> primaryKey } = ' . \$ id)); \n " ;
2006-04-24 16:38:16 +00:00
$actions .= " \t \t } \n " ;
$actions .= " \t \t else \n " ;
$actions .= " \t \t { \n " ;
2006-05-07 03:35:58 +00:00
$actions .= " \t \t \t if( \$ this-> { $controllerModel } ->save( \$ this->data)) \n " ;
2006-04-24 16:38:16 +00:00
$actions .= " \t \t \t { \n " ;
$actions .= " \t \t \t \t \$ this->flash(' { $controllerModel } saved.', '/ { $controllerName } /index'); \n " ;
$actions .= " \t \t \t } \n " ;
$actions .= " \t \t \t else \n " ;
$actions .= " \t \t \t { \n " ;
2006-05-07 03:35:58 +00:00
$actions .= " \t \t \t \t \$ this->set('data', \$ this->data); \n " ;
2006-04-27 10:04:08 +00:00
$actions .= " \t \t \t \t \$ this->validateErrors( \$ this-> { $controllerModel } ); \n " ;
2006-04-24 16:38:16 +00:00
$actions .= " \t \t \t \t \$ this->render(); \n " ;
$actions .= " \t \t \t } \n " ;
$actions .= " \t \t } \n " ;
$actions .= " \t } \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$actions .= " \n " ;
2006-04-27 10:04:08 +00:00
$actions .= " \t function view( \$ id) \n " ;
$actions .= " \t { \n " ;
2006-05-07 03:35:58 +00:00
$actions .= " \t \t \$ this->set('data', \$ this-> { $controllerModel } ->find(' { $controllerModel } . { $tempModel -> primaryKey } = ' . \$ id)); \n " ;
2006-04-24 16:38:16 +00:00
$actions .= " \t } \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$actions .= " \n " ;
2006-04-27 10:04:08 +00:00
$actions .= " \t function delete( \$ id) \n " ;
$actions .= " \t { \n " ;
2006-04-24 16:38:16 +00:00
$actions .= " \t \t \$ this-> { $controllerModel } ->del( \$ id); \n " ;
$actions .= " \t \t \$ this->redirect('/ { $controllerName } /index'); \n " ;
$actions .= " \t } \n " ;
$actions .= " \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$lowerCaseModel = strtolower ( substr ( $controllerModel , 0 , 1 )) . substr ( $controllerModel , 1 );
2006-05-07 03:35:58 +00:00
if ( $tempModel -> displayField === null )
{
$tempModel -> displayField = 'name' ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$actions .= " \t function { $lowerCaseModel } List() \n " ;
$actions .= " \t { \n " ;
2006-05-07 03:35:58 +00:00
$actions .= " \t \t \$ this-> { $controllerModel } ->recursive = 0; \n " ;
$actions .= " \t \t \$ vars = \$ this-> { $controllerModel } ->findAll(null,' { $tempModel -> name } . { $tempModel -> primaryKey } , { $tempModel -> name } . { $tempModel -> displayField } '); \n " ;
2006-04-24 16:38:16 +00:00
$actions .= " \t \t foreach( \$ vars as \$ var) \n " ;
$actions .= " \t \t { \n " ;
2006-05-07 03:35:58 +00:00
$actions .= " \t \t \t \$ list[ \$ var[' { $controllerModel } '][' { $tempModel -> primaryKey } ']] = \$ var[' { $controllerModel } '][' { $tempModel -> displayField } ']; \n " ;
2006-04-24 16:38:16 +00:00
$actions .= " \t \t } \n " ;
$actions .= " \n " ;
$actions .= " \t \t return \$ list; \n " ;
$actions .= " \t } \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$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 ])
2006-04-27 10:04:08 +00:00
{
2006-04-24 16:38:16 +00:00
$this -> stdout ( ucfirst ( $use ) . " , " , false );
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
$this -> stdout ( ucfirst ( $use ));
}
}
}
if ( count ( $helpers ))
{
$this -> stdout ( " Helpers: " , false );
foreach ( $helpers as $help )
{
if ( $help != $helpers [ count ( $helpers ) - 1 ])
2006-04-27 10:04:08 +00:00
{
2006-04-24 16:38:16 +00:00
$this -> stdout ( ucfirst ( $help ) . " , " , false );
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
$this -> stdout ( ucfirst ( $help ));
}
}
}
if ( count ( $components ))
{
$this -> stdout ( " Components: " , false );
foreach ( $components as $comp )
{
if ( $comp != $components [ count ( $components ) - 1 ])
2006-04-27 10:04:08 +00:00
{
2006-04-24 16:38:16 +00:00
$this -> stdout ( ucfirst ( $comp ) . " , " , false );
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
$this -> stdout ( ucfirst ( $comp ));
}
}
}
$this -> hr ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$looksGood = $this -> getInput ( 'Look okay?' , array ( 'y' , 'n' ), 'y' );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( strtolower ( $looksGood ) == 'y' || strtolower ( $looksGood ) == 'yes' )
{
$this -> bakeController ( $controllerClassName , $uses , $helpers , $components , $actions );
2006-04-24 21:22:56 +00:00
if ( $this -> doUnitTest ())
{
$this -> bakeUnitTest ( 'controller' , $controllerClassName );
}
2006-04-24 16:38:16 +00:00
}
else
{
$this -> stdout ( 'Bake Aborted.' );
2006-04-27 10:04:08 +00:00
}
2006-04-24 16:38:16 +00:00
}
2006-04-24 21:22:56 +00:00
function doUnitTest ()
{
if ( is_dir ( 'vendors' . DS . 'simpletest' ) || is_dir ( APP_PATH . 'vendors' . DS . 'simpletest' ))
{
return true ;
}
$unitTest = $this -> getInput ( 'Cake test suite not installed. Do you want to bake unit test files anyway?' , array ( 'y' , 'n' ), 'y' );
$result = strtolower ( $unitTest ) == 'y' || strtolower ( $unitTest ) == 'yes' ;
if ( $result )
{
2006-04-27 10:04:08 +00:00
$this -> stdout ( " \n You can download the Cake test suite from http://cakeforge.org/frs/?group_id=62 " , true );
2006-04-24 21:22:56 +00:00
}
return $result ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
/*---- ----*/
2006-04-27 10:04:08 +00:00
function bakeDbConfig ( $host , $login , $password , $database )
2006-04-24 16:38:16 +00:00
{
$out = " <?php \n " ;
$out .= " class DATABASE_CONFIG \n " ;
$out .= " { \n " ;
$out .= " \t var \$ default = array( \n " ;
$out .= " \t \t 'driver' => 'mysql', \n " ;
$out .= " \t \t 'connect' => 'mysql_pconnect', \n " ;
$out .= " \t \t 'host' => ' $host ', \n " ;
$out .= " \t \t 'login' => ' $login ', \n " ;
$out .= " \t \t 'password' => ' $password ', \n " ;
$out .= " \t \t 'database' => ' $database ' \n " ;
$out .= " \t ); \n " ;
$out .= " } \n " ;
$out .= " ?> " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$filename = CONFIGS . 'database.php' ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$this -> createFile ( $filename , $out );
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function bakeModel ( $modelClassName , $dbConnection , $modelTableName , $validate , $modelAssociations )
{
$out = " <?php \n " ;
$out .= " class $modelClassName extends AppModel \n " ;
$out .= " { \n " ;
$out .= " \t var \$ name = ' $modelClassName '; \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( $dbConnection != 'default' )
{
$out .= " \t var \$ useDbConfig = ' $dbConnection '; \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( $modelTableName != null )
{
$out .= " \t var \$ useTable = ' $modelTableName '; \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +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 " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$out .= " \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( count ( $modelAssociations [ 'belongsTo' ]) || count ( $modelAssociations [ 'hasOne' ]) || count ( $modelAssociations [ 'hasMany' ]) || count ( $modelAssociations [ 'hasAndBelongsToMany' ]))
2006-04-27 10:04:08 +00:00
{
2006-04-24 16:38:16 +00:00
if ( count ( $modelAssociations [ 'belongsTo' ]))
2006-04-27 10:04:08 +00:00
{
2006-04-24 16:38:16 +00:00
$out .= " \t var \$ belongsTo = array( \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
for ( $i = 0 ; $i < count ( $modelAssociations [ 'belongsTo' ]); $i ++ )
{
$out .= " \t \t \t ' { $modelAssociations [ 'belongsTo' ][ $i ] } ' => \n " ;
$out .= " \t \t \t array('className' => ' { $modelAssociations [ 'belongsTo' ][ $i ] } ', \n " ;
$out .= " \t \t \t 'conditions' => '', \n " ;
$out .= " \t \t \t 'order' => '', \n " ;
$out .= " \t \t \t 'foreignKey' => '', \n " ;
$out .= " \t \t \t 'counterCache' => ''), \n \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$out .= " \t ); \n \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( count ( $modelAssociations [ 'hasOne' ]))
{
$out .= " \t var \$ hasOne = array( \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
for ( $i = 0 ; $i < count ( $modelAssociations [ 'hasOne' ]); $i ++ )
{
$out .= " \t \t \t ' { $modelAssociations [ 'hasOne' ][ $i ] } ' => \n " ;
$out .= " \t \t \t array('className' => ' { $modelAssociations [ 'hasOne' ][ $i ] } ', \n " ;
$out .= " \t \t \t 'conditions' => '', \n " ;
$out .= " \t \t \t 'order' => '', \n " ;
$out .= " \t \t \t 'foreignKey' => '', \n " ;
$out .= " \t \t \t 'dependent' => ''), \n \n " ;
}
2006-04-27 10:04:08 +00:00
$out .= " \t ); \n \n " ;
2006-04-24 16:38:16 +00:00
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( count ( $modelAssociations [ 'hasMany' ]))
{
$out .= " \t var \$ hasMany = array( \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
for ( $i = 0 ; $i < count ( $modelAssociations [ 'hasMany' ]); $i ++ )
{
$out .= " \t \t \t ' { $modelAssociations [ 'hasMany' ][ $i ] } ' => \n " ;
$out .= " \t \t \t array('className' => ' { $modelAssociations [ 'hasMany' ][ $i ] } ', \n " ;
$out .= " \t \t \t 'conditions' => '', \n " ;
$out .= " \t \t \t 'order' => '', \n " ;
$out .= " \t \t \t 'foreignKey' => '', \n " ;
$out .= " \t \t \t 'dependent' => '', \n " ;
$out .= " \t \t \t 'exclusive' => '', \n " ;
$out .= " \t \t \t 'finderSql' => '', \n " ;
$out .= " \t \t \t 'counterSql' => ''), \n \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$out .= " \t ); \n \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( count ( $modelAssociations [ 'hasAndBelongsToMany' ]))
{
$out .= " \t var \$ hasAndBelongsToMany = array( \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
for ( $i = 0 ; $i < count ( $modelAssociations [ 'hasAndBelongsToMany' ]); $i ++ )
{
$out .= " \t \t \t ' { $modelAssociations [ 'hasAndBelongsToMany' ][ $i ] } ' => \n " ;
$out .= " \t \t \t array('className' => ' { $modelAssociations [ 'hasAndBelongsToMany' ][ $i ] } ', \n " ;
$out .= " \t \t \t 'conditions' => '', \n " ;
$out .= " \t \t \t 'order' => '', \n " ;
$out .= " \t \t \t 'foreignKey' => '', \n " ;
$out .= " \t \t \t 'joinTable' => '', \n " ;
$out .= " \t \t \t 'associationForeignKey' => '', \n " ;
$out .= " \t \t \t 'uniq' => '', \n " ;
$out .= " \t \t \t 'finderQuery' => '', \n " ;
$out .= " \t \t \t 'deleteQuery' => '', \n " ;
$out .= " \t \t \t 'insertQuery' => ''), \n \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$out .= " \t ); \n \n " ;
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$out .= " } \n " ;
2006-04-27 10:04:08 +00:00
$out .= " ?> " ;
2006-04-24 16:38:16 +00:00
$inflect = new Inflector ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$filename = MODELS . $inflect -> underscore ( $modelClassName ) . '.php' ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$this -> createFile ( $filename , $out );
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function bakeView ( $controllerName , $actionName , $content = '' )
{
$out = " <h1> $actionName </h1> \n " ;
$out .= $content ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$inflect = new Inflector ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( ! file_exists ( VIEWS . strtolower ( $controllerName )))
{
mkdir ( VIEWS . strtolower ( $controllerName ));
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$filename = VIEWS . strtolower ( $controllerName ) . DS . $inflect -> underscore ( $actionName ) . '.thtml' ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$this -> createFile ( $filename , $out );
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function bakeController ( $controllerName , $uses , $helpers , $components , $actions = '' )
{
$inflect = new Inflector ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$out = " <?php \n " ;
$out .= " class $controllerName " . " Controller extends AppController \n " ;
$out .= " { \n " ;
$out .= " \t //var \$ scaffold; \n " ;
$out .= " \t var \$ name = ' $controllerName '; \n " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( count ( $uses ))
{
$out .= " \t var \$ uses = array(' " . $inflect -> singularize ( $controllerName ) . " ', " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
foreach ( $uses as $use )
{
if ( $use != $uses [ count ( $uses ) - 1 ])
2006-04-27 10:04:08 +00:00
{
2006-04-24 16:38:16 +00:00
$out .= " ' " . ucfirst ( $use ) . " ', " ;
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
$out .= " ' " . ucfirst ( $use ) . " ' " ;
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$out .= " ); \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( count ( $helpers ))
{
$out .= " \t var \$ helpers = array('Html', " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
foreach ( $helpers as $help )
{
if ( $help != $helpers [ count ( $helpers ) - 1 ])
2006-04-27 10:04:08 +00:00
{
2006-04-24 16:38:16 +00:00
$out .= " ' " . ucfirst ( $help ) . " ', " ;
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
$out .= " ' " . ucfirst ( $help ) . " ' " ;
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$out .= " ); \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( count ( $components ))
{
$out .= " \t var \$ components = array( " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
foreach ( $components as $comp )
{
if ( $comp != $components [ count ( $components ) - 1 ])
2006-04-27 10:04:08 +00:00
{
2006-04-24 16:38:16 +00:00
$out .= " ' " . ucfirst ( $comp ) . " ', " ;
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
$out .= " ' " . ucfirst ( $comp ) . " ' " ;
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$out .= " ); \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$out .= $actions ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$out .= " } \n " ;
2006-04-27 10:04:08 +00:00
$out .= " ?> " ;
2006-04-24 16:38:16 +00:00
$filename = CONTROLLERS . $inflect -> underscore ( $controllerName ) . '_controller.php' ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$this -> createFile ( $filename , $out );
}
2006-04-24 21:22:56 +00:00
function bakeUnitTest ( $type , $className )
{
$out = '<?php ' . " \n \n " ;
$error = false ;
switch ( $type )
{
case 'model' :
$out .= 'loadModelTest();' . " \n \n " ;
$out .= " class { $className } TestCase extends UnitTestCase \n { \n " ;
$out .= " \t var \$ object = null; \n \n " ;
$out .= " \t function setUp() \n \t { \n \t \t \$ this->object = new { $className } (); \n " ;
$out .= " \t } \n \n \t function tearDown() \n \t { \n \t \t unset( \$ this->object); \n \t } \n " ;
$out .= " \n \t /* \n \t function testMe() \n \t { \n " ;
$out .= " \t \t \$ result = \$ this->object->doSomething(); \n " ;
$out .= " \t \t \$ expected = 1; \n " ;
$out .= " \t \t \$ this->assertEquals( \$ result, \$ expected); \n \t } \n \t */ \n } " ;
$path = MODEL_TESTS ;
$filename = Inflector :: underscore ( $className ) . '.test.php' ;
break ;
case 'controller' :
$out .= 'loadControllerTest();' . " \n \n " ;
$out .= " class { $className } ControllerTestCase extends UnitTestCase \n { \n " ;
$out .= " \t var \$ object = null; \n \n " ;
$out .= " \t function setUp() \n \t { \n \t \t \$ this->object = new { $className } (); \n " ;
$out .= " \t } \n \n \t function tearDown() \n \t { \n \t \t unset( \$ this->object); \n \t } \n " ;
$out .= " \n \t /* \n \t function testMe() \n \t { \n " ;
$out .= " \t \t \$ result = \$ this->object->doSomething(); \n " ;
$out .= " \t \t \$ expected = 1; \n " ;
$out .= " \t \t \$ this->assertEquals( \$ result, \$ expected); \n \t } \n \t */ \n } " ;
$path = CONTROLLER_TESTS ;
$filename = Inflector :: underscore ( $className . 'Controller' ) . '.test.php' ;
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 )
{
if ( $val == '' )
{
unset ( $path [ $i ]);
}
}
$path = implode ( DS , $path );
if ( ! is_dir ( DS . $path ))
{
$create = $this -> getInput ( " Unit test directory does not exist. Create it? " , array ( 'y' , 'n' ), 'y' );
if ( low ( $create ) == 'y' || low ( $create ) == 'yes' )
{
$build = array ();
foreach ( explode ( DS , $path ) as $i => $dir )
{
$build [] = $dir ;
if ( ! is_dir ( DS . implode ( DS , $build )))
{
mkdir ( DS . implode ( DS , $build ));
}
}
}
}
$this -> createFile ( DS . $path . DS . $filename , $out );
}
}
2006-04-24 16:38:16 +00:00
/*----General purpose functions----*/
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function getInput ( $prompt , $options = null , $default = null )
{
if ( ! is_array ( $options ))
{
$print_options = '' ;
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
2006-04-27 10:04:08 +00:00
$print_options = '(' . implode ( '/' , $options ) . ')' ;
2006-04-24 16:38:16 +00:00
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( $default == null )
{
$this -> stdout ( '' );
$this -> stdout ( $prompt . " $print_options \n " . '> ' , false );
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
$this -> stdout ( '' );
$this -> stdout ( $prompt . " $print_options \n " . " [ $default ] > " , false );
}
2006-04-27 10:04:08 +00:00
$result = trim ( fgets ( $this -> stdin ));
2006-04-24 16:38:16 +00:00
if ( $default != null && empty ( $result ))
{
return $default ;
}
2006-04-27 10:04:08 +00:00
else
{
return $result ;
2006-04-24 16:38:16 +00:00
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function stdout ( $string , $newline = true )
{
if ( $newline )
{
fwrite ( $this -> stdout , $string . " \n " );
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
fwrite ( $this -> stdout , $string );
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function stderr ( $string )
{
fwrite ( $this -> stderr , $string );
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function hr ()
{
$this -> stdout ( '---------------------------------------------------------------' );
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function createFile ( $path , $contents )
{
echo " \n Creating file $path\n " ;
$shortPath = str_replace ( ROOT , null , $path );
if ( is_file ( $path ) && ! $this -> dontAsk )
{
fwrite ( $this -> stdout , " File { $shortPath } exists, overwrite? (y/n/q): " );
$key = trim ( fgets ( $this -> stdin ));
if ( $key == 'q' )
{
fwrite ( $this -> stdout , " Quitting. \n " );
exit ;
}
elseif ( $key == 'a' )
{
$this -> dont_ask = true ;
}
elseif ( $key == 'y' )
{
}
else
{
fwrite ( $this -> stdout , " Skip { $shortPath } \n " );
return false ;
}
}
if ( $f = fopen ( $path , 'w' ))
{
fwrite ( $f , $contents );
fclose ( $f );
fwrite ( $this -> stdout , " Wrote { $shortPath } \n " );
return true ;
}
else
{
fwrite ( $this -> stderr , " Error! Couldn't open { $shortPath } for writing. \n " );
return false ;
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
//----------------[Modified Form Helper Methods]--------------------------------------------------------------------------------//
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function generateFields ( $fields , $readOnly = false )
{
$strFormFields = '' ;
foreach ( $fields as $field )
{
if ( isset ( $field [ 'type' ]))
{
if ( ! isset ( $field [ 'required' ]))
{
$field [ 'required' ] = false ;
}
if ( ! isset ( $field [ 'errorMsg' ]))
{
$field [ 'errorMsg' ] = null ;
}
if ( ! isset ( $field [ 'htmlOptions' ]))
{
$field [ 'htmlOptions' ] = array ();
}
if ( $readOnly )
{
$field [ 'htmlOptions' ][ 'READONLY' ] = " readonly " ;
}
switch ( $field [ 'type' ] )
{
case " input " :
if ( ! isset ( $field [ 'size' ] ) )
{
$field [ 'size' ] = 40 ;
}
$strFormFields = $strFormFields . $this -> generateInputDiv ( $field [ 'tagName' ], $field [ 'prompt' ], $field [ 'required' ], $field [ 'errorMsg' ], $field [ 'size' ], $field [ 'htmlOptions' ] );
break ;
case " checkbox " :
$strFormFields = $strFormFields . $this -> generateCheckboxDiv ( $field [ 'tagName' ], $field [ 'prompt' ], $field [ 'required' ], $field [ 'errorMsg' ], $field [ 'htmlOptions' ] );
break ;
case " select " ;
case " selectMultiple " ;
if ( " selectMultiple " == $field [ 'type' ] )
{
$field [ 'selectAttr' ][ 'multiple' ] = 'multiple' ;
$field [ 'selectAttr' ][ 'class' ] = 'selectMultiple' ;
}
if ( ! isset ( $field [ 'selected' ]))
{
$field [ 'selected' ] = null ;
}
if ( ! isset ( $field [ 'selectAttr' ]))
{
$field [ 'selectAttr' ] = null ;
}
if ( ! isset ( $field [ 'optionsAttr' ]))
{
$field [ 'optionsAttr' ] = null ;
}
if ( $readOnly )
{
$field [ 'selectAttr' ][ 'DISABLED' ] = true ;
}
if ( ! isset ( $field [ 'options' ]))
{
$field [ 'options' ] = null ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$strFormFields = $strFormFields . $this -> generateSelectDiv ( $field [ 'tagName' ], $field [ 'prompt' ], $field [ 'options' ], $field [ 'selected' ], $field [ 'selectAttr' ], $field [ 'optionsAttr' ], $field [ 'required' ], $field [ 'errorMsg' ] );
break ;
case " area " ;
if ( ! isset ( $field [ 'rows' ]))
{
$field [ 'rows' ] = 10 ;
}
if ( ! isset ( $field [ 'cols' ]))
{
$field [ 'cols' ] = 60 ;
}
$strFormFields = $strFormFields . $this -> generateAreaDiv ( $field [ 'tagName' ], $field [ 'prompt' ], $field [ 'required' ], $field [ 'errorMsg' ], $field [ 'cols' ], $field [ 'rows' ], $field [ 'htmlOptions' ] );
break ;
case " fieldset " ;
$strFieldsetFields = $this -> generateFields ( $field [ 'fields' ] );
$strFieldSet = sprintf ( '
< fieldset >< legend >% s </ legend >< div class = " notes " >< h4 >% s </ h4 >< p class = " last " >% s </ p ></ div >% s </ fieldset > ' ,
$field [ 'legend' ], $field [ 'noteHeading' ], $field [ 'note' ], $strFieldsetFields );
$strFormFields = $strFormFields . $strFieldSet ;
break ;
case " hidden " ;
//$strFormFields = $strFormFields . $this->Html->hiddenTag( $field['tagName']);
break ;
case " date " :
if ( ! isset ( $field [ 'selected' ]))
{
$field [ 'selected' ] = null ;
}
$strFormFields = $strFormFields . $this -> generateDate ( $field [ 'tagName' ], $field [ 'prompt' ], null , null , null , null , $field [ 'selected' ]);
break ;
case " datetime " :
if ( ! isset ( $field [ 'selected' ]))
{
$field [ 'selected' ] = null ;
}
$strFormFields = $strFormFields . $this -> generateDateTime ( $field [ 'tagName' ], $field [ 'prompt' ], '' , '' , '' , '' , $field [ 'selected' ]);
break ;
default :
break ;
}
}
}
return $strFormFields ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function generateAreaDiv ( $tagName , $prompt , $required = false , $errorMsg = null , $cols = 60 , $rows = 10 , $htmlOptions = null )
{
$htmlOptions [ 'id' ] = strtolower ( str_replace ( '/' , '_' , $tagName ));
$htmlAttributes = $htmlOptions ;
$htmlAttributes [ 'cols' ] = $cols ;
$htmlAttributes [ 'rows' ] = $rows ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$tagNameArray = explode ( '/' , $tagName );
$htmlAttributes [ 'value' ] = " \$ data[' { $tagNameArray [ 0 ] } '][' { $tagNameArray [ 1 ] } '] " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$str = " \t <?php echo \$ html->textarea(' { $tagName } ', " . $this -> attributesToArray ( $htmlAttributes ) . " ) ?> \n " ;
$str .= " \t <?php echo \$ html->tagErrorMsg(' { $tagName } ', 'Error message for { $tagNameArray [ 1 ] } goes here.') ?> \n " ;
$strLabel = " \n \t " . $this -> labelTag ( $tagName , $prompt );
$divClass = " optional " ;
if ( $required )
$divClass = " required " ;
$strError = " " ; // initialize the error to empty.
if ( $this -> isFieldError ( $tagName ) )
{
// if it was an error that occured, then add the error message, and append " error" to the div tag.
$strError = $this -> pTag ( 'error' , $errorMsg );
$divClass = sprintf ( " %s error " , $divClass );
}
$divTagInside = sprintf ( " %s %s %s " , $strError , $strLabel , $str );
return $this -> divTag ( $divClass , $divTagInside );
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function generateCheckboxDiv ( $tagName , $prompt , $required = false , $errorMsg = null , $htmlOptions = null )
{
$htmlOptions [ 'class' ] = " inputCheckbox " ;
$htmlOptions [ 'id' ] = strtolower ( str_replace ( '/' , '_' , $tagName ));
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$tagNameArray = explode ( '/' , $tagName );
2006-05-04 00:35:48 +00:00
$htmlAttributes [ 'checked' ] = " \$ data[' { $tagNameArray [ 0 ] } '][' { $tagNameArray [ 1 ] } '] ? 'checked' : '' " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$str = " \t <?php echo \$ html->checkbox(' { $tagName } ', null, " . $this -> attributesToArray ( $htmlAttributes ) . " )?> \n " ;
$str .= " \t <?php echo \$ html->tagErrorMsg(' { $tagName } ', 'Error message for { $tagNameArray [ 1 ] } goes here.') ?> \n " ;
$strLabel = " \n \t " . $this -> labelTag ( $tagName , $prompt );
$divClass = " optional " ;
if ( $required )
$divClass = " required " ;
$strError = " " ; // initialize the error to empty.
if ( $this -> isFieldError ( $tagName ) )
{
// if it was an error that occured, then add the error message, and append " error" to the div tag.
$strError = $this -> pTag ( 'error' , $errorMsg );
$divClass = sprintf ( " %s error " , $divClass );
}
$divTagInside = sprintf ( " %s %s %s " , $strError , $strLabel , $str );
return $this -> divTag ( $divClass , $divTagInside );
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function generateDate ( $tagName , $prompt , $required = false , $errorMsg = null , $size = 20 , $htmlOptions = null , $selected = null )
{
$htmlOptions [ 'id' ] = strtolower ( str_replace ( '/' , '_' , $tagName ));
$tagNameArray = explode ( '/' , $tagName );
$str = " \t <?php echo \$ html->dateTimeOptionTag(' { $tagName } ', 'MDY' , 'NONE', \$ data[' { $tagNameArray [ 0 ] } '][' { $tagNameArray [ 1 ] } '], " . $this -> attributesToArray ( $htmlOptions ) . " )?> \n " ;
$str .= " \t <?php echo \$ html->tagErrorMsg(' { $tagName } ', 'Error message for { $tagNameArray [ 1 ] } goes here.') ?> \n " ;
$strLabel = " \n \t " . $this -> labelTag ( $tagName , $prompt );
$divClass = " optional " ;
if ( $required )
$divClass = " required " ;
$strError = " " ; // initialize the error to empty.
if ( $this -> isFieldError ( $tagName ) )
{
// if it was an error that occured, then add the error message, and append " error" to the div tag.
$strError = $this -> pTag ( 'error' , $errorMsg );
$divClass = sprintf ( " %s error " , $divClass );
}
$divTagInside = sprintf ( " %s %s %s " , $strError , $strLabel , $str );
$requiredDiv = $this -> divTag ( $divClass , $divTagInside );
return $this -> divTag ( " date " , $requiredDiv );
}
2006-04-27 10:04:08 +00:00
2006-05-07 03:35:58 +00:00
function generateDateTime ( $tagName , $prompt , $required = false , $errorMsg = null , $size = 20 , $htmlOptions = null , $selected = null )
2006-05-04 00:35:48 +00:00
{
$htmlOptions [ 'id' ] = strtolower ( str_replace ( '/' , '_' , $tagName ));
$tagNameArray = explode ( '/' , $tagName );
$str = " \t <?php echo \$ html->dateTimeOptionTag(' { $tagName } ', 'MDY' , '12', \$ data[' { $tagNameArray [ 0 ] } '][' { $tagNameArray [ 1 ] } '], " . $this -> attributesToArray ( $htmlOptions ) . " )?> \n " ;
$str .= " \t <?php echo \$ html->tagErrorMsg(' { $tagName } ', 'Error message for { $tagNameArray [ 1 ] } goes here.') ?> \n " ;
$strLabel = " \n \t " . $this -> labelTag ( $tagName , $prompt );
$divClass = " optional " ;
if ( $required )
$divClass = " required " ;
$strError = " " ; // initialize the error to empty.
if ( $this -> isFieldError ( $tagName ) )
{
// if it was an error that occured, then add the error message, and append " error" to the div tag.
$strError = $this -> pTag ( 'error' , $errorMsg );
$divClass = sprintf ( " %s error " , $divClass );
}
$divTagInside = sprintf ( " %s %s %s " , $strError , $strLabel , $str );
$requiredDiv = $this -> divTag ( $divClass , $divTagInside );
return $this -> divTag ( " date " , $requiredDiv );
}
2006-04-24 16:38:16 +00:00
function generateInputDiv ( $tagName , $prompt , $required = false , $errorMsg = null , $size = 20 , $htmlOptions = null )
{
$htmlOptions [ 'id' ] = strtolower ( str_replace ( '/' , '_' , $tagName ));
$htmlAttributes = $htmlOptions ;
$htmlAttributes [ 'size' ] = $size ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$tagNameArray = explode ( '/' , $tagName );
$htmlAttributes [ 'value' ] = " \$ data[' { $tagNameArray [ 0 ] } '][' { $tagNameArray [ 1 ] } '] " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$str = " \t <?php echo \$ html->input(' { $tagName } ', " . $this -> attributesToArray ( $htmlAttributes ) . " ) ?> \n " ;
$str .= " \t <?php echo \$ html->tagErrorMsg(' { $tagName } ', 'Error message for { $tagNameArray [ 1 ] } goes here.') ?> \n " ;
$strLabel = " \n \t " . $this -> labelTag ( $tagName , $prompt );
$divClass = " optional " ;
if ( $required )
$divClass = " required " ;
$strError = " " ; // initialize the error to empty.
if ( $this -> isFieldError ( $tagName ) )
{
// if it was an error that occured, then add the error message, and append " error" to the div tag.
$strError = $this -> pTag ( 'error' , $errorMsg );
$divClass = sprintf ( " %s error " , $divClass );
}
$divTagInside = sprintf ( " %s %s %s " , $strError , $strLabel , $str );
return $this -> divTag ( $divClass , $divTagInside );
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function generateSelectDiv ( $tagName , $prompt , $options , $selected = null , $selectAttr = null , $optionAttr = null , $required = false , $errorMsg = null )
{
$selectAttr [ 'id' ] = strtolower ( str_replace ( '/' , '_' , $tagName ));
$tagNameArray = explode ( '/' , $tagName );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$inflect = new Inflector ();
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$model = str_replace ( '_id' , '' , $tagNameArray [ 1 ]);
$properModel = $inflect -> camelize ( $model );
$controllerPath = strtolower ( substr ( $inflect -> pluralize ( $properModel ), 0 , 1 )) . substr ( $inflect -> pluralize ( $properModel ), 1 );
$actionPath = strtolower ( substr ( $properModel , 0 , 1 )) . substr ( $properModel , 1 ) . 'List' ;
$path = " / $controllerPath / $actionPath " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
if ( $selectAttr [ 'multiple' ] != 'multiple' )
{
$str = " \t <?php echo \$ html->selectTag(' { $tagName } ', " . " \$ this->requestAction(' { $path } '), \$ data[' { $tagNameArray [ 0 ] } '][' { $tagNameArray [ 1 ] } '], " . $this -> attributesToArray ( $selectAttr ) . " ) ?> \n " ;
$str .= " \t <?php echo \$ html->tagErrorMsg(' { $tagName } ', 'Error message for { $tagNameArray [ 1 ] } goes here.') ?> \n " ;
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
$lowerName = strtolower ( $tagNameArray [ 0 ]);
2006-05-07 03:35:58 +00:00
$str = " \t <?php if(isset( \$ data[' { $tagNameArray [ 0 ] } '])): ?> \n " ;
$str .= " \t <?php foreach ( \$ data[' { $tagNameArray [ 0 ] } '] as \$ var): \$ { $lowerName } Options[ \$ var['id']] = \$ var['id']; endforeach; ?> \n " ;
$str .= " \t <?php else: ?> \n " ;
$str .= " \t <?php \$ { $lowerName } Options = null;?> \n " ;
$str .= " \t <?php endif ?> \n " ;
2006-04-24 16:38:16 +00:00
$str .= " \t <?php echo \$ html->selectTag(' { $tagName } ', " . " \$ this->requestAction(' { $path } '), \$ { $lowerName } Options, " . $this -> attributesToArray ( $selectAttr ) . " ) ?> \n " ;
$str .= " \t <?php echo \$ html->tagErrorMsg(' { $tagName } ', 'Error message for { $tagNameArray [ 1 ] } goes here.') ?> \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$strLabel = " \n \t " . $this -> labelTag ( $tagName , $prompt );
$divClass = " optional " ;
if ( $required )
$divClass = " required " ;
$strError = " " ; // initialize the error to empty.
if ( $this -> isFieldError ( $tagName ) )
{
// if it was an error that occured, then add the error message, and append " error" to the div tag.
$strError = $this -> pTag ( 'error' , $errorMsg );
$divClass = sprintf ( " %s error " , $divClass );
}
$divTagInside = sprintf ( " %s %s %s " , $strError , $strLabel , $str );
return $this -> divTag ( $divClass , $divTagInside );
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function generateSubmitDiv ( $displayText , $htmlOptions = null )
{
return $this -> divTag ( 'submit' , $this -> Html -> submitTag ( $displayText , $htmlOptions ) );
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function labelTag ( $tagName , $text )
{
return sprintf ( TAG_LABEL , strtolower ( str_replace ( '/' , '_' , $tagName )), $text ) . " \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function isFieldError ( $field )
{
$error = 1 ;
$this -> Html -> setFormTag ( $field );
if ( $error == $this -> Html -> tagIsInvalid ( $this -> Html -> model , $this -> Html -> field ) )
{
return true ;
}
else
{
return false ;
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function pTag ( $class , $text )
{
return sprintf ( TAG_P_CLASS , $class , $text ) . " \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function divTag ( $class , $text )
{
return sprintf ( TAG_DIV , $class , $text ) . " \n " ;
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
//=-=-=-=
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
function attributesToArray ( $htmlAttributes )
{
if ( is_array ( $htmlAttributes ))
{
$keys = array_keys ( $htmlAttributes );
$vals = array_values ( $htmlAttributes );
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
$out = " array( " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
for ( $i = 0 ; $i < count ( $htmlAttributes ); $i ++ )
{
//don't put vars in quotes
if ( substr ( $vals [ $i ], 0 , 1 ) != '$' )
{
$out .= " ' { $keys [ $i ] } ' => ' { $vals [ $i ] } ', " ;
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
$out .= " ' { $keys [ $i ] } ' => { $vals [ $i ] } , " ;
}
}
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
//Chop off last comma
if ( substr ( $out , - 3 , 1 ) == ',' )
{
$out = substr ( $out , 0 , strlen ( $out ) - 2 );
}
$out .= " ) " ;
2006-04-27 10:04:08 +00:00
2006-04-24 16:38:16 +00:00
return $out ;
}
2006-04-27 10:04:08 +00:00
else
2006-04-24 16:38:16 +00:00
{
return 'array()' ;
}
}
}
2005-05-22 23:24:09 +00:00
2006-04-24 16:38:16 +00:00
/*
@@@
Make options array in selectTag dynamic ( create a listModels function in the controller and use requestAction ? )
2005-05-22 23:24:09 +00:00
2006-04-24 16:38:16 +00:00
*/
2005-05-22 23:24:09 +00:00
2006-05-07 03:35:58 +00:00
?>