3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 * This class provides the functionality for batch profile update for contributions.
37 class CRM_Contribute_Form_Task_Batch
extends CRM_Contribute_Form_Task
{
40 * The title of the group
47 * Maximum profile fields that will be displayed
49 protected $_maxFields = 9;
52 * Variable to store redirect path
54 protected $_userContext;
57 * Build all the data structures needed to build the form.
59 public function preProcess() {
60 // initialize the task and row fields
63 //get the contact read only fields to display.
64 $readOnlyFields = array_merge(array('sort_name' => ts('Name')),
65 CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
66 'contact_autocomplete_options',
67 TRUE, NULL, FALSE, 'name', TRUE
70 //get the read only field data.
71 $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1);
72 $contactDetails = CRM_Contact_BAO_Contact_Utils
::contactDetails($this->_contributionIds
,
73 'CiviContribute', $returnProperties
75 $this->assign('contactDetails', $contactDetails);
76 $this->assign('readOnlyFields', $readOnlyFields);
80 * Build the form object.
82 public function buildQuickForm() {
83 $ufGroupId = $this->get('ufGroupId');
86 CRM_Core_Error
::fatal('ufGroupId is missing');
88 $this->_title
= ts('Batch Update for Contributions') . ' - ' . CRM_Core_BAO_UFGroup
::getTitle($ufGroupId);
89 CRM_Utils_System
::setTitle($this->_title
);
91 $this->addDefaultButtons(ts('Save'));
92 $this->_fields
= array();
93 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($ufGroupId, FALSE, CRM_Core_Action
::VIEW
);
95 // remove file type field and then limit fields
96 $suppressFields = FALSE;
97 $removehtmlTypes = array('File', 'Autocomplete-Select');
98 foreach ($this->_fields
as $name => $field) {
99 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name) &&
100 in_array($this->_fields
[$name]['html_type'], $removehtmlTypes)
102 $suppressFields = TRUE;
103 unset($this->_fields
[$name]);
106 //fix to reduce size as we are using this field in grid
107 if (is_array($field['attributes']) && $this->_fields
[$name]['attributes']['size'] > 19) {
108 //shrink class to "form-text-medium"
109 $this->_fields
[$name]['attributes']['size'] = 19;
113 $this->_fields
= array_slice($this->_fields
, 0, $this->_maxFields
);
115 $this->addButtons(array(
118 'name' => ts('Update Contribution(s)'),
123 'name' => ts('Cancel'),
128 $this->assign('profileTitle', $this->_title
);
129 $this->assign('componentIds', $this->_contributionIds
);
131 //load all campaigns.
132 if (array_key_exists('contribution_campaign_id', $this->_fields
)) {
133 $this->_componentCampaigns
= array();
134 CRM_Core_PseudoConstant
::populate($this->_componentCampaigns
,
135 'CRM_Contribute_DAO_Contribution',
136 TRUE, 'campaign_id', 'id',
137 ' id IN (' . implode(' , ', array_values($this->_contributionIds
)) . ' ) '
142 $customFields = CRM_Core_BAO_CustomField
::getFields('Contribution');
143 foreach ($this->_contributionIds
as $contributionId) {
144 $typeId = CRM_Core_DAO
::getFieldValue("CRM_Contribute_DAO_Contribution", $contributionId, 'financial_type_id');
145 foreach ($this->_fields
as $name => $field) {
146 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
147 $customValue = CRM_Utils_Array
::value($customFieldID, $customFields);
148 if (!empty($customValue['extends_entity_column_value'])) {
149 $entityColumnValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
150 $customValue['extends_entity_column_value']
154 if (!empty($entityColumnValue[$typeId]) ||
155 CRM_Utils_System
::isNull($entityColumnValue[$typeId])
157 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $contributionId);
161 // handle non custom fields
162 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $contributionId);
167 $this->assign('fields', $this->_fields
);
169 // don't set the status message when form is submitted.
170 $buttonName = $this->controller
->getButtonName('submit');
172 if ($suppressFields && $buttonName != '_qf_Batch_next') {
173 CRM_Core_Session
::setStatus(ts("File or Autocomplete-Select type field(s) in the selected profile are not supported for Batch Update."), ts('Unsupported Field Type'), 'error');
176 $this->addDefaultButtons(ts('Update Contributions'));
180 * Set default values for the form.
182 public function setDefaultValues() {
183 if (empty($this->_fields
)) {
188 foreach ($this->_contributionIds
as $contributionId) {
189 $details[$contributionId] = array();
190 CRM_Core_BAO_UFGroup
::setProfileDefaults(NULL, $this->_fields
, $defaults, FALSE, $contributionId, 'Contribute');
197 * Process the form after the input has been submitted and validated.
199 public function postProcess() {
200 $params = $this->exportValues();
207 if (isset($params['field'])) {
208 foreach ($params['field'] as $key => $value) {
210 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($value,
215 $ids['contribution'] = $key;
216 foreach ($dates as $val) {
217 if (isset($value[$val])) {
218 $value[$val] = CRM_Utils_Date
::processDate($value[$val]);
221 if (!empty($value['financial_type'])) {
222 $value['financial_type_id'] = $value['financial_type'];
225 if (!empty($value['payment_instrument'])) {
226 $value['payment_instrument_id'] = $value['payment_instrument'];
229 if (!empty($value['contribution_source'])) {
230 $value['source'] = $value['contribution_source'];
233 unset($value['financial_type']);
234 unset($value['contribution_source']);
235 $contribution = CRM_Contribute_BAO_Contribution
::add($value, $ids);
237 // add custom field values
238 if (!empty($value['custom']) &&
239 is_array($value['custom'])
241 CRM_Core_BAO_CustomValueTable
::store($value['custom'], 'civicrm_contribution', $contribution->id
);
244 CRM_Core_Session
::setStatus(ts("Your updates have been saved."), ts('Saved'), 'success');
247 CRM_Core_Session
::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert');