Commit | Line | Data |
---|---|---|
ee17a760 EM |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
ee17a760 | 5 | | | |
bc77d7c0 TO |
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 | | |
ee17a760 EM |
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 | * | |
e97c66ff | 20 | * @var array |
ee17a760 EM |
21 | */ |
22 | protected $params; | |
23 | ||
24 | /** | |
25 | * Build the form object. | |
26 | */ | |
27 | public function preProcess() { | |
28 | parent::preProcess(); | |
29 | } | |
30 | ||
229b180c | 31 | public function buildQuickForm() { |
32 | $contactType = $this->get('contactType'); | |
be2fb01f CW |
33 | CRM_Utils_System::setTitle(ts('Add Contacts to %1', [1 => $contactType])); |
34 | $this->addElement('text', 'name', ts('Find Target %1', [1 => $contactType])); | |
229b180c | 35 | |
36 | $this->add('select', | |
37 | 'relationship_type_id', | |
38 | ts('Relationship Type'), | |
be2fb01f | 39 | [ |
229b180c | 40 | '' => ts('- select -'), |
be2fb01f | 41 | ] + |
229b180c | 42 | CRM_Contact_BAO_Relationship::getRelationType($contactType), TRUE |
43 | ); | |
44 | ||
45 | $searchRows = $this->get('searchRows'); | |
46 | $searchCount = $this->get('searchCount'); | |
47 | if ($searchRows) { | |
be2fb01f | 48 | $checkBoxes = []; |
229b180c | 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')); | |
1836ab9e | 67 | $this->assign('contact_type_display', $contactType); |
be2fb01f CW |
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([ | |
69078420 SL |
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 | ]); | |
229b180c | 81 | } |
69078420 | 82 | |
ee17a760 EM |
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']); | |
be2fb01f | 93 | $params = [ |
ee17a760 EM |
94 | 'relationship_type_id' => $relationshipTypeParts[0], |
95 | 'is_active' => 1, | |
be2fb01f | 96 | ]; |
ee17a760 EM |
97 | $secondaryRelationshipSide = $relationshipTypeParts[1]; |
98 | $primaryRelationshipSide = $relationshipTypeParts[2]; | |
99 | $primaryFieldName = 'contact_id_' . $primaryRelationshipSide; | |
100 | $secondaryFieldName = 'contact_id_' . $secondaryRelationshipSide; | |
101 | ||
ee17a760 EM |
102 | $relationshipLabel = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', |
103 | $params['relationship_type_id'], "label_{$secondaryRelationshipSide}_{$primaryRelationshipSide}"); | |
104 | ||
ee17a760 EM |
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 | ||
be2fb01f CW |
112 | $status = [ |
113 | ts('%count %2 %3 relationship created', [ | |
ee17a760 EM |
114 | 'count' => $outcome['valid'], |
115 | 'plural' => '%count %2 %3 relationships created', | |
116 | 2 => $relationshipLabel, | |
117 | 3 => $relatedContactName, | |
be2fb01f CW |
118 | ]), |
119 | ]; | |
ee17a760 | 120 | if ($outcome['duplicate']) { |
be2fb01f | 121 | $status[] = ts('%count was skipped because the contact is already %2 %3', [ |
ee17a760 EM |
122 | 'count' => $outcome['duplicate'], |
123 | 'plural' => '%count were skipped because the contacts are already %2 %3', | |
124 | 2 => $relationshipLabel, | |
125 | 3 => $relatedContactName, | |
be2fb01f | 126 | ]); |
ee17a760 EM |
127 | } |
128 | if ($outcome['invalid']) { | |
be2fb01f | 129 | $status[] = ts('%count relationship was not created because the contact is not of the right type for this relationship', [ |
ee17a760 EM |
130 | 'count' => $outcome['invalid'], |
131 | 'plural' => '%count relationships were not created because the contact is not of the right type for this relationship', | |
be2fb01f | 132 | ]); |
ee17a760 EM |
133 | } |
134 | $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>'; | |
be2fb01f | 135 | CRM_Core_Session::setStatus($status, ts('Relationship created.', [ |
ee17a760 EM |
136 | 'count' => $outcome['valid'], |
137 | 'plural' => 'Relationships created.', | |
be2fb01f | 138 | ]), 'success', ['expires' => 0]); |
ee17a760 | 139 | |
ee17a760 EM |
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 | |
be2fb01f | 151 | $searchValues = []; |
ee17a760 EM |
152 | if (!empty($params['rel_contact'])) { |
153 | if (isset($params['rel_contact_id']) && | |
154 | is_numeric($params['rel_contact_id']) | |
155 | ) { | |
be2fb01f | 156 | $searchValues[] = ['contact_id', '=', $params['rel_contact_id'], 0, 1]; |
ee17a760 EM |
157 | } |
158 | else { | |
be2fb01f | 159 | $searchValues[] = ['sort_name', 'LIKE', $params['rel_contact'], 0, 1]; |
ee17a760 EM |
160 | } |
161 | } | |
162 | $contactTypeAdded = FALSE; | |
163 | ||
be2fb01f | 164 | $excludedContactIds = []; |
ee17a760 EM |
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') { | |
be2fb01f | 187 | $searchValues[] = ['contact_type', '=', $type, 0, 0]; |
ee17a760 EM |
188 | $contactTypeAdded = TRUE; |
189 | } | |
190 | ||
191 | if ($subType) { | |
be2fb01f | 192 | $searchValues[] = ['contact_sub_type', '=', $subType, 0, 0]; |
ee17a760 EM |
193 | } |
194 | } | |
195 | } | |
196 | ||
197 | if (!$contactTypeAdded && !empty($params['contact_type'])) { | |
be2fb01f | 198 | $searchValues[] = ['contact_type', '=', $params['contact_type'], 0, 0]; |
ee17a760 EM |
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(); | |
be2fb01f | 211 | $searchRows = []; |
ee17a760 EM |
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 | ||
b92af807 | 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 | ||
ee17a760 | 268 | } |