| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.3 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 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-2013 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class generates form components for adding a petition |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | class CRM_Campaign_Form_Petition extends CRM_Core_Form { |
| 42 | |
| 43 | /** |
| 44 | * @var int |
| 45 | * @protected |
| 46 | */ |
| 47 | protected $_surveyId; |
| 48 | |
| 49 | public function preProcess() { |
| 50 | if (!CRM_Campaign_BAO_Campaign::accessCampaign()) { |
| 51 | CRM_Utils_System::permissionDenied(); |
| 52 | } |
| 53 | |
| 54 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); |
| 55 | |
| 56 | $this->assign('context', $this->_context); |
| 57 | |
| 58 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this); |
| 59 | |
| 60 | if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::DELETE)) { |
| 61 | $this->_surveyId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE); |
| 62 | |
| 63 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 64 | CRM_Utils_System::setTitle(ts('Edit Survey')); |
| 65 | } |
| 66 | else { |
| 67 | CRM_Utils_System::setTitle(ts('Delete Survey')); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); |
| 72 | $this->assign('cdType', FALSE); |
| 73 | if ($this->_cdType) { |
| 74 | $this->assign('cdType', TRUE); |
| 75 | return CRM_Custom_Form_CustomData::preProcess($this); |
| 76 | } |
| 77 | |
| 78 | // when custom data is included in this page |
| 79 | if (CRM_Utils_Array::value('hidden_custom', $_POST)) { |
| 80 | CRM_Custom_Form_CustomData::preProcess($this); |
| 81 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
| 82 | } |
| 83 | |
| 84 | $session = CRM_Core_Session::singleton(); |
| 85 | $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey'); |
| 86 | $session->pushUserContext($url); |
| 87 | |
| 88 | $this->_values = $this->get('values'); |
| 89 | |
| 90 | if (!is_array($this->_values)) { |
| 91 | $this->_values = array(); |
| 92 | if ($this->_surveyId) { |
| 93 | $params = array('id' => $this->_surveyId); |
| 94 | CRM_Campaign_BAO_Survey::retrieve($params, $this->_values); |
| 95 | } |
| 96 | $this->set('values', $this->_values); |
| 97 | } |
| 98 | |
| 99 | $this->assign('action', $this->_action); |
| 100 | $this->assign('surveyId', $this->_surveyId); |
| 101 | // for custom data |
| 102 | $this->assign('entityID', $this->_surveyId); |
| 103 | |
| 104 | if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::DELETE)) { |
| 105 | $this->_surveyId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE); |
| 106 | |
| 107 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 108 | CRM_Utils_System::setTitle(ts('Edit Petition')); |
| 109 | } |
| 110 | else { |
| 111 | CRM_Utils_System::setTitle(ts('Delete Petition')); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | $session = CRM_Core_Session::singleton(); |
| 116 | $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=petition'); |
| 117 | $session->pushUserContext($url); |
| 118 | |
| 119 | CRM_Utils_System::appendBreadCrumb(array(array('title' => ts('Petition Dashboard'), 'url' => $url))); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * This function sets the default values for the form. Note that in edit/view mode |
| 124 | * the default values are retrieved from the database |
| 125 | * |
| 126 | * @param null |
| 127 | * |
| 128 | * @return array array of default values |
| 129 | * @access public |
| 130 | */ |
| 131 | function setDefaultValues() { |
| 132 | $defaults = $this->_values; |
| 133 | |
| 134 | $ufJoinParams = array( |
| 135 | 'entity_table' => 'civicrm_survey', |
| 136 | 'entity_id' => $this->_surveyId, |
| 137 | 'weight' => 2, |
| 138 | ); |
| 139 | |
| 140 | if ($ufGroupId = CRM_Core_BAO_UFJoin::findUFGroupId($ufJoinParams)) { |
| 141 | $defaults['contact_profile_id'] = $ufGroupId; |
| 142 | } |
| 143 | |
| 144 | if (!isset($defaults['is_active'])) { |
| 145 | $defaults['is_active'] = 1; |
| 146 | } |
| 147 | |
| 148 | $defaultSurveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE); |
| 149 | if (!isset($defaults['is_default']) && empty($defaultSurveys)) { |
| 150 | $defaults['is_default'] = 1; |
| 151 | } |
| 152 | |
| 153 | return $defaults; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | public function buildQuickForm() { |
| 158 | |
| 159 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 160 | |
| 161 | $this->addButtons(array( |
| 162 | array( |
| 163 | 'type' => 'next', |
| 164 | 'name' => ts('Delete'), |
| 165 | 'isDefault' => TRUE, |
| 166 | ), |
| 167 | array( |
| 168 | 'type' => 'cancel', |
| 169 | 'name' => ts('Cancel'), |
| 170 | ), |
| 171 | ) |
| 172 | ); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | $this->add('text', 'title', ts('Petition Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE); |
| 178 | |
| 179 | $attributes = CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey'); |
| 180 | |
| 181 | $petitionTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'petition', 'name'); |
| 182 | $this->addElement('hidden', 'activity_type_id', $petitionTypeID); |
| 183 | |
| 184 | // script / instructions / description of petition purpose |
| 185 | $this->addWysiwyg('instructions', ts('Introduction'), $attributes['instructions']); |
| 186 | |
| 187 | // Campaign id |
| 188 | $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(CRM_Utils_Array::value('campaign_id', $this->_values)); |
| 189 | $this->add('select', 'campaign_id', ts('Campaign'), array('' => ts('- select -')) + $campaigns); |
| 190 | |
| 191 | $customContactProfiles = CRM_Core_BAO_UFGroup::getProfiles(array('Individual')); |
| 192 | // custom group id |
| 193 | $this->add('select', 'contact_profile_id', ts('Contact Profile'), |
| 194 | array( |
| 195 | '' => ts('- select -')) + $customContactProfiles, TRUE |
| 196 | ); |
| 197 | |
| 198 | $customProfiles = CRM_Core_BAO_UFGroup::getProfiles(array('Activity')); |
| 199 | // custom group id |
| 200 | $this->add('select', 'profile_id', ts('Activity Profile'), |
| 201 | array( |
| 202 | '' => ts('- select -')) + $customProfiles |
| 203 | ); |
| 204 | |
| 205 | // thank you title and text (html allowed in text) |
| 206 | $this->add('text', 'thankyou_title', ts('Thank-you Page Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'thankyou_title')); |
| 207 | $this->addWysiwyg('thankyou_text', ts('Thank-you Message'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'thankyou_text')); |
| 208 | |
| 209 | // bypass email confirmation? |
| 210 | $this->add('checkbox', 'bypass_confirm', ts('Bypass email confirmation')); |
| 211 | |
| 212 | // is active ? |
| 213 | $this->add('checkbox', 'is_active', ts('Is Active?')); |
| 214 | |
| 215 | // is default ? |
| 216 | $this->add('checkbox', 'is_default', ts('Is Default?')); |
| 217 | |
| 218 | // add buttons |
| 219 | $this->addButtons(array( |
| 220 | array( |
| 221 | 'type' => 'next', |
| 222 | 'name' => ts('Save'), |
| 223 | 'isDefault' => TRUE, |
| 224 | ), |
| 225 | array( |
| 226 | 'type' => 'next', |
| 227 | 'name' => ts('Save and New'), |
| 228 | 'subName' => 'new', |
| 229 | ), |
| 230 | array( |
| 231 | 'type' => 'cancel', |
| 232 | 'name' => ts('Cancel'), |
| 233 | ), |
| 234 | ) |
| 235 | ); |
| 236 | |
| 237 | // add a form rule to check default value |
| 238 | $this->addFormRule(array('CRM_Campaign_Form_Survey_Results', 'formRule'), $this); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | public function postProcess() { |
| 243 | // store the submitted values in an array |
| 244 | $params = $this->controller->exportValues($this->_name); |
| 245 | |
| 246 | $session = CRM_Core_Session::singleton(); |
| 247 | |
| 248 | $params['last_modified_id'] = $session->get('userID'); |
| 249 | $params['last_modified_date'] = date('YmdHis'); |
| 250 | |
| 251 | if ($this->_surveyId) { |
| 252 | |
| 253 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 254 | CRM_Campaign_BAO_Survey::del($this->_surveyId); |
| 255 | CRM_Core_Session::setStatus(ts(' Petition has been deleted.'), ts('Record Deleted'), 'success'); |
| 256 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=petition')); |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | $params['id'] = $this->_surveyId; |
| 261 | } |
| 262 | else { |
| 263 | $params['created_id'] = $session->get('userID'); |
| 264 | $params['created_date'] = date('YmdHis'); |
| 265 | } |
| 266 | |
| 267 | $params['bypass_confirm'] = CRM_Utils_Array::value('bypass_confirm', $params, 0); |
| 268 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0); |
| 269 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, 0); |
| 270 | |
| 271 | $surveyId = CRM_Campaign_BAO_Survey::create($params); |
| 272 | |
| 273 | |
| 274 | // also update the ProfileModule tables |
| 275 | $ufJoinParams = array( |
| 276 | 'is_active' => 1, |
| 277 | 'module' => 'CiviCampaign', |
| 278 | 'entity_table' => 'civicrm_survey', |
| 279 | 'entity_id' => $surveyId->id, |
| 280 | ); |
| 281 | |
| 282 | // first delete all past entries |
| 283 | if ($this->_surveyId) { |
| 284 | CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams); |
| 285 | } |
| 286 | if (CRM_Utils_Array::value('profile_id', $params)) { |
| 287 | $ufJoinParams['weight'] = 1; |
| 288 | $ufJoinParams['uf_group_id'] = $params['profile_id']; |
| 289 | CRM_Core_BAO_UFJoin::create($ufJoinParams); |
| 290 | } |
| 291 | |
| 292 | if (CRM_Utils_Array::value('contact_profile_id', $params)) { |
| 293 | $ufJoinParams['weight'] = 2; |
| 294 | $ufJoinParams['uf_group_id'] = $params['contact_profile_id']; |
| 295 | CRM_Core_BAO_UFJoin::create($ufJoinParams); |
| 296 | } |
| 297 | |
| 298 | if (!is_a($surveyId, 'CRM_Core_Error')) { |
| 299 | CRM_Core_Session::setStatus(ts('Petition has been saved.'), ts('Saved'), 'success'); |
| 300 | } |
| 301 | |
| 302 | $buttonName = $this->controller->getButtonName(); |
| 303 | if ($buttonName == $this->getButtonName('next', 'new')) { |
| 304 | CRM_Core_Session::setStatus(ts(' You can add another Petition.'), '', 'info'); |
| 305 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/petition/add', 'reset=1&action=add')); |
| 306 | } |
| 307 | else { |
| 308 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=petition')); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | |
| 314 | |
| 315 | |