Overview

Namespaces

  • Cakestrap
    • View
      • Helper

Classes

  • Cakestrap\View\Helper\AlertHelper
  • Cakestrap\View\Helper\AssetHelper
  • Cakestrap\View\Helper\BadgeHelper
  • Cakestrap\View\Helper\Basic
  • Cakestrap\View\Helper\ButtonItemsHelper
  • Cakestrap\View\Helper\CakestrapHelper
  • Cakestrap\View\Helper\CallOutHelper
  • Cakestrap\View\Helper\CollapseHelper
  • Cakestrap\View\Helper\DropdownHelper
  • Cakestrap\View\Helper\ListGroupHelper
  • Cakestrap\View\Helper\ModalHelper
  • Cakestrap\View\Helper\PanelHelper
  • Cakestrap\View\Helper\TabHelper
  • Cakestrap\View\Helper\TableHelper
  • Cakestrap\View\Helper\Templates
  • Cakestrap\View\Helper\WellHelper
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /**
  3:  * Copyright (c) CMNWorks
  4:  *
  5:  * Licensed under The MIT License
  6:  * For full copyright and license information, please see the LICENSE.txt
  7:  * Redistributions of files must retain the above copyright notice.
  8:  *
  9:  * @copyright     Copyright (c) CMNWorks Christopher M. Natan
 10:  * @author        Christopher M. Natan
 11:  * @link          http://cmnworks.com
 12:  * @since         1.8.8
 13:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: 
 16: namespace Cakestrap\View\Helper;
 17: 
 18: /**
 19:  * A HTML table, containing  columns and  rows.
 20:  *
 21:  * <code>
 22:  *<?php
 23:  *  $records = [
 24:  *               0 => ['name' => 'Name 1', 'age' => 26, 'email' => 'name1@gmail.com'],
 25:  *               1 => ['name' => 'Name 2', 'age' => 25, 'email' => 'name2@gmail.com'],
 26:  *               3 => ['name' => 'Name 3', 'age' => 24, 'email' => 'name3@gmail.com']
 27:  *  ];
 28:  *  $header = ['User Name' => 'name',  'Email' => 'email'];
 29:  *?>
 30:  *<?php echo
 31:  *  $this->Cakestrap->Table()
 32:  *                  ->header(['records'=>$records, 'header'=>$header])
 33:  *                  ->options(['class'=>'table-great'])
 34:  *                  ->set()
 35:  *?>
 36:  * </code>
 37:  *
 38:  * @package Cakestrap\View\Helper
 39:  * @link http://getbootstrap.com/css/#tables
 40:  */
 41: 
 42: class TableHelper extends Basic
 43: {
 44:     /**
 45:      * Additional options for this helper
 46:      *
 47:      * @var array
 48:      */
 49:     protected $_options     = [
 50:         'type'   => 'table-bordered',
 51:         'class'  => null,
 52:     ];
 53: 
 54:     /**
 55:      * Set all defined values.
 56:      * This will return the final html template.
 57:      *
 58:      * @return string
 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:      * Set  header settings
 72:      *
 73:      * @param array $settings
 74:      * @return object $this
 75:      */
 76:     public function header($settings = [])
 77:     {
 78:         $this->header = $this->_createColumn($settings['records'], $settings['header']);
 79:         return $this;
 80:     }
 81: 
 82:     /**
 83:      * Create column header.
 84:      *
 85:      * @param array $values
 86:      * @param array $translate
 87:      * @return array $column
 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:      * Add all items to parent container.
108:      *
109:      * @return void
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:      * Set  header and html content template.
123:      *
124:      * @return void
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:      * Get the headers.
149:      *
150:      * @return array $header
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:      * Format attribute class value
171:      *
172:      * @param string $value
173:      * @param string $prefix Class prefix.
174:      * @return string $class
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:      * Add radio element
185:      *
186:      * @param string $value
187:      * @param integer $order
188:      * @return string
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:      * Final content
210:      *
211:      * @return array
212:      */
213:     protected function _contents()
214:     {
215:         return $this->_contents;
216:     }
217: }
API documentation generated by ApiGen