Merge pull request #18183 from colemanw/versionCheck
[civicrm-core.git] / CRM / Contact / Form / Task / AddToParentClass.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This class provides the shared functionality for addToHousehold and addToOrganization.
14 */
15 class CRM_Contact_Form_Task_AddToParentClass extends CRM_Contact_Form_Task {
16
17 /**
18 * Exported parameters from the form.
19 *
20 * @var array
21 */
22 protected $params;
23
24 /**
25 * Build the form object.
26 */
27 public function preProcess() {
28 parent::preProcess();
29 }
30
31 public function buildQuickForm() {
32 $contactType = $this->get('contactType');
33 CRM_Utils_System::setTitle(ts('Add Contacts to %1', [1 => $contactType]));
34 $this->addElement('text', 'name', ts('Find Target %1', [1 => $contactType]));
35
36 $this->add('select',
37 'relationship_type_id',
38 ts('Relationship Type'),
39 [
40 '' => ts('- select -'),
41 ] +
42 CRM_Contact_BAO_Relationship::getRelationType($contactType), TRUE
43 );
44
45 $searchRows = $this->get('searchRows');
46 $searchCount = $this->get('searchCount');
47 if ($searchRows) {
48 $checkBoxes = [];
49 $chekFlag = 0;
50 foreach ($searchRows as $id => $row) {
51 if (!$chekFlag) {
52 $chekFlag = $id;
53 }
54
55 $checkBoxes[$id] = $this->createElement('radio', NULL, NULL, NULL, $id);
56 }
57
58 $this->addGroup($checkBoxes, 'contact_check');
59 if ($chekFlag) {
60 $checkBoxes[$chekFlag]->setChecked(TRUE);
61 }
62 $this->assign('searchRows', $searchRows);
63 }
64
65 $this->assign('searchCount', $searchCount);
66 $this->assign('searchDone', $this->get('searchDone'));
67 $this->assign('contact_type_display', $contactType);
68 $this->addElement('submit', $this->getButtonName('refresh'), ts('Search'), ['class' => 'crm-form-submit']);
69 $this->addElement('submit', $this->getButtonName('cancel'), ts('Cancel'), ['class' => 'crm-form-submit']);
70 $this->addButtons([
71 [
72 'type' => 'next',
73 'name' => ts('Add to %1', [1 => $contactType]),
74 'isDefault' => TRUE,
75 ],
76 [
77 'type' => 'cancel',
78 'name' => ts('Cancel'),
79 ],
80 ]);
81 }
82
83 /**
84 * Add relationships from form.
85 */
86 public function addRelationships() {
87
88 if (!is_array($this->_contactIds)) {
89 // Could this really happen?
90 return;
91 }
92 $relationshipTypeParts = explode('_', $this->params['relationship_type_id']);
93 $params = [
94 'relationship_type_id' => $relationshipTypeParts[0],
95 'is_active' => 1,
96 ];
97 $secondaryRelationshipSide = $relationshipTypeParts[1];
98 $primaryRelationshipSide = $relationshipTypeParts[2];
99 $primaryFieldName = 'contact_id_' . $primaryRelationshipSide;
100 $secondaryFieldName = 'contact_id_' . $secondaryRelationshipSide;
101
102 $relationshipLabel = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType',
103 $params['relationship_type_id'], "label_{$secondaryRelationshipSide}_{$primaryRelationshipSide}");
104
105 $params[$secondaryFieldName] = $this->_contactIds;
106 $params[$primaryFieldName] = $this->params['contact_check'];
107 $outcome = CRM_Contact_BAO_Relationship::createMultiple($params, $primaryRelationshipSide);
108
109 $relatedContactName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params[$primaryFieldName],
110 'display_name');
111
112 $status = [
113 ts('%count %2 %3 relationship created', [
114 'count' => $outcome['valid'],
115 'plural' => '%count %2 %3 relationships created',
116 2 => $relationshipLabel,
117 3 => $relatedContactName,
118 ]),
119 ];
120 if ($outcome['duplicate']) {
121 $status[] = ts('%count was skipped because the contact is already %2 %3', [
122 'count' => $outcome['duplicate'],
123 'plural' => '%count were skipped because the contacts are already %2 %3',
124 2 => $relationshipLabel,
125 3 => $relatedContactName,
126 ]);
127 }
128 if ($outcome['invalid']) {
129 $status[] = ts('%count relationship was not created because the contact is not of the right type for this relationship', [
130 'count' => $outcome['invalid'],
131 'plural' => '%count relationships were not created because the contact is not of the right type for this relationship',
132 ]);
133 }
134 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
135 CRM_Core_Session::setStatus($status, ts('Relationship created.', [
136 'count' => $outcome['valid'],
137 'plural' => 'Relationships created.',
138 ]), 'success', ['expires' => 0]);
139
140 }
141
142 /**
143 * Get the result of the search for Add to * forms.
144 *
145 * @param CRM_Core_Form $form
146 * @param array $params
147 * This contains elements for search criteria.
148 */
149 public function search(&$form, &$params) {
150 //max records that will be listed
151 $searchValues = [];
152 if (!empty($params['rel_contact'])) {
153 if (isset($params['rel_contact_id']) &&
154 is_numeric($params['rel_contact_id'])
155 ) {
156 $searchValues[] = ['contact_id', '=', $params['rel_contact_id'], 0, 1];
157 }
158 else {
159 $searchValues[] = ['sort_name', 'LIKE', $params['rel_contact'], 0, 1];
160 }
161 }
162 $contactTypeAdded = FALSE;
163
164 $excludedContactIds = [];
165 if (isset($form->_contactId)) {
166 $excludedContactIds[] = $form->_contactId;
167 }
168
169 if (!empty($params['relationship_type_id'])) {
170 $relationshipType = new CRM_Contact_DAO_RelationshipType();
171 list($rid, $direction) = explode('_', $params['relationship_type_id'], 2);
172
173 $relationshipType->id = $rid;
174 if ($relationshipType->find(TRUE)) {
175 if ($direction == 'a_b') {
176 $type = $relationshipType->contact_type_b;
177 $subType = $relationshipType->contact_sub_type_b;
178 }
179 else {
180 $type = $relationshipType->contact_type_a;
181 $subType = $relationshipType->contact_sub_type_a;
182 }
183
184 $form->set('contact_type', $type);
185 $form->set('contact_sub_type', $subType);
186 if ($type == 'Individual' || $type == 'Organization' || $type == 'Household') {
187 $searchValues[] = ['contact_type', '=', $type, 0, 0];
188 $contactTypeAdded = TRUE;
189 }
190
191 if ($subType) {
192 $searchValues[] = ['contact_sub_type', '=', $subType, 0, 0];
193 }
194 }
195 }
196
197 if (!$contactTypeAdded && !empty($params['contact_type'])) {
198 $searchValues[] = ['contact_type', '=', $params['contact_type'], 0, 0];
199 }
200
201 // get the count of contact
202 $contactBAO = new CRM_Contact_BAO_Contact();
203 $query = new CRM_Contact_BAO_Query($searchValues);
204 $searchCount = $query->searchQuery(0, 0, NULL, TRUE);
205 $form->set('searchCount', $searchCount);
206 if ($searchCount <= 50) {
207 // get the result of the search
208 $result = $query->searchQuery(0, 50, NULL);
209
210 $config = CRM_Core_Config::singleton();
211 $searchRows = [];
212
213 //variable is set if only one record is foun and that record already has relationship with the contact
214 $duplicateRelationship = 0;
215
216 while ($result->fetch()) {
217 $query->convertToPseudoNames($result);
218 $contactID = $result->contact_id;
219 if (in_array($contactID, $excludedContactIds)) {
220 $duplicateRelationship++;
221 continue;
222 }
223
224 $duplicateRelationship = 0;
225
226 $searchRows[$contactID]['id'] = $contactID;
227 $searchRows[$contactID]['name'] = $result->sort_name;
228 $searchRows[$contactID]['city'] = $result->city;
229 $searchRows[$contactID]['state'] = $result->state_province;
230 $searchRows[$contactID]['email'] = $result->email;
231 $searchRows[$contactID]['phone'] = $result->phone;
232
233 $contact_type = '<img src="' . $config->resourceBase . 'i/contact_';
234
235 $searchRows[$contactID]['type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type
236 );
237 }
238
239 $form->set('searchRows', $searchRows);
240 $form->set('duplicateRelationship', $duplicateRelationship);
241 }
242 else {
243 // resetting the session variables if many records are found
244 $form->set('searchRows', NULL);
245 $form->set('duplicateRelationship', NULL);
246 }
247 }
248
249 /**
250 * Process the form after the input has been submitted and validated.
251 */
252 public function postProcess() {
253 // store the submitted values in an array
254 $this->params = $this->controller->exportValues($this->_name);
255 $this->set('searchDone', 0);
256 $contactType = $this->get('contactType');
257
258 if (!empty($_POST["_qf_AddTo{$contactType}_refresh"])) {
259 $searchParams['contact_type'] = $contactType;
260 $searchParams['rel_contact'] = $this->params['name'];
261 $this->search($this, $searchParams);
262 $this->set('searchDone', 1);
263 return;
264 }
265 $this->addRelationships();
266 }
267
268 }