3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
39 class CRM_Campaign_Form_Search
extends CRM_Core_Form
{
42 * Are we forced to run a search
50 * name of search button
55 protected $_searchButtonName;
58 * name of action button
63 protected $_actionButtonName;
66 * form values that we will be using
71 protected $_formValues;
74 * the params that are sent to the query
79 protected $_queryParams;
82 * have we already done this search
90 * are we restricting ourselves to a single contact
95 protected $_single = FALSE;
98 * are we restricting ourselves to a single contact
103 protected $_limit = NULL;
106 * what context are we being invoked from
111 protected $_context = NULL;
113 protected $_defaults;
116 * prefix for the controller
119 protected $_prefix = "survey_";
122 private $_operation = 'reserve';
125 * processing needed for buildForm and later
129 */ function preProcess() {
130 $this->_done
= FALSE;
131 $this->_defaults
= array();
133 //set the button name.
134 $this->_searchButtonName
= $this->getButtonName('refresh');
135 $this->_printButtonName
= $this->getButtonName('next', 'print');
136 $this->_actionButtonName
= $this->getButtonName('next', 'action');
138 //we allow the controller to set force/reset externally,
139 //useful when we are being driven by the wizard framework
140 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
141 $this->_force
= CRM_Utils_Request
::retrieve('force', 'Boolean', $this, FALSE);
142 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this, FALSE, 'search');
143 $this->_reset
= CRM_Utils_Request
::retrieve('reset', 'Boolean', CRM_Core_DAO
::$_nullObject);
145 //operation for state machine.
146 $this->_operation
= CRM_Utils_Request
::retrieve('op', 'String', $this, FALSE, 'reserve');
147 //validate operation.
148 if (!in_array($this->_operation
, array(
149 'reserve', 'release', 'interview'))) {
150 $this->_operation
= 'reserve';
151 $this->set('op', $this->_operation
);
153 $this->set('searchVoterFor', $this->_operation
);
154 $this->assign('searchVoterFor', $this->_operation
);
155 $this->assign('isFormSubmitted', $this->isSubmitted());
157 //do check permissions.
158 if (!CRM_Core_Permission
::check('administer CiviCampaign') &&
159 !CRM_Core_Permission
::check('manage campaign') &&
160 !CRM_Core_Permission
::check("{$this->_operation} campaign contacts")
162 CRM_Utils_System
::permissionDenied();
163 CRM_Utils_System
::civiExit();
166 $this->assign("context", $this->_context
);
168 // get user submitted values
169 // get it from controller only if form has been submitted, else preProcess has set this
172 $this->_formValues
= $this->get('formValues');
175 $this->_formValues
= $this->controller
->exportValues($this->_name
);
179 $this->postProcess();
180 $this->set('force', 0);
184 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
185 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
186 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
190 //get the voter clause.
191 $voterClause = $this->voterClause();
193 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
195 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
203 if ($this->_context
== 'user') {
204 $prefix = $this->_prefix
;
207 $this->assign("{$prefix}limit", $this->_limit
);
208 $this->assign("{$prefix}single", $this->_single
);
210 $controller = new CRM_Core_Selector_Controller($selector,
211 $this->get(CRM_Utils_Pager
::PAGE_ID
),
213 CRM_Core_Action
::VIEW
,
215 CRM_Core_Selector_Controller
::TRANSFER
,
219 $controller->setEmbedded(TRUE);
220 $controller->moveFromSessionToTemplate();
222 //append breadcrumb to survey dashboard.
223 if (CRM_Campaign_BAO_Campaign
::accessCampaign()) {
224 $url = CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=survey');
225 CRM_Utils_System
::appendBreadCrumb(array(array('title' => ts('Survey(s)'), 'url' => $url)));
228 //set the form title.
229 CRM_Utils_System
::setTitle(ts('Find Respondents To %1', array(1 => ucfirst($this->_operation
))));
232 function setDefaultValues() {
233 //load the default survey for all actions.
234 if (empty($this->_defaults
)) {
235 $defaultSurveyId = key(CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE));
236 if ($defaultSurveyId) {
237 $this->_defaults
['campaign_survey_id'] = $defaultSurveyId;
241 return $this->_defaults
;
251 function buildQuickForm() {
252 //build the search form.
253 CRM_Campaign_BAO_Query
::buildSearchForm($this);
256 * add form checkboxes for each row. This is needed out here to conform to QF protocol
257 * of all elements being declared in builQuickForm
261 $rows = $this->get('rows');
262 if (is_array($rows)) {
263 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'js/crm.searchForm.js');
264 if (!$this->_single
) {
265 $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows'));
266 foreach ($rows as $row) {
267 $this->addElement('checkbox', $row['checkbox'],
269 array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row')
274 $total = $cancel = 0;
276 $permission = CRM_Core_Permission
::getPermission();
277 $allTasks = CRM_Campaign_Task
::permissionedTaskTitles($permission);
279 //hack to serve right page to state machine.
280 $taskMapping = array(
286 $currentTaskValue = CRM_Utils_Array
::value($this->_operation
, $taskMapping);
287 $taskValue = array($currentTaskValue => $allTasks[$currentTaskValue]);
288 if ($this->_operation
== 'interview' && !empty($this->_formValues
['campaign_survey_id'])) {
289 $activityTypes = CRM_Core_PseudoConstant
::activityType(FALSE, TRUE, FALSE, 'label', TRUE);
291 $surveyTypeId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
292 $this->_formValues
['campaign_survey_id'],
296 $currentTaskValue => ts('Record %1 Responses',
297 array(1 => $activityTypes[$surveyTypeId])
301 $this->add('select', 'task', ts('Actions:') . ' ', array('' => ts('- actions -')) +
$taskValue);
303 $this->add('submit', $this->_actionButtonName
, ts('Go'),
305 'class' => 'form-submit',
310 // need to perform tasks on all or selected items ? using radio_ts(task selection) for it
311 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
312 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('class' => 'select-rows', 'onclick' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );"));
316 $this->addButtons(array(
319 'name' => ts('Search'),
327 * The post processing of the form gets done here.
329 * Key things done during post processing are
330 * - check for reset or next request. if present, skip post procesing.
331 * - now check if user requested running a saved search, if so, then
332 * the form values associated with the saved search are used for searching.
333 * - if user has done a submit with new values the regular post submissing is
335 * The processing consists of using a Selector / Controller framework for getting the
343 function postProcess() {
350 if (!empty($_POST)) {
351 $this->_formValues
= $this->controller
->exportValues($this->_name
);
354 $this->fixFormValues();
356 //format params as per task.
357 $this->formatParams();
359 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
361 $this->set('formValues', $this->_formValues
);
362 $this->set('queryParams', $this->_queryParams
);
364 $buttonName = $this->controller
->getButtonName();
365 if ($buttonName == $this->_actionButtonName
) {
366 // check actionName and if next, then do not repeat a search, since we are going to the next page
368 // hack, make sure we reset the task values
369 $stateMachine = $this->controller
->getStateMachine();
370 $formName = $stateMachine->getTaskFormName();
372 $this->controller
->resetPage($formName);
377 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
378 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
379 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
383 //get the voter clause.
384 $voterClause = $this->voterClause();
386 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
393 $selector->setKey($this->controller
->_key
);
396 if ($this->_context
== 'basic' ||
397 $this->_context
== 'user'
399 $prefix = $this->_prefix
;
402 $controller = new CRM_Core_Selector_Controller($selector,
403 $this->get(CRM_Utils_Pager
::PAGE_ID
),
405 CRM_Core_Action
::VIEW
,
407 CRM_Core_Selector_Controller
::SESSION
,
410 $controller->setEmbedded(TRUE);
411 $query = $selector->getQuery();
412 if ($this->_context
== 'user') {
413 $query->setSkipPermission(TRUE);
418 function formatParams() {
419 $interviewerId = CRM_Utils_Array
::value('survey_interviewer_id', $this->_formValues
);
420 if ($interviewerId) {
421 $this->set('interviewerId', $interviewerId);
422 if (empty($this->_formValues
['survey_interviewer_name'])) {
423 $this->_formValues
['survey_interviewer_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
431 //format multi-select group and contact types.
433 'group', 'contact_type') as $param) {
437 $paramValue = CRM_Utils_Array
::value($param, $this->_formValues
);
438 if ($paramValue && is_array($paramValue)) {
439 unset($this->_formValues
[$param]);
440 foreach ($paramValue as $key => $value) {
441 $this->_formValues
[$param][$value] = 1;
446 //apply filter of survey contact type for search.
447 $contactType = CRM_Campaign_BAO_Survey
::getSurveyContactType(CRM_Utils_Array
::value('campaign_survey_id', $this->_formValues
));
448 if ($contactType && in_array($this->_operation
, array(
449 'reserve', 'interview'))) {
450 $this->_formValues
['contact_type'][$contactType] = 1;
453 if ($this->_operation
== 'reserve') {
454 if (!empty($this->_formValues
['campaign_survey_id'])) {
455 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
456 $this->_formValues
['campaign_survey_id'],
460 //allow voter search in sub-part of given constituents,
461 //but make sure in case user does not select any group.
462 //get all associated campaign groups in where filter, CRM-7406
463 $groups = CRM_Utils_Array
::value('group', $this->_formValues
);
464 if ($campaignId && CRM_Utils_System
::isNull($groups)) {
465 $campGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
466 foreach ($campGroups as $id => $title) $this->_formValues
['group'][$id] = 1;
469 //carry servey id w/ this.
470 $this->set('surveyId', $this->_formValues
['campaign_survey_id']);
471 unset($this->_formValues
['campaign_survey_id']);
473 unset($this->_formValues
['survey_interviewer_id']);
475 elseif ($this->_operation
== 'interview' ||
476 $this->_operation
== 'release'
478 //to conduct interview / release activity status should be scheduled.
479 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
480 if ($scheduledStatusId = array_search('Scheduled', $activityStatus)) {
481 $this->_formValues
['survey_status_id'] = $scheduledStatusId;
485 //pass voter search operation.
486 $this->_formValues
['campaign_search_voter_for'] = $this->_operation
;
489 function fixFormValues() {
490 // if this search has been forced
491 // then see if there are any get values, and if so over-ride the post values
492 // note that this means that GET over-rides POST :)
494 //since we have qfKey, no need to manipulate set defaults.
495 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', CRM_Core_DAO
::$_nullObject);
497 if (!$this->_force || CRM_Utils_Rule
::qfKey($qfKey)) {
502 $surveyId = CRM_Utils_Request
::retrieve('sid', 'Positive', CRM_Core_DAO
::$_nullObject);
505 $surveyId = CRM_Utils_Type
::escape($surveyId, 'Integer');
508 // use default survey id
509 $surveyId = key(CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE));
512 CRM_Core_Error
::fatal('Could not find valid Survey Id.');
514 $this->_formValues
['campaign_survey_id'] = $this->_formValues
['campaign_survey_id'] = $surveyId;
516 $session = CRM_Core_Session
::singleton();
517 $userId = $session->get('userID');
519 // get interviewer id
520 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive',
521 CRM_Core_DAO
::$_nullObject, FALSE, $userId
523 //to force other contact as interviewer, user should be admin.
524 if ($cid != $userId &&
525 !CRM_Core_Permission
::check('administer CiviCampaign')
527 CRM_Utils_System
::permissionDenied();
528 CRM_Utils_System
::civiExit();
530 $this->_formValues
['survey_interviewer_id'] = $cid;
531 $this->_formValues
['survey_interviewer_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
536 //get all in defaults.
537 $this->_defaults
= $this->_formValues
;
538 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
541 function voterClause() {
542 $params = array('campaign_search_voter_for' => $this->_operation
);
544 $clauseFields = array(
545 'surveyId' => 'campaign_survey_id',
546 'interviewerId' => 'survey_interviewer_id',
549 foreach ($clauseFields as $param => $key) {
550 $params[$key] = CRM_Utils_Array
::value($key, $this->_formValues
);
551 if (!$params[$key]) {
552 $params[$key] = $this->get($param);
557 $voterClause = CRM_Campaign_BAO_Query
::voterClause($params);
563 * Return a descriptive name for the page, used in wizard header
568 public function getTitle() {
569 return ts('Find Respondents');