Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
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 processing a campaign | |
38 | * | |
39 | */ | |
40 | class CRM_Campaign_Form_Campaign extends CRM_Core_Form { | |
41 | ||
42 | /** | |
43 | * action | |
44 | * | |
45 | * @var int | |
46 | */ | |
47 | protected $_action; | |
48 | ||
49 | /** | |
50 | * context | |
51 | * | |
52 | * @var string | |
53 | */ | |
54 | protected $_context; | |
55 | ||
56 | /** | |
57 | * object values. | |
58 | * | |
59 | * @var array | |
60 | */ | |
61 | protected $_values; | |
62 | ||
63 | /** | |
64 | * the id of the campaign we are proceessing | |
65 | * | |
66 | * @var int | |
67 | * @protected | |
68 | */ | |
69 | protected $_campaignId; | |
70 | ||
71 | public function preProcess() { | |
72 | if (!CRM_Campaign_BAO_Campaign::accessCampaign()) { | |
73 | CRM_Utils_System::permissionDenied(); | |
74 | } | |
75 | ||
76 | //check for custom data type. | |
77 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); | |
78 | $this->assign('cdType', FALSE); | |
79 | if ($this->_cdType) { | |
80 | $this->assign('cdType', TRUE); | |
81 | return CRM_Custom_Form_CustomData::preProcess($this); | |
82 | } | |
83 | ||
84 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
85 | ||
86 | $this->assign('context', $this->_context); | |
87 | ||
88 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this); | |
89 | $this->_campaignId = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
90 | ||
91 | $title = NULL; | |
92 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
93 | $title = ts('Edit Campaign'); | |
94 | } | |
95 | if ($this->_action & CRM_Core_Action::DELETE) { | |
96 | $title = ts('Delete Campaign'); | |
97 | } | |
98 | if ($title) { | |
99 | CRM_Utils_System::setTitle($title); | |
100 | } | |
101 | ||
102 | $session = CRM_Core_Session::singleton(); | |
103 | $session->pushUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=campaign')); | |
104 | $this->assign('action', $this->_action); | |
105 | ||
106 | //load the values; | |
107 | $this->_values = $this->get('values'); | |
108 | if (!is_array($this->_values)) { | |
109 | $this->_values = array(); | |
110 | ||
111 | // if we are editing | |
112 | if (isset($this->_campaignId) && $this->_campaignId) { | |
113 | $params = array('id' => $this->_campaignId); | |
114 | CRM_Campaign_BAO_Campaign::retrieve($params, $this->_values); | |
115 | } | |
116 | ||
117 | //lets use current object session. | |
118 | $this->set('values', $this->_values); | |
119 | } | |
120 | ||
121 | // when custom data is included in form. | |
a7488080 | 122 | if (!empty($_POST['hidden_custom'])) { |
6a488035 TO |
123 | $this->set('type', 'Campaign'); |
124 | $this->set('subType', CRM_Utils_Array::value('campaign_type_id', $_POST)); | |
125 | $this->set('entityId', $this->_campaignId); | |
126 | ||
127 | CRM_Custom_Form_CustomData::preProcess($this); | |
128 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
129 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
130 | } | |
131 | } | |
132 | ||
133 | /** | |
134 | * This function sets the default values for the form. Note that in edit/view mode | |
135 | * the default values are retrieved from the database | |
136 | * | |
137 | * @access public | |
138 | * | |
355ba699 | 139 | * @return void |
6a488035 TO |
140 | */ |
141 | function setDefaultValues() { | |
142 | $defaults = $this->_values; | |
143 | ||
144 | //load only custom data defaults. | |
145 | if ($this->_cdType) { | |
146 | return CRM_Custom_Form_CustomData::setDefaultValues($this); | |
147 | } | |
148 | ||
149 | if (isset($defaults['start_date'])) { | |
150 | list($defaults['start_date'], | |
151 | $defaults['start_date_time'] | |
152 | ) = CRM_Utils_Date::setDateDefaults($defaults['start_date'], | |
153 | 'activityDateTime' | |
154 | ); | |
155 | } | |
156 | else { | |
157 | list($defaults['start_date'], | |
158 | $defaults['start_date_time'] | |
159 | ) = CRM_Utils_Date::setDateDefaults(); | |
160 | } | |
161 | ||
162 | if (isset($defaults['end_date'])) { | |
163 | list($defaults['end_date'], | |
164 | $defaults['end_date_time'] | |
165 | ) = CRM_Utils_Date::setDateDefaults($defaults['end_date'], | |
166 | 'activityDateTime' | |
167 | ); | |
168 | } | |
169 | ||
170 | if (!isset($defaults['is_active'])) { | |
171 | $defaults['is_active'] = 1; | |
172 | } | |
173 | ||
174 | if (!$this->_campaignId) { | |
175 | return $defaults; | |
176 | } | |
177 | ||
178 | $dao = new CRM_Campaign_DAO_CampaignGroup(); | |
179 | ||
180 | $campaignGroups = array(); | |
181 | $dao->campaign_id = $this->_campaignId; | |
182 | $dao->find(); | |
183 | ||
184 | while ($dao->fetch()) { | |
185 | $campaignGroups[$dao->entity_table][$dao->group_type][] = $dao->entity_id; | |
186 | } | |
187 | ||
188 | if (!empty($campaignGroups)) { | |
189 | $defaults['includeGroups'] = $campaignGroups['civicrm_group']['Include']; | |
190 | } | |
191 | return $defaults; | |
192 | } | |
193 | ||
194 | public function buildQuickForm() { | |
195 | if ($this->_action & CRM_Core_Action::DELETE) { | |
196 | ||
197 | $this->addButtons(array( | |
198 | array( | |
199 | 'type' => 'next', | |
200 | 'name' => ts('Delete'), | |
201 | 'isDefault' => TRUE, | |
202 | ), | |
203 | array( | |
204 | 'type' => 'cancel', | |
205 | 'name' => ts('Cancel'), | |
206 | ), | |
207 | ) | |
208 | ); | |
209 | return; | |
210 | } | |
211 | ||
212 | $this->applyFilter('__ALL__', 'trim'); | |
213 | ||
214 | if ($this->_cdType) { | |
215 | return CRM_Custom_Form_CustomData::buildQuickForm($this); | |
216 | } | |
217 | ||
218 | //campaign types. | |
219 | $campaignTypes = CRM_Campaign_PseudoConstant::campaignType(); | |
220 | ||
221 | //lets assign custom data type and subtype. | |
222 | $this->assign('customDataType', 'Campaign'); | |
223 | $this->assign('entityID', $this->_campaignId); | |
224 | $this->assign('customDataSubType', CRM_Utils_Array::value('campaign_type_id', $this->_values)); | |
225 | ||
226 | $attributes = CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Campaign'); | |
227 | ||
228 | // add comaign title. | |
229 | $this->add('text', 'title', ts('Title'), $attributes['title'], TRUE); | |
230 | ||
231 | // add description | |
232 | $this->add('textarea', 'description', ts('Description'), $attributes['description']); | |
233 | ||
234 | // add campaign start date | |
235 | $this->addDateTime('start_date', ts('Start Date'), TRUE, array('formatType' => 'activityDateTime')); | |
236 | ||
237 | // add campaign end date | |
238 | $this->addDateTime('end_date', ts('End Date'), FALSE, array('formatType' => 'activityDateTime')); | |
239 | ||
240 | // add campaign type | |
241 | $this->add('select', 'campaign_type_id', ts('Campaign Type'), | |
242 | array( | |
243 | '' => ts('- select -')) + $campaignTypes, TRUE, | |
244 | array('onChange' => "CRM.buildCustomData( 'Campaign', this.value );") | |
245 | ); | |
246 | ||
247 | // add campaign status | |
248 | $campaignStatus = CRM_Campaign_PseudoConstant::campaignStatus(); | |
249 | $this->addElement('select', 'status_id', ts('Campaign Status'), | |
250 | array( | |
251 | '' => ts('- select -')) + $campaignStatus | |
252 | ); | |
253 | ||
254 | // add External Identifire Element | |
255 | $this->add('text', 'external_identifier', ts('External Id'), | |
256 | CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Campaign', 'external_identifier'), FALSE | |
257 | ); | |
258 | ||
259 | // add Campaign Parent Id | |
260 | $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(CRM_Utils_Array::value('parent_id', $this->_values), | |
261 | $this->_campaignId | |
262 | ); | |
263 | if (!empty($campaigns)) { | |
264 | $this->addElement('select', 'parent_id', ts('Parent Id'), | |
265 | array( | |
266 | '' => ts('- select Parent -')) + $campaigns | |
267 | ); | |
268 | } | |
269 | ||
270 | //get the campaign groups. | |
271 | $groups = CRM_Core_PseudoConstant::group(); | |
272 | ||
273 | $inG = &$this->addElement('advmultiselect', 'includeGroups', | |
274 | ts('Include Group(s)') . ' ', | |
275 | $groups, | |
276 | array( | |
277 | 'size' => 5, | |
278 | 'style' => 'width:240px', | |
279 | 'class' => 'advmultiselect', | |
280 | ) | |
281 | ); | |
282 | $inG->setButtonAttributes('add', array('value' => ts('Add >>'))); | |
283 | $inG->setButtonAttributes('remove', array('value' => ts('<< Remove'))); | |
284 | ||
285 | $this->addWysiwyg('goal_general', ts('Campaign Goals'), array('rows' => 2, 'cols' => 40)); | |
286 | $this->add('text', 'goal_revenue', ts('Revenue Goal'), array('size' => 8, 'maxlength' => 12)); | |
287 | $this->addRule('goal_revenue', ts('Please enter a valid money value (e.g. %1).', | |
288 | array(1 => CRM_Utils_Money::format('99.99', ' ')) | |
289 | ), 'money'); | |
290 | ||
291 | // is this Campaign active | |
292 | $this->addElement('checkbox', 'is_active', ts('Is Active?')); | |
293 | ||
294 | $this->addButtons(array( | |
295 | array( | |
296 | 'type' => 'upload', | |
297 | 'name' => ts('Save'), | |
298 | 'isDefault' => TRUE, | |
299 | ), | |
300 | array( | |
301 | 'type' => 'upload', | |
302 | 'name' => ts('Save and New'), | |
303 | 'subName' => 'new', | |
304 | ), | |
305 | array( | |
306 | 'type' => 'cancel', | |
307 | 'name' => ts('Cancel'), | |
308 | ), | |
309 | ) | |
310 | ); | |
311 | } | |
312 | ||
313 | /** | |
314 | * This function is used to add the rules (mainly global rules) for form. | |
315 | * All local rules are added near the element | |
316 | * | |
355ba699 | 317 | * @return void |
6a488035 TO |
318 | * @access public |
319 | * @see valid_date | |
320 | */ | |
321 | static function formRule($fields, $files, $errors) { | |
322 | $errors = array(); | |
323 | ||
324 | return empty($errors) ? TRUE : $errors; | |
325 | } | |
326 | ||
327 | /** | |
328 | * Form submission of new/edit campaign is processed. | |
329 | * | |
330 | * @access public | |
331 | * | |
355ba699 | 332 | * @return void |
6a488035 TO |
333 | */ |
334 | public function postProcess() { | |
335 | // store the submitted values in an array | |
336 | $params = $this->controller->exportValues($this->_name); | |
337 | $session = CRM_Core_Session::singleton(); | |
338 | ||
339 | $groups = array(); | |
340 | if (isset($this->_campaignId)) { | |
341 | if ($this->_action & CRM_Core_Action::DELETE) { | |
342 | CRM_Campaign_BAO_Campaign::del($this->_campaignId); | |
343 | CRM_Core_Session::setStatus(ts('Campaign has been deleted.'), ts('Record Deleted'), 'success'); | |
344 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=campaign')); | |
345 | return; | |
346 | } | |
347 | $params['id'] = $this->_campaignId; | |
348 | } | |
349 | else { | |
350 | $params['created_id'] = $session->get('userID'); | |
351 | $params['created_date'] = date('YmdHis'); | |
352 | } | |
353 | // format params | |
354 | $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], $params['start_date_time']); | |
355 | $params['end_date'] = CRM_Utils_Date::processDate($params['end_date'], $params['end_date_time'], TRUE); | |
356 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); | |
357 | $params['last_modified_id'] = $session->get('userID'); | |
358 | $params['last_modified_date'] = date('YmdHis'); | |
359 | ||
360 | if (is_array($params['includeGroups'])) { | |
361 | foreach ($params['includeGroups'] as $key => $id) { | |
362 | if ($id) { | |
363 | $groups['include'][] = $id; | |
364 | } | |
365 | } | |
366 | } | |
367 | $params['groups'] = $groups; | |
368 | ||
369 | // delete previous includes/excludes, if campaign already existed | |
370 | $groupTableName = CRM_Contact_BAO_Group::getTableName(); | |
371 | $dao = new CRM_Campaign_DAO_CampaignGroup(); | |
372 | $dao->campaign_id = $this->_campaignId; | |
373 | $dao->entity_table = $groupTableName; | |
374 | $dao->find(); | |
375 | while ($dao->fetch()) { | |
376 | $dao->delete(); | |
377 | } | |
378 | ||
379 | //process custom data. | |
380 | $customFields = CRM_Core_BAO_CustomField::getFields('Campaign', FALSE, FALSE, | |
381 | CRM_Utils_Array::value('campaign_type_id', $params) | |
382 | ); | |
383 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
384 | $customFields, | |
385 | $this->_campaignId, | |
386 | 'Campaign' | |
387 | ); | |
388 | ||
389 | $result = CRM_Campaign_BAO_Campaign::create($params); | |
390 | ||
391 | if ($result) { | |
392 | CRM_Core_Session::setStatus(ts('Campaign %1 has been saved.', array(1 => $result->title)), ts('Saved'), 'success'); | |
393 | $session->pushUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=campaign')); | |
394 | } | |
395 | ||
396 | $buttonName = $this->controller->getButtonName(); | |
397 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
398 | CRM_Core_Session::setStatus(ts(' You can add another Campaign.'), '', 'info'); | |
399 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign/add', 'reset=1&action=add')); | |
400 | } | |
401 | else { | |
402 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=campaign')); | |
403 | } | |
404 | } | |
405 | } | |
406 | ||
407 | ||
408 |