3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 * This class provides the functionality for batch profile update for contributions.
37 class CRM_Contribute_Form_Task_Batch
extends CRM_Contribute_Form_Task
{
40 * The title of the group
47 * Maximum profile fields that will be displayed
49 protected $_maxFields = 9;
52 * Variable to store redirect path
54 protected $_userContext;
57 * Build all the data structures needed to build the form.
59 public function preProcess() {
60 // initialize the task and row fields
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
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
75 $this->assign('contactDetails', $contactDetails);
76 $this->assign('readOnlyFields', $readOnlyFields);
80 * Build the form object.
82 public function buildQuickForm() {
83 $ufGroupId = $this->get('ufGroupId');
86 CRM_Core_Error
::fatal('ufGroupId is missing');
88 $this->_title
= ts('Update multiple contributions') . ' - ' . CRM_Core_BAO_UFGroup
::getTitle($ufGroupId);
89 CRM_Utils_System
::setTitle($this->_title
);
91 $this->addDefaultButtons(ts('Save'));
92 $this->_fields
= array();
93 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($ufGroupId, FALSE, CRM_Core_Action
::VIEW
);
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)
102 $suppressFields = TRUE;
103 unset($this->_fields
[$name]);
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;
113 $this->_fields
= array_slice($this->_fields
, 0, $this->_maxFields
);
115 $this->addButtons(array(
118 'name' => ts('Update Contribution(s)'),
123 'name' => ts('Cancel'),
128 $this->assign('profileTitle', $this->_title
);
129 $this->assign('componentIds', $this->_contributionIds
);
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
)) . ' ) '
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');
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']
160 if (!empty($entityColumnValue[$typeId]) ||
161 CRM_Utils_System
::isNull(CRM_Utils_Array
::value($typeId, $entityColumnValue))
163 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $contributionId);
167 // handle non custom fields
168 if (in_array($field['name'], $requiredFields)) {
169 $field['is_required'] = TRUE;
171 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $contributionId);
176 $this->assign('fields', $this->_fields
);
178 // don't set the status message when form is submitted.
179 $buttonName = $this->controller
->getButtonName('submit');
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');
185 $this->addDefaultButtons(ts('Update Contributions'));
189 * Set default values for the form.
191 public function setDefaultValues() {
192 if (empty($this->_fields
)) {
197 foreach ($this->_contributionIds
as $contributionId) {
198 CRM_Core_BAO_UFGroup
::setProfileDefaults(NULL, $this->_fields
, $defaults, FALSE, $contributionId, 'Contribute');
205 * Process the form after the input has been submitted and validated.
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) {
215 $value['id'] = $contributionID;
216 if (!empty($value['financial_type'])) {
217 $value['financial_type_id'] = $value['financial_type'];
220 $value['options'] = array(
223 $contribution = civicrm_api3('Contribution', 'create', $value);
224 $contribution = $contribution['values'][$contributionID];
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']
236 CRM_Core_Session
::setStatus(ts("Your updates have been saved."), ts('Saved'), 'success');
239 CRM_Core_Session
::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert');