1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Kotchasan;
12:
13: 14: 15: 16: 17: 18: 19:
20: class Accordion
21: {
22: 23: 24:
25: private $datas;
26: 27: 28:
29: private $id;
30: 31: 32:
33: private $type;
34:
35: 36: 37: 38: 39: 40: 41:
42: public function __construct($id, $items = array(), $onetab = false)
43: {
44: $this->id = $id;
45: $this->datas = empty($items) ? array() : $items;
46: $this->type = $onetab ? 'radio' : 'checkbox';
47: }
48:
49: 50: 51: 52: 53: 54: 55: 56:
57: public function add($title, $detail, $select = false, $className = 'article')
58: {
59: $this->datas[$title] = array(
60: 'detail' => $detail,
61: 'select' => $select,
62: 'className' => $className,
63: );
64: }
65:
66: 67: 68: 69: 70:
71: public function render()
72: {
73: $html = '<div class="accordion">';
74: $n = 1;
75: foreach ($this->datas as $title => $item) {
76: $html .= '<div class="item">';
77: $html .= '<input id="'.$this->id.$n.'" name="'.$this->id.'" type="'.$this->type.'"'.($item['select'] ? ' checked' : '').'>';
78: $html .= '<label for="'.$this->id.$n.'">'.$title.'</label>';
79: $html .= '<div class="body"><div class="'.$item['className'].'">'.$item['detail'].'</div></div>';
80: $html .= '</div>';
81: ++$n;
82: }
83:
84: return $html.'</div>';
85: }
86: }
87: