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