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