|
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('patterns', $this->extract->getPattern()); |
|
48
|
|
|
$this->setData('directories', $this->getRelativeDirectories()); |
|
49
|
|
|
|
|
50
|
|
|
$this->submitExtract(); |
|
51
|
|
|
$this->outputEditExtract(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns an array of relative directories to scan |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function getRelativeDirectories() |
|
59
|
|
|
{ |
|
60
|
|
|
$directories = array(); |
|
61
|
|
|
foreach ($this->extract->getScannedDirectories() as $directory) { |
|
62
|
|
|
$directories[] = gplcart_relative_path($directory); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $directories; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Downloads a file with extracted strings |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function downloadExtract() |
|
72
|
|
|
{ |
|
73
|
|
|
$download = $this->getQuery('download'); |
|
74
|
|
|
|
|
75
|
|
|
if (!empty($download)) { |
|
76
|
|
|
$this->download(gplcart_string_decode($download)); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Sets title on the extractor page |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function setTitleEditExtract() |
|
84
|
|
|
{ |
|
85
|
|
|
$this->setTitle($this->text('Extractor')); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Sets breadcrumbs on the extractor page |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function setBreadcrumbEditExtract() |
|
92
|
|
|
{ |
|
93
|
|
|
$this->setBreadcrumbHome(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Handles submitted actions related to string extraction |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function submitExtract() |
|
100
|
|
|
{ |
|
101
|
|
|
if ($this->isPosted('extract')) { |
|
102
|
|
|
$file = $this->getFileExtract(); |
|
103
|
|
|
|
|
104
|
|
|
if (empty($file)) { |
|
105
|
|
|
$this->redirect('', $this->text('Failed to create file'), 'warning'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$this->setJobExtract($file); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Creates a CSV file to write extracted string to and returns its path |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function getFileExtract() |
|
117
|
|
|
{ |
|
118
|
|
|
$file = gplcart_file_unique(GC_PRIVATE_TEMP_DIR . '/extracted-translations.csv'); |
|
119
|
|
|
return file_put_contents($file, '') === false ? '' : $file; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Renders and outputs the extractor page |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function outputEditExtract() |
|
126
|
|
|
{ |
|
127
|
|
|
$this->output('extractor|extract'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Returns a total number of files to scan |
|
132
|
|
|
* @return integer |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function getTotalExtract() |
|
135
|
|
|
{ |
|
136
|
|
|
$options = array( |
|
137
|
|
|
'count' => true, |
|
138
|
|
|
'directory' => $this->extract->getScannedDirectories() |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
return (int) $this->extract->scan($options); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Sets and performs string extraction job |
|
146
|
|
|
* @param string $file |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function setJobExtract($file) |
|
149
|
|
|
{ |
|
150
|
|
|
$limit = 10; |
|
151
|
|
|
|
|
152
|
|
|
$vars = array('@url' => $this->url('', array('download' => gplcart_string_encode($file)))); |
|
153
|
|
|
$finish = $this->text('Extracted %inserted strings from %total files. <a href="@url">Download</a>', $vars); |
|
154
|
|
|
|
|
155
|
|
|
$job = array( |
|
156
|
|
|
'id' => 'extract', |
|
157
|
|
|
'data' => array( |
|
158
|
|
|
'file' => $file, |
|
159
|
|
|
'limit' => $limit, |
|
160
|
|
|
'directory' => $this->extract->getScannedDirectories() |
|
161
|
|
|
), |
|
162
|
|
|
'total' => $this->getTotalExtract(), |
|
163
|
|
|
'redirect_message' => array('finish' => $finish) |
|
164
|
|
|
); |
|
165
|
|
|
|
|
166
|
|
|
$this->job->submit($job); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|