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