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
= array();
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');
65 $this->loadStandardSearchOptionsFromUrl();
67 //operation for state machine.
68 $this->_operation
= CRM_Utils_Request
::retrieve('op', 'String', $this, FALSE, 'reserve');
70 if (!in_array($this->_operation
, array(
76 $this->_operation
= 'reserve';
77 $this->set('op', $this->_operation
);
79 $this->set('searchVoterFor', $this->_operation
);
80 $this->assign('searchVoterFor', $this->_operation
);
81 $this->assign('isFormSubmitted', $this->isSubmitted());
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")
88 CRM_Utils_System
::permissionDenied();
89 CRM_Utils_System
::civiExit();
92 $this->assign("context", $this->_context
);
94 // get user submitted values
95 // get it from controller only if form has been submitted, else preProcess has set this
98 $this->_formValues
= $this->get('formValues');
101 $this->_formValues
= $this->controller
->exportValues($this->_name
);
105 $this->postProcess();
106 $this->set('force', 0);
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
)
116 //get the voter clause.
117 $voterClause = $this->voterClause();
119 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
121 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
129 if ($this->_context
== 'user') {
130 $prefix = $this->_prefix
;
133 $this->assign("{$prefix}limit", $this->_limit
);
134 $this->assign("{$prefix}single", $this->_single
);
136 $controller = new CRM_Core_Selector_Controller($selector,
137 $this->get(CRM_Utils_Pager
::PAGE_ID
),
139 CRM_Core_Action
::VIEW
,
141 CRM_Core_Selector_Controller
::TRANSFER
,
145 $controller->setEmbedded(TRUE);
146 $controller->moveFromSessionToTemplate();
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)));
154 //set the form title.
155 CRM_Utils_System
::setTitle(ts('Find Respondents To %1', array(1 => ucfirst($this->_operation
))));
159 * Load the default survey for all actions.
163 public function setDefaultValues() {
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;
171 return $this->_defaults
;
175 * Build the form object.
177 public function buildQuickForm() {
178 parent
::buildQuickForm();
179 //build the search form.
180 CRM_Campaign_BAO_Query
::buildSearchForm($this);
182 $rows = $this->get('rows');
183 if (is_array($rows)) {
184 if (!$this->_single
) {
185 $this->addRowSelectors($rows);
188 $allTasks = CRM_Campaign_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission());
190 //hack to serve right page to state machine.
191 $taskMapping = array(
192 'interview' => CRM_Campaign_Task
::INTERVIEW
,
193 'reserve' => CRM_Campaign_Task
::RESERVE
,
194 'release' => CRM_Campaign_Task
::RELEASE
,
197 $currentTaskValue = CRM_Utils_Array
::value($this->_operation
, $taskMapping);
198 $taskValue = array($currentTaskValue => $allTasks[$currentTaskValue]);
199 if ($this->_operation
== 'interview' && !empty($this->_formValues
['campaign_survey_id'])) {
200 $activityTypes = CRM_Core_PseudoConstant
::activityType(FALSE, TRUE, FALSE, 'label', TRUE);
202 $surveyTypeId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
203 $this->_formValues
['campaign_survey_id'],
207 $currentTaskValue => ts('Record %1 Responses',
208 array(1 => $activityTypes[$surveyTypeId])
213 $this->addTaskMenu($taskValue);
219 * The post processing of the form gets done here.
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
227 * The processing consists of using a Selector / Controller framework for getting the
230 public function postProcess() {
237 if (!empty($_POST)) {
238 $this->_formValues
= $this->controller
->exportValues($this->_name
);
241 $this->fixFormValues();
243 //format params as per task.
244 $this->formatParams();
246 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
248 $this->set('formValues', $this->_formValues
);
249 $this->set('queryParams', $this->_queryParams
);
251 $buttonName = $this->controller
->getButtonName();
252 if ($buttonName == $this->_actionButtonName
) {
253 // check actionName and if next, then do not repeat a search, since we are going to the next page
255 // hack, make sure we reset the task values
256 $stateMachine = $this->controller
->getStateMachine();
257 $formName = $stateMachine->getTaskFormName();
259 $this->controller
->resetPage($formName);
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
)
270 //get the voter clause.
271 $voterClause = $this->voterClause();
273 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
280 $selector->setKey($this->controller
->_key
);
283 if ($this->_context
== 'basic' ||
284 $this->_context
== 'user'
286 $prefix = $this->_prefix
;
289 $controller = new CRM_Core_Selector_Controller($selector,
290 $this->get(CRM_Utils_Pager
::PAGE_ID
),
292 CRM_Core_Action
::VIEW
,
294 CRM_Core_Selector_Controller
::SESSION
,
297 $controller->setEmbedded(TRUE);
298 $query = $selector->getQuery();
299 if ($this->_context
== 'user') {
300 $query->setSkipPermission(TRUE);
305 public function formatParams() {
306 $interviewerId = CRM_Utils_Array
::value('survey_interviewer_id', $this->_formValues
);
307 if ($interviewerId) {
308 $this->set('interviewerId', $interviewerId);
311 //format multi-select group and contact types.
312 foreach (array('group', 'contact_type') as $param) {
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;
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
));
327 if ($contactType && in_array($this->_operation
, ['reserve', 'interview'])) {
328 $this->_formValues
['contact_type'][$contactType] = 1;
331 if ($this->_operation
== 'reserve') {
332 if (!empty($this->_formValues
['campaign_survey_id'])) {
333 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
334 $this->_formValues
['campaign_survey_id'],
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);
344 foreach ($campGroups as $id => $title) {
345 $this->_formValues
['group'][$id] = 1;
349 //carry servey id w/ this.
350 $this->set('surveyId', $this->_formValues
['campaign_survey_id']);
351 unset($this->_formValues
['campaign_survey_id']);
353 unset($this->_formValues
['survey_interviewer_id']);
355 elseif ($this->_operation
== 'interview' ||
356 $this->_operation
== 'release'
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;
365 //pass voter search operation.
366 $this->_formValues
['campaign_search_voter_for'] = $this->_operation
;
369 public function fixFormValues() {
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 :)
374 //since we have qfKey, no need to manipulate set defaults.
375 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String');
377 if (!$this->_force || CRM_Utils_Rule
::qfKey($qfKey)) {
382 $surveyId = CRM_Utils_Request
::retrieve('sid', 'Positive');
385 $surveyId = CRM_Utils_Type
::escape($surveyId, 'Integer');
388 // use default survey id
389 $surveyId = key(CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE));
392 CRM_Core_Error
::statusBounce(ts('Could not find valid Survey Id.'));
394 $this->_formValues
['campaign_survey_id'] = $this->_formValues
['campaign_survey_id'] = $surveyId;
396 $session = CRM_Core_Session
::singleton();
397 $userId = $session->get('userID');
399 // get interviewer id
400 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive',
401 CRM_Core_DAO
::$_nullObject, FALSE, $userId
403 //to force other contact as interviewer, user should be admin.
404 if ($cid != $userId &&
405 !CRM_Core_Permission
::check('administer CiviCampaign')
407 CRM_Utils_System
::permissionDenied();
408 CRM_Utils_System
::civiExit();
410 $this->_formValues
['survey_interviewer_id'] = $cid;
411 //get all in defaults.
412 $this->_defaults
= $this->_formValues
;
413 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
419 public function voterClause() {
420 $params = array('campaign_search_voter_for' => $this->_operation
);
422 $clauseFields = array(
423 'surveyId' => 'campaign_survey_id',
424 'interviewerId' => 'survey_interviewer_id',
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);
435 $voterClause = CRM_Campaign_BAO_Query
::voterClause($params);
441 * Return a descriptive name for the page, used in wizard header
445 public function getTitle() {
446 return ts('Find Respondents');