3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
37 class CRM_Campaign_Form_Search
extends CRM_Core_Form_Search
{
40 * The params that are sent to the query.
44 protected $_queryParams;
47 * Are we restricting ourselves to a single contact.
51 protected $_single = FALSE;
54 * Are we restricting ourselves to a single contact.
58 protected $_limit = NULL;
61 * Prefix for the controller.
64 protected $_prefix = "survey_";
67 private $_operation = 'reserve';
70 * Processing needed for buildForm and later.
72 public function preProcess() {
74 $this->_defaults
= array();
76 //set the button name.
77 $this->_searchButtonName
= $this->getButtonName('refresh');
78 $this->_printButtonName
= $this->getButtonName('next', 'print');
79 $this->_actionButtonName
= $this->getButtonName('next', 'action');
81 $this->loadStandardSearchOptionsFromUrl();
83 //operation for state machine.
84 $this->_operation
= CRM_Utils_Request
::retrieve('op', 'String', $this, FALSE, 'reserve');
86 if (!in_array($this->_operation
, array(
92 $this->_operation
= 'reserve';
93 $this->set('op', $this->_operation
);
95 $this->set('searchVoterFor', $this->_operation
);
96 $this->assign('searchVoterFor', $this->_operation
);
97 $this->assign('isFormSubmitted', $this->isSubmitted());
99 //do check permissions.
100 if (!CRM_Core_Permission
::check('administer CiviCampaign') &&
101 !CRM_Core_Permission
::check('manage campaign') &&
102 !CRM_Core_Permission
::check("{$this->_operation} campaign contacts")
104 CRM_Utils_System
::permissionDenied();
105 CRM_Utils_System
::civiExit();
108 $this->assign("context", $this->_context
);
110 // get user submitted values
111 // get it from controller only if form has been submitted, else preProcess has set this
114 $this->_formValues
= $this->get('formValues');
117 $this->_formValues
= $this->controller
->exportValues($this->_name
);
121 $this->postProcess();
122 $this->set('force', 0);
126 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
127 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
128 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
132 //get the voter clause.
133 $voterClause = $this->voterClause();
135 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
137 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
145 if ($this->_context
== 'user') {
146 $prefix = $this->_prefix
;
149 $this->assign("{$prefix}limit", $this->_limit
);
150 $this->assign("{$prefix}single", $this->_single
);
152 $controller = new CRM_Core_Selector_Controller($selector,
153 $this->get(CRM_Utils_Pager
::PAGE_ID
),
155 CRM_Core_Action
::VIEW
,
157 CRM_Core_Selector_Controller
::TRANSFER
,
161 $controller->setEmbedded(TRUE);
162 $controller->moveFromSessionToTemplate();
164 //append breadcrumb to survey dashboard.
165 if (CRM_Campaign_BAO_Campaign
::accessCampaign()) {
166 $url = CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=survey');
167 CRM_Utils_System
::appendBreadCrumb(array(array('title' => ts('Survey(s)'), 'url' => $url)));
170 //set the form title.
171 CRM_Utils_System
::setTitle(ts('Find Respondents To %1', array(1 => ucfirst($this->_operation
))));
175 * Load the default survey for all actions.
179 public function setDefaultValues() {
180 if (empty($this->_defaults
)) {
181 $defaultSurveyId = key(CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE));
182 if ($defaultSurveyId) {
183 $this->_defaults
['campaign_survey_id'] = $defaultSurveyId;
187 return $this->_defaults
;
191 * Build the form object.
193 public function buildQuickForm() {
194 parent
::buildQuickForm();
195 //build the search form.
196 CRM_Campaign_BAO_Query
::buildSearchForm($this);
198 $rows = $this->get('rows');
199 if (is_array($rows)) {
200 if (!$this->_single
) {
201 $this->addRowSelectors($rows);
204 $allTasks = CRM_Campaign_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission());
206 //hack to serve right page to state machine.
207 $taskMapping = array(
208 'interview' => CRM_Campaign_Task
::INTERVIEW
,
209 'reserve' => CRM_Campaign_Task
::RESERVE
,
210 'release' => CRM_Campaign_Task
::RELEASE
,
213 $currentTaskValue = CRM_Utils_Array
::value($this->_operation
, $taskMapping);
214 $taskValue = array($currentTaskValue => $allTasks[$currentTaskValue]);
215 if ($this->_operation
== 'interview' && !empty($this->_formValues
['campaign_survey_id'])) {
216 $activityTypes = CRM_Core_PseudoConstant
::activityType(FALSE, TRUE, FALSE, 'label', TRUE);
218 $surveyTypeId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
219 $this->_formValues
['campaign_survey_id'],
223 $currentTaskValue => ts('Record %1 Responses',
224 array(1 => $activityTypes[$surveyTypeId])
229 $this->addTaskMenu($taskValue);
235 * The post processing of the form gets done here.
237 * Key things done during post processing are
238 * - check for reset or next request. if present, skip post procesing.
239 * - now check if user requested running a saved search, if so, then
240 * the form values associated with the saved search are used for searching.
241 * - if user has done a submit with new values the regular post submissing is
243 * The processing consists of using a Selector / Controller framework for getting the
246 public function postProcess() {
253 if (!empty($_POST)) {
254 $this->_formValues
= $this->controller
->exportValues($this->_name
);
257 $this->fixFormValues();
259 //format params as per task.
260 $this->formatParams();
262 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
264 $this->set('formValues', $this->_formValues
);
265 $this->set('queryParams', $this->_queryParams
);
267 $buttonName = $this->controller
->getButtonName();
268 if ($buttonName == $this->_actionButtonName
) {
269 // check actionName and if next, then do not repeat a search, since we are going to the next page
271 // hack, make sure we reset the task values
272 $stateMachine = $this->controller
->getStateMachine();
273 $formName = $stateMachine->getTaskFormName();
275 $this->controller
->resetPage($formName);
280 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
281 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
282 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
286 //get the voter clause.
287 $voterClause = $this->voterClause();
289 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
296 $selector->setKey($this->controller
->_key
);
299 if ($this->_context
== 'basic' ||
300 $this->_context
== 'user'
302 $prefix = $this->_prefix
;
305 $controller = new CRM_Core_Selector_Controller($selector,
306 $this->get(CRM_Utils_Pager
::PAGE_ID
),
308 CRM_Core_Action
::VIEW
,
310 CRM_Core_Selector_Controller
::SESSION
,
313 $controller->setEmbedded(TRUE);
314 $query = $selector->getQuery();
315 if ($this->_context
== 'user') {
316 $query->setSkipPermission(TRUE);
321 public function formatParams() {
322 $interviewerId = CRM_Utils_Array
::value('survey_interviewer_id', $this->_formValues
);
323 if ($interviewerId) {
324 $this->set('interviewerId', $interviewerId);
327 //format multi-select group and contact types.
328 foreach (array('group', 'contact_type') as $param) {
332 $paramValue = CRM_Utils_Array
::value($param, $this->_formValues
);
333 if ($paramValue && is_array($paramValue)) {
334 unset($this->_formValues
[$param]);
335 foreach ($paramValue as $key => $value) {
336 $this->_formValues
[$param][$value] = 1;
341 //apply filter of survey contact type for search.
342 $contactType = CRM_Campaign_BAO_Survey
::getSurveyContactType(CRM_Utils_Array
::value('campaign_survey_id', $this->_formValues
));
343 if ($contactType && in_array($this->_operation
, ['reserve', 'interview'])) {
344 $this->_formValues
['contact_type'][$contactType] = 1;
347 if ($this->_operation
== 'reserve') {
348 if (!empty($this->_formValues
['campaign_survey_id'])) {
349 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
350 $this->_formValues
['campaign_survey_id'],
354 //allow voter search in sub-part of given constituents,
355 //but make sure in case user does not select any group.
356 //get all associated campaign groups in where filter, CRM-7406
357 $groups = CRM_Utils_Array
::value('group', $this->_formValues
);
358 if ($campaignId && CRM_Utils_System
::isNull($groups)) {
359 $campGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
360 foreach ($campGroups as $id => $title) {
361 $this->_formValues
['group'][$id] = 1;
365 //carry servey id w/ this.
366 $this->set('surveyId', $this->_formValues
['campaign_survey_id']);
367 unset($this->_formValues
['campaign_survey_id']);
369 unset($this->_formValues
['survey_interviewer_id']);
371 elseif ($this->_operation
== 'interview' ||
372 $this->_operation
== 'release'
374 //to conduct interview / release activity status should be scheduled.
375 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
376 if ($scheduledStatusId = array_search('Scheduled', $activityStatus)) {
377 $this->_formValues
['survey_status_id'] = $scheduledStatusId;
381 //pass voter search operation.
382 $this->_formValues
['campaign_search_voter_for'] = $this->_operation
;
385 public function fixFormValues() {
386 // if this search has been forced
387 // then see if there are any get values, and if so over-ride the post values
388 // note that this means that GET over-rides POST :)
390 //since we have qfKey, no need to manipulate set defaults.
391 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String');
393 if (!$this->_force || CRM_Utils_Rule
::qfKey($qfKey)) {
398 $surveyId = CRM_Utils_Request
::retrieve('sid', 'Positive');
401 $surveyId = CRM_Utils_Type
::escape($surveyId, 'Integer');
404 // use default survey id
405 $surveyId = key(CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE));
408 CRM_Core_Error
::fatal('Could not find valid Survey Id.');
410 $this->_formValues
['campaign_survey_id'] = $this->_formValues
['campaign_survey_id'] = $surveyId;
412 $session = CRM_Core_Session
::singleton();
413 $userId = $session->get('userID');
415 // get interviewer id
416 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive',
417 CRM_Core_DAO
::$_nullObject, FALSE, $userId
419 //to force other contact as interviewer, user should be admin.
420 if ($cid != $userId &&
421 !CRM_Core_Permission
::check('administer CiviCampaign')
423 CRM_Utils_System
::permissionDenied();
424 CRM_Utils_System
::civiExit();
426 $this->_formValues
['survey_interviewer_id'] = $cid;
427 //get all in defaults.
428 $this->_defaults
= $this->_formValues
;
429 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
435 public function voterClause() {
436 $params = array('campaign_search_voter_for' => $this->_operation
);
438 $clauseFields = array(
439 'surveyId' => 'campaign_survey_id',
440 'interviewerId' => 'survey_interviewer_id',
443 foreach ($clauseFields as $param => $key) {
444 $params[$key] = CRM_Utils_Array
::value($key, $this->_formValues
);
445 if (!$params[$key]) {
446 $params[$key] = $this->get($param);
451 $voterClause = CRM_Campaign_BAO_Query
::voterClause($params);
457 * Return a descriptive name for the page, used in wizard header
461 public function getTitle() {
462 return ts('Find Respondents');