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