|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package Exporter |
|
5
|
|
|
* @author Iurii Makukh |
|
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh |
|
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gplcart\modules\export\controllers; |
|
11
|
|
|
|
|
12
|
|
|
use gplcart\core\controllers\backend\Controller; |
|
13
|
|
|
use gplcart\core\models\Product; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Handles incoming requests and outputs data related to Expor module |
|
17
|
|
|
*/ |
|
18
|
|
|
class Export extends Controller |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Product model class instance |
|
23
|
|
|
* @var \gplcart\core\models\Product $product |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $product; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Export constructor. |
|
29
|
|
|
* @param Product $product |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Product $product) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct(); |
|
34
|
|
|
|
|
35
|
|
|
$this->product = $product; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Route callback to display the export page |
|
40
|
|
|
*/ |
|
41
|
|
|
public function doExport() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->downloadCsvExport(); |
|
44
|
|
|
|
|
45
|
|
|
$settings = $this->module->getSettings('export'); |
|
46
|
|
|
|
|
47
|
|
|
if (empty($settings['columns'])) { |
|
48
|
|
|
$settings['columns'] = array_keys($settings['header']); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->setData('settings', $settings); |
|
52
|
|
|
$this->setData('columns', $settings['header']); |
|
53
|
|
|
$this->setData('stores', $this->store->getList()); |
|
54
|
|
|
|
|
55
|
|
|
$this->submitExport(); |
|
56
|
|
|
$this->setTitleDoExport(); |
|
57
|
|
|
$this->setBreadcrumbDoExport(); |
|
58
|
|
|
|
|
59
|
|
|
$this->outputDoExport(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Sets titles on the export page |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function setTitleDoExport() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->setTitle($this->text('Export')); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Sets breadcrumbs on the export page |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function setBreadcrumbDoExport() |
|
74
|
|
|
{ |
|
75
|
|
|
$breadcrumb = array( |
|
76
|
|
|
'text' => $this->text('Dashboard'), |
|
77
|
|
|
'url' => $this->url('admin') |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
$this->setBreadcrumb($breadcrumb); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Renders the export page templates |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function outputDoExport() |
|
87
|
|
|
{ |
|
88
|
|
|
$this->output('export|export'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Start export |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function submitExport() |
|
95
|
|
|
{ |
|
96
|
|
|
if ($this->isPosted('export') && $this->validateExport()) { |
|
97
|
|
|
$this->setJobExport(); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Validates submitted export data |
|
103
|
|
|
* @return boolean |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function validateExport() |
|
106
|
|
|
{ |
|
107
|
|
|
$this->setSubmitted('settings'); |
|
108
|
|
|
$this->validateElement('columns', 'required'); |
|
109
|
|
|
$this->validateFileExport(); |
|
110
|
|
|
|
|
111
|
|
|
return !$this->hasErrors(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Validates destination directory and file |
|
116
|
|
|
* @return boolean |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function validateFileExport() |
|
119
|
|
|
{ |
|
120
|
|
|
$directory = gplcart_file_private_module('export'); |
|
121
|
|
|
|
|
122
|
|
|
if (!file_exists($directory) && !mkdir($directory, 0775, true)) { |
|
123
|
|
|
$this->setError('file', $this->text('Unable to create @name', array('@name' => $directory))); |
|
124
|
|
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$date = date('d-m-Y--H-i'); |
|
128
|
|
|
$file = gplcart_file_unique("$directory/$date.csv"); |
|
129
|
|
|
|
|
130
|
|
|
if (file_put_contents($file, '') === false) { |
|
131
|
|
|
$this->setError('file', $this->text('Unable to create @name', array('@name' => $file))); |
|
132
|
|
|
return false; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$this->setSubmitted('file', $file); |
|
136
|
|
|
return true; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Sets up export job |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function setJobExport() |
|
143
|
|
|
{ |
|
144
|
|
|
$submitted = $this->getSubmitted(); |
|
145
|
|
|
$settings = $this->module->getSettings('export'); |
|
146
|
|
|
|
|
147
|
|
|
$settings['columns'] = $submitted['columns']; |
|
148
|
|
|
$settings['options'] = $submitted['options']; |
|
149
|
|
|
|
|
150
|
|
|
$this->module->setSettings('export', $settings); |
|
151
|
|
|
|
|
152
|
|
|
$data = array_merge($settings, $submitted); |
|
153
|
|
|
|
|
154
|
|
|
$data['header'] = array_intersect_key($data['header'], array_flip($data['columns'])); |
|
155
|
|
|
gplcart_file_csv($data['file'], $data['header'], $data['delimiter']); |
|
156
|
|
|
|
|
157
|
|
|
$hash = gplcart_string_encode($data['file']); |
|
158
|
|
|
$total = $this->getTotalProductExport($data['options']); |
|
159
|
|
|
|
|
160
|
|
|
$vars = array('@url' => $this->url('', array('download' => $hash)), '@num' => $total); |
|
161
|
|
|
$finish = $this->text('Exported @num items. <a href="@url">Download</a>', $vars); |
|
162
|
|
|
|
|
163
|
|
|
$job = array( |
|
164
|
|
|
'data' => $data, |
|
165
|
|
|
'total' => $total, |
|
166
|
|
|
'id' => 'export_product', |
|
167
|
|
|
'redirect_message' => array('finish' => $finish) |
|
168
|
|
|
); |
|
169
|
|
|
|
|
170
|
|
|
$this->job->submit($job); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Download a created CSV file |
|
175
|
|
|
*/ |
|
176
|
|
|
protected function downloadCsvExport() |
|
177
|
|
|
{ |
|
178
|
|
|
$file = $this->getQuery('download'); |
|
179
|
|
|
|
|
180
|
|
|
if (!empty($file)) { |
|
181
|
|
|
$this->download(gplcart_string_decode($file)); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Returns a total number of products found for the given conditions |
|
187
|
|
|
* @param array $options |
|
188
|
|
|
* @return integer |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function getTotalProductExport(array $options) |
|
191
|
|
|
{ |
|
192
|
|
|
$options['count'] = true; |
|
193
|
|
|
return (int) $this->product->getList($options); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
} |
|
197
|
|
|
|