1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Extractor |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2015, Iurii Makukh |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\extractor\controllers; |
11
|
|
|
|
12
|
|
|
use gplcart\modules\extractor\models\Extract as ExtractorExtractModel; |
13
|
|
|
use gplcart\core\controllers\backend\Controller as BackendController; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Handles incoming requests and outputs data related to string extraction |
17
|
|
|
*/ |
18
|
|
|
class Extract extends BackendController |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Extractor's model instance |
23
|
|
|
* @var \gplcart\modules\extractor\models\Extract $extract |
24
|
|
|
*/ |
25
|
|
|
protected $extract; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ExtractorExtractModel $extract |
29
|
|
|
*/ |
30
|
|
|
public function __construct(ExtractorExtractModel $extract) |
31
|
|
|
{ |
32
|
|
|
parent::__construct(); |
33
|
|
|
|
34
|
|
|
$this->extract = $extract; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Displays the extractor page |
39
|
|
|
*/ |
40
|
|
|
public function editExtract() |
41
|
|
|
{ |
42
|
|
|
$this->downloadExtract(); |
43
|
|
|
|
44
|
|
|
$this->setTitleEditExtract(); |
45
|
|
|
$this->setBreadcrumbEditExtract(); |
46
|
|
|
|
47
|
|
|
$this->setData('scopes', $this->getScopesExtract()); |
48
|
|
|
$this->setData('patterns', $this->extract->getPattern()); |
49
|
|
|
|
50
|
|
|
$this->submitExtract(); |
51
|
|
|
$this->outputEditExtract(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns an array of scopes to extract from |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
protected function getScopesExtract() |
59
|
|
|
{ |
60
|
|
|
$scopes = array( |
61
|
|
|
array( |
62
|
|
|
'name' => $this->text('Core'), |
63
|
|
|
'directories' => $this->extract->getScannedDirectories() |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
foreach ($this->config->getModules() as $module) { |
68
|
|
|
$scopes[$module['module_id']] = array( |
69
|
|
|
'name' => $module['name'], |
70
|
|
|
'directories' => array($module['directory']) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $scopes; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Downloads a file with extracted strings |
79
|
|
|
*/ |
80
|
|
|
protected function downloadExtract() |
81
|
|
|
{ |
82
|
|
|
$download = $this->getQuery('download'); |
83
|
|
|
|
84
|
|
|
if (!empty($download)) { |
85
|
|
|
$this->download(gplcart_string_decode($download)); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sets title on the extractor page |
91
|
|
|
*/ |
92
|
|
|
protected function setTitleEditExtract() |
93
|
|
|
{ |
94
|
|
|
$this->setTitle($this->text('Extractor')); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Sets breadcrumbs on the extractor page |
99
|
|
|
*/ |
100
|
|
|
protected function setBreadcrumbEditExtract() |
101
|
|
|
{ |
102
|
|
|
$this->setBreadcrumbHome(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Handles submitted actions related to string extraction |
107
|
|
|
*/ |
108
|
|
|
protected function submitExtract() |
109
|
|
|
{ |
110
|
|
|
if ($this->isPosted('extract') && $this->validateExtract()) { |
111
|
|
|
$this->setJobExtract(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Validates submitted data |
117
|
|
|
*/ |
118
|
|
|
protected function validateExtract() |
119
|
|
|
{ |
120
|
|
|
$this->setSubmitted('settings'); |
121
|
|
|
$scope = $this->getSubmitted('scope'); |
122
|
|
|
$scopes = $this->getScopesExtract(); |
123
|
|
|
|
124
|
|
|
if (empty($scopes[$scope]['directories'])) { |
125
|
|
|
$this->setError('scope', $this->text('@field has invalid value', array('@field' => $this->text('Scope')))); |
126
|
|
|
} else { |
127
|
|
|
$this->setSubmitted('directories', $scopes[$scope]['directories']); |
128
|
|
|
$this->setSubmitted('file', $this->getFileExtract()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return !$this->hasErrors(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Creates a CSV file to write extracted string to and returns its path |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
protected function getFileExtract() |
139
|
|
|
{ |
140
|
|
|
$file = gplcart_file_unique(GC_PRIVATE_TEMP_DIR . '/extracted-translations.csv'); |
141
|
|
|
file_put_contents($file, ''); |
142
|
|
|
return $file; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Renders and outputs the extractor page |
147
|
|
|
*/ |
148
|
|
|
protected function outputEditExtract() |
149
|
|
|
{ |
150
|
|
|
$this->output('extractor|extract'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns a total number of files to scan |
155
|
|
|
* @param array $directories |
156
|
|
|
* @return integer |
157
|
|
|
*/ |
158
|
|
|
protected function getTotalExtract(array $directories) |
159
|
|
|
{ |
160
|
|
|
$options = array( |
161
|
|
|
'count' => true, |
162
|
|
|
'directory' => $directories |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
return (int) $this->extract->scan($options); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Sets and performs string extraction job |
170
|
|
|
*/ |
171
|
|
|
protected function setJobExtract() |
172
|
|
|
{ |
173
|
|
|
$limit = 10; |
174
|
|
|
$file = $this->getSubmitted('file'); |
175
|
|
|
$directories = $this->getSubmitted('directories'); |
176
|
|
|
$total = $this->getTotalExtract($directories); |
177
|
|
|
|
178
|
|
|
$vars = array('@url' => $this->url('', array('download' => gplcart_string_encode($file)))); |
179
|
|
|
$finish = $this->text('Extracted %inserted strings from %total files. <a href="@url">Download</a>', $vars); |
180
|
|
|
|
181
|
|
|
$job = array( |
182
|
|
|
'id' => 'extract', |
183
|
|
|
'data' => array( |
184
|
|
|
'file' => $file, |
185
|
|
|
'limit' => $limit, |
186
|
|
|
'directory' => $directories |
187
|
|
|
), |
188
|
|
|
'total' => $total, |
189
|
|
|
'redirect_message' => array('finish' => $finish) |
190
|
|
|
); |
191
|
|
|
|
192
|
|
|
$this->job->submit($job); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
} |
196
|
|
|
|