Completed
Push — master ( 48cc3d...5640fa )
by Craig
12:32 queued 05:19
created

AbstractTwigExtension   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 221
rs 10
wmc 20
lcom 3
cbo 4

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A setTranslator() 0 4 1
A getFunctions() 0 8 1
A getFilters() 0 7 1
A getObjectState() 0 11 2
B getListEntry() 0 8 5
A getObjectTypeSelector() 0 11 1
A getTemplateSelector() 0 19 1
C getUserAvatar() 0 35 7
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.4 (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\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface;
20
use Zikula\RoutesModule\Helper\ListEntriesHelper;
21
use Zikula\RoutesModule\Helper\WorkflowHelper;
22
23
/**
24
 * Twig extension base class.
25
 */
26
abstract class AbstractTwigExtension extends Twig_Extension
27
{
28
    use TranslatorTrait;
29
    
30
    /**
31
     * @var VariableApi
32
     */
33
    protected $variableApi;
34
    
35
    /**
36
     * @var UserRepositoryInterface
37
     */
38
    protected $userRepository;
39
    
40
    /**
41
     * @var WorkflowHelper
42
     */
43
    protected $workflowHelper;
44
    
45
    /**
46
     * @var ListEntriesHelper
47
     */
48
    protected $listHelper;
49
    
50
    /**
51
     * TwigExtension constructor.
52
     *
53
     * @param TranslatorInterface $translator     Translator service instance
54
     * @param VariableApi         $variableApi    VariableApi service instance
55
     * @param UserRepositoryInterface $userRepository UserRepository service instance
56
     * @param WorkflowHelper      $workflowHelper WorkflowHelper service instance
57
     * @param ListEntriesHelper   $listHelper     ListEntriesHelper service instance
58
     */
59
    public function __construct(
60
        TranslatorInterface $translator,
61
        VariableApi $variableApi,
62
        UserRepositoryInterface $userRepository,
63
        WorkflowHelper $workflowHelper,
64
        ListEntriesHelper $listHelper)
65
    {
66
        $this->setTranslator($translator);
67
        $this->variableApi = $variableApi;
68
        $this->userRepository = $userRepository;
69
        $this->workflowHelper = $workflowHelper;
70
        $this->listHelper = $listHelper;
71
    }
72
    
73
    /**
74
     * Sets the translator.
75
     *
76
     * @param TranslatorInterface $translator Translator service instance
77
     */
78
    public function setTranslator(/*TranslatorInterface */$translator)
79
    {
80
        $this->translator = $translator;
81
    }
82
    
83
    /**
84
     * Returns a list of custom Twig functions.
85
     *
86
     * @return array
87
     */
88
    public function getFunctions()
89
    {
90
        return [
91
            new \Twig_SimpleFunction('zikularoutesmodule_objectTypeSelector', [$this, 'getObjectTypeSelector']),
92
            new \Twig_SimpleFunction('zikularoutesmodule_templateSelector', [$this, 'getTemplateSelector']),
93
            new \Twig_SimpleFunction('zikularoutesmodule_userAvatar', [$this, 'getUserAvatar'], ['is_safe' => ['html']])
94
        ];
95
    }
96
    
97
    /**
98
     * Returns a list of custom Twig filters.
99
     *
100
     * @return array
101
     */
102
    public function getFilters()
103
    {
104
        return [
105
            new \Twig_SimpleFilter('zikularoutesmodule_listEntry', [$this, 'getListEntry']),
106
            new \Twig_SimpleFilter('zikularoutesmodule_objectState', [$this, 'getObjectState'], ['is_safe' => ['html']])
107
        ];
108
    }
109
    
110
    /**
111
     * The zikularoutesmodule_objectState filter displays the name of a given object's workflow state.
112
     * Examples:
113
     *    {{ item.workflowState|zikularoutesmodule_objectState }}        {# with visual feedback #}
114
     *    {{ item.workflowState|zikularoutesmodule_objectState(false) }} {# no ui feedback #}
115
     *
116
     * @param string  $state      Name of given workflow state
117
     * @param boolean $uiFeedback Whether the output should include some visual feedback about the state
118
     *
119
     * @return string Enriched and translated workflow state ready for display
120
     */
121
    public function getObjectState($state = 'initial', $uiFeedback = true)
122
    {
123
        $stateInfo = $this->workflowHelper->getStateInfo($state);
124
    
125
        $result = $stateInfo['text'];
126
        if (true === $uiFeedback) {
127
            $result = '<span class="label label-' . $stateInfo['ui'] . '">' . $result . '</span>';
128
        }
129
    
130
        return $result;
131
    }
132
    
133
    
134
    /**
135
     * The zikularoutesmodule_listEntry filter displays the name
136
     * or names for a given list item.
137
     * Example:
138
     *     {{ entity.listField|zikularoutesmodule_listEntry('entityName', 'fieldName') }}
139
     *
140
     * @param string $value      The dropdown value to process
141
     * @param string $objectType The treated object type
142
     * @param string $fieldName  The list field's name
143
     * @param string $delimiter  String used as separator for multiple selections
144
     *
145
     * @return string List item name
146
     */
147
    public function getListEntry($value, $objectType = '', $fieldName = '', $delimiter = ', ')
148
    {
149
        if ((empty($value) && $value != '0') || empty($objectType) || empty($fieldName)) {
150
            return $value;
151
        }
152
    
153
        return $this->listHelper->resolve($value, $objectType, $fieldName, $delimiter);
154
    }
155
    
156
    
157
    /**
158
     * The zikularoutesmodule_objectTypeSelector function provides items for a dropdown selector.
159
     *
160
     * @return string The output of the plugin
161
     */
162
    public function getObjectTypeSelector()
163
    {
164
        $result = [];
165
    
166
        $result[] = [
167
            'text' => $this->__('Routes'),
168
            'value' => 'route'
169
        ];
170
    
171
        return $result;
172
    }
173
    
174
    
175
    /**
176
     * The zikularoutesmodule_templateSelector function provides items for a dropdown selector.
177
     *
178
     * @return string The output of the plugin
179
     */
180
    public function getTemplateSelector()
181
    {
182
        $result = [];
183
    
184
        $result[] = [
185
            'text' => $this->__('Only item titles'),
186
            'value' => 'itemlist_display.html.twig'
187
        ];
188
        $result[] = [
189
            'text' => $this->__('With description'),
190
            'value' => 'itemlist_display_description.html.twig'
191
        ];
192
        $result[] = [
193
            'text' => $this->__('Custom template'),
194
            'value' => 'custom'
195
        ];
196
    
197
        return $result;
198
    }
199
    
200
    /**
201
     * Display the avatar of a user.
202
     *
203
     * @param int|string $uid    The user's id or name
204
     * @param int        $width  Image width (optional)
205
     * @param int        $height Image height (optional)
206
     * @param int        $size   Gravatar size (optional)
207
     * @param string     $rating Gravatar self-rating [g|pg|r|x] see: http://en.gravatar.com/site/implement/images/ (optional)
208
     *
209
     * @return string
210
     */
211
    public function getUserAvatar($uid = 0, $width = 0, $height = 0, $size = 0, $rating = '')
212
    {
213
        if (!is_numeric($uid)) {
214
            $limit = 1;
215
            $filter = [
216
                'uname' => ['operator' => '=', 'operand' => $uid]
217
            ];
218
            $results = $this->userRepository->query($filter, [], $limit);
219
            if (!count($results)) {
220
                return '';
221
            }
222
    
223
            $uid = $results->getIterator()->getArrayCopy()[0]->getUname();
224
        }
225
        $params = ['uid' => $uid];
226
        if ($width > 0) {
227
            $params['width'] = $width;
228
        }
229
        if ($height > 0) {
230
            $params['height'] = $height;
231
        }
232
        if ($size > 0) {
233
            $params['size'] = $size;
234
        }
235
        if ($rating != '') {
236
            $params['rating'] = $rating;
237
        }
238
    
239
        include_once 'lib/legacy/viewplugins/function.useravatar.php';
240
    
241
        $view = \Zikula_View::getInstance('ZikulaRoutesModule');
242
        $result = smarty_function_useravatar($params, $view);
243
    
244
        return $result;
245
    }
246
}
247