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