Completed
Push — master ( a7cf71...06c6ea )
by Craig
06:43
created

AbstractTwigExtension::getImageThumb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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