3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 class CRM_Campaign_Form_Search
extends CRM_Core_Form_Search
{
24 * The params that are sent to the query.
28 protected $_queryParams;
31 * Are we restricting ourselves to a single contact.
35 protected $_single = FALSE;
38 * Are we restricting ourselves to a single contact.
42 protected $_limit = NULL;
45 * Prefix for the controller.
48 protected $_prefix = "survey_";
51 private $_operation = 'reserve';
54 * Processing needed for buildForm and later.
56 public function preProcess() {
58 $this->_defaults
= [];
60 //set the button name.
61 $this->_printButtonName
= $this->getButtonName('next', 'print');
62 $this->_actionButtonName
= $this->getButtonName('next', 'action');
64 $this->loadStandardSearchOptionsFromUrl();
66 //operation for state machine.
67 $this->_operation
= CRM_Utils_Request
::retrieve('op', 'String', $this, FALSE, 'reserve');
69 if (!in_array($this->_operation
, array(
75 $this->_operation
= 'reserve';
76 $this->set('op', $this->_operation
);
78 $this->set('searchVoterFor', $this->_operation
);
79 $this->assign('searchVoterFor', $this->_operation
);
80 $this->assign('isFormSubmitted', $this->isSubmitted());
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")
87 CRM_Utils_System
::permissionDenied();
88 CRM_Utils_System
::civiExit();
91 $this->assign("context", $this->_context
);
93 // get user submitted values
94 // get it from controller only if form has been submitted, else preProcess has set this
97 $this->_formValues
= $this->get('formValues');
100 $this->_formValues
= $this->controller
->exportValues($this->_name
);
104 $this->postProcess();
105 $this->set('force', 0);
108 //get the voter clause.
109 $voterClause = $this->voterClause();
111 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
113 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
121 if ($this->_context
== 'user') {
122 $prefix = $this->_prefix
;
125 $this->assign("{$prefix}limit", $this->_limit
);
126 $this->assign("{$prefix}single", $this->_single
);
128 $controller = new CRM_Core_Selector_Controller($selector,
129 $this->get(CRM_Utils_Pager
::PAGE_ID
),
131 CRM_Core_Action
::VIEW
,
133 CRM_Core_Selector_Controller
::TRANSFER
,
137 $controller->setEmbedded(TRUE);
138 $controller->moveFromSessionToTemplate();
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)));
146 //set the form title.
147 $this->setTitle(ts('Find Respondents To %1', array(1 => ucfirst($this->_operation
))));
151 * Load the default survey for all actions.
155 public function setDefaultValues() {
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;
163 return $this->_defaults
;
167 * Build the form object.
169 public function buildQuickForm() {
170 parent
::buildQuickForm();
171 //build the search form.
172 CRM_Campaign_BAO_Query
::buildSearchForm($this);
174 $rows = $this->get('rows');
175 if (is_array($rows)) {
176 if (!$this->_single
) {
177 $this->addRowSelectors($rows);
180 $allTasks = CRM_Campaign_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission());
182 //hack to serve right page to state machine.
183 $taskMapping = array(
184 'interview' => CRM_Campaign_Task
::INTERVIEW
,
185 'reserve' => CRM_Campaign_Task
::RESERVE
,
186 'release' => CRM_Campaign_Task
::RELEASE
,
189 $currentTaskValue = $taskMapping[$this->_operation
] ??
NULL;
190 $taskValue = array($currentTaskValue => $allTasks[$currentTaskValue]);
191 if ($this->_operation
== 'interview' && !empty($this->_formValues
['campaign_survey_id'])) {
192 $activityTypes = CRM_Core_PseudoConstant
::activityType(FALSE, TRUE, FALSE, 'label', TRUE);
194 $surveyTypeId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
195 $this->_formValues
['campaign_survey_id'],
199 $currentTaskValue => ts('Record %1 Responses',
200 array(1 => $activityTypes[$surveyTypeId])
205 $this->addTaskMenu($taskValue);
211 * The post processing of the form gets done here.
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
219 * The processing consists of using a Selector / Controller framework for getting the
222 public function postProcess() {
229 if (!empty($_POST)) {
230 $this->_formValues
= $this->controller
->exportValues($this->_name
);
233 $this->fixFormValues();
235 //format params as per task.
236 $this->formatParams();
238 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
240 $this->set('formValues', $this->_formValues
);
241 $this->set('queryParams', $this->_queryParams
);
243 $buttonName = $this->controller
->getButtonName();
244 if ($buttonName == $this->_actionButtonName
) {
245 // check actionName and if next, then do not repeat a search, since we are going to the next page
247 // hack, make sure we reset the task values
248 $stateMachine = $this->controller
->getStateMachine();
249 $formName = $stateMachine->getTaskFormName();
251 $this->controller
->resetPage($formName);
255 //get the voter clause.
256 $voterClause = $this->voterClause();
258 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
265 $selector->setKey($this->controller
->_key
);
268 if ($this->_context
== 'basic' ||
269 $this->_context
== 'user'
271 $prefix = $this->_prefix
;
274 $controller = new CRM_Core_Selector_Controller($selector,
275 $this->get(CRM_Utils_Pager
::PAGE_ID
),
277 CRM_Core_Action
::VIEW
,
279 CRM_Core_Selector_Controller
::SESSION
,
282 $controller->setEmbedded(TRUE);
283 $query = $selector->getQuery();
284 if ($this->_context
== 'user') {
285 $query->setSkipPermission(TRUE);
290 public function formatParams() {
291 $interviewerId = $this->_formValues
['survey_interviewer_id'] ??
NULL;
292 if ($interviewerId) {
293 $this->set('interviewerId', $interviewerId);
296 //format multi-select group and contact types.
297 foreach (array('group', 'contact_type') as $param) {
301 $paramValue = $this->_formValues
[$param] ??
NULL;
302 if ($paramValue && is_array($paramValue)) {
303 unset($this->_formValues
[$param]);
304 foreach ($paramValue as $key => $value) {
305 $this->_formValues
[$param][$value] = 1;
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
));
312 if ($contactType && in_array($this->_operation
, ['reserve', 'interview'])) {
313 $this->_formValues
['contact_type'][$contactType] = 1;
316 if ($this->_operation
== 'reserve') {
317 if (!empty($this->_formValues
['campaign_survey_id'])) {
318 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
319 $this->_formValues
['campaign_survey_id'],
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
326 $groups = $this->_formValues
['group'] ??
NULL;
327 if ($campaignId && CRM_Utils_System
::isNull($groups)) {
328 $campGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
329 foreach ($campGroups as $id => $title) {
330 $this->_formValues
['group'][$id] = 1;
334 //carry servey id w/ this.
335 $this->set('surveyId', $this->_formValues
['campaign_survey_id']);
336 unset($this->_formValues
['campaign_survey_id']);
338 unset($this->_formValues
['survey_interviewer_id']);
340 elseif ($this->_operation
== 'interview' ||
341 $this->_operation
== 'release'
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;
350 //pass voter search operation.
351 $this->_formValues
['campaign_search_voter_for'] = $this->_operation
;
354 public function fixFormValues() {
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 :)
359 //since we have qfKey, no need to manipulate set defaults.
360 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String');
362 if (!$this->_force || CRM_Utils_Rule
::qfKey($qfKey)) {
367 $surveyId = CRM_Utils_Request
::retrieve('sid', 'Positive');
370 $surveyId = CRM_Utils_Type
::escape($surveyId, 'Integer');
373 // use default survey id
374 $surveyId = key(CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE));
377 CRM_Core_Error
::statusBounce(ts('Could not find valid Survey Id.'));
379 $this->_formValues
['campaign_survey_id'] = $this->_formValues
['campaign_survey_id'] = $surveyId;
381 $session = CRM_Core_Session
::singleton();
382 $userId = $session->get('userID');
384 // get interviewer id
385 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive',
386 CRM_Core_DAO
::$_nullObject, FALSE, $userId
388 //to force other contact as interviewer, user should be admin.
389 if ($cid != $userId &&
390 !CRM_Core_Permission
::check('administer CiviCampaign')
392 CRM_Utils_System
::permissionDenied();
393 CRM_Utils_System
::civiExit();
395 $this->_formValues
['survey_interviewer_id'] = $cid;
396 //get all in defaults.
397 $this->_defaults
= $this->_formValues
;
398 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
404 public function voterClause() {
405 $params = array('campaign_search_voter_for' => $this->_operation
);
407 $clauseFields = array(
408 'surveyId' => 'campaign_survey_id',
409 'interviewerId' => 'survey_interviewer_id',
412 foreach ($clauseFields as $param => $key) {
413 $params[$key] = $this->_formValues
[$key] ??
NULL;
414 if (!$params[$key]) {
415 $params[$key] = $this->get($param);
420 $voterClause = CRM_Campaign_BAO_Query
::voterClause($params);
426 * Return a descriptive name for the page, used in wizard header
430 public function getTitle() {
431 return ts('Find Respondents');