TranslationsPresenter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 52.73%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 2
cbo 8
dl 0
loc 89
ccs 29
cts 55
cp 0.5273
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A renderDefault() 0 4 1
A actionDeleteTranslation() 0 12 1
A handleUpdateTranslation() 0 16 1
B createComponentTranslationGrid() 0 39 1
A handleRegenerateTranslations() 0 11 2
1
<?php
2
3
/**
4
 * Webcms2 admin module package.
5
 */
6
7
namespace AdminModule;
8
9
/**
10
 * @author Tomáš Voslař <tomas.voslar at webcook.cz>
11
 * @package WebCMS2
12
 */
13
class TranslationsPresenter extends BasePresenter
14
{
15 1
    public function renderDefault()
16
    {
17 1
        $this->reloadContent();
18 1
    }
19
20 1
    protected function createComponentTranslationGrid($name)
21
    {
22 1
        $grid = $this->createGrid($this, $name, "Translation");
23
24 1
        $langs = $this->getAllLanguages();
25
26
        $backend = array(
27 1
            '' => $this->translation['Pick filter'],
28 1
            0 => $this->translation['No'],
29 1
            1 => $this->translation['Yes'],
30 1
        );
31
32 1
        $grid->addColumnText('id', 'ID')->setSortable()->setFilterNumber();
33 1
        $grid->addColumnText('key', 'Key')->setSortable()->setFilterText();
34
        $grid->addColumnText('translation', 'Value')->setSortable()->setCustomRender(function ($item) {
35 1
            return '<div class="translation" contentEditable>'.$item->getTranslation().'</div>';
36 1
        });
37 1
        $grid->addColumnText('backend', 'Backend')->setReplacement(array(
38 1
            '1' => 'Yes',
39 1
            NULL => 'No',
40 1
        ))->setFilterSelect($backend);
41
42 1
        $grid->addColumnText('translated', 'Translated')->setReplacement(array(
43 1
            '1' => 'Yes',
44 1
            NULL => 'No',
45 1
        ))->setFilterSelect($backend);
46
47 1
        $grid->addColumnText('language', 'Language')->setCustomRender(function ($item) {
48 1
            return $item->getLanguage()->getName();
49 1
        })->setSortable();
50
51 1
        $grid->addFilterSelect('language', 'Language')->getControl()->setTranslator(null)->setItems($langs);
52
53 1
        $grid->addActionHref("deleteTranslation", 'Delete')->getElementPrototype()->addAttributes(array('class' => array('btn', 'btn-danger'), 'data-confirm' => 'Are you sure you want to delete the item?'));
54
55 1
        $grid->setFilterRenderType(\Grido\Components\Filters\Filter::RENDER_INNER);
56
57 1
        return $grid;
58
    }
59
60
    public function actionDeleteTranslation($id)
61
    {
62
        $translation = $this->em->find("WebCMS\Entity\Translation", $id);
63
        $this->em->remove($translation);
64
        $this->em->flush();
65
66
        $this->flashMessage('Translation has been removed.', 'success');
67
68
        $this->cleanCache();
0 ignored issues
show
Documentation Bug introduced by
The method cleanCache does not exist on object<AdminModule\TranslationsPresenter>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
69
70
        $this->forward('Languages:Translates');
71
    }
72
73
    public function handleUpdateTranslation($idTranslation, $value)
74
    {
75
        $value = strip_tags($value, '<strong><b><i><u>');
76
77
        $translation = $this->em->find('WebCMS\Entity\Translation', trim($idTranslation));
78
        $translation->setTranslation(trim($value));
79
80
        $this->em->persist($translation);
81
        $this->em->flush();
82
83
        $this->flashMessage('Translation has been added.', 'success');
84
85
        $this->invalidateControl('flashMessages');
86
87
        $this->forward('Languages:Translates');
88
    }
89
90
    public function handleRegenerateTranslations()
91
    {
92
        $translations = $this->em->getRepository('WebCMS\Entity\Translation')->findAll();
93
94
        foreach ($translations as $t) {
95
            $t->setTranslation($t->getTranslation());
96
	    $t->setHash();
97
        }
98
99
        $this->em->flush();
100
    }
101
}
102