Merge branch '4.6' into 'master'
[civicrm-core.git] / CRM / Contribute / Form / Task / Batch.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 */
33
34/**
07f8d162 35 * This class provides the functionality for batch profile update for contributions.
6a488035
TO
36 */
37class CRM_Contribute_Form_Task_Batch extends CRM_Contribute_Form_Task {
38
39 /**
100fef9d 40 * The title of the group
6a488035
TO
41 *
42 * @var string
43 */
44 protected $_title;
45
46 /**
100fef9d 47 * Maximum profile fields that will be displayed
6a488035
TO
48 */
49 protected $_maxFields = 9;
50
51 /**
100fef9d 52 * Variable to store redirect path
6a488035
TO
53 */
54 protected $_userContext;
55
56 /**
fe482240 57 * Build all the data structures needed to build the form.
6a488035
TO
58 *
59 * @return void
6a488035 60 */
00be9182 61 public function preProcess() {
d424ffde 62 // initialize the task and row fields
6a488035
TO
63 parent::preProcess();
64
65 //get the contact read only fields to display.
66 $readOnlyFields = array_merge(array('sort_name' => ts('Name')),
67 CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
68 'contact_autocomplete_options',
69 TRUE, NULL, FALSE, 'name', TRUE
70 )
71 );
72 //get the read only field data.
73 $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1);
74 $contactDetails = CRM_Contact_BAO_Contact_Utils::contactDetails($this->_contributionIds,
75 'CiviContribute', $returnProperties
76 );
77 $this->assign('contactDetails', $contactDetails);
78 $this->assign('readOnlyFields', $readOnlyFields);
79 }
80
81 /**
fe482240 82 * Build the form object.
6a488035 83 *
6a488035
TO
84 *
85 * @return void
86 */
00be9182 87 public function buildQuickForm() {
6a488035
TO
88 $ufGroupId = $this->get('ufGroupId');
89
90 if (!$ufGroupId) {
91 CRM_Core_Error::fatal('ufGroupId is missing');
92 }
93 $this->_title = ts('Batch Update for Contributions') . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId);
94 CRM_Utils_System::setTitle($this->_title);
95
96 $this->addDefaultButtons(ts('Save'));
97 $this->_fields = array();
98 $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW);
99
100 // remove file type field and then limit fields
101 $suppressFields = FALSE;
102 $removehtmlTypes = array('File', 'Autocomplete-Select');
103 foreach ($this->_fields as $name => $field) {
104 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name) &&
105 in_array($this->_fields[$name]['html_type'], $removehtmlTypes)
106 ) {
107 $suppressFields = TRUE;
108 unset($this->_fields[$name]);
109 }
110
111 //fix to reduce size as we are using this field in grid
112 if (is_array($field['attributes']) && $this->_fields[$name]['attributes']['size'] > 19) {
113 //shrink class to "form-text-medium"
114 $this->_fields[$name]['attributes']['size'] = 19;
115 }
116 }
117
118 $this->_fields = array_slice($this->_fields, 0, $this->_maxFields);
119
120 $this->addButtons(array(
121 array(
122 'type' => 'submit',
123 'name' => ts('Update Contribution(s)'),
124 'isDefault' => TRUE,
125 ),
126 array(
127 'type' => 'cancel',
128 'name' => ts('Cancel'),
129 ),
130 )
131 );
132
6a488035
TO
133 $this->assign('profileTitle', $this->_title);
134 $this->assign('componentIds', $this->_contributionIds);
6a488035
TO
135
136 //load all campaigns.
137 if (array_key_exists('contribution_campaign_id', $this->_fields)) {
138 $this->_componentCampaigns = array();
139 CRM_Core_PseudoConstant::populate($this->_componentCampaigns,
140 'CRM_Contribute_DAO_Contribution',
141 TRUE, 'campaign_id', 'id',
142 ' id IN (' . implode(' , ', array_values($this->_contributionIds)) . ' ) '
143 );
144 }
145
146 //fix for CRM-2752
147 $customFields = CRM_Core_BAO_CustomField::getFields('Contribution');
148 foreach ($this->_contributionIds as $contributionId) {
9e12d5ee 149 $typeId = CRM_Core_DAO::getFieldValue("CRM_Contribute_DAO_Contribution", $contributionId, 'financial_type_id');
6a488035
TO
150 foreach ($this->_fields as $name => $field) {
151 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
152 $customValue = CRM_Utils_Array::value($customFieldID, $customFields);
a7488080 153 if (!empty($customValue['extends_entity_column_value'])) {
6a488035
TO
154 $entityColumnValue = explode(CRM_Core_DAO::VALUE_SEPARATOR,
155 $customValue['extends_entity_column_value']
156 );
157 }
158
a7488080 159 if (!empty($entityColumnValue[$typeId]) ||
6a488035
TO
160 CRM_Utils_System::isNull($entityColumnValue[$typeId])
161 ) {
162 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $contributionId);
163 }
164 }
165 else {
166 // handle non custom fields
167 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $contributionId);
168 }
169 }
170 }
171
172 $this->assign('fields', $this->_fields);
173
174 // don't set the status message when form is submitted.
175 $buttonName = $this->controller->getButtonName('submit');
176
177 if ($suppressFields && $buttonName != '_qf_Batch_next') {
79dc94e4 178 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');
6a488035
TO
179 }
180
181 $this->addDefaultButtons(ts('Update Contributions'));
182 }
183
184 /**
c490a46a 185 * Set default values for the form.
6a488035 186 *
6a488035 187 *
355ba699 188 * @return void
6a488035 189 */
00be9182 190 public function setDefaultValues() {
6a488035
TO
191 if (empty($this->_fields)) {
192 return;
193 }
194
195 $defaults = array();
196 foreach ($this->_contributionIds as $contributionId) {
197 $details[$contributionId] = array();
198 CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $contributionId, 'Contribute');
199 }
200
201 return $defaults;
202 }
203
204 /**
fe482240 205 * Process the form after the input has been submitted and validated.
6a488035 206 *
6a488035 207 *
355ba699 208 * @return void
6a488035
TO
209 */
210 public function postProcess() {
211 $params = $this->exportValues();
212 $dates = array(
213 'receive_date',
214 'receipt_date',
215 'thankyou_date',
216 'cancel_date',
217 );
218 if (isset($params['field'])) {
219 foreach ($params['field'] as $key => $value) {
220
221 $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value,
6a488035
TO
222 $key,
223 'Contribution'
224 );
225
226 $ids['contribution'] = $key;
227 foreach ($dates as $val) {
228 if (isset($value[$val])) {
229 $value[$val] = CRM_Utils_Date::processDate($value[$val]);
230 }
231 }
a7488080 232 if (!empty($value['financial_type'])) {
6a488035
TO
233 $value['financial_type_id'] = $value['financial_type'];
234 }
235
a7488080 236 if (!empty($value['payment_instrument'])) {
6a488035
TO
237 $value['payment_instrument_id'] = $value['payment_instrument'];
238 }
239
a7488080 240 if (!empty($value['contribution_source'])) {
6a488035
TO
241 $value['source'] = $value['contribution_source'];
242 }
243
9e12d5ee 244 unset($value['financial_type']);
6a488035
TO
245 unset($value['contribution_source']);
246 $contribution = CRM_Contribute_BAO_Contribution::add($value, $ids);
247
248 // add custom field values
a7488080 249 if (!empty($value['custom']) &&
6a488035
TO
250 is_array($value['custom'])
251 ) {
252 CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_contribution', $contribution->id);
253 }
254 }
255 CRM_Core_Session::setStatus(ts("Your updates have been saved."), ts('Saved'), 'success');
256 }
257 else {
258 CRM_Core_Session::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert');
259 }
260 }
96025800 261
6a488035 262}