Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
f299f7db | 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
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 | |
f299f7db | 31 | * @copyright CiviCRM LLC (c) 2004-2020 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
ce064e4f | 35 | * This class generates form components for processing a campaign. |
6a488035 TO |
36 | */ |
37 | class CRM_Campaign_Form_Campaign extends CRM_Core_Form { | |
38 | ||
39 | /** | |
100fef9d | 40 | * Action |
6a488035 TO |
41 | * |
42 | * @var int | |
43 | */ | |
3a936dab | 44 | public $_action; |
6a488035 TO |
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 | ||
edc80cda | 79 | $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); |
6a488035 TO |
80 | |
81 | $this->assign('context', $this->_context); | |
82 | ||
83 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this); | |
484627ca | 84 | $this->_campaignId = CRM_Utils_Request::retrieve('id', 'Positive'); |
6a488035 TO |
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)) { | |
be2fb01f | 104 | $this->_values = []; |
6a488035 TO |
105 | |
106 | // if we are editing | |
107 | if (isset($this->_campaignId) && $this->_campaignId) { | |
be2fb01f | 108 | $params = ['id' => $this->_campaignId]; |
6a488035 TO |
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'])) { |
c5366541 | 118 | $campaignTypeId = empty($_POST['campaign_type_id']) ? NULL : $_POST['campaign_type_id']; |
6a488035 | 119 | $this->set('type', 'Campaign'); |
c5366541 | 120 | $this->set('subType', $campaignTypeId); |
6a488035 TO |
121 | $this->set('entityId', $this->_campaignId); |
122 | ||
c5366541 | 123 | CRM_Custom_Form_CustomData::preProcess($this, NULL, $campaignTypeId, 1, 'Campaign', $this->_campaignId); |
6a488035 TO |
124 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
125 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
126 | } | |
127 | } | |
128 | ||
129 | /** | |
c490a46a | 130 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
131 | * the default values are retrieved from the database |
132 | * | |
6a488035 | 133 | * |
2221b3e3 | 134 | * @return array |
6a488035 | 135 | */ |
00be9182 | 136 | public function setDefaultValues() { |
6a488035 TO |
137 | $defaults = $this->_values; |
138 | ||
c38a7f6b CW |
139 | if (empty($defaults['start_date'])) { |
140 | $defaults['start_date'] = date('Y-m-d H:i:s'); | |
6a488035 TO |
141 | } |
142 | ||
143 | if (!isset($defaults['is_active'])) { | |
144 | $defaults['is_active'] = 1; | |
145 | } | |
146 | ||
147 | if (!$this->_campaignId) { | |
148 | return $defaults; | |
149 | } | |
150 | ||
151 | $dao = new CRM_Campaign_DAO_CampaignGroup(); | |
152 | ||
be2fb01f | 153 | $campaignGroups = []; |
6a488035 TO |
154 | $dao->campaign_id = $this->_campaignId; |
155 | $dao->find(); | |
156 | ||
157 | while ($dao->fetch()) { | |
158 | $campaignGroups[$dao->entity_table][$dao->group_type][] = $dao->entity_id; | |
159 | } | |
160 | ||
161 | if (!empty($campaignGroups)) { | |
162 | $defaults['includeGroups'] = $campaignGroups['civicrm_group']['Include']; | |
163 | } | |
164 | return $defaults; | |
165 | } | |
166 | ||
167 | public function buildQuickForm() { | |
1107b319 | 168 | $this->add('hidden', 'id', $this->_campaignId); |
6a488035 TO |
169 | if ($this->_action & CRM_Core_Action::DELETE) { |
170 | ||
be2fb01f | 171 | $this->addButtons([ |
5d4fcf54 TO |
172 | [ |
173 | 'type' => 'next', | |
174 | 'name' => ts('Delete'), | |
175 | 'isDefault' => TRUE, | |
176 | ], | |
177 | [ | |
178 | 'type' => 'cancel', | |
179 | 'name' => ts('Cancel'), | |
180 | ], | |
181 | ]); | |
6a488035 TO |
182 | return; |
183 | } | |
184 | ||
185 | $this->applyFilter('__ALL__', 'trim'); | |
186 | ||
6a488035 TO |
187 | //lets assign custom data type and subtype. |
188 | $this->assign('customDataType', 'Campaign'); | |
189 | $this->assign('entityID', $this->_campaignId); | |
190 | $this->assign('customDataSubType', CRM_Utils_Array::value('campaign_type_id', $this->_values)); | |
191 | ||
192 | $attributes = CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Campaign'); | |
193 | ||
194 | // add comaign title. | |
195 | $this->add('text', 'title', ts('Title'), $attributes['title'], TRUE); | |
196 | ||
197 | // add description | |
198 | $this->add('textarea', 'description', ts('Description'), $attributes['description']); | |
199 | ||
200 | // add campaign start date | |
c38a7f6b | 201 | $this->add('datepicker', 'start_date', ts('Start Date'), [], TRUE); |
6a488035 TO |
202 | |
203 | // add campaign end date | |
c38a7f6b | 204 | $this->add('datepicker', 'end_date', ts('End Date')); |
6a488035 TO |
205 | |
206 | // add campaign type | |
be2fb01f | 207 | $this->addSelect('campaign_type_id', ['onChange' => "CRM.buildCustomData( 'Campaign', this.value );"], TRUE); |
6a488035 TO |
208 | |
209 | // add campaign status | |
a2015e41 | 210 | $this->addSelect('status_id'); |
6a488035 | 211 | |
7b99ead3 CW |
212 | // add External Identifier Element |
213 | $this->add('text', 'external_identifier', ts('External ID'), | |
6a488035 TO |
214 | CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Campaign', 'external_identifier'), FALSE |
215 | ); | |
216 | ||
217 | // add Campaign Parent Id | |
2221b3e3 | 218 | $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(CRM_Utils_Array::value('parent_id', $this->_values), $this->_campaignId); |
6a488035 | 219 | if (!empty($campaigns)) { |
2221b3e3 | 220 | $this->addElement('select', 'parent_id', ts('Parent ID'), |
be2fb01f CW |
221 | ['' => ts('- select Parent -')] + $campaigns, |
222 | ['class' => 'crm-select2'] | |
6a488035 TO |
223 | ); |
224 | } | |
2221b3e3 | 225 | $groups = CRM_Core_PseudoConstant::nestedGroup(); |
6a488035 | 226 | //get the campaign groups. |
11cac306 CW |
227 | $this->add('select', 'includeGroups', |
228 | ts('Include Group(s)'), | |
6a488035 | 229 | $groups, |
11cac306 | 230 | FALSE, |
be2fb01f | 231 | [ |
11cac306 CW |
232 | 'multiple' => TRUE, |
233 | 'class' => 'crm-select2 huge', | |
234 | 'placeholder' => ts('- none -'), | |
be2fb01f | 235 | ] |
6a488035 | 236 | ); |
6a488035 | 237 | |
be2fb01f CW |
238 | $this->add('wysiwyg', 'goal_general', ts('Campaign Goals'), ['rows' => 2, 'cols' => 40]); |
239 | $this->add('text', 'goal_revenue', ts('Revenue Goal'), ['size' => 8, 'maxlength' => 12]); | |
6a488035 | 240 | $this->addRule('goal_revenue', ts('Please enter a valid money value (e.g. %1).', |
be2fb01f | 241 | [1 => CRM_Utils_Money::format('99.99', ' ')] |
353ffa53 | 242 | ), 'money'); |
6a488035 TO |
243 | |
244 | // is this Campaign active | |
245 | $this->addElement('checkbox', 'is_active', ts('Is Active?')); | |
246 | ||
8dbd6052 CW |
247 | $buttons = [ |
248 | [ | |
249 | 'type' => 'upload', | |
250 | 'name' => ts('Save'), | |
251 | 'isDefault' => TRUE, | |
252 | ], | |
253 | ]; | |
254 | // Skip this button when adding a new campaign from an entityRef | |
255 | if (empty($_GET['snippet']) || empty($_GET['returnExtra'])) { | |
256 | $buttons[] = [ | |
257 | 'type' => 'upload', | |
258 | 'name' => ts('Save and New'), | |
259 | 'subName' => 'new', | |
260 | ]; | |
261 | } | |
262 | $buttons[] = [ | |
263 | 'type' => 'cancel', | |
264 | 'name' => ts('Cancel'), | |
265 | ]; | |
266 | ||
267 | $this->addButtons($buttons); | |
6a488035 TO |
268 | } |
269 | ||
270 | /** | |
dc195289 | 271 | * add the rules (mainly global rules) for form. |
6a488035 TO |
272 | * All local rules are added near the element |
273 | * | |
fd31fa4c EM |
274 | * @param $fields |
275 | * @param $files | |
276 | * @param $errors | |
277 | * | |
2221b3e3 | 278 | * @return bool|array |
6a488035 TO |
279 | * @see valid_date |
280 | */ | |
00be9182 | 281 | public static function formRule($fields, $files, $errors) { |
be2fb01f | 282 | $errors = []; |
6a488035 TO |
283 | |
284 | return empty($errors) ? TRUE : $errors; | |
285 | } | |
286 | ||
287 | /** | |
288 | * Form submission of new/edit campaign is processed. | |
6a488035 TO |
289 | */ |
290 | public function postProcess() { | |
291 | // store the submitted values in an array | |
6a488035 | 292 | |
484627ca SL |
293 | $session = CRM_Core_Session::singleton(); |
294 | $params = $this->controller->exportValues($this->_name); | |
1107b319 SL |
295 | // To properly save the DAO we need to ensure we don't have a blank id key passed through. |
296 | if (empty($params['id'])) { | |
297 | unset($params['id']); | |
298 | } | |
299 | if (!empty($params['id'])) { | |
6a488035 | 300 | if ($this->_action & CRM_Core_Action::DELETE) { |
1107b319 | 301 | CRM_Campaign_BAO_Campaign::del($params['id']); |
6a488035 TO |
302 | CRM_Core_Session::setStatus(ts('Campaign has been deleted.'), ts('Record Deleted'), 'success'); |
303 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=campaign')); | |
304 | return; | |
305 | } | |
1107b319 | 306 | $this->_campaignId = $params['id']; |
6a488035 TO |
307 | } |
308 | else { | |
309 | $params['created_id'] = $session->get('userID'); | |
310 | $params['created_date'] = date('YmdHis'); | |
311 | } | |
312 | // format params | |
6a488035 TO |
313 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
314 | $params['last_modified_id'] = $session->get('userID'); | |
315 | $params['last_modified_date'] = date('YmdHis'); | |
484627ca SL |
316 | $result = self::submit($params, $this); |
317 | if (!$result['is_error']) { | |
318 | CRM_Core_Session::setStatus(ts('Campaign %1 has been saved.', [1 => $result['values'][$result['id']]['title']]), ts('Saved'), 'success'); | |
319 | $session->pushUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=campaign')); | |
320 | $this->ajaxResponse['id'] = $result['id']; | |
321 | $this->ajaxResponse['label'] = $result['values'][$result['id']]['title']; | |
322 | } | |
323 | $buttonName = $this->controller->getButtonName(); | |
324 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
325 | CRM_Core_Session::setStatus(ts(' You can add another Campaign.'), '', 'info'); | |
326 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign/add', 'reset=1&action=add')); | |
327 | } | |
328 | else { | |
329 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=campaign')); | |
330 | } | |
331 | } | |
6a488035 | 332 | |
484627ca SL |
333 | public static function submit($params = [], $form) { |
334 | $groups = []; | |
1107b319 | 335 | if (!empty($params['includeGroups']) && is_array($params['includeGroups'])) { |
6a488035 TO |
336 | foreach ($params['includeGroups'] as $key => $id) { |
337 | if ($id) { | |
338 | $groups['include'][] = $id; | |
339 | } | |
340 | } | |
341 | } | |
342 | $params['groups'] = $groups; | |
343 | ||
344 | // delete previous includes/excludes, if campaign already existed | |
353ffa53 TO |
345 | $groupTableName = CRM_Contact_BAO_Group::getTableName(); |
346 | $dao = new CRM_Campaign_DAO_CampaignGroup(); | |
484627ca | 347 | $dao->campaign_id = $form->_campaignId; |
6a488035 TO |
348 | $dao->entity_table = $groupTableName; |
349 | $dao->find(); | |
350 | while ($dao->fetch()) { | |
351 | $dao->delete(); | |
352 | } | |
353 | ||
354 | //process custom data. | |
355 | $customFields = CRM_Core_BAO_CustomField::getFields('Campaign', FALSE, FALSE, | |
356 | CRM_Utils_Array::value('campaign_type_id', $params) | |
357 | ); | |
358 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
484627ca | 359 | $form->_campaignId, |
6a488035 TO |
360 | 'Campaign' |
361 | ); | |
1107b319 | 362 | |
484627ca SL |
363 | // dev/core#1067 Clean Money before passing onto BAO to do the create. |
364 | $params['goal_revenue'] = CRM_Utils_Rule::cleanMoney($params['goal_revenue']); | |
365 | $result = civicrm_api3('Campaign', 'create', $params); | |
366 | return $result; | |
6a488035 | 367 | } |
96025800 | 368 | |
6a488035 | 369 | } |