Merge pull request #17956 from eileenmcnaughton/export_phone
[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 $buttonAttrs = [
69 'type' => 'submit',
70 'class' => 'crm-form-submit',
71 'value' => 1,
72 ];
73 $this->addElement('xbutton', $this->getButtonName('refresh'), ts('Search'), $buttonAttrs);
74 $this->addElement('xbutton', $this->getButtonName('cancel'), ts('Cancel'), $buttonAttrs);
75 $this->addButtons([
76 [
77 'type' => 'next',
78 'name' => ts('Add to %1', [1 => $contactType]),
79 'isDefault' => TRUE,
80 ],
81 [
82 'type' => 'cancel',
83 'name' => ts('Cancel'),
84 ],
85 ]);
86 }
87
88 /**
89 * Add relationships from form.
90 */
91 public function addRelationships() {
92
93 if (!is_array($this->_contactIds)) {
94 // Could this really happen?
95 return;
96 }
97 $relationshipTypeParts = explode('_', $this->params['relationship_type_id']);
98 $params = [
99 'relationship_type_id' => $relationshipTypeParts[0],
100 'is_active' => 1,
101 ];
102 $secondaryRelationshipSide = $relationshipTypeParts[1];
103 $primaryRelationshipSide = $relationshipTypeParts[2];
104 $primaryFieldName = 'contact_id_' . $primaryRelationshipSide;
105 $secondaryFieldName = 'contact_id_' . $secondaryRelationshipSide;
106
107 $relationshipLabel = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType',
108 $params['relationship_type_id'], "label_{$secondaryRelationshipSide}_{$primaryRelationshipSide}");
109
110 $params[$secondaryFieldName] = $this->_contactIds;
111 $params[$primaryFieldName] = $this->params['contact_check'];
112 $outcome = CRM_Contact_BAO_Relationship::createMultiple($params, $primaryRelationshipSide);
113
114 $relatedContactName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params[$primaryFieldName],
115 'display_name');
116
117 $status = [
118 ts('%count %2 %3 relationship created', [
119 'count' => $outcome['valid'],
120 'plural' => '%count %2 %3 relationships created',
121 2 => $relationshipLabel,
122 3 => $relatedContactName,
123 ]),
124 ];
125 if ($outcome['duplicate']) {
126 $status[] = ts('%count was skipped because the contact is already %2 %3', [
127 'count' => $outcome['duplicate'],
128 'plural' => '%count were skipped because the contacts are already %2 %3',
129 2 => $relationshipLabel,
130 3 => $relatedContactName,
131 ]);
132 }
133 if ($outcome['invalid']) {
134 $status[] = ts('%count relationship was not created because the contact is not of the right type for this relationship', [
135 'count' => $outcome['invalid'],
136 'plural' => '%count relationships were not created because the contact is not of the right type for this relationship',
137 ]);
138 }
139 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
140 CRM_Core_Session::setStatus($status, ts('Relationship created.', [
141 'count' => $outcome['valid'],
142 'plural' => 'Relationships created.',
143 ]), 'success', ['expires' => 0]);
144
145 }
146
147 /**
148 * Get the result of the search for Add to * forms.
149 *
150 * @param CRM_Core_Form $form
151 * @param array $params
152 * This contains elements for search criteria.
153 */
154 public function search(&$form, &$params) {
155 //max records that will be listed
156 $searchValues = [];
157 if (!empty($params['rel_contact'])) {
158 if (isset($params['rel_contact_id']) &&
159 is_numeric($params['rel_contact_id'])
160 ) {
161 $searchValues[] = ['contact_id', '=', $params['rel_contact_id'], 0, 1];
162 }
163 else {
164 $searchValues[] = ['sort_name', 'LIKE', $params['rel_contact'], 0, 1];
165 }
166 }
167 $contactTypeAdded = FALSE;
168
169 $excludedContactIds = [];
170 if (isset($form->_contactId)) {
171 $excludedContactIds[] = $form->_contactId;
172 }
173
174 if (!empty($params['relationship_type_id'])) {
175 $relationshipType = new CRM_Contact_DAO_RelationshipType();
176 list($rid, $direction) = explode('_', $params['relationship_type_id'], 2);
177
178 $relationshipType->id = $rid;
179 if ($relationshipType->find(TRUE)) {
180 if ($direction == 'a_b') {
181 $type = $relationshipType->contact_type_b;
182 $subType = $relationshipType->contact_sub_type_b;
183 }
184 else {
185 $type = $relationshipType->contact_type_a;
186 $subType = $relationshipType->contact_sub_type_a;
187 }
188
189 $form->set('contact_type', $type);
190 $form->set('contact_sub_type', $subType);
191 if ($type == 'Individual' || $type == 'Organization' || $type == 'Household') {
192 $searchValues[] = ['contact_type', '=', $type, 0, 0];
193 $contactTypeAdded = TRUE;
194 }
195
196 if ($subType) {
197 $searchValues[] = ['contact_sub_type', '=', $subType, 0, 0];
198 }
199 }
200 }
201
202 if (!$contactTypeAdded && !empty($params['contact_type'])) {
203 $searchValues[] = ['contact_type', '=', $params['contact_type'], 0, 0];
204 }
205
206 // get the count of contact
207 $contactBAO = new CRM_Contact_BAO_Contact();
208 $query = new CRM_Contact_BAO_Query($searchValues);
209 $searchCount = $query->searchQuery(0, 0, NULL, TRUE);
210 $form->set('searchCount', $searchCount);
211 if ($searchCount <= 50) {
212 // get the result of the search
213 $result = $query->searchQuery(0, 50, NULL);
214
215 $config = CRM_Core_Config::singleton();
216 $searchRows = [];
217
218 //variable is set if only one record is foun and that record already has relationship with the contact
219 $duplicateRelationship = 0;
220
221 while ($result->fetch()) {
222 $query->convertToPseudoNames($result);
223 $contactID = $result->contact_id;
224 if (in_array($contactID, $excludedContactIds)) {
225 $duplicateRelationship++;
226 continue;
227 }
228
229 $duplicateRelationship = 0;
230
231 $searchRows[$contactID]['id'] = $contactID;
232 $searchRows[$contactID]['name'] = $result->sort_name;
233 $searchRows[$contactID]['city'] = $result->city;
234 $searchRows[$contactID]['state'] = $result->state_province;
235 $searchRows[$contactID]['email'] = $result->email;
236 $searchRows[$contactID]['phone'] = $result->phone;
237
238 $contact_type = '<img src="' . $config->resourceBase . 'i/contact_';
239
240 $searchRows[$contactID]['type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type
241 );
242 }
243
244 $form->set('searchRows', $searchRows);
245 $form->set('duplicateRelationship', $duplicateRelationship);
246 }
247 else {
248 // resetting the session variables if many records are found
249 $form->set('searchRows', NULL);
250 $form->set('duplicateRelationship', NULL);
251 }
252 }
253
254 /**
255 * Process the form after the input has been submitted and validated.
256 */
257 public function postProcess() {
258 // store the submitted values in an array
259 $this->params = $this->controller->exportValues($this->_name);
260 $this->set('searchDone', 0);
261 $contactType = $this->get('contactType');
262
263 if (!empty($_POST["_qf_AddTo{$contactType}_refresh"])) {
264 $searchParams['contact_type'] = $contactType;
265 $searchParams['rel_contact'] = $this->params['name'];
266 $this->search($this, $searchParams);
267 $this->set('searchDone', 1);
268 return;
269 }
270 $this->addRelationships();
271 }
272
273 }