Merge pull request #4914 from eileenmcnaughton/minor-tidies
[civicrm-core.git] / CRM / Campaign / Form / Petition.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for adding a petition
38 *
39 */
40 class CRM_Campaign_Form_Petition extends CRM_Core_Form {
41
42 /**
43 * Making this public so we can reference it in the formRule
44 * @var int
45 */
46 public $_surveyId;
47
48 public function preProcess() {
49 if (!CRM_Campaign_BAO_Campaign::accessCampaign()) {
50 CRM_Utils_System::permissionDenied();
51 }
52
53 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this);
54
55 $this->assign('context', $this->_context);
56
57 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this);
58
59 if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::DELETE)) {
60 $this->_surveyId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
61
62 if ($this->_action & CRM_Core_Action::UPDATE) {
63 CRM_Utils_System::setTitle(ts('Edit Survey'));
64 }
65 else {
66 CRM_Utils_System::setTitle(ts('Delete Survey'));
67 }
68 }
69
70 $this->_cdType = CRM_Utils_Array::value('type', $_GET);
71 $this->assign('cdType', FALSE);
72 if ($this->_cdType) {
73 $this->assign('cdType', TRUE);
74 return CRM_Custom_Form_CustomData::preProcess($this);
75 }
76
77 // when custom data is included in this page
78 if (!empty($_POST['hidden_custom'])) {
79 CRM_Custom_Form_CustomData::preProcess($this);
80 CRM_Custom_Form_CustomData::buildQuickForm($this);
81 }
82
83 $session = CRM_Core_Session::singleton();
84 $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey');
85 $session->pushUserContext($url);
86
87 $this->_values = $this->get('values');
88
89 if (!is_array($this->_values)) {
90 $this->_values = array();
91 if ($this->_surveyId) {
92 $params = array('id' => $this->_surveyId);
93 CRM_Campaign_BAO_Survey::retrieve($params, $this->_values);
94 }
95 $this->set('values', $this->_values);
96 }
97
98 $this->assign('action', $this->_action);
99 $this->assign('surveyId', $this->_surveyId);
100 // for custom data
101 $this->assign('entityID', $this->_surveyId);
102
103 if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::DELETE)) {
104 $this->_surveyId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
105
106 if ($this->_action & CRM_Core_Action::UPDATE) {
107 CRM_Utils_System::setTitle(ts('Edit Petition'));
108 }
109 else {
110 CRM_Utils_System::setTitle(ts('Delete Petition'));
111 }
112 }
113
114 $session = CRM_Core_Session::singleton();
115 $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=petition');
116 $session->pushUserContext($url);
117
118 CRM_Utils_System::appendBreadCrumb(array(array('title' => ts('Petition Dashboard'), 'url' => $url)));
119 }
120
121 /**
122 * Set default values for the form. Note that in edit/view mode
123 * the default values are retrieved from the database
124 *
125 * @param null
126 *
127 * @return array
128 * array of default values
129 */
130 public function setDefaultValues() {
131 $defaults = $this->_values;
132
133 $ufContactJoinParams = array(
134 'entity_table' => 'civicrm_survey',
135 'entity_id' => $this->_surveyId,
136 'weight' => 2,
137 );
138
139 if ($ufContactGroupId = CRM_Core_BAO_UFJoin::findUFGroupId($ufContactJoinParams)) {
140 $defaults['contact_profile_id'] = $ufContactGroupId;
141 }
142 $ufActivityJoinParams = array(
143 'entity_table' => 'civicrm_survey',
144 'entity_id' => $this->_surveyId,
145 'weight' => 1,
146 );
147
148 if ($ufActivityGroupId = CRM_Core_BAO_UFJoin::findUFGroupId($ufActivityJoinParams)) {
149 $defaults['profile_id'] = $ufActivityGroupId;
150 }
151
152 if (!isset($defaults['is_active'])) {
153 $defaults['is_active'] = 1;
154 }
155
156 $defaultSurveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE);
157 if (!isset($defaults['is_default']) && empty($defaultSurveys)) {
158 $defaults['is_default'] = 1;
159 }
160
161 return $defaults;
162 }
163
164
165 public function buildQuickForm() {
166
167 if ($this->_action & CRM_Core_Action::DELETE) {
168 $this->addButtons(
169 array(
170 array(
171 'type' => 'next',
172 'name' => ts('Delete'),
173 'isDefault' => TRUE,
174 ),
175 array(
176 'type' => 'cancel',
177 'name' => ts('Cancel'),
178 ),
179 )
180 );
181 return;
182 }
183
184 $this->add('text', 'title', ts('Petition Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE);
185
186 $attributes = CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey');
187
188 $petitionTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'petition', 'name');
189 $this->addElement('hidden', 'activity_type_id', $petitionTypeID);
190
191 // script / instructions / description of petition purpose
192 $this->addWysiwyg('instructions', ts('Introduction'), $attributes['instructions']);
193
194 // Campaign id
195 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(CRM_Utils_Array::value('campaign_id', $this->_values));
196 $this->add('select', 'campaign_id', ts('Campaign'), array('' => ts('- select -')) + $campaigns);
197
198 $customContactProfiles = CRM_Core_BAO_UFGroup::getProfiles(array('Individual'));
199 // custom group id
200 $this->add('select', 'contact_profile_id', ts('Contact Profile'),
201 array(
202 '' => ts('- select -')
203 ) + $customContactProfiles, TRUE
204 );
205
206 $customProfiles = CRM_Core_BAO_UFGroup::getProfiles(array('Activity'));
207 // custom group id
208 $this->add('select', 'profile_id', ts('Activity Profile'),
209 array(
210 '' => ts('- select -'),
211 ) + $customProfiles
212 );
213
214 // thank you title and text (html allowed in text)
215 $this->add('text', 'thankyou_title', ts('Thank-you Page Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'thankyou_title'));
216 $this->addWysiwyg('thankyou_text', ts('Thank-you Message'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'thankyou_text'));
217
218 // bypass email confirmation?
219 $this->add('checkbox', 'bypass_confirm', ts('Bypass email confirmation'));
220
221 //is share through social media
222 $this->addElement('checkbox', 'is_share', ts('Allow sharing through social media?'));
223
224 // is active ?
225 $this->add('checkbox', 'is_active', ts('Is Active?'));
226
227 // is default ?
228 $this->add('checkbox', 'is_default', ts('Is Default?'));
229
230 // add buttons
231 $this->addButtons(
232 array(
233 array(
234 'type' => 'next',
235 'name' => ts('Save'),
236 'isDefault' => TRUE,
237 ),
238 array(
239 'type' => 'next',
240 'name' => ts('Save and New'),
241 'subName' => 'new',
242 ),
243 array(
244 'type' => 'cancel',
245 'name' => ts('Cancel'),
246 ),
247 )
248 );
249
250 // add a form rule to check default value
251 $this->addFormRule(array('CRM_Campaign_Form_Petition', 'formRule'), $this);
252 }
253
254 /**
255 * Global validation rules for the form
256 * @param $fields
257 * @param $files
258 * @param $form
259 * @return array|bool
260 */
261 public static function formRule($fields, $files, $form) {
262 $errors = array();
263 // Petitions should be unique by: title, campaign ID (if assigned) and activity type ID
264 // NOTE: This class is called for both Petition create / update AND for Survey Results tab, but this rule is only for Petition.
265 $where = array('activity_type_id = %1', 'title = %2');
266 $params = array(
267 1 => array($fields['activity_type_id'], 'Integer'),
268 2 => array($fields['title'], 'String'),
269 );
270 $uniqueRuleErrorMessage = ts('This title is already associated with the selected activity type. Please specify a unique title.');
271
272 if (empty($fields['campaign_id'])) {
273 $where[] = 'campaign_id IS NULL';
274 }
275 else {
276 $where[] = 'campaign_id = %3';
277 $params[3] = array($fields['campaign_id'], 'Integer');
278 $uniqueRuleErrorMessage = ts('This title is already associated with the selected campaign and activity type. Please specify a unique title.');
279 }
280
281 // Exclude current Petition row if UPDATE.
282 if ($form->_surveyId) {
283 $where[] = 'id != %4';
284 $params[4] = array($form->_surveyId, 'Integer');
285 }
286
287 $whereClause = implode(' AND ', $where);
288
289 $query = "
290 SELECT COUNT(*) AS row_count
291 FROM civicrm_survey
292 WHERE $whereClause
293 ";
294
295 $result = CRM_Core_DAO::singleValueQuery($query, $params);
296 if ($result >= 1) {
297 $errors['title'] = $uniqueRuleErrorMessage;
298 }
299 return empty($errors) ? TRUE : $errors;
300 }
301
302
303 public function postProcess() {
304 // store the submitted values in an array
305 $params = $this->controller->exportValues($this->_name);
306
307 $session = CRM_Core_Session::singleton();
308
309 $params['last_modified_id'] = $session->get('userID');
310 $params['last_modified_date'] = date('YmdHis');
311 $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE);
312
313 if ($this->_surveyId) {
314
315 if ($this->_action & CRM_Core_Action::DELETE) {
316 CRM_Campaign_BAO_Survey::del($this->_surveyId);
317 CRM_Core_Session::setStatus(ts(' Petition has been deleted.'), ts('Record Deleted'), 'success');
318 $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=petition'));
319 return;
320 }
321
322 $params['id'] = $this->_surveyId;
323 }
324 else {
325 $params['created_id'] = $session->get('userID');
326 $params['created_date'] = date('YmdHis');
327 }
328
329 $params['bypass_confirm'] = CRM_Utils_Array::value('bypass_confirm', $params, 0);
330 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0);
331 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, 0);
332
333 $surveyId = CRM_Campaign_BAO_Survey::create($params);
334
335 // also update the ProfileModule tables
336 $ufJoinParams = array(
337 'is_active' => 1,
338 'module' => 'CiviCampaign',
339 'entity_table' => 'civicrm_survey',
340 'entity_id' => $surveyId->id,
341 );
342
343 // first delete all past entries
344 if ($this->_surveyId) {
345 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
346 }
347 if (!empty($params['profile_id'])) {
348 $ufJoinParams['weight'] = 1;
349 $ufJoinParams['uf_group_id'] = $params['profile_id'];
350 CRM_Core_BAO_UFJoin::create($ufJoinParams);
351 }
352
353 if (!empty($params['contact_profile_id'])) {
354 $ufJoinParams['weight'] = 2;
355 $ufJoinParams['uf_group_id'] = $params['contact_profile_id'];
356 CRM_Core_BAO_UFJoin::create($ufJoinParams);
357 }
358
359 if (!is_a($surveyId, 'CRM_Core_Error')) {
360 CRM_Core_Session::setStatus(ts('Petition has been saved.'), ts('Saved'), 'success');
361 }
362
363 $buttonName = $this->controller->getButtonName();
364 if ($buttonName == $this->getButtonName('next', 'new')) {
365 CRM_Core_Session::setStatus(ts(' You can add another Petition.'), '', 'info');
366 $session->replaceUserContext(CRM_Utils_System::url('civicrm/petition/add', 'reset=1&action=add'));
367 }
368 else {
369 $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=petition'));
370 }
371 }
372 }