Merge pull request #5687 from relldoesphp/ckeditor_fix
[civicrm-core.git] / CRM / Campaign / Form / Petition.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 * @return array
126 * array of default values
127 */
128 public function setDefaultValues() {
129 $defaults = $this->_values;
130
131 $ufContactJoinParams = array(
132 'entity_table' => 'civicrm_survey',
133 'entity_id' => $this->_surveyId,
134 'weight' => 2,
135 );
136
137 if ($ufContactGroupId = CRM_Core_BAO_UFJoin::findUFGroupId($ufContactJoinParams)) {
138 $defaults['contact_profile_id'] = $ufContactGroupId;
139 }
140 $ufActivityJoinParams = array(
141 'entity_table' => 'civicrm_survey',
142 'entity_id' => $this->_surveyId,
143 'weight' => 1,
144 );
145
146 if ($ufActivityGroupId = CRM_Core_BAO_UFJoin::findUFGroupId($ufActivityJoinParams)) {
147 $defaults['profile_id'] = $ufActivityGroupId;
148 }
149
150 if (!isset($defaults['is_active'])) {
151 $defaults['is_active'] = 1;
152 }
153
154 $defaultSurveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE);
155 if (!isset($defaults['is_default']) && empty($defaultSurveys)) {
156 $defaults['is_default'] = 1;
157 }
158
159 return $defaults;
160 }
161
162
163 public function buildQuickForm() {
164
165 if ($this->_action & CRM_Core_Action::DELETE) {
166 $this->addButtons(
167 array(
168 array(
169 'type' => 'next',
170 'name' => ts('Delete'),
171 'isDefault' => TRUE,
172 ),
173 array(
174 'type' => 'cancel',
175 'name' => ts('Cancel'),
176 ),
177 )
178 );
179 return;
180 }
181
182 $this->add('text', 'title', ts('Petition Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE);
183
184 $attributes = CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey');
185
186 $petitionTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'petition', 'name');
187 $this->addElement('hidden', 'activity_type_id', $petitionTypeID);
188
189 // script / instructions / description of petition purpose
190 $this->add('wysiwyg', 'instructions', ts('Introduction'), $attributes['instructions']);
191
192 // Campaign id
193 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(CRM_Utils_Array::value('campaign_id', $this->_values));
194 $this->add('select', 'campaign_id', ts('Campaign'), array('' => ts('- select -')) + $campaigns);
195
196 $customContactProfiles = CRM_Core_BAO_UFGroup::getProfiles(array('Individual'));
197 // custom group id
198 $this->add('select', 'contact_profile_id', ts('Contact Profile'),
199 array(
200 '' => ts('- select -'),
201 ) + $customContactProfiles, TRUE
202 );
203
204 $customProfiles = CRM_Core_BAO_UFGroup::getProfiles(array('Activity'));
205 // custom group id
206 $this->add('select', 'profile_id', ts('Activity Profile'),
207 array(
208 '' => ts('- select -'),
209 ) + $customProfiles
210 );
211
212 // thank you title and text (html allowed in text)
213 $this->add('text', 'thankyou_title', ts('Thank-you Page Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'thankyou_title'));
214 $this->add('wysiwyg', 'thankyou_text', ts('Thank-you Message'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'thankyou_text'));
215
216 // bypass email confirmation?
217 $this->add('checkbox', 'bypass_confirm', ts('Bypass email confirmation'));
218
219 //is share through social media
220 $this->addElement('checkbox', 'is_share', ts('Allow sharing through social media?'));
221
222 // is active ?
223 $this->add('checkbox', 'is_active', ts('Is Active?'));
224
225 // is default ?
226 $this->add('checkbox', 'is_default', ts('Is Default?'));
227
228 // add buttons
229 $this->addButtons(
230 array(
231 array(
232 'type' => 'next',
233 'name' => ts('Save'),
234 'isDefault' => TRUE,
235 ),
236 array(
237 'type' => 'next',
238 'name' => ts('Save and New'),
239 'subName' => 'new',
240 ),
241 array(
242 'type' => 'cancel',
243 'name' => ts('Cancel'),
244 ),
245 )
246 );
247
248 // add a form rule to check default value
249 $this->addFormRule(array('CRM_Campaign_Form_Petition', 'formRule'), $this);
250 }
251
252 /**
253 * Global validation rules for the form.
254 * @param $fields
255 * @param $files
256 * @param $form
257 * @return array|bool
258 */
259 public static function formRule($fields, $files, $form) {
260 $errors = array();
261 // Petitions should be unique by: title, campaign ID (if assigned) and activity type ID
262 // NOTE: This class is called for both Petition create / update AND for Survey Results tab, but this rule is only for Petition.
263 $where = array('activity_type_id = %1', 'title = %2');
264 $params = array(
265 1 => array($fields['activity_type_id'], 'Integer'),
266 2 => array($fields['title'], 'String'),
267 );
268 $uniqueRuleErrorMessage = ts('This title is already associated with the selected activity type. Please specify a unique title.');
269
270 if (empty($fields['campaign_id'])) {
271 $where[] = 'campaign_id IS NULL';
272 }
273 else {
274 $where[] = 'campaign_id = %3';
275 $params[3] = array($fields['campaign_id'], 'Integer');
276 $uniqueRuleErrorMessage = ts('This title is already associated with the selected campaign and activity type. Please specify a unique title.');
277 }
278
279 // Exclude current Petition row if UPDATE.
280 if ($form->_surveyId) {
281 $where[] = 'id != %4';
282 $params[4] = array($form->_surveyId, 'Integer');
283 }
284
285 $whereClause = implode(' AND ', $where);
286
287 $query = "
288 SELECT COUNT(*) AS row_count
289 FROM civicrm_survey
290 WHERE $whereClause
291 ";
292
293 $result = CRM_Core_DAO::singleValueQuery($query, $params);
294 if ($result >= 1) {
295 $errors['title'] = $uniqueRuleErrorMessage;
296 }
297 return empty($errors) ? TRUE : $errors;
298 }
299
300
301 public function postProcess() {
302 // store the submitted values in an array
303 $params = $this->controller->exportValues($this->_name);
304
305 $session = CRM_Core_Session::singleton();
306
307 $params['last_modified_id'] = $session->get('userID');
308 $params['last_modified_date'] = date('YmdHis');
309 $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE);
310
311 if ($this->_surveyId) {
312
313 if ($this->_action & CRM_Core_Action::DELETE) {
314 CRM_Campaign_BAO_Survey::del($this->_surveyId);
315 CRM_Core_Session::setStatus(ts(' Petition has been deleted.'), ts('Record Deleted'), 'success');
316 $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=petition'));
317 return;
318 }
319
320 $params['id'] = $this->_surveyId;
321 }
322 else {
323 $params['created_id'] = $session->get('userID');
324 $params['created_date'] = date('YmdHis');
325 }
326
327 $params['bypass_confirm'] = CRM_Utils_Array::value('bypass_confirm', $params, 0);
328 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0);
329 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, 0);
330
331 $surveyId = CRM_Campaign_BAO_Survey::create($params);
332
333 // also update the ProfileModule tables
334 $ufJoinParams = array(
335 'is_active' => 1,
336 'module' => 'CiviCampaign',
337 'entity_table' => 'civicrm_survey',
338 'entity_id' => $surveyId->id,
339 );
340
341 // first delete all past entries
342 if ($this->_surveyId) {
343 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
344 }
345 if (!empty($params['profile_id'])) {
346 $ufJoinParams['weight'] = 1;
347 $ufJoinParams['uf_group_id'] = $params['profile_id'];
348 CRM_Core_BAO_UFJoin::create($ufJoinParams);
349 }
350
351 if (!empty($params['contact_profile_id'])) {
352 $ufJoinParams['weight'] = 2;
353 $ufJoinParams['uf_group_id'] = $params['contact_profile_id'];
354 CRM_Core_BAO_UFJoin::create($ufJoinParams);
355 }
356
357 if (!is_a($surveyId, 'CRM_Core_Error')) {
358 CRM_Core_Session::setStatus(ts('Petition has been saved.'), ts('Saved'), 'success');
359 }
360
361 $buttonName = $this->controller->getButtonName();
362 if ($buttonName == $this->getButtonName('next', 'new')) {
363 CRM_Core_Session::setStatus(ts(' You can add another Petition.'), '', 'info');
364 $session->replaceUserContext(CRM_Utils_System::url('civicrm/petition/add', 'reset=1&action=add'));
365 }
366 else {
367 $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=petition'));
368 }
369 }
370
371 }