TranslatorPresenter::actionTranslator()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 9.4285
cc 3
eloc 8
nc 4
nop 0
crap 12
1
<?php
2
3
/**
4
 * Webcms2 admin module package.
5
 */
6
7
namespace AdminModule;
8
9
use Nette\Application\UI;
10
11
/**
12
 * @author Tomáš Voslař <tomas.voslar at webcook.cz>
13
 * @package WebCMS2
14
 */
15
class TranslatorPresenter extends BasePresenter
16
{
17
    /* @var \Webcook\Translator\ServiceFactory */
18
    private $serviceFactory;
19
20
    /* @var \Webcook\Translator\ITranslator */
21
    private $translatorService;
22
23 1
    public function renderDefault()
24
    {
25 1
        $this->reloadContent();
26 1
    }
27
28
    public function actionTranslator()
29
    {
30
        $this->serviceFactory = new \Webcook\Translator\ServiceFactory();
31
32
        try {
33
            $this->translatorService = $this->getTranslateService();
34
        } catch (Exception $exc) {
0 ignored issues
show
Bug introduced by
The class AdminModule\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
35
            $this->flashMessage($exc->getMessage(), 'danger');
36
        }
37
38
        if (!$this->translatorService instanceof \Webcook\Translator\ITranslator) {
39
            $this->flashMessage('You must fill in API key.', 'danger');
40
        }
41
    }
42
43
    private function getTranslateService()
44
    {
45
        $serviceId = $this->settings->get('Translate service', \WebCMS\Settings::SECTION_BASIC, 'select')->getValue();
46
47
        $this->serviceFactory->setSettings(array(
48
            \Webcook\Translator\ServiceFactory::YANDEX => array(
49
            'key' => $this->settings->get('Yandex API key', \WebCMS\Settings::SECTION_BASIC)->getValue(),
50
            ),
51
            \Webcook\Translator\ServiceFactory::GOOGLE => array(
52
            'key' => $this->settings->get('Google API key', \WebCMS\Settings::SECTION_BASIC)->getValue(),
53
            ),
54
            \Webcook\Translator\ServiceFactory::BING => array(
55
            'clientId' => $this->settings->get('Bing client id', \WebCMS\Settings::SECTION_BASIC)->getValue(),
56
            'clientSecret' => $this->settings->get('Bing client secret', \WebCMS\Settings::SECTION_BASIC)->getValue(),
57
            ),
58
        ));
59
60
        return $this->serviceFactory->build($serviceId);
61
    }
62
63
    private function getLanguages()
64
    {
65
        $serviceId = $this->settings->get('Translate service', \WebCMS\Settings::SECTION_BASIC, 'select')->getValue();
66
67
        $cache = new \Nette\Caching\Cache($this->getContext()->getService('cacheStorage'), 'htmlFront');
68
69
        if (!$languages = $cache->load('tl'.$serviceId)) {
70
            $languages = $this->translatorService->getLanguages();
71
72
            $cache->save('tl'.$serviceId, $languages);
73
        }
74
75
        return $languages;
76
    }
77
78 1
    public function createComponentTranslatorForm()
79
    {
80 1
        $form = $this->createForm();
81
82 1
        $packages = \WebCMS\Helpers\SystemHelper::getPackages();
83
84 1
        if ($this->translatorService instanceof \Webcook\Translator\ITranslator) {
85
            $langs = $this->getLanguages();
86
            $langst = array();
87
            foreach ($langs as $yl) {
88
                $langst[$yl->getAbbreviation()] = $yl->getName();
89
            }
90
91
            $form->addGroup('System');
92
            $form->addSelect('systemLanguage', 'System language', $this->getAllLanguages())->setAttribute('class', 'form-control');
0 ignored issues
show
Documentation introduced by
'form-control' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
94
            $form->addGroup('Service');
95
            $form->addSelect('languageFrom', 'From', $langst)->setAttribute('class', 'form-control');
0 ignored issues
show
Documentation introduced by
'form-control' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
            $form->addSelect('languageTo', 'To', $langst)->setAttribute('class', 'form-control');
0 ignored issues
show
Documentation introduced by
'form-control' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
97
98
            $form->addGroup('Settings');
99
100 View Code Duplication
            foreach ($packages as $key => $package) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
                if ($package['vendor'] === 'webcms2' && $package['package'] !== 'webcms2') {
102
                    $object = $this->createObject($package['package']);
103
104
                    if ($object->isTranslatable()) {
105
                        $form->addCheckbox(str_replace('-', '_', $package['package']), $package['package']);
106
                    } else {
107
                        $form->addCheckbox(str_replace('-', '_', $package['package']), $package['package'].' not translatable.')->setDisabled(true);
108
                    }
109
                }
110
            }
111
112
            $form->addSubmit('translate', 'Translate');
113
        }
114
115 1
        $form->onSuccess[] = callback($this, 'translatorFormSubmitted');
116
117 1
        return $form;
118
    }
119
120
    public function translatorFormSubmitted(UI\Form $form)
121
    {
122
        $values = $form->getValues();
123
        $from = $values->languageFrom;
124
        $to = $values->languageTo;
125
        $language = $this->em->getRepository('WebCMS\Entity\Language')->find($values->systemLanguage);
126
127
        // clear values
128
        unset($values->languageFrom);
129
        unset($values->languageTo);
130
        unset($values->systemLanguage);
131
132
        $pages = $this->em->getRepository('WebCMS\Entity\Page')->findBy(array(
133
            'language' => $language,
134
            ), array('lft' => 'asc'));
135
136
        foreach ($pages as $page) {
137
            $t = $this->translatorService->translate($page->getTitle(), $from, $to);
138
            $page->setTitle($t->getTranslation());
139
            $page->setSlug(\Nette\Utils\Strings::webalize($page->getTitle()));
140
141
            $this->em->flush();
142
143
            $path = $this->em->getRepository('WebCMS\Entity\Page')->getPath($page);
144
            $final = array();
145
            foreach ($path as $p) {
146
                if ($p->getParent() != NULL) {
147
                    $final[] = $p->getSlug();
148
                }
149
            }
150
151
            $page->setPath(implode('/', $final));
152
        }
153
154
        // translate all data
155
        foreach ($values as $key => $value) {
156
            if ($value) {
157
                $module = $this->createObject(str_replace('_', '-', $key));
158
                if ($module->isTranslatable()) {
159
                    $module->translateData($this->em, $language, $from, $to, $this->translatorService);
160
                }
161
            }
162
        }
163
164
        // translate all static texts
165
        $translations = $this->em->getRepository('WebCMS\Entity\Translation')->findBy(array(
166
            'language' => $language,
167
        ));
168
169
        foreach ($translations as $translation) {
170
            $t = $this->translatorService->translate($translation->getTranslation(), $from, $to);
171
            $translation->setTranslation($t->getTranslation());
172
            $translation->setHash();
173
        }
174
175
        $this->em->flush();
176
177
        $this->flashMessage('Translation of language finished.', 'success');
178
        if (!$this->isAjax()) {
179
            $this->forward('Languages:translator');
180
        }
181
    }
182
}
183