1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: namespace Cakestrap\View\Helper;
17:
18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40:
41:
42: class TableHelper extends Basic
43: {
44: 45: 46: 47: 48:
49: protected $_options = [
50: 'type' => 'table-bordered',
51: 'class' => null,
52: ];
53:
54: 55: 56: 57: 58: 59:
60: public function set()
61: {
62: $this->_setHeaderAndContent();
63:
64: $this->_addToContainer();
65: $this->_reset();
66:
67: return $this->_container;
68: }
69:
70: 71: 72: 73: 74: 75:
76: public function header($settings = [])
77: {
78: $this->header = $this->_createColumn($settings['records'], $settings['header']);
79: return $this;
80: }
81:
82: 83: 84: 85: 86: 87: 88:
89: protected function _createColumn($values = [], $translate = [])
90: {
91: $translate = array_merge(['id'=>'id'], $translate);
92:
93: foreach($values as $key=>$value) {
94: foreach($translate as $index=>$name) {
95: if(isset($value[$name])) {
96: $column[$key][$index] = $value[$name];
97: } else {
98: $column[$key][$index] = null;
99: }
100: }
101: }
102:
103: return $column;
104: }
105:
106: 107: 108: 109: 110:
111: protected function _addToContainer()
112: {
113: $contents = $this->_contents();
114: $this->_container = $this->_stringTemplate->format('container', [
115: 'container'=> $contents,
116: 'type' => $this->_options['type'],
117: 'class' => $this->_options['class']
118: ]);
119: }
120:
121: 122: 123: 124: 125:
126: protected function _setHeaderAndContent()
127: {
128: $header = $this->_getHeader();
129: foreach($this->header as $key=>$value) {
130: $tr = [];
131: if(trim($this->radio)) $tr[] = $this->_setRadio($value['id'], $key);
132:
133: foreach($value as $index=>$name) {
134: if(strtolower($index) != "id") {
135: $tr[] = $this->_stringTemplate->format('td', ['td'=>$name]);
136: }
137: }
138: $class = $this->_format($value['id'], 'tr');
139: $content['content'][] = $this->_stringTemplate->format('tr', ['tr'=>implode("", $tr), 'class'=>$class]);
140: }
141:
142: $header = $this->_stringTemplate->format('thead', ['thead'=>implode('', $header)]);
143: $content = $this->_stringTemplate->format('tbody', ['tbody'=>implode('', $content['content'])]);
144: $this->_contents = $header . $content;
145: }
146:
147: 148: 149: 150: 151:
152: protected function _getHeader()
153: {
154: $keys = array_keys($this->header[0]);
155: if(trim($this->radio)) {
156: $header[] = $this->_stringTemplate->format('th', ['th'=>'', 'class'=>'table-radio']);
157: }
158:
159:
160: foreach($keys as $key=>$value) {
161: if($value != 'id') {
162: $class = $this->_format($value, 'th');
163: $header[] = $this->_stringTemplate->format('th', ['th'=>$value, 'class' => $class]);
164: }
165: }
166: return $header;
167: }
168:
169: 170: 171: 172: 173: 174: 175:
176: protected function _format($value, $prefix = 'table')
177: {
178: $replace = [' ', "/"];
179: $class = $prefix . '-' . str_replace($replace , "-", strtolower($value));
180: return $class;
181: }
182:
183: 184: 185: 186: 187: 188: 189:
190: private function _setRadio($value, $order = 0)
191: {
192: $checked = null;
193: $class = null;
194: if($value == $this->checked) {
195: $checked = 'checked';
196: $class = 'current';
197: }
198: $content = $this->_stringTemplate->format('radio', [
199: 'value' => $value,
200: 'checked'=> $checked,
201: 'name' => $this->radio,
202: 'order' => $order,
203: 'class' => $class]);
204:
205: return $this->_stringTemplate->format('td', ['td'=>$content]);
206: }
207:
208: 209: 210: 211: 212:
213: protected function _contents()
214: {
215: return $this->_contents;
216: }
217: }