Update version numbers to 4.4 and lint php
[civicrm-core.git] / CRM / Contact / Form / Task.php
CommitLineData
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 */
40class 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');
156 $cacheKey = ($sortByCharacter && $sortByCharacter != 'all') ? "{$cacheKey}_alphabet" : $cacheKey;
8ef12e64 157
6a488035
TO
158 if ($form->_action == CRM_Core_Action::COPY) {
159 $allCids[$cacheKey] = $form->getContactIds( );
160 }
161 else {
162 $allCids = CRM_Core_BAO_PrevNextCache::getSelection($cacheKey, "getall");
163 }
164
165 $form->_contactIds = array();
166 if ($useTable) {
167 $count = 0;
168 $insertString = array();
169 foreach ($allCids[$cacheKey] as $cid => $ignore) {
170 $count++;
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();
177 }
178 }
179 if (!empty($insertString)) {
180 $string = implode(',', $insertString);
181 $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string";
182 CRM_Core_DAO::executeQuery($sql);
183 }
184 }
185 else {
186 // filter duplicates here
187 // CRM-7058
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.
191
192 foreach ($allCids[$cacheKey] as $cid => $ignore) {
193 $form->_contactIds[] = $cid;
194 }
195 }
196 }
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();
201
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);
209 if ($useTable) {
210 $insertString[] = " ( {$contactID} ) ";
211 }
212 else {
213 $form->_contactIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
214 }
215 }
216 }
217 }
218 else {
219 // fetching selected contact ids of passed cache key
220 $selectedCids = CRM_Core_BAO_PrevNextCache::getSelection($cacheKey);
221 foreach ($selectedCids[$cacheKey] as $selectedCid => $ignore) {
222 if ($useTable) {
223 $insertString[] = " ( {$selectedCid} ) ";
224 }
225 else {
226 $form->_contactIds[] = $selectedCid;
227 }
228 }
229 }
8ef12e64 230
6a488035
TO
231 if (!empty($insertString)) {
232 $string = implode(',', $insertString);
233 $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string";
234 CRM_Core_DAO::executeQuery($sql);
235 }
236 }
237
238 //contact type for pick up profiles as per selected contact types with subtypes
239 //CRM-5521
240 if ($selectedTypes = CRM_Utils_Array::value('contact_type', self::$_searchFormValues)) {
241 if (!is_array($selectedTypes)) {
242 $selectedTypes = explode(' ', $selectedTypes);
243 }
244 foreach ($selectedTypes as $ct => $dontcare) {
245 if (strpos($ct, CRM_Core_DAO::VALUE_SEPARATOR) === FALSE) {
246 $form->_contactTypes[] = $ct;
247 }
248 else {
249 $separator = strpos($ct, CRM_Core_DAO::VALUE_SEPARATOR);
250 $form->_contactTypes[] = substr($ct, $separator + 1);
251 }
252 }
253 }
254
8ef12e64 255
6a488035
TO
256 if (CRM_Utils_Array::value('radio_ts', self::$_searchFormValues) == 'ts_sel'
257 && ($form->_action != CRM_Core_Action::COPY) ) {
258 $params = array();
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);
263 }
8ef12e64 264
6a488035
TO
265 if (!empty($form->_contactIds)) {
266 $form->_componentClause = ' contact_a.id IN ( ' . implode(',', $form->_contactIds) . ' ) ';
267 $form->assign('totalSelectedContacts', count($form->_contactIds));
268
269 $form->_componentIds = $form->_contactIds;
270 }
271 }
272
273 /**
274 * Function to get the contact id for custom search
275 * we are not using prev/next table incase of custom search
276 */
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
280 $sortID = NULL;
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)
284 );
285 }
286
287 $selectorName = $this->controller->selectorName();
288 require_once (str_replace('_', DIRECTORY_SEPARATOR, $selectorName) . '.php');
289
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);
294
4d5c2eb5 295 $selector = new $selectorName(
296 $customClass, $fv, null, $returnProperties
6a488035
TO
297 );
298
299 $params = $this->get('queryParams');
300
301 // fix for CRM-5165
302 $sortByCharacter = $this->get('sortByCharacter');
303 if ($sortByCharacter &&
304 $sortByCharacter != 1
305 ) {
306 $params[] = array('sortByCharacter', '=', $sortByCharacter, 0, 0);
307 }
308 $queryOperator = $this->get('queryOperator');
309 if (!$queryOperator) {
310 $queryOperator = 'AND';
311 }
312 $dao = &$selector->contactIDQuery($params, $this->_action, $sortID,
313 CRM_Utils_Array::value('display_relationship_type', $fv ),
314 $queryOperator
315 );
316
317 $contactIds = array();
318 while( $dao->fetch()) {
319 $contactIds[$dao->contact_id] = $dao->contact_id;
320 }
321
322 return $contactIds;
323 }
324
325
326 /**
327 * This function sets the default values for the form. Relationship that in edit/view action
328 * the default values are retrieved from the database
329 *
330 * @access public
331 *
332 * @return void
333 */
334 function setDefaultValues() {
335 $defaults = array();
336 return $defaults;
337 }
338
339 /**
340 * This function is used to add the rules for form.
341 *
342 * @return void
343 * @access public
344 */
345 function addRules() {}
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 */
364 public function postProcess() {}
365 //end of function
366
367 /**
368 * simple shell that derived classes can call to add buttons to
369 * the form with a customized title for the main Submit
370 *
371 * @param string $title title of the main button
372 * @param string $type button type for the form after processing
373 *
374 * @return void
375 * @access public
376 */
377 function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
378 $this->addButtons(array(
379 array(
380 'type' => $nextType,
381 'name' => $title,
382 'isDefault' => TRUE,
383 ),
384 array(
385 'type' => $backType,
386 'name' => ts('Cancel'),
387 ),
388 )
389 );
390 }
391}
392