* Copyright 2005-2007, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.console.libs.tasks
* @since CakePHP(tm) v 1.2
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Task class for creating and updating view files.
*
* @package cake
* @subpackage cake.cake.console.libs.tasks
*/
class ViewTask extends BakeShell {
function execute() {
if(empty($this->args)) {
$this->__interactive();
}
}
function __interactive() {
$this->hr();
$this->out('View Bake:');
$this->hr();
$uses = array();
$wannaUseSession = 'y';
$wannaDoScaffold = 'y';
$useDbConfig = 'default';
$this->__doList($useDbConfig, 'Controllers');
$enteredController = '';
while ($enteredController == '') {
$enteredController = $this->in('Enter a number from the list above, or type in the name of another controller.');
if ($enteredController == '' || intval($enteredController) > count($this->_controllerNames)) {
$this->out('Error:');
$this->out("The Controller name you supplied was empty, or the number \nyou selected was not an option. Please try again.");
$enteredController = '';
}
}
if (intval($enteredController) > 0 && intval($enteredController) <= count($this->_controllerNames) ) {
$controllerName = $this->_controllerNames[intval($enteredController) - 1];
} else {
$controllerName = Inflector::camelize($enteredController);
}
$controllerPath = low(Inflector::underscore($controllerName));
$doItInteractive = $this->in("Would you like bake to build your views interactively?\nWarning: Choosing no will overwrite {$controllerName} views if it exist.", array('y','n'), 'y');
if (low($doItInteractive) == 'y' || low($doItInteractive) == 'yes') {
$this->interactive = true;
$wannaDoScaffold = $this->in("Would you like to create some scaffolded views (index, add, view, edit) for this controller?\nNOTE: Before doing so, you'll need to create your controller and model classes (including associated models).", array('y','n'), 'n');
}
$admin = null;
$admin_url = null;
if (low($wannaDoScaffold) == 'y' || low($wannaDoScaffold) == 'yes') {
$wannaDoAdmin = $this->in("Would you like to create the views for admin routing?", array('y','n'), 'y');
}
if ((low($wannaDoAdmin) == 'y' || low($wannaDoAdmin) == 'yes')) {
if(defined('CAKE_ADMIN')) {
$admin = CAKE_ADMIN . '_';
$admin_url = '/'.CAKE_ADMIN;
} else {
$adminRoute = '';
$this->out('You need to enable CAKE_ADMIN in /app/config/core.php to use admin routing.');
$this->out('What would you like the admin route to be?');
$this->out('Example: www.example.com/admin/controller');
while ($adminRoute == '') {
$adminRoute = $this->in("What would you like the admin route to be?", null, 'admin');
}
if($this->__addAdminRoute($adminRoute) !== true){
$this->out('Unable to write to /app/config/core.php.');
$this->out('You need to enable CAKE_ADMIN in /app/config/core.php to use admin routing.');
exit();
} else {
$admin = $adminRoute . '_';
$admin_url = '/'.$adminRoute;
}
}
}
if (low($wannaDoScaffold) == 'y' || low($wannaDoScaffold) == 'yes') {
$file = CONTROLLERS . $controllerPath . '_controller.php';
if(!file_exists($file)) {
$shortPath = str_replace(ROOT, null, $file);
$shortPath = str_replace('../', '', $shortPath);
$shortPath = str_replace('//', '/', $shortPath);
$this->out('');
$this->out("The file '$shortPath' could not be found.\nIn order to scaffold, you'll need to first create the controller. ");
$this->out('');
die();
} else {
uses('controller'.DS.'controller');
loadController($controllerName);
//loadModels();
if($admin) {
$this->__bake($controllerName, $controllerPath, $admin, $admin_url);
}
$this->__bake($controllerName, $controllerPath, null, null);
$this->hr();
$this->out('');
$this->out('View Scaffolding Complete.'."\n");
}
} else {
$actionName = '';
while ($actionName == '') {
$actionName = $this->in('Action Name? (use camelCased function name)');
if ($actionName == '') {
$this->out('The action name you supplied was empty. Please try again.');
}
}
$this->out('');
$this->hr();
$this->out('The following view will be created:');
$this->hr();
$this->out("Controller Name: $controllerName");
$this->out("Action Name: $actionName");
$this->out("Path: app/views/" . $controllerPath . DS . Inflector::underscore($actionName) . '.ctp');
$this->hr();
$looksGood = $this->in('Look okay?', array('y','n'), 'y');
if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
$this->__bakeView($controllerName, $actionName);
} else {
$this->out('Bake Aborted.');
}
}
}
function __bake($controllerName, $controllerPath, $admin= null, $admin_url = null) {
$controllerClassName = $controllerName.'Controller';
$controllerObj = & new $controllerClassName();
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;
$modelKey = $controllerObj->modelKey;
$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;
$indexView .= "
\n";
$indexView .= "
List " . $pluralHumanName . "
\n\n";
$indexView .= "
\n";
$indexView .= "\t\n";
foreach ($fieldNames as $fieldName) {
$indexView .= "\t\tsort('{$fieldName['name']}');?> | \n";
}
$indexView .= "\t\tActions | \n";
$indexView .= "\t
\n";
$indexView .= "\n";
$indexView .= "\t\n";
$count = 0;
foreach($fieldNames as $field => $value) {
if(isset($value['foreignKey'])) {
$otherModelName = $this->_modelName($value['model']);
$otherModelKey = Inflector::underscore($value['modelKey']);
$otherModelObj =& ClassRegistry::getObject($otherModelKey);
$otherControllerName = $this->_controllerName($value['modelKey']);
$otherControllerPath = $this->_controllerPath($otherControllerName);
if(is_object($otherModelObj)) {
$displayField = $otherModelObj->getDisplayField();
$indexView .= "\t\tlink(\$".$singularName."['{$otherModelName}']['{$displayField}'], array('controller'=> '{$otherControllerPath}', 'action'=>'view', \$".$singularName."['{$otherModelName}']['{$otherModelObj->primaryKey}'])); ?> | \n";
} else {
$indexView .= "\t\tname}']['{$field}']; ?> | \n";
}
$count++;
} else {
$indexView .= "\t\tname}']['{$field}']; ?> | \n";
}
}
$indexView .= "\t\t\n";
$indexView .= "\t\t\tlink('View', array('action'=>'view', \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}'])); ?>\n";
$indexView .= "\t\t\tlink('Edit', array('action'=>'edit', \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}'])); ?>\n";
$indexView .= "\t\t\tlink('Delete', array('action'=>'delete', \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}']), null, 'Are you sure you want to delete #' . \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}']); ?>\n";
$indexView .= "\t\t | \n";
$indexView .= "\t
\n";
$indexView .= "\n";
$indexView .= "
\n\n";
$indexView .= "
\n";
$indexView .= "\n";
$indexView .= "prev('<< previous', array(), null, array('class'=>'disabled'));?>\n";
$indexView .= "|\n";
$indexView .= "next('next >>', array(), null, array('class'=>'disabled'));?>\n";
$indexView .= "
\n";
$indexView .= "\n";
$indexView .= "\t
\n";
$indexView .= "\t\t- link('New {$singularHumanName}', array('action'=>'add')); ?>
\n";
$indexView .= "\t
\n";
$indexView .= "
";
//-------------------------[VIEW]-------------------------//
$viewView = null;
$viewView .= "\n";
$viewView .= "
View " . $singularHumanName . "
\n\n";
$viewView .= "\t
\n";
$count = 0;
foreach($fieldNames as $field => $value) {
$viewView .= "\t\t- " . $value['label'] . "
\n";
if(isset($value['foreignKey'])) {
$otherModelName = $this->_modelName($value['model']);
$otherModelKey = Inflector::underscore($value['modelKey']);
$otherModelObj =& ClassRegistry::getObject($value['modelKey']);
$otherControllerName = $this->_controllerName($value['modelKey']);
$otherControllerPath = $this->_controllerPath($otherControllerName);
$displayField = $otherModelObj->getDisplayField();
$viewView .= "\t\t- link(\$".$singularName."['{$otherModelName}']['{$displayField}'], array('controller'=> '{$otherControllerPath}', 'action'=>'view', \$".$singularName."['{$otherModelName}']['{$otherModelObj->primaryKey}'])); ?>
\n";
$count++;
} else {
$viewView .= "\t\t- name}']['{$field}']?>
\n";
}
}
$viewView .= "\t
\n";
$viewView .= "
\n";
$viewView .= "\n";
$viewView .= "\t
\n";
$viewView .= "\t\t- link('Edit " . $singularHumanName . "', array('action'=>'edit', \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}'])); ?>
\n";
$viewView .= "\t\t- link('Delete " . $singularHumanName . "', array('action'=>'delete', \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}']), null, 'Are you sure you want to delete #' . \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}'] . '?'); ?>
\n";
$viewView .= "\t\t- link('List " . $pluralHumanName ."', array('action'=>'index')); ?>
\n";
$viewView .= "\t\t- link('New " . $singularHumanName . "', array('action'=>'add')); ?>
\n";
foreach( $fieldNames as $field => $value ) {
if( isset( $value['foreignKey'] ) ) {
$otherModelName = $this->_modelName($value['modelKey']);
if($otherModelName != $currentModelName) {
$otherControllerName = $this->_controllerName($otherModelName);
$otherControllerPath = $this->_controllerPath($otherControllerName);
$otherSingularHumanName = $this->_singularHumanName($value['controller']);
$otherPluralHumanName = $this->_pluralHumanName($value['controller']);
$viewView .= "\t\t- link('List " . $otherSingularHumanName . "', array('controller'=> '{$otherControllerPath}', 'action'=>'index')); ?>
\n";
$viewView .= "\t\t- link('New " . $otherPluralHumanName . "', array('controller'=> '{$otherControllerPath}', 'action'=>'add')); ?>
\n";
}
}
}
$viewView .= "\t
\n\n";
$viewView .= "
\n";
foreach ($modelObj->hasOne as $associationName => $relation) {
$new = true;
$otherModelName = $this->_modelName($relation['className']);
$otherControllerName = $this->_controllerName($otherModelName);
$otherControllerPath = $this->_controllerPath($otherControllerName);
$otherSingularName = $this->_singularName($associationName);
$otherPluralHumanName = $this->_pluralHumanName($associationName);
$otherSingularHumanName = $this->_singularHumanName($associationName);
$otherModelKey = Inflector::underscore($relation['className']);
$otherModelObj =& ClassRegistry::getObject($otherModelKey);
$viewView .= "\n";
}
$relations = array_merge($modelObj->hasMany, $modelObj->hasAndBelongsToMany);
foreach($relations as $associationName => $relation) {
$otherModelName = $associationName;
$otherControllerName = $this->_controllerName($relation['className']);
$otherControllerPath = $this->_controllerPath($otherControllerName);
$otherSingularName = $this->_singularName($associationName);
$otherPluralHumanName = $this->_pluralHumanName($associationName);
$otherSingularHumanName = $this->_singularHumanName($associationName);
$otherModelKey = Inflector::underscore($relation['className']);
$otherModelObj =& ClassRegistry::getObject($otherModelKey);
$viewView .= "\n";
}
$fields = $controllerObj->generateFieldNames(null, true);
//-------------------------[EDIT]-------------------------//
$editView = null;
$editView .= "\n";
$editView .= "
Edit " . $singularHumanName . "
\n";
$editView .= "\tcreate('{$currentModelName}');?>\n";
$editView .= $this->inputs($fields);
$editView .= "\t\tsubmit('Update');?>\n";
$editView .= "\t\n";
$editView .= "\n";
$editView .= "\n";
$editView .= "\t
\n";
$editView .= "\t\t- link('Delete', array('action'=>'delete', \$html->tagValue('{$modelObj->name}/{$modelObj->primaryKey}')), null, 'Are you sure you want to delete #' . \$html->tagValue('{$modelObj->name}/{$modelObj->primaryKey}')); ?>\n";
$editView .= "\t\t
- link('List {$pluralHumanName}', array('action'=>'index')); ?>
\n";
foreach ($modelObj->belongsTo as $associationName => $relation) {
$otherModelName = $this->_modelName($relation['className']);
if($otherModelName != $currentModelName) {
$otherControllerName = $this->_controllerName($otherModelName);
$otherControllerPath = $this->_controllerPath($otherControllerName);
$otherSingularName = $this->_singularName($associationName);
$otherPluralName = $this->_pluralHumanName($associationName);
$editView .= "\t\t- link('View " . $otherPluralName . "', array('controller'=> '{$otherControllerPath}', 'action'=>'view')); ?>
\n";
$editView .= "\t\t- link('Add " . $otherPluralName . "', array('controller'=> '{$otherControllerPath}', 'action'=>'add')); ?>
\n";
}
}
$editView .= "\t
\n";
$editView .= "
\n";
//-------------------------[ADD]-------------------------//
unset($fields[$modelObj->primaryKey]);
$addView = null;
$addView .= "\n";
$addView .= "
New " . $singularHumanName . "
\n";
$addView .= "\tcreate('{$currentModelName}');?>\n";
$addView .= $this->inputs($fields);
$addView .= "\t\tsubmit('Add');?>\n";
$addView .= "\t\n";
$addView .= "\n";
$addView .= "\n";
$addView .= "\t
\n";
$addView .= "\t\t- link('List {$pluralHumanName}', array('action'=>'index')); ?>
\n";
foreach ($modelObj->belongsTo as $associationName => $relation) {
$otherModelName = $this->_modelName($relation['className']);
if($otherModelName != $currentModelName) {
$otherControllerName = $this->_controllerName($otherModelName);
$otherControllerPath = $this->_controllerPath($otherControllerName);
$otherSingularName = $this->_singularName($associationName);
$otherPluralName = $this->_pluralHumanName($associationName);
$addView .= "\t\t- link('View " . $otherPluralName . "', array('controller'=> '{$otherControllerPath}', 'action'=>'view'));?>
\n";
$addView .= "\t\t- link('Add " . $otherPluralName . "', array('controller'=> '{$otherControllerPath}', 'action'=>'add')); ?>
\n";
}
}
$addView .= "\t
\n";
$addView .= "
\n";
//------------------------------------------------------------------------------------//
$Folder =& new Folder(VIEWS . $controllerPath, true);
if($path = $Folder->cd(VIEWS . $controllerPath)) {
$path = $Folder->slashTerm(VIEWS . $controllerPath);
$filename = $path . $admin . 'index.ctp';
$this->createFile($filename, $indexView);
$filename = $path . $admin . 'view.ctp';
$this->createFile($filename, $viewView);
$filename = $path . $admin . 'add.ctp';
$this->createFile($filename, $addView);
$filename = $path . $admin . 'edit.ctp';
$this->createFile($filename, $editView);
} else {
return false;
}
}
/**
* Assembles and writes a View file.
*
* @param string $controllerName
* @param string $actionName
* @param string $content
*/
function __bakeView($controllerName, $actionName, $content = '') {
$out = "{$actionName}
\n";
$out .= $content;
if(!file_exists(VIEWS.$this->_controllerPath($controllerName))) {
mkdir(VIEWS.$this->_controllerPath($controllerName));
}
$filename = VIEWS . $this->_controllerPath($controllerName) . DS . Inflector::underscore($actionName) . '.ctp';
$Folder =& new Folder(VIEWS . $controllerPath, true);
if($path = $Folder->cd(VIEWS . $controllerPath)) {
$path = $Folder->slashTerm(VIEWS . $controllerPath);
return $this->createFile($filename, $out);
} else {
return false;
}
}
/**
* returns the fields to be display in the baked forms.
*
* @access private
* @param array $fields
*/
function inputs($fields = array()) {
$displayFields = null;
foreach($fields as $name => $options) {
if(isset($options['tagName'])){
$tag = explode('/', $options['tagName']);
$tagName = $tag[1];
unset($options['tagName']);
}
$formOptions = array();
if(isset($options['type'])){
$type = $options['type'];
unset($options['type']);
//$formOptions['type'] = "'type' => '{$type}'";
}
if(isset($options['class']) && $options['class'] == 'required'){
$class = $options['class'];
unset($options['class']);
$formOptions['class'] = "'class' => '{$class}'";
}
if(isset($options['options'])){
unset($formOptions['type']);
$fieldOptions = $this->_pluralName($options['model']);
unset($options['options']);
$formOptions['options'] = "'options' => \${$fieldOptions}";
if(isset($options['multiple'])){
$formOptions['multiple'] = "'multiple' => 'multiple'";
$tagName = $tagName.'/'.$tagName;
}
}
if(isset($options['size'])){
$size = $options['size'];
unset($options['size']);
//$formOptions['size'] = "'size' => '{$size}'";
}
if(isset($options['cols'])){
$cols = $options['cols'];
unset($options['cols']);
//$formOptions['cols'] = "'cols' => '{$cols}'";
}
if(isset($options['rows'])){
$rows = $options['rows'];
unset($options['rows']);
//$formOptions['rows'] = "'rows' => '{$rows}'";
}
if(!empty($formOptions)) {
$formOptions = ", array(".join(', ', $formOptions).")";
} else {
$formOptions = null;
}
$displayFields .= "\t\tinput('{$tagName}'{$formOptions});?>\n";
}
return $displayFields;
}
/**
* outputs the a list of possible models or controllers from database
*
* @param string $useDbConfig
* @param string $type = Models or Controllers
* @return output
*/
function __doList($useDbConfig = 'default') {
$db =& ConnectionManager::getDataSource($useDbConfig);
$usePrefix = empty($db->config['prefix']) ? '' : $db->config['prefix'];
if ($usePrefix) {
$tables = array();
foreach ($db->listSources() as $table) {
if (!strncmp($table, $usePrefix, strlen($usePrefix))) {
$tables[] = substr($table, strlen($usePrefix));
}
}
} else {
$tables = $db->listSources();
}
$this->__tables = $tables;
$this->out('Possible Models based on your current database:');
$this->_controllerNames = array();
$count = count($tables);
for ($i = 0; $i < $count; $i++) {
$this->_controllerNames[] = $this->_controllerName($this->_modelName($tables[$i]));
$this->out($i + 1 . ". " . $this->_controllerNames[$i]);
}
}
}