Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Files required | |
38 | */ | |
3efb5b86 | 39 | class CRM_Campaign_Form_Search extends CRM_Core_Form_Search { |
6a488035 TO |
40 | |
41 | /** | |
42 | * Are we forced to run a search | |
43 | * | |
44 | * @var int | |
45 | * @access protected | |
46 | */ | |
47 | protected $_force; | |
48 | ||
49 | /** | |
50 | * name of search button | |
51 | * | |
52 | * @var string | |
53 | * @access protected | |
54 | */ | |
55 | protected $_searchButtonName; | |
56 | ||
6a488035 TO |
57 | /** |
58 | * name of action button | |
59 | * | |
60 | * @var string | |
61 | * @access protected | |
62 | */ | |
63 | protected $_actionButtonName; | |
64 | ||
65 | /** | |
66 | * form values that we will be using | |
67 | * | |
68 | * @var array | |
69 | * @access protected | |
70 | */ | |
71 | protected $_formValues; | |
72 | ||
73 | /** | |
74 | * the params that are sent to the query | |
75 | * | |
76 | * @var array | |
77 | * @access protected | |
78 | */ | |
79 | protected $_queryParams; | |
80 | ||
81 | /** | |
82 | * have we already done this search | |
83 | * | |
84 | * @access protected | |
85 | * @var boolean | |
86 | */ | |
87 | protected $_done; | |
88 | ||
89 | /** | |
90 | * are we restricting ourselves to a single contact | |
91 | * | |
92 | * @access protected | |
93 | * @var boolean | |
94 | */ | |
95 | protected $_single = FALSE; | |
96 | ||
97 | /** | |
98 | * are we restricting ourselves to a single contact | |
99 | * | |
100 | * @access protected | |
101 | * @var boolean | |
102 | */ | |
103 | protected $_limit = NULL; | |
104 | ||
105 | /** | |
106 | * what context are we being invoked from | |
107 | * | |
108 | * @access protected | |
109 | * @var string | |
110 | */ | |
111 | protected $_context = NULL; | |
112 | ||
113 | protected $_defaults; | |
114 | ||
115 | /** | |
116 | * prefix for the controller | |
117 | * | |
118 | */ | |
119 | protected $_prefix = "survey_"; | |
120 | ||
121 | ||
122 | private $_operation = 'reserve'; | |
123 | ||
124 | /** | |
125 | * processing needed for buildForm and later | |
126 | * | |
127 | * @return void | |
128 | * @access public | |
129 | */ function preProcess() { | |
130 | $this->_done = FALSE; | |
131 | $this->_defaults = array(); | |
132 | ||
133 | //set the button name. | |
134 | $this->_searchButtonName = $this->getButtonName('refresh'); | |
135 | $this->_printButtonName = $this->getButtonName('next', 'print'); | |
136 | $this->_actionButtonName = $this->getButtonName('next', 'action'); | |
137 | ||
138 | //we allow the controller to set force/reset externally, | |
139 | //useful when we are being driven by the wizard framework | |
140 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this); | |
141 | $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE); | |
142 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search'); | |
143 | $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject); | |
144 | ||
145 | //operation for state machine. | |
146 | $this->_operation = CRM_Utils_Request::retrieve('op', 'String', $this, FALSE, 'reserve'); | |
147 | //validate operation. | |
148 | if (!in_array($this->_operation, array( | |
149 | 'reserve', 'release', 'interview'))) { | |
150 | $this->_operation = 'reserve'; | |
151 | $this->set('op', $this->_operation); | |
152 | } | |
153 | $this->set('searchVoterFor', $this->_operation); | |
154 | $this->assign('searchVoterFor', $this->_operation); | |
155 | $this->assign('isFormSubmitted', $this->isSubmitted()); | |
156 | ||
157 | //do check permissions. | |
158 | if (!CRM_Core_Permission::check('administer CiviCampaign') && | |
159 | !CRM_Core_Permission::check('manage campaign') && | |
160 | !CRM_Core_Permission::check("{$this->_operation} campaign contacts") | |
161 | ) { | |
162 | CRM_Utils_System::permissionDenied(); | |
163 | CRM_Utils_System::civiExit(); | |
164 | } | |
165 | ||
166 | $this->assign("context", $this->_context); | |
167 | ||
168 | // get user submitted values | |
169 | // get it from controller only if form has been submitted, else preProcess has set this | |
170 | ||
171 | if (empty($_POST)) { | |
172 | $this->_formValues = $this->get('formValues'); | |
173 | } | |
174 | else { | |
175 | $this->_formValues = $this->controller->exportValues($this->_name); | |
176 | } | |
177 | ||
178 | if ($this->_force) { | |
179 | $this->postProcess(); | |
180 | $this->set('force', 0); | |
181 | } | |
182 | ||
183 | $sortID = NULL; | |
184 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
185 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
186 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
187 | ); | |
188 | } | |
189 | ||
190 | //get the voter clause. | |
191 | $voterClause = $this->voterClause(); | |
192 | ||
193 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
194 | ||
195 | $selector = new CRM_Campaign_Selector_Search($this->_queryParams, | |
196 | $this->_action, | |
197 | $voterClause, | |
198 | $this->_single, | |
199 | $this->_limit, | |
200 | $this->_context | |
201 | ); | |
202 | $prefix = NULL; | |
203 | if ($this->_context == 'user') { | |
204 | $prefix = $this->_prefix; | |
205 | } | |
206 | ||
207 | $this->assign("{$prefix}limit", $this->_limit); | |
208 | $this->assign("{$prefix}single", $this->_single); | |
209 | ||
210 | $controller = new CRM_Core_Selector_Controller($selector, | |
211 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
212 | $sortID, | |
213 | CRM_Core_Action::VIEW, | |
214 | $this, | |
215 | CRM_Core_Selector_Controller::TRANSFER, | |
216 | $prefix | |
217 | ); | |
218 | ||
219 | $controller->setEmbedded(TRUE); | |
220 | $controller->moveFromSessionToTemplate(); | |
221 | ||
222 | //append breadcrumb to survey dashboard. | |
223 | if (CRM_Campaign_BAO_Campaign::accessCampaign()) { | |
224 | $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey'); | |
225 | CRM_Utils_System::appendBreadCrumb(array(array('title' => ts('Survey(s)'), 'url' => $url))); | |
226 | } | |
227 | ||
228 | //set the form title. | |
229 | CRM_Utils_System::setTitle(ts('Find Respondents To %1', array(1 => ucfirst($this->_operation)))); | |
230 | } | |
231 | ||
232 | function setDefaultValues() { | |
233 | //load the default survey for all actions. | |
234 | if (empty($this->_defaults)) { | |
235 | $defaultSurveyId = key(CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE)); | |
236 | if ($defaultSurveyId) { | |
237 | $this->_defaults['campaign_survey_id'] = $defaultSurveyId; | |
238 | } | |
239 | } | |
240 | ||
241 | return $this->_defaults; | |
242 | } | |
243 | ||
244 | /** | |
245 | * Build the form | |
246 | * | |
247 | * @access public | |
248 | * | |
249 | * @return void | |
250 | */ | |
251 | function buildQuickForm() { | |
3efb5b86 | 252 | parent::buildQuickForm(); |
6a488035 TO |
253 | //build the search form. |
254 | CRM_Campaign_BAO_Query::buildSearchForm($this); | |
255 | ||
8ef12e64 | 256 | /* |
257 | * add form checkboxes for each row. This is needed out here to conform to QF protocol | |
258 | * of all elements being declared in builQuickForm | |
6a488035 TO |
259 | */ |
260 | ||
261 | ||
262 | $rows = $this->get('rows'); | |
263 | if (is_array($rows)) { | |
264 | if (!$this->_single) { | |
d664f648 | 265 | $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows')); |
6a488035 TO |
266 | foreach ($rows as $row) { |
267 | $this->addElement('checkbox', $row['checkbox'], | |
268 | NULL, NULL, | |
d664f648 | 269 | array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row') |
6a488035 TO |
270 | ); |
271 | } | |
272 | } | |
273 | ||
274 | $total = $cancel = 0; | |
275 | ||
276 | $permission = CRM_Core_Permission::getPermission(); | |
277 | $allTasks = CRM_Campaign_Task::permissionedTaskTitles($permission); | |
278 | ||
279 | //hack to serve right page to state machine. | |
280 | $taskMapping = array( | |
281 | 'interview' => 1, | |
282 | 'reserve' => 2, | |
283 | 'release' => 3, | |
284 | ); | |
285 | ||
286 | $currentTaskValue = CRM_Utils_Array::value($this->_operation, $taskMapping); | |
287 | $taskValue = array($currentTaskValue => $allTasks[$currentTaskValue]); | |
8cc574cf | 288 | if ($this->_operation == 'interview' && !empty($this->_formValues['campaign_survey_id'])) { |
6a488035 TO |
289 | $activityTypes = CRM_Core_PseudoConstant::activityType(FALSE, TRUE, FALSE, 'label', TRUE); |
290 | ||
291 | $surveyTypeId = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', | |
292 | $this->_formValues['campaign_survey_id'], | |
293 | 'activity_type_id' | |
294 | ); | |
295 | $taskValue = array( | |
296 | $currentTaskValue => ts('Record %1 Responses', | |
297 | array(1 => $activityTypes[$surveyTypeId]) | |
298 | )); | |
299 | } | |
300 | ||
45008844 | 301 | $this->add('select', 'task', ts('Actions:') . ' ', array('' => ts('- actions -')) + $taskValue); |
6a488035 TO |
302 | |
303 | $this->add('submit', $this->_actionButtonName, ts('Go'), | |
304 | array( | |
305 | 'class' => 'form-submit', | |
306 | 'id' => 'Go', | |
307 | ) | |
308 | ); | |
309 | ||
6a488035 TO |
310 | // need to perform tasks on all or selected items ? using radio_ts(task selection) for it |
311 | $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked')); | |
d664f648 | 312 | $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('class' => 'select-rows', 'onclick' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );")); |
6a488035 TO |
313 | } |
314 | ||
6a488035 TO |
315 | } |
316 | ||
317 | /** | |
318 | * The post processing of the form gets done here. | |
319 | * | |
320 | * Key things done during post processing are | |
321 | * - check for reset or next request. if present, skip post procesing. | |
322 | * - now check if user requested running a saved search, if so, then | |
323 | * the form values associated with the saved search are used for searching. | |
324 | * - if user has done a submit with new values the regular post submissing is | |
325 | * done. | |
326 | * The processing consists of using a Selector / Controller framework for getting the | |
327 | * search results. | |
328 | * | |
329 | * @param | |
330 | * | |
331 | * @return void | |
332 | * @access public | |
333 | */ | |
334 | function postProcess() { | |
335 | if ($this->_done) { | |
336 | return; | |
337 | } | |
338 | ||
339 | $this->_done = TRUE; | |
340 | ||
341 | if (!empty($_POST)) { | |
342 | $this->_formValues = $this->controller->exportValues($this->_name); | |
343 | } | |
344 | ||
345 | $this->fixFormValues(); | |
346 | ||
347 | //format params as per task. | |
348 | $this->formatParams(); | |
349 | ||
350 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
351 | ||
352 | $this->set('formValues', $this->_formValues); | |
353 | $this->set('queryParams', $this->_queryParams); | |
354 | ||
355 | $buttonName = $this->controller->getButtonName(); | |
45008844 | 356 | if ($buttonName == $this->_actionButtonName) { |
6a488035 TO |
357 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
358 | ||
359 | // hack, make sure we reset the task values | |
360 | $stateMachine = $this->controller->getStateMachine(); | |
361 | $formName = $stateMachine->getTaskFormName(); | |
362 | ||
363 | $this->controller->resetPage($formName); | |
364 | return; | |
365 | } | |
366 | ||
367 | $sortID = NULL; | |
368 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
369 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
370 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
371 | ); | |
372 | } | |
373 | ||
374 | //get the voter clause. | |
375 | $voterClause = $this->voterClause(); | |
376 | ||
377 | $selector = new CRM_Campaign_Selector_Search($this->_queryParams, | |
378 | $this->_action, | |
379 | $voterClause, | |
380 | $this->_single, | |
381 | $this->_limit, | |
382 | $this->_context | |
383 | ); | |
384 | $selector->setKey($this->controller->_key); | |
385 | ||
386 | $prefix = NULL; | |
387 | if ($this->_context == 'basic' || | |
388 | $this->_context == 'user' | |
389 | ) { | |
390 | $prefix = $this->_prefix; | |
391 | } | |
392 | ||
393 | $controller = new CRM_Core_Selector_Controller($selector, | |
394 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
395 | $sortID, | |
396 | CRM_Core_Action::VIEW, | |
397 | $this, | |
398 | CRM_Core_Selector_Controller::SESSION, | |
399 | $prefix | |
400 | ); | |
401 | $controller->setEmbedded(TRUE); | |
402 | $query = $selector->getQuery(); | |
403 | if ($this->_context == 'user') { | |
404 | $query->setSkipPermission(TRUE); | |
405 | } | |
406 | $controller->run(); | |
407 | } | |
408 | ||
409 | function formatParams() { | |
410 | $interviewerId = CRM_Utils_Array::value('survey_interviewer_id', $this->_formValues); | |
64e716d7 JM |
411 | if ($interviewerId) { |
412 | $this->set('interviewerId', $interviewerId); | |
6a488035 TO |
413 | } |
414 | ||
415 | //format multi-select group and contact types. | |
56ecdf7b | 416 | foreach (array('group', 'contact_type') as $param) { |
6a488035 TO |
417 | if ($this->_force) { |
418 | continue; | |
419 | } | |
420 | $paramValue = CRM_Utils_Array::value($param, $this->_formValues); | |
421 | if ($paramValue && is_array($paramValue)) { | |
422 | unset($this->_formValues[$param]); | |
423 | foreach ($paramValue as $key => $value) { | |
424 | $this->_formValues[$param][$value] = 1; | |
425 | } | |
426 | } | |
427 | } | |
428 | ||
429 | //apply filter of survey contact type for search. | |
430 | $contactType = CRM_Campaign_BAO_Survey::getSurveyContactType(CRM_Utils_Array::value('campaign_survey_id', $this->_formValues)); | |
431 | if ($contactType && in_array($this->_operation, array( | |
432 | 'reserve', 'interview'))) { | |
433 | $this->_formValues['contact_type'][$contactType] = 1; | |
434 | } | |
435 | ||
436 | if ($this->_operation == 'reserve') { | |
a7488080 | 437 | if (!empty($this->_formValues['campaign_survey_id'])) { |
6a488035 TO |
438 | $campaignId = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', |
439 | $this->_formValues['campaign_survey_id'], | |
440 | 'campaign_id' | |
441 | ); | |
442 | ||
443 | //allow voter search in sub-part of given constituents, | |
444 | //but make sure in case user does not select any group. | |
445 | //get all associated campaign groups in where filter, CRM-7406 | |
446 | $groups = CRM_Utils_Array::value('group', $this->_formValues); | |
447 | if ($campaignId && CRM_Utils_System::isNull($groups)) { | |
448 | $campGroups = CRM_Campaign_BAO_Campaign::getCampaignGroups($campaignId); | |
449 | foreach ($campGroups as $id => $title) $this->_formValues['group'][$id] = 1; | |
450 | } | |
451 | ||
452 | //carry servey id w/ this. | |
453 | $this->set('surveyId', $this->_formValues['campaign_survey_id']); | |
454 | unset($this->_formValues['campaign_survey_id']); | |
455 | } | |
456 | unset($this->_formValues['survey_interviewer_id']); | |
457 | } | |
458 | elseif ($this->_operation == 'interview' || | |
459 | $this->_operation == 'release' | |
460 | ) { | |
461 | //to conduct interview / release activity status should be scheduled. | |
462 | $activityStatus = CRM_Core_PseudoConstant::activityStatus('name'); | |
463 | if ($scheduledStatusId = array_search('Scheduled', $activityStatus)) { | |
464 | $this->_formValues['survey_status_id'] = $scheduledStatusId; | |
465 | } | |
466 | } | |
467 | ||
468 | //pass voter search operation. | |
469 | $this->_formValues['campaign_search_voter_for'] = $this->_operation; | |
470 | } | |
471 | ||
472 | function fixFormValues() { | |
473 | // if this search has been forced | |
474 | // then see if there are any get values, and if so over-ride the post values | |
475 | // note that this means that GET over-rides POST :) | |
476 | ||
477 | //since we have qfKey, no need to manipulate set defaults. | |
478 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject); | |
479 | ||
480 | if (!$this->_force || CRM_Utils_Rule::qfKey($qfKey)) { | |
481 | return; | |
482 | } | |
483 | ||
484 | // get survey id | |
485 | $surveyId = CRM_Utils_Request::retrieve('sid', 'Positive', CRM_Core_DAO::$_nullObject); | |
486 | ||
487 | if ($surveyId) { | |
488 | $surveyId = CRM_Utils_Type::escape($surveyId, 'Integer'); | |
489 | } | |
490 | else { | |
491 | // use default survey id | |
492 | $surveyId = key(CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE)); | |
493 | } | |
494 | if (!$surveyId) { | |
495 | CRM_Core_Error::fatal('Could not find valid Survey Id.'); | |
496 | } | |
497 | $this->_formValues['campaign_survey_id'] = $this->_formValues['campaign_survey_id'] = $surveyId; | |
498 | ||
499 | $session = CRM_Core_Session::singleton(); | |
500 | $userId = $session->get('userID'); | |
501 | ||
502 | // get interviewer id | |
503 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', | |
504 | CRM_Core_DAO::$_nullObject, FALSE, $userId | |
505 | ); | |
506 | //to force other contact as interviewer, user should be admin. | |
507 | if ($cid != $userId && | |
508 | !CRM_Core_Permission::check('administer CiviCampaign') | |
509 | ) { | |
510 | CRM_Utils_System::permissionDenied(); | |
511 | CRM_Utils_System::civiExit(); | |
512 | } | |
513 | $this->_formValues['survey_interviewer_id'] = $cid; | |
6a488035 TO |
514 | //get all in defaults. |
515 | $this->_defaults = $this->_formValues; | |
516 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this); | |
517 | } | |
518 | ||
30c4e065 EM |
519 | /** |
520 | * @return array | |
521 | */ | |
6a488035 TO |
522 | function voterClause() { |
523 | $params = array('campaign_search_voter_for' => $this->_operation); | |
524 | ||
525 | $clauseFields = array( | |
526 | 'surveyId' => 'campaign_survey_id', | |
527 | 'interviewerId' => 'survey_interviewer_id', | |
528 | ); | |
529 | ||
530 | foreach ($clauseFields as $param => $key) { | |
531 | $params[$key] = CRM_Utils_Array::value($key, $this->_formValues); | |
532 | if (!$params[$key]) { | |
533 | $params[$key] = $this->get($param); | |
534 | } | |
535 | } | |
536 | ||
537 | //build the clause. | |
538 | $voterClause = CRM_Campaign_BAO_Query::voterClause($params); | |
539 | ||
540 | return $voterClause; | |
541 | } | |
542 | ||
543 | /** | |
544 | * Return a descriptive name for the page, used in wizard header | |
545 | * | |
546 | * @return string | |
547 | * @access public | |
548 | */ | |
549 | public function getTitle() { | |
550 | return ts('Find Respondents'); | |
551 | } | |
552 | } | |
553 |