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