|
14 | 14 | */
|
15 | 15 | use Cake\Utility\Inflector;
|
16 | 16 |
|
| 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 | + |
17 | 32 | $groupedFields = collection($fields)
|
18 |
| - ->groupBy(function($field) use ($schema) { |
| 33 | + ->groupBy(function($field) use ($schema, $associationFields) { |
19 | 34 | $type = $schema->columnType($field);
|
| 35 | + if (isset($associationFields[$field])) { |
| 36 | + return 'string'; |
| 37 | + } |
20 | 38 | if (in_array($type, ['integer', 'float', 'decimal', 'biginteger'])) {
|
21 | 39 | return 'number';
|
22 | 40 | }
|
23 | 41 | if (in_array($type, ['date', 'time', 'datetime', 'timestamp'])) {
|
24 | 42 | return 'date';
|
25 | 43 | }
|
26 | 44 | return in_array($type, ['text', 'boolean']) ? $type : 'string';
|
27 |
| - })->toArray(); |
| 45 | + }) |
| 46 | + ->toArray(); |
28 | 47 |
|
29 |
| - $groupedFields += ['number' => [], 'string' => [], 'boolean' => [], 'date' => [], 'text' => []]; |
| 48 | +$groupedFields += ['number' => [], 'string' => [], 'boolean' => [], 'date' => [], 'text' => []]; |
30 | 49 | ?>
|
31 | 50 | <div class="actions columns large-2 medium-3">
|
32 | 51 | <h3><?= "<?= __('Actions'); ?>"; ?></h3>
|
@@ -55,92 +74,57 @@ $groupedFields = collection($fields)
|
55 | 74 | <div class="<?= $pluralVar ?> view large-10 medium-9 columns">
|
56 | 75 | <h2><?= "<?= h(\${$singularVar}->{$displayField}) ?>"; ?></h2>
|
57 | 76 | <div class="row">
|
| 77 | +<?php if ($groupedFields['string']) : ?> |
58 | 78 | <div class="large-5 columns strings">
|
59 | 79 | <?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 : ?> |
60 | 86 | <h6 class="subheader"><?= "<?= __('" . Inflector::humanize($field) . "') ?>" ?></h6>
|
61 | 87 | <p><?= "<?= h(\${$singularVar}->{$field}) ?>" ?></p>
|
| 88 | +<?php endif; ?> |
62 | 89 | <?php endforeach; ?>
|
63 | 90 | </div>
|
| 91 | +<?php endif; ?> |
| 92 | +<?php if ($groupedFields['number']) : ?> |
64 | 93 | <div class="large-2 larege-offset-1 columns numbers end">
|
65 | 94 | <?php foreach ($groupedFields['number'] as $field) : ?>
|
66 | 95 | <h6 class="subheader"><?= "<?= __('" . Inflector::humanize($field) . "') ?>" ?></h6>
|
67 | 96 | <p><?= "<?= \$this->Number->format(\${$singularVar}->{$field}) ?>" ?></p>
|
68 | 97 | <?php endforeach; ?>
|
69 | 98 | </div>
|
| 99 | +<?php endif; ?> |
| 100 | +<?php if ($groupedFields['date']) : ?> |
70 | 101 | <div class="large-2 columns dates end">
|
71 | 102 | <?php foreach ($groupedFields['date'] as $field) : ?>
|
72 | 103 | <h6 class="subheader"><?= "<?= __('" . Inflector::humanize($field) . "') ?>" ?></h6>
|
73 | 104 | <p><?= "<?= h(\${$singularVar}->{$field}) ?>" ?></p>
|
74 | 105 | <?php endforeach; ?>
|
75 | 106 | </div>
|
| 107 | +<?php endif; ?> |
| 108 | +<?php if ($groupedFields['boolean']) : ?> |
76 | 109 | <div class="large-2 columns booleans end">
|
77 | 110 | <?php foreach ($groupedFields['boolean'] as $field) : ?>
|
78 | 111 | <h6 class="subheader"><?= "<?= __('" . Inflector::humanize($field) . "') ?>" ?></h6>
|
79 | 112 | <p><?= "<?= \${$singularVar}->{$field} ? __('Yes') : __('No'); ?>" ?></p>
|
80 | 113 | <?php endforeach; ?>
|
81 | 114 | </div>
|
| 115 | +<?php endif; ?> |
82 | 116 | </div>
|
| 117 | +<?php if ($groupedFields['text']) : ?> |
83 | 118 | <?php foreach ($groupedFields['text'] as $field) : ?>
|
84 | 119 | <div class="row">
|
85 | 120 | <h6 class="subheader"><?= "<?= __('" . Inflector::humanize($field) . "') ?>" ?></h6>
|
86 | 121 | <?= "<?= \$this->Text->autoParagraph(h(\${$singularVar}->{$field})); ?>" ?>
|
87 | 122 | </div>
|
88 | 123 | <?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 \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 \n\t\t</dd>\n"; |
106 |
| - } |
107 |
| -} |
108 |
| -?> |
109 |
| - </dl> |
| 124 | +<?php endif; ?> |
110 | 125 | </div>
|
111 | 126 | <?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 </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']; |
144 | 128 | foreach ($relations as $alias => $details):
|
145 | 129 | $otherSingularVar = Inflector::variable($alias);
|
146 | 130 | $otherPluralHumanName = Inflector::humanize($details['controller']);
|
|
0 commit comments