1: <?php
2:
3: namespace Yajra\Datatables;
4:
5: use Exception;
6: use Illuminate\Http\Request as IlluminateRequest;
7:
8: /**
9: * Class Request.
10: *
11: * @property array columns
12: * @package Yajra\Datatables
13: * @author Arjay Angeles <aqangeles@gmail.com>
14: */
15: class Request extends IlluminateRequest
16: {
17: /**
18: * Check if request uses legacy code
19: *
20: * @throws Exception
21: */
22: public function checkLegacyCode()
23: {
24: if (! $this->get('draw') && $this->get('sEcho')) {
25: throw new Exception('DataTables legacy code is not supported! Please use DataTables 1.10++ coding convention.');
26: } elseif (! $this->get('draw') && ! $this->get('columns')) {
27: throw new Exception('Insufficient parameters');
28: }
29: }
30:
31: /**
32: * Check if Datatables is searchable.
33: *
34: * @return bool
35: */
36: public function isSearchable()
37: {
38: return $this->get('search')['value'] != '';
39: }
40:
41: /**
42: * Get column's search value.
43: *
44: * @param integer $index
45: * @return string
46: */
47: public function columnKeyword($index)
48: {
49: return $this->columns[$index]['search']['value'];
50: }
51:
52: /**
53: * Check if Datatables must uses regular expressions
54: *
55: * @param integer $index
56: * @return string
57: */
58: public function isRegex($index)
59: {
60: return $this->columns[$index]['search']['regex'] === 'true';
61: }
62:
63: /**
64: * Get orderable columns
65: *
66: * @return array
67: */
68: public function orderableColumns()
69: {
70: if (! $this->isOrderable()) {
71: return [];
72: }
73:
74: $orderable = [];
75: for ($i = 0, $c = count($this->get('order')); $i < $c; $i++) {
76: $order_col = (int) $this->get('order')[$i]['column'];
77: $order_dir = $this->get('order')[$i]['dir'];
78: if ($this->isColumnOrderable($order_col)) {
79: $orderable[] = ['column' => $order_col, 'direction' => $order_dir];
80: }
81: }
82:
83: return $orderable;
84: }
85:
86: /**
87: * Check if Datatables ordering is enabled.
88: *
89: * @return bool
90: */
91: public function isOrderable()
92: {
93: return $this->get('order') && count($this->get('order')) > 0;
94: }
95:
96: /**
97: * Check if a column is orderable.
98: *
99: * @param integer $index
100: * @return bool
101: */
102: public function isColumnOrderable($index)
103: {
104: return $this->get('columns')[$index]['orderable'] == 'true';
105: }
106:
107: /**
108: * Get searchable column indexes
109: *
110: * @return array
111: */
112: public function searchableColumnIndex()
113: {
114: $searchable = [];
115: for ($i = 0, $c = count($this->get('columns')); $i < $c; $i++) {
116: if ($this->isColumnSearchable($i, false)) {
117: $searchable[] = $i;
118: }
119: }
120:
121: return $searchable;
122: }
123:
124: /**
125: * Check if a column is searchable.
126: *
127: * @param integer $i
128: * @param bool $column_search
129: * @return bool
130: */
131: public function isColumnSearchable($i, $column_search = true)
132: {
133: $columns = $this->get('columns');
134: if ($column_search) {
135: return $columns[$i]['searchable'] == 'true' && $columns[$i]['search']['value'] != '';
136: }
137:
138: return $columns[$i]['searchable'] == 'true';
139: }
140:
141: /**
142: * Get global search keyword
143: *
144: * @return string
145: */
146: public function keyword()
147: {
148: return $this->get('search')['value'];
149: }
150:
151: /**
152: * Get column identity from input or database.
153: *
154: * @param integer $i
155: * @return string
156: */
157: public function columnName($i)
158: {
159: $column = $this->get('columns')[$i];
160:
161: return isset($column['name']) && $column['name'] <> '' ? $column['name'] : $column['data'];
162: }
163:
164: /**
165: * Check if Datatables allow pagination.
166: *
167: * @return bool
168: */
169: public function isPaginationable()
170: {
171: return ! is_null($this->get('start')) && ! is_null($this->get('length')) && $this->get('length') != -1;
172: }
173: }
174: