Completed
Push — master ( bb16e7...820610 )
by Arjay
01:46
created

BaseEngine::setRowAttr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\Datatables\Engines;
4
5
use Illuminate\Contracts\Logging\Log;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Support\Str;
8
use Yajra\Datatables\Contracts\DataTableEngine;
9
use Yajra\Datatables\Exception;
10
use Yajra\Datatables\Helper;
11
use Yajra\Datatables\Processors\DataProcessor;
12
use Yajra\Datatables\Transformers\FractalTransformer;
13
14
/**
15
 * Class BaseEngine.
16
 *
17
 * @package Yajra\Datatables\Engines
18
 * @author  Arjay Angeles <[email protected]>
19
 */
20
abstract class BaseEngine implements DataTableEngine
21
{
22
    /**
23
     * Datatables Request object.
24
     *
25
     * @var \Yajra\Datatables\Request
26
     */
27
    public $request;
28
29
    /**
30
     * @var \Illuminate\Contracts\Logging\Log
31
     */
32
    protected $logger;
33
34
    /**
35
     * Array of result columns/fields.
36
     *
37
     * @var array
38
     */
39
    protected $columns = [];
40
41
    /**
42
     * DT columns definitions container (add/edit/remove/filter/order/escape).
43
     *
44
     * @var array
45
     */
46
    protected $columnDef = [
47
        'index'  => false,
48
        'append' => [],
49
        'edit'   => [],
50
        'filter' => [],
51
        'order'  => [],
52
    ];
53
54
    /**
55
     * Extra/Added columns.
56
     *
57
     * @var array
58
     */
59
    protected $extraColumns = [];
60
61
    /**
62
     * Total records.
63
     *
64
     * @var int
65
     */
66
    protected $totalRecords = 0;
67
68
    /**
69
     * Total filtered records.
70
     *
71
     * @var int
72
     */
73
    protected $filteredRecords = 0;
74
75
    /**
76
     * Auto-filter flag.
77
     *
78
     * @var bool
79
     */
80
    protected $autoFilter = true;
81
82
    /**
83
     * Callback to override global search.
84
     *
85
     * @var callable
86
     */
87
    protected $filterCallback;
88
89
    /**
90
     * Parameters to passed on filterCallback.
91
     *
92
     * @var mixed
93
     */
94
    protected $filterCallbackParameters;
95
96
    /**
97
     * DT row templates container.
98
     *
99
     * @var array
100
     */
101
    protected $templates = [
102
        'DT_RowId'    => '',
103
        'DT_RowClass' => '',
104
        'DT_RowData'  => [],
105
        'DT_RowAttr'  => [],
106
    ];
107
108
    /**
109
     * Output transformer.
110
     *
111
     * @var mixed
112
     */
113
    protected $transformer = null;
114
115
    /**
116
     * [internal] Track if any filter was applied for at least one column.
117
     *
118
     * @var boolean
119
     */
120
    protected $isFilterApplied = false;
121
122
    /**
123
     * Fractal serializer class.
124
     *
125
     * @var mixed
126
     */
127
    protected $serializer = null;
128
129
    /**
130
     * Custom ordering callback.
131
     *
132
     * @var callable
133
     */
134
    protected $orderCallback;
135
136
    /**
137
     * Skip paginate as needed.
138
     *
139
     * @var bool
140
     */
141
    protected $skipPaging = false;
142
143
    /**
144
     * Array of data to append on json response.
145
     *
146
     * @var array
147
     */
148
    protected $appends = [];
149
150
    /**
151
     * Flag for ordering NULLS LAST option.
152
     *
153
     * @var bool
154
     */
155
    protected $nullsLast = false;
156
157
    /**
158
     * @var \Yajra\Datatables\Config
159
     */
160
    protected $config;
161
162
    /**
163
     * Add column in collection.
164
     *
165
     * @param string          $name
166
     * @param string|callable $content
167
     * @param bool|int        $order
168
     * @return $this
169
     */
170
    public function addColumn($name, $content, $order = false)
171
    {
172
        $this->extraColumns[] = $name;
173
174
        $this->columnDef['append'][] = ['name' => $name, 'content' => $content, 'order' => $order];
175
176
        return $this;
177
    }
178
179
    /**
180
     * Add DT row index column on response.
181
     *
182
     * @return $this
183
     */
184
    public function addIndexColumn()
185
    {
186
        $this->columnDef['index'] = true;
187
188
        return $this;
189
    }
190
191
    /**
192
     * Edit column's content.
193
     *
194
     * @param string          $name
195
     * @param string|callable $content
196
     * @return $this
197
     */
198
    public function editColumn($name, $content)
199
    {
200
        $this->columnDef['edit'][] = ['name' => $name, 'content' => $content];
201
202
        return $this;
203
    }
204
205
    /**
206
     * Remove column from collection.
207
     *
208
     * @return $this
209
     */
210
    public function removeColumn()
211
    {
212
        $names                     = func_get_args();
213
        $this->columnDef['excess'] = array_merge($this->columnDef['excess'], $names);
214
215
        return $this;
216
    }
217
218
    /**
219
     * Declare columns to escape values.
220
     *
221
     * @param string|array $columns
222
     * @return $this
223
     */
224
    public function escapeColumns($columns = '*')
225
    {
226
        $this->columnDef['escape'] = $columns;
227
228
        return $this;
229
    }
230
231
    /**
232
     * Set columns that should not be escaped.
233
     *
234
     * @param array $columns
235
     * @return $this
236
     */
237
    public function rawColumns(array $columns)
238
    {
239
        $this->columnDef['raw'] = $columns;
240
241
        return $this;
242
    }
243
244
    /**
245
     * Sets DT_RowClass template.
246
     * result: <tr class="output_from_your_template">.
247
     *
248
     * @param string|callable $content
249
     * @return $this
250
     */
251
    public function setRowClass($content)
252
    {
253
        $this->templates['DT_RowClass'] = $content;
254
255
        return $this;
256
    }
257
258
    /**
259
     * Sets DT_RowId template.
260
     * result: <tr id="output_from_your_template">.
261
     *
262
     * @param string|callable $content
263
     * @return $this
264
     */
265
    public function setRowId($content)
266
    {
267
        $this->templates['DT_RowId'] = $content;
268
269
        return $this;
270
    }
271
272
    /**
273
     * Set DT_RowData templates.
274
     *
275
     * @param array $data
276
     * @return $this
277
     */
278
    public function setRowData(array $data)
279
    {
280
        $this->templates['DT_RowData'] = $data;
281
282
        return $this;
283
    }
284
285
    /**
286
     * Add DT_RowData template.
287
     *
288
     * @param string          $key
289
     * @param string|callable $value
290
     * @return $this
291
     */
292
    public function addRowData($key, $value)
293
    {
294
        $this->templates['DT_RowData'][$key] = $value;
295
296
        return $this;
297
    }
298
299
    /**
300
     * Set DT_RowAttr templates.
301
     * result: <tr attr1="attr1" attr2="attr2">.
302
     *
303
     * @param array $data
304
     * @return $this
305
     */
306
    public function setRowAttr(array $data)
307
    {
308
        $this->templates['DT_RowAttr'] = $data;
309
310
        return $this;
311
    }
312
313
    /**
314
     * Add DT_RowAttr template.
315
     *
316
     * @param string          $key
317
     * @param string|callable $value
318
     * @return $this
319
     */
320
    public function addRowAttr($key, $value)
321
    {
322
        $this->templates['DT_RowAttr'][$key] = $value;
323
324
        return $this;
325
    }
326
327
    /**
328
     * Add custom filter handler for the give column.
329
     *
330
     * @param string   $column
331
     * @param callable $callback
332
     * @return $this
333
     */
334
    public function filterColumn($column, callable $callback)
335
    {
336
        $this->columnDef['filter'][$column] = ['method' => $callback];
337
338
        return $this;
339
    }
340
341
    /**
342
     * Order each given columns versus the given custom sql.
343
     *
344
     * @param array  $columns
345
     * @param string $sql
346
     * @param array  $bindings
347
     * @return $this
348
     */
349
    public function orderColumns(array $columns, $sql, $bindings = [])
350
    {
351
        foreach ($columns as $column) {
352
            $this->orderColumn($column, str_replace(':column', $column, $sql), $bindings);
353
        }
354
355
        return $this;
356
    }
357
358
    /**
359
     * Override default column ordering.
360
     *
361
     * @param string $column
362
     * @param string $sql
363
     * @param array  $bindings
364
     * @return $this
365
     * @internal string $1 Special variable that returns the requested order direction of the column.
366
     */
367
    public function orderColumn($column, $sql, $bindings = [])
368
    {
369
        $this->columnDef['order'][$column] = compact('sql', 'bindings');
370
371
        return $this;
372
    }
373
374
    /**
375
     * Set data output transformer.
376
     *
377
     * @param mixed $transformer
378
     * @return $this
379
     */
380
    public function setTransformer($transformer)
381
    {
382
        $this->transformer = $transformer;
383
384
        return $this;
385
    }
386
387
    /**
388
     * Set fractal serializer class.
389
     *
390
     * @param string $serializer
391
     * @return $this
392
     */
393
    public function setSerializer($serializer)
394
    {
395
        $this->serializer = $serializer;
396
397
        return $this;
398
    }
399
400
    /**
401
     * Append data on json response.
402
     *
403
     * @param mixed $key
404
     * @param mixed $value
405
     * @return $this
406
     */
407
    public function with($key, $value = '')
408
    {
409
        if (is_array($key)) {
410
            $this->appends = $key;
411
        } elseif (is_callable($value)) {
412
            $this->appends[$key] = value($value);
413
        } else {
414
            $this->appends[$key] = value($value);
415
        }
416
417
        return $this;
418
    }
419
420
    /**
421
     * Override default ordering method with a closure callback.
422
     *
423
     * @param callable $closure
424
     * @return $this
425
     */
426
    public function order(callable $closure)
427
    {
428
        $this->orderCallback = $closure;
429
430
        return $this;
431
    }
432
433
    /**
434
     * Update list of columns that is not allowed for search/sort.
435
     *
436
     * @param  array $blacklist
437
     * @return $this
438
     */
439
    public function blacklist(array $blacklist)
440
    {
441
        $this->columnDef['blacklist'] = $blacklist;
442
443
        return $this;
444
    }
445
446
    /**
447
     * Update list of columns that is allowed for search/sort.
448
     *
449
     * @param  string|array $whitelist
450
     * @return $this
451
     */
452
    public function whitelist($whitelist = '*')
453
    {
454
        $this->columnDef['whitelist'] = $whitelist;
455
456
        return $this;
457
    }
458
459
    /**
460
     * Set smart search config at runtime.
461
     *
462
     * @param bool $bool
463
     * @return $this
464
     */
465
    public function smart($bool = true)
466
    {
467
        $this->config->set(['datatables.search.smart' => $bool]);
468
469
        return $this;
470
    }
471
472
    /**
473
     * Set total records manually.
474
     *
475
     * @param int $total
476
     * @return $this
477
     */
478
    public function setTotalRecords($total)
479
    {
480
        $this->totalRecords = $total;
481
482
        return $this;
483
    }
484
485
    /**
486
     * Skip pagination as needed.
487
     *
488
     * @return $this
489
     */
490
    public function skipPaging()
491
    {
492
        $this->skipPaging = true;
493
494
        return $this;
495
    }
496
497
    /**
498
     * Set datatables to do ordering with NULLS LAST option.
499
     *
500
     * @return $this
501
     */
502
    public function orderByNullsLast()
503
    {
504
        $this->nullsLast = true;
505
506
        return $this;
507
    }
508
509
    /**
510
     * Push a new column name to blacklist.
511
     *
512
     * @param string $column
513
     * @return $this
514
     */
515
    public function pushToBlacklist($column)
516
    {
517
        if (!$this->isBlacklisted($column)) {
518
            $this->columnDef['blacklist'][] = $column;
519
        }
520
521
        return $this;
522
    }
523
524
    /**
525
     * Check if column is blacklisted.
526
     *
527
     * @param string $column
528
     * @return bool
529
     */
530
    protected function isBlacklisted($column)
531
    {
532
        $colDef = $this->getColumnsDefinition();
533
534
        if (in_array($column, $colDef['blacklist'])) {
535
            return true;
536
        }
537
538
        if ($colDef['whitelist'] === '*' || in_array($column, $colDef['whitelist'])) {
539
            return false;
540
        }
541
542
        return true;
543
    }
544
545
    /**
546
     * Get columns definition.
547
     *
548
     * @return array
549
     */
550
    protected function getColumnsDefinition()
551
    {
552
        $config  = $this->config->get('datatables.columns');
553
        $allowed = ['excess', 'escape', 'raw', 'blacklist', 'whitelist'];
554
555
        return array_merge(array_only($config, $allowed), $this->columnDef);
556
    }
557
558
    /**
559
     * Perform sorting of columns.
560
     */
561
    public function ordering()
562
    {
563
        if ($this->orderCallback) {
564
            return call_user_func($this->orderCallback, $this->resolveCallbackParameter());
565
        }
566
567
        return $this->defaultOrdering();
568
    }
569
570
    /**
571
     * Resolve callback parameter instance.
572
     *
573
     * @return mixed
574
     */
575
    abstract protected function resolveCallbackParameter();
576
577
    /**
578
     * Perform default query orderBy clause.
579
     */
580
    abstract protected function defaultOrdering();
581
582
    /**
583
     * Perform necessary filters.
584
     *
585
     * @return void
586
     */
587
    protected function filterRecords()
588
    {
589
        if ($this->autoFilter && $this->request->isSearchable()) {
590
            $this->filtering();
591
        }
592
593
        if (is_callable($this->filterCallback)) {
594
            call_user_func($this->filterCallback, $this->filterCallbackParameters);
595
        }
596
597
        $this->columnSearch();
598
        $this->filteredRecords = $this->isFilterApplied ? $this->count() : $this->totalRecords;
599
    }
600
601
    /**
602
     * Perform global search.
603
     *
604
     * @return void
605
     */
606
    public function filtering()
607
    {
608
        $keyword = $this->request->keyword();
609
610
        if ($this->config->isSmartSearch()) {
611
            $this->smartGlobalSearch($keyword);
612
613
            return;
614
        }
615
616
        $this->globalSearch($keyword);
617
    }
618
619
    /**
620
     * Perform multi-term search by splitting keyword into
621
     * individual words and searches for each of them.
622
     *
623
     * @param string $keyword
624
     */
625
    protected function smartGlobalSearch($keyword)
626
    {
627
        $keywords = array_filter(explode(' ', $keyword));
628
629
        foreach ($keywords as $keyword) {
630
            $this->globalSearch($keyword);
631
        }
632
    }
633
634
    /**
635
     * Perform global search for the given keyword.
636
     *
637
     * @param string $keyword
638
     */
639
    abstract protected function globalSearch($keyword);
640
641
    /**
642
     * Apply pagination.
643
     *
644
     * @return void
645
     */
646
    protected function paginate()
647
    {
648
        if ($this->request->isPaginationable() && !$this->skipPaging) {
649
            $this->paging();
650
        }
651
    }
652
653
    /**
654
     * Transform output.
655
     *
656
     * @param mixed $output
657
     * @return array
658
     */
659
    protected function transform($output)
660
    {
661
        if (!isset($this->transformer)) {
662
            return Helper::transform($output);
663
        }
664
665
        return (new FractalTransformer)->transform($output, $this->transformer, $this->serializer);
666
    }
667
668
    /**
669
     * Get processed data.
670
     *
671
     * @param bool|false $object
672
     * @return array
673
     */
674
    protected function getProcessedData($object = false)
675
    {
676
        $processor = new DataProcessor(
677
            $this->results(),
678
            $this->getColumnsDefinition(),
679
            $this->templates,
680
            $this->request->input('start')
681
        );
682
683
        return $processor->process($object);
684
    }
685
686
    /**
687
     * Render json response.
688
     *
689
     * @param array $data
690
     * @return \Illuminate\Http\JsonResponse
691
     */
692
    protected function render(array $data)
693
    {
694
        $output = array_merge([
695
            'draw'            => (int) $this->request->input('draw'),
696
            'recordsTotal'    => $this->totalRecords,
697
            'recordsFiltered' => $this->filteredRecords,
698
            'data'            => $data,
699
        ], $this->appends);
700
701
        if ($this->config->isDebugging()) {
702
            $output = $this->showDebugger($output);
703
        }
704
705
        return new JsonResponse(
706
            $output,
707
            200,
708
            $this->config->get('datatables.json.header', []),
709
            $this->config->get('datatables.json.options', 0)
710
        );
711
    }
712
713
    /**
714
     * Append debug parameters on output.
715
     *
716
     * @param  array $output
717
     * @return array
718
     */
719
    abstract protected function showDebugger(array $output);
720
721
    /**
722
     * Return an error json response.
723
     *
724
     * @param \Exception $exception
725
     * @return \Illuminate\Http\JsonResponse
726
     * @throws \Yajra\Datatables\Exception
727
     */
728
    protected function errorResponse(\Exception $exception)
729
    {
730
        $error = $this->config->get('datatables.error');
731
        if ($error === 'throw') {
732
            throw new Exception($exception->getMessage(), $code = 0, $exception);
733
        }
734
735
        $this->getLogger()->error($exception);
736
737
        return new JsonResponse([
738
            'draw'            => (int) $this->request->input('draw'),
739
            'recordsTotal'    => (int) $this->totalRecords,
740
            'recordsFiltered' => 0,
741
            'data'            => [],
742
            'error'           => $error ? __($error) : "Exception Message:\n\n" . $exception->getMessage(),
743
        ]);
744
    }
745
746
    /**
747
     * Get monolog/logger instance.
748
     *
749
     * @return \Illuminate\Contracts\Logging\Log
750
     */
751
    public function getLogger()
752
    {
753
        $this->logger = $this->logger ?: resolve(Log::class);
754
755
        return $this->logger;
756
    }
757
758
    /**
759
     * Set monolog/logger instance.
760
     *
761
     * @param \Illuminate\Contracts\Logging\Log $logger
762
     * @return $this
763
     */
764
    public function setLogger(Log $logger)
765
    {
766
        $this->logger = $logger;
767
768
        return $this;
769
    }
770
771
    /**
772
     * Setup search keyword.
773
     *
774
     * @param  string $value
775
     * @return string
776
     */
777
    protected function setupKeyword($value)
778
    {
779
        if ($this->config->isSmartSearch()) {
780
            $keyword = '%' . $value . '%';
781
            if ($this->config->isWildcard()) {
782
                $keyword = Helper::wildcardLikeString($value);
783
            }
784
            // remove escaping slash added on js script request
785
            $keyword = str_replace('\\', '%', $keyword);
786
787
            return $keyword;
788
        }
789
790
        return $value;
791
    }
792
793
    /**
794
     * Update flags to disable global search.
795
     *
796
     * @param  callable $callback
797
     * @param  mixed    $parameters
798
     * @param  bool     $autoFilter
799
     */
800
    protected function overrideGlobalSearch(callable $callback, $parameters, $autoFilter = false)
801
    {
802
        $this->autoFilter               = $autoFilter;
803
        $this->isFilterApplied          = true;
804
        $this->filterCallback           = $callback;
805
        $this->filterCallbackParameters = $parameters;
806
    }
807
808
    /**
809
     * Get column name to be use for filtering and sorting.
810
     *
811
     * @param integer $index
812
     * @param bool    $wantsAlias
813
     * @return string
814
     */
815
    protected function getColumnName($index, $wantsAlias = false)
816
    {
817
        $column = $this->request->columnName($index);
818
819
        // DataTables is using make(false)
820
        if (is_numeric($column)) {
821
            $column = $this->getColumnNameByIndex($index);
822
        }
823
824
        if (Str::contains(Str::upper($column), ' AS ')) {
825
            $column = Helper::extractColumnName($column, $wantsAlias);
826
        }
827
828
        return $column;
829
    }
830
831
    /**
832
     * Get column name by order column index.
833
     *
834
     * @param int $index
835
     * @return string
836
     */
837
    protected function getColumnNameByIndex($index)
838
    {
839
        $name = (isset($this->columns[$index]) && $this->columns[$index] != '*') ? $this->columns[$index] : $this->getPrimaryKeyName();
840
841
        return in_array($name, $this->extraColumns, true) ? $this->getPrimaryKeyName() : $name;
842
    }
843
844
    /**
845
     * If column name could not be resolved then use primary key.
846
     *
847
     * @return string
848
     */
849
    protected function getPrimaryKeyName()
850
    {
851
        return 'id';
852
    }
853
}
854