Completed
Push — master ( ec0b76...22789e )
by Craig
06:04
created

AbstractTwigExtension::getUserAvatar()   C

Complexity

Conditions 7
Paths 33

Size

Total Lines 40
Code Lines 25

Duplication

Lines 40
Ratio 100 %

Importance

Changes 0
Metric Value
dl 40
loc 40
c 0
b 0
f 0
cc 7
eloc 25
nc 33
nop 5
rs 6.7272
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.5 (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\ApiInterface\VariableApiInterface;
19
use Zikula\RoutesModule\Helper\ListEntriesHelper;
20
use Zikula\RoutesModule\Helper\EntityDisplayHelper;
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 VariableApiInterface
32
     */
33
    protected $variableApi;
34
    
35
    /**
36
     * @var EntityDisplayHelper
37
     */
38
    protected $entityDisplayHelper;
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 VariableApiInterface $variableApi    VariableApi service instance
55
     * @param EntityDisplayHelper $entityDisplayHelper EntityDisplayHelper 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
        VariableApiInterface $variableApi,
62
        EntityDisplayHelper $entityDisplayHelper,
63
        WorkflowHelper $workflowHelper,
64
        ListEntriesHelper $listHelper)
65
    {
66
        $this->setTranslator($translator);
67
        $this->variableApi = $variableApi;
68
        $this->entityDisplayHelper = $entityDisplayHelper;
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
        ];
94
    }
95
    
96
    /**
97
     * Returns a list of custom Twig filters.
98
     *
99
     * @return array
100
     */
101 View Code Duplication
    public function getFilters()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103
        return [
104
            new \Twig_SimpleFilter('zikularoutesmodule_listEntry', [$this, 'getListEntry']),
105
            new \Twig_SimpleFilter('zikularoutesmodule_formattedTitle', [$this, 'getFormattedEntityTitle']),
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
     * The zikularoutesmodule_formattedTitle filter outputs a formatted title for a given entity.
202
     * Example:
203
     *     {{ myPost|zikularoutesmodule_formattedTitle }}
204
     *
205
     * @param object $entity The given entity instance
206
     *
207
     * @return string The formatted title
208
     */
209
    public function getFormattedEntityTitle($entity)
210
    {
211
        return $this->entityDisplayHelper->getFormattedTitle($entity);
212
    }
213
}
214