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