3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for Relationship Type.
21 class CRM_Admin_Form_RelationshipType
extends CRM_Admin_Form
{
23 use CRM_Core_Form_EntityFormTrait
;
26 * Fields for the entity to be assigned to the template.
28 * Fields may have keys
29 * - name (required to show in tpl from the array)
30 * - description (optional, will appear below the field)
31 * Auto-added by setEntityFieldsMetadata unless specified here (use description => '' to hide)
32 * - not-auto-addable - this class will not attempt to add the field using addField.
33 * (this will be automatically set if the field does not have html in it's metadata
34 * or is not a core field on the form's entity).
35 * - help (option) add help to the field - e.g ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact']]
36 * - template - use a field specific template to render this field
38 * - is_freeze (field should be frozen).
42 protected $entityFields = [];
45 * Set entity fields to be assigned to the form.
47 protected function setEntityFields() {
48 $this->entityFields
= [
50 'name' => 'label_a_b',
51 'description' => ts("Label for the relationship from Contact A to Contact B. EXAMPLE: Contact A is 'Parent of' Contact B."),
55 'name' => 'label_b_a',
56 '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)."),
59 'name' => 'description',
62 'contact_types_a' => ['name' => 'contact_types_a', 'not-auto-addable' => TRUE],
63 'contact_types_b' => ['name' => 'contact_types_b', 'not-auto-addable' => TRUE],
64 'is_active' => ['name' => 'is_active'],
67 self
::setEntityFieldsMetadata();
71 * Deletion message to be assigned to the form.
75 protected $deleteMessage;
78 * Explicitly declare the entity api name.
80 public function getDefaultEntity() {
81 return 'RelationshipType';
85 * Set the delete message.
87 * We do this from the constructor in order to do a translation.
89 public function setDeleteMessage() {
90 $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?');
94 * Build the form object.
96 public function buildQuickForm() {
97 $isReserved = ($this->_id
&& CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType', $this->_id
, 'is_reserved'));
98 $this->entityFields
['is_active']['is_freeze'] = $isReserved;
100 self
::buildQuickEntityForm();
101 if ($this->_action
& CRM_Core_Action
::DELETE
) {
105 $this->addRule('label_a_b', ts('Label already exists in Database.'),
106 'objectExists', ['CRM_Contact_DAO_RelationshipType', $this->_id
, 'label_a_b']
108 $this->addRule('label_b_a', ts('Label already exists in Database.'),
109 'objectExists', ['CRM_Contact_DAO_RelationshipType', $this->_id
, 'label_b_a']
112 $contactTypes = CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, TRUE, '__');
113 foreach (['contact_types_a' => ts('Contact Type A'), 'contact_types_b' => ts('Contact Type B')] as $name => $label) {
114 $element = $this->add('select', $name, $label . ' ',
116 '' => ts('All Contacts'),
124 if ($this->_action
& CRM_Core_Action
::VIEW
) {
133 public function setDefaultValues() {
134 if ($this->_action
!= CRM_Core_Action
::DELETE
&&
137 $defaults = $params = [];
138 $params = ['id' => $this->_id
];
139 $baoName = $this->_BAOName
;
140 $baoName::retrieve($params, $defaults);
141 $defaults['contact_types_a'] = $defaults['contact_type_a'] ??
NULL;
142 if (!empty($defaults['contact_sub_type_a'])) {
143 $defaults['contact_types_a'] .= '__' . $defaults['contact_sub_type_a'];
146 $defaults['contact_types_b'] = $defaults['contact_type_b'] ??
NULL;
147 if (!empty($defaults['contact_sub_type_b'])) {
148 $defaults['contact_types_b'] .= '__' . $defaults['contact_sub_type_b'];
153 return parent
::setDefaultValues();
158 * Process the form submission.
160 public function postProcess() {
161 if ($this->_action
& CRM_Core_Action
::DELETE
) {
162 CRM_Contact_BAO_RelationshipType
::del($this->_id
);
163 CRM_Core_Session
::setStatus(ts('Selected Relationship type has been deleted.'), ts('Record Deleted'), 'success');
166 // store the submitted values in an array
167 $params = $this->exportValues();
168 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
170 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
171 $params['id'] = $this->_id
;
174 $cTypeA = CRM_Utils_System
::explode('__',
175 $params['contact_types_a'],
178 $cTypeB = CRM_Utils_System
::explode('__',
179 $params['contact_types_b'],
183 $params['contact_type_a'] = $cTypeA[0];
184 $params['contact_type_b'] = $cTypeB[0];
186 $params['contact_sub_type_a'] = $cTypeA[1] ?
$cTypeA[1] : 'null';
187 $params['contact_sub_type_b'] = $cTypeB[1] ?
$cTypeB[1] : 'null';
189 if (!strlen(trim(CRM_Utils_Array
::value('label_b_a', $params)))) {
190 $params['label_b_a'] = $params['label_a_b'] ??
NULL;
193 if (empty($params['id'])) {
194 // Set name on created but don't update on update as the machine name is not exposed.
195 $params['name_b_a'] = $params['label_b_a'];
196 $params['name_a_b'] = $params['label_a_b'];
199 $result = civicrm_api3('RelationshipType', 'create', $params);
201 $this->ajaxResponse
['relationshipType'] = $result['values'];
203 CRM_Core_Session
::setStatus(ts('The Relationship Type has been saved.'), ts('Saved'), 'success');