Completed
Push — master ( 215d1a...3112e8 )
by Arjay
01:13
created

Column::renderJs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Yajra\DataTables\Html;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Fluent;
7
use Illuminate\Support\Str;
8
9
/**
10
 * @property string data
11
 * @property string name
12
 * @property string orderable
13
 * @property string searchable
14
 * @property string printable
15
 * @property string exportable
16
 * @property string footer
17
 * @property array attributes
18
 * @see https://datatables.net/reference/option/#columns
19
 */
20
class Column extends Fluent
21
{
22
    /**
23
     * @param array $attributes
24
     */
25
    public function __construct($attributes = [])
26
    {
27
        $attributes['title'] = isset($attributes['title']) ? $attributes['title'] : self::titleFormat($attributes['data']);
28
        $attributes['orderable'] = isset($attributes['orderable']) ? $attributes['orderable'] : true;
29
        $attributes['searchable'] = isset($attributes['searchable']) ? $attributes['searchable'] : true;
30
        $attributes['exportable'] = isset($attributes['exportable']) ? $attributes['exportable'] : true;
31
        $attributes['printable'] = isset($attributes['printable']) ? $attributes['printable'] : true;
32
        $attributes['footer'] = isset($attributes['footer']) ? $attributes['footer'] : '';
33
        $attributes['attributes'] = isset($attributes['attributes']) ? $attributes['attributes'] : [];
34
35
        // Allow methods override attribute value
36
        foreach ($attributes as $attribute => $value) {
37
            $method = 'parse' . ucfirst(strtolower($attribute));
38
            if (! is_null($value) && method_exists($this, $method)) {
39
                $attributes[$attribute] = $this->$method($value);
40
            }
41
        }
42
43
        if (! isset($attributes['name']) && isset($attributes['data'])) {
44
            $attributes['name'] = $attributes['data'];
45
        }
46
47
        parent::__construct($attributes);
48
    }
49
50
    /**
51
     * Format string to title case.
52
     *
53
     * @param string $value
54
     * @return string
55
     */
56
    public static function titleFormat($value)
57
    {
58
        return Str::title(str_replace('_', ' ', $value));
59
    }
60
61
    /**
62
     * Create a computed column that is not searchable/orderable.
63
     *
64
     * @param string $data
65
     * @param string|null $title
66
     * @return Column
67
     */
68
    public static function computed($data, $title = null)
69
    {
70
        if (is_null($title)) {
71
            $title = self::titleFormat($data);
72
        }
73
74
        return static::make($data)->title($title)->orderable(false)->searchable(false);
75
    }
76
77
    /**
78
     * Set column searchable flag.
79
     *
80
     * @param bool $flag
81
     * @return $this
82
     * @see https://datatables.net/reference/option/columns.searchable
83
     */
84
    public function searchable(bool $flag = true)
85
    {
86
        $this->attributes['searchable'] = $flag;
87
88
        return $this;
89
    }
90
91
    /**
92
     * Set column orderable flag.
93
     *
94
     * @param bool $flag
95
     * @return $this
96
     * @see https://datatables.net/reference/option/columns.orderable
97
     */
98
    public function orderable(bool $flag = true)
99
    {
100
        $this->attributes['orderable'] = $flag;
101
102
        return $this;
103
    }
104
105
    /**
106
     * Set column title.
107
     *
108
     * @param string $value
109
     * @return $this
110
     * @see https://datatables.net/reference/option/columns.title
111
     */
112
    public function title($value)
113
    {
114
        $this->attributes['title'] = $value;
115
116
        return $this;
117
    }
118
119
    /**
120
     * Make a new column instance.
121
     *
122
     * @param string $data
123
     * @param string $name
124
     * @return Column
125
     */
126
    public static function make($data, $name = '')
127
    {
128
        $attr = [
129
            'data' => $data,
130
            'name' => $name ?: $data,
131
        ];
132
133
        return new static($attr);
134
    }
135
136
    /**
137
     * Create a checkbox column.
138
     *
139
     * @param string $title
140
     * @return Column
141
     */
142
    public static function checkbox($title = '')
143
    {
144
        return static::make('')
145
                     ->content('')
146
                     ->title($title)
147
                     ->className('select-checkbox')
148
                     ->orderable(false)
149
                     ->searchable(false);
150
    }
151
152
    /**
153
     * Set column class name.
154
     *
155
     * @param string $class
156
     * @return $this
157
     * @see https://datatables.net/reference/option/columns.className
158
     */
159
    public function className($class)
160
    {
161
        $this->attributes['className'] = $class;
162
163
        return $this;
164
    }
165
166
    /**
167
     * Set column default content.
168
     *
169
     * @param string $value
170
     * @return $this
171
     * @see https://datatables.net/reference/option/columns.defaultContent
172
     */
173
    public function content($value)
174
    {
175
        $this->attributes['defaultContent'] = $value;
176
177
        return $this;
178
    }
179
180
    /**
181
     * Set column visible flag.
182
     *
183
     * @param bool $flag
184
     * @return $this
185
     * @see https://datatables.net/reference/option/columns.visible
186
     */
187
    public function visible(bool $flag = true)
188
    {
189
        $this->attributes['visible'] = $flag;
190
191
        return $this;
192
    }
193
194
    /**
195
     * Append a class name to field.
196
     *
197
     * @param string $class
198
     * @return $this
199
     */
200
    public function addClass($class)
201
    {
202
        if (! isset($this->attributes['className'])) {
203
            $this->attributes['className'] = $class;
204
        } else {
205
            $this->attributes['className'] .= " $class";
206
        }
207
208
        return $this;
209
    }
210
211
    /**
212
     * Set column exportable flag.
213
     *
214
     * @param bool $flag
215
     * @return $this
216
     */
217
    public function exportable(bool $flag = true)
218
    {
219
        $this->attributes['exportable'] = $flag;
220
221
        return $this;
222
    }
223
224
    /**
225
     * Set column printable flag.
226
     *
227
     * @param bool $flag
228
     * @return $this
229
     */
230
    public function printable(bool $flag = true)
231
    {
232
        $this->attributes['printable'] = $flag;
233
234
        return $this;
235
    }
236
237
    /**
238
     * Set column width value.
239
     *
240
     * @param int|string $value
241
     * @return $this
242
     * @see https://datatables.net/reference/option/columns.width
243
     */
244
    public function width($value)
245
    {
246
        $this->attributes['width'] = $value;
247
248
        return $this;
249
    }
250
251
    /**
252
     * Set column data option value.
253
     *
254
     * @param string $value
255
     * @return $this
256
     * @see https://datatables.net/reference/option/columns.data
257
     */
258
    public function data($value)
259
    {
260
        $this->attributes['data'] = $value;
261
262
        return $this;
263
    }
264
265
    /**
266
     * Set column name option value.
267
     *
268
     * @param string $value
269
     * @return $this
270
     * @see https://datatables.net/reference/option/columns.name
271
     */
272
    public function name($value)
273
    {
274
        $this->attributes['name'] = $value;
275
276
        return $this;
277
    }
278
279
    /**
280
     * Set column edit field option value.
281
     *
282
     * @param string $value
283
     * @return $this
284
     * @see https://datatables.net/reference/option/columns.editField
285
     */
286
    public function editField($value)
287
    {
288
        $this->attributes['editField'] = $value;
289
290
        return $this;
291
    }
292
293
    /**
294
     * Set column orderData option value.
295
     *
296
     * @param mixed $value
297
     * @return $this
298
     * @see https://datatables.net/reference/option/columns.orderData
299
     */
300
    public function orderData($value)
301
    {
302
        $this->attributes['orderData'] = $value;
303
304
        return $this;
305
    }
306
307
    /**
308
     * Set column orderDataType option value.
309
     *
310
     * @param mixed $value
311
     * @return $this
312
     * @see https://datatables.net/reference/option/columns.orderDataType
313
     */
314
    public function orderDataType($value)
315
    {
316
        $this->attributes['orderDataType'] = $value;
317
318
        return $this;
319
    }
320
321
    /**
322
     * Set column orderSequence option value.
323
     *
324
     * @param mixed $value
325
     * @return $this
326
     * @see https://datatables.net/reference/option/columns.orderSequence
327
     */
328
    public function orderSequence($value)
329
    {
330
        $this->attributes['orderSequence'] = $value;
331
332
        return $this;
333
    }
334
335
    /**
336
     * Set column cellType option value.
337
     *
338
     * @param mixed $value
339
     * @return $this
340
     * @see https://datatables.net/reference/option/columns.cellType
341
     */
342
    public function cellType($value)
343
    {
344
        $this->attributes['cellType'] = $value;
345
346
        return $this;
347
    }
348
349
    /**
350
     * Set column type option value.
351
     *
352
     * @param mixed $value
353
     * @return $this
354
     * @see https://datatables.net/reference/option/columns.type
355
     */
356
    public function type($value)
357
    {
358
        $this->attributes['type'] = $value;
359
360
        return $this;
361
    }
362
363
    /**
364
     * Set column contentPadding option value.
365
     *
366
     * @param mixed $value
367
     * @return $this
368
     * @see https://datatables.net/reference/option/columns.contentPadding
369
     */
370
    public function contentPadding($value)
371
    {
372
        $this->attributes['contentPadding'] = $value;
373
374
        return $this;
375
    }
376
377
    /**
378
     * Set column createdCell option value.
379
     *
380
     * @param mixed $value
381
     * @return $this
382
     * @see https://datatables.net/reference/option/columns.createdCell
383
     */
384
    public function createdCell($value)
385
    {
386
        $this->attributes['createdCell'] = $value;
387
388
        return $this;
389
    }
390
391
    /**
392
     * Use the js renderer "$.fn.dataTable.render.".
393
     *
394
     * @param mixed $value
395
     * @return $this
396
     * @see https://datatables.net/reference/option/columns.render
397
     */
398
    public function renderJs($value)
399
    {
400
        return $this->render('$.fn.dataTable.render.' . $value);
401
    }
402
403
    /**
404
     * Set column renderer.
405
     *
406
     * @param mixed $value
407
     * @return $this
408
     * @see https://datatables.net/reference/option/columns.render
409
     */
410
    public function render($value)
411
    {
412
        $this->attributes['render'] = $this->parseRender($value);
413
414
        return $this;
415
    }
416
417
    /**
418
     * Set column footer.
419
     *
420
     * @param mixed $value
421
     * @return $this
422
     */
423
    public function footer($value)
424
    {
425
        $this->attributes['footer'] = $value;
426
427
        return $this;
428
    }
429
430
    /**
431
     * Parse render attribute.
432
     *
433
     * @param mixed $value
434
     * @return string|null
435
     */
436
    public function parseRender($value)
437
    {
438
        /** @var \Illuminate\Contracts\View\Factory $view */
439
        $view = app('view');
440
        $parameters = [];
441
442
        if (is_array($value)) {
443
            $parameters = Arr::except($value, 0);
444
            $value = $value[0];
445
        }
446
447
        if (is_callable($value)) {
448
            return $value($parameters);
449
        } elseif ($this->isBuiltInRenderFunction($value)) {
450
            return $value;
451
        } elseif ($view->exists($value)) {
452
            return $view->make($value)->with($parameters)->render();
453
        }
454
455
        return $value ? $this->parseRenderAsString($value) : null;
456
    }
457
458
    /**
459
     * Check if given key & value is a valid datatables built-in renderer function.
460
     *
461
     * @param string $value
462
     * @return bool
463
     */
464
    private function isBuiltInRenderFunction($value)
465
    {
466
        if (empty($value)) {
467
            return false;
468
        }
469
470
        return Str::startsWith(trim($value), ['$.fn.dataTable.render']);
471
    }
472
473
    /**
474
     * Display render value as is.
475
     *
476
     * @param mixed $value
477
     * @return string
478
     */
479
    private function parseRenderAsString($value)
480
    {
481
        return "function(data,type,full,meta){return $value;}";
482
    }
483
484
    /**
485
     * @return array
486
     */
487
    public function toArray()
488
    {
489
        return Arr::except($this->attributes, ['printable', 'exportable', 'footer']);
490
    }
491
}
492