From 48c242dc7c4935e7c4f4aade8a7554aabd7bf95a Mon Sep 17 00:00:00 2001 From: "Matthew Wire (MJW Consulting)" Date: Tue, 12 Feb 2019 10:59:03 +0000 Subject: [PATCH] Towards supporting EntityForm for 'View Action' --- CRM/Core/Form/EntityFormTrait.php | 28 ++++++++++++++++++++++++---- CRM/Custom/Form/CustomData.php | 17 ++++++++++++----- templates/CRM/Core/Form/Field.tpl | 2 +- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/CRM/Core/Form/EntityFormTrait.php b/CRM/Core/Form/EntityFormTrait.php index 102393d4b569..06eafaaba005 100644 --- a/CRM/Core/Form/EntityFormTrait.php +++ b/CRM/Core/Form/EntityFormTrait.php @@ -114,6 +114,10 @@ public function buildQuickEntityForm() { $this->assign('entityTable', CRM_Core_DAO_AllCoreTables::getTableForClass(CRM_Core_DAO_AllCoreTables::getFullName($this->getDefaultEntity()))); $this->addCustomDataToForm(); $this->addFormButtons(); + + if ($this->isViewContext()) { + $this->freeze(); + } } /** @@ -128,7 +132,7 @@ protected function buildDeleteForm() { * Add relevant buttons to the form. */ protected function addFormButtons() { - if ($this->_action & CRM_Core_Action::VIEW || $this->_action & CRM_Core_Action::PREVIEW) { + if ($this->isViewContext() || $this->_action & CRM_Core_Action::PREVIEW) { $this->addButtons(array( array( 'type' => 'cancel', @@ -142,7 +146,7 @@ protected function addFormButtons() { $this->addButtons(array( array( 'type' => 'next', - 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'), + 'name' => $this->isDeleteContext() ? ts('Delete') : ts('Save'), 'isDefault' => TRUE, ), array( @@ -159,19 +163,26 @@ protected function addFormButtons() { */ protected function getEntityDefaults() { $defaults = []; - if ($this->_action != CRM_Core_Action::DELETE && + if (!$this->isDeleteContext() && $this->getEntityId() ) { $params = ['id' => $this->getEntityId()]; $baoName = $this->_BAOName; $baoName::retrieve($params, $defaults); } - foreach ($this->entityFields as $fieldSpec) { + foreach ($this->entityFields as &$fieldSpec) { $value = CRM_Utils_Request::retrieveValue($fieldSpec['name'], $this->getValidationTypeForField($fieldSpec['name'])); if ($value !== FALSE && $value !== NULL) { $defaults[$fieldSpec['name']] = $value; } + if (CRM_Utils_Array::value('formatter', $fieldSpec) == 'crmMoney') { + if (!empty($defaults['currency'])) { + $fieldSpec['formatterParam'] = $defaults['currency']; + } + } } + // Assign again as we may have modified above + $this->assign('entityFields', $this->entityFields); return $defaults; } @@ -244,6 +255,15 @@ protected function isDeleteContext() { return ($this->_action & CRM_Core_Action::DELETE); } + /** + * Is the form being used in the context of a view. + * + * @return bool + */ + protected function isViewContext() { + return ($this->_action & CRM_Core_Action::VIEW); + } + protected function setEntityFieldsMetadata() { foreach ($this->entityFields as $field => &$props) { if (!empty($props['not-auto-addable'])) { diff --git a/CRM/Custom/Form/CustomData.php b/CRM/Custom/Form/CustomData.php index 532a11ef8181..d2a644f7ff18 100644 --- a/CRM/Custom/Form/CustomData.php +++ b/CRM/Custom/Form/CustomData.php @@ -63,11 +63,18 @@ public static function addToForm(&$form, $subType = NULL, $subName = NULL, $grou $entitySubType = $form->getEntitySubTypeId($subType); } - // when custom data is included in this page - if (!empty($_POST['hidden_custom'])) { - self::preProcess($form, $subName, $entitySubType, $groupCount, $entityName, $entityID); - self::buildQuickForm($form); - self::setDefaultValues($form); + if ($form->getAction() == CRM_Core_Action::VIEW) { + // Viewing custom data (Use with {include file="CRM/Custom/Page/CustomDataView.tpl"} in template) + $groupTree = CRM_Core_BAO_CustomGroup::getTree($entityName, NULL, $entityID, 0, $entitySubType); + CRM_Core_BAO_CustomGroup::buildCustomDataView($form, $groupTree, FALSE, NULL, NULL, NULL, $entityID); + } + else { + // Editing custom data (Use with {include file="CRM/common/customDataBlock.tpl"} in template) + if (!empty($_POST['hidden_custom'])) { + self::preProcess($form, $subName, $entitySubType, $groupCount, $entityName, $entityID); + self::buildQuickForm($form); + self::setDefaultValues($form); + } } // need to assign custom data type and subtype to the template $form->assign('customDataType', $entityName); diff --git a/templates/CRM/Core/Form/Field.tpl b/templates/CRM/Core/Form/Field.tpl index 210454b5751d..cc2a90472f06 100644 --- a/templates/CRM/Core/Form/Field.tpl +++ b/templates/CRM/Core/Form/Field.tpl @@ -33,7 +33,7 @@ {/capture}{help id=$help.id file=$help.file}{/if} {if $action == 2 && $fieldSpec.is_add_translate_dialog}{include file='CRM/Core/I18n/Dialog.tpl' table=$entityTable field=$fieldName id=$entityID}{/if} - {$fieldSpec.pre_html_text}{if $form.$fieldName.html}{if $fieldSpec.formatter === 'crmMoney'}{$form.$fieldName.html|crmMoney}{else}{$form.$fieldName.html}{/if}{else}{$fieldSpec.place_holder}{/if}{$fieldSpec.post_html_text}
+ {$fieldSpec.pre_html_text}{if $form.$fieldName.html}{if $fieldSpec.formatter === 'crmMoney'}{$form.$fieldName.html|crmMoney:$fieldSpec.formatterParam}{else}{$form.$fieldName.html}{/if}{else}{$fieldSpec.place_holder}{/if}{$fieldSpec.post_html_text}
{if $fieldSpec.description}{$fieldSpec.description}{/if} {if $fieldSpec.documentation_link}{docURL page=$fieldSpec.documentation_link.page resource=$fieldSpec.documentation_link.resource}{/if}