Completed
Push — master ( 5247c6...b8f763 )
by Craig
10:35 queued 03:41
created

AbstractTwigExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 4
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Routes.
4
 *
5
 * @copyright Zikula contributors (Zikula)
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 * @author Zikula contributors <[email protected]>.
8
 * @link http://www.zikula.org
9
 * @link http://zikula.org
10
 * @version Generated by ModuleStudio 0.7.1 (http://modulestudio.de).
11
 */
12
13
namespace Zikula\RoutesModule\Twig\Base;
14
15
use Twig_Extension;
16
use Zikula\Common\Translator\TranslatorInterface;
17
use Zikula\Common\Translator\TranslatorTrait;
18
use Zikula\ExtensionsModule\Api\VariableApi;
19
use Zikula\RoutesModule\Helper\ListEntriesHelper;
20
use Zikula\RoutesModule\Helper\WorkflowHelper;
21
22
/**
23
 * Twig extension base class.
24
 */
25
abstract class AbstractTwigExtension extends Twig_Extension
26
{
27
    use TranslatorTrait;
28
    
29
    /**
30
     * @var VariableApi
31
     */
32
    protected $variableApi;
33
    
34
    /**
35
     * @var WorkflowHelper
36
     */
37
    protected $workflowHelper;
38
    
39
    /**
40
     * @var ListEntriesHelper
41
     */
42
    protected $listHelper;
43
    
44
    /**
45
     * TwigExtension constructor.
46
     *
47
     * @param TranslatorInterface $translator     Translator service instance
48
     * @param VariableApi         $variableApi    VariableApi service instance
49
     * @param WorkflowHelper      $workflowHelper WorkflowHelper service instance
50
     * @param ListEntriesHelper   $listHelper     ListEntriesHelper service instance
51
     */
52
    public function __construct(TranslatorInterface $translator, VariableApi $variableApi, WorkflowHelper $workflowHelper, ListEntriesHelper $listHelper)
53
    {
54
        $this->setTranslator($translator);
55
        $this->variableApi = $variableApi;
56
        $this->workflowHelper = $workflowHelper;
57
        $this->listHelper = $listHelper;
58
    }
59
    
60
    /**
61
     * Sets the translator.
62
     *
63
     * @param TranslatorInterface $translator Translator service instance
64
     */
65
    public function setTranslator(/*TranslatorInterface */$translator)
66
    {
67
        $this->translator = $translator;
0 ignored issues
show
Documentation Bug introduced by
$translator is of type object<Zikula\Common\Tra...or\TranslatorInterface>, but the property $translator was declared to be of type object<Zikula\Common\Translator\Translator>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
68
    }
69
    
70
    /**
71
     * Returns a list of custom Twig functions.
72
     *
73
     * @return array
74
     */
75
    public function getFunctions()
76
    {
77
        return [
78
            new \Twig_SimpleFunction('zikularoutesmodule_objectTypeSelector', [$this, 'getObjectTypeSelector']),
79
            new \Twig_SimpleFunction('zikularoutesmodule_templateSelector', [$this, 'getTemplateSelector']),
80
            new \Twig_SimpleFunction('zikularoutesmodule_userVar', [$this, 'getUserVar']),
81
            new \Twig_SimpleFunction('zikularoutesmodule_userAvatar', [$this, 'getUserAvatar'], ['is_safe' => ['html']])
82
        ];
83
    }
84
    
85
    /**
86
     * Returns a list of custom Twig filters.
87
     *
88
     * @return array
89
     */
90
    public function getFilters()
91
    {
92
        return [
93
            new \Twig_SimpleFilter('zikularoutesmodule_listEntry', [$this, 'getListEntry']),
94
            new \Twig_SimpleFilter('zikularoutesmodule_objectState', [$this, 'getObjectState'], ['is_safe' => ['html']])
95
        ];
96
    }
97
    
98
    /**
99
     * The zikularoutesmodule_objectState filter displays the name of a given object's workflow state.
100
     * Examples:
101
     *    {{ item.workflowState|zikularoutesmodule_objectState }}        {# with visual feedback #}
102
     *    {{ item.workflowState|zikularoutesmodule_objectState(false) }} {# no ui feedback #}
103
     *
104
     * @param string  $state      Name of given workflow state
105
     * @param boolean $uiFeedback Whether the output should include some visual feedback about the state
106
     *
107
     * @return string Enriched and translated workflow state ready for display
108
     */
109
    public function getObjectState($state = 'initial', $uiFeedback = true)
110
    {
111
        $stateInfo = $this->workflowHelper->getStateInfo($state);
112
    
113
        $result = $stateInfo['text'];
114
        if (true === $uiFeedback) {
115
            $result = '<span class="label label-' . $stateInfo['ui'] . '">' . $result . '</span>';
116
        }
117
    
118
        return $result;
119
    }
120
    
121
    
122
    /**
123
     * The zikularoutesmodule_listEntry filter displays the name
124
     * or names for a given list item.
125
     * Example:
126
     *     {{ entity.listField|zikularoutesmodule_listEntry('entityName', 'fieldName') }}
127
     *
128
     * @param string $value      The dropdown value to process
129
     * @param string $objectType The treated object type
130
     * @param string $fieldName  The list field's name
131
     * @param string $delimiter  String used as separator for multiple selections
132
     *
133
     * @return string List item name
134
     */
135
    public function getListEntry($value, $objectType = '', $fieldName = '', $delimiter = ', ')
136
    {
137
        if ((empty($value) && $value != '0') || empty($objectType) || empty($fieldName)) {
138
            return $value;
139
        }
140
    
141
        return $this->listHelper->resolve($value, $objectType, $fieldName, $delimiter);
142
    }
143
    
144
    
145
    /**
146
     * The zikularoutesmodule_objectTypeSelector function provides items for a dropdown selector.
147
     *
148
     * @return string The output of the plugin
149
     */
150
    public function getObjectTypeSelector()
151
    {
152
        $result = [];
153
    
154
        $result[] = ['text' => $this->__('Routes'), 'value' => 'route'];
155
    
156
        return $result;
157
    }
158
    
159
    
160
    /**
161
     * The zikularoutesmodule_templateSelector function provides items for a dropdown selector.
162
     *
163
     * @return string The output of the plugin
164
     */
165
    public function getTemplateSelector()
166
    {
167
        $result = [];
168
    
169
        $result[] = ['text' => $this->__('Only item titles'), 'value' => 'itemlist_display.html.twig'];
170
        $result[] = ['text' => $this->__('With description'), 'value' => 'itemlist_display_description.html.twig'];
171
        $result[] = ['text' => $this->__('Custom template'), 'value' => 'custom'];
172
    
173
        return $result;
174
    }
175
    
176
    /**
177
     * Returns the value of a user variable.
178
     *
179
     * @param string     $name    Name of desired property
180
     * @param int        $uid     The user's id
181
     * @param string|int $default The default value
182
     *
183
     * @return string
184
     */
185
    public function getUserVar($name, $uid = -1, $default = '')
186
    {
187
        if (!$uid) {
188
            $uid = -1;
189
        }
190
    
191
        $result = \UserUtil::getVar($name, $uid, $default);
192
    
193
        return $result;
194
    }
195
    
196
    /**
197
     * Display the avatar of a user.
198
     *
199
     * @param int    $uid    The user's id
200
     * @param int    $width  Image width (optional)
201
     * @param int    $height Image height (optional)
202
     * @param int    $size   Gravatar size (optional)
203
     * @param string $rating Gravatar self-rating [g|pg|r|x] see: http://en.gravatar.com/site/implement/images/ (optional)
204
     *
205
     * @return string
206
     */
207
    public function getUserAvatar($uid = 0, $width = 0, $height = 0, $size = 0, $rating = '')
208
    {
209
        $params = ['uid' => $uid];
210
        if ($width > 0) {
211
            $params['width'] = $width;
212
        }
213
        if ($height > 0) {
214
            $params['height'] = $height;
215
        }
216
        if ($size > 0) {
217
            $params['size'] = $size;
218
        }
219
        if ($rating != '') {
220
            $params['rating'] = $rating;
221
        }
222
    
223
        include_once 'lib/legacy/viewplugins/function.useravatar.php';
224
    
225
        $view = \Zikula_View::getInstance('ZikulaRoutesModule');
226
        $result = smarty_function_useravatar($params, $view);
227
    
228
        return $result;
229
    }
230
}
231