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