Merge pull request #11821 from michaelmcandrew/500-http-response-code
[civicrm-core.git] / CRM / Contribute / Form / Task / Batch.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
32 */
33
34 /**
35 * This class provides the functionality for batch profile update for contributions.
36 */
37 class CRM_Contribute_Form_Task_Batch extends CRM_Contribute_Form_Task {
38
39 /**
40 * The title of the group
41 *
42 * @var string
43 */
44 protected $_title;
45
46 /**
47 * Maximum profile fields that will be displayed
48 */
49 protected $_maxFields = 9;
50
51 /**
52 * Variable to store redirect path
53 */
54 protected $_userContext;
55
56 /**
57 * Build all the data structures needed to build the form.
58 */
59 public function preProcess() {
60 // initialize the task and row fields
61 parent::preProcess();
62
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
68 )
69 );
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
74 );
75 $this->assign('contactDetails', $contactDetails);
76 $this->assign('readOnlyFields', $readOnlyFields);
77 }
78
79 /**
80 * Build the form object.
81 */
82 public function buildQuickForm() {
83 $ufGroupId = $this->get('ufGroupId');
84
85 if (!$ufGroupId) {
86 CRM_Core_Error::fatal('ufGroupId is missing');
87 }
88 $this->_title = ts('Update multiple contributions') . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId);
89 CRM_Utils_System::setTitle($this->_title);
90
91 $this->addDefaultButtons(ts('Save'));
92 $this->_fields = array();
93 $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW);
94
95 // remove file type field and then limit fields
96 $suppressFields = FALSE;
97 $removehtmlTypes = array('File');
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)
101 ) {
102 $suppressFields = TRUE;
103 unset($this->_fields[$name]);
104 }
105
106 //fix to reduce size as we are using this field in grid
107 if (is_array($field['attributes']) && !empty($this->_fields[$name]['attributes']['size']) && $this->_fields[$name]['attributes']['size'] > 19) {
108 //shrink class to "form-text-medium"
109 $this->_fields[$name]['attributes']['size'] = 19;
110 }
111 }
112
113 $this->_fields = array_slice($this->_fields, 0, $this->_maxFields);
114
115 $this->addButtons(array(
116 array(
117 'type' => 'submit',
118 'name' => ts('Update Contribution(s)'),
119 'isDefault' => TRUE,
120 ),
121 array(
122 'type' => 'cancel',
123 'name' => ts('Cancel'),
124 ),
125 )
126 );
127
128 $this->assign('profileTitle', $this->_title);
129 $this->assign('componentIds', $this->_contributionIds);
130
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)) . ' ) '
138 );
139 }
140
141 // It is possible to have fields that are required in CiviCRM not be required in the
142 // profile. Overriding that here. Perhaps a better approach would be to
143 // make them required in the schema & read that up through getFields functionality.
144 $requiredFields = array('receive_date');
145
146 //fix for CRM-2752
147 $customFields = CRM_Core_BAO_CustomField::getFields('Contribution');
148 foreach ($this->_contributionIds as $contributionId) {
149 $typeId = CRM_Core_DAO::getFieldValue("CRM_Contribute_DAO_Contribution", $contributionId, 'financial_type_id');
150 foreach ($this->_fields as $name => $field) {
151 $entityColumnValue = array();
152 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
153 $customValue = CRM_Utils_Array::value($customFieldID, $customFields);
154 if (!empty($customValue['extends_entity_column_value'])) {
155 $entityColumnValue = explode(CRM_Core_DAO::VALUE_SEPARATOR,
156 $customValue['extends_entity_column_value']
157 );
158 }
159
160 if (!empty($entityColumnValue[$typeId]) ||
161 CRM_Utils_System::isNull(CRM_Utils_Array::value($typeId, $entityColumnValue))
162 ) {
163 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $contributionId);
164 }
165 }
166 else {
167 // handle non custom fields
168 if (in_array($field['name'], $requiredFields)) {
169 $field['is_required'] = TRUE;
170 }
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 type field(s) in the selected profile are not supported for Update multiple contributions."), 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 public function setDefaultValues() {
192 if (empty($this->_fields)) {
193 return;
194 }
195
196 $defaults = array();
197 foreach ($this->_contributionIds as $contributionId) {
198 CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $contributionId, 'Contribute');
199 }
200
201 return $defaults;
202 }
203
204 /**
205 * Process the form after the input has been submitted and validated.
206 */
207 public function postProcess() {
208 $params = $this->exportValues();
209 // @todo extract submit functions &
210 // extend CRM_Event_Form_Task_BatchTest::testSubmit with a data provider to test
211 // handling of custom data, specifically checkbox fields.
212 if (isset($params['field'])) {
213 foreach ($params['field'] as $contributionID => $value) {
214
215 $value['id'] = $contributionID;
216 if (!empty($value['financial_type'])) {
217 $value['financial_type_id'] = $value['financial_type'];
218 }
219
220 $value['options'] = array(
221 'reload' => 1,
222 );
223 $contribution = civicrm_api3('Contribution', 'create', $value);
224 $contribution = $contribution['values'][$contributionID];
225
226 // @todo add check as to whether the status is updated.
227 if (!empty($value['contribution_status_id'])) {
228 // @todo - use completeorder api or make api call do this.
229 CRM_Contribute_BAO_Contribution::transitionComponentWithReturnMessage($contribution['id'],
230 $value['contribution_status_id'],
231 CRM_Utils_Array::value("field[{$contributionID}][contribution_status_id]", $this->_defaultValues),
232 $contribution['receive_date']
233 );
234 }
235 }
236 CRM_Core_Session::setStatus(ts("Your updates have been saved."), ts('Saved'), 'success');
237 }
238 else {
239 CRM_Core_Session::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert');
240 }
241 }
242
243 }