Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 | 32 | * $Id$ |
6a488035 TO |
33 | */ |
34 | ||
35 | /** | |
36 | * This class generates form components for search-result tasks | |
6a488035 TO |
37 | */ |
38 | class CRM_Contact_Form_Task extends CRM_Core_Form { | |
39 | ||
40 | /** | |
100fef9d | 41 | * The task being performed |
6a488035 TO |
42 | * |
43 | * @var int | |
44 | */ | |
45 | protected $_task; | |
46 | ||
47 | /** | |
48 | * The array that holds all the contact ids | |
49 | * | |
50 | * @var array | |
51 | */ | |
52 | public $_contactIds; | |
53 | ||
54 | /** | |
55 | * The array that holds all the contact types | |
56 | * | |
57 | * @var array | |
58 | */ | |
59 | public $_contactTypes; | |
60 | ||
61 | /** | |
62 | * The additional clause that we restrict the search with | |
63 | * | |
64 | * @var string | |
65 | */ | |
66 | protected $_componentClause = NULL; | |
67 | ||
68 | /** | |
69 | * The name of the temp table where we store the contact IDs | |
70 | * | |
71 | * @var string | |
72 | */ | |
73 | protected $_componentTable = NULL; | |
74 | ||
75 | /** | |
76 | * The array that holds all the component ids | |
77 | * | |
78 | * @var array | |
79 | */ | |
80 | protected $_componentIds; | |
81 | ||
82 | /** | |
83 | * This includes the submitted values of the search form | |
84 | */ | |
85 | static protected $_searchFormValues; | |
86 | ||
87 | /** | |
d8689418 | 88 | * Build all the data structures needed to build the form. |
8ef12e64 | 89 | */ |
00be9182 | 90 | public function preProcess() { |
6a488035 TO |
91 | self::preProcessCommon($this); |
92 | } | |
93 | ||
86538308 | 94 | /** |
d8689418 EM |
95 | * Common pre-processing function. |
96 | * | |
c490a46a | 97 | * @param CRM_Core_Form $form |
86538308 EM |
98 | * @param bool $useTable |
99 | */ | |
00be9182 | 100 | public static function preProcessCommon(&$form, $useTable = FALSE) { |
6a488035 TO |
101 | |
102 | $form->_contactIds = array(); | |
103 | $form->_contactTypes = array(); | |
104 | ||
105 | // get the submitted values of the search form | |
106 | // we'll need to get fv from either search or adv search in the future | |
107 | $fragment = 'search'; | |
108 | if ($form->_action == CRM_Core_Action::ADVANCED) { | |
109 | self::$_searchFormValues = $form->controller->exportValues('Advanced'); | |
110 | $fragment .= '/advanced'; | |
111 | } | |
112 | elseif ($form->_action == CRM_Core_Action::PROFILE) { | |
113 | self::$_searchFormValues = $form->controller->exportValues('Builder'); | |
114 | $fragment .= '/builder'; | |
115 | } | |
116 | elseif ($form->_action == CRM_Core_Action::COPY) { | |
117 | self::$_searchFormValues = $form->controller->exportValues('Custom'); | |
118 | $fragment .= '/custom'; | |
119 | } | |
120 | else { | |
121 | self::$_searchFormValues = $form->controller->exportValues('Basic'); | |
122 | } | |
123 | ||
124 | //set the user context for redirection of task actions | |
125 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form); | |
126 | $urlParams = 'force=1'; | |
127 | if (CRM_Utils_Rule::qfKey($qfKey)) { | |
128 | $urlParams .= "&qfKey=$qfKey"; | |
129 | } | |
130 | ||
131 | $cacheKey = "civicrm search {$qfKey}"; | |
132 | ||
133 | $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams); | |
134 | $session = CRM_Core_Session::singleton(); | |
135 | $session->replaceUserContext($url); | |
136 | ||
137 | $form->_task = CRM_Utils_Array::value('task', self::$_searchFormValues); | |
138 | $crmContactTaskTasks = CRM_Contact_Task::taskTitles(); | |
139 | $form->assign('taskName', CRM_Utils_Array::value($form->_task, $crmContactTaskTasks)); | |
140 | ||
141 | if ($useTable) { | |
142 | $form->_componentTable = CRM_Core_DAO::createTempTableName('civicrm_task_action', TRUE, $qfKey); | |
143 | $sql = " DROP TABLE IF EXISTS {$form->_componentTable}"; | |
144 | CRM_Core_DAO::executeQuery($sql); | |
145 | ||
146 | $sql = "CREATE TABLE {$form->_componentTable} ( contact_id int primary key) ENGINE=MyISAM DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"; | |
147 | CRM_Core_DAO::executeQuery($sql); | |
148 | } | |
149 | ||
150 | // all contacts or action = save a search | |
151 | if ((CRM_Utils_Array::value('radio_ts', self::$_searchFormValues) == 'ts_all') || | |
152 | ($form->_task == CRM_Contact_Task::SAVE_SEARCH) | |
153 | ) { | |
154 | $sortByCharacter = $form->get('sortByCharacter'); | |
5d272ea2 | 155 | $cacheKey = ($sortByCharacter && $sortByCharacter != 'all') ? "{$cacheKey}_alphabet" : $cacheKey; |
8ef12e64 | 156 | |
66ceb5d9 | 157 | // since we don't store all contacts in prevnextcache, when user selects "all" use query to retrieve contacts |
8e1a7c71 | 158 | // rather than prevnext cache table for most of the task actions except export where we rebuild query to fetch |
159 | // final result set | |
160 | if ($useTable) { | |
161 | $allCids = CRM_Core_BAO_PrevNextCache::getSelection($cacheKey, "getall"); | |
162 | } | |
163 | else { | |
164 | $allCids[$cacheKey] = $form->getContactIds(); | |
165 | } | |
6a488035 TO |
166 | |
167 | $form->_contactIds = array(); | |
168 | if ($useTable) { | |
169 | $count = 0; | |
170 | $insertString = array(); | |
171 | foreach ($allCids[$cacheKey] as $cid => $ignore) { | |
172 | $count++; | |
173 | $insertString[] = " ( {$cid} ) "; | |
174 | if ($count % 200 == 0) { | |
175 | $string = implode(',', $insertString); | |
176 | $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string"; | |
177 | CRM_Core_DAO::executeQuery($sql); | |
178 | $insertString = array(); | |
179 | } | |
180 | } | |
181 | if (!empty($insertString)) { | |
182 | $string = implode(',', $insertString); | |
183 | $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string"; | |
184 | CRM_Core_DAO::executeQuery($sql); | |
185 | } | |
186 | } | |
187 | else { | |
188 | // filter duplicates here | |
189 | // CRM-7058 | |
190 | // might be better to do this in the query, but that logic is a bit complex | |
191 | // and it decides when to use distinct based on input criteria, which needs | |
192 | // to be fixed and optimized. | |
193 | ||
194 | foreach ($allCids[$cacheKey] as $cid => $ignore) { | |
195 | $form->_contactIds[] = $cid; | |
196 | } | |
197 | } | |
198 | } | |
199 | elseif (CRM_Utils_Array::value('radio_ts', self::$_searchFormValues) == 'ts_sel') { | |
200 | // selected contacts only | |
201 | // need to perform action on only selected contacts | |
202 | $insertString = array(); | |
203 | ||
204 | // refire sql in case of custom seach | |
205 | if ($form->_action == CRM_Core_Action::COPY) { | |
206 | // selected contacts only | |
207 | // need to perform action on only selected contacts | |
208 | foreach (self::$_searchFormValues as $name => $value) { | |
209 | if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) { | |
210 | $contactID = substr($name, CRM_Core_Form::CB_PREFIX_LEN); | |
211 | if ($useTable) { | |
212 | $insertString[] = " ( {$contactID} ) "; | |
213 | } | |
214 | else { | |
215 | $form->_contactIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN); | |
216 | } | |
217 | } | |
218 | } | |
219 | } | |
220 | else { | |
5d272ea2 | 221 | // fetching selected contact ids of passed cache key |
222 | $selectedCids = CRM_Core_BAO_PrevNextCache::getSelection($cacheKey); | |
223 | foreach ($selectedCids[$cacheKey] as $selectedCid => $ignore) { | |
224 | if ($useTable) { | |
225 | $insertString[] = " ( {$selectedCid} ) "; | |
226 | } | |
227 | else { | |
228 | $form->_contactIds[] = $selectedCid; | |
229 | } | |
6a488035 TO |
230 | } |
231 | } | |
8ef12e64 | 232 | |
6a488035 TO |
233 | if (!empty($insertString)) { |
234 | $string = implode(',', $insertString); | |
235 | $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string"; | |
236 | CRM_Core_DAO::executeQuery($sql); | |
237 | } | |
238 | } | |
239 | ||
240 | //contact type for pick up profiles as per selected contact types with subtypes | |
241 | //CRM-5521 | |
242 | if ($selectedTypes = CRM_Utils_Array::value('contact_type', self::$_searchFormValues)) { | |
243 | if (!is_array($selectedTypes)) { | |
244 | $selectedTypes = explode(' ', $selectedTypes); | |
245 | } | |
246 | foreach ($selectedTypes as $ct => $dontcare) { | |
247 | if (strpos($ct, CRM_Core_DAO::VALUE_SEPARATOR) === FALSE) { | |
248 | $form->_contactTypes[] = $ct; | |
249 | } | |
250 | else { | |
251 | $separator = strpos($ct, CRM_Core_DAO::VALUE_SEPARATOR); | |
252 | $form->_contactTypes[] = substr($ct, $separator + 1); | |
253 | } | |
254 | } | |
255 | } | |
256 | ||
6a488035 | 257 | if (CRM_Utils_Array::value('radio_ts', self::$_searchFormValues) == 'ts_sel' |
5d272ea2 | 258 | && ($form->_action != CRM_Core_Action::COPY) |
259 | ) { | |
6a488035 | 260 | $sel = CRM_Utils_Array::value('radio_ts', self::$_searchFormValues); |
5d272ea2 | 261 | $form->assign('searchtype', $sel); |
6a488035 TO |
262 | $result = CRM_Core_BAO_PrevNextCache::getSelectedContacts(); |
263 | $form->assign("value", $result); | |
264 | } | |
8ef12e64 | 265 | |
6a488035 TO |
266 | if (!empty($form->_contactIds)) { |
267 | $form->_componentClause = ' contact_a.id IN ( ' . implode(',', $form->_contactIds) . ' ) '; | |
268 | $form->assign('totalSelectedContacts', count($form->_contactIds)); | |
269 | ||
270 | $form->_componentIds = $form->_contactIds; | |
271 | } | |
272 | } | |
273 | ||
274 | /** | |
d8689418 EM |
275 | * Get the contact id for custom search. |
276 | * | |
277 | * we are not using prev/next table in case of custom search | |
6a488035 TO |
278 | */ |
279 | public function getContactIds() { | |
280 | // need to perform action on all contacts | |
281 | // fire the query again and get the contact id's + display name | |
282 | $sortID = NULL; | |
283 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
284 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
285 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
286 | ); | |
287 | } | |
288 | ||
289 | $selectorName = $this->controller->selectorName(); | |
d3e86119 | 290 | require_once str_replace('_', DIRECTORY_SEPARATOR, $selectorName) . '.php'; |
6a488035 TO |
291 | |
292 | $fv = $this->get('formValues'); | |
293 | $customClass = $this->get('customSearchClass'); | |
294 | require_once 'CRM/Core/BAO/Mapping.php'; | |
295 | $returnProperties = CRM_Core_BAO_Mapping::returnProperties(self::$_searchFormValues); | |
296 | ||
5d272ea2 | 297 | $selector = new $selectorName($customClass, $fv, NULL, $returnProperties); |
6a488035 TO |
298 | |
299 | $params = $this->get('queryParams'); | |
300 | ||
301 | // fix for CRM-5165 | |
302 | $sortByCharacter = $this->get('sortByCharacter'); | |
a7cddb8c | 303 | if ($sortByCharacter && $sortByCharacter != 1) { |
6a488035 TO |
304 | $params[] = array('sortByCharacter', '=', $sortByCharacter, 0, 0); |
305 | } | |
306 | $queryOperator = $this->get('queryOperator'); | |
307 | if (!$queryOperator) { | |
308 | $queryOperator = 'AND'; | |
309 | } | |
66ceb5d9 | 310 | $dao = $selector->contactIDQuery($params, $this->_action, $sortID, |
5d272ea2 | 311 | CRM_Utils_Array::value('display_relationship_type', $fv), |
6a488035 TO |
312 | $queryOperator |
313 | ); | |
314 | ||
315 | $contactIds = array(); | |
5d272ea2 | 316 | while ($dao->fetch()) { |
6a488035 TO |
317 | $contactIds[$dao->contact_id] = $dao->contact_id; |
318 | } | |
319 | ||
320 | return $contactIds; | |
321 | } | |
322 | ||
323 | ||
324 | /** | |
ee0ce2ef | 325 | * Set default values for the form. Relationship that in edit/view action. |
6a488035 | 326 | * |
ee0ce2ef | 327 | * The default values are retrieved from the database. |
6a488035 | 328 | * |
e2046b33 | 329 | * @return array |
6a488035 | 330 | */ |
00be9182 | 331 | public function setDefaultValues() { |
6a488035 TO |
332 | $defaults = array(); |
333 | return $defaults; | |
334 | } | |
335 | ||
336 | /** | |
57507ae6 | 337 | * Add the rules for form. |
6a488035 | 338 | */ |
00be9182 | 339 | public function addRules() { |
5d272ea2 | 340 | } |
6a488035 TO |
341 | |
342 | /** | |
5c9ff055 | 343 | * Build the form object. |
6a488035 TO |
344 | */ |
345 | public function buildQuickForm() { | |
346 | $this->addDefaultButtons(ts('Confirm Action')); | |
347 | } | |
348 | ||
349 | /** | |
ee0ce2ef | 350 | * Process the form after the input has been submitted and validated. |
6a488035 | 351 | */ |
5d272ea2 | 352 | public function postProcess() { |
353 | } | |
354 | ||
6a488035 | 355 | /** |
57507ae6 EM |
356 | * Simple shell that derived classes can call to add form buttons. |
357 | * | |
358 | * Allows customized title for the main Submit | |
6a488035 | 359 | * |
77c5b619 TO |
360 | * @param string $title |
361 | * Title of the main button. | |
362 | * @param string $nextType | |
363 | * Button type for the form after processing. | |
77b97be7 EM |
364 | * @param string $backType |
365 | * @param bool $submitOnce | |
6a488035 | 366 | */ |
00be9182 | 367 | public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) { |
6a488035 TO |
368 | $this->addButtons(array( |
369 | array( | |
370 | 'type' => $nextType, | |
371 | 'name' => $title, | |
372 | 'isDefault' => TRUE, | |
373 | ), | |
374 | array( | |
375 | 'type' => $backType, | |
376 | 'name' => ts('Cancel'), | |
377 | ), | |
378 | ) | |
379 | ); | |
380 | } | |
57884c26 DJ |
381 | |
382 | /** | |
100fef9d | 383 | * Replace ids of household members in $this->_contactIds with the id of their household. |
57884c26 | 384 | * |
5c9ff055 | 385 | * CRM-8338 |
57884c26 DJ |
386 | */ |
387 | public function mergeContactIdsByHousehold() { | |
388 | if (empty($this->_contactIds)) { | |
389 | return; | |
390 | } | |
391 | ||
392 | $contactRelationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType( | |
393 | NULL, | |
394 | NULL, | |
395 | NULL, | |
396 | NULL, | |
397 | TRUE, | |
398 | 'name', | |
399 | FALSE | |
400 | ); | |
401 | ||
402 | // Get Head of Household & Household Member relationships | |
403 | $relationKeyMOH = CRM_Utils_Array::key('Household Member of', $contactRelationshipTypes); | |
404 | $relationKeyHOH = CRM_Utils_Array::key('Head of Household for', $contactRelationshipTypes); | |
405 | $householdRelationshipTypes = array( | |
406 | $relationKeyMOH => $contactRelationshipTypes[$relationKeyMOH], | |
407 | $relationKeyHOH => $contactRelationshipTypes[$relationKeyHOH], | |
408 | ); | |
409 | ||
410 | $relID = implode(',', $this->_contactIds); | |
411 | ||
412 | foreach ($householdRelationshipTypes as $rel => $dnt) { | |
413 | list($id, $direction) = explode('_', $rel, 2); | |
414 | // identify the relationship direction | |
415 | $contactA = 'contact_id_a'; | |
416 | $contactB = 'contact_id_b'; | |
417 | if ($direction == 'b_a') { | |
418 | $contactA = 'contact_id_b'; | |
419 | $contactB = 'contact_id_a'; | |
420 | } | |
421 | ||
422 | // Find related households. | |
353ffa53 | 423 | $relationSelect = "SELECT contact_household.id as household_id, {$contactA} as refContact "; |
57884c26 DJ |
424 | $relationFrom = " FROM civicrm_contact contact_household |
425 | INNER JOIN civicrm_relationship crel ON crel.{$contactB} = contact_household.id AND crel.relationship_type_id = {$id} "; | |
426 | ||
427 | // Check for active relationship status only. | |
353ffa53 TO |
428 | $today = date('Ymd'); |
429 | $relationActive = " AND (crel.is_active = 1 AND ( crel.end_date is NULL OR crel.end_date >= {$today} ) )"; | |
430 | $relationWhere = " WHERE contact_household.is_deleted = 0 AND crel.{$contactA} IN ( {$relID} ) {$relationActive}"; | |
431 | $relationGroupBy = " GROUP BY crel.{$contactA}"; | |
57884c26 DJ |
432 | $relationQueryString = "$relationSelect $relationFrom $relationWhere $relationGroupBy"; |
433 | ||
434 | $householdsDAO = CRM_Core_DAO::executeQuery($relationQueryString); | |
435 | while ($householdsDAO->fetch()) { | |
436 | // Remove contact's id from $this->_contactIds and replace with their household's id. | |
437 | foreach (array_keys($this->_contactIds, $householdsDAO->refContact) as $idKey) { | |
438 | unset($this->_contactIds[$idKey]); | |
439 | } | |
440 | if (!in_array($householdsDAO->household_id, $this->_contactIds)) { | |
441 | $this->_contactIds[] = $householdsDAO->household_id; | |
442 | } | |
443 | } | |
444 | $householdsDAO->free(); | |
445 | } | |
446 | } | |
96025800 | 447 | |
36c74cd6 TO |
448 | /** |
449 | * Given this task's list of targets, produce a hidden group. | |
450 | * | |
451 | * @return array | |
452 | * Array(0 => int $groupID, 1 => int|NULL $ssID). | |
453 | * @throws Exception | |
454 | */ | |
455 | public function createHiddenGroup() { | |
456 | // Did the user select "All" matches or cherry-pick a few records? | |
457 | $searchParams = $this->controller->exportValues(); | |
458 | if ($searchParams['radio_ts'] == 'ts_sel') { | |
459 | // Create a static group. | |
460 | ||
461 | $randID = md5(time() . rand(1, 1000)); // groups require a unique name | |
462 | $grpTitle = "Hidden Group {$randID}"; | |
463 | $grpID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $grpTitle, 'id', 'title'); | |
464 | ||
465 | if (!$grpID) { | |
466 | $groupParams = array( | |
467 | 'title' => $grpTitle, | |
468 | 'is_active' => 1, | |
469 | 'is_hidden' => 1, | |
470 | 'group_type' => array('2' => 1), | |
471 | ); | |
472 | ||
473 | $group = CRM_Contact_BAO_Group::create($groupParams); | |
474 | $grpID = $group->id; | |
475 | ||
476 | CRM_Contact_BAO_GroupContact::addContactsToGroup($this->_contactIds, $group->id); | |
477 | ||
478 | $newGroupTitle = "Hidden Group {$grpID}"; | |
479 | $groupParams = array( | |
480 | 'id' => $grpID, | |
481 | 'name' => CRM_Utils_String::titleToVar($newGroupTitle), | |
482 | 'title' => $newGroupTitle, | |
483 | 'group_type' => array('2' => 1), | |
484 | ); | |
485 | $group = CRM_Contact_BAO_Group::create($groupParams); | |
486 | } | |
487 | ||
488 | // note at this point its a static group | |
489 | return array($grpID, NULL); | |
490 | } | |
491 | else { | |
492 | // Create a smart group. | |
493 | ||
494 | $ssId = $this->get('ssID'); | |
495 | $hiddenSmartParams = array( | |
496 | 'group_type' => array('2' => 1), | |
497 | 'form_values' => $this->get('formValues'), | |
498 | 'saved_search_id' => $ssId, | |
499 | 'search_custom_id' => $this->get('customSearchID'), | |
500 | 'search_context' => $this->get('context'), | |
501 | ); | |
502 | ||
503 | list($smartGroupId, $savedSearchId) = CRM_Contact_BAO_Group::createHiddenSmartGroup($hiddenSmartParams); | |
504 | return array($smartGroupId, $savedSearchId); | |
505 | } | |
506 | ||
507 | } | |
508 | ||
6a488035 | 509 | } |