Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for search-result tasks | |
38 | * | |
39 | */ | |
40 | class CRM_Contact_Form_Task extends CRM_Core_Form { | |
41 | ||
42 | /** | |
43 | * the task being performed | |
44 | * | |
45 | * @var int | |
46 | */ | |
47 | protected $_task; | |
48 | ||
49 | /** | |
50 | * The array that holds all the contact ids | |
51 | * | |
52 | * @var array | |
53 | */ | |
54 | public $_contactIds; | |
55 | ||
56 | /** | |
57 | * The array that holds all the contact types | |
58 | * | |
59 | * @var array | |
60 | */ | |
61 | public $_contactTypes; | |
62 | ||
63 | /** | |
64 | * The additional clause that we restrict the search with | |
65 | * | |
66 | * @var string | |
67 | */ | |
68 | protected $_componentClause = NULL; | |
69 | ||
70 | /** | |
71 | * The name of the temp table where we store the contact IDs | |
72 | * | |
73 | * @var string | |
74 | */ | |
75 | protected $_componentTable = NULL; | |
76 | ||
77 | /** | |
78 | * The array that holds all the component ids | |
79 | * | |
80 | * @var array | |
81 | */ | |
82 | protected $_componentIds; | |
83 | ||
84 | /** | |
85 | * This includes the submitted values of the search form | |
86 | */ | |
87 | static protected $_searchFormValues; | |
88 | ||
89 | /** | |
90 | * build all the data structures needed to build the form | |
91 | * | |
92 | * @param | |
93 | * | |
94 | * @return void | |
95 | * @access public | |
8ef12e64 | 96 | */ |
6a488035 TO |
97 | function preProcess() { |
98 | self::preProcessCommon($this); | |
99 | } | |
100 | ||
101 | static function preProcessCommon(&$form, $useTable = FALSE) { | |
102 | ||
103 | $form->_contactIds = array(); | |
104 | $form->_contactTypes = array(); | |
105 | ||
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'; | |
112 | } | |
113 | elseif ($form->_action == CRM_Core_Action::PROFILE) { | |
114 | self::$_searchFormValues = $form->controller->exportValues('Builder'); | |
115 | $fragment .= '/builder'; | |
116 | } | |
117 | elseif ($form->_action == CRM_Core_Action::COPY) { | |
118 | self::$_searchFormValues = $form->controller->exportValues('Custom'); | |
119 | $fragment .= '/custom'; | |
120 | } | |
121 | else { | |
122 | self::$_searchFormValues = $form->controller->exportValues('Basic'); | |
123 | } | |
124 | ||
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"; | |
130 | } | |
131 | ||
132 | $cacheKey = "civicrm search {$qfKey}"; | |
133 | ||
134 | $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams); | |
135 | $session = CRM_Core_Session::singleton(); | |
136 | $session->replaceUserContext($url); | |
137 | ||
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)); | |
141 | ||
142 | if ($useTable) { | |
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); | |
146 | ||
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); | |
149 | } | |
150 | ||
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) | |
154 | ) { | |
155 | $sortByCharacter = $form->get('sortByCharacter'); | |
5d272ea2 | 156 | $cacheKey = ($sortByCharacter && $sortByCharacter != 'all') ? "{$cacheKey}_alphabet" : $cacheKey; |
8ef12e64 | 157 | |
66ceb5d9 | 158 | // since we don't store all contacts in prevnextcache, when user selects "all" use query to retrieve contacts |
8e1a7c71 | 159 | // rather than prevnext cache table for most of the task actions except export where we rebuild query to fetch |
160 | // final result set | |
161 | if ($useTable) { | |
162 | $allCids = CRM_Core_BAO_PrevNextCache::getSelection($cacheKey, "getall"); | |
163 | } | |
164 | else { | |
165 | $allCids[$cacheKey] = $form->getContactIds(); | |
166 | } | |
6a488035 TO |
167 | |
168 | $form->_contactIds = array(); | |
169 | if ($useTable) { | |
170 | $count = 0; | |
171 | $insertString = array(); | |
172 | foreach ($allCids[$cacheKey] as $cid => $ignore) { | |
173 | $count++; | |
174 | $insertString[] = " ( {$cid} ) "; | |
175 | if ($count % 200 == 0) { | |
176 | $string = implode(',', $insertString); | |
177 | $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string"; | |
178 | CRM_Core_DAO::executeQuery($sql); | |
179 | $insertString = array(); | |
180 | } | |
181 | } | |
182 | if (!empty($insertString)) { | |
183 | $string = implode(',', $insertString); | |
184 | $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string"; | |
185 | CRM_Core_DAO::executeQuery($sql); | |
186 | } | |
187 | } | |
188 | else { | |
189 | // filter duplicates here | |
190 | // CRM-7058 | |
191 | // might be better to do this in the query, but that logic is a bit complex | |
192 | // and it decides when to use distinct based on input criteria, which needs | |
193 | // to be fixed and optimized. | |
194 | ||
195 | foreach ($allCids[$cacheKey] as $cid => $ignore) { | |
196 | $form->_contactIds[] = $cid; | |
197 | } | |
198 | } | |
199 | } | |
200 | elseif (CRM_Utils_Array::value('radio_ts', self::$_searchFormValues) == 'ts_sel') { | |
201 | // selected contacts only | |
202 | // need to perform action on only selected contacts | |
203 | $insertString = array(); | |
204 | ||
205 | // refire sql in case of custom seach | |
206 | if ($form->_action == CRM_Core_Action::COPY) { | |
207 | // selected contacts only | |
208 | // need to perform action on only selected contacts | |
209 | foreach (self::$_searchFormValues as $name => $value) { | |
210 | if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) { | |
211 | $contactID = substr($name, CRM_Core_Form::CB_PREFIX_LEN); | |
212 | if ($useTable) { | |
213 | $insertString[] = " ( {$contactID} ) "; | |
214 | } | |
215 | else { | |
216 | $form->_contactIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN); | |
217 | } | |
218 | } | |
219 | } | |
220 | } | |
221 | else { | |
5d272ea2 | 222 | // fetching selected contact ids of passed cache key |
223 | $selectedCids = CRM_Core_BAO_PrevNextCache::getSelection($cacheKey); | |
224 | foreach ($selectedCids[$cacheKey] as $selectedCid => $ignore) { | |
225 | if ($useTable) { | |
226 | $insertString[] = " ( {$selectedCid} ) "; | |
227 | } | |
228 | else { | |
229 | $form->_contactIds[] = $selectedCid; | |
230 | } | |
6a488035 TO |
231 | } |
232 | } | |
8ef12e64 | 233 | |
6a488035 TO |
234 | if (!empty($insertString)) { |
235 | $string = implode(',', $insertString); | |
236 | $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string"; | |
237 | CRM_Core_DAO::executeQuery($sql); | |
238 | } | |
239 | } | |
240 | ||
241 | //contact type for pick up profiles as per selected contact types with subtypes | |
242 | //CRM-5521 | |
243 | if ($selectedTypes = CRM_Utils_Array::value('contact_type', self::$_searchFormValues)) { | |
244 | if (!is_array($selectedTypes)) { | |
245 | $selectedTypes = explode(' ', $selectedTypes); | |
246 | } | |
247 | foreach ($selectedTypes as $ct => $dontcare) { | |
248 | if (strpos($ct, CRM_Core_DAO::VALUE_SEPARATOR) === FALSE) { | |
249 | $form->_contactTypes[] = $ct; | |
250 | } | |
251 | else { | |
252 | $separator = strpos($ct, CRM_Core_DAO::VALUE_SEPARATOR); | |
253 | $form->_contactTypes[] = substr($ct, $separator + 1); | |
254 | } | |
255 | } | |
256 | } | |
257 | ||
8ef12e64 | 258 | |
6a488035 | 259 | if (CRM_Utils_Array::value('radio_ts', self::$_searchFormValues) == 'ts_sel' |
5d272ea2 | 260 | && ($form->_action != CRM_Core_Action::COPY) |
261 | ) { | |
6a488035 | 262 | $sel = CRM_Utils_Array::value('radio_ts', self::$_searchFormValues); |
5d272ea2 | 263 | $form->assign('searchtype', $sel); |
6a488035 TO |
264 | $result = CRM_Core_BAO_PrevNextCache::getSelectedContacts(); |
265 | $form->assign("value", $result); | |
266 | } | |
8ef12e64 | 267 | |
6a488035 TO |
268 | if (!empty($form->_contactIds)) { |
269 | $form->_componentClause = ' contact_a.id IN ( ' . implode(',', $form->_contactIds) . ' ) '; | |
270 | $form->assign('totalSelectedContacts', count($form->_contactIds)); | |
271 | ||
272 | $form->_componentIds = $form->_contactIds; | |
273 | } | |
274 | } | |
275 | ||
276 | /** | |
277 | * Function to get the contact id for custom search | |
278 | * we are not using prev/next table incase of custom search | |
279 | */ | |
280 | public function getContactIds() { | |
281 | // need to perform action on all contacts | |
282 | // fire the query again and get the contact id's + display name | |
283 | $sortID = NULL; | |
284 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
285 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
286 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
287 | ); | |
288 | } | |
289 | ||
290 | $selectorName = $this->controller->selectorName(); | |
5d272ea2 | 291 | require_once(str_replace('_', DIRECTORY_SEPARATOR, $selectorName) . '.php'); |
6a488035 TO |
292 | |
293 | $fv = $this->get('formValues'); | |
294 | $customClass = $this->get('customSearchClass'); | |
295 | require_once 'CRM/Core/BAO/Mapping.php'; | |
296 | $returnProperties = CRM_Core_BAO_Mapping::returnProperties(self::$_searchFormValues); | |
297 | ||
5d272ea2 | 298 | $selector = new $selectorName($customClass, $fv, NULL, $returnProperties); |
6a488035 TO |
299 | |
300 | $params = $this->get('queryParams'); | |
301 | ||
302 | // fix for CRM-5165 | |
303 | $sortByCharacter = $this->get('sortByCharacter'); | |
a7cddb8c | 304 | if ($sortByCharacter && $sortByCharacter != 1) { |
6a488035 TO |
305 | $params[] = array('sortByCharacter', '=', $sortByCharacter, 0, 0); |
306 | } | |
307 | $queryOperator = $this->get('queryOperator'); | |
308 | if (!$queryOperator) { | |
309 | $queryOperator = 'AND'; | |
310 | } | |
66ceb5d9 | 311 | $dao = $selector->contactIDQuery($params, $this->_action, $sortID, |
5d272ea2 | 312 | CRM_Utils_Array::value('display_relationship_type', $fv), |
6a488035 TO |
313 | $queryOperator |
314 | ); | |
315 | ||
316 | $contactIds = array(); | |
5d272ea2 | 317 | while ($dao->fetch()) { |
6a488035 TO |
318 | $contactIds[$dao->contact_id] = $dao->contact_id; |
319 | } | |
320 | ||
321 | return $contactIds; | |
322 | } | |
323 | ||
324 | ||
325 | /** | |
326 | * This function sets the default values for the form. Relationship that in edit/view action | |
327 | * the default values are retrieved from the database | |
328 | * | |
329 | * @access public | |
330 | * | |
331 | * @return void | |
332 | */ | |
333 | function setDefaultValues() { | |
334 | $defaults = array(); | |
335 | return $defaults; | |
336 | } | |
337 | ||
338 | /** | |
339 | * This function is used to add the rules for form. | |
340 | * | |
341 | * @return void | |
342 | * @access public | |
343 | */ | |
5d272ea2 | 344 | function addRules() { |
345 | } | |
6a488035 TO |
346 | |
347 | /** | |
348 | * Function to actually build the form | |
349 | * | |
350 | * @return void | |
351 | * @access public | |
352 | */ | |
353 | public function buildQuickForm() { | |
354 | $this->addDefaultButtons(ts('Confirm Action')); | |
355 | } | |
356 | ||
357 | /** | |
358 | * process the form after the input has been submitted and validated | |
359 | * | |
360 | * @access public | |
361 | * | |
362 | * @return void | |
363 | */ | |
5d272ea2 | 364 | public function postProcess() { |
365 | } | |
366 | ||
6a488035 TO |
367 | //end of function |
368 | ||
369 | /** | |
370 | * simple shell that derived classes can call to add buttons to | |
371 | * the form with a customized title for the main Submit | |
372 | * | |
373 | * @param string $title title of the main button | |
374 | * @param string $type button type for the form after processing | |
375 | * | |
376 | * @return void | |
377 | * @access public | |
378 | */ | |
379 | function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) { | |
380 | $this->addButtons(array( | |
381 | array( | |
382 | 'type' => $nextType, | |
383 | 'name' => $title, | |
384 | 'isDefault' => TRUE, | |
385 | ), | |
386 | array( | |
387 | 'type' => $backType, | |
388 | 'name' => ts('Cancel'), | |
389 | ), | |
390 | ) | |
391 | ); | |
392 | } | |
393 | } | |
394 |