3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for processing a campaign.
21 class CRM_Campaign_Form_Campaign
extends CRM_Core_Form
{
45 * The id of the campaign we are proceessing
49 protected $_campaignId;
52 * Explicitly declare the entity api name.
54 public function getDefaultEntity() {
58 public function preProcess() {
59 if (!CRM_Campaign_BAO_Campaign
::accessCampaign()) {
60 CRM_Utils_System
::permissionDenied();
63 $this->_context
= CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
65 $this->assign('context', $this->_context
);
67 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this);
68 $this->_campaignId
= CRM_Utils_Request
::retrieve('id', 'Positive');
71 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
72 $title = ts('Edit Campaign');
74 if ($this->_action
& CRM_Core_Action
::DELETE
) {
75 $title = ts('Delete Campaign');
78 CRM_Utils_System
::setTitle($title);
81 $session = CRM_Core_Session
::singleton();
82 $session->pushUserContext(CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=campaign'));
83 $this->assign('action', $this->_action
);
86 $this->_values
= $this->get('values');
87 if (!is_array($this->_values
)) {
91 if (isset($this->_campaignId
) && $this->_campaignId
) {
92 $params = ['id' => $this->_campaignId
];
93 CRM_Campaign_BAO_Campaign
::retrieve($params, $this->_values
);
96 //lets use current object session.
97 $this->set('values', $this->_values
);
100 // when custom data is included in form.
101 if (!empty($_POST['hidden_custom'])) {
102 $campaignTypeId = empty($_POST['campaign_type_id']) ?
NULL : $_POST['campaign_type_id'];
103 $this->set('type', 'Campaign');
104 $this->set('subType', $campaignTypeId);
105 $this->set('entityId', $this->_campaignId
);
107 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $campaignTypeId, 1, 'Campaign', $this->_campaignId
);
108 CRM_Custom_Form_CustomData
::buildQuickForm($this);
109 CRM_Custom_Form_CustomData
::setDefaultValues($this);
114 * Set default values for the form. Note that in edit/view mode
115 * the default values are retrieved from the database
120 public function setDefaultValues() {
121 $defaults = $this->_values
;
123 if (empty($defaults['start_date'])) {
124 $defaults['start_date'] = date('Y-m-d H:i:s');
127 if (!isset($defaults['is_active'])) {
128 $defaults['is_active'] = 1;
131 if (!$this->_campaignId
) {
135 $dao = new CRM_Campaign_DAO_CampaignGroup();
137 $campaignGroups = [];
138 $dao->campaign_id
= $this->_campaignId
;
141 while ($dao->fetch()) {
142 $campaignGroups[$dao->entity_table
][$dao->group_type
][] = $dao->entity_id
;
145 if (!empty($campaignGroups)) {
146 $defaults['includeGroups'] = $campaignGroups['civicrm_group']['Include'];
151 public function buildQuickForm() {
152 $this->add('hidden', 'id', $this->_campaignId
);
153 if ($this->_action
& CRM_Core_Action
::DELETE
) {
158 'name' => ts('Delete'),
163 'name' => ts('Cancel'),
169 $this->applyFilter('__ALL__', 'trim');
171 //lets assign custom data type and subtype.
172 $this->assign('customDataType', 'Campaign');
173 $this->assign('entityID', $this->_campaignId
);
174 $this->assign('customDataSubType', CRM_Utils_Array
::value('campaign_type_id', $this->_values
));
176 $attributes = CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Campaign');
178 // add comaign title.
179 $this->add('text', 'title', ts('Title'), $attributes['title'], TRUE);
182 $this->add('textarea', 'description', ts('Description'), $attributes['description']);
184 // add campaign start date
185 $this->add('datepicker', 'start_date', ts('Start Date'), [], TRUE);
187 // add campaign end date
188 $this->add('datepicker', 'end_date', ts('End Date'));
191 $this->addSelect('campaign_type_id', ['onChange' => "CRM.buildCustomData( 'Campaign', this.value );"], TRUE);
193 // add campaign status
194 $this->addSelect('status_id');
196 // add External Identifier Element
197 $this->add('text', 'external_identifier', ts('External ID'),
198 CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Campaign', 'external_identifier'), FALSE
201 // add Campaign Parent Id
202 $campaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(CRM_Utils_Array
::value('parent_id', $this->_values
), $this->_campaignId
);
203 if (!empty($campaigns)) {
204 $this->addElement('select', 'parent_id', ts('Parent ID'),
205 ['' => ts('- select Parent -')] +
$campaigns,
206 ['class' => 'crm-select2']
209 $groups = CRM_Core_PseudoConstant
::nestedGroup();
210 //get the campaign groups.
211 $this->add('select', 'includeGroups',
212 ts('Include Group(s)'),
217 'class' => 'crm-select2 huge',
218 'placeholder' => ts('- none -'),
222 $this->add('wysiwyg', 'goal_general', ts('Campaign Goals'), ['rows' => 2, 'cols' => 40]);
223 $this->add('text', 'goal_revenue', ts('Revenue Goal'), ['size' => 8, 'maxlength' => 12]);
224 $this->addRule('goal_revenue', ts('Please enter a valid money value (e.g. %1).',
225 [1 => CRM_Utils_Money
::format('99.99', ' ')]
228 // is this Campaign active
229 $this->addElement('checkbox', 'is_active', ts('Is Active?'));
234 'name' => ts('Save'),
238 // Skip this button when adding a new campaign from an entityRef
239 if (empty($_GET['snippet']) ||
empty($_GET['returnExtra'])) {
242 'name' => ts('Save and New'),
248 'name' => ts('Cancel'),
251 $this->addButtons($buttons);
255 * add the rules (mainly global rules) for form.
256 * All local rules are added near the element
265 public static function formRule($fields, $files, $errors) {
268 return empty($errors) ?
TRUE : $errors;
272 * Form submission of new/edit campaign is processed.
274 public function postProcess() {
275 // store the submitted values in an array
277 $session = CRM_Core_Session
::singleton();
278 $params = $this->controller
->exportValues($this->_name
);
279 // To properly save the DAO we need to ensure we don't have a blank id key passed through.
280 if (empty($params['id'])) {
281 unset($params['id']);
283 if (!empty($params['id'])) {
284 if ($this->_action
& CRM_Core_Action
::DELETE
) {
285 CRM_Campaign_BAO_Campaign
::del($params['id']);
286 CRM_Core_Session
::setStatus(ts('Campaign has been deleted.'), ts('Record Deleted'), 'success');
287 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=campaign'));
290 $this->_campaignId
= $params['id'];
293 $params['created_id'] = $session->get('userID');
294 $params['created_date'] = date('YmdHis');
297 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
298 $params['last_modified_id'] = $session->get('userID');
299 $params['last_modified_date'] = date('YmdHis');
300 $result = self
::submit($params, $this);
301 if (!$result['is_error']) {
302 CRM_Core_Session
::setStatus(ts('Campaign %1 has been saved.', [1 => $result['values'][$result['id']]['title']]), ts('Saved'), 'success');
303 $session->pushUserContext(CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=campaign'));
304 $this->ajaxResponse
['id'] = $result['id'];
305 $this->ajaxResponse
['label'] = $result['values'][$result['id']]['title'];
307 $buttonName = $this->controller
->getButtonName();
308 if ($buttonName == $this->getButtonName('upload', 'new')) {
309 CRM_Core_Session
::setStatus(ts(' You can add another Campaign.'), '', 'info');
310 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/campaign/add', 'reset=1&action=add'));
313 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=campaign'));
317 public static function submit($params = [], $form) {
319 if (!empty($params['includeGroups']) && is_array($params['includeGroups'])) {
320 foreach ($params['includeGroups'] as $key => $id) {
322 $groups['include'][] = $id;
326 $params['groups'] = $groups;
328 // delete previous includes/excludes, if campaign already existed
329 $groupTableName = CRM_Contact_BAO_Group
::getTableName();
330 $dao = new CRM_Campaign_DAO_CampaignGroup();
331 $dao->campaign_id
= $form->_campaignId
;
332 $dao->entity_table
= $groupTableName;
334 while ($dao->fetch()) {
338 //process custom data.
339 $customFields = CRM_Core_BAO_CustomField
::getFields('Campaign', FALSE, FALSE,
340 CRM_Utils_Array
::value('campaign_type_id', $params)
342 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
347 // dev/core#1067 Clean Money before passing onto BAO to do the create.
348 $params['goal_revenue'] = CRM_Utils_Rule
::cleanMoney($params['goal_revenue']);
349 $result = civicrm_api3('Campaign', 'create', $params);