phpcs fix
[civicrm-core.git] / CRM / Campaign / Form / Campaign.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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;
d5965a37 69
6e62b28c
TM
70 /**
71 * Explicitly declare the entity api name.
72 */
73 public function getDefaultEntity() {
74 return 'Campaign';
75 }
6a488035
TO
76
77 public function preProcess() {
78 if (!CRM_Campaign_BAO_Campaign::accessCampaign()) {
79 CRM_Utils_System::permissionDenied();
80 }
81
82 //check for custom data type.
83 $this->_cdType = CRM_Utils_Array::value('type', $_GET);
84 $this->assign('cdType', FALSE);
85 if ($this->_cdType) {
86 $this->assign('cdType', TRUE);
87 return CRM_Custom_Form_CustomData::preProcess($this);
88 }
89
90 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this);
91
92 $this->assign('context', $this->_context);
93
94 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this);
95 $this->_campaignId = CRM_Utils_Request::retrieve('id', 'Positive', $this);
96
97 $title = NULL;
98 if ($this->_action & CRM_Core_Action::UPDATE) {
99 $title = ts('Edit Campaign');
100 }
101 if ($this->_action & CRM_Core_Action::DELETE) {
102 $title = ts('Delete Campaign');
103 }
104 if ($title) {
105 CRM_Utils_System::setTitle($title);
106 }
107
108 $session = CRM_Core_Session::singleton();
109 $session->pushUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=campaign'));
110 $this->assign('action', $this->_action);
111
112 //load the values;
113 $this->_values = $this->get('values');
114 if (!is_array($this->_values)) {
115 $this->_values = array();
116
117 // if we are editing
118 if (isset($this->_campaignId) && $this->_campaignId) {
119 $params = array('id' => $this->_campaignId);
120 CRM_Campaign_BAO_Campaign::retrieve($params, $this->_values);
121 }
122
123 //lets use current object session.
124 $this->set('values', $this->_values);
125 }
126
127 // when custom data is included in form.
a7488080 128 if (!empty($_POST['hidden_custom'])) {
6a488035
TO
129 $this->set('type', 'Campaign');
130 $this->set('subType', CRM_Utils_Array::value('campaign_type_id', $_POST));
131 $this->set('entityId', $this->_campaignId);
132
133 CRM_Custom_Form_CustomData::preProcess($this);
134 CRM_Custom_Form_CustomData::buildQuickForm($this);
135 CRM_Custom_Form_CustomData::setDefaultValues($this);
136 }
137 }
138
139 /**
c490a46a 140 * Set default values for the form. Note that in edit/view mode
6a488035
TO
141 * the default values are retrieved from the database
142 *
6a488035 143 *
2221b3e3 144 * @return array
6a488035 145 */
00be9182 146 public function setDefaultValues() {
6a488035
TO
147 $defaults = $this->_values;
148
149 //load only custom data defaults.
150 if ($this->_cdType) {
151 return CRM_Custom_Form_CustomData::setDefaultValues($this);
152 }
153
154 if (isset($defaults['start_date'])) {
006389de
TO
155 list($defaults['start_date'], $defaults['start_date_time'])
156 = CRM_Utils_Date::setDateDefaults($defaults['start_date'], 'activityDateTime');
6a488035
TO
157 }
158 else {
006389de
TO
159 list($defaults['start_date'], $defaults['start_date_time'])
160 = CRM_Utils_Date::setDateDefaults();
6a488035
TO
161 }
162
163 if (isset($defaults['end_date'])) {
006389de
TO
164 list($defaults['end_date'], $defaults['end_date_time'])
165 = CRM_Utils_Date::setDateDefaults($defaults['end_date'], 'activityDateTime');
6a488035
TO
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 266
5d51a2f9 267 $this->add('wysiwyg', 'goal_general', ts('Campaign Goals'), array('rows' => 2, 'cols' => 40));
6a488035
TO
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).',
353ffa53
TO
270 array(1 => CRM_Utils_Money::format('99.99', ' '))
271 ), 'money');
6a488035
TO
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 /**
dc195289 296 * add the rules (mainly global rules) for form.
6a488035
TO
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
353ffa53
TO
354 $groupTableName = CRM_Contact_BAO_Group::getTableName();
355 $dao = new CRM_Campaign_DAO_CampaignGroup();
356 $dao->campaign_id = $this->_campaignId;
6a488035
TO
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,
6a488035
TO
368 $this->_campaignId,
369 'Campaign'
370 );
371
372 $result = CRM_Campaign_BAO_Campaign::create($params);
373
374 if ($result) {
375 CRM_Core_Session::setStatus(ts('Campaign %1 has been saved.', array(1 => $result->title)), ts('Saved'), 'success');
376 $session->pushUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=campaign'));
377 }
378
379 $buttonName = $this->controller->getButtonName();
380 if ($buttonName == $this->getButtonName('upload', 'new')) {
381 CRM_Core_Session::setStatus(ts(' You can add another Campaign.'), '', 'info');
382 $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign/add', 'reset=1&action=add'));
383 }
384 else {
385 $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=campaign'));
386 }
387 }
96025800 388
6a488035 389}