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