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