3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
39 class CRM_Campaign_Form_Search
extends CRM_Core_Form_Search
{
42 * The params that are sent to the query
46 protected $_queryParams;
49 * Are we restricting ourselves to a single contact
53 protected $_single = FALSE;
56 * Are we restricting ourselves to a single contact
60 protected $_limit = NULL;
65 * Prefix for the controller
67 protected $_prefix = "survey_";
70 private $_operation = 'reserve';
73 * Processing needed for buildForm and later
77 function preProcess() {
79 $this->_defaults
= array();
81 //set the button name.
82 $this->_searchButtonName
= $this->getButtonName('refresh');
83 $this->_printButtonName
= $this->getButtonName('next', 'print');
84 $this->_actionButtonName
= $this->getButtonName('next', 'action');
86 //we allow the controller to set force/reset externally,
87 //useful when we are being driven by the wizard framework
88 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
89 $this->_force
= CRM_Utils_Request
::retrieve('force', 'Boolean', $this, FALSE);
90 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this, FALSE, 'search');
91 $this->_reset
= CRM_Utils_Request
::retrieve('reset', 'Boolean', CRM_Core_DAO
::$_nullObject);
93 //operation for state machine.
94 $this->_operation
= CRM_Utils_Request
::retrieve('op', 'String', $this, FALSE, 'reserve');
96 if (!in_array($this->_operation
, array(
97 'reserve', 'release', 'interview'))) {
98 $this->_operation
= 'reserve';
99 $this->set('op', $this->_operation
);
101 $this->set('searchVoterFor', $this->_operation
);
102 $this->assign('searchVoterFor', $this->_operation
);
103 $this->assign('isFormSubmitted', $this->isSubmitted());
105 //do check permissions.
106 if (!CRM_Core_Permission
::check('administer CiviCampaign') &&
107 !CRM_Core_Permission
::check('manage campaign') &&
108 !CRM_Core_Permission
::check("{$this->_operation} campaign contacts")
110 CRM_Utils_System
::permissionDenied();
111 CRM_Utils_System
::civiExit();
114 $this->assign("context", $this->_context
);
116 // get user submitted values
117 // get it from controller only if form has been submitted, else preProcess has set this
120 $this->_formValues
= $this->get('formValues');
123 $this->_formValues
= $this->controller
->exportValues($this->_name
);
127 $this->postProcess();
128 $this->set('force', 0);
132 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
133 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
134 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
138 //get the voter clause.
139 $voterClause = $this->voterClause();
141 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
143 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
151 if ($this->_context
== 'user') {
152 $prefix = $this->_prefix
;
155 $this->assign("{$prefix}limit", $this->_limit
);
156 $this->assign("{$prefix}single", $this->_single
);
158 $controller = new CRM_Core_Selector_Controller($selector,
159 $this->get(CRM_Utils_Pager
::PAGE_ID
),
161 CRM_Core_Action
::VIEW
,
163 CRM_Core_Selector_Controller
::TRANSFER
,
167 $controller->setEmbedded(TRUE);
168 $controller->moveFromSessionToTemplate();
170 //append breadcrumb to survey dashboard.
171 if (CRM_Campaign_BAO_Campaign
::accessCampaign()) {
172 $url = CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=survey');
173 CRM_Utils_System
::appendBreadCrumb(array(array('title' => ts('Survey(s)'), 'url' => $url)));
176 //set the form title.
177 CRM_Utils_System
::setTitle(ts('Find Respondents To %1', array(1 => ucfirst($this->_operation
))));
180 public function setDefaultValues() {
181 //load the default survey for all actions.
182 if (empty($this->_defaults
)) {
183 $defaultSurveyId = key(CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE));
184 if ($defaultSurveyId) {
185 $this->_defaults
['campaign_survey_id'] = $defaultSurveyId;
189 return $this->_defaults
;
193 * Build the form object
198 public function buildQuickForm() {
199 parent
::buildQuickForm();
200 //build the search form.
201 CRM_Campaign_BAO_Query
::buildSearchForm($this);
203 $rows = $this->get('rows');
204 if (is_array($rows)) {
205 if (!$this->_single
) {
206 $this->addRowSelectors($rows);
209 $permission = CRM_Core_Permission
::getPermission();
210 $allTasks = CRM_Campaign_Task
::permissionedTaskTitles($permission);
212 //hack to serve right page to state machine.
213 $taskMapping = array(
219 $currentTaskValue = CRM_Utils_Array
::value($this->_operation
, $taskMapping);
220 $taskValue = array($currentTaskValue => $allTasks[$currentTaskValue]);
221 if ($this->_operation
== 'interview' && !empty($this->_formValues
['campaign_survey_id'])) {
222 $activityTypes = CRM_Core_PseudoConstant
::activityType(FALSE, TRUE, FALSE, 'label', TRUE);
224 $surveyTypeId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
225 $this->_formValues
['campaign_survey_id'],
229 $currentTaskValue => ts('Record %1 Responses',
230 array(1 => $activityTypes[$surveyTypeId])
234 $this->addTaskMenu($taskValue);
240 * The post processing of the form gets done here.
242 * Key things done during post processing are
243 * - check for reset or next request. if present, skip post procesing.
244 * - now check if user requested running a saved search, if so, then
245 * the form values associated with the saved search are used for searching.
246 * - if user has done a submit with new values the regular post submissing is
248 * The processing consists of using a Selector / Controller framework for getting the
255 public function postProcess() {
262 if (!empty($_POST)) {
263 $this->_formValues
= $this->controller
->exportValues($this->_name
);
266 $this->fixFormValues();
268 //format params as per task.
269 $this->formatParams();
271 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
273 $this->set('formValues', $this->_formValues
);
274 $this->set('queryParams', $this->_queryParams
);
276 $buttonName = $this->controller
->getButtonName();
277 if ($buttonName == $this->_actionButtonName
) {
278 // check actionName and if next, then do not repeat a search, since we are going to the next page
280 // hack, make sure we reset the task values
281 $stateMachine = $this->controller
->getStateMachine();
282 $formName = $stateMachine->getTaskFormName();
284 $this->controller
->resetPage($formName);
289 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
290 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
291 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
295 //get the voter clause.
296 $voterClause = $this->voterClause();
298 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
305 $selector->setKey($this->controller
->_key
);
308 if ($this->_context
== 'basic' ||
309 $this->_context
== 'user'
311 $prefix = $this->_prefix
;
314 $controller = new CRM_Core_Selector_Controller($selector,
315 $this->get(CRM_Utils_Pager
::PAGE_ID
),
317 CRM_Core_Action
::VIEW
,
319 CRM_Core_Selector_Controller
::SESSION
,
322 $controller->setEmbedded(TRUE);
323 $query = $selector->getQuery();
324 if ($this->_context
== 'user') {
325 $query->setSkipPermission(TRUE);
330 public function formatParams() {
331 $interviewerId = CRM_Utils_Array
::value('survey_interviewer_id', $this->_formValues
);
332 if ($interviewerId) {
333 $this->set('interviewerId', $interviewerId);
336 //format multi-select group and contact types.
337 foreach (array('group', 'contact_type') as $param) {
341 $paramValue = CRM_Utils_Array
::value($param, $this->_formValues
);
342 if ($paramValue && is_array($paramValue)) {
343 unset($this->_formValues
[$param]);
344 foreach ($paramValue as $key => $value) {
345 $this->_formValues
[$param][$value] = 1;
350 //apply filter of survey contact type for search.
351 $contactType = CRM_Campaign_BAO_Survey
::getSurveyContactType(CRM_Utils_Array
::value('campaign_survey_id', $this->_formValues
));
352 if ($contactType && in_array($this->_operation
, array(
353 'reserve', 'interview'))) {
354 $this->_formValues
['contact_type'][$contactType] = 1;
357 if ($this->_operation
== 'reserve') {
358 if (!empty($this->_formValues
['campaign_survey_id'])) {
359 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
360 $this->_formValues
['campaign_survey_id'],
364 //allow voter search in sub-part of given constituents,
365 //but make sure in case user does not select any group.
366 //get all associated campaign groups in where filter, CRM-7406
367 $groups = CRM_Utils_Array
::value('group', $this->_formValues
);
368 if ($campaignId && CRM_Utils_System
::isNull($groups)) {
369 $campGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
370 foreach ($campGroups as $id => $title) $this->_formValues
['group'][$id] = 1;
373 //carry servey id w/ this.
374 $this->set('surveyId', $this->_formValues
['campaign_survey_id']);
375 unset($this->_formValues
['campaign_survey_id']);
377 unset($this->_formValues
['survey_interviewer_id']);
379 elseif ($this->_operation
== 'interview' ||
380 $this->_operation
== 'release'
382 //to conduct interview / release activity status should be scheduled.
383 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
384 if ($scheduledStatusId = array_search('Scheduled', $activityStatus)) {
385 $this->_formValues
['survey_status_id'] = $scheduledStatusId;
389 //pass voter search operation.
390 $this->_formValues
['campaign_search_voter_for'] = $this->_operation
;
393 public function fixFormValues() {
394 // if this search has been forced
395 // then see if there are any get values, and if so over-ride the post values
396 // note that this means that GET over-rides POST :)
398 //since we have qfKey, no need to manipulate set defaults.
399 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', CRM_Core_DAO
::$_nullObject);
401 if (!$this->_force || CRM_Utils_Rule
::qfKey($qfKey)) {
406 $surveyId = CRM_Utils_Request
::retrieve('sid', 'Positive', CRM_Core_DAO
::$_nullObject);
409 $surveyId = CRM_Utils_Type
::escape($surveyId, 'Integer');
412 // use default survey id
413 $surveyId = key(CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE));
416 CRM_Core_Error
::fatal('Could not find valid Survey Id.');
418 $this->_formValues
['campaign_survey_id'] = $this->_formValues
['campaign_survey_id'] = $surveyId;
420 $session = CRM_Core_Session
::singleton();
421 $userId = $session->get('userID');
423 // get interviewer id
424 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive',
425 CRM_Core_DAO
::$_nullObject, FALSE, $userId
427 //to force other contact as interviewer, user should be admin.
428 if ($cid != $userId &&
429 !CRM_Core_Permission
::check('administer CiviCampaign')
431 CRM_Utils_System
::permissionDenied();
432 CRM_Utils_System
::civiExit();
434 $this->_formValues
['survey_interviewer_id'] = $cid;
435 //get all in defaults.
436 $this->_defaults
= $this->_formValues
;
437 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
443 public function voterClause() {
444 $params = array('campaign_search_voter_for' => $this->_operation
);
446 $clauseFields = array(
447 'surveyId' => 'campaign_survey_id',
448 'interviewerId' => 'survey_interviewer_id',
451 foreach ($clauseFields as $param => $key) {
452 $params[$key] = CRM_Utils_Array
::value($key, $this->_formValues
);
453 if (!$params[$key]) {
454 $params[$key] = $this->get($param);
459 $voterClause = CRM_Campaign_BAO_Query
::voterClause($params);
465 * Return a descriptive name for the page, used in wizard header
469 public function getTitle() {
470 return ts('Find Respondents');