(NFC) (dev/core#878) Simplify copyright header (CRM/*)
[civicrm-core.git] / CRM / Campaign / Form / Gotv.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Files required
20 */
21 class CRM_Campaign_Form_Gotv extends CRM_Core_Form {
22
23 /**
24 * Are we forced to run a search
25 *
26 * @var int
27 */
28 protected $_force;
29
30 protected $_votingTab = FALSE;
31
32 protected $_searchVoterFor;
33
34 /**
35 * Processing needed for buildForm and later.
36 */
37 public function preProcess() {
38 $this->_search = CRM_Utils_Array::value('search', $_GET);
39 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
40 $this->_surveyId = CRM_Utils_Request::retrieve('sid', 'Positive', $this);
41 $this->_interviewerId = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
42
43 //does control come from voting tab interface.
44 $this->_votingTab = $this->get('votingTab');
45 $this->_subVotingTab = $this->get('subVotingTab');
46 $this->_searchVoterFor = 'gotv';
47 if ($this->_votingTab) {
48 if ($this->_subVotingTab == 'searchANDReserve') {
49 $this->_searchVoterFor = 'reserve';
50 }
51 elseif ($this->_subVotingTab == 'searchANDInterview') {
52 $this->_searchVoterFor = 'interview';
53 }
54 }
55 $this->assign('force', $this->_force);
56 $this->assign('votingTab', $this->_votingTab);
57 $this->assign('searchParams', json_encode($this->get('searchParams')));
58 $this->assign('buildSelector', $this->_search);
59 $this->assign('searchVoterFor', $this->_searchVoterFor);
60 $this->set('searchVoterFor', $this->_searchVoterFor);
61
62 $surveyTitle = NULL;
63 if ($this->_surveyId) {
64 $surveyTitle = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $this->_surveyId, 'title');
65 }
66 $this->assign('surveyTitle', $surveyTitle);
67
68 //append breadcrumb to survey dashboard.
69 if (CRM_Campaign_BAO_Campaign::accessCampaign()) {
70 $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey');
71 CRM_Utils_System::appendBreadCrumb([['title' => ts('Survey(s)'), 'url' => $url]]);
72 }
73
74 //set the form title.
75 CRM_Utils_System::setTitle(ts('GOTV (Voter Tracking)'));
76 }
77
78 /**
79 * Build the form object.
80 */
81 public function buildQuickForm() {
82 if ($this->_search) {
83 return;
84 }
85
86 //build common search form.
87 CRM_Campaign_BAO_Query::buildSearchForm($this);
88
89 //build the array of all search params.
90 $this->_searchParams = [];
91 foreach ($this->_elements as $element) {
92 $name = $element->_attributes['name'];
93 if ($name == 'qfKey') {
94 continue;
95 }
96 $this->_searchParams[$name] = $name;
97 }
98 $this->set('searchParams', $this->_searchParams);
99 $this->assign('searchParams', json_encode($this->_searchParams));
100
101 $defaults = [];
102
103 if (!$this->_surveyId) {
104 $this->_surveyId = key(CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE));
105 }
106
107 if ($this->_force || $this->_votingTab) {
108 $session = CRM_Core_Session::singleton();
109 $userId = $session->get('userID');
110 // get interviewer id
111 $cid = CRM_Utils_Request::retrieve('cid', 'Positive',
112 CRM_Core_DAO::$_nullObject, FALSE, $userId
113 );
114
115 $defaults['survey_interviewer_id'] = $cid;
116 }
117 if ($this->_surveyId) {
118 $defaults['campaign_survey_id'] = $this->_surveyId;
119 }
120 if (!empty($defaults)) {
121 $this->setDefaults($defaults);
122 }
123
124 //validate the required ids.
125 $this->validateIds();
126 }
127
128 public function validateIds() {
129 $errorMessages = [];
130 //check for required permissions.
131 if (!CRM_Core_Permission::check('manage campaign') &&
132 !CRM_Core_Permission::check('administer CiviCampaign') &&
133 !CRM_Core_Permission::check("{$this->_searchVoterFor} campaign contacts")
134 ) {
135 $errorMessages[] = ts('You are not authorized to access this page.');
136 }
137
138 $surveys = CRM_Campaign_BAO_Survey::getSurveys();
139 if (empty($surveys)) {
140 $errorMessages[] = ts("Oops. It looks like no surveys have been created. <a href='%1'>Click here to create a new survey.</a>", [1 => CRM_Utils_System::url('civicrm/survey/add', 'reset=1&action=add')]);
141 }
142
143 if ($this->_force && !$this->_surveyId) {
144
145 $errorMessages[] = ts('Could not find Survey.');
146
147 }
148
149 $this->assign('errorMessages', empty($errorMessages) ? FALSE : $errorMessages);
150 }
151
152 }