PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/admincrud/extensions/bootstrap/widgets/TbGridView.php

https://github.com/max-rautkin/yii-admincrud
PHP | 230 lines | 125 code | 32 blank | 73 comment | 15 complexity | 6e555edb8f95f29571bd03071090e658 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /*## TbGridView class file.
  3. *
  4. * @author Christoffer Niska <ChristofferNiska@gmail.com>
  5. * @copyright Copyright &copy; Christoffer Niska 2011-
  6. * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
  7. * @package bootstrap.widgets
  8. */
  9. Yii::import('zii.widgets.grid.CGridView');
  10. Yii::import('bootstrap.widgets.TbDataColumn');
  11. /**
  12. * Bootstrap Zii grid view.
  13. *
  14. * @property CActiveDataProvider $dataProvider the data provider for the view.
  15. */
  16. class TbGridView extends CGridView
  17. {
  18. // Table types.
  19. const TYPE_STRIPED = 'striped';
  20. const TYPE_BORDERED = 'bordered';
  21. const TYPE_CONDENSED = 'condensed';
  22. const TYPE_HOVER = 'hover';
  23. /**
  24. * @var string|array the table type.
  25. * Valid values are 'striped', 'bordered', 'condensed' and/or 'hover'.
  26. */
  27. public $type;
  28. /**
  29. * @var string the CSS class name for the pager container. Defaults to 'pagination'.
  30. */
  31. public $pagerCssClass = 'pagination';
  32. /**
  33. * @var array the configuration for the pager.
  34. * Defaults to <code>array('class'=>'ext.bootstrap.widgets.TbPager')</code>.
  35. */
  36. public $pager = array('class' => 'bootstrap.widgets.TbPager');
  37. /**
  38. * @var string the URL of the CSS file used by this grid view.
  39. * Defaults to false, meaning that no CSS will be included.
  40. */
  41. public $cssFile = false;
  42. /**
  43. * @var bool whether to make the grid responsive
  44. */
  45. public $responsiveTable = false;
  46. /**
  47. * @var array of additional parameters to pass to values
  48. */
  49. public $extraParams = array();
  50. /**
  51. *### .init()
  52. *
  53. * Initializes the widget.
  54. */
  55. public function init()
  56. {
  57. parent::init();
  58. $classes = array('table');
  59. if (isset($this->type)) {
  60. if (is_string($this->type)) {
  61. $this->type = explode(' ', $this->type);
  62. }
  63. if (!empty($this->type)) {
  64. $validTypes = array(self::TYPE_STRIPED, self::TYPE_BORDERED, self::TYPE_CONDENSED, self::TYPE_HOVER);
  65. foreach ($this->type as $type) {
  66. if (in_array($type, $validTypes)) {
  67. $classes[] = 'table-' . $type;
  68. }
  69. }
  70. }
  71. }
  72. if (!empty($classes)) {
  73. $classes = implode(' ', $classes);
  74. if (isset($this->itemsCssClass)) {
  75. $this->itemsCssClass .= ' ' . $classes;
  76. } else {
  77. $this->itemsCssClass = $classes;
  78. }
  79. }
  80. $popover = Yii::app()->bootstrap->popoverSelector;
  81. $tooltip = Yii::app()->bootstrap->tooltipSelector;
  82. $afterAjaxUpdate = "js:function() {
  83. jQuery('.popover').remove();
  84. jQuery('{$popover}').popover();
  85. jQuery('.tooltip').remove();
  86. jQuery('{$tooltip}').tooltip();
  87. }";
  88. if (!isset($this->afterAjaxUpdate)) {
  89. $this->afterAjaxUpdate = $afterAjaxUpdate;
  90. }
  91. }
  92. /**
  93. *### .initColumns()
  94. *
  95. * Creates column objects and initializes them.
  96. */
  97. protected function initColumns()
  98. {
  99. foreach ($this->columns as $i => $column) {
  100. if (is_array($column) && !isset($column['class'])) {
  101. $this->columns[$i]['class'] = 'bootstrap.widgets.TbDataColumn';
  102. }
  103. }
  104. parent::initColumns();
  105. if ($this->responsiveTable) {
  106. $this->writeResponsiveCss();
  107. }
  108. }
  109. /**
  110. *### .createDataColumn()
  111. *
  112. * Creates a column based on a shortcut column specification string.
  113. *
  114. * @param mixed $text the column specification string
  115. *
  116. * @return \TbDataColumn|\CDataColumn the column instance
  117. * @throws CException if the column format is incorrect
  118. */
  119. protected function createDataColumn($text)
  120. {
  121. if (!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/', $text, $matches)) {
  122. throw new CException(Yii::t(
  123. 'zii',
  124. 'The column must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.'
  125. ));
  126. }
  127. $column = new TbDataColumn($this);
  128. $column->name = $matches[1];
  129. if (isset($matches[3]) && $matches[3] !== '') {
  130. $column->type = $matches[3];
  131. }
  132. if (isset($matches[5])) {
  133. $column->header = $matches[5];
  134. }
  135. return $column;
  136. }
  137. /**
  138. *### .writeResponsiveCss()
  139. *
  140. * Writes responsiveCSS
  141. */
  142. protected function writeResponsiveCss()
  143. {
  144. $cnt = 1;
  145. $labels = '';
  146. foreach ($this->columns as $column) {
  147. /** @var TbDataColumn $column */
  148. ob_start();
  149. $column->renderHeaderCell();
  150. $name = strip_tags(ob_get_clean());
  151. $labels .= "#$this->id td:nth-of-type($cnt):before { content: '{$name}'; }\n";
  152. $cnt++;
  153. }
  154. $css = <<<EOD
  155. @media
  156. only screen and (max-width: 760px),
  157. (min-device-width: 768px) and (max-device-width: 1024px) {
  158. /* Force table to not be like tables anymore */
  159. #{$this->id} table,#{$this->id} thead,#{$this->id} tbody,#{$this->id} th,#{$this->id} td,#{$this->id} tr {
  160. display: block;
  161. }
  162. /* Hide table headers (but not display: none;, for accessibility) */
  163. #{$this->id} thead tr {
  164. position: absolute;
  165. top: -9999px;
  166. left: -9999px;
  167. }
  168. #{$this->id} tr { border: 1px solid #ccc; }
  169. #{$this->id} td {
  170. /* Behave like a "row" */
  171. border: none;
  172. border-bottom: 1px solid #eee;
  173. position: relative;
  174. padding-left: 50%;
  175. }
  176. #{$this->id} td:before {
  177. /* Now like a table header */
  178. position: absolute;
  179. /* Top/left values mimic padding */
  180. top: 6px;
  181. left: 6px;
  182. width: 45%;
  183. padding-right: 10px;
  184. white-space: nowrap;
  185. }
  186. .grid-view .button-column {
  187. text-align: left;
  188. width:auto;
  189. }
  190. /*
  191. Label the data
  192. */
  193. {$labels}
  194. }
  195. EOD;
  196. Yii::app()->clientScript->registerCss(__CLASS__ . '#' . $this->id, $css);
  197. }
  198. }