Completed
Push — master ( 07f861...31eb2d )
by Iurii
02:00
created

Extractor::getTotalExtractor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
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\Extractor as ExtractorModel;
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 Extractor extends BackendController
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
44
        $this->setTitleEditExtractor();
45
        $this->setBreadcrumbEditExtractor();
46
47
        $this->setData('patterns', $this->extractor->get());
48
        $this->setData('scopes', $this->getScopesExtractor());
49
        $this->setData('files', $this->getCoreTranslationsExtractor());
50
51
        $this->submitExtractor();
52
        $this->outputEditExtractor();
53
    }
54
55
    /**
56
     * Returns an array of core translation files
57
     * @return array
58
     */
59
    protected function getCoreTranslationsExtractor()
60
    {
61
        $files = array();
62
        foreach (array_keys($this->language->getAvailable()) as $langcode) {
63
            $file = $this->language->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
        foreach ($this->config->getModules() as $module) {
80
            $scopes[$module['module_id']] = array(
81
                'name' => $module['name'],
82
                'directories' => array($module['directory'])
83
            );
84
        }
85
86
        gplcart_array_sort($scopes, 'name');
87
88
        $core = array(
89
            'name' => $this->text('Core'),
90
            'directories' => $this->extractor->getScannedDirectories()
91
        );
92
93
        array_unshift($scopes, $core);
94
        return $scopes;
95
    }
96
97
    /**
98
     * Downloads a file with extracted strings
99
     */
100
    protected function downloadExtractor()
101
    {
102
        $download = $this->getQuery('download');
103
104
        if (!empty($download)) {
105
            $this->download(gplcart_string_decode($download));
106
        }
107
    }
108
109
    /**
110
     * Sets title on the extractor page
111
     */
112
    protected function setTitleEditExtractor()
113
    {
114
        $this->setTitle($this->text('Extractor'));
115
    }
116
117
    /**
118
     * Sets breadcrumbs on the extractor page
119
     */
120
    protected function setBreadcrumbEditExtractor()
121
    {
122
        $this->setBreadcrumbHome();
123
    }
124
125
    /**
126
     * Handles submitted actions related to string extraction
127
     */
128
    protected function submitExtractor()
129
    {
130
        if ($this->isPosted('extract') && $this->validateExtractor()) {
131
            $this->setJobExtractor();
132
        }
133
    }
134
135
    /**
136
     * Validates submitted data
137
     */
138
    protected function validateExtractor()
139
    {
140
        $this->setSubmitted('settings');
141
        $scope = $this->getSubmitted('scope');
142
        $check = $this->getSubmitted('check_duplicates');
143
144
        $scopes = $this->getScopesExtractor();
145
        $core_files = $this->getCoreTranslationsExtractor();
146
147
        if (empty($scopes[$scope]['directories'])) {
148
            $this->setError('scope', $this->text('@field has invalid value', array('@field' => $this->text('Scope'))));
149
        } else {
150
            $this->setSubmitted('directories', $scopes[$scope]['directories']);
151
        }
152
153
        if (!empty($check) && !empty($scope)) {
154
            if (empty($core_files[$check])) {
155
                $this->setError('check_duplicates', $this->text('@field has invalid value', array('@field' => $this->text('Check duplicates'))));
156
            } else {
157
                $this->setSubmitted('check_file', $core_files[$check]);
158
            }
159
        }
160
161
        if (!$this->isError()) {
162
            $this->setSubmitted('file', $this->getFileExtractor());
163
        }
164
165
        return !$this->hasErrors();
166
    }
167
168
    /**
169
     * Creates a CSV file to write extracted string to and returns its path
170
     * @return string
171
     */
172
    protected function getFileExtractor()
173
    {
174
        $file = gplcart_file_unique(GC_PRIVATE_TEMP_DIR . '/extracted-translations.csv');
175
        file_put_contents($file, '');
176
        return $file;
177
    }
178
179
    /**
180
     * Renders and outputs the extractor page
181
     */
182
    protected function outputEditExtractor()
183
    {
184
        $this->output('extractor|extract');
185
    }
186
187
    /**
188
     * Returns a total number of files to scan
189
     * @param array $directories
190
     * @return integer
191
     */
192
    protected function getTotalExtractor(array $directories)
193
    {
194
        $options = array(
195
            'count' => true,
196
            'directory' => $directories
197
        );
198
199
        return (int) $this->extractor->scan($options);
200
    }
201
202
    /**
203
     * Sets and performs string extraction job
204
     */
205
    protected function setJobExtractor()
206
    {
207
        $this->controlAccess('module_extractor_edit');
208
209
        $limit = 10;
210
        $file = $this->getSubmitted('file');
211
        $directories = $this->getSubmitted('directories');
212
        $total = $this->getTotalExtractor($directories);
213
214
        $vars = array('@url' => $this->url('', array('download' => gplcart_string_encode($file))));
215
        $finish_message = $this->text('Extracted %inserted strings from %total files. <a href="@url">Download</a>', $vars);
216
        $noresults_message = $this->text('Processed %total files. Nothing was extracted!');
217
218
        $job = array(
219
            'id' => 'extract',
220
            'data' => array(
221
                'file' => $file,
222
                'limit' => $limit,
223
                'directory' => $directories,
224
                'check_file' => $this->getSubmitted('check_file')
225
            ),
226
            'total' => $total,
227
            'redirect_message' => array('finish' => $finish_message, 'no_results' => $noresults_message)
228
        );
229
230
        $this->job->submit($job);
231
    }
232
233
}
234