3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * This class generates form components for processing a campaign
40 class CRM_Campaign_Form_Campaign
extends CRM_Core_Form
{
64 * the id of the campaign we are proceessing
69 protected $_campaignId;
71 public function preProcess() {
72 if (!CRM_Campaign_BAO_Campaign
::accessCampaign()) {
73 CRM_Utils_System
::permissionDenied();
76 //check for custom data type.
77 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
78 $this->assign('cdType', FALSE);
80 $this->assign('cdType', TRUE);
81 return CRM_Custom_Form_CustomData
::preProcess($this);
84 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
86 $this->assign('context', $this->_context
);
88 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this);
89 $this->_campaignId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
92 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
93 $title = ts('Edit Campaign');
95 if ($this->_action
& CRM_Core_Action
::DELETE
) {
96 $title = ts('Delete Campaign');
99 CRM_Utils_System
::setTitle($title);
102 $session = CRM_Core_Session
::singleton();
103 $session->pushUserContext(CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=campaign'));
104 $this->assign('action', $this->_action
);
107 $this->_values
= $this->get('values');
108 if (!is_array($this->_values
)) {
109 $this->_values
= array();
112 if (isset($this->_campaignId
) && $this->_campaignId
) {
113 $params = array('id' => $this->_campaignId
);
114 CRM_Campaign_BAO_Campaign
::retrieve($params, $this->_values
);
117 //lets use current object session.
118 $this->set('values', $this->_values
);
121 // when custom data is included in form.
122 if (!empty($_POST['hidden_custom'])) {
123 $this->set('type', 'Campaign');
124 $this->set('subType', CRM_Utils_Array
::value('campaign_type_id', $_POST));
125 $this->set('entityId', $this->_campaignId
);
127 CRM_Custom_Form_CustomData
::preProcess($this);
128 CRM_Custom_Form_CustomData
::buildQuickForm($this);
129 CRM_Custom_Form_CustomData
::setDefaultValues($this);
134 * This function sets the default values for the form. Note that in edit/view mode
135 * the default values are retrieved from the database
141 function setDefaultValues() {
142 $defaults = $this->_values
;
144 //load only custom data defaults.
145 if ($this->_cdType
) {
146 return CRM_Custom_Form_CustomData
::setDefaultValues($this);
149 if (isset($defaults['start_date'])) {
150 list($defaults['start_date'],
151 $defaults['start_date_time']
152 ) = CRM_Utils_Date
::setDateDefaults($defaults['start_date'],
157 list($defaults['start_date'],
158 $defaults['start_date_time']
159 ) = CRM_Utils_Date
::setDateDefaults();
162 if (isset($defaults['end_date'])) {
163 list($defaults['end_date'],
164 $defaults['end_date_time']
165 ) = CRM_Utils_Date
::setDateDefaults($defaults['end_date'],
170 if (!isset($defaults['is_active'])) {
171 $defaults['is_active'] = 1;
174 if (!$this->_campaignId
) {
178 $dao = new CRM_Campaign_DAO_CampaignGroup();
180 $campaignGroups = array();
181 $dao->campaign_id
= $this->_campaignId
;
184 while ($dao->fetch()) {
185 $campaignGroups[$dao->entity_table
][$dao->group_type
][] = $dao->entity_id
;
188 if (!empty($campaignGroups)) {
189 $defaults['includeGroups'] = $campaignGroups['civicrm_group']['Include'];
194 public function buildQuickForm() {
195 if ($this->_action
& CRM_Core_Action
::DELETE
) {
197 $this->addButtons(array(
200 'name' => ts('Delete'),
205 'name' => ts('Cancel'),
212 $this->applyFilter('__ALL__', 'trim');
214 if ($this->_cdType
) {
215 return CRM_Custom_Form_CustomData
::buildQuickForm($this);
218 //lets assign custom data type and subtype.
219 $this->assign('customDataType', 'Campaign');
220 $this->assign('entityID', $this->_campaignId
);
221 $this->assign('customDataSubType', CRM_Utils_Array
::value('campaign_type_id', $this->_values
));
223 $attributes = CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Campaign');
225 // add comaign title.
226 $this->add('text', 'title', ts('Title'), $attributes['title'], TRUE);
229 $this->add('textarea', 'description', ts('Description'), $attributes['description']);
231 // add campaign start date
232 $this->addDateTime('start_date', ts('Start Date'), TRUE, array('formatType' => 'activityDateTime'));
234 // add campaign end date
235 $this->addDateTime('end_date', ts('End Date'), FALSE, array('formatType' => 'activityDateTime'));
238 $this->addSelect('campaign_type_id', array('onChange' => "CRM.buildCustomData( 'Campaign', this.value );"), TRUE);
240 // add campaign status
241 $this->addSelect('status_id');
243 // add External Identifire Element
244 $this->add('text', 'external_identifier', ts('External Id'),
245 CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Campaign', 'external_identifier'), FALSE
248 // add Campaign Parent Id
249 $campaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(CRM_Utils_Array
::value('parent_id', $this->_values
),
252 if (!empty($campaigns)) {
253 $this->addElement('select', 'parent_id', ts('Parent Id'),
255 '' => ts('- select Parent -')) +
$campaigns
259 //get the campaign groups.
260 $groups = CRM_Core_PseudoConstant
::group();
262 $inG = &$this->addElement('advmultiselect', 'includeGroups',
263 ts('Include Group(s)') . ' ',
267 'style' => 'width:240px',
268 'class' => 'advmultiselect',
271 $inG->setButtonAttributes('add', array('value' => ts('Add >>')));
272 $inG->setButtonAttributes('remove', array('value' => ts('<< Remove')));
274 $this->addWysiwyg('goal_general', ts('Campaign Goals'), array('rows' => 2, 'cols' => 40));
275 $this->add('text', 'goal_revenue', ts('Revenue Goal'), array('size' => 8, 'maxlength' => 12));
276 $this->addRule('goal_revenue', ts('Please enter a valid money value (e.g. %1).',
277 array(1 => CRM_Utils_Money
::format('99.99', ' '))
280 // is this Campaign active
281 $this->addElement('checkbox', 'is_active', ts('Is Active?'));
283 $this->addButtons(array(
286 'name' => ts('Save'),
291 'name' => ts('Save and New'),
296 'name' => ts('Cancel'),
303 * This function is used to add the rules (mainly global rules) for form.
304 * All local rules are added near the element
310 static function formRule($fields, $files, $errors) {
313 return empty($errors) ?
TRUE : $errors;
317 * Form submission of new/edit campaign is processed.
323 public function postProcess() {
324 // store the submitted values in an array
325 $params = $this->controller
->exportValues($this->_name
);
326 $session = CRM_Core_Session
::singleton();
329 if (isset($this->_campaignId
)) {
330 if ($this->_action
& CRM_Core_Action
::DELETE
) {
331 CRM_Campaign_BAO_Campaign
::del($this->_campaignId
);
332 CRM_Core_Session
::setStatus(ts('Campaign has been deleted.'), ts('Record Deleted'), 'success');
333 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=campaign'));
336 $params['id'] = $this->_campaignId
;
339 $params['created_id'] = $session->get('userID');
340 $params['created_date'] = date('YmdHis');
343 $params['start_date'] = CRM_Utils_Date
::processDate($params['start_date'], $params['start_date_time']);
344 $params['end_date'] = CRM_Utils_Date
::processDate($params['end_date'], $params['end_date_time'], TRUE);
345 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
346 $params['last_modified_id'] = $session->get('userID');
347 $params['last_modified_date'] = date('YmdHis');
349 if (is_array($params['includeGroups'])) {
350 foreach ($params['includeGroups'] as $key => $id) {
352 $groups['include'][] = $id;
356 $params['groups'] = $groups;
358 // delete previous includes/excludes, if campaign already existed
359 $groupTableName = CRM_Contact_BAO_Group
::getTableName();
360 $dao = new CRM_Campaign_DAO_CampaignGroup();
361 $dao->campaign_id
= $this->_campaignId
;
362 $dao->entity_table
= $groupTableName;
364 while ($dao->fetch()) {
368 //process custom data.
369 $customFields = CRM_Core_BAO_CustomField
::getFields('Campaign', FALSE, FALSE,
370 CRM_Utils_Array
::value('campaign_type_id', $params)
372 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
378 $result = CRM_Campaign_BAO_Campaign
::create($params);
381 CRM_Core_Session
::setStatus(ts('Campaign %1 has been saved.', array(1 => $result->title
)), ts('Saved'), 'success');
382 $session->pushUserContext(CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=campaign'));
385 $buttonName = $this->controller
->getButtonName();
386 if ($buttonName == $this->getButtonName('upload', 'new')) {
387 CRM_Core_Session
::setStatus(ts(' You can add another Campaign.'), '', 'info');
388 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/campaign/add', 'reset=1&action=add'));
391 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=campaign'));