1: <?php
2:
3: App::uses('AppHelper', 'View/Helper');
4:
5: class FieldSetLayoutHelper extends AppHelper {
6:
7: public $helpers = array(
8: 'Html',
9: );
10:
11: 12: 13: 14: 15: 16:
17: public function fieldSet($lines, $options = array()) {
18: return $this->Html->tag(
19: 'table', $this->_rows($lines),
20: $this->Html->addClass($options, __CLASS__)
21: );
22: }
23:
24: 25: 26: 27: 28: 29:
30: private function _rows($lines) {
31: $b = '';
32: $lineIndex = 0;
33: $lineCount = count($lines);
34: $maxColumnCount = $this->_maxColumnCount($lines);
35: foreach ($lines as $line) {
36: $b .= $this->_row(
37: $line, compact('lineIndex', 'lineCount', 'maxColumnCount')
38: );
39: $lineIndex++;
40: }
41: return $b;
42: }
43:
44: 45: 46: 47: 48: 49:
50: private function _row($line, $options) {
51: $b = '';
52: $columnIndex = 0;
53: $columnCount = count($line);
54: $columnExpandIndex = $this->_columnExpandIndex($line);
55: foreach ($line as $label => $content) {
56: $b .= $this->_column($label, $content,
57: array_merge(compact(
58: 'columnIndex', 'columnCount',
59: 'columnExpandIndex'
60: ), $options));
61: }
62: return $this->Html->tag('tr', $b);
63: }
64:
65: 66: 67: 68: 69: 70: 71:
72: private function _column($label, $content, $options) {
73: $b = $this->Html->tag('th', $label);
74: $b .= $this->Html->tag('td', $content,
75: array(
76: 'colspan' => $this->_fieldColumnSpan($options)
77: ));
78: return $b;
79: }
80:
81: 82: 83: 84: 85:
86: private function _fieldColumnSpan($options) {
87: if ($options['columnIndex'] == $options['columnExpandIndex']) {
88: return ($options['maxColumnCount'] * 2) -
89: ($options['columnCount'] - 1) * 2 -
90: 1;
91: } else {
92: return 1;
93: }
94: }
95:
96: 97: 98: 99: 100:
101: private function _maxColumnCount($lines) {
102: return count($lines) > 0 ? max(array_map('count', $lines)) : 0;
103: }
104:
105: 106: 107: 108: 109:
110: private function _columnExpandIndex($line) {
111: foreach ($line as $index => $column) {
112: if (!empty($column['expand']) && $column['expand']) {
113: return $index;
114: }
115: }
116: return 0;
117: }
118:
119: }
120: