Extractor::getCoreTranslationsExtractor()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
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\core\controllers\backend\Controller;
13
use gplcart\modules\extractor\models\Extractor as ExtractorModel;
14
15
/**
16
 * Handles incoming requests and outputs data related to string extraction
17
 */
18
class Extractor extends Controller
19
{
20
21
    /**
22
     * Extractor's model instance
23
     * @var \gplcart\modules\extractor\models\Extractor $extractor
24
     */
25
    protected $extractor;
26
27
    /**
28
     * @param ExtractorModel $extract
29
     */
30
    public function __construct(ExtractorModel $extract)
31
    {
32
        parent::__construct();
33
34
        $this->extractor = $extract;
35
    }
36
37
    /**
38
     * Displays the extractor page
39
     */
40
    public function editExtractor()
41
    {
42
        $this->downloadExtractor();
43
        $this->setTitleEditExtractor();
44
        $this->setBreadcrumbEditExtractor();
45
46
        $this->setData('patterns', $this->extractor->get());
47
        $this->setData('scopes', $this->getScopesExtractor());
48
        $this->setData('files', $this->getCoreTranslationsExtractor());
49
50
        $this->submitExtractor();
51
        $this->outputEditExtractor();
52
    }
53
54
    /**
55
     * Returns an array of core translation files
56
     * @return array
57
     */
58
    protected function getCoreTranslationsExtractor()
59
    {
60
        $files = array();
61
62
        foreach (array_keys($this->language->getList()) as $langcode) {
63
            $file = $this->translation->getFile($langcode);
64
            if (is_file($file)) {
65
                $files[basename($file)] = $file;
66
            }
67
        }
68
69
        return $files;
70
    }
71
72
    /**
73
     * Returns an array of scopes to extract from
74
     * @return array
75
     */
76
    protected function getScopesExtractor()
77
    {
78
        $scopes = array();
79
80
        foreach ($this->module->getList() as $module) {
81
            $scopes[$module['module_id']] = array(
82
                'name' => $module['name'],
83
                'directories' => array($module['directory'])
84
            );
85
        }
86
87
        gplcart_array_sort($scopes, 'name');
88
89
        $core = array(
90
            'name' => $this->text('Core'),
91
            'directories' => $this->extractor->getScannedDirectories()
92
        );
93
94
        array_unshift($scopes, $core);
95
        return $scopes;
96
    }
97
98
    /**
99
     * Downloads a file with extracted strings
100
     */
101
    protected function downloadExtractor()
102
    {
103
        $download = $this->getQuery('download');
104
105
        if (!empty($download)) {
106
            $this->download(gplcart_string_decode($download));
107
        }
108
    }
109
110
    /**
111
     * Sets title on the extractor page
112
     */
113
    protected function setTitleEditExtractor()
114
    {
115
        $this->setTitle($this->text('Extractor'));
116
    }
117
118
    /**
119
     * Sets breadcrumbs on the extractor page
120
     */
121
    protected function setBreadcrumbEditExtractor()
122
    {
123
        $breadcrumb = array(
124
            'url' => $this->url('admin'),
125
            'text' => $this->text('Dashboard')
126
        );
127
128
        $this->setBreadcrumb($breadcrumb);
129
    }
130
131
    /**
132
     * Handles submitted actions related to string extraction
133
     */
134
    protected function submitExtractor()
135
    {
136
        if ($this->isPosted('extract') && $this->validateExtractor()) {
137
            $this->setJobExtractor();
138
        }
139
    }
140
141
    /**
142
     * Validates submitted data
143
     */
144
    protected function validateExtractor()
145
    {
146
        $this->setSubmitted('settings');
147
        $scope = $this->getSubmitted('scope');
148
        $check = $this->getSubmitted('check_duplicates');
149
150
        $scopes = $this->getScopesExtractor();
151
        $core_files = $this->getCoreTranslationsExtractor();
152
153
        if (empty($scopes[$scope]['directories'])) {
154
            $this->setError('scope', $this->text('@field has invalid value', array('@field' => $this->text('Scope'))));
155
        } else {
156
            $this->setSubmitted('directories', $scopes[$scope]['directories']);
157
        }
158
159
        if (!empty($check) && !empty($scope)) {
160
            if (empty($core_files[$check])) {
161
                $this->setError('check_duplicates', $this->text('@field has invalid value', array('@field' => $this->text('Check duplicates'))));
162
            } else {
163
                $this->setSubmitted('check_file', $core_files[$check]);
164
            }
165
        }
166
167
        if (!$this->isError()) {
168
            $this->setSubmitted('file', $this->getFileExtractor());
169
        }
170
171
        return !$this->hasErrors();
172
    }
173
174
    /**
175
     * Creates a CSV file to write extracted string to and returns its path
176
     * @return string
177
     */
178
    protected function getFileExtractor()
179
    {
180
        $file = gplcart_file_private_temp('extracted-translations.csv', true);
181
        file_put_contents($file, '');
182
        return $file;
183
    }
184
185
    /**
186
     * Renders and outputs the extractor page
187
     */
188
    protected function outputEditExtractor()
189
    {
190
        $this->output('extractor|extract');
191
    }
192
193
    /**
194
     * Returns a total number of files to scan
195
     * @param array $directories
196
     * @return integer
197
     */
198
    protected function getTotalExtractor(array $directories)
199
    {
200
        $options = array(
201
            'count' => true,
202
            'directory' => $directories
203
        );
204
205
        return (int) $this->extractor->scan($options);
206
    }
207
208
    /**
209
     * Sets and performs string extraction job
210
     */
211
    protected function setJobExtractor()
212
    {
213
        $this->controlAccess('module_extractor_edit');
214
215
        $limit = 10;
216
        $file = $this->getSubmitted('file');
217
        $directories = $this->getSubmitted('directories');
218
        $total = $this->getTotalExtractor($directories);
219
220
        $vars = array('@url' => $this->url('', array('download' => gplcart_string_encode($file))));
221
        $finish_message = $this->text('Extracted %inserted strings from %total files. <a href="@url">Download</a>', $vars);
222
        $noresults_message = $this->text('Processed %total files. Nothing was extracted!');
223
224
        $job = array(
225
            'id' => 'extract',
226
            'data' => array(
227
                'file' => $file,
228
                'limit' => $limit,
229
                'directory' => $directories,
230
                'check_file' => $this->getSubmitted('check_file')
231
            ),
232
            'total' => $total,
233
            'redirect_message' => array('finish' => $finish_message, 'no_results' => $noresults_message)
234
        );
235
236
        $this->job->submit($job);
237
    }
238
239
}
240