From 1434f1782b6caddae5580e06d40aec6e6fba8909 Mon Sep 17 00:00:00 2001 From: Ioannis Igoumenos Date: Sun, 28 Apr 2024 18:25:10 +0300 Subject: [PATCH] FieldHelper improvements --- app/src/View/Helper/FieldHelper.php | 148 +++++++++++++++------------- 1 file changed, 79 insertions(+), 69 deletions(-) diff --git a/app/src/View/Helper/FieldHelper.php b/app/src/View/Helper/FieldHelper.php index 205dd2f64..60355b63c 100644 --- a/app/src/View/Helper/FieldHelper.php +++ b/app/src/View/Helper/FieldHelper.php @@ -25,7 +25,6 @@ * @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) */ -// XXX can we merge this with Match so we only maintain one file? declare(strict_types = 1); namespace App\View\Helper; @@ -37,7 +36,7 @@ use App\Lib\Util\StringUtilities; class FieldHelper extends Helper { - public $helpers = ['Form', 'Html', 'Url', 'Alert']; + public $helpers = ['Form', 'Html']; // Is this read-only or read-write? protected bool $editable = true; @@ -307,14 +306,8 @@ public function formField(string $fieldName, $fieldArgs['empty'] = !\in_array($fieldName, ['status', 'sync_status_on_delete'], true) || (isset($fieldOptions['empty']) && $fieldOptions['empty']); - // Remove prefix from field value - if(!empty($fieldPrefix) && !empty($this->getEntity()->$fieldName)) { - $vv_obj = $this->getView()->get('vv_obj'); - $fieldValue = $vv_obj->$fieldName; - $fieldValueTemp = str_replace($fieldPrefix, '', $fieldValue); - $vv_obj->$fieldName = $fieldValueTemp; - $this->getView()->set('vv_obj', $vv_obj); - } + // Manipulate the vv_object for the hasPrefix use case + $this->handlePrefix($fieldPrefix, $fieldName); // Get the field type from the map of fields (e.g. 'boolean', 'string', 'timestamp') $fieldType = $fieldType ?? $this->getFieldType($fieldName); @@ -336,63 +329,29 @@ public function formField(string $fieldName, } /** - * Emit a source control for an MVEA that has a source_foo_id field pointing - * to an External Identity attribute. - * - * @param Entity $entity Entity to emit control for - * - * @return string Source Link - * @since COmanage Registry v5.0.0 + * @return object|null */ - public function sourceLink($entity): string + public function getEntity(): ?object { - // eg: Identifiers - // eg: source_identifier_id, or source_external_identity_role_id - $sourceFK = $this->getView()->get('vv_source_fk'); - // e.g.: source_identifier - we need to construct this from the $sourceFK - $sourceEntityName = substr($sourceFK, 0, strlen($sourceFK) - 3); - // In most cases, $sourceModelName = $modelName, but not for PersonRoles - $sourceModelName = substr(StringUtilities::foreignKeyToClassName($sourceFK), 6); - - $link = ''; - if (!empty($entity->$sourceFK)) { - $link .= $this->Html->Link( - title: __d('controller', $sourceModelName, [1]), - url: [ - 'controller' => $sourceModelName, - 'action' => 'view', - $entity->$sourceFK - ] - ); - } - - if (!empty($entity->$sourceEntityName)) { - $link .= ', ' . $this->Html->Link( - title: __d('controller', 'ExternalIdentities', [1]), - url: [ - 'controller' => 'external_identities', - 'action' => 'view', - $entity->$sourceEntityName->external_identity_id - ] - ); - } - return $link; + return $this->entity; } /** - * @return bool + * @param string $field + * + * @return string|null */ - public function isEditable(): bool + public function getFieldType(string $field): ?string { - return $this->editable; + return $this->fieldTypes[$field] ?? null; } /** - * @return string|null + * @return array */ - public function getPluginName(): ?string + public function getFieldTypes(): array { - return $this->pluginName; + return $this->fieldTypes; } /** @@ -404,11 +363,11 @@ public function getModelName(): ?string } /** - * @return object|null + * @return string|null */ - public function getEntity(): ?object + public function getPluginName(): ?string { - return $this->entity; + return $this->pluginName; } /** @@ -420,33 +379,84 @@ public function getReqFields(): array } /** - * @param string $field + * For the records that have a value like co_x.value, and we want to handle + * the value separate from the field * - * @return bool + * @param string $fieldPrefix e.g. co_2. + * @param string $fieldName e.g. username + * + * @return void */ - public function isReqField(string $field): bool + protected function handlePrefix(string $fieldPrefix, string $fieldName): void { - return \in_array($field, $this->reqFields, true); + // Remove prefix from field value + if(!empty($fieldPrefix) && !empty($this->getEntity()->$fieldName)) { + $fieldValue = $this->getEntity()->$fieldName; + $this->getEntity()->$fieldName = str_replace($fieldPrefix, '', $fieldValue); + $this->getView()->set('vv_obj', $this->getEntity()); + } } - /** - * @return array + * @return bool */ - public function getFieldTypes(): array + public function isEditable(): bool { - return $this->fieldTypes; + return $this->editable; } /** * @param string $field * - * @return string|null + * @return bool */ - public function getFieldType(string $field): ?string + public function isReqField(string $field): bool { - return $this->fieldTypes[$field] ?? null; + return \in_array($field, $this->reqFields, true); } + /** + * Emit a source control for an MVEA that has a source_foo_id field pointing + * to an External Identity attribute. + * + * @param Entity $entity Entity to emit control for + * + * @return string Source Link + * @since COmanage Registry v5.0.0 + */ + public function sourceLink($entity): string + { + // eg: Identifiers + // eg: source_identifier_id, or source_external_identity_role_id + $sourceFK = $this->getView()->get('vv_source_fk'); + // e.g.: source_identifier - we need to construct this from the $sourceFK + $sourceEntityName = substr($sourceFK, 0, strlen($sourceFK) - 3); + // In most cases, $sourceModelName = $modelName, but not for PersonRoles + $sourceModelName = substr(StringUtilities::foreignKeyToClassName($sourceFK), 6); + + $link = ''; + if (!empty($entity->$sourceFK)) { + $link .= $this->Html->Link( + title: __d('controller', $sourceModelName, [1]), + url: [ + 'controller' => $sourceModelName, + 'action' => 'view', + $entity->$sourceFK + ] + ); + } + + if (!empty($entity->$sourceEntityName)) { + $link .= ', ' . $this->Html->Link( + title: __d('controller', 'ExternalIdentities', [1]), + url: [ + 'controller' => 'external_identities', + 'action' => 'view', + $entity->$sourceEntityName->external_identity_id + ] + ); + } + return $link; + } } \ No newline at end of file