Completed
Push — master ( 354c04...4e6a57 )
by Arjay
02:34
created

Builder::addCheckbox()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 19
rs 9.4285
cc 1
eloc 14
nc 1
nop 1
1
<?php
2
3
namespace Yajra\Datatables\Html;
4
5
use Collective\Html\FormBuilder;
6
use Collective\Html\HtmlBuilder;
7
use Illuminate\Contracts\Config\Repository;
8
use Illuminate\Contracts\View\Factory;
9
use Illuminate\Routing\UrlGenerator;
10
use Illuminate\Support\Collection;
11
use Illuminate\Support\Str;
12
13
/**
14
 * Class Builder
15
 *
16
 * @package Yajra\Datatables\Html
17
 */
18
class Builder
19
{
20
    /**
21
     * @var Collection
22
     */
23
    public $collection;
24
25
    /**
26
     * @var Repository
27
     */
28
    public $config;
29
30
    /**
31
     * @var Factory
32
     */
33
    public $view;
34
35
    /**
36
     * @var HtmlBuilder
37
     */
38
    public $html;
39
40
    /**
41
     * @var UrlGenerator
42
     */
43
    public $url;
44
45
    /**
46
     * @var FormBuilder
47
     */
48
    public $form;
49
50
    /**
51
     * @var string|array
52
     */
53
    protected $ajax = '';
54
55
    /**
56
     * @var array
57
     */
58
    protected $tableAttributes = ['class' => 'table', 'id' => 'dataTableBuilder'];
59
60
    /**
61
     * @var string
62
     */
63
    protected $template = '';
64
65
    /**
66
     * @var array
67
     */
68
    protected $attributes = [];
69
70
    /**
71
     * @param Repository $config
72
     * @param Factory $view
73
     * @param HtmlBuilder $html
74
     * @param UrlGenerator $url
75
     * @param FormBuilder $form
76
     */
77
    public function __construct(
78
        Repository $config,
79
        Factory $view,
80
        HtmlBuilder $html,
81
        UrlGenerator $url,
82
        FormBuilder $form
83
    ) {
84
        $this->config     = $config;
85
        $this->view       = $view;
86
        $this->html       = $html;
87
        $this->url        = $url;
88
        $this->collection = new Collection;
89
        $this->form       = $form;
90
    }
91
92
    /**
93
     * Generate DataTable javascript.
94
     *
95
     * @param  null $script
96
     * @param  array $attributes
97
     * @return string
98
     */
99
    public function scripts($script = null, array $attributes = ['type' => 'text/javascript'])
100
    {
101
        $script = $script ?: $this->generateScripts();
102
103
        return '<script' . $this->html->attributes($attributes) . '>' . $script . '</script>' . PHP_EOL;
104
    }
105
106
    /**
107
     * Get generated raw scripts.
108
     *
109
     * @return string
110
     */
111
    public function generateScripts()
112
    {
113
        $args = array_merge(
114
            $this->attributes, [
115
                'ajax'    => $this->ajax,
116
                'columns' => $this->collection->toArray(),
117
            ]
118
        );
119
120
        $parameters = $this->parameterize($args);
121
122
        return sprintf(
123
            $this->template(),
124
            $this->tableAttributes['id'], $parameters
125
        );
126
    }
127
128
    /**
129
     * Generate datatable js parameters.
130
     *
131
     * @param  array $attributes
132
     * @return string
133
     */
134
    public function parameterize($attributes = [])
135
    {
136
        $parameters       = (new Parameters($attributes))->toArray();
137
        $column_functions = [];
138
139
        foreach ($parameters['columns'] as $i => $column) {
140
            unset($parameters['columns'][$i]['exportable']);
141
            unset($parameters['columns'][$i]['printable']);
142
143
            if (isset($column['render'])) {
144
                $column_functions[$i]                = $column['render'];
145
                $parameters['columns'][$i]['render'] = "#column_function.{$i}#";
146
            }
147
        }
148
149
        $json = json_encode($parameters);
150
151
        foreach ($column_functions as $i => $function) {
152
            $json = str_replace("\"#column_function.{$i}#\"", $function, $json);
153
        }
154
155
        return $json;
156
    }
157
158
    /**
159
     * Get javascript template to use.
160
     *
161
     * @return string
162
     */
163
    protected function template()
164
    {
165
        return $this->view->make(
166
            $this->template ?: $this->config->get('datatables.script_template', 'datatables::script')
167
        )->render();
168
    }
169
170
    /**
171
     * Add a column in collection using attributes.
172
     *
173
     * @param  array $attributes
174
     * @return $this
175
     */
176
    public function addColumn(array $attributes)
177
    {
178
        $this->collection->push(new Column($attributes));
179
180
        return $this;
181
    }
182
183
    /**
184
     * Add a Column object in collection.
185
     *
186
     * @param \Yajra\Datatables\Html\Column $column
187
     * @return $this
188
     */
189
    public function add(Column $column)
190
    {
191
        $this->collection->push($column);
192
193
        return $this;
194
    }
195
196
    /**
197
     * Set datatables columns from array definition.
198
     *
199
     * @param array $columns
200
     * @return $this
201
     */
202
    public function columns(array $columns)
203
    {
204
        foreach ($columns as $key => $value) {
205
            if (is_array($value)) {
206
                $attributes = array_merge(['name' => $key, 'data' => $key], $this->setTitle($key, $value));
207
            } else {
208
                $attributes = [
209
                    'name'  => $value,
210
                    'data'  => $value,
211
                    'title' => $this->getQualifiedTitle($value),
212
                ];
213
            }
214
215
            $this->collection->push(new Column($attributes));
216
        }
217
218
        return $this;
219
    }
220
221
    /**
222
     * Set title attribute of an array if not set.
223
     *
224
     * @param string $title
225
     * @param array $attributes
226
     * @return array
227
     */
228
    public function setTitle($title, array $attributes)
229
    {
230
        if (! isset($attributes['title'])) {
231
            $attributes['title'] = $this->getQualifiedTitle($title);
232
        }
233
234
        return $attributes;
235
    }
236
237
    /**
238
     * Convert string into a readable title.
239
     *
240
     * @param string $title
241
     * @return string
242
     */
243
    public function getQualifiedTitle($title)
244
    {
245
        return Str::title(str_replace(['.', '_'], ' ', Str::snake($title)));
246
    }
247
248
    /**
249
     * Add a checkbox column.
250
     *
251
     * @param  array $attributes
252
     * @return $this
253
     */
254
    public function addCheckbox(array $attributes = [])
255
    {
256
        $attributes = array_merge(
257
            [
258
                'defaultContent' => '<input type="checkbox" ' . $this->html->attributes($attributes) . '/>',
259
                'title'          => $this->form->checkbox('', '', false, ['id' => 'dataTablesCheckbox']),
260
                'data'           => 'checkbox',
261
                'name'           => 'checkbox',
262
                'orderable'      => false,
263
                'searchable'     => false,
264
                'exportable'     => false,
265
                'printable'      => true,
266
                'width'          => '10px',
267
            ], $attributes
268
        );
269
        $this->collection->push(new Column($attributes));
270
271
        return $this;
272
    }
273
274
    /**
275
     * Add a action column.
276
     *
277
     * @param  array $attributes
278
     * @return $this
279
     */
280
    public function addAction(array $attributes = [])
281
    {
282
        $attributes = array_merge(
283
            [
284
                'defaultContent' => '',
285
                'data'           => 'action',
286
                'name'           => 'action',
287
                'title'          => 'Action',
288
                'render'         => null,
289
                'orderable'      => false,
290
                'searchable'     => false,
291
                'exportable'     => false,
292
                'printable'      => true,
293
            ], $attributes
294
        );
295
        $this->collection->push(new Column($attributes));
296
297
        return $this;
298
    }
299
300
    /**
301
     * Setup ajax parameter
302
     *
303
     * @param  string|array $attributes
304
     * @return $this
305
     */
306
    public function ajax($attributes)
307
    {
308
        $this->ajax = $attributes;
309
310
        return $this;
311
    }
312
313
    /**
314
     * Generate DataTable's table html.
315
     *
316
     * @param  array $attributes
317
     * @return string
318
     */
319
    public function table(array $attributes = [])
320
    {
321
        $this->tableAttributes = array_merge($this->tableAttributes, $attributes);
322
323
        return '<table ' . $this->html->attributes($this->tableAttributes) . '></table>';
324
    }
325
326
    /**
327
     * Configure DataTable's parameters.
328
     *
329
     * @param  array $attributes
330
     * @return $this
331
     */
332
    public function parameters(array $attributes = [])
333
    {
334
        $this->attributes = array_merge($this->attributes, $attributes);
335
336
        return $this;
337
    }
338
339
    /**
340
     * Set custom javascript template.
341
     *
342
     * @param string $template
343
     * @return $this
344
     */
345
    public function setTemplate($template)
346
    {
347
        $this->template = $template;
348
349
        return $this;
350
    }
351
352
    /**
353
     * Get collection of columns.
354
     *
355
     * @return Collection
356
     */
357
    public function getColumns()
358
    {
359
        return $this->collection;
360
    }
361
}
362