Completed
Push — master ( 13a5b7...3bf9bc )
by Elf
05:43
created

DataTable::filename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
     * Get render Closure for an <a> link.
35
     *
36
     * @param  string  $url  "/uri/to/{data}/{full.otherData}"
37
     * @param  string  $content  "{data} content"
38
     * @param  string  $class
39
     * @return Closure
40
     */
41
    protected function linkRender($url = '{data}', $content = '{data}', $class = '')
42
    {
43
        $url = $this->getRenderedJsString($url);
44
        $content = $this->getRenderedJsString($content);
45
        $class = $class ? ' class="'.$class.'"' : '';
46
47
        return function () use ($url, $content, $class) {
48
            return <<<JS
49
function (data, type, full, meta) {
50
    if (type === 'display' && data) {
51
        return '<a href=\"'+ {$url} +'\"{$class}>'+ {$content} +'</a>';
52
    }
53
    return data;
54
}
55
JS;
56
        };
57
    }
58
59
    /**
60
     * Convert PHP string value to a rendered JavaScript string.
61
     *
62
     * @param  string  $string
63
     * @return string
64
     */
65
    protected function getRenderedJsString($string)
66
    {
67
        return "'".str_replace(['{', '}'], ["'+", "+'"], $string)."'";
68
    }
69
70
    /**
71
     * Get filename for export.
72
     *
73
     * @return string
74
     */
75
    protected function filename()
76
    {
77
        return preg_replace('#datatable$#i', '', class_basename($this)).'-'.date('YmdHis');
78
    }
79
80
    /**
81
     * Get default builder parameters.
82
     *
83
     * @return array
84
     */
85
    protected function getBuilderParameters()
86
    {
87
        return [
88
            'order' => [[0, 'desc']],
89
        ];
90
    }
91
}
92