PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/engines/lcml/bcsTable.php

https://bitbucket.org/Balancer/bors-core
PHP | 172 lines | 126 code | 35 blank | 11 comment | 25 complexity | 8e736ed1a34f1651d306cbfb834fdae3 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. /*
  3. Используется composer: anahkiasen/html-object
  4. */
  5. class bcsTable
  6. {
  7. var $table_data;
  8. var $row_spans;
  9. var $col_spans;
  10. var $heads;
  11. var $rows;
  12. var $cols;
  13. var $row;
  14. var $col;
  15. var $table_width;
  16. var $table_border;
  17. function rewind()
  18. {
  19. $this->row = $this->col = 0;
  20. }
  21. function __construct($args = array())
  22. {
  23. $this->layout = defval($args, 'layout');
  24. // var_dump($layout);
  25. $this->data = array();
  26. $this->row_spans = array();
  27. $this->col_spans = array();
  28. $this->heads = array();
  29. $this->rows = $this->cols = 0;
  30. $this->rewind();
  31. $this->table_width = '';
  32. }
  33. function table_width($width)
  34. {
  35. $this->table_width = $width;
  36. }
  37. function set_max($row = NULL, $col = NULL)
  38. {
  39. if(is_null($col))
  40. $col = $this->col;
  41. if(is_null($row))
  42. $row = $this->row;
  43. if($col >= $this->cols)
  44. $this->cols = $col + 1;
  45. if($row >= $this->rows)
  46. $this->rows = $row + 1;
  47. }
  48. function next_col()
  49. {
  50. if(!empty($this->col_spans[$this->row][$this->col]) && $this->col_spans[$this->row][$this->col] > 1)
  51. $this->col += $this->col_spans[$this->row][$this->col];
  52. else
  53. $this->col++;
  54. }
  55. function new_row()
  56. {
  57. $this->col = 0;
  58. $this->set_max();
  59. $this->row++;
  60. }
  61. function setData($data, $row = NULL, $col = NULL)
  62. {
  63. if(is_null($col))
  64. $col = $this->col;
  65. if(is_null($row))
  66. $row = $this->row;
  67. $this->set_max($row, $col);
  68. $this->data[$row][$col] = $data;
  69. }
  70. function append($data)
  71. {
  72. $this->setData($data);
  73. $this->next_col();
  74. }
  75. function setColSpan($col_span)
  76. {
  77. $this->col_spans[$this->row][$this->col] = $col_span;
  78. }
  79. function setRowSpan($row_span)
  80. {
  81. $this->row_spans[$this->row][$this->col] = $row_span;
  82. for($i=1; $i<$row_span; $i++)
  83. $this->row_spans[$this->row+$i][$this->col] = -1;
  84. }
  85. function setHead($head_bit=1, $row = NULL, $col = NULL)
  86. {
  87. $this->heads[is_null($row) ? $this->row : $row][is_null($col) ? $this->col : $col] = $head_bit;
  88. }
  89. var $style = array();
  90. function addStyle($style) { $this->style[] = $style; }
  91. function get_html()
  92. {
  93. $table = \HtmlObject\Element::table();
  94. if($w = $this->table_width)
  95. {
  96. $table->width($w);
  97. if(is_numeric($w))
  98. $w = "{$w}px";
  99. $this->addStyle("width:{$w}!important");
  100. }
  101. for($r=0; $r < $this->rows; $r++)
  102. {
  103. $tr = \HtmlObject\Element::tr();
  104. for($c=0; $c < $this->cols; $c += (!empty($this->col_spans[$r][$c]) && $this->col_spans[$r][$c] > 1 ? $this->col_spans[$r][$c] : 1))
  105. {
  106. if(!empty($this->row_spans[$r][$c]) && $this->row_spans[$r][$c] < 0)
  107. continue;
  108. // Убрано из-за http://balancer.ru/2007/12/10/post-1361199.html
  109. // if(@$this->row_spans[$r][$c] < 0)
  110. // continue;
  111. // Тесты: http://balancer.ru/g/p2547411
  112. $data = @$this->data[$r][$c];
  113. if($data == '')
  114. $data = '&nbsp;';
  115. $tx = !empty($this->heads[$r][$c]) ? \HtmlObject\Element::th() : \HtmlObject\Element::td();
  116. if(!empty($this->col_spans[$r][$c]) && $this->col_spans[$r][$c] > 1)
  117. $tx->colspan($this->col_spans[$r][$c]);
  118. if(!empty($this->row_spans[$r][$c]) && $this->row_spans[$r][$c] > 1)
  119. $tx->rowspan($this->row_spans[$r][$c]);
  120. $tx->nest($data);
  121. $tr->nest($tx);
  122. }
  123. $table->nest($tr);
  124. }
  125. if($this->layout)
  126. {
  127. if($class = $this->layout->get('table_class'))
  128. $table->addClass($class);
  129. // else
  130. // $this->addstyle('border: 1px solid');
  131. }
  132. if($this->style)
  133. $table->style(join('; ', $this->style));
  134. // echo "<xmp>{$table}</xmp>";
  135. return $table;
  136. }
  137. }