Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
07f8d162 | 35 | * This class provides the functionality for batch profile update for contributions. |
6a488035 TO |
36 | */ |
37 | class 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 |
1330f57a | 48 | * @var int |
6a488035 TO |
49 | */ |
50 | protected $_maxFields = 9; | |
51 | ||
52 | /** | |
100fef9d | 53 | * Variable to store redirect path |
1330f57a | 54 | * @var string |
6a488035 TO |
55 | */ |
56 | protected $_userContext; | |
57 | ||
58 | /** | |
fe482240 | 59 | * Build all the data structures needed to build the form. |
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. | |
be2fb01f | 66 | $readOnlyFields = array_merge(['sort_name' => ts('Name')], |
6a488035 TO |
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 | */ |
00be9182 | 84 | public function buildQuickForm() { |
6a488035 TO |
85 | $ufGroupId = $this->get('ufGroupId'); |
86 | ||
87 | if (!$ufGroupId) { | |
88 | CRM_Core_Error::fatal('ufGroupId is missing'); | |
89 | } | |
b581842f | 90 | $this->_title = ts('Update multiple contributions') . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId); |
6a488035 TO |
91 | CRM_Utils_System::setTitle($this->_title); |
92 | ||
93 | $this->addDefaultButtons(ts('Save')); | |
be2fb01f | 94 | $this->_fields = []; |
6a488035 TO |
95 | $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW); |
96 | ||
97 | // remove file type field and then limit fields | |
98 | $suppressFields = FALSE; | |
be2fb01f | 99 | $removehtmlTypes = ['File']; |
6a488035 TO |
100 | foreach ($this->_fields as $name => $field) { |
101 | if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name) && | |
102 | in_array($this->_fields[$name]['html_type'], $removehtmlTypes) | |
103 | ) { | |
104 | $suppressFields = TRUE; | |
105 | unset($this->_fields[$name]); | |
106 | } | |
107 | ||
108 | //fix to reduce size as we are using this field in grid | |
b581842f | 109 | if (is_array($field['attributes']) && !empty($this->_fields[$name]['attributes']['size']) && $this->_fields[$name]['attributes']['size'] > 19) { |
6a488035 TO |
110 | //shrink class to "form-text-medium" |
111 | $this->_fields[$name]['attributes']['size'] = 19; | |
112 | } | |
113 | } | |
114 | ||
115 | $this->_fields = array_slice($this->_fields, 0, $this->_maxFields); | |
116 | ||
be2fb01f | 117 | $this->addButtons([ |
1330f57a SL |
118 | [ |
119 | 'type' => 'submit', | |
120 | 'name' => ts('Update Contribution(s)'), | |
121 | 'isDefault' => TRUE, | |
122 | ], | |
123 | [ | |
124 | 'type' => 'cancel', | |
125 | 'name' => ts('Cancel'), | |
126 | ], | |
127 | ]); | |
6a488035 | 128 | |
6a488035 TO |
129 | $this->assign('profileTitle', $this->_title); |
130 | $this->assign('componentIds', $this->_contributionIds); | |
6a488035 TO |
131 | |
132 | //load all campaigns. | |
133 | if (array_key_exists('contribution_campaign_id', $this->_fields)) { | |
be2fb01f | 134 | $this->_componentCampaigns = []; |
6a488035 TO |
135 | CRM_Core_PseudoConstant::populate($this->_componentCampaigns, |
136 | 'CRM_Contribute_DAO_Contribution', | |
137 | TRUE, 'campaign_id', 'id', | |
138 | ' id IN (' . implode(' , ', array_values($this->_contributionIds)) . ' ) ' | |
139 | ); | |
140 | } | |
141 | ||
3d927ee7 | 142 | // It is possible to have fields that are required in CiviCRM not be required in the |
143 | // profile. Overriding that here. Perhaps a better approach would be to | |
144 | // make them required in the schema & read that up through getFields functionality. | |
be2fb01f | 145 | $requiredFields = ['receive_date']; |
3d927ee7 | 146 | |
6a488035 TO |
147 | //fix for CRM-2752 |
148 | $customFields = CRM_Core_BAO_CustomField::getFields('Contribution'); | |
149 | foreach ($this->_contributionIds as $contributionId) { | |
9e12d5ee | 150 | $typeId = CRM_Core_DAO::getFieldValue("CRM_Contribute_DAO_Contribution", $contributionId, 'financial_type_id'); |
6a488035 | 151 | foreach ($this->_fields as $name => $field) { |
be2fb01f | 152 | $entityColumnValue = []; |
6a488035 TO |
153 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) { |
154 | $customValue = CRM_Utils_Array::value($customFieldID, $customFields); | |
a7488080 | 155 | if (!empty($customValue['extends_entity_column_value'])) { |
6a488035 TO |
156 | $entityColumnValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
157 | $customValue['extends_entity_column_value'] | |
158 | ); | |
159 | } | |
160 | ||
a7488080 | 161 | if (!empty($entityColumnValue[$typeId]) || |
f43890a4 | 162 | CRM_Utils_System::isNull(CRM_Utils_Array::value($typeId, $entityColumnValue)) |
6a488035 TO |
163 | ) { |
164 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $contributionId); | |
165 | } | |
166 | } | |
167 | else { | |
168 | // handle non custom fields | |
3d927ee7 | 169 | if (in_array($field['name'], $requiredFields)) { |
170 | $field['is_required'] = TRUE; | |
171 | } | |
6a488035 TO |
172 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $contributionId); |
173 | } | |
174 | } | |
175 | } | |
176 | ||
177 | $this->assign('fields', $this->_fields); | |
178 | ||
179 | // don't set the status message when form is submitted. | |
180 | $buttonName = $this->controller->getButtonName('submit'); | |
181 | ||
182 | if ($suppressFields && $buttonName != '_qf_Batch_next') { | |
657c89d9 | 183 | 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'); |
6a488035 TO |
184 | } |
185 | ||
186 | $this->addDefaultButtons(ts('Update Contributions')); | |
187 | } | |
188 | ||
189 | /** | |
c490a46a | 190 | * Set default values for the form. |
6a488035 | 191 | */ |
00be9182 | 192 | public function setDefaultValues() { |
6a488035 TO |
193 | if (empty($this->_fields)) { |
194 | return; | |
195 | } | |
196 | ||
be2fb01f | 197 | $defaults = []; |
6a488035 | 198 | foreach ($this->_contributionIds as $contributionId) { |
6a488035 TO |
199 | CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $contributionId, 'Contribute'); |
200 | } | |
201 | ||
202 | return $defaults; | |
203 | } | |
204 | ||
205 | /** | |
fe482240 | 206 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
207 | */ |
208 | public function postProcess() { | |
209 | $params = $this->exportValues(); | |
3eb99314 | 210 | // @todo extract submit functions & |
211 | // extend CRM_Event_Form_Task_BatchTest::testSubmit with a data provider to test | |
212 | // handling of custom data, specifically checkbox fields. | |
6a488035 | 213 | if (isset($params['field'])) { |
3d927ee7 | 214 | foreach ($params['field'] as $contributionID => $value) { |
6a488035 | 215 | |
3d927ee7 | 216 | $value['id'] = $contributionID; |
a7488080 | 217 | if (!empty($value['financial_type'])) { |
6a488035 TO |
218 | $value['financial_type_id'] = $value['financial_type']; |
219 | } | |
220 | ||
be2fb01f | 221 | $value['options'] = [ |
3d927ee7 | 222 | 'reload' => 1, |
be2fb01f | 223 | ]; |
3d927ee7 | 224 | $contribution = civicrm_api3('Contribution', 'create', $value); |
225 | $contribution = $contribution['values'][$contributionID]; | |
6a488035 | 226 | |
3d927ee7 | 227 | // @todo add check as to whether the status is updated. |
a2cd6f6c | 228 | if (!empty($value['contribution_status_id'])) { |
3d927ee7 | 229 | // @todo - use completeorder api or make api call do this. |
230 | CRM_Contribute_BAO_Contribution::transitionComponentWithReturnMessage($contribution['id'], | |
a2cd6f6c | 231 | $value['contribution_status_id'], |
3d927ee7 | 232 | CRM_Utils_Array::value("field[{$contributionID}][contribution_status_id]", $this->_defaultValues), |
233 | $contribution['receive_date'] | |
a2cd6f6c | 234 | ); |
235 | } | |
6a488035 TO |
236 | } |
237 | CRM_Core_Session::setStatus(ts("Your updates have been saved."), ts('Saved'), 'success'); | |
238 | } | |
239 | else { | |
240 | CRM_Core_Session::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert'); | |
241 | } | |
242 | } | |
96025800 | 243 | |
6a488035 | 244 | } |