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