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