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