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