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