Merge pull request #14288 from MegaphoneJon/access-page-simplify
[civicrm-core.git] / CRM / Financial / Form / Export.php
CommitLineData
6a488035 1<?php
6a488035
TO
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/**
35 * This class provides the functionality to delete a group of
36 * contributions. This class provides functionality for the actual
37 * deletion.
38 */
39class CRM_Financial_Form_Export extends CRM_Core_Form {
40
41 /**
42 * The financial batch id, used when editing the field
43 *
44 * @var int
6a488035
TO
45 */
46 protected $_id;
47
48 /**
fe482240 49 * Financial batch ids.
7b966967 50 * @var array
6a488035 51 */
be2fb01f 52 protected $_batchIds = [];
6a488035
TO
53
54 /**
fe482240 55 * Export status id.
7b966967 56 * @var int
6a488035
TO
57 */
58 protected $_exportStatusId;
59
60 /**
fe482240 61 * Export format.
7b966967 62 * @var string
6a488035
TO
63 */
64 protected $_exportFormat;
65
f3d529fc
PN
66 /**
67 * Download export File.
7b966967 68 * @var bool
f3d529fc
PN
69 */
70 protected $_downloadFile = TRUE;
71
6a488035 72 /**
fe482240 73 * Build all the data structures needed to build the form.
6a488035 74 */
00be9182 75 public function preProcess() {
6a488035
TO
76 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
77
78 // this mean it's a batch action
481a74f4 79 if (!$this->_id) {
6a488035 80 if (!empty($_GET['batch_id'])) {
cded2ebf 81 // validate batch ids
6a488035 82 $batchIds = explode(',', $_GET['batch_id']);
22e263ad 83 foreach ($batchIds as $batchId) {
045f52a3 84 CRM_Utils_Type::validate($batchId, 'Positive');
6a488035
TO
85 }
86
87 $this->_batchIds = $_GET['batch_id'];
88 $this->set('batchIds', $this->_batchIds);
89 }
90 else {
91 $this->_batchIds = $this->get('batchIds');
92 }
be2fb01f 93 if (!empty($_GET['export_format']) && in_array($_GET['export_format'], ['IIF', 'CSV'])) {
6a488035
TO
94 $this->_exportFormat = $_GET['export_format'];
95 }
96 }
97 else {
98 $this->_batchIds = $this->_id;
99 }
100
4b2bcea5 101 $this->_exportStatusId = CRM_Core_PseudoConstant::getKey('CRM_Batch_DAO_Batch', 'status_id', 'Exported');
6a488035 102
cded2ebf 103 // check if batch status is valid, do not allow exported batches to export again
6a488035
TO
104 $batchStatus = CRM_Batch_BAO_Batch::getBatchStatuses($this->_batchIds);
105
481a74f4 106 foreach ($batchStatus as $batchStatusId) {
6a488035 107 if ($batchStatusId == $this->_exportStatusId) {
0efdabe7
MW
108 $url = CRM_Core_Session::singleton()->readUserContext();
109 CRM_Core_Error::statusBounce(ts('You cannot export batches which have already been exported.'), $url);
6a488035
TO
110 }
111 }
112
113 $session = CRM_Core_Session::singleton();
114 $session->replaceUserContext(CRM_Utils_System::url('civicrm/financial/financialbatches',
fd7abdce 115 "reset=1&batchStatus={$this->_exportStatusId}"));
6a488035 116 }
03e04002 117
6a488035 118 /**
fe482240 119 * Build the form object.
6a488035 120 */
00be9182 121 public function buildQuickForm() {
6a488035
TO
122 // this mean it's a batch action
123 if (!empty($this->_batchIds)) {
124 $batchNames = CRM_Batch_BAO_Batch::getBatchNames($this->_batchIds);
125 $this->assign('batchNames', $batchNames);
126 // Skip building the form if we already have batches and an export format
127 if ($this->_exportFormat) {
128 $this->postProcess();
129 }
130 }
131
be2fb01f 132 $optionTypes = [
6a488035
TO
133 'IIF' => ts('Export to IIF'),
134 'CSV' => ts('Export to CSV'),
be2fb01f 135 ];
6a488035
TO
136
137 $this->addRadio('export_format', NULL, $optionTypes, NULL, '<br/>', TRUE);
138
139 $this->addButtons(
be2fb01f
CW
140 [
141 [
6a488035
TO
142 'type' => 'next',
143 'name' => ts('Export Batch'),
144 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
145 'isDefault' => TRUE,
be2fb01f
CW
146 ],
147 [
6a488035
TO
148 'type' => 'cancel',
149 'name' => ts('Cancel'),
be2fb01f
CW
150 ],
151 ]
6a488035
TO
152 );
153 }
03e04002 154
6a488035 155 /**
fe482240 156 * Process the form after the input has been submitted and validated.
6a488035 157 */
045f52a3 158 public function postProcess() {
6a488035
TO
159 if (!$this->_exportFormat) {
160 $params = $this->exportValues();
161 $this->_exportFormat = $params['export_format'];
162 }
163
164 if ($this->_id) {
be2fb01f 165 $batchIds = [$this->_id];
6a488035 166 }
4c9b6178 167 elseif (!empty($this->_batchIds)) {
6a488035
TO
168 $batchIds = explode(',', $this->_batchIds);
169 }
170 // Recalculate totals
171 $totals = CRM_Batch_BAO_Batch::batchTotals($batchIds);
172
173 // build batch params
174 $session = CRM_Core_Session::singleton();
175 $batchParams['modified_date'] = date('YmdHis');
176 $batchParams['modified_id'] = $session->get('userID');
177 $batchParams['status_id'] = $this->_exportStatusId;
178
22e263ad 179 foreach ($batchIds as $batchId) {
92e088c9 180 $batchParams['id'] = $batchId;
6a488035
TO
181 // Update totals
182 $batchParams = array_merge($batchParams, $totals[$batchId]);
92e088c9 183 CRM_Batch_BAO_Batch::create($batchParams);
6a488035
TO
184 }
185
f3d529fc 186 CRM_Batch_BAO_Batch::exportFinancialBatch($batchIds, $this->_exportFormat, $this->_downloadFile);
6a488035 187 }
96025800 188
6a488035 189}