From a9999eb61ad95fe6a27cf3011057e3a8785763ed Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 8 May 2018 18:46:22 +1200 Subject: [PATCH] Fix Relationship Type form to use the api in postProcess in preparation to support custom data. Now that we have custom-data-on-any forms that use the api to save can potentially support custom data. This is a preparatory tidy-up commit. It switches to the api & does minor tidy ups, including using metadata and addField to add fields. This last part does slightly alter the wording on the form. I think that is OK? --- CRM/Admin/Form/RelationshipType.php | 45 +++++++++++++------------ CRM/Contact/DAO/RelationshipType.php | 17 +++++++++- xml/schema/Contact/RelationshipType.xml | 15 +++++++++ 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/CRM/Admin/Form/RelationshipType.php b/CRM/Admin/Form/RelationshipType.php index 55d3616a69..2c07ed8863 100644 --- a/CRM/Admin/Form/RelationshipType.php +++ b/CRM/Admin/Form/RelationshipType.php @@ -36,6 +36,13 @@ */ class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form { + /** + * Explicitly declare the entity api name. + */ + public function getDefaultEntity() { + return 'RelationshipType'; + } + /** * Build the form object. */ @@ -49,40 +56,32 @@ class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form { $this->applyFilter('__ALL__', 'trim'); - $this->add('text', 'label_a_b', ts('Relationship Label-A to B'), - CRM_Core_DAO::getAttribute('CRM_Contact_DAO_RelationshipType', 'label_a_b'), TRUE - ); + $this->addField('label_a_b'); + $this->addField('label_b_a'); $this->addRule('label_a_b', ts('Label already exists in Database.'), 'objectExists', array('CRM_Contact_DAO_RelationshipType', $this->_id, 'label_a_b') ); - - $this->add('text', 'label_b_a', ts('Relationship Label-B to A'), - CRM_Core_DAO::getAttribute('CRM_Contact_DAO_RelationshipType', 'label_b_a') - ); - $this->addRule('label_b_a', ts('Label already exists in Database.'), 'objectExists', array('CRM_Contact_DAO_RelationshipType', $this->_id, 'label_b_a') ); - $this->add('text', 'description', ts('Description'), - CRM_Core_DAO::getAttribute('CRM_Contact_DAO_RelationshipType', 'description') - ); + $this->addField('description'); $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, '__'); // add select for contact type - $contactTypeA = &$this->add('select', 'contact_types_a', ts('Contact Type A') . ' ', + $this->add('select', 'contact_types_a', ts('Contact Type A') . ' ', array( '' => ts('All Contacts'), ) + $contactTypes ); - $contactTypeB = &$this->add('select', 'contact_types_b', ts('Contact Type B') . ' ', + $this->add('select', 'contact_types_b', ts('Contact Type B') . ' ', array( '' => ts('All Contacts'), ) + $contactTypes ); - $isActive = &$this->add('checkbox', 'is_active', ts('Enabled?')); + $this->addField('is_active'); //only selected field should be allow for edit, CRM-4888 if ($this->_id && @@ -157,20 +156,22 @@ class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form { $params['contact_type_a'] = $cTypeA[0]; $params['contact_type_b'] = $cTypeB[0]; - $params['contact_sub_type_a'] = $cTypeA[1] ? $cTypeA[1] : 'NULL'; - $params['contact_sub_type_b'] = $cTypeB[1] ? $cTypeB[1] : 'NULL'; + $params['contact_sub_type_a'] = $cTypeA[1] ? $cTypeA[1] : 'null'; + $params['contact_sub_type_b'] = $cTypeB[1] ? $cTypeB[1] : 'null'; - // if label B to A is blank, insert the value label A to B for it - if (!strlen(trim(CRM_Utils_Array::value('name_b_a', $params)))) { - $params['name_b_a'] = CRM_Utils_Array::value('name_a_b', $params); - } if (!strlen(trim(CRM_Utils_Array::value('label_b_a', $params)))) { $params['label_b_a'] = CRM_Utils_Array::value('label_a_b', $params); } - $result = CRM_Contact_BAO_RelationshipType::add($params); + if (empty($params['id'])) { + // Set name on created but don't update on update as the machine name is not exposed. + $params['name_b_a'] = CRM_Utils_String::munge($params['label_b_a']); + $params['name_a_b'] = CRM_Utils_String::munge($params['label_a_b']); + } + + $result = civicrm_api3('RelationshipType', 'create', $params); - $this->ajaxResponse['relationshipType'] = $result->toArray(); + $this->ajaxResponse['relationshipType'] = $result['values']; CRM_Core_Session::setStatus(ts('The Relationship Type has been saved.'), ts('Saved'), 'success'); } diff --git a/CRM/Contact/DAO/RelationshipType.php b/CRM/Contact/DAO/RelationshipType.php index 23b8cff703..975a8b2a0c 100644 --- a/CRM/Contact/DAO/RelationshipType.php +++ b/CRM/Contact/DAO/RelationshipType.php @@ -6,7 +6,7 @@ * * Generated from xml/schema/CRM/Contact/RelationshipType.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:8fb00d8376af049ce62bc57ca01bc1bf) + * (GenCodeChecksum:74b3771302922f8c055c64ac67ded44b) */ /** @@ -162,6 +162,9 @@ class CRM_Contact_DAO_RelationshipType extends CRM_Core_DAO { 'entity' => 'RelationshipType', 'bao' => 'CRM_Contact_BAO_RelationshipType', 'localizable' => 1, + 'html' => [ + 'type' => 'Text', + ], ], 'name_b_a' => [ 'name' => 'name_b_a', @@ -186,6 +189,9 @@ class CRM_Contact_DAO_RelationshipType extends CRM_Core_DAO { 'entity' => 'RelationshipType', 'bao' => 'CRM_Contact_BAO_RelationshipType', 'localizable' => 1, + 'html' => [ + 'type' => 'Text', + ], ], 'description' => [ 'name' => 'description', @@ -198,6 +204,9 @@ class CRM_Contact_DAO_RelationshipType extends CRM_Core_DAO { 'entity' => 'RelationshipType', 'bao' => 'CRM_Contact_BAO_RelationshipType', 'localizable' => 1, + 'html' => [ + 'type' => 'Text', + ], ], 'contact_type_a' => [ 'name' => 'contact_type_a', @@ -294,6 +303,9 @@ class CRM_Contact_DAO_RelationshipType extends CRM_Core_DAO { 'entity' => 'RelationshipType', 'bao' => 'CRM_Contact_BAO_RelationshipType', 'localizable' => 0, + 'html' => [ + 'type' => 'CheckBox', + ], ], 'is_active' => [ 'name' => 'is_active', @@ -306,6 +318,9 @@ class CRM_Contact_DAO_RelationshipType extends CRM_Core_DAO { 'entity' => 'RelationshipType', 'bao' => 'CRM_Contact_BAO_RelationshipType', 'localizable' => 0, + 'html' => [ + 'type' => 'CheckBox', + ], ], ]; CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']); diff --git a/xml/schema/Contact/RelationshipType.xml b/xml/schema/Contact/RelationshipType.xml index f530b03211..6fa4b12709 100644 --- a/xml/schema/Contact/RelationshipType.xml +++ b/xml/schema/Contact/RelationshipType.xml @@ -33,6 +33,9 @@ Relationship Type Label A to B 64 label for relationship of contact_a to contact_b. + + Text + 3.0 true @@ -50,6 +53,9 @@ Relationship Type Label B to A 64 Optional label for relationship of contact_b to contact_a. + + Text + 3.0 true @@ -60,6 +66,9 @@ 255 Optional verbose description of the relationship type. 1.1 + + Text + true @@ -137,6 +146,9 @@ boolean Relationship Type is Reserved Is this relationship type a predefined system type (can not be changed or de-activated)? + + CheckBox + 1.1 @@ -146,6 +158,9 @@ 1 Is this relationship type currently active (i.e. can be used when creating or editing relationships)? + + CheckBox + 1.1 -- 2.25.1