diff --git a/cake/console/libs/tasks/controller.php b/cake/console/libs/tasks/controller.php index 873dc640d..2b538b83b 100644 --- a/cake/console/libs/tasks/controller.php +++ b/cake/console/libs/tasks/controller.php @@ -277,7 +277,7 @@ class ControllerTask extends Shell { $this->err(__('You must have a model for this class to build basic methods. Please try again.', true)); $this->_stop(); } - $actions = null; + $modelObj =& ClassRegistry::init($currentModelName); $controllerPath = $this->_controllerPath($controllerName); $pluralName = $this->_pluralName($currentModelName); @@ -285,136 +285,10 @@ class ControllerTask extends Shell { $singularHumanName = Inflector::humanize($currentModelName); $pluralHumanName = Inflector::humanize($controllerName); - $actions .= "\n"; - $actions .= "\tfunction {$admin}index() {\n"; - $actions .= "\t\t\$this->{$currentModelName}->recursive = 0;\n"; - $actions .= "\t\t\$this->set('{$pluralName}', \$this->paginate());\n"; - $actions .= "\t}\n"; - $actions .= "\n"; - $actions .= "\tfunction {$admin}view(\$id = null) {\n"; - $actions .= "\t\tif (!\$id) {\n"; - if ($wannaUseSession) { - $actions .= "\t\t\t\$this->Session->setFlash(__('Invalid {$singularHumanName}.', true));\n"; - $actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n"; - } else { - $actions .= "\t\t\t\$this->flash(__('Invalid {$singularHumanName}', true), array('action'=>'index'));\n"; - } - $actions .= "\t\t}\n"; - $actions .= "\t\t\$this->set('".$singularName."', \$this->{$currentModelName}->read(null, \$id));\n"; - $actions .= "\t}\n"; - $actions .= "\n"; + $this->Template->set(compact('admin', 'controllerPath', 'pluralName', 'singularName', 'singularHumanName', + 'pluralHumanName', 'modelObj', 'wannaUseSession', 'currentModelName')); + $actions = $this->Template->generate('objects', 'controller_actions'); - /* ADD ACTION */ - $compact = array(); - $actions .= "\tfunction {$admin}add() {\n"; - $actions .= "\t\tif (!empty(\$this->data)) {\n"; - $actions .= "\t\t\t\$this->{$currentModelName}->create();\n"; - $actions .= "\t\t\tif (\$this->{$currentModelName}->save(\$this->data)) {\n"; - if ($wannaUseSession) { - $actions .= "\t\t\t\t\$this->Session->setFlash(__('The ".$singularHumanName." has been saved', true));\n"; - $actions .= "\t\t\t\t\$this->redirect(array('action'=>'index'));\n"; - } else { - $actions .= "\t\t\t\t\$this->flash(__('{$currentModelName} saved.', true), array('action'=>'index'));\n"; - } - $actions .= "\t\t\t} else {\n"; - if ($wannaUseSession) { - $actions .= "\t\t\t\t\$this->Session->setFlash(__('The {$singularHumanName} could not be saved. Please, try again.', true));\n"; - } - $actions .= "\t\t\t}\n"; - $actions .= "\t\t}\n"; - foreach ($modelObj->hasAndBelongsToMany as $associationName => $relation) { - if (!empty($associationName)) { - $habtmModelName = $this->_modelName($associationName); - $habtmSingularName = $this->_singularName($associationName); - $habtmPluralName = $this->_pluralName($associationName); - $actions .= "\t\t\${$habtmPluralName} = \$this->{$currentModelName}->{$habtmModelName}->find('list');\n"; - $compact[] = "'{$habtmPluralName}'"; - } - } - foreach ($modelObj->belongsTo as $associationName => $relation) { - if (!empty($associationName)) { - $belongsToModelName = $this->_modelName($associationName); - $belongsToPluralName = $this->_pluralName($associationName); - $actions .= "\t\t\${$belongsToPluralName} = \$this->{$currentModelName}->{$belongsToModelName}->find('list');\n"; - $compact[] = "'{$belongsToPluralName}'"; - } - } - if (!empty($compact)) { - $actions .= "\t\t\$this->set(compact(".join(', ', $compact)."));\n"; - } - $actions .= "\t}\n"; - $actions .= "\n"; - - /* EDIT ACTION */ - $compact = array(); - $actions .= "\tfunction {$admin}edit(\$id = null) {\n"; - $actions .= "\t\tif (!\$id && empty(\$this->data)) {\n"; - if ($wannaUseSession) { - $actions .= "\t\t\t\$this->Session->setFlash(__('Invalid {$singularHumanName}', true));\n"; - $actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n"; - } else { - $actions .= "\t\t\t\$this->flash(__('Invalid {$singularHumanName}', true), array('action'=>'index'));\n"; - } - $actions .= "\t\t}\n"; - $actions .= "\t\tif (!empty(\$this->data)) {\n"; - $actions .= "\t\t\tif (\$this->{$currentModelName}->save(\$this->data)) {\n"; - if ($wannaUseSession) { - $actions .= "\t\t\t\t\$this->Session->setFlash(__('The ".$singularHumanName." has been saved', true));\n"; - $actions .= "\t\t\t\t\$this->redirect(array('action'=>'index'));\n"; - } else { - $actions .= "\t\t\t\t\$this->flash(__('The ".$singularHumanName." has been saved.', true), array('action'=>'index'));\n"; - } - $actions .= "\t\t\t} else {\n"; - if ($wannaUseSession) { - $actions .= "\t\t\t\t\$this->Session->setFlash(__('The {$singularHumanName} could not be saved. Please, try again.', true));\n"; - } - $actions .= "\t\t\t}\n"; - $actions .= "\t\t}\n"; - $actions .= "\t\tif (empty(\$this->data)) {\n"; - $actions .= "\t\t\t\$this->data = \$this->{$currentModelName}->read(null, \$id);\n"; - $actions .= "\t\t}\n"; - - foreach ($modelObj->hasAndBelongsToMany as $associationName => $relation) { - if (!empty($associationName)) { - $habtmModelName = $this->_modelName($associationName); - $habtmSingularName = $this->_singularName($associationName); - $habtmPluralName = $this->_pluralName($associationName); - $actions .= "\t\t\${$habtmPluralName} = \$this->{$currentModelName}->{$habtmModelName}->find('list');\n"; - $compact[] = "'{$habtmPluralName}'"; - } - } - foreach ($modelObj->belongsTo as $associationName => $relation) { - if (!empty($associationName)) { - $belongsToModelName = $this->_modelName($associationName); - $belongsToPluralName = $this->_pluralName($associationName); - $actions .= "\t\t\${$belongsToPluralName} = \$this->{$currentModelName}->{$belongsToModelName}->find('list');\n"; - $compact[] = "'{$belongsToPluralName}'"; - } - } - if (!empty($compact)) { - $actions .= "\t\t\$this->set(compact(".join(',', $compact)."));\n"; - } - $actions .= "\t}\n"; - $actions .= "\n"; - $actions .= "\tfunction {$admin}delete(\$id = null) {\n"; - $actions .= "\t\tif (!\$id) {\n"; - if ($wannaUseSession) { - $actions .= "\t\t\t\$this->Session->setFlash(__('Invalid id for {$singularHumanName}', true));\n"; - $actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n"; - } else { - $actions .= "\t\t\t\$this->flash(__('Invalid {$singularHumanName}', true), array('action'=>'index'));\n"; - } - $actions .= "\t\t}\n"; - $actions .= "\t\tif (\$this->{$currentModelName}->del(\$id)) {\n"; - if ($wannaUseSession) { - $actions .= "\t\t\t\$this->Session->setFlash(__('{$singularHumanName} deleted', true));\n"; - $actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n"; - } else { - $actions .= "\t\t\t\$this->flash(__('{$singularHumanName} deleted', true), array('action'=>'index'));\n"; - } - $actions .= "\t\t}\n"; - $actions .= "\t}\n"; - $actions .= "\n"; return $actions; } diff --git a/cake/console/libs/templates/objects/controller_actions.ctp b/cake/console/libs/templates/objects/controller_actions.ctp new file mode 100644 index 000000000..90c4a02c3 --- /dev/null +++ b/cake/console/libs/templates/objects/controller_actions.ctp @@ -0,0 +1,138 @@ + + + function index() { + $this->->recursive = 0; + $this->set('', $this->paginate()); + } + + + function view($id = null) { + if (!$id) { + + $this->Session->setFlash(__('Invalid ', true)); + $this->redirect(array('action'=>'index')); + + $this->flash(__('Invalid ', true), array('action'=>'index')); + + } + $this->set('', $this->->read(null, $id)); + } + + + + function add() { + if (!empty($this->data)) { + $this->->create(); + if ($this->->save($this->data)) { + + $this->Session->setFlash(__('The has been saved', true)); + $this->redirect(array('action'=>'index')); + + $this->flash(__(' saved.', true), array('action'=>'index')); + + } else { + + $this->Session->setFlash(__('The could not be saved. Please, try again.', true)); + + } + } +{$assoc} as $associationName => $relation): + if (!empty($associationName)): + $otherModelName = $this->_modelName($associationName); + $otherPluralName = $this->_pluralName($associationName); + echo "\t\t\${$otherPluralName} = \$this->{$currentModelName}->{$otherModelName}->find('list');\n"; + $compact[] = "'{$otherPluralName}'"; + endif; + endforeach; + endforeach; + if (!empty($compact)): + echo "\t\t\$this->set(compact(".join(', ', $compact)."));\n"; + endif; +?> + } + + + function edit($id = null) { + if (!$id && empty($this->data)) { + + $this->Session->setFlash(__('Invalid ', true)); + $this->redirect(array('action'=>'index')); + + $this->flash(__('Invalid ', true), array('action'=>'index')); + + } + if (!empty($this->data)) { + if ($this->->save($this->data)) { + + $this->Session->setFlash(__('The has been saved', true)); + $this->redirect(array('action'=>'index')); + + $this->flash(__('The has been saved.', true), array('action'=>'index')); + + } else { + + $this->Session->setFlash(__('The could not be saved. Please, try again.', true)); + + } + } + if (empty($this->data)) { + $this->data = $this->->read(null, $id); + } +{$assoc} as $associationName => $relation): + if (!empty($associationName)): + $otherModelName = $this->_modelName($associationName); + $otherPluralName = $this->_pluralName($associationName); + echo "\t\t\${$otherPluralName} = \$this->{$currentModelName}->{$otherModelName}->find('list');\n"; + $compact[] = "'{$otherPluralName}'"; + endif; + endforeach; + endforeach; + if (!empty($compact)): + echo "\t\t\$this->set(compact(".join(', ', $compact)."));\n"; + endif; + ?> + } + + function delete($id = null) { + if (!$id) { + + $this->Session->setFlash(__('Invalid id for ', true)); + $this->redirect(array('action'=>'index')); + + $this->flash(__('Invalid ', true), array('action'=>'index')); + + } + if ($this->->del($id)) { + + $this->Session->setFlash(__(' deleted', true)); + $this->redirect(array('action'=>'index')); + + $this->flash(__(' deleted', true), array('action'=>'index')); + + } + }