3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This class generates form components for processing a campaign.
37 class CRM_Campaign_Form_Campaign
extends CRM_Core_Form
{
61 * The id of the campaign we are proceessing
65 protected $_campaignId;
68 * Explicitly declare the entity api name.
70 public function getDefaultEntity() {
74 public function preProcess() {
75 if (!CRM_Campaign_BAO_Campaign
::accessCampaign()) {
76 CRM_Utils_System
::permissionDenied();
79 $this->_context
= CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
81 $this->assign('context', $this->_context
);
83 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this);
84 $this->_campaignId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
87 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
88 $title = ts('Edit Campaign');
90 if ($this->_action
& CRM_Core_Action
::DELETE
) {
91 $title = ts('Delete Campaign');
94 CRM_Utils_System
::setTitle($title);
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
);
102 $this->_values
= $this->get('values');
103 if (!is_array($this->_values
)) {
107 if (isset($this->_campaignId
) && $this->_campaignId
) {
108 $params = ['id' => $this->_campaignId
];
109 CRM_Campaign_BAO_Campaign
::retrieve($params, $this->_values
);
112 //lets use current object session.
113 $this->set('values', $this->_values
);
116 // when custom data is included in form.
117 if (!empty($_POST['hidden_custom'])) {
118 $campaignTypeId = empty($_POST['campaign_type_id']) ?
NULL : $_POST['campaign_type_id'];
119 $this->set('type', 'Campaign');
120 $this->set('subType', $campaignTypeId);
121 $this->set('entityId', $this->_campaignId
);
123 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $campaignTypeId, 1, 'Campaign', $this->_campaignId
);
124 CRM_Custom_Form_CustomData
::buildQuickForm($this);
125 CRM_Custom_Form_CustomData
::setDefaultValues($this);
130 * Set default values for the form. Note that in edit/view mode
131 * the default values are retrieved from the database
136 public function setDefaultValues() {
137 $defaults = $this->_values
;
139 if (empty($defaults['start_date'])) {
140 $defaults['start_date'] = date('Y-m-d H:i:s');
143 if (!isset($defaults['is_active'])) {
144 $defaults['is_active'] = 1;
147 if (!$this->_campaignId
) {
151 $dao = new CRM_Campaign_DAO_CampaignGroup();
153 $campaignGroups = [];
154 $dao->campaign_id
= $this->_campaignId
;
157 while ($dao->fetch()) {
158 $campaignGroups[$dao->entity_table
][$dao->group_type
][] = $dao->entity_id
;
161 if (!empty($campaignGroups)) {
162 $defaults['includeGroups'] = $campaignGroups['civicrm_group']['Include'];
167 public function buildQuickForm() {
168 if ($this->_action
& CRM_Core_Action
::DELETE
) {
173 'name' => ts('Delete'),
178 'name' => ts('Cancel'),
184 $this->applyFilter('__ALL__', 'trim');
186 //lets assign custom data type and subtype.
187 $this->assign('customDataType', 'Campaign');
188 $this->assign('entityID', $this->_campaignId
);
189 $this->assign('customDataSubType', CRM_Utils_Array
::value('campaign_type_id', $this->_values
));
191 $attributes = CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Campaign');
193 // add comaign title.
194 $this->add('text', 'title', ts('Title'), $attributes['title'], TRUE);
197 $this->add('textarea', 'description', ts('Description'), $attributes['description']);
199 // add campaign start date
200 $this->add('datepicker', 'start_date', ts('Start Date'), [], TRUE);
202 // add campaign end date
203 $this->add('datepicker', 'end_date', ts('End Date'));
206 $this->addSelect('campaign_type_id', ['onChange' => "CRM.buildCustomData( 'Campaign', this.value );"], TRUE);
208 // add campaign status
209 $this->addSelect('status_id');
211 // add External Identifier Element
212 $this->add('text', 'external_identifier', ts('External ID'),
213 CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Campaign', 'external_identifier'), FALSE
216 // add Campaign Parent Id
217 $campaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(CRM_Utils_Array
::value('parent_id', $this->_values
), $this->_campaignId
);
218 if (!empty($campaigns)) {
219 $this->addElement('select', 'parent_id', ts('Parent ID'),
220 ['' => ts('- select Parent -')] +
$campaigns,
221 ['class' => 'crm-select2']
224 $groups = CRM_Core_PseudoConstant
::nestedGroup();
225 //get the campaign groups.
226 $this->add('select', 'includeGroups',
227 ts('Include Group(s)'),
232 'class' => 'crm-select2 huge',
233 'placeholder' => ts('- none -'),
237 $this->add('wysiwyg', 'goal_general', ts('Campaign Goals'), ['rows' => 2, 'cols' => 40]);
238 $this->add('text', 'goal_revenue', ts('Revenue Goal'), ['size' => 8, 'maxlength' => 12]);
239 $this->addRule('goal_revenue', ts('Please enter a valid money value (e.g. %1).',
240 [1 => CRM_Utils_Money
::format('99.99', ' ')]
243 // is this Campaign active
244 $this->addElement('checkbox', 'is_active', ts('Is Active?'));
249 'name' => ts('Save'),
253 // Skip this button when adding a new campaign from an entityRef
254 if (empty($_GET['snippet']) ||
empty($_GET['returnExtra'])) {
257 'name' => ts('Save and New'),
263 'name' => ts('Cancel'),
266 $this->addButtons($buttons);
270 * add the rules (mainly global rules) for form.
271 * All local rules are added near the element
280 public static function formRule($fields, $files, $errors) {
283 return empty($errors) ?
TRUE : $errors;
287 * Form submission of new/edit campaign is processed.
289 public function postProcess() {
290 // store the submitted values in an array
291 $params = $this->controller
->exportValues($this->_name
);
292 $session = CRM_Core_Session
::singleton();
295 if (isset($this->_campaignId
)) {
296 if ($this->_action
& CRM_Core_Action
::DELETE
) {
297 CRM_Campaign_BAO_Campaign
::del($this->_campaignId
);
298 CRM_Core_Session
::setStatus(ts('Campaign has been deleted.'), ts('Record Deleted'), 'success');
299 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=campaign'));
302 $params['id'] = $this->_campaignId
;
305 $params['created_id'] = $session->get('userID');
306 $params['created_date'] = date('YmdHis');
309 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
310 $params['last_modified_id'] = $session->get('userID');
311 $params['last_modified_date'] = date('YmdHis');
313 if (is_array($params['includeGroups'])) {
314 foreach ($params['includeGroups'] as $key => $id) {
316 $groups['include'][] = $id;
320 $params['groups'] = $groups;
322 // delete previous includes/excludes, if campaign already existed
323 $groupTableName = CRM_Contact_BAO_Group
::getTableName();
324 $dao = new CRM_Campaign_DAO_CampaignGroup();
325 $dao->campaign_id
= $this->_campaignId
;
326 $dao->entity_table
= $groupTableName;
328 while ($dao->fetch()) {
332 //process custom data.
333 $customFields = CRM_Core_BAO_CustomField
::getFields('Campaign', FALSE, FALSE,
334 CRM_Utils_Array
::value('campaign_type_id', $params)
336 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
341 $result = CRM_Campaign_BAO_Campaign
::create($params);
344 CRM_Core_Session
::setStatus(ts('Campaign %1 has been saved.', [1 => $result->title
]), ts('Saved'), 'success');
345 $session->pushUserContext(CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=campaign'));
346 $this->ajaxResponse
['id'] = $result->id
;
347 $this->ajaxResponse
['label'] = $result->title
;
350 $buttonName = $this->controller
->getButtonName();
351 if ($buttonName == $this->getButtonName('upload', 'new')) {
352 CRM_Core_Session
::setStatus(ts(' You can add another Campaign.'), '', 'info');
353 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/campaign/add', 'reset=1&action=add'));
356 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=campaign'));