1: <?php
2:
3: namespace Yajra\Datatables\Processors;
4:
5: use Illuminate\Support\Arr;
6: use Illuminate\Support\Facades\Config;
7: use Yajra\Datatables\Helper;
8:
9: 10: 11: 12: 13: 14:
15: class DataProcessor
16: {
17: 18: 19:
20: protected $start;
21:
22: 23: 24: 25: 26:
27: protected $escapeColumns = [];
28:
29: 30: 31: 32: 33:
34: protected $output = [];
35:
36: 37: 38:
39: protected $appendColumns = [];
40:
41: 42: 43:
44: protected $editColumns = [];
45:
46: 47: 48:
49: protected $excessColumns = [];
50:
51: 52: 53:
54: protected $results;
55:
56: 57: 58:
59: protected $templates;
60:
61: 62: 63:
64: protected $includeIndex;
65:
66: 67: 68: 69: 70: 71:
72: public function __construct($results, array $columnDef, array $templates, $start)
73: {
74: $this->results = $results;
75: $this->appendColumns = $columnDef['append'];
76: $this->editColumns = $columnDef['edit'];
77: $this->excessColumns = $columnDef['excess'];
78: $this->escapeColumns = $columnDef['escape'];
79: $this->includeIndex = $columnDef['index'];
80: $this->templates = $templates;
81: $this->start = $start;
82: }
83:
84: 85: 86: 87: 88: 89:
90: public function process($object = false)
91: {
92: $this->output = [];
93: $indexColumn = Config::get('datatables.index_column', 'DT_Row_Index');
94:
95: foreach ($this->results as $row) {
96: $data = Helper::convertToArray($row);
97: $value = $this->addColumns($data, $row);
98: $value = $this->editColumns($value, $row);
99: $value = $this->setupRowVariables($value, $row);
100: $value = $this->removeExcessColumns($value);
101:
102: if ($this->includeIndex) {
103: $value[$indexColumn] = ++$this->start;
104: }
105:
106: $this->output[] = $object ? $value : $this->flatten($value);
107: }
108:
109: return $this->escapeColumns($this->output);
110: }
111:
112: 113: 114: 115: 116: 117: 118:
119: protected function addColumns($data, $row)
120: {
121: foreach ($this->appendColumns as $key => $value) {
122: $value['content'] = Helper::compileContent($value['content'], $data, $row);
123: $data = Helper::includeInArray($value, $data);
124: }
125:
126: return $data;
127: }
128:
129: 130: 131: 132: 133: 134: 135:
136: protected function editColumns($data, $row)
137: {
138: foreach ($this->editColumns as $key => $value) {
139: $value['content'] = Helper::compileContent($value['content'], $data, $row);
140: Arr::set($data, $value['name'], $value['content']);
141: }
142:
143: return $data;
144: }
145:
146: 147: 148: 149: 150: 151: 152:
153: protected function setupRowVariables($data, $row)
154: {
155: $processor = new RowProcessor($data, $row);
156:
157: return $processor
158: ->rowValue('DT_RowId', $this->templates['DT_RowId'])
159: ->rowValue('DT_RowClass', $this->templates['DT_RowClass'])
160: ->rowData('DT_RowData', $this->templates['DT_RowData'])
161: ->rowData('DT_RowAttr', $this->templates['DT_RowAttr'])
162: ->getData();
163: }
164:
165: 166: 167: 168: 169: 170:
171: protected function removeExcessColumns(array $data)
172: {
173: foreach ($this->excessColumns as $value) {
174: unset($data[$value]);
175: }
176:
177: return $data;
178: }
179:
180: 181: 182: 183: 184: 185:
186: public function flatten(array $array)
187: {
188: $return = [];
189: $exceptions = ['DT_RowId', 'DT_RowClass', 'DT_RowData', 'DT_RowAttr'];
190:
191: foreach ($array as $key => $value) {
192: if (in_array($key, $exceptions)) {
193: $return[$key] = $value;
194: } else {
195: $return[] = $value;
196: }
197: }
198:
199: return $return;
200: }
201:
202: 203: 204: 205: 206: 207:
208: protected function escapeColumns(array $output)
209: {
210: return array_map(function ($row) {
211: if ($this->escapeColumns == '*') {
212: $row = $this->escapeRow($row, $this->escapeColumns);
213: } else {
214: foreach ($this->escapeColumns as $key) {
215: if (array_get($row, $key)) {
216: array_set($row, $key, e(array_get($row, $key)));
217: }
218: }
219: }
220:
221: return $row;
222: }, $output);
223: }
224:
225: 226: 227: 228: 229: 230: 231:
232: protected function escapeRow(array $row, $escapeColumns)
233: {
234: foreach ($row as $key => $value) {
235: if (is_array($value)) {
236: $row[$key] = $this->escapeRow($value, $escapeColumns);
237: } else {
238: $row[$key] = e($value);
239: }
240: }
241:
242: return $row;
243: }
244: }
245: