Skip to content

Commit

Permalink
fix field captions
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Oct 8, 2021
1 parent c2b9ce3 commit 49ed8fd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion data/src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ public function isHidden(): bool
*/
public function getCaption(): string
{
return $this->caption ?? $this->ui['caption'] ?? $this->readableCaption(preg_replace('~^atk_fp_\w+?__~', '', $this->short_name));
return $this->caption ?? $this->ui['caption'] ?? $this->readableCaption($this->short_name);
}

// }}}
Expand Down
25 changes: 23 additions & 2 deletions demos/init-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

class ModelWithPrefixedFields extends Model
{
/** @var <string, string> */
private static $prefixedFieldNames = [];

private function prefixFieldName(string $fieldName, bool $forActualName = false): string
{
$tableShort = $this->table;
Expand All @@ -30,7 +33,7 @@ private function prefixFieldName(string $fieldName, bool $forActualName = false)
}

if ($forActualName) {
$fieldName = preg_replace('~^atk_fp_' . preg_quote($tableShort, '~') . '__~', '', $fieldName);
$fieldName = $this->unprefixFieldName($fieldName);
}

$fieldShort = $fieldName;
Expand All @@ -46,7 +49,24 @@ private function prefixFieldName(string $fieldName, bool $forActualName = false)
$fieldShort .= '_id';
}

return 'atk_' . ($forActualName ? 'a' : '') . 'fp_' . $tableShort . '__' . $fieldShort;
$res = 'atk_' . ($forActualName ? 'a' : '') . 'fp_' . $tableShort . '__' . $fieldShort;

self::$prefixedFieldNames[$res] = $fieldName;

return $res;
}

private function unprefixFieldName(string $name): string
{
if (!str_starts_with($name, 'atk_fp_')) {
return $name;
}

if (isset(self::$prefixedFieldNames[$name . '_id'])) {
return self::$prefixedFieldNames[$name . '_id'];
}

return self::$prefixedFieldNames[$name];
}

protected function createHintablePropsFromClassDoc(string $className): array
Expand Down Expand Up @@ -75,6 +95,7 @@ public function addField($name, $seed = []): \Atk4\Data\Field
{
$seed = \Atk4\Core\Factory::mergeSeeds($seed, [
'actual' => $this->prefixFieldName($name, true),
'caption' => $this->readableCaption($this->unprefixFieldName($name)),
]);

return parent::addField($name, $seed);
Expand Down

0 comments on commit 49ed8fd

Please sign in to comment.