AbstractColumn::getReplaceValues()   A
last analyzed

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 0
1
<?php
2
3
namespace ZfcDatagrid\Column;
4
5
use ZfcDatagrid\Column\Formatter\AbstractFormatter;
6
use ZfcDatagrid\Filter;
7
8
abstract class AbstractColumn
9
{
10
    protected $label = '';
11
12
    protected $uniqueId;
13
14
    /**
15
     * @var Type\AbstractType
16
     */
17
    protected $type = null;
18
19
    protected $styles = [];
20
21
    protected $width = 5;
22
23
    protected $isHidden = false;
24
25
    protected $isIdentity = false;
26
27
    protected $userSortEnabled = true;
28
29
    protected $sortDefault = [];
30
31
    protected $sortActive = null;
32
33
    protected $filterDefaultValue = null;
34
35
    protected $filterDefaultOperation = null;
36
37
    /**
38
     * @var null array
39
     */
40
    protected $filterSelectOptions;
41
42
    protected $filterActive = null;
43
44
    protected $filterActiveValue = '';
45
46
    protected $userFilterEnabled = true;
47
48
    protected $translationEnabled = false;
49
50
    protected $replaceValues = [];
51
52
    protected $notReplacedGetEmpty = true;
53
54
    protected $rowClickEnabled = true;
55
56
    protected $rendererParameter = [];
57
58
    /**
59
     * @var AbstractFormatter[]
60
     */
61
    protected $formatters = [];
62
63
    /**
64
     * @param $name
65
     */
66
    public function setLabel($name)
67
    {
68
        $this->label = (string) $name;
69
    }
70
71
    /**
72
     * Get the label.
73
     *
74
     * @return string null
75
     */
76
    public function getLabel()
77
    {
78
        return $this->label;
79
    }
80
81
    /**
82
     * @param $id
83
     */
84
    public function setUniqueId($id)
85
    {
86
        $this->uniqueId = $id;
87
    }
88
89
    /**
90
     * @return mixed
91
     */
92
    public function getUniqueId()
93
    {
94
        return $this->uniqueId;
95
    }
96
97
    /**
98
     * Set the width in "percent"
99
     * It will be calculated to 100% dependend on what is displayed
100
     * If it's a different output mode like Excel it's dependend on the papersize/orientation.
101
     *
102
     * @param number $percent
103
     */
104
    public function setWidth($percent)
105
    {
106
        $this->width = (float) $percent;
0 ignored issues
show
Documentation Bug introduced by
The property $width was declared of type integer, but (double) $percent is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
107
    }
108
109
    /**
110
     * Get the width.
111
     *
112
     * @return number
113
     */
114
    public function getWidth()
115
    {
116
        return $this->width;
117
    }
118
119
    /**
120
     * Hide or show the column.
121
     *
122
     * @param bool $mode
123
     */
124
    public function setHidden($mode = true)
125
    {
126
        $this->isHidden = (bool) $mode;
127
    }
128
129
    /**
130
     * Is this column hidden?
131
     *
132
     * @return bool
133
     */
134
    public function isHidden()
135
    {
136
        return (bool) $this->isHidden;
137
    }
138
139
    /**
140
     * Set this column as primaryKey column.
141
     *
142
     * @param bool $mode
143
     */
144
    public function setIdentity($mode = true)
145
    {
146
        $this->isIdentity = (bool) $mode;
147
148
        // Because IDs are normally hidden
149
        $this->setHidden($mode);
150
    }
151
152
    /**
153
     * Is this a primaryKey column?
154
     *
155
     * @return bool
156
     */
157
    public function isIdentity()
158
    {
159
        return (bool) $this->isIdentity;
160
    }
161
162
    /**
163
     * Set the column type.
164
     *
165
     * @param Type\AbstractType $type
166
     */
167
    public function setType(Type\AbstractType $type)
168
    {
169
        if ($type instanceof Type\Image && $this->hasFormatters() === false) {
170
            $this->addFormatter(new Formatter\Image());
171
            $this->setRowClickDisabled(true);
172
        }
173
174
        $this->type = $type;
175
    }
176
177
    /**
178
     * @return Type\AbstractType
179
     */
180
    public function getType()
181
    {
182
        if (null === $this->type) {
183
            $this->type = new Type\PhpString();
184
        }
185
186
        return $this->type;
187
    }
188
189
    /**
190
     * Set styles.
191
     *
192
     * @param array $styles
193
     */
194
    public function setStyles(array $styles)
195
    {
196
        $this->styles = [];
197
198
        foreach ($styles as $style) {
199
            $this->addStyle($style);
200
        }
201
    }
202
203
    /**
204
     * @param Style\AbstractStyle $style
205
     */
206
    public function addStyle(Style\AbstractStyle $style)
207
    {
208
        $this->styles[] = $style;
209
    }
210
211
    /**
212
     * @return Style\AbstractStyle[]
213
     */
214
    public function getStyles()
215
    {
216
        return $this->styles;
217
    }
218
219
    /**
220
     * @return bool
221
     */
222
    public function hasStyles()
223
    {
224
        if (count($this->styles) > 0) {
225
            return true;
226
        }
227
228
        return false;
229
    }
230
231
    /**
232
     * Is the user allowed to do sort on this column?
233
     *
234
     * @param bool $mode
235
     */
236
    public function setUserSortDisabled($mode = true)
237
    {
238
        $this->userSortEnabled = (bool) !$mode;
239
    }
240
241
    /**
242
     * Is user sort enabled?
243
     *
244
     * @return bool
245
     */
246
    public function isUserSortEnabled()
247
    {
248
        return (bool) $this->userSortEnabled;
249
    }
250
251
    /**
252
     * The data will get sorted by this column (by default)
253
     * If will be changed by the user per request (POST,GET....).
254
     *
255
     * @param int    $priority
256
     * @param string $direction
257
     */
258
    public function setSortDefault($priority = 1, $direction = 'ASC')
259
    {
260
        $this->sortDefault = [
261
            'priority' => $priority,
262
            'sortDirection' => $direction,
263
        ];
264
    }
265
266
    /**
267
     * Get the sort defaults.
268
     *
269
     * @return array
270
     */
271
    public function getSortDefault()
272
    {
273
        return $this->sortDefault;
274
    }
275
276
    /**
277
     * Does this column has sort defaults?
278
     *
279
     * @return bool
280
     */
281
    public function hasSortDefault()
282
    {
283
        if (count($this->sortDefault) > 0) {
284
            return true;
285
        }
286
287
        return false;
288
    }
289
290
    /**
291
     * Set that the data is getting sorted by this columns.
292
     *
293
     * @param string $direction
294
     */
295
    public function setSortActive($direction = 'ASC')
296
    {
297
        $this->sortActive = $direction;
298
    }
299
300
    /**
301
     * @return bool
302
     */
303
    public function isSortActive()
304
    {
305
        if ($this->sortActive !== null) {
306
            return true;
307
        }
308
309
        return false;
310
    }
311
312
    /**
313
     * @return string
314
     */
315
    public function getSortActiveDirection()
316
    {
317
        return $this->sortActive;
318
    }
319
320
    /**
321
     * @param bool $mode
322
     */
323
    public function setUserFilterDisabled($mode = true)
324
    {
325
        $this->userFilterEnabled = (bool) !$mode;
326
    }
327
328
    /**
329
     * Set the default filterung value (used as long no user filtering getting applied)
330
     * Examples
331
     * $grid->setFilterDefaultValue('something');
332
     * $grid->setFilterDefaultValue('>20');.
333
     *
334
     * OPERATORS are ALLOWED (like for the user)
335
     *
336
     * @param string $value
337
     */
338
    public function setFilterDefaultValue($value = null)
339
    {
340
        if ($value != '') {
341
            $this->filterDefaultValue = (string) $value;
342
        }
343
    }
344
345
    /**
346
     * @return string
347
     */
348
    public function getFilterDefaultValue()
349
    {
350
        return $this->filterDefaultValue;
351
    }
352
353
    /**
354
     * @return bool
355
     */
356
    public function hasFilterDefaultValue()
357
    {
358
        if ($this->filterDefaultValue != '') {
359
            return true;
360
        } else {
361
            return false;
362
        }
363
    }
364
365
    /**
366
     * @param string $operation
367
     */
368
    public function setFilterDefaultOperation($operation = Filter::LIKE)
369
    {
370
        $this->filterDefaultOperation = $operation;
371
    }
372
373
    /**
374
     * @return string
375
     */
376
    public function getFilterDefaultOperation()
377
    {
378
        if ($this->filterDefaultOperation != '') {
379
            return $this->filterDefaultOperation;
380
        }
381
382
        return $this->getType()->getFilterDefaultOperation();
383
    }
384
385
    /**
386
     * @param array $options
387
     * @param bool  $noSelect
388
     */
389
    public function setFilterSelectOptions(array $options = null, $noSelect = true)
390
    {
391
        // This work also with options with integer based array index such as
392
        // array(0 => 'zero', 1 => 'once', 2 => 'double', 3 => 'triple'....)
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
393
394
        if (true === $noSelect) {
395
            $options = ['' => '-'] + $options;
396
            $this->setFilterDefaultValue('');
397
        }
398
399
        $this->filterSelectOptions = $options;
400
    }
401
402
    /**
403
     * Unset the filter select options (normal search).
404
     */
405
    public function unsetFilterSelectOptions()
406
    {
407
        $this->filterSelectOptions = null;
408
    }
409
410
    /**
411
     * @return array null
412
     */
413
    public function getFilterSelectOptions()
414
    {
415
        return $this->filterSelectOptions;
416
    }
417
418
    /**
419
     * @return bool
420
     */
421
    public function hasFilterSelectOptions()
422
    {
423
        if (is_array($this->filterSelectOptions)) {
424
            return true;
425
        }
426
427
        return false;
428
    }
429
430
    /**
431
     * @param mixed $value
432
     */
433
    public function setFilterActive($value = '')
434
    {
435
        $this->filterActive = (bool) true;
436
        $this->filterActiveValue = $value;
437
    }
438
439
    /**
440
     * @return bool
441
     */
442
    public function isFilterActive()
443
    {
444
        return $this->filterActive;
445
    }
446
447
    /**
448
     * @return string
449
     */
450
    public function getFilterActiveValue()
451
    {
452
        return $this->filterActiveValue;
453
    }
454
455
    /**
456
     * @return bool
457
     */
458
    public function isUserFilterEnabled()
459
    {
460
        return (bool) $this->userFilterEnabled;
461
    }
462
463
    /**
464
     * Enable data translation.
465
     *
466
     * @param bool $mode
467
     */
468
    public function setTranslationEnabled($mode = true)
469
    {
470
        $this->translationEnabled = (bool) $mode;
471
    }
472
473
    /**
474
     * Is data translation enabled?
475
     *
476
     * @return bool
477
     */
478
    public function isTranslationEnabled()
479
    {
480
        return (bool) $this->translationEnabled;
481
    }
482
483
    /**
484
     * Replace the column values with the applied values.
485
     *
486
     * @param array $values
487
     * @param bool  $notReplacedGetEmpty
488
     */
489
    public function setReplaceValues(array $values, $notReplacedGetEmpty = true)
490
    {
491
        $this->replaceValues = $values;
492
        $this->notReplacedGetEmpty = (bool) $notReplacedGetEmpty;
493
494
        $this->setFilterDefaultOperation(Filter::EQUAL);
495
        $this->setFilterSelectOptions($values);
496
    }
497
498
    /**
499
     * @return bool
500
     */
501
    public function hasReplaceValues()
502
    {
503
        return $this->replaceValues ? true : false;
504
    }
505
506
    /**
507
     * @return array
508
     */
509
    public function getReplaceValues()
510
    {
511
        return $this->replaceValues;
512
    }
513
514
    /**
515
     * @return bool
516
     */
517
    public function notReplacedGetEmpty()
518
    {
519
        return $this->notReplacedGetEmpty;
520
    }
521
522
    /**
523
     * Set parameter for a specific renderer (currently only supported for jqGrid).
524
     *
525
     * @param string $name
526
     * @param mixed  $value
527
     * @param string $rendererType
528
     */
529
    public function setRendererParameter($name, $value, $rendererType = 'jqGrid')
530
    {
531
        if (!isset($this->rendererParameter[$rendererType])) {
532
            $this->rendererParameter[$rendererType] = [];
533
        }
534
535
        $parameters = $this->rendererParameter[$rendererType];
536
        $parameters[$name] = $value;
537
538
        $this->rendererParameter[$rendererType] = $parameters;
539
    }
540
541
    /**
542
     * @param string $rendererName
543
     *
544
     * @return array
545
     */
546
    public function getRendererParameters($rendererName = 'jqGrid')
547
    {
548
        if (!isset($this->rendererParameter[$rendererName])) {
549
            $this->rendererParameter[$rendererName] = [];
550
        }
551
552
        return $this->rendererParameter[$rendererName];
553
    }
554
555
    /**
556
     * Set a template formatter and overwrite other formatter.
557
     *
558
     * @param AbstractFormatter[] $formatters
559
     */
560
    public function setFormatters(array $formatters)
561
    {
562
        $this->formatters = $formatters;
563
    }
564
565
    /**
566
     * Set a template formatter and overwrite other formatter.
567
     *
568
     * @param AbstractFormatter $formatter
569
     *
570
     * @deprecated please use setFormatters
571
     */
572
    public function setFormatter(AbstractFormatter $formatter)
573
    {
574
        trigger_error('Please use setFormatters()', E_USER_DEPRECATED);
575
576
        $this->setFormatters([$formatter]);
577
    }
578
579
    /**
580
     * add a template formatter in the list.
581
     *
582
     * @param AbstractFormatter $formatter
583
     */
584
    public function addFormatter(AbstractFormatter $formatter)
585
    {
586
        $this->formatters[] = $formatter;
587
    }
588
589
    /**
590
     * return a list of different formatter.
591
     *
592
     * @return AbstractFormatter[]
593
     */
594
    public function getFormatters()
595
    {
596
        return $this->formatters;
597
    }
598
599
    /**
600
     * return a list of different formatter.
601
     *
602
     * @return AbstractFormatter[]
603
     *
604
     * @deprecated please use getFormatters
605
     */
606
    public function getFormatter()
607
    {
608
        trigger_error('Please use getFormatters()', E_USER_DEPRECATED);
609
610
        return $this->getFormatters();
611
    }
612
613
    /**
614
     * @return bool
615
     */
616
    public function hasFormatters()
617
    {
618
        if (count($this->formatters) > 0) {
619
            return true;
620
        }
621
622
        return false;
623
    }
624
625
    public function hasFormatter()
626
    {
627
        trigger_error('Please use hasFormatters()', E_USER_DEPRECATED);
628
629
        return $this->hasFormatters();
630
    }
631
632
    /**
633
     * @param bool $mode
634
     */
635
    public function setRowClickDisabled($mode = true)
636
    {
637
        $this->rowClickEnabled = (bool) !$mode;
638
    }
639
640
    /**
641
     * @return bool
642
     */
643
    public function isRowClickEnabled()
644
    {
645
        return $this->rowClickEnabled;
646
    }
647
}
648