1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace App\Lib\Html;
13:
14: use Nette\Object,
15: Nette\Utils\Html,
16: App\Interfaces as I,
17: App\Lib\Statics\Cons,
18: App\Lib\Statics\Vars;
19:
20: 21: 22:
23: class Table extends Object implements I\IHtml {
24:
25:
26: private $fields;
27:
28:
29: private $data;
30:
31:
32: private $footer;
33:
34:
35: private $id;
36:
37:
38: private $link;
39:
40: 41: 42: 43: 44: 45: 46: 47:
48: public function __construct($link, $fields, $data, $footer = false, $id = null) {
49: $this->fields = $fields;
50: $this->data = $data;
51: $this->footer = $footer;
52: $this->id = ($id) ? 'list_table_' . $id : 'list_table';
53: $this->link = $link;
54: }
55:
56: 57: 58: 59: 60:
61: public function getCode() {
62: $table = Html::el('table')->addAttributes(array('id' => $this->id, 'class' => 'display'));
63: $table->insert(NULL, $this->getTableHead($this->fields));
64: (!$this->footer)? :
65: $table->insert(NULL, $this->getTableFoot($this->data, $this->fields));
66: $table->insert(NULL, $this->getTableBody($this->data, $this->fields));
67: return $table;
68: }
69:
70: 71: 72: 73: 74: 75: 76:
77: private function getTableBody($data, $fields) {
78: $body = Html::el('tbody');
79: foreach ($data as $row) {
80: $body->insert($row[Cons::COLUMN_ID], $this->getBodyTr($row, $fields));
81: }
82: return $body;
83: }
84:
85: 86: 87: 88: 89: 90: 91:
92: private function getTableFoot($data, $fields) {
93: return Html::el('tfoot')->add($this->getFootTr($data, $fields));
94: }
95:
96: 97: 98: 99: 100: 101:
102: private function getTableHead($fields) {
103: return Html::el('thead')->add($this->getHeadTr($fields));
104: }
105:
106: 107: 108: 109: 110: 111: 112:
113: private function getBodyTr($activeRowData, $fields) {
114: $linkArray = explode('?', $this->link);
115: $link = $linkArray[0] . '/' . $activeRowData[Cons::COLUMN_ID];
116: $link .= isset($linkArray[1]) ? '?'.$linkArray[1]:'';
117: $tr = Html::el('tr class="tr-click"')
118: ->data('href', $link);
119: foreach ($fields as $field) {
120: $tr->insert(NULL, $this->getBodyTd($activeRowData, $field));
121: }
122: return $tr;
123: }
124:
125: 126: 127: 128: 129: 130:
131: private function getFootTr($data, $fields) {
132: $tr = Html::el('tr');
133: foreach ($fields as $row) {
134: $tr->insert(
135: (isset($row[Cons::COLUMN_FOOTER]) && $row[Cons::COLUMN_TYPE] === ('float' || 'integer' )) ?
136: $this->getFootTd($data, $row[Cons::COLUMN_FIELD]) : $this->getFootTd());
137: }
138: return $tr;
139: }
140:
141: 142: 143: 144: 145: 146:
147: private function getHeadTr($fields) {
148: $tr = Html::el('tr');
149: foreach ($fields as $row) {
150: $tr->insert($row[Cons::COLUMN_SEQ], $this->getHeadTh($row[Cons::COLUMN_NAME]));
151: }
152: return $tr;
153: }
154:
155: 156: 157: 158: 159: 160: 161:
162: private function getBodyTd($activeRowData, $field, $td = '') {
163: if (isset($activeRowData[$field[Cons::COLUMN_FIELD]])) {
164: $td = $activeRowData[$field[Cons::COLUMN_FIELD]];
165: if ($field[Cons::COLUMN_LENGHT] === 1) {
166: $td = $this->getBinar($activeRowData, $field);
167: }
168: if (isset($field[Cons::COLUMN_RELATION]) && $field[Cons::COLUMN_RELATION] !== "" && \Nette\Utils\Strings::endsWith($field[Cons::COLUMN_FIELD], '_id')) {
169: $td = $this->getRelation($activeRowData, $field);
170: }
171: }
172: return Html::el('td')->setText($td);
173: }
174:
175: 176: 177: 178: 179: 180:
181: private function getHeadTh($field) {
182: return Html::el('th')
183: ->setText($field);
184: }
185:
186: 187: 188: 189: 190: 191: 192:
193: private function getFootTd($data = null, $field = null) {
194: return Html::el('td')->setText(
195: ($data && $field) ?
196: $data->sum($field) : NULL
197: );
198: }
199:
200: 201: 202: 203: 204: 205:
206: private function getRelation($activeRowData, $field) {
207: $target = explode(',',rtrim(strtolower($field[Cons::COLUMN_RELATION]),'/null'));
208: $ref = ($field[Cons::COLUMN_FIELD] === Cons::COLUMN_PARENT) ?
209: $activeRowData->ref($field[Cons::COLUMN_TABLE.'_'.Cons::COLUMN_ID], $field[Cons::COLUMN_FIELD]) :
210: $activeRowData->ref(str_replace('_id', '', $field[Cons::COLUMN_FIELD]));
211: $relation = '';
212: foreach ($target as $item){
213: if ($item === reset($target)) {
214: $relation .= $ref[$item];
215: } else {
216: $relation .= ' ,' . $ref[$item];
217: }
218: }
219: return ($relation === Cons::ROOT) ? '' : $relation;
220: }
221:
222: 223: 224: 225: 226: 227:
228: private function getBinar($activeRowData, $field){
229: return ($field[Cons::COLUMN_FIELD] === 'state') ?
230: Vars::active($activeRowData[$field[Cons::COLUMN_FIELD]]) :
231: Vars::aN($activeRowData[$field[Cons::COLUMN_FIELD]]);
232: }
233: }