Skip to content

Commit

Permalink
Merge pull request #1528 from younginnovations/1501-review-mandatory-…
Browse files Browse the repository at this point in the history
…field-display-in-interface-for-document-link

Review: 1501-review-mandatory-field-display-in-interface-for-document-link
  • Loading branch information
Sanilblank authored Aug 16, 2024
2 parents 4e6ac28 + d6f384e commit e41103a
Show file tree
Hide file tree
Showing 23 changed files with 1,359 additions and 456 deletions.
43 changes: 43 additions & 0 deletions app/Helpers/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -1367,3 +1367,46 @@ function trimStringValueInArray($array): array
}, $array);
}
}

if (!function_exists('getCollapsableClass')) {
/**
* @param $element
* @param string $sourceForm
*
* @return string
*/
function getCollapsableClass($element, string $sourceForm): string
{
if (!isCollapsable($element)) {
return '';
}

// $elementName = Arr::get($element, 'name', '');

return "collapsable $sourceForm";
}
}

if (!function_exists('isCollapsable')) {
/**
* @param $element
*
* @return bool
*/
function isCollapsable($element): bool
{
return Arr::get($element, 'is_collapsable', false);
}
}

if (!function_exists('canAddMore')) {
/**
* @param $element
*
* @return bool
*/
function canAddMore($element): bool
{
return Arr::get($element, 'add_more', false) || Arr::get($element, 'add_more_attributes', false);
}
}
8 changes: 8 additions & 0 deletions app/IATI/Data/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# NOTES ON ELEMENT JSON SCHEMA PROPERTIES:

1. `{ ... criteria: mandatory ... }` will give red asterisk.
2. `{ ... required: true ... }` wont necessarily mean required validation.
3. `{ ... element_criteria : mandatory ... }` will give `element` core element icon.
4. `{ ... read_only: true ...}` will freeze the element. See `reporting_org` for reference.
5. `{ ... is_collapsable: true ... }` will make the element collapsable. See `document_link` for reference. Will need tweaks on `formbuilder.ts` on issues.
6. `{ ... show_full_help_text: true ... }` will render the actual `help_text` instead of 'Help' text for form fields. See `activity.document_link.url` for reference.
295 changes: 157 additions & 138 deletions app/IATI/Data/elementJsonSchema.json

Large diffs are not rendered by default.

51 changes: 27 additions & 24 deletions app/IATI/Data/organizationElementJsonSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1397,10 +1397,35 @@
}
}
},
"category": {
"name": "category",
"label": "Category",
"add_more": true,
"element_criteria": "mandatory",
"wrapper_collection": false,
"hover_text": "IATI Document Category Code. <a target='_blank' href='https://iatistandard.org/en/iati-standard/203/organisation-standard/iati-organisations/iati-organisation/document-link/category/'>For more information</a>",
"help_text": "",
"attributes": {
"code": {
"name": "code",
"label": "code",
"type": "select",
"multiSelect": false,
"choices": "Organization/DocumentCategory.json",
"placeholder": "Select code",
"required": true,
"criteria": "mandatory",
"hover_text": "An IATI code defining the category of the document. <a target='_blank' href='https://iatistandard.org/en/iati-standard/203/organisation-standard/iati-organisations/iati-organisation/document-link/category/'>For more information</a>",
"help_text": "You must select a category of the document or webpage that you have provided."
}
},
"sub_elements": {}
},
"description": {
"name": "description",
"label": "Description",
"add_more": false,
"is_collapsable": true,
"attributes": {},
"hover_text": "A description of the document contents, or guidance on where to access the relevant information in the document. <a target='_blank' href='https://iatistandard.org/en/iati-standard/203/organisation-standard/iati-organisations/iati-organisation/document-link/description/'>For more information</a>",
"sub_elements": {
Expand Down Expand Up @@ -1435,34 +1460,11 @@
}
}
},
"category": {
"name": "category",
"label": "Category",
"add_more": true,
"element_criteria": "mandatory",
"wrapper_collection": false,
"hover_text": "IATI Document Category Code. <a target='_blank' href='https://iatistandard.org/en/iati-standard/203/organisation-standard/iati-organisations/iati-organisation/document-link/category/'>For more information</a>",
"help_text": "",
"attributes": {
"code": {
"name": "code",
"label": "code",
"type": "select",
"multiSelect": false,
"choices": "Organization/DocumentCategory.json",
"placeholder": "Select code",
"required": true,
"criteria": "mandatory",
"hover_text": "An IATI code defining the category of the document. <a target='_blank' href='https://iatistandard.org/en/iati-standard/203/organisation-standard/iati-organisations/iati-organisation/document-link/category/'>For more information</a>",
"help_text": "You must select a category of the document or webpage that you have provided."
}
},
"sub_elements": {}
},
"language": {
"name": "language",
"label": "Language",
"add_more": true,
"is_collapsable": true,
"wrapper_collection": false,
"hover_text": "The ISO 639-1 language code in which target document is written, e.g. “en”. Can be repeated to describe multi-lingual documents. <a target='_blank' href='https://iatistandard.org/en/iati-standard/203/organisation-standard/iati-organisations/iati-organisation/document-link/language/'>For more information</a>",
"attributes": {
Expand All @@ -1485,6 +1487,7 @@
"name": "document_date",
"label": "document date",
"add_more": false,
"is_collapsable": true,
"hover_text": "The date of publication of the document that is being linked to. <a target='_blank' href='https://iatistandard.org/en/iati-standard/203/organisation-standard/iati-organisations/iati-organisation/document-link/document-date/'>For more information</a>",
"attributes": {
"date": {
Expand Down
113 changes: 74 additions & 39 deletions app/IATI/Elements/Forms/BaseForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function buildForm(): void
{
$this->setClientValidationEnabled(false);
$element = $this->getData();

$attributes = Arr::get($element, 'attributes', null);
$sub_elements = Arr::get($element, 'sub_elements', null);

Expand All @@ -32,9 +31,9 @@ public function buildForm(): void
)) {
$this->buildCollection($attributes);
} else {
foreach ($attributes as $attribute) {
foreach ($attributes as $i => $attribute) {
if (is_array($attribute)) {
$this->buildField($attribute);
$this->buildField($attribute, $i);
}
}

Expand All @@ -49,7 +48,7 @@ public function buildForm(): void
}

if ($sub_elements) {
foreach ($sub_elements as $sub_element) {
foreach ($sub_elements as $i => $sub_element) {
$this->buildCollection($sub_element);

if (Arr::get($element, 'add_more', false) && Arr::get($sub_element, 'add_more', false)) {
Expand All @@ -71,12 +70,11 @@ public function buildForm(): void
public function buildCollection($field): void
{
$element = $this->getData();
$hasFieldType = Arr::get($field, 'type', null);
$hasSubElements = array_key_exists('sub_elements', $field);
$isWrapperCollection = Arr::get($field, 'wrapper_collection', true);

if (!Arr::get($field, 'type', null) && array_key_exists('sub_elements', $field) && Arr::get(
$field,
'wrapper_collection',
true
)) {
if (!$hasFieldType && $hasSubElements && $isWrapperCollection) {
$field['parent'] = $element['name'];
$this->add(
$field['name'],
Expand All @@ -94,20 +92,8 @@ public function buildCollection($field): void
'hover_text' => Arr::get($field, 'hover_text', ''),
'help_text' => Arr::get($field, 'help_text', ''),
'helper_text' => Arr::get($field, 'helper_text', ''),
'wrapper' => [
'class' => 'wrapped-child-body',
],
'dynamic_wrapper' => [
'class' => (isset($field['add_more']) && $field['add_more']) ?
((!Arr::get($element, 'attributes', null) && strtolower(
$field['name']
) === 'narrative') && !strtolower($element['name']) == 'mailing_address' ? 'border-l border-spring-50 pb-11' : 'subelement rounded-tl-lg border-l border-spring-50 pb-11')
: ((!Arr::get(
$field,
'attributes',
null
) && $field['sub_elements'] && isset($field['sub_elements']['narrative'])) ? 'subelement rounded-tl-lg mb-6' : 'subelement rounded-tl-lg border-l border-spring-50 mb-6'),
],
'wrapper' => ['class' => $this->getWrapperCollectionFormWrapperClasses()],
'dynamic_wrapper' => ['class' => $this->getWrapperCollectionFormDynamicWrapperClasses($field, $element)],
],
]
);
Expand Down Expand Up @@ -139,22 +125,8 @@ public function buildCollection($field): void
'element_criteria' => $field['element_criteria'] ?? '',
'hover_text' => isset($field['name']) ? Arr::get($field, 'hover_text', '') : Arr::get($element, 'hover_text', ''),
'help_text' => isset($field['name']) ? Arr::get($field, 'help_text', '') : Arr::get($element, 'help_text', ''),
'wrapper' => [
'class' => ((Arr::get($element, 'attributes', null) && isset($field['name']) && strtolower(
$field['name']
) === 'narrative') ? 'form-field-group form-child-body xl:flex flex-wrap rounded-tl-lg rounded-br-lg border-y border-r border-spring-50 p-6' : 'form-field-group form-child-body xl:flex flex-wrap rounded-br-lg border-y border-r border-spring-50 p-6'),
],
'dynamic_wrapper' => [
'class' => (((isset($field['add_more']) && $field['add_more']) || Arr::get(
$element,
'add_more_attributes',
false
)) ?
(!Arr::get($element, 'attributes', null) && strtolower(
$field['name']
) === 'narrative' ? 'border-l border-spring-50 pb-11' : 'subelement rounded-tl-lg border-l border-spring-50 pb-11')
: 'subelement rounded-tl-lg border-l border-spring-50 mb-6') . (Arr::get($field, 'read_only', false) ? ' freeze' : ''),
],
'wrapper' => ['class' => $this->getSubElementFormWrapperClasses($field, $element)],
'dynamic_wrapper' => ['class' => $this->getSubElementFormDynamicWrapperClasses($field, $element)],
'overRideDefaultFieldValue' => $element['overRideDefaultFieldValue'] ?? [],
],
]
Expand Down Expand Up @@ -191,6 +163,7 @@ public function buildField($field): void
'help_block' => [
'text' => $field['help_text'] ?? '',
'title' => $field['label'],
'show_full_help_text'=>Arr::get($field, 'show_full_help_text', ''),
],
'hover_block' => [
'title' => $field['label'],
Expand Down Expand Up @@ -308,4 +281,66 @@ public function getCodeList(string $filePath, bool $code = true, $deprecationSta

return $data;
}

private function getWrapperCollectionFormWrapperClasses(): string
{
return 'wrapped-child-body';
}

private function getWrapperCollectionFormDynamicWrapperClasses($field, $element): string
{
$fieldHasAddMoreButton = isset($field['add_more']) && $field['add_more'];
$isSubElementNarrative = isset($field['name']) && strtolower($field['name']) === 'narrative';
$elementHasAttributes = Arr::get($element, 'attributes', null);
$isMailingAddressElement = strtolower($element['name']) == 'mailing_address';
$collapsableClass = getCollapsableClass($field, 'base-form', $element);

if ($fieldHasAddMoreButton) {
if (!$elementHasAttributes && $isSubElementNarrative && !$isMailingAddressElement) {
return "border-l border-spring-50 pb-11 $collapsableClass";
}

return "subelement rounded-tl-lg border-l border-spring-50 pb-11 $collapsableClass";
}

$fieldHasAttributes = Arr::get($field, 'attributes', null);
$hasNarrativeSubElement = isset($field['sub_elements']['narrative']);

if (!$fieldHasAttributes && $field['sub_elements'] && $hasNarrativeSubElement) {
return "subelement rounded-tl-lg mb-6 $collapsableClass";
}

return "subelement rounded-tl-lg border-l border-spring-50 mb-6 $collapsableClass";
}

private function getSubElementFormWrapperClasses($field, $element): string
{
$elementHasAttributes = Arr::get($element, 'attributes', null);
$isSubElementNarrative = isset($field['name']) && strtolower($field['name']) === 'narrative';

return $elementHasAttributes && $isSubElementNarrative
? 'form-field-group form-child-body xl:flex flex-wrap rounded-tl-lg rounded-br-lg border-y border-r border-spring-50 p-6'
: 'form-field-group form-child-body xl:flex flex-wrap rounded-br-lg border-y border-r border-spring-50 p-6';
}

private function getSubElementFormDynamicWrapperClasses($field, $element): string
{
$hasAddMoreButton = isset($field['add_more']) && $field['add_more'];
$canAddMoreAttributes = Arr::get($element, 'add_more_attributes', false);
$elementHasAttributes = Arr::get($element, 'attributes', null);
$isSubElementNarrative = isset($field['name']) && strtolower($field['name']) === 'narrative';
$collapsableClass = getCollapsableClass($field, 'base-form', $element);

if ($hasAddMoreButton || $canAddMoreAttributes) {
if (!$elementHasAttributes && $isSubElementNarrative) {
return "border-l border-spring-50 pb-11 $collapsableClass";
}

return "subelement rounded-tl-lg border-l border-spring-50 pb-11 $collapsableClass";
}

$frozenClass = Arr::get($field, 'read_only', false) ? 'freeze' : '';

return "subelement rounded-tl-lg border-l border-spring-50 mb-6 $frozenClass $collapsableClass";
}
}
55 changes: 36 additions & 19 deletions app/IATI/Elements/Forms/MultilevelSubElementForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,20 @@ public function buildForm(): void
$this->getData(sprintf('sub_elements.%s.name', $name)),
'collection',
[
'type' => 'form',
'property' => 'name',
'prototype' => true,
'type' => 'form',
'property' => 'name',
'prototype' => true,
'prototype_name' => '__PARENT_NAME__',
'options' => [
'class' => 'App\IATI\Elements\Forms\BaseForm',
'data' => $this->getData(sprintf('sub_elements.%s', $name)),
'label' => false,
'options' => [
'class' => 'App\IATI\Elements\Forms\BaseForm',
'data' => $this->getData(sprintf('sub_elements.%s', $name)),
'label' => false,
'element_criteria' => $this->getData(sprintf('sub_elements.%s.element_criteria', $name)),
'hover_text' => $this->getData(sprintf('sub_elements.%s.hover_text', $name)) ?? '',
'help_text' => $this->getData(sprintf('sub_elements.%s.help_text', $name)) ?? '',
'helper_text' => $this->getData(sprintf('sub_elements.%s.helper_text', $name)) ?? '',

'wrapper' => [
'class' => 'multi-form relative',
],
'dynamic_wrapper' => [
'class' => (isset($sub_element['add_more']) && $sub_element['add_more'] || Arr::get($element, 'add_more_attributes', false)) ?
(strtolower($sub_element['name']) === 'narrative' && !isset($sub_element['attributes']) && !count($sub_element['attributes']) > 0 ? 'border-l border-spring-50 pb-11' : 'subelement rounded-tl-lg border-l border-spring-50 pb-11')
: 'subelement rounded-tl-lg border-l border-spring-50 mb-6',
], ],
'hover_text' => $this->getData(sprintf('sub_elements.%s.hover_text', $name)) ?? '',
'help_text' => $this->getData(sprintf('sub_elements.%s.help_text', $name)) ?? '',
'helper_text' => $this->getData(sprintf('sub_elements.%s.helper_text', $name)) ?? '',
'wrapper' => ['class' => $this->getBaseFormWrapperClasses()],
'dynamic_wrapper' => ['class' => $this->getBaseFormDynamicWrapperClasses($sub_element, $element)]],
]
)->add('add_to_collection', 'button', [
'label' => generateAddAdditionalLabel($element['name'], $this->getData(sprintf('sub_elements.%s.name', $name))),
Expand All @@ -73,4 +66,28 @@ public function buildForm(): void
}
}
}

private function getBaseFormWrapperClasses(): string
{
return 'multi-form relative';
}

private function getBaseFormDynamicWrapperClasses($sub_element, $element): string
{
$hasAddMoreButton = isset($sub_element['add_more']) && $sub_element['add_more'];
$canAddMoreAttributes = Arr::get($element, 'add_more_attributes', false);
$elementHasAttributes = isset($sub_element['attributes']) && !count($sub_element['attributes']) > 0;
$isSubElementNarrative = isset($sub_element['name']) && strtolower($sub_element['name']) === 'narrative';
$collapsableClass = getCollapsableClass($element, 'multi-level-form');

if ($hasAddMoreButton || $canAddMoreAttributes) {
if ($isSubElementNarrative && !$elementHasAttributes) {
return "border-l border-spring-50 pb-11 border-reed $collapsableClass";
}

return "subelement rounded-tl-lg border-l border-spring-50 pb-11 $collapsableClass";
}

return "subelement rounded-tl-lg border-l border-spring-50 mb-6 $collapsableClass";
}
}
Loading

0 comments on commit e41103a

Please sign in to comment.