Fix unrelease regression - fatal when editing relationship type Employer
[civicrm-core.git] / CRM / Admin / Form / RelationshipType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33
34/**
ce064e4f 35 * This class generates form components for Relationship Type.
6a488035
TO
36 */
37class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form {
38
d84ae5f6 39 use CRM_Core_Form_EntityFormTrait;
40
41 /**
42 * Fields for the entity to be assigned to the template.
43 *
44 * Fields may have keys
45 * - name (required to show in tpl from the array)
46 * - description (optional, will appear below the field)
47 * - not-auto-addable - this class will not attempt to add the field using addField.
48 * (this will be automatically set if the field does not have html in it's metadata
49 * or is not a core field on the form's entity).
50 * - help (option) add help to the field - e.g ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact']]
51 * - template - use a field specific template to render this field
a2fdee87 52 * - required
4d876348 53 * - is_freeze (field should be frozen).
54 *
d84ae5f6 55 * @var array
56 */
57 protected $entityFields = [];
58
59 /**
60 * Set entity fields to be assigned to the form.
61 */
62 protected function setEntityFields() {
63 $this->entityFields = [
64 'label_a_b' => [
65 'name' => 'label_a_b',
a2fdee87 66 'description' => ts("Label for the relationship from Contact A to Contact B. EXAMPLE: Contact A is 'Parent of' Contact B."),
67 'required' => TRUE,
d84ae5f6 68 ],
69 'label_b_a' => [
70 'name' => 'label_b_a',
71 '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).")
72 ],
73 'description' => ['name' => 'description'],
74 'contact_types_a' => ['name' => 'contact_types_a', 'not-auto-addable' => TRUE],
75 'contact_types_b' => ['name' => 'contact_types_b', 'not-auto-addable' => TRUE],
76 'is_active' => ['name' => 'is_active'],
77 ];
78 }
79
80 /**
81 * Deletion message to be assigned to the form.
82 *
83 * @var string
84 */
85 protected $deleteMessage;
86
a9999eb6 87 /**
88 * Explicitly declare the entity api name.
89 */
90 public function getDefaultEntity() {
91 return 'RelationshipType';
92 }
93
d84ae5f6 94 /**
95 * Set the delete message.
96 *
97 * We do this from the constructor in order to do a translation.
98 */
99 public function setDeleteMessage() {
100 $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?');
101 }
102
6a488035 103 /**
eceb18cc 104 * Build the form object.
6a488035
TO
105 */
106 public function buildQuickForm() {
4d876348 107 $isReserved = ($this->_id && CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $this->_id, 'is_reserved'));
108 $this->entityFields['is_active']['is_freeze'] = $isReserved;
6a488035 109
d84ae5f6 110 self::buildQuickEntityForm();
6a488035 111 if ($this->_action & CRM_Core_Action::DELETE) {
6a488035
TO
112 return;
113 }
114
6a488035
TO
115 $this->addRule('label_a_b', ts('Label already exists in Database.'),
116 'objectExists', array('CRM_Contact_DAO_RelationshipType', $this->_id, 'label_a_b')
117 );
6a488035
TO
118 $this->addRule('label_b_a', ts('Label already exists in Database.'),
119 'objectExists', array('CRM_Contact_DAO_RelationshipType', $this->_id, 'label_b_a')
120 );
121
9deb4ed3 122 $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, '__');
4d876348 123 foreach (['contact_types_a' => ts('Contact Type A'), 'contact_types_b' => ts('Contact Type B')] as $name => $label) {
124 $element = $this->add('select', $name, $label . ' ',
125 array(
126 '' => ts('All Contacts'),
127 ) + $contactTypes
128 );
129 if ($isReserved) {
130 $element->freeze();
02fc859b 131 }
6a488035
TO
132 }
133
134 if ($this->_action & CRM_Core_Action::VIEW) {
135 $this->freeze();
6a488035 136 }
f2f99298 137
6a488035
TO
138 }
139
e0ef6999
EM
140 /**
141 * @return array
142 */
00be9182 143 public function setDefaultValues() {
6a488035
TO
144 if ($this->_action != CRM_Core_Action::DELETE &&
145 isset($this->_id)
146 ) {
147 $defaults = $params = array();
148 $params = array('id' => $this->_id);
0e6e8724
DL
149 $baoName = $this->_BAOName;
150 $baoName::retrieve($params, $defaults);
6a488035 151 $defaults['contact_types_a'] = CRM_Utils_Array::value('contact_type_a', $defaults);
a7488080 152 if (!empty($defaults['contact_sub_type_a'])) {
9deb4ed3 153 $defaults['contact_types_a'] .= '__' . $defaults['contact_sub_type_a'];
6a488035
TO
154 }
155
18cb89ba 156 $defaults['contact_types_b'] = CRM_Utils_Array::value('contact_type_b', $defaults);
a7488080 157 if (!empty($defaults['contact_sub_type_b'])) {
9deb4ed3 158 $defaults['contact_types_b'] .= '__' . $defaults['contact_sub_type_b'];
6a488035
TO
159 }
160 return $defaults;
161 }
162 else {
163 return parent::setDefaultValues();
164 }
165 }
166
167 /**
eceb18cc 168 * Process the form submission.
6a488035
TO
169 */
170 public function postProcess() {
171 if ($this->_action & CRM_Core_Action::DELETE) {
172 CRM_Contact_BAO_RelationshipType::del($this->_id);
173 CRM_Core_Session::setStatus(ts('Selected Relationship type has been deleted.'), ts('Record Deleted'), 'success');
174 }
175 else {
6a488035
TO
176 // store the submitted values in an array
177 $params = $this->exportValues();
178 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
179
180 if ($this->_action & CRM_Core_Action::UPDATE) {
780df37b 181 $params['id'] = $this->_id;
6a488035
TO
182 }
183
9deb4ed3 184 $cTypeA = CRM_Utils_System::explode('__',
6a488035
TO
185 $params['contact_types_a'],
186 2
187 );
9deb4ed3 188 $cTypeB = CRM_Utils_System::explode('__',
6a488035
TO
189 $params['contact_types_b'],
190 2
191 );
192
193 $params['contact_type_a'] = $cTypeA[0];
194 $params['contact_type_b'] = $cTypeB[0];
195
a9999eb6 196 $params['contact_sub_type_a'] = $cTypeA[1] ? $cTypeA[1] : 'null';
197 $params['contact_sub_type_b'] = $cTypeB[1] ? $cTypeB[1] : 'null';
6a488035 198
780df37b
CW
199 if (!strlen(trim(CRM_Utils_Array::value('label_b_a', $params)))) {
200 $params['label_b_a'] = CRM_Utils_Array::value('label_a_b', $params);
201 }
202
a9999eb6 203 if (empty($params['id'])) {
204 // Set name on created but don't update on update as the machine name is not exposed.
205 $params['name_b_a'] = CRM_Utils_String::munge($params['label_b_a']);
206 $params['name_a_b'] = CRM_Utils_String::munge($params['label_a_b']);
207 }
208
209 $result = civicrm_api3('RelationshipType', 'create', $params);
4324b8d7 210
a9999eb6 211 $this->ajaxResponse['relationshipType'] = $result['values'];
6a488035
TO
212
213 CRM_Core_Session::setStatus(ts('The Relationship Type has been saved.'), ts('Saved'), 'success');
214 }
215 }
e2046b33 216
6a488035 217}