diff --git a/cake/console/libs/tasks/view.php b/cake/console/libs/tasks/view.php
index a6130601c..fddddbfeb 100644
--- a/cake/console/libs/tasks/view.php
+++ b/cake/console/libs/tasks/view.php
@@ -32,92 +32,172 @@
* @package cake
* @subpackage cake.cake.console.libs.tasks
*/
-class ViewTask extends BakeShell {
-
-
+class ViewTask extends Shell {
+/**
+ * Tasks to be loaded by this Task
+ *
+ * @var array
+ */
+ var $tasks = array('Project', 'Controller');
+/**
+ * name of the controller being used
+ *
+ * @var string
+ */
+ var $controllerName = null;
+/**
+ * path to controller to put views
+ *
+ * @var string
+ */
+ var $controllerPath = null;
+/**
+ * the template file to use
+ *
+ * @var string
+ */
+ var $template = null;
+/**
+ * Actions to use for scaffolding
+ *
+ * @var array
+ */
+ var $scaffoldActions = array('index', 'view', 'add', 'edit');
+/**
+ * Override initialize
+ *
+ * @return void
+ */
+ function initialize() {}
+/**
+ * Execution method always used for tasks
+ *
+ * @return void
+ */
function execute() {
if(empty($this->args)) {
$this->__interactive();
}
- }
+ if(isset($this->args[0]) == 1 && $this->args[0] == 'help') {
+ $this->help();
+ }
+
+ $controller = $action = $alias = null;
+ if(isset($this->args[0])) {
+ $this->controllerName = Inflector::camelize($this->args[0]);
+ $this->controllerPath = Inflector::underscore($this->controllerName);
+ }
+
+ if(isset($this->args[1])) {
+ $this->template = $this->args[1];
+ }
+
+ if(isset($this->args[2])) {
+ $action = $this->args[2];
+ }
+
+ if(!$action) {
+ $action = $this->template;
+ }
+
+ if(in_array($action, $this->scaffoldActions)) {
+ $this->bake($action, true);
+ } else if($action) {
+ $this->bake($action, true);
+ } else {
+ $vars = $this->__loadController();
+ if($vars) {
+ $protected = array( 'object', low($this->controllerName. 'Controller'), 'controller', 'appcontroller',
+ 'tostring', 'requestaction', 'log', 'cakeerror', 'constructclasses', 'redirect', 'set', 'setaction',
+ 'validate', 'validateerrors', 'render', 'referer', 'flash', 'flashout', 'generatefieldnames',
+ 'postconditions', 'cleanupfields', 'beforefilter', 'beforerender', 'afterfilter', 'disablecache', 'paginate');
+
+ $vars = get_class_vars($this->controllerName . 'Controller');
+ if(array_key_exists('scaffold', $vars)) {
+ $methods = $this->scaffoldActions;
+ } else {
+ $methods = get_class_methods($this->controllerName . 'Controller');
+ }
+ foreach($methods as $method) {
+ if($method{0} != '_' && !in_array(low($method), $protected)) {
+ $content = $this->getContent($method, $vars);
+ $this->bake($method, $content);
+ }
+ }
+ }
+ }
+ }
+/**
+ * Handles interactive baking
+ *
+ * @access private
+ * @return void
+ */
function __interactive() {
$this->hr();
$this->out('View Bake:');
$this->hr();
- $uses = array();
- $wannaUseSession = 'y';
+ $wannaDoAdmin = 'n';
$wannaDoScaffold = 'y';
- $controllerName = $this->__getControllerName();
- $controllerPath = low(Inflector::underscore($controllerName));
+ $this->controllerName = $this->Controller->getName();
- $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');
+ $this->controllerPath = low(Inflector::underscore($this->controllerName));
- if (low($doItInteractive) == 'y' || low($doItInteractive) == 'yes') {
- $this->interactive = true;
+ $interactive = $this->in("Would you like bake to build your views interactively?\nWarning: Choosing no will overwrite {$this->controllerName} views if it exist.", array('y','n'), 'y');
+
+ if (low($interactive) == 'y' || low($interactive) == 'yes') {
$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');
}
+ $admin = '';
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');
+ while ($admin == '') {
+ $admin = $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.');
+ if($this->Project->cakeAdmin($admin) !== true){
+ $this->err('Unable to write to /app/config/core.php.');
+ $this->err('You need to enable CAKE_ADMIN in /app/config/core.php to use admin routing.');
exit();
} else {
- $admin = $adminRoute . '_';
- $admin_url = '/'.$adminRoute;
+ $admin = $admin . '_';
}
}
}
if (low($wannaDoScaffold) == 'y' || low($wannaDoScaffold) == 'yes') {
- $file = CONTROLLERS . $controllerPath . '_controller.php';
+ $file = CONTROLLERS . $this->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);
+ if($admin) {
+ foreach($this->scaffoldActions as $action) {
+ $this->scaffoldActions[] = $admin . $action;
}
- $this->__bake($controllerName, $controllerPath, null, null);
-
- $this->hr();
- $this->out('');
- $this->out('View Scaffolding Complete.'."\n");
}
+ $vars = $this->__loadController();
+ if($vars) {
+ foreach($this->scaffoldActions as $action) {
+ $content = $this->getContent($action, $vars);
+ $this->bake($action, $content);
+ }
+ }
+ $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 == '') {
+ $action = '';
+ while ($action == '') {
+ $action = $this->in('Action Name? (use camelCased function name)');
+ if ($action == '') {
$this->out('The action name you supplied was empty. Please try again.');
}
}
@@ -125,430 +205,137 @@ class ViewTask extends BakeShell {
$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->out("Controller Name: {$this->controllerName}");
+ $this->out("Action Name: {$action}");
+ $this->out("Path: ".$this->params['app'] . DS . $this->controllerPath . DS . Inflector::underscore($action) . ".ctp");
$this->hr();
$looksGood = $this->in('Look okay?', array('y','n'), 'y');
-
if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
- $this->__bakeView($controllerName, $actionName);
+ $this->bake($action);
+ exit();
} else {
$this->out('Bake Aborted.');
+ exit();
}
}
}
-
- 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\tlink('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\tlink('Edit " . $singularHumanName . "', array('action'=>'edit', \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}'])); ?> \n";
- $viewView .= "\t\tlink('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\tlink('List " . $pluralHumanName ."', array('action'=>'index')); ?> \n";
- $viewView .= "\t\tlink('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\tlink('List " . $otherSingularHumanName . "', array('controller'=> '{$otherControllerPath}', 'action'=>'index')); ?> \n";
- $viewView .= "\t\tlink('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\tlink('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\tlink('View " . $otherPluralName . "', array('controller'=> '{$otherControllerPath}', 'action'=>'view')); ?> \n";
- $editView .= "\t\tlink('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\tlink('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\tlink('View " . $otherPluralName . "', array('controller'=> '{$otherControllerPath}', 'action'=>'view'));?> \n";
- $addView .= "\t\tlink('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.
+ * Loads Controller and sets variables for the template
+ * Available template variables
+ * 'modelClass', 'primaryKey', 'displayField', 'singularVar', 'pluralVar',
+ * 'singularHumanName', 'pluralHumanName', 'fields', 'foreignKeys',
+ * 'belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany'
*
* @access private
- * @param array $fields
+ * @return array Returns an variables to be made available to a view template
*/
- 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";
+ function __loadController() {
+ if(!$this->controllerName) {
+ $this->err('could not find the controller');
}
- return $displayFields;
+
+ if(!loadController($this->controllerName)) {
+ $file = CONTROLLERS . $this->controllerPath . '_controller.php';
+ $shortPath = $this->shortPath($file);
+ $this->err("The file '{$shortPath}' could not be found.\nIn order to bake a view, you'll need to first create the controller.");
+ return false;
+ }
+
+ $controllerClassName = $this->controllerName . 'Controller';
+ $controllerObj = & new $controllerClassName();
+ $controllerObj->constructClasses();
+ $modelClass = $controllerObj->modelClass;
+ $modelKey = $controllerObj->modelKey;
+ $modelObj =& ClassRegistry::getObject($modelKey);
+ $primaryKey = $modelObj->primaryKey;
+ $displayField = $modelObj->displayField;
+ $singularVar = Inflector::variable($modelClass);
+ $pluralVar = Inflector::variable($this->controllerName);
+ $singularHumanName = Inflector::humanize($modelClass);
+ $pluralHumanName = Inflector::humanize($this->controllerName);
+ $fields = $modelObj->_tableInfo->value;
+ $foreignKeys = $modelObj->keyToTable;
+ $belongsTo = $modelObj->belongsTo;
+ $hasOne = $modelObj->hasOne;
+ $hasMany = $modelObj->hasMany;
+ $hasAndBelongsToMany = $modelObj->hasAndBelongsToMany;
+
+ return compact('modelClass', 'primaryKey', 'displayField', 'singularVar', 'pluralVar',
+ 'singularHumanName', 'pluralHumanName', 'fields', 'foreignKeys',
+ 'belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
}
-
/**
- * outputs the a list of possible models or controllers from database
+ * Assembles and writes bakes the view file.
*
- * @param string $useDbConfig
- * @param string $type = Models or Controllers
- * @return output
+ * @param string $action
+ * @param string $content
+ * @return bool
*/
- 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));
- }
- }
+ function bake($action, $content = '') {
+ if($content === true) {
+ $content = $this->getContent();
+ }
+ $filename = VIEWS . $this->controllerPath . DS . Inflector::underscore($action) . '.ctp';
+ $Folder =& new Folder(VIEWS . $this->controllerPath, true);
+ $errors = $Folder->errors();
+ if(empty($errors)) {
+ $path = $Folder->slashTerm($Folder->pwd());
+ return $this->createFile($filename, $content);
} 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]);
+ foreach($errors as $error) {
+ $this->err($error);
+ }
}
+ return false;
}
-
/**
- * Forces the user to specify the controller for which he wants to bake views, and returns the selected controller name.
+ * Builds content from template and variables
*
- * @return the controller name
+ * @param string $template file to use
+ * @param array $vars passed for use in templates
+ * @return string content from template
*/
- function __getControllerName() {
- $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 = '';
+ function getContent($template = null, $vars = null) {
+ if(!$template) {
+ $template = $this->template;
+ }
+ $action = $template;
+ if(in_array($template, array('add', 'edit'))) {
+ $action = $template;
+ $template = 'form';
+ }
+ $loaded = false;
+ foreach($this->Dispatch->shellPaths as $path) {
+ $templatePath = $path . 'templates' . DS . 'views' . DS .Inflector::underscore($template).'.ctp';
+ if (file_exists($templatePath) && is_file($templatePath)) {
+ $loaded = true;
+ break;
}
}
-
- if (intval($enteredController) > 0 && intval($enteredController) <= count($this->_controllerNames) ) {
- $controllerName = $this->_controllerNames[intval($enteredController) - 1];
- } else {
- $controllerName = Inflector::camelize($enteredController);
+ if(!$vars) {
+ $vars = $this->__loadController();
}
-
- return $controllerName;
+ if($loaded) {
+ extract($vars);
+ ob_start();
+ ob_implicit_flush(0);
+ include($templatePath);
+ $content = ob_get_clean();
+ return $content;
+ }
+
+ $this->err('Template for '. $template .' could not be found');
+ return false;
+ }
+/**
+ * Builds content from template and variables
+ *
+ * @param string $template file to use
+ * @param array $params passed from controller
+ * @return void
+ */
+ function help() {
+ $this->out('Help');
+ exit();
}
}
\ No newline at end of file
diff --git a/cake/console/libs/templates/views/form.ctp b/cake/console/libs/templates/views/form.ctp
new file mode 100644
index 000000000..7a23a9fad
--- /dev/null
+++ b/cake/console/libs/templates/views/form.ctp
@@ -0,0 +1,72 @@
+
+ * 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.templates.views
+ * @since CakePHP(tm) v 0.10.0.1076
+ * @version $Revision$
+ * @modifiedby $LastChangedBy$
+ * @lastmodified $Date$
+ * @license http://www.opensource.org/licenses/mit-license.php The MIT License
+ */
+?>
+;
+create('{$modelClass}');?>\n";?>
+
+
+input('{$field['name']}');\n";
+ }
+ }
+ foreach($hasAndBelongsToMany as $assocName => $assocData) {
+ echo "\t\techo \$form->input('{$assocName}.{$assocName}');\n";
+ }
+ echo "\t?>\n";
+?>
+
+end('Submit');?>\n";
+?>
+
+
+
+
+ link('Delete', array('action'=>'delete', \$form->value('{$modelClass}.{$primaryKey}')), null, 'Are you sure you want to delete #' . \$form->value('{$modelClass}.{$primaryKey}')); ?>";?>
+
+ link('List {$pluralHumanName}', array('action'=>'index'));?>";?>
+ $value) {
+ $otherModelClass = $value['1'];
+ if($otherModelClass != $modelClass) {
+ $otherModelKey = Inflector::underscore($otherModelClass);
+ $otherControllerName = Inflector::pluralize($otherModelClass);
+ $otherControllerPath = Inflector::underscore($otherControllerName);
+ $otherSingularName = Inflector::variable($otherModelClass);
+ $otherPluralHumanName = Inflector::humanize($otherControllerPath);
+ $otherSingularHumanName = Inflector::humanize($otherModelKey);
+ echo "\t\tlink('List {$otherPluralHumanName}', array('controller'=> '{$otherControllerPath}', 'action'=>'index')); ?> \n";
+ echo "\t\tlink('New {$otherSingularHumanName}', array('controller'=> '{$otherControllerPath}', 'action'=>'add')); ?> \n";
+ }
+ }
+?>
+
+
\ No newline at end of file
diff --git a/cake/console/libs/templates/views/index.ctp b/cake/console/libs/templates/views/index.ctp
new file mode 100644
index 000000000..f6caf5d74
--- /dev/null
+++ b/cake/console/libs/templates/views/index.ctp
@@ -0,0 +1,99 @@
+
+ * 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.templates.views
+ * @since CakePHP(tm) v 0.10.0.1076
+ * @version $Revision$
+ * @modifiedby $LastChangedBy$
+ * @lastmodified $Date$
+ * @license http://www.opensource.org/licenses/mit-license.php The MIT License
+ */
+?>
+
+
List
+
+
+
+ sort('{$field['name']}');?>";?>
+
+ Actions
+
+\n";
+ echo "\t>\n";
+
+ foreach($fields as $field) {
+ if(in_array($field['name'], array_keys($foreignKeys))) {
+ $otherModelClass = $foreignKeys[$field['name']][1];
+ $otherModelKey = Inflector::underscore($otherModelClass);
+ $otherControllerName = Inflector::pluralize($otherModelClass);
+ $otherControllerPath = Inflector::underscore($otherControllerName);
+ $otherVariableName = Inflector::variable($otherModelClass);
+ $otherModelObj =& ClassRegistry::getObject($otherModelKey);
+ $otherPrimaryKey = $otherModelObj->primaryKey;
+ $otherDisplayField = $otherModelObj->displayField;
+ echo "\t\t\n\t\t\tlink(\${$singularVar}['{$otherModelClass}']['{$otherDisplayField}'], array('controller'=> '{$otherControllerPath}', 'action'=>'view', \${$singularVar}['{$otherModelClass}']['{$otherPrimaryKey}'])); ?>\n\t\t \n";
+ } else {
+ echo "\t\t\n\t\t\t\n\t\t \n";
+ }
+ }
+
+ echo "\t\t\n";
+ echo "\t\t\tlink('View', array('action'=>'view', \${$singularVar}['{$modelClass}']['{$primaryKey}'])); ?>\n";
+ echo "\t\t\tlink('Edit', array('action'=>'edit', \${$singularVar}['{$modelClass}']['{$primaryKey}'])); ?>\n";
+ echo "\t\t\tlink('Delete', array('action'=>'delete', \${$singularVar}['{$modelClass}']['{$primaryKey}']), null, 'Are you sure you want to delete #' . \${$singularVar}['{$modelClass}']['{$primaryKey}']); ?>\n";
+ echo "\t\t \n";
+ echo "\t \n";
+
+echo "\n";
+?>
+
+
+
+prev('<< previous', array(), null, array('class'=>'disabled'));?>\n";?>
+ | numbers();?>\n"?>
+next('next >>', array(), null, array('class'=>'disabled'));?>\n";?>
+
+
+
+ link('New {$singularHumanName}', array('action'=>'add')); ?>";?>
+ $value) {
+ $otherModelClass = $value['1'];
+ if($otherModelClass != $modelClass) {
+ $otherModelKey = Inflector::underscore($otherModelClass);
+ $otherControllerName = Inflector::pluralize($otherModelClass);
+ $otherControllerPath = Inflector::underscore($otherControllerName);
+ $otherVariableName = Inflector::variable($otherModelClass);
+ $otherPluralHumanName = Inflector::humanize($otherControllerPath);
+ $otherSingularHumanName = Inflector::humanize($otherModelKey);
+ echo "\t\tlink('List {$otherPluralHumanName}', array('controller'=> '{$otherControllerPath}', 'action'=>'index')); ?> \n";
+ echo "\t\tlink('New {$otherSingularHumanName}', array('controller'=> '{$otherControllerPath}', 'action'=>'add')); ?> \n";
+ }
+ }
+?>
+
+
\ No newline at end of file
diff --git a/cake/console/libs/templates/views/view.ctp b/cake/console/libs/templates/views/view.ctp
new file mode 100644
index 000000000..c250df9ec
--- /dev/null
+++ b/cake/console/libs/templates/views/view.ctp
@@ -0,0 +1,179 @@
+
+ * 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.templates.views
+ * @since CakePHP(tm) v 0.10.0.1076
+ * @version $Revision$
+ * @modifiedby $LastChangedBy$
+ * @lastmodified $Date$
+ * @license http://www.opensource.org/licenses/mit-license.php The MIT License
+ */
+?>
+
+
View
+
+primaryKey;
+ $otherDisplayField = $otherModelObj->displayField;
+ echo "\t\t".Inflector::humanize($otherModelClass)." \n";
+ echo "\t\t\n\t\t\tlink(\${$singularVar}['{$otherModelClass}']['{$otherDisplayField}'], array('controller'=> '{$otherControllerPath}', 'action'=>'view', \${$singularVar}['{$otherModelClass}']['{$otherPrimaryKey}'])); ?>\n\t\t\t \n\t\t \n";
+ } else {
+ echo "\t\t".Inflector::humanize($field['name'])." \n";
+ echo "\t\t\n\t\t\t\n\t\t\t \n\t\t \n";
+ }
+}
+?>
+
+
+
+
+link('Edit {$singularHumanName}', array('action'=>'edit', \${$singularVar}['{$modelClass}']['{$primaryKey}'])); ?> \n";
+ echo "\t\tlink('Delete {$singularHumanName}', array('action'=>'delete', \${$singularVar}['{$modelClass}']['{$primaryKey}']), null, 'Are you sure you want to delete #' . \${$singularVar}['{$modelClass}']['{$primaryKey}'] . '?'); ?> \n";
+ echo "\t\tlink('List {$pluralHumanName}', array('action'=>'index')); ?> \n";
+ echo "\t\tlink('New {$singularHumanName}', array('action'=>'add')); ?> \n";
+
+ foreach($foreignKeys as $field => $value) {
+ $otherModelClass = $value['1'];
+ if($otherModelClass != $modelClass) {
+ $otherModelKey = Inflector::underscore($otherModelClass);
+ $otherControllerName = Inflector::pluralize($otherModelClass);
+ $otherControllerPath = Inflector::underscore($otherControllerName);
+ $otherSingularVar = Inflector::variable($otherModelClass);
+ $otherPluralHumanName = Inflector::humanize($otherControllerPath);
+ $otherSingularHumanName = Inflector::humanize($otherModelKey);
+ echo "\t\tlink('List {$otherPluralHumanName}', array('controller'=> '{$otherControllerPath}', 'action'=>'index')); ?> \n";
+ echo "\t\tlink('New {$otherSingularHumanName}', array('controller'=> '{$otherControllerPath}', 'action'=>'add')); ?> \n";
+ }
+ }
+ ?>
+
+
+ $assocData):
+ $otherModelKey = Inflector::underscore($assocData['className']);
+ $otherControllerPath = Inflector::pluralize($otherModelKey);
+ $otherControllerName = Inflector::camelize($otherControllerPath);
+ $assocKey = Inflector::underscore($assocName);
+ $otherPluralHumanName = Inflector::humanize(Inflector::pluralize($assocKey));
+ $otherSingularHumanName = Inflector::humanize($assocKey);
+ $otherModelObj =& ClassRegistry::getObject($otherModelKey);
+ $otherFields = $otherModelObj->_tableInfo->value;
+ $otherPrimaryKey = $otherModelObj->primaryKey;
+
+ if($i++ % 2 == 0) {
+ $class = ' class="altrow"';
+ } else {
+ $class = null;
+ }
+?>
+
+ $assocData):
+ $otherModelKey = Inflector::underscore($assocData['className']);
+ $otherModelObj =& ClassRegistry::getObject($otherModelKey);
+ $otherControllerPath = Inflector::pluralize($otherModelKey);
+ $otherControllerName = Inflector::camelize($otherControllerPath);
+ $otherSingularVar = Inflector::variable($assocName);
+ $otherPluralHumanName = Inflector::humanize(Inflector::pluralize($assocName));
+ $otherSingularHumanName = Inflector::humanize($assocName);
+ $otherFields = $otherModelObj->_tableInfo->value;
+ $otherPrimaryKey = $otherModelObj->primaryKey;
+?>
+
+
\ No newline at end of file