Attempt to fix paging on contact summary
[civicrm-core.git] / CRM / Financial / Form / Export.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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 */
39 class CRM_Financial_Form_Export extends CRM_Core_Form {
40
41 /**
42 * The financial batch id, used when editing the field
43 *
44 * @var int
45 */
46 protected $_id;
47
48 /**
49 * Financial batch ids.
50 * @var array
51 */
52 protected $_batchIds = [];
53
54 /**
55 * Export status id.
56 * @var int
57 */
58 protected $_exportStatusId;
59
60 /**
61 * Export format.
62 * @var string
63 */
64 protected $_exportFormat;
65
66 /**
67 * Download export File.
68 * @var bool
69 */
70 protected $_downloadFile = TRUE;
71
72 /**
73 * Build all the data structures needed to build the form.
74 */
75 public function preProcess() {
76 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
77
78 // this mean it's a batch action
79 if (!$this->_id) {
80 if (!empty($_GET['batch_id'])) {
81 // validate batch ids
82 $batchIds = explode(',', $_GET['batch_id']);
83 foreach ($batchIds as $batchId) {
84 CRM_Utils_Type::validate($batchId, 'Positive');
85 }
86
87 $this->_batchIds = $_GET['batch_id'];
88 $this->set('batchIds', $this->_batchIds);
89 }
90 else {
91 $this->_batchIds = $this->get('batchIds');
92 }
93 if (!empty($_GET['export_format']) && in_array($_GET['export_format'], ['IIF', 'CSV'])) {
94 $this->_exportFormat = $_GET['export_format'];
95 }
96 }
97 else {
98 $this->_batchIds = $this->_id;
99 }
100
101 $this->_exportStatusId = CRM_Core_PseudoConstant::getKey('CRM_Batch_DAO_Batch', 'status_id', 'Exported');
102
103 // check if batch status is valid, do not allow exported batches to export again
104 $batchStatus = CRM_Batch_BAO_Batch::getBatchStatuses($this->_batchIds);
105
106 foreach ($batchStatus as $batchStatusId) {
107 if ($batchStatusId == $this->_exportStatusId) {
108 $url = CRM_Core_Session::singleton()->readUserContext();
109 CRM_Core_Error::statusBounce(ts('You cannot export batches which have already been exported.'), $url);
110 }
111 }
112
113 $session = CRM_Core_Session::singleton();
114 $session->replaceUserContext(CRM_Utils_System::url('civicrm/financial/financialbatches',
115 "reset=1&batchStatus={$this->_exportStatusId}"));
116 }
117
118 /**
119 * Build the form object.
120 */
121 public function buildQuickForm() {
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
132 $optionTypes = [
133 'IIF' => ts('Export to IIF'),
134 'CSV' => ts('Export to CSV'),
135 ];
136
137 $this->addRadio('export_format', NULL, $optionTypes, NULL, '<br/>', TRUE);
138
139 $this->addButtons(
140 [
141 [
142 'type' => 'next',
143 'name' => ts('Export Batch'),
144 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
145 'isDefault' => TRUE,
146 ],
147 [
148 'type' => 'cancel',
149 'name' => ts('Cancel'),
150 ],
151 ]
152 );
153 }
154
155 /**
156 * Process the form after the input has been submitted and validated.
157 */
158 public function postProcess() {
159 if (!$this->_exportFormat) {
160 $params = $this->exportValues();
161 $this->_exportFormat = $params['export_format'];
162 }
163
164 if ($this->_id) {
165 $batchIds = [$this->_id];
166 }
167 elseif (!empty($this->_batchIds)) {
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
179 foreach ($batchIds as $batchId) {
180 $batchParams['id'] = $batchId;
181 // Update totals
182 $batchParams = array_merge($batchParams, $totals[$batchId]);
183 CRM_Batch_BAO_Batch::create($batchParams);
184 }
185
186 CRM_Batch_BAO_Batch::exportFinancialBatch($batchIds, $this->_exportFormat, $this->_downloadFile);
187 }
188
189 }