Helper::translate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/**
4
 * Helper
5
 * @copyright Copyright (c) 2011 - 2014 Aleksandr Torosh (http://wezoom.com.ua)
6
 * @author Aleksandr Torosh <[email protected]>
7
 */
8
9
namespace Application\Mvc;
10
11
use Application\Mvc\Router\DefaultRouter;
12
use Cms\Model\Language;
13
14
class Helper extends \Phalcon\Mvc\User\Component
15
{
16
    const StaticWidgetDefaultOptions = [
17
        'lifetime' => 120
18
    ];
19
20
    private $translate = null;
21
    private $admin_translate = null;
22
23
    public $menu;
24
25
    public function __construct()
26
    {
27
        $this->menu = \Menu\Helper\Menu::getInstance();
28
    }
29
30
    /**
31
     * Мультиязычный перевод строки по сайту/пользовательской_части
32
     */
33 View Code Duplication
    public function translate($string, $placeholders = null)
34
    {
35
        if (!$this->translate) {
36
            $this->translate = $this->getDi()->get('translate');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->getDi() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
37
        }
38
        return $this->translate->query($string, $placeholders);
39
40
    }
41
42
    /**
43
     * Мультиязычный перевод строки по админке
44
     */
45 View Code Duplication
    public function at($string, $placeholders = null)
46
    {
47
        if (!$this->admin_translate) {
48
            $this->admin_translate = $this->getDi()->get('admin_translate');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->getDi() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
49
        }
50
        return $this->admin_translate->query($string, $placeholders);
51
52
    }
53
54
    public function widget($namespace = 'Index', array $params = [])
55
    {
56
        return new \Application\Widget\Proxy($namespace, $params);
57
    }
58
59
    /**
60
     * Вызов выджета из модуля StaticWidget
61
     * @param $id - идентификатор виджета, например "phone"
62
     */
63
    public function staticWidget($id, $params = [])
64
    {
65
        $mergeConfig = array_merge(self::StaticWidgetDefaultOptions, $params);
66
        $widget = \Widget\Model\Widget::findFirst(["id='{$id}'", "cache" => ["lifetime" => $mergeConfig["lifetime"], "key" => HOST_HASH . md5("Widget::findFirst({$id})")]]);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $widget is correct as \Widget\Model\Widget::fi...::findFirst({$id})")))) (which targets Phalcon\Mvc\Model::findFirst()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
67
        if ($widget) {
68
            return $widget->getHtml();
69
        }
70
    }
71
72 View Code Duplication
    public function langUrl($params)
73
    {
74
        $routeName = $params['for'];
75
        $routeName = DefaultRouter::ML_PREFIX . $routeName . '_' . LANG;
76
        $params['for'] = $routeName;
77
        return $this->url->get($params);
78
    }
79
80
    public function languages()
81
    {
82
        return Language::findCachedLanguages();
83
84
    }
85
86
    public function langSwitcher($lang, $string)
87
    {
88
        $helper = new \Application\Mvc\Helper\LangSwitcher();
89
        return $helper->render($lang, $string);
90
    }
91
92
    public function cacheExpire($seconds)
93
    {
94
        $response = $this->getDi()->get('response');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->getDi() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
95
        $expireDate = new \DateTime();
96
        $expireDate->modify("+$seconds seconds");
97
        $response->setExpires($expireDate);
98
        $response->setHeader('Cache-Control', "max-age=$seconds");
99
    }
100
101
    public function isAdminSession()
102
    {
103
        $session = $this->getDi()->get('session');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->getDi() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
104
        $auth = $session->get('auth');
105
        if ($auth) {
106
            if ($auth->admin_session == true) {
107
                return true;
108
            }
109
        }
110
    }
111
112
    public function error($code = 404)
113
    {
114
        $helper = new \Application\Mvc\Helper\ErrorReporting();
115
        return $helper->{'error' . $code}();
116
117
    }
118
119
    public function title($title = null, $h1 = false)
120
    {
121
        return \Application\Mvc\Helper\Title::getInstance($title, $h1);
122
    }
123
124
    public function meta()
125
    {
126
        return \Application\Mvc\Helper\Meta::getInstance();
127
    }
128
129
    public function activeMenu()
130
    {
131
        return \Application\Mvc\Helper\ActiveMenu::getInstance();
132
    }
133
134
    public function announce($incomeString, $num)
135
    {
136
        $object = new \Application\Mvc\Helper\Announce();
137
        return $object->getString($incomeString, $num);
138
    }
139
140
    public function dbProfiler()
141
    {
142
        $object = new \Application\Mvc\Helper\DbProfiler();
143
        return $object->DbOutput();
144
    }
145
146
    public function constant($name)
147
    {
148
        return get_defined_constants()[$name];
149
    }
150
151
    public function image($args, $attributes = [])
152
    {
153
        $imageFilter = new \Image\Storage($args, $attributes);
154
        return $imageFilter;
155
    }
156
157
    public function querySymbol()
158
    {
159
        $object = new \Application\Mvc\Helper\RequestQuery();
160
        return $object->getSymbol();
161
    }
162
163
    public function javascript($id)
164
    {
165
        $javascript = \Cms\Model\Javascript::findCachedById($id);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $javascript is correct as \Cms\Model\Javascript::findCachedById($id) (which targets Cms\Model\Javascript::findCachedById()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
166
        if ($javascript) {
167
            return $javascript->getText();
168
        }
169
    }
170
171
    public function modulePartial($template, $data, $module = null)
172
    {
173
        $view = clone $this->getDi()->get('view');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->getDi() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
174
        $partialsDir = '';
175
        if ($module) {
176
            $moduleName = \Application\Utils\ModuleName::camelize($module);
177
            $partialsDir = '../../../modules/' . $moduleName . '/views/';
178
        }
179
        $view->setPartialsDir($partialsDir);
180
181
        return $view->partial($template, $data);
182
    }
183
184
}
185