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