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