Skip to content

Commit

Permalink
Update templates and ViewTask to kind of work.
Browse files Browse the repository at this point in the history
The code kind of works right now. I still need to do some manual testing
to catch any errors the test suite is not catching.
  • Loading branch information
markstory committed Mar 25, 2014
1 parent 3f2166c commit b8c8922
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 81 deletions.
28 changes: 16 additions & 12 deletions src/Console/Command/Task/ViewTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,31 +290,33 @@ protected function _loadController() {
$plugin = $this->plugin . '.';
}

$controllerClassName = $this->controllerName;
$controllerClassName = App::className($plugin . $controllerClassName, 'Controller');

if (!class_exists($controllerClassName)) {
$file = $controllerClassName . '.php';
$this->err(__d('cake_console', "The file '%s' could not be found.\nIn order to bake a view, you'll need to first create the controller.", $file));
if (!class_exists($this->controllerClass)) {
$file = $controllerClass . '.php';
$this->err(__d(
'cake_console',
"The file '%s' could not be found.\nIn order to bake a view, you'll need to first create the controller.",
str_replace('\\', '/', $file)
));
return $this->_stop();
}

$controllerObj = new $controllerClassName();
$controllerObj = new $this->controllerClass();
$controllerObj->plugin = $this->plugin;
$controllerObj->constructClasses();
$modelClass = $controllerObj->modelClass;
$modelObj = $controllerObj->{$modelClass};

if ($modelObj) {
$primaryKey = $modelObj->primaryKey();
$primaryKey = (array)$modelObj->primaryKey();
$displayField = $modelObj->displayField();
$singularVar = Inflector::variable($modelClass);
$singularVar = $this->_singularName($this->controllerName);
$singularHumanName = $this->_singularHumanName($this->controllerName);
$schema = $modelObj->schema();
$fields = $schema->columns();
$associations = $this->_associations($modelObj);
} else {
$primaryKey = $displayField = null;
$primaryKey = [];
$displayField = null;
$singularVar = Inflector::variable(Inflector::singularize($this->controllerName));
$singularHumanName = $this->_singularHumanName($this->controllerName);
$fields = $schema = $associations = [];
Expand Down Expand Up @@ -504,8 +506,10 @@ protected function _associations(Table $model) {
$target = $assoc->target();
$assocName = $assoc->name();
$alias = $target->alias();
$assoiations[$type][$assocName] = [
'primaryKey' => $target->primaryKey(),

$associations[$type][$assocName] = [
'property' => $assoc->property(),
'primaryKey' => (array)$target->primaryKey(),
'displayField' => $target->displayField(),
'foreignKey' => $assoc->foreignKey(),
'controller' => Inflector::underscore($alias),
Expand Down
17 changes: 8 additions & 9 deletions src/Console/Templates/default/views/form.ctp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
/**
*
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -17,36 +15,37 @@
use Cake\Utility\Inflector;
?>
<div class="<?= $pluralVar; ?> form">
<?= "<?= \$this->Form->create('{$modelClass}'); ?>\n"; ?>
<?= "<?= \$this->Form->create(\${$singularVar}); ?>\n"; ?>
<fieldset>
<legend><?php printf("<?= __('%s %s'); ?>", Inflector::humanize($action), $singularHumanName); ?></legend>
<?php
echo "\t<?php\n";
foreach ($fields as $field) {
if (strpos($action, 'add') !== false && $field == $primaryKey) {
if (strpos($action, 'add') !== false && in_array($field, $primaryKey)) {
continue;
} elseif (!in_array($field, ['created', 'modified', 'updated'])) {
echo "\t\techo \$this->Form->input('{$field}');\n";
}
}
if (!empty($associations['hasAndBelongsToMany'])) {
foreach ($associations['hasAndBelongsToMany'] as $assocName => $assocData) {
echo "\t\techo \$this->Form->input('{$assocName}');\n";
if (!empty($associations['BelongsToMany'])) {
foreach ($associations['BelongsToMany'] as $assocName => $assocData) {
echo "\t\techo \$this->Form->input('{$assocName}._ids', ['options' => ${$assocName}]);\n";
}
}
echo "\t?>\n";
?>
</fieldset>
<?php
echo "<?= \$this->Form->end(__('Submit')); ?>\n";
echo "<?= \$this->Form->submit(__('Submit')); ?>\n";
echo "<?= \$this->Form->end(); ?>\n";
?>
</div>
<div class="actions">
<h3><?= "<?= __('Actions'); ?>"; ?></h3>
<ul>

<?php if (strpos($action, 'add') === false): ?>
<li><?= "<?= \$this->Form->postLink(__('Delete'), ['action' => 'delete', \$this->Form->value('{$modelClass}.{$primaryKey}')], null, __('Are you sure you want to delete # %s?', \$this->Form->value('{$modelClass}.{$primaryKey}'))); ?>"; ?></li>
<li><?= "<?= \$this->Form->postLink(__('Delete'), ['action' => 'delete', \${$singularVar}->{$primaryKey[0]}], null, __('Are you sure you want to delete # %s?', \${$singularVar}->{$primaryKey[0]})); ?>"; ?></li>
<?php endif; ?>
<li><?= "<?= \$this->Html->link(__('List " . $pluralHumanName . "'), ['action' => 'index']); ?>"; ?></li>
<?php
Expand Down
26 changes: 13 additions & 13 deletions src/Console/Templates/default/views/index.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,26 @@ use Cake\Utility\Inflector;
echo "\t<tr>\n";
foreach ($fields as $field) {
$isKey = false;
if (!empty($associations['belongsTo'])) {
foreach ($associations['belongsTo'] as $alias => $details) {
if (!empty($associations['BelongsTo'])) {
foreach ($associations['BelongsTo'] as $alias => $details) {
if ($field === $details['foreignKey']) {
$isKey = true;
echo "\t\t<td>\n\t\t\t<?= \$this->Html->link(\${$singularVar}['{$alias}']['{$details['displayField']}'], ['controller' => '{$details['controller']}', 'action' => 'view', \${$singularVar}['{$alias}']['{$details['primaryKey']}']]); ?>\n\t\t</td>\n";
echo "\t\t<td>\n\t\t\t<?= \$this->Html->link(\${$singularVar}->{$details['property']}->{$details['displayField']}, ['controller' => '{$details['controller']}', 'action' => 'view', \${$singularVar}->{$details['primaryKey'][0]}]); ?>\n\t\t</td>\n";
break;
}
}
}
if ($isKey !== true) {
echo "\t\t<td><?= h(\${$singularVar}['{$modelClass}']['{$field}']); ?>&nbsp;</td>\n";
echo "\t\t<td><?= h(\${$singularVar}->{$field}); ?>&nbsp;</td>\n";
}
}

$pk = "\${$singularVar}->{$primaryKey[0]}";

echo "\t\t<td class=\"actions\">\n";
echo "\t\t\t<?= \$this->Html->link(__('View'), ['action' => 'view', \${$singularVar}['{$modelClass}']['{$primaryKey}']]); ?>\n";
echo "\t\t\t<?= \$this->Html->link(__('Edit'), ['action' => 'edit', \${$singularVar}['{$modelClass}']['{$primaryKey}']]); ?>\n";
echo "\t\t\t<?= \$this->Form->postLink(__('Delete'), ['action' => 'delete', \${$singularVar}['{$modelClass}']['{$primaryKey}']], null, __('Are you sure you want to delete # %s?', \${$singularVar}['{$modelClass}']['{$primaryKey}'])); ?>\n";
echo "\t\t\t<?= \$this->Html->link(__('View'), ['action' => 'view', {$pk}]]); ?>\n";
echo "\t\t\t<?= \$this->Html->link(__('Edit'), ['action' => 'edit', {$pk}]]); ?>\n";
echo "\t\t\t<?= \$this->Form->postLink(__('Delete'), ['action' => 'delete', {$pk}], null, __('Are you sure you want to delete # %s?', {$pk})); ?>\n";
echo "\t\t</td>\n";
echo "\t</tr>\n";

Expand All @@ -56,17 +58,15 @@ use Cake\Utility\Inflector;
</table>
<p>
<?= "<?php
echo \$this->Paginator->counter([
'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
]);
echo \$this->Paginator->counter();
?>"; ?>
</p>
<div class="paging">
<?php
echo "<?php\n";
echo "\t\techo \$this->Paginator->prev('< ' . __('previous'), [], null, ['class' => 'prev disabled']);\n";
echo "\t\techo \$this->Paginator->numbers(['separator' => '']);\n";
echo "\t\techo \$this->Paginator->next(__('next') . ' >', [], null, ['class' => 'next disabled']);\n";
echo "\t\techo \$this->Paginator->prev('< ' . __('previous'));\n";
echo "\t\techo \$this->Paginator->numbers();\n";
echo "\t\techo \$this->Paginator->next(__('next') . ' >');\n";
echo "\t?>\n";
?>
</div>
Expand Down
50 changes: 27 additions & 23 deletions src/Console/Templates/default/views/view.ctp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
/**
*
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -22,19 +20,19 @@ use Cake\Utility\Inflector;
<?php
foreach ($fields as $field) {
$isKey = false;
if (!empty($associations['belongsTo'])) {
foreach ($associations['belongsTo'] as $alias => $details) {
if (!empty($associations['BelongsTo'])) {
foreach ($associations['BelongsTo'] as $alias => $details) {
if ($field === $details['foreignKey']) {
$isKey = true;
echo "\t\t<dt><?= __('" . Inflector::humanize(Inflector::underscore($alias)) . "'); ?></dt>\n";
echo "\t\t<dd>\n\t\t\t<?= \$this->Html->link(\${$singularVar}['{$alias}']['{$details['displayField']}'], ['controller' => '{$details['controller']}', 'action' => 'view', \${$singularVar}['{$alias}']['{$details['primaryKey']}']]); ?>\n\t\t\t&nbsp;\n\t\t</dd>\n";
echo "\t\t<dd>\n\t\t\t<?= \$this->Html->link(\${$singularVar}->{$details['property']}->{$details['displayField']}, ['controller' => '{$details['controller']}', 'action' => 'view', \${$singularVar}->{$details['primaryKey'][0]}]); ?>\n\t\t\t&nbsp;\n\t\t</dd>\n";
break;
}
}
}
if ($isKey !== true) {
echo "\t\t<dt><?= __('" . Inflector::humanize($field) . "'); ?></dt>\n";
echo "\t\t<dd>\n\t\t\t<?= h(\${$singularVar}['{$modelClass}']['{$field}']); ?>\n\t\t\t&nbsp;\n\t\t</dd>\n";
echo "\t\t<dd>\n\t\t\t<?= h(\${$singularVar}->{$field}); ?>\n\t\t\t&nbsp;\n\t\t</dd>\n";
}
}
?>
Expand All @@ -44,8 +42,10 @@ foreach ($fields as $field) {
<h3><?= "<?= __('Actions'); ?>"; ?></h3>
<ul>
<?php
echo "\t\t<li><?= \$this->Html->link(__('Edit " . $singularHumanName ."'), ['action' => 'edit', \${$singularVar}['{$modelClass}']['{$primaryKey}']]); ?> </li>\n";
echo "\t\t<li><?= \$this->Form->postLink(__('Delete " . $singularHumanName . "'), ['action' => 'delete', \${$singularVar}['{$modelClass}']['{$primaryKey}']], null, __('Are you sure you want to delete # %s?', \${$singularVar}['{$modelClass}']['{$primaryKey}'])); ?> </li>\n";
$pk = "\${$singularVar}->{$primaryKey[0]}";

echo "\t\t<li><?= \$this->Html->link(__('Edit " . $singularHumanName ."'), ['action' => 'edit', {$pk}]); ?> </li>\n";
echo "\t\t<li><?= \$this->Form->postLink(__('Delete " . $singularHumanName . "'), ['action' => 'delete', {$pk}], null, __('Are you sure you want to delete # %s?', {$pk})); ?> </li>\n";
echo "\t\t<li><?= \$this->Html->link(__('List " . $pluralHumanName . "'), ['action' => 'index']); ?> </li>\n";
echo "\t\t<li><?= \$this->Html->link(__('New " . $singularHumanName . "'), ['action' => 'add']); ?> </li>\n";

Expand All @@ -63,43 +63,45 @@ foreach ($fields as $field) {
</ul>
</div>
<?php
if (!empty($associations['hasOne'])) :
foreach ($associations['hasOne'] as $alias => $details): ?>
if (!empty($associations['HasOne'])) :
foreach ($associations['HasOne'] as $alias => $details): ?>
<div class="related">
<h3><?= "<?= __('Related " . Inflector::humanize($details['controller']) . "'); ?>"; ?></h3>
<?= "<?php if (!empty(\${$singularVar}['{$alias}'])): ?>\n"; ?>
<dl>
<?php
foreach ($details['fields'] as $field) {
echo "\t\t<dt><?= __('" . Inflector::humanize($field) . "'); ?></dt>\n";
echo "\t\t<dd>\n\t<?= \${$singularVar}['{$alias}']['{$field}']; ?>\n&nbsp;</dd>\n";
echo "\t\t<dd>\n\t<?= \${$singularVar}->{$details['property']}->{$field}; ?>\n&nbsp;</dd>\n";
}
?>
</dl>
<?= "<?php endif; ?>\n"; ?>
<div class="actions">
<ul>
<li><?= "<?= \$this->Html->link(__('Edit " . Inflector::humanize(Inflector::underscore($alias)) . "'), ['controller' => '{$details['controller']}', 'action' => 'edit', \${$singularVar}['{$alias}']['{$details['primaryKey']}']]); ?></li>\n"; ?>
<li><?= "<?= \$this->Html->link(__('Edit " . Inflector::humanize(Inflector::underscore($alias)) . "'), ['controller' => '{$details['controller']}', 'action' => 'edit', \${$singularVar}->{$details['property']}->{$details['primaryKey'][0]}]); ?></li>\n"; ?>
</ul>
</div>
</div>
<?php
endforeach;
endif;
if (empty($associations['hasMany'])) {
$associations['hasMany'] = [];

if (empty($associations['HasMany'])) {
$associations['HasMany'] = [];
}
if (empty($associations['hasAndBelongsToMany'])) {
$associations['hasAndBelongsToMany'] = [];
if (empty($associations['BelongsToMany'])) {
$associations['BelongsToMany'] = [];
}
$relations = array_merge($associations['hasMany'], $associations['hasAndBelongsToMany']);

$relations = array_merge($associations['HasMany'], $associations['BelongsToMany']);
foreach ($relations as $alias => $details):
$otherSingularVar = Inflector::variable($alias);
$otherPluralHumanName = Inflector::humanize($details['controller']);
?>
<div class="related">
<h3><?= "<?= __('Related " . $otherPluralHumanName . "'); ?>"; ?></h3>
<?= "<?php if (!empty(\${$singularVar}['{$alias}'])): ?>\n"; ?>
<?= "<?php if (!empty(\${$singularVar}->{$alias})): ?>\n"; ?>
<table cellpadding = "0" cellspacing = "0">
<tr>
<?php
Expand All @@ -110,16 +112,18 @@ foreach ($relations as $alias => $details):
<th class="actions"><?= "<?= __('Actions'); ?>"; ?></th>
</tr>
<?php
echo "\t<?php foreach (\${$singularVar}['{$alias}'] as \${$otherSingularVar}): ?>\n";
echo "\t<?php foreach (\${$singularVar}->{$details['property']} as \${$otherSingularVar}): ?>\n";
echo "\t\t<tr>\n";
foreach ($details['fields'] as $field) {
echo "\t\t\t<td><?= \${$otherSingularVar}['{$field}']; ?></td>\n";
echo "\t\t\t<td><?= \${$otherSingularVar}->{$field} ?></td>\n";
}

$otherPk = "\${$otherSingularVar}->{$details['primaryKey'][0]}";

echo "\t\t\t<td class=\"actions\">\n";
echo "\t\t\t\t<?= \$this->Html->link(__('View'), ['controller' => '{$details['controller']}', 'action' => 'view', \${$otherSingularVar}['{$details['primaryKey']}']]); ?>\n";
echo "\t\t\t\t<?= \$this->Html->link(__('Edit'), ['controller' => '{$details['controller']}', 'action' => 'edit', \${$otherSingularVar}['{$details['primaryKey']}']]); ?>\n";
echo "\t\t\t\t<?= \$this->Form->postLink(__('Delete'), ['controller' => '{$details['controller']}', 'action' => 'delete', \${$otherSingularVar}['{$details['primaryKey']}']], null, __('Are you sure you want to delete # %s?', \${$otherSingularVar}['{$details['primaryKey']}'])); ?>\n";
echo "\t\t\t\t<?= \$this->Html->link(__('View'), ['controller' => '{$details['controller']}', 'action' => 'view', {$otherPk}]); ?>\n";
echo "\t\t\t\t<?= \$this->Html->link(__('Edit'), ['controller' => '{$details['controller']}', 'action' => 'edit', {$otherPk}]); ?>\n";
echo "\t\t\t\t<?= \$this->Form->postLink(__('Delete'), ['controller' => '{$details['controller']}', 'action' => 'delete', {$otherPk}], null, __('Are you sure you want to delete # %s?', {$otherPk})); ?>\n";
echo "\t\t\t</td>\n";
echo "\t\t</tr>\n";

Expand Down

0 comments on commit b8c8922

Please sign in to comment.