Skip to content

Commit 44bd7c4

Browse files
committed
Better displaying of HasOne and BelongsTo in baked view templates
1 parent 8c58d49 commit 44bd7c4

File tree

1 file changed

+40
-56
lines changed

1 file changed

+40
-56
lines changed

src/Template/Bake/default/views/view.ctp

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,38 @@
1414
*/
1515
use Cake\Utility\Inflector;
1616

17+
$associations += ['BelongsTo' => [], 'HasOne' => [], 'HasMany' => [], 'BelongsToMany' => []];
18+
$immediateAssociations = $associations['BelongsTo'] + $associations['HasOne'];
19+
$associationFields = collection($fields)
20+
->map(function($field) use ($immediateAssociations) {
21+
foreach ($immediateAssociations as $alias => $details) {
22+
if ($field === $details['foreignKey']) {
23+
return [$field => $details];
24+
}
25+
}
26+
})
27+
->filter()
28+
->reduce(function($fields, $value) {
29+
return $fields + $value;
30+
}, []);
31+
1732
$groupedFields = collection($fields)
18-
->groupBy(function($field) use ($schema) {
33+
->groupBy(function($field) use ($schema, $associationFields) {
1934
$type = $schema->columnType($field);
35+
if (isset($associationFields[$field])) {
36+
return 'string';
37+
}
2038
if (in_array($type, ['integer', 'float', 'decimal', 'biginteger'])) {
2139
return 'number';
2240
}
2341
if (in_array($type, ['date', 'time', 'datetime', 'timestamp'])) {
2442
return 'date';
2543
}
2644
return in_array($type, ['text', 'boolean']) ? $type : 'string';
27-
})->toArray();
45+
})
46+
->toArray();
2847

29-
$groupedFields += ['number' => [], 'string' => [], 'boolean' => [], 'date' => [], 'text' => []];
48+
$groupedFields += ['number' => [], 'string' => [], 'boolean' => [], 'date' => [], 'text' => []];
3049
?>
3150
<div class="actions columns large-2 medium-3">
3251
<h3><?= "<?= __('Actions'); ?>"; ?></h3>
@@ -55,92 +74,57 @@ $groupedFields = collection($fields)
5574
<div class="<?= $pluralVar ?> view large-10 medium-9 columns">
5675
<h2><?= "<?= h(\${$singularVar}->{$displayField}) ?>"; ?></h2>
5776
<div class="row">
77+
<?php if ($groupedFields['string']) : ?>
5878
<div class="large-5 columns strings">
5979
<?php foreach ($groupedFields['string'] as $field) : ?>
80+
<?php if (isset($associationFields[$field])) :
81+
$details = $associationFields[$field];
82+
?>
83+
<h6 class="subheader"><?= "<?= __('" . Inflector::humanize($details['property']) . "') ?>" ?></h6>
84+
<p><?= "<?= \${$singularVar}->has('{$details['property']}') ? \$this->Html->link(\${$singularVar}->{$details['property']}->{$details['displayField']}, ['controller' => '{$details['controller']}', 'action' => 'view', \${$singularVar}->{$details['property']}->{$details['primaryKey'][0]}]) : '' ?>" ?></p>
85+
<?php else : ?>
6086
<h6 class="subheader"><?= "<?= __('" . Inflector::humanize($field) . "') ?>" ?></h6>
6187
<p><?= "<?= h(\${$singularVar}->{$field}) ?>" ?></p>
88+
<?php endif; ?>
6289
<?php endforeach; ?>
6390
</div>
91+
<?php endif; ?>
92+
<?php if ($groupedFields['number']) : ?>
6493
<div class="large-2 larege-offset-1 columns numbers end">
6594
<?php foreach ($groupedFields['number'] as $field) : ?>
6695
<h6 class="subheader"><?= "<?= __('" . Inflector::humanize($field) . "') ?>" ?></h6>
6796
<p><?= "<?= \$this->Number->format(\${$singularVar}->{$field}) ?>" ?></p>
6897
<?php endforeach; ?>
6998
</div>
99+
<?php endif; ?>
100+
<?php if ($groupedFields['date']) : ?>
70101
<div class="large-2 columns dates end">
71102
<?php foreach ($groupedFields['date'] as $field) : ?>
72103
<h6 class="subheader"><?= "<?= __('" . Inflector::humanize($field) . "') ?>" ?></h6>
73104
<p><?= "<?= h(\${$singularVar}->{$field}) ?>" ?></p>
74105
<?php endforeach; ?>
75106
</div>
107+
<?php endif; ?>
108+
<?php if ($groupedFields['boolean']) : ?>
76109
<div class="large-2 columns booleans end">
77110
<?php foreach ($groupedFields['boolean'] as $field) : ?>
78111
<h6 class="subheader"><?= "<?= __('" . Inflector::humanize($field) . "') ?>" ?></h6>
79112
<p><?= "<?= \${$singularVar}->{$field} ? __('Yes') : __('No'); ?>" ?></p>
80113
<?php endforeach; ?>
81114
</div>
115+
<?php endif; ?>
82116
</div>
117+
<?php if ($groupedFields['text']) : ?>
83118
<?php foreach ($groupedFields['text'] as $field) : ?>
84119
<div class="row">
85120
<h6 class="subheader"><?= "<?= __('" . Inflector::humanize($field) . "') ?>" ?></h6>
86121
<?= "<?= \$this->Text->autoParagraph(h(\${$singularVar}->{$field})); ?>" ?>
87122
</div>
88123
<?php endforeach; ?>
89-
<dl>
90-
<?php
91-
foreach ($fields as $field) {
92-
$isKey = false;
93-
if (!empty($associations['BelongsTo'])) {
94-
foreach ($associations['BelongsTo'] as $alias => $details) {
95-
if ($field === $details['foreignKey']) {
96-
$isKey = true;
97-
echo "\t\t<dt><?= __('" . Inflector::humanize(Inflector::underscore($details['property'])) . "') ?></dt>\n";
98-
echo "\t\t<dd>\n\t\t\t<?= \${$singularVar}->has('{$details['property']}') ? \$this->Html->link(\${$singularVar}->{$details['property']}->{$details['displayField']}, ['controller' => '{$details['controller']}', 'action' => 'view', \${$singularVar}->{$details['property']}->{$details['primaryKey'][0]}]) : '' ?>\n\t\t\t&nbsp;\n\t\t</dd>\n";
99-
break;
100-
}
101-
}
102-
}
103-
if ($isKey !== true) {
104-
echo "\t\t<dt><?= __('" . Inflector::humanize($field) . "') ?></dt>\n";
105-
echo "\t\t<dd>\n\t\t\t<?= h(\${$singularVar}->{$field}) ?>\n\t\t\t&nbsp;\n\t\t</dd>\n";
106-
}
107-
}
108-
?>
109-
</dl>
124+
<?php endif; ?>
110125
</div>
111126
<?php
112-
if (!empty($associations['HasOne'])) :
113-
foreach ($associations['HasOne'] as $alias => $details): ?>
114-
<div class="related">
115-
<h3 class="subheader"><?= "<?= __('Related " . Inflector::humanize($details['controller']) . "') ?>"; ?></h3>
116-
<?= "<?php if (!empty(\${$singularVar}['{$alias}'])): ?>\n"; ?>
117-
<dl>
118-
<?php
119-
foreach ($details['fields'] as $field) {
120-
echo "\t\t<dt><?= __('" . Inflector::humanize($field) . "') ?></dt>\n";
121-
echo "\t\t<dd>\n\t<?= h(\${$singularVar}->{$details['property']}->{$field}) ?>\n&nbsp;</dd>\n";
122-
}
123-
?>
124-
</dl>
125-
<?= "<?php endif; ?>\n"; ?>
126-
<div class="actions">
127-
<ul>
128-
<li><?= "<?= \$this->Html->link(__('Edit " . Inflector::humanize(Inflector::underscore($alias)) . "'), ['controller' => '{$details['controller']}', 'action' => 'edit', \${$singularVar}->{$details['property']}->{$details['primaryKey'][0]}]) ?></li>\n"; ?>
129-
</ul>
130-
</div>
131-
</div>
132-
<?php
133-
endforeach;
134-
endif;
135-
136-
if (empty($associations['HasMany'])) {
137-
$associations['HasMany'] = [];
138-
}
139-
if (empty($associations['BelongsToMany'])) {
140-
$associations['BelongsToMany'] = [];
141-
}
142-
143-
$relations = array_merge($associations['HasMany'], $associations['BelongsToMany']);
127+
$relations = $associations['HasMany'] + $associations['BelongsToMany'];
144128
foreach ($relations as $alias => $details):
145129
$otherSingularVar = Inflector::variable($alias);
146130
$otherPluralHumanName = Inflector::humanize($details['controller']);

0 commit comments

Comments
 (0)