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