Completed
Push — master ( b8f763...47a15d )
by Craig
14:22 queued 07:28
created

AbstractTwigExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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