Skip to content

Commit

Permalink
FieldHelper improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Apr 29, 2024
1 parent 99c73b5 commit 1ba5a7a
Showing 1 changed file with 79 additions and 69 deletions.
148 changes: 79 additions & 69 deletions app/src/View/Helper/FieldHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

}

0 comments on commit 1ba5a7a

Please sign in to comment.