Completed
Push — master ( 210fe7...1a3f10 )
by Elf
02:31
created

DataTable::linkRender()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 15
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
crap 2
1
<?php
2
3
namespace ElfSundae\Laravel\Datatables\Services;
4
5
use Yajra\Datatables\Services\DataTable as BaseDataTable;
6
7
abstract class DataTable extends BaseDataTable
8
{
9
    /**
10
     * Get attributes for a "static" column that can not be
11
     * ordered, searched, or exported.
12
     *
13
     * @param  string  $name
14
     * @param  array  $attributes
15
     * @return $this
16
     */
17
    protected function staticColumn($name, array $attributes = [])
18
    {
19
        return array_merge([
20
            'data' => $name,
21
            'name' => $name,
22
            'title' => $this->builder()->getQualifiedTitle($name),
23
            'defaultContent' => '',
24
            'render' => null,
25
            'orderable' => false,
26
            'searchable' => false,
27
            'exportable' => false,
28
            'printable' => true,
29
            'footer' => '',
30
        ], $attributes);
31
    }
32
33
    /**
34
     * Return a render Closure for <a> link.
35
     *
36
     * @param  string  $url  "/uri/to/{data}/{full.otherData}"
37
     * @param  string  $content
38
     * @return Closure
39
     */
40
    protected function linkRender($url = '{data}', $content = '{data}')
41
    {
42
        $url = $this->parseForEmbedJsString($url);
43
        $content = $this->parseForEmbedJsString($content);
44
45
        return function () use ($url, $content) {
46
            return <<<JS
47
function (data, type, full, meta) {
48
    if (type === 'display' && data) {
49
        return '<a href=\"'+ {$url} +'\">'+ {$content} +'</a>';
50
    }
51
    return data;
52
}
53
JS;
54
        };
55
    }
56
57
    /**
58
     * Parse the given string, return the emmbed Javascript string,
59
     * for JS string concat.
60
     *
61
     * @param  string  $string
62
     * @return string
63
     */
64
    protected function parseForEmbedJsString($string)
65
    {
66
        return "'".str_replace(['{', '}'], ["'+", "+'"], $string)."'";
67
    }
68
69
    /**
70
     * Get filename for export.
71
     *
72
     * @return string
73
     */
74
    protected function filename()
75
    {
76
        return preg_replace('#datatable$#i', '', class_basename($this)).'-'.date('YmdHis');
77
    }
78
79
    /**
80
     * Get default builder parameters.
81
     *
82
     * @return array
83
     */
84
    protected function getBuilderParameters()
85
    {
86
        return [
87
            'order' => [[0, 'desc']],
88
        ];
89
    }
90
}
91