Completed
Push — halfpastfouram-master ( 90521d )
by Martin
14:17
created

AbstractAction::getColumnValuePlaceholder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace ZfcDatagrid\Column\Action;
3
4
use ZfcDatagrid\Column;
5
use ZfcDatagrid\Column\AbstractColumn;
6
use ZfcDatagrid\Filter;
7
8
abstract class AbstractAction
9
{
10
    const ROW_ID_PLACEHOLDER = ':rowId:';
11
12
    /**
13
     * @var \ZfcDatagrid\Column\AbstractColumn[]
14
     */
15
    protected $linkColumnPlaceholders = [];
16
17
    /**
18
     * @var array
19
     */
20
    protected $htmlAttributes = [];
21
22
    /**
23
     * @var string
24
     */
25
    protected $showOnValueOperator = 'OR';
26
27
    /**
28
     * @var string
29
     */
30
    protected $route;
31
32
    /**
33
     * @var array
34
     */
35
    protected $routeParams = [];
36
37
    /**
38
     * @var array
39
     */
40
    protected $showOnValues = [];
41
42
    public function __construct()
43
    {
44
        $this->setLink('#');
45
    }
46
47
    /**
48
     * Set the link.
49
     *
50
     * @param string $href
51
     */
52
    public function setLink($href)
53
    {
54
        $this->setAttribute('href', $href);
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getLink()
61
    {
62
        return $this->getAttribute('href');
63
    }
64
65
    /**
66
     * @param string $route
67
     */
68
    public function setRoute($route)
69
    {
70
        $this->route = $route;
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getRoute()
77
    {
78
        return $this->route;
79
    }
80
81
    /**
82
     * @param array $params
83
     */
84
    public function setRouteParams(array $params)
85
    {
86
        $this->routeParams = $params;
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    public function getRouteParams()
93
    {
94
        return $this->routeParams;
95
    }
96
97
    /**
98
     * This is needed public for rowClickAction...
99
     *
100
     * @param array $row
101
     *
102
     * @return string
103
     */
104
    public function getLinkReplaced(array $row)
105
    {
106
        $link = $this->getLink();
107
108
        // Replace placeholders
109 View Code Duplication
        if (strpos($this->getLink(), self::ROW_ID_PLACEHOLDER) !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
110
            $id = '';
111
            if (isset($row['idConcated'])) {
112
                $id = $row['idConcated'];
113
            }
114
            $link = str_replace(self::ROW_ID_PLACEHOLDER, $id, $link);
115
        }
116
117 View Code Duplication
        foreach ($this->getLinkColumnPlaceholders() as $col) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
118
            $link = str_replace(':' . $col->getUniqueId() . ':', $row[$col->getUniqueId()], $link);
119
        }
120
121
        return $link;
122
    }
123
124
    /**
125
     * Get the column row value placeholder
126
     * $action->setLink('/myLink/something/id/'.$action->getRowIdPlaceholder().'/something/'.$action->getColumnRowPlaceholder($myCol));.
127
     *
128
     * @param AbstractColumn $col
129
     *
130
     * @return string
131
     */
132
    public function getColumnValuePlaceholder(AbstractColumn $col)
133
    {
134
        $this->linkColumnPlaceholders[] = $col;
135
136
        return ':' . $col->getUniqueId() . ':';
137
    }
138
139
    /**
140
     * @return \ZfcDatagrid\Column\AbstractColumn[]
141
     */
142
    public function getLinkColumnPlaceholders()
143
    {
144
        return $this->linkColumnPlaceholders;
145
    }
146
147
    /**
148
     * Returns the rowId placeholder
149
     * Can be used e.g.
150
     * $action->setLink('/myLink/something/id/'.$action->getRowIdPlaceholder());.
151
     *
152
     * @return string
153
     */
154
    public function getRowIdPlaceholder()
155
    {
156
        return self::ROW_ID_PLACEHOLDER;
157
    }
158
159
    /**
160
     * Set a HTML attributes.
161
     *
162
     * @param string $name
163
     * @param string $value
164
     */
165
    public function setAttribute($name, $value)
166
    {
167
        $this->htmlAttributes[$name] = (string) $value;
168
    }
169
170
    /**
171
     * Get a HTML attribute.
172
     *
173
     * @param string $name
174
     *
175
     * @return string
176
     */
177
    public function getAttribute($name)
178
    {
179
        if (isset($this->htmlAttributes[$name])) {
180
            return $this->htmlAttributes[$name];
181
        }
182
183
        return '';
184
    }
185
186
    /**
187
     * Removes an HTML attribute.
188
     *
189
     * @param string $name
190
     */
191
    public function removeAttribute($name)
192
    {
193
        if (isset($this->htmlAttributes[$name])) {
194
            unset($this->htmlAttributes[$name]);
195
        }
196
    }
197
198
    /**
199
     * Get all HTML attributes.
200
     *
201
     * @return array
202
     */
203
    public function getAttributes()
204
    {
205
        return $this->htmlAttributes;
206
    }
207
208
    /**
209
     * Get the string version of the attributes.
210
     *
211
     * @param array $row
212
     *
213
     * @return string
214
     */
215
    protected function getAttributesString(array $row)
216
    {
217
        $attributes = [];
218 View Code Duplication
        foreach ($this->getAttributes() as $attrKey => $attrValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
219
            if ('href' === $attrKey) {
220
                $attrValue = $this->getLinkReplaced($row);
221
            }
222
            $attributes[] = $attrKey . '="' . $attrValue . '"';
223
        }
224
225
        return implode(' ', $attributes);
226
    }
227
228
    /**
229
     * Set the title attribute.
230
     *
231
     * @param string $name
232
     */
233
    public function setTitle($name)
234
    {
235
        $this->setAttribute('title', $name);
236
    }
237
238
    /**
239
     * Get the title attribute.
240
     *
241
     * @return string
242
     */
243
    public function getTitle()
244
    {
245
        return $this->getAttribute('title');
246
    }
247
248
    /**
249
     * Add a css class.
250
     *
251
     * @param string $className
252
     */
253
    public function addClass($className)
254
    {
255
        $attr = $this->getAttribute('class');
256
        if ($attr != '') {
257
            $attr .= ' ';
258
        }
259
        $attr .= (string) $className;
260
261
        $this->setAttribute('class', $attr);
262
    }
263
264
    /**
265
     * Display the values with AND or OR (if multiple showOnValues are defined).
266
     *
267
     * @param string $operator
268
     */
269 View Code Duplication
    public function setShowOnValueOperator($operator = 'OR')
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...
270
    {
271
        if ($operator != 'AND' && $operator != 'OR') {
272
            throw new \InvalidArgumentException('not allowed operator: "' . $operator . '" (AND / OR is allowed)');
273
        }
274
275
        $this->showOnValueOperator = (string) $operator;
276
    }
277
278
    /**
279
     * Get the show on value operator, e.g.
280
     * OR, AND.
281
     *
282
     * @return string
283
     */
284
    public function getShowOnValueOperator()
285
    {
286
        return $this->showOnValueOperator;
287
    }
288
289
    /**
290
     * Show this action only on the values defined.
291
     *
292
     * @param Column\AbstractColumn $col
293
     * @param string                $value
294
     * @param string                $comparison
295
     */
296
    public function addShowOnValue(Column\AbstractColumn $col, $value = null, $comparison = Filter::EQUAL)
297
    {
298
        $this->showOnValues[] = [
299
            'column'     => $col,
300
            'value'      => $value,
301
            'comparison' => $comparison,
302
        ];
303
    }
304
305
    /**
306
     * @return array
307
     */
308
    public function getShowOnValues()
309
    {
310
        return $this->showOnValues;
311
    }
312
313
    /**
314
     * @return bool
315
     */
316
    public function hasShowOnValues()
317
    {
318
        if (count($this->showOnValues) > 0) {
319
            return true;
320
        }
321
322
        return false;
323
    }
324
325
    /**
326
     * Display this action on this row?
327
     *
328
     * @param array $row
329
     *
330
     * @return bool
331
     */
332 View Code Duplication
    public function isDisplayed(array $row)
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...
333
    {
334
        if ($this->hasShowOnValues() === false) {
335
            return true;
336
        }
337
338
        $isDisplayed = false;
339
        foreach ($this->getShowOnValues() as $rule) {
340
            $value = '';
341
            if (isset($row[$rule['column']->getUniqueId()])) {
342
                $value = $row[$rule['column']->getUniqueId()];
343
            }
344
345
            if ($rule['value'] instanceof AbstractColumn) {
346
                if (isset($row[$rule['value']->getUniqueId()])) {
347
                    $ruleValue = $row[$rule['value']->getUniqueId()];
348
                } else {
349
                    $ruleValue = '';
350
                }
351
            } else {
352
                $ruleValue = $rule['value'];
353
            }
354
355
            $isDisplayedMatch = Filter::isApply($value, $ruleValue, $rule['comparison']);
356
            if ($this->getShowOnValueOperator() == 'OR' && true === $isDisplayedMatch) {
357
                // For OR one match is enough
358
                return true;
359
            } elseif ($this->getShowOnValueOperator() == 'AND' && false === $isDisplayedMatch) {
360
                return false;
361
            } else {
362
                $isDisplayed = $isDisplayedMatch;
363
            }
364
        }
365
366
        return $isDisplayed;
367
    }
368
369
    /**
370
     * Get the HTML from the type.
371
     *
372
     * @return string
373
     */
374
    abstract protected function getHtmlType();
375
376
    /**
377
     * @param array $row
378
     *
379
     * @return string
380
     */
381
    public function toHtml(array $row)
382
    {
383
        return '<a ' . $this->getAttributesString($row) . '>' . $this->getHtmlType() . '</a>';
384
    }
385
}
386