X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FAdmin%2FForm%2FRelationshipType.php;h=9f939d2bbb531cca0fd745447c00679bfa3ec9a9;hb=f1195cae6d39dfe0dfa53a424add819afa0dca52;hp=b74ba659b8ede3c4e6b15e4ebb801114b53b2f4f;hpb=12ec5bb5bfe8d3ae7a2ec327f65e521ccf233647;p=civicrm-core.git diff --git a/CRM/Admin/Form/RelationshipType.php b/CRM/Admin/Form/RelationshipType.php index b74ba659b8..fde8d4c3b3 100644 --- a/CRM/Admin/Form/RelationshipType.php +++ b/CRM/Admin/Form/RelationshipType.php @@ -1,9 +1,9 @@ 'id-source', 'file' => 'CRM/Contact/Form/Contact']] + * - template - use a field specific template to render this field + * - required + * - is_freeze (field should be frozen). + * + * @var array + */ + protected $entityFields = []; + + /** + * Set entity fields to be assigned to the form. + */ + protected function setEntityFields() { + $this->entityFields = [ + 'label_a_b' => [ + 'name' => 'label_a_b', + 'description' => ts("Label for the relationship from Contact A to Contact B. EXAMPLE: Contact A is 'Parent of' Contact B."), + 'required' => TRUE, + ], + 'label_b_a' => [ + 'name' => 'label_b_a', + 'description' => ts("Label for the relationship from Contact B to Contact A. EXAMPLE: Contact B is 'Child of' Contact A. You may leave this blank for relationships where the name is the same in both directions (e.g. Spouse).") + ], + 'description' => ['name' => 'description'], + 'contact_types_a' => ['name' => 'contact_types_a', 'not-auto-addable' => TRUE], + 'contact_types_b' => ['name' => 'contact_types_b', 'not-auto-addable' => TRUE], + 'is_active' => ['name' => 'is_active'], + ]; + } + + /** + * Deletion message to be assigned to the form. + * + * @var string + */ + protected $deleteMessage; + + /** + * Explicitly declare the entity api name. + */ + public function getDefaultEntity() { + return 'RelationshipType'; + } + + /** + * Set the delete message. + * + * We do this from the constructor in order to do a translation. + */ + public function setDeleteMessage() { + $this->deleteMessage = ts('WARNING: Deleting this option will result in the loss of all Relationship records of this type.') . ts('This may mean the loss of a substantial amount of data, and the action cannot be undone.') . ts('Do you want to continue?'); + } + /** * Build the form object. */ public function buildQuickForm() { - parent::buildQuickForm(); - $this->setPageTitle(ts('Relationship Type')); + $isReserved = ($this->_id && CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $this->_id, 'is_reserved')); + $this->entityFields['is_active']['is_freeze'] = $isReserved; + self::buildQuickEntityForm(); if ($this->_action & CRM_Core_Action::DELETE) { return; } - $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->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') - ); - $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, '__'); - - // add select for contact type - $contactTypeA = &$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') . ' ', - array( - '' => ts('All Contacts'), - ) + $contactTypes - ); - - $isActive = &$this->add('checkbox', 'is_active', ts('Enabled?')); - - //only selected field should be allow for edit, CRM-4888 - if ($this->_id && - CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $this->_id, 'is_reserved') - ) { - foreach (array('contactTypeA', 'contactTypeB', 'isActive') as $field) { - $$field->freeze(); + foreach (['contact_types_a' => ts('Contact Type A'), 'contact_types_b' => ts('Contact Type B')] as $name => $label) { + $element = $this->add('select', $name, $label . ' ', + array( + '' => ts('All Contacts'), + ) + $contactTypes + ); + if ($isReserved) { + $element->freeze(); } } @@ -97,8 +135,6 @@ class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form { $this->freeze(); } - $this->assign('relationship_type_id', $this->_id); - } /** @@ -117,7 +153,7 @@ class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form { $defaults['contact_types_a'] .= '__' . $defaults['contact_sub_type_a']; } - $defaults['contact_types_b'] = $defaults['contact_type_b']; + $defaults['contact_types_b'] = CRM_Utils_Array::value('contact_type_b', $defaults); if (!empty($defaults['contact_sub_type_b'])) { $defaults['contact_types_b'] .= '__' . $defaults['contact_sub_type_b']; } @@ -137,14 +173,12 @@ class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form { CRM_Core_Session::setStatus(ts('Selected Relationship type has been deleted.'), ts('Record Deleted'), 'success'); } else { - $ids = array(); - // store the submitted values in an array $params = $this->exportValues(); $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); if ($this->_action & CRM_Core_Action::UPDATE) { - $ids['relationshipType'] = $this->_id; + $params['id'] = $this->_id; } $cTypeA = CRM_Utils_System::explode('__', @@ -159,12 +193,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 (!strlen(trim(CRM_Utils_Array::value('label_b_a', $params)))) { + $params['label_b_a'] = CRM_Utils_Array::value('label_a_b', $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 = CRM_Contact_BAO_RelationshipType::add($params, $ids); + $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'); }