3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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.
63 protected $_prefix = "survey_";
66 private $_operation = 'reserve';
69 * Processing needed for buildForm and later.
71 public function preProcess() {
73 $this->_defaults
= array();
75 //set the button name.
76 $this->_searchButtonName
= $this->getButtonName('refresh');
77 $this->_printButtonName
= $this->getButtonName('next', 'print');
78 $this->_actionButtonName
= $this->getButtonName('next', 'action');
80 //we allow the controller to set force/reset externally,
81 //useful when we are being driven by the wizard framework
82 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
83 $this->_force
= CRM_Utils_Request
::retrieve('force', 'Boolean', $this, FALSE);
84 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this, FALSE, 'search');
85 $this->_reset
= CRM_Utils_Request
::retrieve('reset', 'Boolean', CRM_Core_DAO
::$_nullObject);
87 //operation for state machine.
88 $this->_operation
= CRM_Utils_Request
::retrieve('op', 'String', $this, FALSE, 'reserve');
90 if (!in_array($this->_operation
, array(
96 $this->_operation
= 'reserve';
97 $this->set('op', $this->_operation
);
99 $this->set('searchVoterFor', $this->_operation
);
100 $this->assign('searchVoterFor', $this->_operation
);
101 $this->assign('isFormSubmitted', $this->isSubmitted());
103 //do check permissions.
104 if (!CRM_Core_Permission
::check('administer CiviCampaign') &&
105 !CRM_Core_Permission
::check('manage campaign') &&
106 !CRM_Core_Permission
::check("{$this->_operation} campaign contacts")
108 CRM_Utils_System
::permissionDenied();
109 CRM_Utils_System
::civiExit();
112 $this->assign("context", $this->_context
);
114 // get user submitted values
115 // get it from controller only if form has been submitted, else preProcess has set this
118 $this->_formValues
= $this->get('formValues');
121 $this->_formValues
= $this->controller
->exportValues($this->_name
);
125 $this->postProcess();
126 $this->set('force', 0);
130 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
131 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
132 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
136 //get the voter clause.
137 $voterClause = $this->voterClause();
139 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
141 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
149 if ($this->_context
== 'user') {
150 $prefix = $this->_prefix
;
153 $this->assign("{$prefix}limit", $this->_limit
);
154 $this->assign("{$prefix}single", $this->_single
);
156 $controller = new CRM_Core_Selector_Controller($selector,
157 $this->get(CRM_Utils_Pager
::PAGE_ID
),
159 CRM_Core_Action
::VIEW
,
161 CRM_Core_Selector_Controller
::TRANSFER
,
165 $controller->setEmbedded(TRUE);
166 $controller->moveFromSessionToTemplate();
168 //append breadcrumb to survey dashboard.
169 if (CRM_Campaign_BAO_Campaign
::accessCampaign()) {
170 $url = CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=survey');
171 CRM_Utils_System
::appendBreadCrumb(array(array('title' => ts('Survey(s)'), 'url' => $url)));
174 //set the form title.
175 CRM_Utils_System
::setTitle(ts('Find Respondents To %1', array(1 => ucfirst($this->_operation
))));
178 public function setDefaultValues() {
179 //load the default survey for all actions.
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 $permission = CRM_Core_Permission
::getPermission();
205 $allTasks = CRM_Campaign_Task
::permissionedTaskTitles($permission);
207 //hack to serve right page to state machine.
208 $taskMapping = array(
214 $currentTaskValue = CRM_Utils_Array
::value($this->_operation
, $taskMapping);
215 $taskValue = array($currentTaskValue => $allTasks[$currentTaskValue]);
216 if ($this->_operation
== 'interview' && !empty($this->_formValues
['campaign_survey_id'])) {
217 $activityTypes = CRM_Core_PseudoConstant
::activityType(FALSE, TRUE, FALSE, 'label', TRUE);
219 $surveyTypeId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
220 $this->_formValues
['campaign_survey_id'],
224 $currentTaskValue => ts('Record %1 Responses',
225 array(1 => $activityTypes[$surveyTypeId])
230 $this->addTaskMenu($taskValue);
236 * The post processing of the form gets done here.
238 * Key things done during post processing are
239 * - check for reset or next request. if present, skip post procesing.
240 * - now check if user requested running a saved search, if so, then
241 * the form values associated with the saved search are used for searching.
242 * - if user has done a submit with new values the regular post submissing is
244 * The processing consists of using a Selector / Controller framework for getting the
247 public function postProcess() {
254 if (!empty($_POST)) {
255 $this->_formValues
= $this->controller
->exportValues($this->_name
);
258 $this->fixFormValues();
260 //format params as per task.
261 $this->formatParams();
263 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
265 $this->set('formValues', $this->_formValues
);
266 $this->set('queryParams', $this->_queryParams
);
268 $buttonName = $this->controller
->getButtonName();
269 if ($buttonName == $this->_actionButtonName
) {
270 // check actionName and if next, then do not repeat a search, since we are going to the next page
272 // hack, make sure we reset the task values
273 $stateMachine = $this->controller
->getStateMachine();
274 $formName = $stateMachine->getTaskFormName();
276 $this->controller
->resetPage($formName);
281 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
282 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
283 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
287 //get the voter clause.
288 $voterClause = $this->voterClause();
290 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
297 $selector->setKey($this->controller
->_key
);
300 if ($this->_context
== 'basic' ||
301 $this->_context
== 'user'
303 $prefix = $this->_prefix
;
306 $controller = new CRM_Core_Selector_Controller($selector,
307 $this->get(CRM_Utils_Pager
::PAGE_ID
),
309 CRM_Core_Action
::VIEW
,
311 CRM_Core_Selector_Controller
::SESSION
,
314 $controller->setEmbedded(TRUE);
315 $query = $selector->getQuery();
316 if ($this->_context
== 'user') {
317 $query->setSkipPermission(TRUE);
322 public function formatParams() {
323 $interviewerId = CRM_Utils_Array
::value('survey_interviewer_id', $this->_formValues
);
324 if ($interviewerId) {
325 $this->set('interviewerId', $interviewerId);
328 //format multi-select group and contact types.
329 foreach (array('group', 'contact_type') as $param) {
333 $paramValue = CRM_Utils_Array
::value($param, $this->_formValues
);
334 if ($paramValue && is_array($paramValue)) {
335 unset($this->_formValues
[$param]);
336 foreach ($paramValue as $key => $value) {
337 $this->_formValues
[$param][$value] = 1;
342 //apply filter of survey contact type for search.
343 $contactType = CRM_Campaign_BAO_Survey
::getSurveyContactType(CRM_Utils_Array
::value('campaign_survey_id', $this->_formValues
));
344 if ($contactType && in_array($this->_operation
, array(
349 $this->_formValues
['contact_type'][$contactType] = 1;
352 if ($this->_operation
== 'reserve') {
353 if (!empty($this->_formValues
['campaign_survey_id'])) {
354 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
355 $this->_formValues
['campaign_survey_id'],
359 //allow voter search in sub-part of given constituents,
360 //but make sure in case user does not select any group.
361 //get all associated campaign groups in where filter, CRM-7406
362 $groups = CRM_Utils_Array
::value('group', $this->_formValues
);
363 if ($campaignId && CRM_Utils_System
::isNull($groups)) {
364 $campGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
365 foreach ($campGroups as $id => $title) {
366 $this->_formValues
['group'][$id] = 1;
370 //carry servey id w/ this.
371 $this->set('surveyId', $this->_formValues
['campaign_survey_id']);
372 unset($this->_formValues
['campaign_survey_id']);
374 unset($this->_formValues
['survey_interviewer_id']);
376 elseif ($this->_operation
== 'interview' ||
377 $this->_operation
== 'release'
379 //to conduct interview / release activity status should be scheduled.
380 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
381 if ($scheduledStatusId = array_search('Scheduled', $activityStatus)) {
382 $this->_formValues
['survey_status_id'] = $scheduledStatusId;
386 //pass voter search operation.
387 $this->_formValues
['campaign_search_voter_for'] = $this->_operation
;
390 public function fixFormValues() {
391 // if this search has been forced
392 // then see if there are any get values, and if so over-ride the post values
393 // note that this means that GET over-rides POST :)
395 //since we have qfKey, no need to manipulate set defaults.
396 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', CRM_Core_DAO
::$_nullObject);
398 if (!$this->_force || CRM_Utils_Rule
::qfKey($qfKey)) {
403 $surveyId = CRM_Utils_Request
::retrieve('sid', 'Positive', CRM_Core_DAO
::$_nullObject);
406 $surveyId = CRM_Utils_Type
::escape($surveyId, 'Integer');
409 // use default survey id
410 $surveyId = key(CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE));
413 CRM_Core_Error
::fatal('Could not find valid Survey Id.');
415 $this->_formValues
['campaign_survey_id'] = $this->_formValues
['campaign_survey_id'] = $surveyId;
417 $session = CRM_Core_Session
::singleton();
418 $userId = $session->get('userID');
420 // get interviewer id
421 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive',
422 CRM_Core_DAO
::$_nullObject, FALSE, $userId
424 //to force other contact as interviewer, user should be admin.
425 if ($cid != $userId &&
426 !CRM_Core_Permission
::check('administer CiviCampaign')
428 CRM_Utils_System
::permissionDenied();
429 CRM_Utils_System
::civiExit();
431 $this->_formValues
['survey_interviewer_id'] = $cid;
432 //get all in defaults.
433 $this->_defaults
= $this->_formValues
;
434 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
440 public function voterClause() {
441 $params = array('campaign_search_voter_for' => $this->_operation
);
443 $clauseFields = array(
444 'surveyId' => 'campaign_survey_id',
445 'interviewerId' => 'survey_interviewer_id',
448 foreach ($clauseFields as $param => $key) {
449 $params[$key] = CRM_Utils_Array
::value($key, $this->_formValues
);
450 if (!$params[$key]) {
451 $params[$key] = $this->get($param);
456 $voterClause = CRM_Campaign_BAO_Query
::voterClause($params);
462 * Return a descriptive name for the page, used in wizard header
466 public function getTitle() {
467 return ts('Find Respondents');