3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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);
101 static function preProcessCommon(&$form, $useTable = FALSE) {
103 $form->_contactIds
= array();
104 $form->_contactTypes
= array();
106 // get the submitted values of the search form
107 // we'll need to get fv from either search or adv search in the future
108 $fragment = 'search';
109 if ($form->_action
== CRM_Core_Action
::ADVANCED
) {
110 self
::$_searchFormValues = $form->controller
->exportValues('Advanced');
111 $fragment .= '/advanced';
113 elseif ($form->_action
== CRM_Core_Action
::PROFILE
) {
114 self
::$_searchFormValues = $form->controller
->exportValues('Builder');
115 $fragment .= '/builder';
117 elseif ($form->_action
== CRM_Core_Action
::COPY
) {
118 self
::$_searchFormValues = $form->controller
->exportValues('Custom');
119 $fragment .= '/custom';
122 self
::$_searchFormValues = $form->controller
->exportValues('Basic');
125 //set the user context for redirection of task actions
126 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $form);
127 $urlParams = 'force=1';
128 if (CRM_Utils_Rule
::qfKey($qfKey)) {
129 $urlParams .= "&qfKey=$qfKey";
132 $cacheKey = "civicrm search {$qfKey}";
134 $url = CRM_Utils_System
::url('civicrm/contact/' . $fragment, $urlParams);
135 $session = CRM_Core_Session
::singleton();
136 $session->replaceUserContext($url);
138 $form->_task
= CRM_Utils_Array
::value('task', self
::$_searchFormValues);
139 $crmContactTaskTasks = CRM_Contact_Task
::taskTitles();
140 $form->assign('taskName', CRM_Utils_Array
::value($form->_task
, $crmContactTaskTasks));
143 $form->_componentTable
= CRM_Core_DAO
::createTempTableName('civicrm_task_action', TRUE, $qfKey);
144 $sql = " DROP TABLE IF EXISTS {$form->_componentTable}";
145 CRM_Core_DAO
::executeQuery($sql);
147 $sql = "CREATE TABLE {$form->_componentTable} ( contact_id int primary key) ENGINE=MyISAM DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
148 CRM_Core_DAO
::executeQuery($sql);
151 // all contacts or action = save a search
152 if ((CRM_Utils_Array
::value('radio_ts', self
::$_searchFormValues) == 'ts_all') ||
153 ($form->_task
== CRM_Contact_Task
::SAVE_SEARCH
)
155 $sortByCharacter = $form->get('sortByCharacter');
156 $cacheKey = ($sortByCharacter && $sortByCharacter != 'all') ?
"{$cacheKey}_alphabet" : $cacheKey;
158 if ($form->_action
== CRM_Core_Action
::COPY
) {
159 $allCids[$cacheKey] = $form->getContactIds( );
162 $allCids = CRM_Core_BAO_PrevNextCache
::getSelection($cacheKey, "getall");
165 $form->_contactIds
= array();
168 $insertString = array();
169 foreach ($allCids[$cacheKey] as $cid => $ignore) {
171 $insertString[] = " ( {$cid} ) ";
172 if ($count %
200 == 0) {
173 $string = implode(',', $insertString);
174 $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string";
175 CRM_Core_DAO
::executeQuery($sql);
176 $insertString = array();
179 if (!empty($insertString)) {
180 $string = implode(',', $insertString);
181 $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string";
182 CRM_Core_DAO
::executeQuery($sql);
186 // filter duplicates here
188 // might be better to do this in the query, but that logic is a bit complex
189 // and it decides when to use distinct based on input criteria, which needs
190 // to be fixed and optimized.
192 foreach ($allCids[$cacheKey] as $cid => $ignore) {
193 $form->_contactIds
[] = $cid;
197 elseif (CRM_Utils_Array
::value('radio_ts', self
::$_searchFormValues) == 'ts_sel') {
198 // selected contacts only
199 // need to perform action on only selected contacts
200 $insertString = array();
202 // refire sql in case of custom seach
203 if ($form->_action
== CRM_Core_Action
::COPY
) {
204 // selected contacts only
205 // need to perform action on only selected contacts
206 foreach (self
::$_searchFormValues as $name => $value) {
207 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
208 $contactID = substr($name, CRM_Core_Form
::CB_PREFIX_LEN
);
210 $insertString[] = " ( {$contactID} ) ";
213 $form->_contactIds
[] = substr($name, CRM_Core_Form
::CB_PREFIX_LEN
);
219 // fetching selected contact ids of passed cache key
220 $selectedCids = CRM_Core_BAO_PrevNextCache
::getSelection($cacheKey);
221 foreach ($selectedCids[$cacheKey] as $selectedCid => $ignore) {
223 $insertString[] = " ( {$selectedCid} ) ";
226 $form->_contactIds
[] = $selectedCid;
231 if (!empty($insertString)) {
232 $string = implode(',', $insertString);
233 $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string";
234 CRM_Core_DAO
::executeQuery($sql);
238 //contact type for pick up profiles as per selected contact types with subtypes
240 if ($selectedTypes = CRM_Utils_Array
::value('contact_type', self
::$_searchFormValues)) {
241 if (!is_array($selectedTypes)) {
242 $selectedTypes = explode(' ', $selectedTypes);
244 foreach ($selectedTypes as $ct => $dontcare) {
245 if (strpos($ct, CRM_Core_DAO
::VALUE_SEPARATOR
) === FALSE) {
246 $form->_contactTypes
[] = $ct;
249 $separator = strpos($ct, CRM_Core_DAO
::VALUE_SEPARATOR
);
250 $form->_contactTypes
[] = substr($ct, $separator +
1);
256 if (CRM_Utils_Array
::value('radio_ts', self
::$_searchFormValues) == 'ts_sel'
257 && ($form->_action
!= CRM_Core_Action
::COPY
) ) {
259 $sel = CRM_Utils_Array
::value('radio_ts', self
::$_searchFormValues);
260 $form->assign('searchtype',$sel);
261 $result = CRM_Core_BAO_PrevNextCache
::getSelectedContacts();
262 $form->assign("value", $result);
265 if (!empty($form->_contactIds
)) {
266 $form->_componentClause
= ' contact_a.id IN ( ' . implode(',', $form->_contactIds
) . ' ) ';
267 $form->assign('totalSelectedContacts', count($form->_contactIds
));
269 $form->_componentIds
= $form->_contactIds
;
274 * Function to get the contact id for custom search
275 * we are not using prev/next table incase of custom search
277 public function getContactIds() {
278 // need to perform action on all contacts
279 // fire the query again and get the contact id's + display name
281 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
282 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
283 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
287 $selectorName = $this->controller
->selectorName();
288 require_once (str_replace('_', DIRECTORY_SEPARATOR
, $selectorName) . '.php');
290 $fv = $this->get('formValues');
291 $customClass = $this->get('customSearchClass');
292 require_once 'CRM/Core/BAO/Mapping.php';
293 $returnProperties = CRM_Core_BAO_Mapping
::returnProperties(self
::$_searchFormValues);
295 eval('$selector = new ' .
297 '( $customClass, $fv, null, $returnProperties ); '
300 $params = $this->get('queryParams');
303 $sortByCharacter = $this->get('sortByCharacter');
304 if ($sortByCharacter &&
305 $sortByCharacter != 1
307 $params[] = array('sortByCharacter', '=', $sortByCharacter, 0, 0);
309 $queryOperator = $this->get('queryOperator');
310 if (!$queryOperator) {
311 $queryOperator = 'AND';
313 $dao = &$selector->contactIDQuery($params, $this->_action
, $sortID,
314 CRM_Utils_Array
::value('display_relationship_type', $fv ),
318 $contactIds = array();
319 while( $dao->fetch()) {
320 $contactIds[$dao->contact_id
] = $dao->contact_id
;
328 * This function sets the default values for the form. Relationship that in edit/view action
329 * the default values are retrieved from the database
335 function setDefaultValues() {
341 * This function is used to add the rules for form.
346 function addRules() {}
349 * Function to actually build the form
354 public function buildQuickForm() {
355 $this->addDefaultButtons(ts('Confirm Action'));
359 * process the form after the input has been submitted and validated
365 public function postProcess() {}
369 * simple shell that derived classes can call to add buttons to
370 * the form with a customized title for the main Submit
372 * @param string $title title of the main button
373 * @param string $type button type for the form after processing
378 function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
379 $this->addButtons(array(
387 'name' => ts('Cancel'),