3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * This class generates form components for search-result tasks
40 class CRM_Contact_Form_Task
extends CRM_Core_Form
{
43 * the task being performed
50 * The array that holds all the contact ids
57 * The array that holds all the contact types
61 public $_contactTypes;
64 * The additional clause that we restrict the search with
68 protected $_componentClause = NULL;
71 * The name of the temp table where we store the contact IDs
75 protected $_componentTable = NULL;
78 * The array that holds all the component ids
82 protected $_componentIds;
85 * This includes the submitted values of the search form
87 static protected $_searchFormValues;
90 * build all the data structures needed to build the form
97 function preProcess() {
98 self
::preProcessCommon($this);
103 * @param bool $useTable
105 static function preProcessCommon(&$form, $useTable = FALSE) {
107 $form->_contactIds
= array();
108 $form->_contactTypes
= array();
110 // get the submitted values of the search form
111 // we'll need to get fv from either search or adv search in the future
112 $fragment = 'search';
113 if ($form->_action
== CRM_Core_Action
::ADVANCED
) {
114 self
::$_searchFormValues = $form->controller
->exportValues('Advanced');
115 $fragment .= '/advanced';
117 elseif ($form->_action
== CRM_Core_Action
::PROFILE
) {
118 self
::$_searchFormValues = $form->controller
->exportValues('Builder');
119 $fragment .= '/builder';
121 elseif ($form->_action
== CRM_Core_Action
::COPY
) {
122 self
::$_searchFormValues = $form->controller
->exportValues('Custom');
123 $fragment .= '/custom';
126 self
::$_searchFormValues = $form->controller
->exportValues('Basic');
129 //set the user context for redirection of task actions
130 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $form);
131 $urlParams = 'force=1';
132 if (CRM_Utils_Rule
::qfKey($qfKey)) {
133 $urlParams .= "&qfKey=$qfKey";
136 $cacheKey = "civicrm search {$qfKey}";
138 $url = CRM_Utils_System
::url('civicrm/contact/' . $fragment, $urlParams);
139 $session = CRM_Core_Session
::singleton();
140 $session->replaceUserContext($url);
142 $form->_task
= CRM_Utils_Array
::value('task', self
::$_searchFormValues);
143 $crmContactTaskTasks = CRM_Contact_Task
::taskTitles();
144 $form->assign('taskName', CRM_Utils_Array
::value($form->_task
, $crmContactTaskTasks));
147 $form->_componentTable
= CRM_Core_DAO
::createTempTableName('civicrm_task_action', TRUE, $qfKey);
148 $sql = " DROP TABLE IF EXISTS {$form->_componentTable}";
149 CRM_Core_DAO
::executeQuery($sql);
151 $sql = "CREATE TABLE {$form->_componentTable} ( contact_id int primary key) ENGINE=MyISAM DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
152 CRM_Core_DAO
::executeQuery($sql);
155 // all contacts or action = save a search
156 if ((CRM_Utils_Array
::value('radio_ts', self
::$_searchFormValues) == 'ts_all') ||
157 ($form->_task
== CRM_Contact_Task
::SAVE_SEARCH
)
159 $sortByCharacter = $form->get('sortByCharacter');
160 $cacheKey = ($sortByCharacter && $sortByCharacter != 'all') ?
"{$cacheKey}_alphabet" : $cacheKey;
162 // since we don't store all contacts in prevnextcache, when user selects "all" use query to retrieve contacts
163 // rather than prevnext cache table for most of the task actions except export where we rebuild query to fetch
166 $allCids = CRM_Core_BAO_PrevNextCache
::getSelection($cacheKey, "getall");
169 $allCids[$cacheKey] = $form->getContactIds();
172 $form->_contactIds
= array();
175 $insertString = array();
176 foreach ($allCids[$cacheKey] as $cid => $ignore) {
178 $insertString[] = " ( {$cid} ) ";
179 if ($count %
200 == 0) {
180 $string = implode(',', $insertString);
181 $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string";
182 CRM_Core_DAO
::executeQuery($sql);
183 $insertString = array();
186 if (!empty($insertString)) {
187 $string = implode(',', $insertString);
188 $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string";
189 CRM_Core_DAO
::executeQuery($sql);
193 // filter duplicates here
195 // might be better to do this in the query, but that logic is a bit complex
196 // and it decides when to use distinct based on input criteria, which needs
197 // to be fixed and optimized.
199 foreach ($allCids[$cacheKey] as $cid => $ignore) {
200 $form->_contactIds
[] = $cid;
204 elseif (CRM_Utils_Array
::value('radio_ts', self
::$_searchFormValues) == 'ts_sel') {
205 // selected contacts only
206 // need to perform action on only selected contacts
207 $insertString = array();
209 // refire sql in case of custom seach
210 if ($form->_action
== CRM_Core_Action
::COPY
) {
211 // selected contacts only
212 // need to perform action on only selected contacts
213 foreach (self
::$_searchFormValues as $name => $value) {
214 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
215 $contactID = substr($name, CRM_Core_Form
::CB_PREFIX_LEN
);
217 $insertString[] = " ( {$contactID} ) ";
220 $form->_contactIds
[] = substr($name, CRM_Core_Form
::CB_PREFIX_LEN
);
226 // fetching selected contact ids of passed cache key
227 $selectedCids = CRM_Core_BAO_PrevNextCache
::getSelection($cacheKey);
228 foreach ($selectedCids[$cacheKey] as $selectedCid => $ignore) {
230 $insertString[] = " ( {$selectedCid} ) ";
233 $form->_contactIds
[] = $selectedCid;
238 if (!empty($insertString)) {
239 $string = implode(',', $insertString);
240 $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string";
241 CRM_Core_DAO
::executeQuery($sql);
245 //contact type for pick up profiles as per selected contact types with subtypes
247 if ($selectedTypes = CRM_Utils_Array
::value('contact_type', self
::$_searchFormValues)) {
248 if (!is_array($selectedTypes)) {
249 $selectedTypes = explode(' ', $selectedTypes);
251 foreach ($selectedTypes as $ct => $dontcare) {
252 if (strpos($ct, CRM_Core_DAO
::VALUE_SEPARATOR
) === FALSE) {
253 $form->_contactTypes
[] = $ct;
256 $separator = strpos($ct, CRM_Core_DAO
::VALUE_SEPARATOR
);
257 $form->_contactTypes
[] = substr($ct, $separator +
1);
263 if (CRM_Utils_Array
::value('radio_ts', self
::$_searchFormValues) == 'ts_sel'
264 && ($form->_action
!= CRM_Core_Action
::COPY
)
266 $sel = CRM_Utils_Array
::value('radio_ts', self
::$_searchFormValues);
267 $form->assign('searchtype', $sel);
268 $result = CRM_Core_BAO_PrevNextCache
::getSelectedContacts();
269 $form->assign("value", $result);
272 if (!empty($form->_contactIds
)) {
273 $form->_componentClause
= ' contact_a.id IN ( ' . implode(',', $form->_contactIds
) . ' ) ';
274 $form->assign('totalSelectedContacts', count($form->_contactIds
));
276 $form->_componentIds
= $form->_contactIds
;
281 * Function to get the contact id for custom search
282 * we are not using prev/next table incase of custom search
284 public function getContactIds() {
285 // need to perform action on all contacts
286 // fire the query again and get the contact id's + display name
288 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
289 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
290 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
294 $selectorName = $this->controller
->selectorName();
295 require_once(str_replace('_', DIRECTORY_SEPARATOR
, $selectorName) . '.php');
297 $fv = $this->get('formValues');
298 $customClass = $this->get('customSearchClass');
299 require_once 'CRM/Core/BAO/Mapping.php';
300 $returnProperties = CRM_Core_BAO_Mapping
::returnProperties(self
::$_searchFormValues);
302 $selector = new $selectorName($customClass, $fv, NULL, $returnProperties);
304 $params = $this->get('queryParams');
307 $sortByCharacter = $this->get('sortByCharacter');
308 if ($sortByCharacter && $sortByCharacter != 1) {
309 $params[] = array('sortByCharacter', '=', $sortByCharacter, 0, 0);
311 $queryOperator = $this->get('queryOperator');
312 if (!$queryOperator) {
313 $queryOperator = 'AND';
315 $dao = $selector->contactIDQuery($params, $this->_action
, $sortID,
316 CRM_Utils_Array
::value('display_relationship_type', $fv),
320 $contactIds = array();
321 while ($dao->fetch()) {
322 $contactIds[$dao->contact_id
] = $dao->contact_id
;
330 * This function sets the default values for the form. Relationship that in edit/view action
331 * the default values are retrieved from the database
337 function setDefaultValues() {
343 * This function is used to add the rules for form.
348 function addRules() {
352 * Function to actually build the form
357 public function buildQuickForm() {
358 $this->addDefaultButtons(ts('Confirm Action'));
362 * process the form after the input has been submitted and validated
368 public function postProcess() {
374 * simple shell that derived classes can call to add buttons to
375 * the form with a customized title for the main Submit
377 * @param string $title title of the main button
378 * @param string $nextType
379 * @param string $backType
380 * @param bool $submitOnce
382 * @internal param string $type button type for the form after processing
387 function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
388 $this->addButtons(array(
396 'name' => ts('Cancel'),
403 * replace ids of household members in $this->_contactIds with the id of their household.
410 public function mergeContactIdsByHousehold() {
411 if (empty($this->_contactIds
)) {
415 $contactRelationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(
425 // Get Head of Household & Household Member relationships
426 $relationKeyMOH = CRM_Utils_Array
::key('Household Member of', $contactRelationshipTypes);
427 $relationKeyHOH = CRM_Utils_Array
::key('Head of Household for', $contactRelationshipTypes);
428 $householdRelationshipTypes = array(
429 $relationKeyMOH => $contactRelationshipTypes[$relationKeyMOH],
430 $relationKeyHOH => $contactRelationshipTypes[$relationKeyHOH],
433 $relID = implode(',', $this->_contactIds
);
435 foreach ($householdRelationshipTypes as $rel => $dnt) {
436 list($id, $direction) = explode('_', $rel, 2);
437 // identify the relationship direction
438 $contactA = 'contact_id_a';
439 $contactB = 'contact_id_b';
440 if ($direction == 'b_a') {
441 $contactA = 'contact_id_b';
442 $contactB = 'contact_id_a';
445 // Find related households.
446 $relationSelect = "SELECT contact_household.id as household_id, {$contactA} as refContact ";
447 $relationFrom = " FROM civicrm_contact contact_household
448 INNER JOIN civicrm_relationship crel ON crel.{$contactB} = contact_household.id AND crel.relationship_type_id = {$id} ";
450 // Check for active relationship status only.
451 $today = date('Ymd');
452 $relationActive = " AND (crel.is_active = 1 AND ( crel.end_date is NULL OR crel.end_date >= {$today} ) )";
453 $relationWhere = " WHERE contact_household.is_deleted = 0 AND crel.{$contactA} IN ( {$relID} ) {$relationActive}";
454 $relationGroupBy = " GROUP BY crel.{$contactA}";
455 $relationQueryString = "$relationSelect $relationFrom $relationWhere $relationGroupBy";
457 $householdsDAO = CRM_Core_DAO
::executeQuery($relationQueryString);
458 while ($householdsDAO->fetch()) {
459 // Remove contact's id from $this->_contactIds and replace with their household's id.
460 foreach (array_keys($this->_contactIds
, $householdsDAO->refContact
) as $idKey) {
461 unset($this->_contactIds
[$idKey]);
463 if (!in_array($householdsDAO->household_id
, $this->_contactIds
)) {
464 $this->_contactIds
[] = $householdsDAO->household_id
;
467 $householdsDAO->free();