Completed
Pull Request — master (#270)
by
unknown
27:04 queued 11:59
created

AbstractAction::setTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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