\n";
- $viewView .= "singularize($this->lowCtrl)."['{$association}'])): ?>\n";
+ $viewView .= "
\n";
}
- $relations = array_merge($objModel->hasMany, $objModel->hasAndBelongsToMany);
+ $relations = array_merge($modelObj->hasMany, $modelObj->hasAndBelongsToMany);
- foreach($relations as $association => $relation) {
-
- $model = $relation['className'];
- $associationModel = new $model();
- $count = 0;
- $otherModelName = $inflect->singularize($model);
- $controller = $inflect->pluralize($model);
- $viewView .= "\n
\n
Related " . $inflect->humanize($inflect->pluralize($association)) . "
\n";
- $viewView .= "singularize($this->lowCtrl)."['{$association}'])):?>\n";
+ foreach($relations as $associationName => $relation) {
+ $otherModelName = $this->__modelName($relation['className']);
+ $otherControllerName = $this->__controllerName($otherModelName);
+ $otherControllerPath = $this->__controllerFn($otherModelName);
+ $otherSingularName = $this->__singularName($associationName);
+ $otherModelKey = Inflector::underscore($otherModelName);
+ $otherModelObj =& ClassRegistry::getObject($otherModelKey);
+
+
+ $viewView .= "
\n";
}
//-------------------------[ADD]-------------------------//
- $addView .= "
New " . $inflect->humanize($temp->modelKey) . "
\n";
- $fields .= "
\n";
$addView .= "
\n";
- $addView .= "- link('List " . $temp->viewPath . "', '/{$temp->viewPath}/index')?>
\n";
+ $addView .= "- link('List {$pluralHumanName}', '/{$this->controllerPath}/index')?>
\n";
$addView .= "
\n";
//-------------------------[EDIT]-------------------------//
- $editView .= "
Edit " . $inflect->humanize($temp->modelKey) . "
\n";
- $editView .= "
\n";
$editView .= "
\n";
- $editView .= "\t- link('List " . $temp->viewPath . "', '/{$temp->viewPath}/index')?>
\n";
+ $editView .= "- link('List {$pluralHumanName}', '/{$this->controllerPath}/index')?>
\n";
$editView .= "
\n";
//------------------------------------------------------------------------------------//
- if(!file_exists(VIEWS.strtolower($controllerName))) {
- mkdir(VIEWS.strtolower($controllerName));
+ if(!file_exists(VIEWS.$this->controllerPath)) {
+ mkdir(VIEWS.$this->controllerPath);
}
-
- $filename = VIEWS . strtolower($controllerName) . DS . 'index.thtml';
+
+ $filename = VIEWS . $this->controllerPath . DS . $admin . 'index.thtml';
$this->createFile($filename, $indexView);
- $filename = VIEWS . strtolower($controllerName) . DS . 'view.thtml';
+ $filename = VIEWS . $this->controllerPath . DS . $admin . 'view.thtml';
$this->createFile($filename, $viewView);
- $filename = VIEWS . strtolower($controllerName) . DS . 'add.thtml';
+ $filename = VIEWS . $this->controllerPath . DS . $admin . 'add.thtml';
$this->createFile($filename, $addView);
- $filename = VIEWS . strtolower($controllerName) . DS . 'edit.thtml';
+ $filename = VIEWS . $this->controllerPath . DS . $admin . 'edit.thtml';
$this->createFile($filename, $editView);
$this->hr();
$this->stdout('');
@@ -795,14 +878,14 @@ class Bake {
$this->hr();
$this->stdout('The following view will be created:');
$this->hr();
- $this->stdout("Controller Name: $controllerName");
+ $this->stdout("Controller Name: $this->controllerName");
$this->stdout("Action Name: $actionName");
- $this->stdout("Path: app/views/" . strtolower($controllerName) . DS . $inflect->underscore($actionName) . '.thtml');
+ $this->stdout("Path: app/views/" . $this->controllerPath . DS . Inflector::underscore($actionName) . '.thtml');
$this->hr();
$looksGood = $this->getInput('Look okay?', array('y','n'), 'y');
if (strtolower($looksGood) == 'y' || strtolower($looksGood) == 'yes') {
- $this->bakeView($controllerName, $actionName);
+ $this->bakeView($this->controllerName, $actionName);
} else {
$this->stdout('Bake Aborted.');
}
@@ -816,13 +899,13 @@ class Bake {
$this->hr();
$this->stdout('Controller Bake:');
$this->hr();
- $controllerName = '';
$uses = array();
$helpers = array();
$components = array();
$wannaUseSession = 'y';
$wannaDoScaffolding = 'y';
+ $controllerName = '';
while ($controllerName == '') {
$controllerName = $this->getInput('Controller name? Remember that Cake controller names are plural.');
@@ -830,14 +913,14 @@ class Bake {
$this->stdout('The controller name you supplied was empty. Please try again.');
}
}
-
- $inflect = & new Inflector();
- $controllerClassName = $inflect->camelize($controllerName);
+ $this->controllerPath = $this->__controllerFn($controllerName);
+ $this->controllerName = $this->__controllerName($controllerName);
+
$doItInteractive = $this->getInput("Would you like bake to build your controller interactively?\nWarning: Choosing no will overwrite {$controllerClassName} controller if it exist.", array('y','n'), 'y');
if (strtolower($doItInteractive) == 'y' || strtolower($doItInteractive) == 'yes') {
$this->interactive = true;
- $wannaDoUses = $this->getInput("Would you like this controller to use other models besides '" . $inflect->singularize($controllerClassName) . "'?", array('y','n'), 'n');
+ $wannaDoUses = $this->getInput("Would you like this controller to use other models besides '" . $this->__modelName($this->controllerName) . "'?", array('y','n'), 'n');
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.\nExample: 'Author, Article, Book'");
@@ -864,156 +947,185 @@ class Bake {
$wannaDoScaffolding = $this->getInput("Would you like to include some basic class methods (index(), add(), view(), edit())?", array('y','n'), 'n');
}
+
+ $admin = null;
+ if (strtolower($wannaDoScaffold) == 'y' || strtolower($wannaDoScaffold) == 'yes') {
+ $wannaDoAdmin = $this->getInput("Would you like to create the methods for admin routing?", array('y','n'), 'n');
+ }
+
+ if ((strtolower($wannaDoAdmin) == 'y' || strtolower($wannaDoAdmin) == 'yes')) {
+ if(defined('CAKE_ADMIN')) {
+ $admin = CAKE_ADMIN.'_';
+ } else {
+ $this->stdout('You need to enable CAKE_ADMIN in /app/config/core.php to use admin routing.');
+ }
+ }
if (strtolower($wannaDoScaffolding) == 'y' || strtolower($wannaDoScaffolding) == 'yes') {
- $controllerModel = $inflect->camelize($inflect->singularize($controllerClassName));
- $this->lowCtrl = $inflect->underscore($controllerName);
+
+ $currentModelName = $this->__modelName($this->controllerName);
+ $modelKey = Inflector::underscore($currentModelName);
+ $singularName = $this->__singularName($currentModelName);
+ $singularHumanName = $this->__singularHumanName($currentModelName);
+ $pluralHumanName = $this->__pluralHumanName($this->controllerName);
+
loadModels();
- if(!class_exists($controllerModel)) {
+ if(!class_exists($currentModelName)) {
$this->stdout('You must have a model for this class to build scaffold methods. Please try again.');
exit;
}
- $tempModel = new $controllerModel();
+ $modelObj = new $currentModelName();
$actions .= "\n";
- $actions .= "\tfunction index() {\n";
- $actions .= "\t\t\$this->{$controllerModel}->recursive = 0;\n";
- $actions .= "\t\t\$this->set('{$this->lowCtrl}', \$this->{$controllerModel}->findAll());\n";
+ $actions .= "\tfunction {$admin}index() {\n";
+ $actions .= "\t\t\$this->{$currentModelName}->recursive = 0;\n";
+ $actions .= "\t\t\$this->set('{$this->controllerPath}', \$this->{$currentModelName}->findAll());\n";
$actions .= "\t}\n";
$actions .= "\n";
- $actions .= "\tfunction add() {\n";
+ $actions .= "\tfunction {$admin}view(\$id = null) {\n";
+ $actions .= "\t\tif(!\$id) {\n";
+ $actions .= "\t\t\treturn false;\n";
+ $actions .= "\t\t}\n";
+ $actions .= "\t\t\$this->set('".$singularName."', \$this->{$currentModelName}->read(null, \$id));\n";
+ $actions .= "\t}\n";
+ $actions .= "\n";
+
+ $actions .= "\tfunction {$admin}add() {\n";
$actions .= "\t\tif(empty(\$this->data)) {\n";
- foreach($tempModel->hasAndBelongsToMany as $association => $relation) {
+ foreach($modelObj->hasAndBelongsToMany as $associationName => $relation) {
if(!empty($relation['className'])) {
- $model = $relation['className'];
- $lowerFirst = strtolower(substr($association, 0, 1));
- $lowerName = preg_replace('/\\w/', $lowerFirst, $association, 1);
- $actions .= "\t\t\t\$this->set('{$lowerName}Array', \$this->{$controllerModel}->{$model}->generateList());\n";
- $actions .= "\t\t\t\$this->set('selected{$association}', null);\n";
+ $otherModelName = $this->__modelName($relation['className']);
+ $otherSingularName = $this->__singularName($associationName);
+ $otherPluralName = $this->__pluralName($associationName);
+
+ $actions .= "\t\t\t\$this->set('{$otherPluralName}', \$this->{$currentModelName}->{$otherModelName}->generateList());\n";
+ $actions .= "\t\t\t\$this->set('selected_{$otherPluralName}', null);\n";
}
}
- foreach($tempModel->belongsTo as $association => $relation) {
+ foreach($modelObj->belongsTo as $associationName => $relation) {
if(!empty($relation['className'])) {
- $model = $relation['className'];
- $lowerFirst = strtolower(substr($model, 0, 1));
- $lowerName = preg_replace('/\\w/', $lowerFirst, $model, 1);
- $actions .= "\t\t\t\$this->set('{$lowerName}Array', \$this->{$controllerModel}->{$model}->generateList());\n";
+ $otherModelName = $this->__modelName($relation['className']);
+ $otherSingularName = $this->__singularName($associationName);
+ $otherPluralName = $this->__pluralName($associationName);
+
+ $actions .= "\t\t\t\$this->set('{$otherPluralName}', \$this->{$currentModelName}->{$otherModelName}->generateList());\n";
}
}
$actions .= "\t\t\t\$this->render();\n";
$actions .= "\t\t} else {\n";
$actions .= "\t\t\t\$this->cleanUpFields();\n";
- $actions .= "\t\t\tif(\$this->{$controllerModel}->save(\$this->data)) {\n";
+ $actions .= "\t\t\tif(\$this->{$currentModelName}->save(\$this->data)) {\n";
if (strtolower($wannaUseSession) == 'y' || strtolower($wannaUseSession) == 'yes') {
- $actions .= "\t\t\t\t\$this->Session->setFlash('The ".$inflect->humanize($controllerModel)." has been saved');\n";
- $actions .= "\t\t\t\t\$this->redirect('/{$this->lowCtrl}/index');\n";
+ $actions .= "\t\t\t\t\$this->Session->setFlash('The ".Inflector::humanize($currentModelName)." has been saved');\n";
+ $actions .= "\t\t\t\t\$this->redirect('/{$this->controllerPath}/index');\n";
} else {
- $actions .= "\t\t\t\t\$this->flash('{$controllerModel} saved.', '/{$this->lowCtrl}/index');\n";
+ $actions .= "\t\t\t\t\$this->flash('{$currentModelName} saved.', '/{$this->controllerPath}/index');\n";
}
$actions .= "\t\t\t} else {\n";
if (strtolower($wannaUseSession) == 'y' || strtolower($wannaUseSession) == 'yes') {
$actions .= "\t\t\t\t\$this->Session->setFlash('Please correct errors below.');\n";
}
- foreach($tempModel->hasAndBelongsToMany as $association => $relation) {
+ foreach($modelObj->hasAndBelongsToMany as $associationName => $relation) {
if(!empty($relation['className'])) {
- $model = $relation['className'];
- $associationModel = new $model();
- $lowerFirst = strtolower(substr($association, 0, 1));
- $lowerName = preg_replace('/\\w/', $lowerFirst, $association, 1);
- $actions .= "\t\t\t\t\$this->set('{$lowerName}Array', \$this->{$controllerModel}->{$model}->generateList());\n";
- $actions .= "\t\t\t\tif(empty(\$this->data['{$association}']['{$association}'])) { \$this->data['{$association}']['{$association}'] = null; }\n";
- $actions .= "\t\t\t\t\$this->set('selected{$association}', \$this->data['{$association}']['{$association}']);\n";
+ $otherModelName = $this->__modelName($relation['className']);
+ $otherSingularName = $this->__singularName($associationName);
+ $otherPluralName = $this->__pluralName($associationName);
+
+ $actions .= "\t\t\t\t\$this->set('{$otherPluralName}', \$this->{$currentModelName}->{$otherModelName}->generateList());\n";
+ $actions .= "\t\t\t\t\$this->set('selected_{$otherPluralName}', \$this->data['{$associationName}']['{$associationName}']);\n";
}
}
- foreach($tempModel->belongsTo as $association => $relation) {
+ foreach($modelObj->belongsTo as $associationName => $relation) {
if(!empty($relation['className'])) {
- $model = $relation['className'];
- $lowerFirst = strtolower(substr($model, 0, 1));
- $lowerName = preg_replace('/\\w/', $lowerFirst, $model, 1);
- $actions .= "\t\t\t\t\$this->set('{$lowerName}Array', \$this->{$controllerModel}->{$model}->generateList());\n";
+ $otherModelName = $this->__modelName($relation['className']);
+ $otherSingularName = $this->__singularName($associationName);
+ $otherPluralName = $this->__pluralName($associationName);
+
+ $actions .= "\t\t\t\t\$this->set('{$otherPluralName}', \$this->{$currentModelName}->{$otherModelName}->generateList());\n";
}
}
$actions .= "\t\t\t}\n";
$actions .= "\t\t}\n";
$actions .= "\t}\n";
$actions .= "\n";
- $actions .= "\tfunction edit(\$id) {\n";
+ $actions .= "\tfunction {$admin}edit(\$id) {\n";
$actions .= "\t\tif(empty(\$this->data)) {\n";
- $actions .= "\t\t\t\$this->data = \$this->{$controllerModel}->read(null, \$id);\n";
+ $actions .= "\t\t\t\$this->data = \$this->{$currentModelName}->read(null, \$id);\n";
- foreach($tempModel->hasAndBelongsToMany as $association => $relation) {
+ foreach($modelObj->hasAndBelongsToMany as $associationName => $relation) {
if(!empty($relation['className'])) {
- $model = $relation['className'];
- $associationModel = new $model();
- $lowerFirst = strtolower(substr($association, 0, 1));
- $lowerName = preg_replace('/\\w/', $lowerFirst, $association, 1);
- $actions .= "\t\t\t\$this->set('{$lowerName}Array', \$this->{$controllerModel}->{$model}->generateList());\n";
- $actions .= "\t\t\t\$this->set('selected{$association}', \$this->_selectedArray(\$this->data['{$association}'], '{$associationModel->primaryKey}'));\n";
+ $otherModelName = $this->__modelName($relation['className']);
+ $otherSingularName = $this->__singularName($associationName);
+ $otherPluralName = $this->__pluralName($associationName);
+
+ $actions .= "\t\t\t\$this->set('{$otherPluralName}', \$this->{$currentModelName}->{$otherModelName}->generateList());\n";
+ $actions .= "\t\t\t\$this->set('selected_{$otherPluralName}', \$this->data['{$associationName}']['{$associationName}']);\n";
}
}
- foreach($tempModel->belongsTo as $association => $relation) {
+ foreach($modelObj->belongsTo as $association => $relation) {
if(!empty($relation['className'])) {
- $model = $relation['className'];
- $lowerFirst = strtolower(substr($model, 0, 1));
- $lowerName = preg_replace('/\\w/', $lowerFirst, $model, 1);
- $actions .= "\t\t\t\$this->set('{$lowerName}Array', \$this->{$controllerModel}->{$model}->generateList());\n";
+ $otherModelName = $this->__modelName($relation['className']);
+ $otherSingularName = $this->__singularName($associationName);
+ $otherPluralName = $this->__pluralName($associationName);
+
+ $actions .= "\t\t\t\$this->set('{$otherPluralName}', \$this->{$currentModelName}->{$otherModelName}->generateList());\n";
}
}
$actions .= "\t\t} else {\n";
$actions .= "\t\t\t\$this->cleanUpFields();\n";
- $actions .= "\t\t\tif(\$this->{$controllerModel}->save(\$this->data)) {\n";
+ $actions .= "\t\t\tif(\$this->{$currentModelName}->save(\$this->data)) {\n";
if (strtolower($wannaUseSession) == 'y' || strtolower($wannaUseSession) == 'yes') {
- $actions .= "\t\t\t\t\$this->Session->setFlash('The ".$inflect->humanize($controllerModel)." has been saved');\n";
- $actions .= "\t\t\t\t\$this->redirect('/{$this->lowCtrl}/index');\n";
+ $actions .= "\t\t\t\t\$this->Session->setFlash('The ".Inflector::humanize($currentModelName)." has been saved');\n";
+ $actions .= "\t\t\t\t\$this->redirect('/{$this->controllerName}/index');\n";
} else {
- $actions .= "\t\t\t\t\$this->flash('{$controllerModel} saved.', '/{$this->lowCtrl}/index');\n";
+ $actions .= "\t\t\t\t\$this->flash('{$currentModelName} saved.', '/{$this->controllerName}/index');\n";
}
$actions .= "\t\t\t} else {\n";
if (strtolower($wannaUseSession) == 'y' || strtolower($wannaUseSession) == 'yes') {
$actions .= "\t\t\t\t\$this->Session->setFlash('Please correct errors below.');\n";
}
- foreach($tempModel->hasAndBelongsToMany as $association => $relation) {
+ foreach($modelObj->hasAndBelongsToMany as $associationName => $relation) {
if(!empty($relation['className'])) {
- $model = $relation['className'];
- $associationModel = new $model();
- $lowerFirst = strtolower(substr($association, 0, 1));
- $lowerName = preg_replace('/\\w/', $lowerFirst, $association, 1);
- $actions .= "\t\t\t\t\$this->set('{$lowerName}Array', \$this->{$controllerModel}->{$model}->generateList());\n";
- $actions .= "\t\t\t\tif(empty(\$this->data['{$association}']['{$association}'])) { \$this->data['{$association}']['{$association}'] = null; }\n";
- $actions .= "\t\t\t\t\$this->set('selected{$association}', \$this->data['{$association}']['{$association}']);\n";
+ $otherModelName = $this->__modelName($relation['className']);
+ $otherSingularName = $this->__singularName($associationName);
+ $otherPluralName = $this->__pluralName($associationName);
+
+ $actions .= "\t\t\t\t\$this->set('{$otherPluralName}', \$this->{$currentModelName}->{$otherModelName}->generateList());\n";
+ $actions .= "\t\t\t\t\if(empty(\$this->data['{$associationName}']['{$association}'])) { \$this->data['{$associationName}']['{$associationName}'] = null; }\n";
+ $actions .= "\t\t\t\t\$this->set('selected_{$otherPluralName}', \$this->data['{$associationName}']['{$associationName}']);\n";
}
}
- foreach($tempModel->belongsTo as $association => $relation) {
+ foreach($modelObj->belongsTo as $association => $relation) {
if(!empty($relation['className'])) {
- $model = $relation['className'];
- $lowerFirst = strtolower(substr($model, 0, 1));
- $lowerName = preg_replace('/\\w/', $lowerFirst, $model, 1);
- $actions .= "\t\t\t\t\$this->set('{$lowerName}Array', \$this->{$controllerModel}->{$model}->generateList());\n";
+ $otherModelName = $this->__modelName($relation['className']);
+ $otherSingularName = $this->__singularName($associationName);
+ $otherPluralName = $this->__pluralName($associationName);
+
+ $actions .= "\t\t\t\t\$this->set('{$otherPluralName}', \$this->{$currentModelName}->{$otherModelName}->generateList());\n";
}
}
$actions .= "\t\t\t}\n";
$actions .= "\t\t}\n";
$actions .= "\t}\n";
$actions .= "\n";
- $actions .= "\tfunction view(\$id) {\n";
- $actions .= "\t\t\$this->set('".$inflect->singularize($this->lowCtrl)."', \$this->{$controllerModel}->read(null, \$id));\n";
- $actions .= "\t}\n";
- $actions .= "\n";
- $actions .= "\tfunction delete(\$id) {\n";
- $actions .= "\t\tif(\$this->{$controllerModel}->del(\$id)) {\n";
+ $actions .= "\tfunction delete(\$id = null) {\n";
+ $actions .= "\t\tif(!\$id) {\n";
+ $actions .= "\t\t\treturn false;\n";
+ $actions .= "\t\t}\n";
+ $actions .= "\t\tif(\$this->{$currentModelName}->del(\$id)) {\n";
if (strtolower($wannaUseSession) == 'y' || strtolower($wannaUseSession) == 'yes') {
- $actions .= "\t\t\t\$this->Session->setFlash('The ".$inflect->humanize($controllerModel)." deleted: id '.\$id.'');\n";
- $actions .= "\t\t\t\$this->redirect('/{$this->lowCtrl}/index');\n";
+ $actions .= "\t\t\t\$this->Session->setFlash('The ".$this->__singularHumanName($currentModelName)." deleted: id '.\$id.'');\n";
+ $actions .= "\t\t\t\$this->redirect('/{$this->controllerName}/index');\n";
} else {
- $actions .= "\t\t\t\$this->flash('{$controllerModel} deleted: id '.\$id.'.', '/{$this->lowCtrl}/index');\n";
+ $actions .= "\t\t\t\$this->flash('{$currentModelName} deleted: id '.\$id.'.', '/{$this->controllerPath}/index');\n";
}
$actions .= "\t\t}\n";
$actions .= "\t}\n";
$actions .= "\n";
- if(!empty($tempModel->hasAndBelongsToMany)) {
+ if(!empty($modelObj->hasAndBelongsToMany)) {
$actions .= "\tfunction _selectedArray(\$data, \$key) {\n";
$actions .= "\t\t\$array = array();\n";
$actions .= "\t\tif(!empty(\$data)) {\n";
@@ -1073,16 +1185,16 @@ class Bake {
$looksGood = $this->getInput('Look okay?', array('y','n'), 'y');
if (strtolower($looksGood) == 'y' || strtolower($looksGood) == 'yes') {
- $this->bakeController($controllerClassName, $uses, $helpers, $components, $actions);
+ $this->bakeController($controllerName, $uses, $helpers, $components, $actions);
if ($this->doUnitTest()) {
- $this->bakeUnitTest('controller', $controllerClassName);
+ $this->bakeUnitTest('controller', $controllerName);
}
} else {
$this->stdout('Bake Aborted.');
}
} else {
- $this->bakeController($controllerClassName, $uses, $helpers, $components, $actions);
+ $this->bakeController($controllerName, $uses, $helpers, $components, $actions);
exit();
}
}
@@ -1111,17 +1223,18 @@ class Bake {
* @param string $password
* @param string $database
*/
- function bakeDbConfig($host, $login, $password, $database) {
+ function bakeDbConfig( $driver, $connect, $host, $login, $password, $database, $prefix) {
$out = " 'mysql',\n";
- $out .= "\t\t'connect' => 'mysql_connect',\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\t'driver' => '{$driver}',\n";
+ $out .= "\t\t'connect' => '{$connect}',\n";
+ $out .= "\t\t'host' => '{$host}',\n";
+ $out .= "\t\t'login' => '{$login}',\n";
+ $out .= "\t\t'password' => '{$password}',\n";
+ $out .= "\t\t'database' => '{$database}' \n";
+ $out .= "\t\t'prefix' => '{$prefix}' \n";
$out .= "\t);\n";
$out .= "}\n";
$out .= "?>";
@@ -1237,8 +1350,7 @@ class Bake {
}
$out .= "}\n";
$out .= "?>";
- $inflect = & new Inflector();
- $filename = MODELS.$inflect->underscore($modelClassName) . '.php';
+ $filename = MODELS.Inflector::underscore($modelClassName) . '.php';
$this->createFile($filename, $out);
}
/**
@@ -1251,12 +1363,10 @@ class Bake {
function bakeView($controllerName, $actionName, $content = '') {
$out = "
$actionName
\n";
$out .= $content;
- $inflect = new Inflector();
-
- if(!file_exists(VIEWS.strtolower($controllerName))) {
- mkdir(VIEWS.strtolower($controllerName));
+ if(!file_exists(VIEWS.$this->__controllerFn($controllerName))) {
+ mkdir(VIEWS.$this->__controllerFn($controllerName));
}
- $filename = VIEWS . strtolower($controllerName) . DS . $inflect->underscore($actionName) . '.thtml';
+ $filename = VIEWS . $this->__controllerFn($controllerName) . DS . Inflector::underscore($actionName) . '.thtml';
$this->createFile($filename, $out);
}
/**
@@ -1269,7 +1379,6 @@ class Bake {
* @param string $actions
*/
function bakeController($controllerName, $uses, $helpers, $components, $actions = '') {
- $inflect = & new Inflector();
$out = "singularize($controllerName) . "', ";
+ $out .= "\tvar \$uses = array('" . $this->__modelName($controllerName) . "', ";
foreach($uses as $use) {
if ($use != $uses[count($uses) - 1]) {
- $out .= "'" . ucfirst($use) . "', ";
+ $out .= "'" . $this->__modelName($use) . "', ";
} else {
- $out .= "'" . ucfirst($use) . "'";
+ $out .= "'" . $this->__modelName($use) . "'";
}
}
$out .= ");\n";
@@ -1293,9 +1402,9 @@ class Bake {
if (count($helpers)) {
foreach($helpers as $help) {
if ($help != $helpers[count($helpers) - 1]) {
- $out .= ", '" . ucfirst($help) . "'";
+ $out .= ", '" . Inflector::camelize($help) . "'";
} else {
- $out .= ", '" . ucfirst($help) . "'";
+ $out .= ", '" . Inflector::camelize($help) . "'";
}
}
}
@@ -1306,9 +1415,9 @@ class Bake {
foreach($components as $comp) {
if ($comp != $components[count($components) - 1]) {
- $out .= "'" . ucfirst($comp) . "', ";
+ $out .= "'" . Inflector::camelize($comp) . "', ";
} else {
- $out .= "'" . ucfirst($comp) . "'";
+ $out .= "'" . Inflector::camelize($comp) . "'";
}
}
$out .= ");\n";
@@ -1317,7 +1426,7 @@ class Bake {
$out .= $actions;
$out .= "}\n";
$out .= "?>";
- $filename = CONTROLLERS . $inflect->underscore($controllerName) . '_controller.php';
+ $filename = CONTROLLERS . $this->__controllerFn($controllerName) . '_controller.php';
$this->createFile($filename, $out);
}
/**
@@ -1327,7 +1436,6 @@ class Bake {
* @param string $className
*/
function bakeUnitTest($type, $className) {
- $inflect = & new Inflector();
$out = 'assertEquals(\$result, \$expected);\n\t}\n\t*/\n}";
$path = MODEL_TESTS;
- $filename = $inflect->underscore($className).'.test.php';
+ $filename = $this->__singularName($className).'.test.php';
break;
case 'controller':
$out .= "class {$className}ControllerTestCase extends UnitTestCase\n{\n";
@@ -1353,7 +1461,7 @@ class Bake {
$out .= "\t\t\$expected = 1;\n";
$out .= "\t\t\$this->assertEquals(\$result, \$expected);\n\t}\n\t*/\n}";
$path = CONTROLLER_TESTS;
- $filename = $inflect->underscore($className.'Controller').'.test.php';
+ $filename = $this->__pluralName($className.'Controller').'.test.php';
break;
default:
$error = true;
@@ -1365,16 +1473,16 @@ class Bake {
$this->stdout("Baking unit test for $className...");
$path = explode(DS, $path);
foreach($path as $i => $val) {
- if ($val == '') {
+ if ($val == '' || $val == '../') {
unset($path[$i]);
}
}
$path = implode(DS, $path);
+ echo $path;
$unixPath = DS;
if (strpos(PHP_OS, 'WIN') === 0){
$unixPath = null;
}
-
if (!is_dir($unixPath.$path)) {
$create = $this->getInput("Unit test directory does not exist. Create it?", array('y','n'), 'y');
if (low($create) == 'y' || low($create) == 'yes') {
@@ -1457,10 +1565,10 @@ class Bake {
* @return Success
*/
function createFile ($path, $contents) {
- $shortPath = str_replace(ROOT, null, $path);
- $shortPath = str_replace('../', '', $shortPath);
- $shortPath = str_replace('//', '/', $shortPath);
-
+ //$shortPath = str_replace(ROOT, null, $path);
+ //$shortPath = str_replace('../', '', $shortPath);
+ //$shortPath = str_replace('//', '/', $shortPath);
+ $shortPath = $path;
echo "\nCreating file $shortPath\n";
$path = str_replace('//', '/', $path);
if (is_file($path) && $this->interactive === true) {
@@ -1499,7 +1607,7 @@ class Bake {
*/
function generateFields( $fields, $readOnly = false ) {
$strFormFields = '';
- foreach( $fields as $field ) {
+ foreach( $fields as $field) {
if(isset( $field['type'])) {
if(!isset($field['required'])) {
$field['required'] = false;
@@ -1548,6 +1656,17 @@ class Bake {
if(!isset( $field['options'])) {
$field['options'] = null;
}
+ if(isset($field['foreignKey'])) {
+ $tagNameArray = explode('/', $field['tagName']);
+ $modelKey = Inflector::underscore($tagNameArray['0']);
+ $myModelObj =& ClassRegistry::getObject($modelKey);
+ $aliases = array_flip($myModelObj->alias);
+ if(isset($aliases[$field['table']])) {
+ $this->__modelAlias = $this->__modelName($aliases[$field['table']]);
+ } else {
+ $this->__modelAlias = $this->__modelNameFromKey($field['foreignKey']);
+ }
+ }
$strFormFields = $strFormFields.$this->generateSelectDiv( $field['tagName'], $field['prompt'], $field['options'], $field['selected'], $field['selectAttr'], $field['optionsAttr'], $field['required'], $field['errorMsg'] );
break;
case "area";
@@ -1610,7 +1729,6 @@ class Bake {
$htmlAttributes = $htmlOptions;
$htmlAttributes['cols'] = $cols;
$htmlAttributes['rows'] = $rows;
- $tagNameArray = explode('/', $tagName);
$str = "\ttextarea('{$tagName}', " . $this->attributesToArray($htmlAttributes) . ");?>\n";
$str .= "\ttagErrorMsg('{$tagName}', 'Please enter the {$prompt}.');?>\n";
$strLabel = "\n\tlabel( '{$tagName}', '{$prompt}' );?>\n";
@@ -1620,13 +1738,6 @@ class Bake {
$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 );
}
@@ -1642,7 +1753,7 @@ class Bake {
*/
function generateCheckboxDiv($tagName, $prompt, $required=false, $errorMsg=null, $htmlOptions=null ) {
$htmlOptions['class'] = "inputCheckbox";
- $tagNameArray = explode('/', $tagName);
+
$str = "\n\tcheckbox('{$tagName}', null, " . $this->attributesToArray($htmlAttributes) . ");?>\n";
$str .= "\ttagErrorMsg('{$tagName}', 'Please check the {$prompt}.');?>\n";
$strLabel = "\tlabel('{$tagName}', '{$prompt}');?>\n";
@@ -1652,12 +1763,6 @@ class Bake {
$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, $str, $strLabel );
return $this->divTag( $divClass, $divTagInside );
}
@@ -1674,7 +1779,6 @@ class Bake {
* @return Generated HTML and PHP.
*/
function generateDate($tagName, $prompt, $required=false, $errorMsg=null, $size=20, $htmlOptions=null, $selected=null ) {
- $tagNameArray = explode('/', $tagName);
$str = "\tdateTimeOptionTag('{$tagName}', 'MDY' , 'NONE', \$html->tagValue('{$tagName}'), " . $this->attributesToArray($htmlOptions) . ");?>\n";
$str .= "\ttagErrorMsg('{$tagName}', 'Please select the {$prompt}.');?>\n";
$strLabel = "\n\tlabel('{$tagName}', '{$prompt}');?>\n";
@@ -1684,12 +1788,6 @@ class Bake {
$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 );
}
@@ -1713,11 +1811,6 @@ class Bake {
$divClass = "required";
}
$strError = "";
-
- if ($this->isFieldError($tagName)) {
- $strError = $this->pTag('error', $errorMsg);
- $divClass = sprintf("%s error", $divClass);
- }
$divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str);
return $this->divTag($divClass, $divTagInside);
}
@@ -1734,7 +1827,6 @@ class Bake {
* @return Generated HTML and PHP.
*/
function generateDateTime($tagName, $prompt, $required=false, $errorMsg=null, $size=20, $htmlOptions=null, $selected = null ) {
- $tagNameArray = explode('/', $tagName);
$str = "\tdateTimeOptionTag('{$tagName}', 'MDY' , '12', \$html->tagValue('{$tagName}'), " . $this->attributesToArray($htmlOptions) . ");?>\n";
$str .= "\ttagErrorMsg('{$tagName}', 'Please select the {$prompt}.');?>\n";
$strLabel = "\n\tlabel('{$tagName}', '{$prompt}');?>\n";
@@ -1744,12 +1836,6 @@ class Bake {
$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 );
}
@@ -1767,7 +1853,6 @@ class Bake {
function generateInputDiv($tagName, $prompt, $required=false, $errorMsg=null, $size=20, $htmlOptions=null ) {
$htmlAttributes = $htmlOptions;
$htmlAttributes['size'] = $size;
- $tagNameArray = explode('/', $tagName);
$str = "\tinput('{$tagName}', " . $this->attributesToArray($htmlAttributes) . ");?>\n";
$str .= "\ttagErrorMsg('{$tagName}', 'Please enter the {$prompt}.');?>\n";
$strLabel = "\n\tlabel('{$tagName}', '{$prompt}');?>\n";
@@ -1777,15 +1862,10 @@ class Bake {
$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 );
}
+
/**
* Generates PHP code for a View file that makes a SELECT box, wrapped in a DIV.
*
@@ -1800,21 +1880,14 @@ class Bake {
* @return Generated HTML and PHP.
*/
function generateSelectDiv($tagName, $prompt, $options, $selected=null, $selectAttr=null, $optionAttr=null, $required=false, $errorMsg=null) {
- $tagNameArray = explode('/', $tagName);
- $inflect = & new Inflector();
- $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";
- $lowerFirst = strtolower(substr($tagNameArray[0], 0, 1));
- $lowerName = preg_replace('/\\w/', $lowerFirst, $tagNameArray[0], 1);
-
+
+ $pluralName = $this->__controllerFn($this->__modelAlias);
+
if($selectAttr['multiple'] != 'multiple') {
- $str = "\tselectTag('{$tagName}', " . "\${$model}Array, \$html->tagValue('{$tagName}'), " . $this->attributesToArray($selectAttr) . ");?>\n";
+ $str = "\tselectTag('{$tagName}', " . "\${$pluralName}, \$html->tagValue('{$tagName}'), " . $this->attributesToArray($selectAttr) . ");?>\n";
$str .= "\ttagErrorMsg('{$tagName}', 'Please select the {$prompt}.') ?>\n";
} else {
- $str = "\tselectTag('{$tagName}', \${$lowerName}Array, \$selected{$tagNameArray[0]}, array('multiple' => 'multiple', 'class' => 'selectMultiple'));?>\n";
+ $str = "\tselectTag('{$tagName}', \${$pluralName}, \$selected_{$controller}, array('multiple' => 'multiple', 'class' => 'selectMultiple'));?>\n";
$str .= "\ttagErrorMsg('{$tagName}', 'Please select the {$prompt}.');?>\n";
}
$strLabel = "\n\tlabel('{$tagName}', '{$prompt}');?>\n";
@@ -1824,12 +1897,6 @@ class Bake {
$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 );
}
@@ -1845,32 +1912,6 @@ class Bake {
$divTagInside = sprintf( "%s", $str );
return $this->divTag( 'submit', $divTagInside);
}
-/**
- * Returns HTML for a LABEL form element.
- *
- * @param string $tagName
- * @param string $text
- * @return Generated HTML.
- */
- function labelTag( $tagName, $text ) {
- return sprintf( TAG_LABEL, strtolower(str_replace('/', '_',$tagName)), $text ) . "\n";
- }
-
-/**
- * Tests given field for validity, and returns true if there are errors.
- *
- * @param string $field
- * @return Success.
- */
- 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;
- }
- }
/**
* Returns the text wrapped in an HTML P tag, followed by a newline.
*
@@ -2003,12 +2044,12 @@ class Bake {
*/
function __buildDirLayout($projectPath, $appName) {
$skel = '';
- if($this->__checkPath(ROOT.'cake'.DS.'scripts'.DS.'templates'.DS.'skel') === true) {
- $skel = ROOT.'cake'.DS.'scripts'.DS.'templates'.DS.'skel';
+ if($this->__checkPath(CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'scripts'.DS.'templates'.DS.'skel') === true) {
+ $skel = CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'scripts'.DS.'templates'.DS.'skel';
} else {
while ($skel == '') {
- $skel = $this->getInput("What is the full path for the cake install app directory?\nExample: ", null, ROOT.'app'.DS);
+ $skel = $this->getInput("What is the full path for the cake install app directory?\nExample: ", null, ROOT.'myapp'.DS);
if ($skel == '') {
$this->stdout('The directory path you supplied was empty. Please try again.');
@@ -2149,8 +2190,91 @@ class Bake {
*/
function __defaultHome($dir, $app) {
$path = $dir.DS.'views'.DS.'pages'.DS;
- include(ROOT.'cake'.DS.'scripts'.DS.'templates'.DS.'views'.DS.'home.thtml');
+ include(CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'scripts'.DS.'templates'.DS.'views'.DS.'home.thtml');
$this->createFile($path.'home.thtml', $output);
}
+/**
+ * creates the proper pluralize controller for the file path
+ *
+ * @param string $name
+ * @return string $name
+ */
+ function __controllerFn($name) {
+ return low(Inflector::tableize($name));
+ }
+/**
+ * creates the proper pluralize controller class name.
+ *
+ * @param string $name
+ * @return string $name
+ */
+ function __controllerName($name) {
+ return Inflector::pluralize(Inflector::camelize($name));
+ }
+/**
+ * creates the proper singular model name.
+ *
+ * @param string $name
+ * @return string $name
+ */
+ function __modelName($name) {
+ return Inflector::camelize(Inflector::singularize($name));
+ }
+/**
+ * creates the proper singular model key for associations.
+ *
+ * @param string $name
+ * @return string $name
+ */
+ function __modelKey($name) {
+ return Inflector::underscore(Inflector::singularize($name)).'_id';
+ }
+/**
+ * creates the proper model name from a foreign key.
+ *
+ * @param string $key
+ * @return string $name
+ */
+ function __modelNameFromKey($key) {
+ $name = str_replace('_id', '',$key);
+ return $this->__modelName($name);
+ }
+/**
+ * creates the singular name for use in views.
+ *
+ * @param string $name
+ * @return string $name
+ */
+ function __singularName($name) {
+ return low(Inflector::underscore(Inflector::singularize($name)));
+ }
+/**
+ * creates the plural name for views.
+ *
+ * @param string $name
+ * @return string $name
+ */
+ function __pluralName($name) {
+ return low(Inflector::underscore(Inflector::pluralize($name)));
+ }
+/**
+ * creates the singular human name used in views
+ *
+ * @param string $name
+ * @return string $name
+ */
+ function __singularHumanName($name) {
+ return Inflector::humanize(Inflector::underscore(Inflector::singularize($name)));
+ }
+/**
+ * creates the plural humna name used in views
+ *
+ * @param string $name
+ * @return string $name
+ */
+ function __pluralHumanName($name) {
+ return Inflector::humanize(Inflector::underscore(Inflector::pluralize($name)));
+ }
+
}
?>