3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
36 * This class generates form components for search-result tasks
38 class CRM_Contact_Form_Task
extends CRM_Core_Form
{
41 * The task being performed
48 * The array that holds all the contact ids
55 * The array that holds all the contact types
59 public $_contactTypes;
62 * The additional clause that we restrict the search with
66 protected $_componentClause = NULL;
69 * The name of the temp table where we store the contact IDs
73 protected $_componentTable = NULL;
76 * The array that holds all the component ids
80 protected $_componentIds;
83 * This includes the submitted values of the search form
85 static protected $_searchFormValues;
88 * Build all the data structures needed to build the form.
90 public function preProcess() {
91 self
::preProcessCommon($this);
95 * Common pre-processing function.
97 * @param CRM_Core_Form $form
98 * @param bool $useTable
100 public static function preProcessCommon(&$form, $useTable = FALSE) {
102 $form->_contactIds
= array();
103 $form->_contactTypes
= array();
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';
112 elseif ($form->_action
== CRM_Core_Action
::PROFILE
) {
113 self
::$_searchFormValues = $form->controller
->exportValues('Builder');
114 $fragment .= '/builder';
116 elseif ($form->_action
== CRM_Core_Action
::COPY
) {
117 self
::$_searchFormValues = $form->controller
->exportValues('Custom');
118 $fragment .= '/custom';
121 self
::$_searchFormValues = $form->controller
->exportValues('Basic');
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";
131 $cacheKey = "civicrm search {$qfKey}";
133 $url = CRM_Utils_System
::url('civicrm/contact/' . $fragment, $urlParams);
134 $session = CRM_Core_Session
::singleton();
135 $session->replaceUserContext($url);
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));
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);
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);
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
)
154 $sortByCharacter = $form->get('sortByCharacter');
155 $cacheKey = ($sortByCharacter && $sortByCharacter != 'all') ?
"{$cacheKey}_alphabet" : $cacheKey;
157 // since we don't store all contacts in prevnextcache, when user selects "all" use query to retrieve contacts
158 // rather than prevnext cache table for most of the task actions except export where we rebuild query to fetch
161 $allCids = CRM_Core_BAO_PrevNextCache
::getSelection($cacheKey, "getall");
164 $allCids[$cacheKey] = $form->getContactIds();
167 $form->_contactIds
= array();
170 $insertString = array();
171 foreach ($allCids[$cacheKey] as $cid => $ignore) {
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();
181 if (!empty($insertString)) {
182 $string = implode(',', $insertString);
183 $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string";
184 CRM_Core_DAO
::executeQuery($sql);
188 // filter duplicates here
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.
194 foreach ($allCids[$cacheKey] as $cid => $ignore) {
195 $form->_contactIds
[] = $cid;
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();
204 // refire sql in case of custom search
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
);
212 $insertString[] = " ( {$contactID} ) ";
215 $form->_contactIds
[] = substr($name, CRM_Core_Form
::CB_PREFIX_LEN
);
221 // fetching selected contact ids of passed cache key
222 $selectedCids = CRM_Core_BAO_PrevNextCache
::getSelection($cacheKey);
223 foreach ($selectedCids[$cacheKey] as $selectedCid => $ignore) {
225 $insertString[] = " ( {$selectedCid} ) ";
228 $form->_contactIds
[] = $selectedCid;
233 if (!empty($insertString)) {
234 $string = implode(',', $insertString);
235 $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string";
236 CRM_Core_DAO
::executeQuery($sql);
240 //contact type for pick up profiles as per selected contact types with subtypes
242 if ($selectedTypes = CRM_Utils_Array
::value('contact_type', self
::$_searchFormValues)) {
243 if (!is_array($selectedTypes)) {
244 $selectedTypes = explode(' ', $selectedTypes);
246 foreach ($selectedTypes as $ct => $dontcare) {
247 if (strpos($ct, CRM_Core_DAO
::VALUE_SEPARATOR
) === FALSE) {
248 $form->_contactTypes
[] = $ct;
251 $separator = strpos($ct, CRM_Core_DAO
::VALUE_SEPARATOR
);
252 $form->_contactTypes
[] = substr($ct, $separator +
1);
257 if (CRM_Utils_Array
::value('radio_ts', self
::$_searchFormValues) == 'ts_sel'
258 && ($form->_action
!= CRM_Core_Action
::COPY
)
260 $sel = CRM_Utils_Array
::value('radio_ts', self
::$_searchFormValues);
261 $form->assign('searchtype', $sel);
262 $result = CRM_Core_BAO_PrevNextCache
::getSelectedContacts();
263 $form->assign("value", $result);
266 if (!empty($form->_contactIds
)) {
267 $form->_componentClause
= ' contact_a.id IN ( ' . implode(',', $form->_contactIds
) . ' ) ';
268 $form->assign('totalSelectedContacts', count($form->_contactIds
));
270 $form->_componentIds
= $form->_contactIds
;
275 * Get the contact id for custom search.
277 * we are not using prev/next table in case of custom search
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
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
)
289 $selectorName = $this->controller
->selectorName();
290 require_once str_replace('_', DIRECTORY_SEPARATOR
, $selectorName) . '.php';
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);
297 $selector = new $selectorName($customClass, $fv, NULL, $returnProperties);
299 $params = $this->get('queryParams');
302 $sortByCharacter = $this->get('sortByCharacter');
303 if ($sortByCharacter && $sortByCharacter != 1) {
304 $params[] = array('sortByCharacter', '=', $sortByCharacter, 0, 0);
306 $queryOperator = $this->get('queryOperator');
307 if (!$queryOperator) {
308 $queryOperator = 'AND';
310 $dao = $selector->contactIDQuery($params, $this->_action
, $sortID,
311 CRM_Utils_Array
::value('display_relationship_type', $fv),
315 $contactIds = array();
316 while ($dao->fetch()) {
317 $contactIds[$dao->contact_id
] = $dao->contact_id
;
325 * Set default values for the form. Relationship that in edit/view action.
327 * The default values are retrieved from the database.
331 public function setDefaultValues() {
337 * Add the rules for form.
339 public function addRules() {
343 * Build the form object.
345 public function buildQuickForm() {
346 $this->addDefaultButtons(ts('Confirm Action'));
350 * Process the form after the input has been submitted and validated.
352 public function postProcess() {
356 * Simple shell that derived classes can call to add form buttons.
358 * Allows customized title for the main Submit
360 * @param string $title
361 * Title of the main button.
362 * @param string $nextType
363 * Button type for the form after processing.
364 * @param string $backType
365 * @param bool $submitOnce
367 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
368 $this->addButtons(array(
376 'name' => ts('Cancel'),
384 * Replace ids of household members in $this->_contactIds with the id of their household.
388 public function mergeContactIdsByHousehold() {
389 if (empty($this->_contactIds
)) {
393 $contactRelationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(
403 // Get Head of Household & Household Member relationships
404 $relationKeyMOH = CRM_Utils_Array
::key('Household Member of', $contactRelationshipTypes);
405 $relationKeyHOH = CRM_Utils_Array
::key('Head of Household for', $contactRelationshipTypes);
406 $householdRelationshipTypes = array(
407 $relationKeyMOH => $contactRelationshipTypes[$relationKeyMOH],
408 $relationKeyHOH => $contactRelationshipTypes[$relationKeyHOH],
411 $relID = implode(',', $this->_contactIds
);
413 foreach ($householdRelationshipTypes as $rel => $dnt) {
414 list($id, $direction) = explode('_', $rel, 2);
415 // identify the relationship direction
416 $contactA = 'contact_id_a';
417 $contactB = 'contact_id_b';
418 if ($direction == 'b_a') {
419 $contactA = 'contact_id_b';
420 $contactB = 'contact_id_a';
423 // Find related households.
424 $relationSelect = "SELECT contact_household.id as household_id, {$contactA} as refContact ";
425 $relationFrom = " FROM civicrm_contact contact_household
426 INNER JOIN civicrm_relationship crel ON crel.{$contactB} = contact_household.id AND crel.relationship_type_id = {$id} ";
428 // Check for active relationship status only.
429 $today = date('Ymd');
430 $relationActive = " AND (crel.is_active = 1 AND ( crel.end_date is NULL OR crel.end_date >= {$today} ) )";
431 $relationWhere = " WHERE contact_household.is_deleted = 0 AND crel.{$contactA} IN ( {$relID} ) {$relationActive}";
432 $relationGroupBy = " GROUP BY crel.{$contactA}";
433 $relationQueryString = "$relationSelect $relationFrom $relationWhere $relationGroupBy";
435 $householdsDAO = CRM_Core_DAO
::executeQuery($relationQueryString);
436 while ($householdsDAO->fetch()) {
437 // Remove contact's id from $this->_contactIds and replace with their household's id.
438 foreach (array_keys($this->_contactIds
, $householdsDAO->refContact
) as $idKey) {
439 unset($this->_contactIds
[$idKey]);
441 if (!in_array($householdsDAO->household_id
, $this->_contactIds
)) {
442 $this->_contactIds
[] = $householdsDAO->household_id
;
445 $householdsDAO->free();
450 * Given this task's list of targets, produce a hidden group.
453 * Array(0 => int $groupID, 1 => int|NULL $ssID).
456 public function createHiddenGroup() {
457 // Did the user select "All" matches or cherry-pick a few records?
458 $searchParams = $this->controller
->exportValues();
459 if ($searchParams['radio_ts'] == 'ts_sel') {
460 // Create a static group.
462 $randID = md5(time() . rand(1, 1000)); // groups require a unique name
463 $grpTitle = "Hidden Group {$randID}";
464 $grpID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $grpTitle, 'id', 'title');
467 $groupParams = array(
468 'title' => $grpTitle,
471 'group_type' => array('2' => 1),
474 $group = CRM_Contact_BAO_Group
::create($groupParams);
477 CRM_Contact_BAO_GroupContact
::addContactsToGroup($this->_contactIds
, $group->id
);
479 $newGroupTitle = "Hidden Group {$grpID}";
480 $groupParams = array(
482 'name' => CRM_Utils_String
::titleToVar($newGroupTitle),
483 'title' => $newGroupTitle,
484 'group_type' => array('2' => 1),
486 $group = CRM_Contact_BAO_Group
::create($groupParams);
489 // note at this point its a static group
490 return array($grpID, NULL);
493 // Create a smart group.
495 $ssId = $this->get('ssID');
496 $hiddenSmartParams = array(
497 'group_type' => array('2' => 1),
498 'form_values' => $this->get('formValues'),
499 'saved_search_id' => $ssId,
500 'search_custom_id' => $this->get('customSearchID'),
501 'search_context' => $this->get('context'),
504 list($smartGroupId, $savedSearchId) = CRM_Contact_BAO_Group
::createHiddenSmartGroup($hiddenSmartParams);
505 return array($smartGroupId, $savedSearchId);