1: <?php
2:
3: namespace Zippy\Html\DataList;
4:
5: use Zippy\Html\HtmlComponent;
6: use Zippy\Html\HtmlContainer;
7: use Zippy\Interfaces\DataItem;
8: use Zippy\Html\Form\RadioButton;
9: use Zippy\Exception as ZE;
10:
11: /**
12: * Класс строки табличных данных
13: */
14: class DataRow extends HtmlContainer
15: {
16: private $number;
17: private $dataitem = null;
18:
19: /**
20: * Конструктор
21: * @param DataItem Елемент данных отображаемый строкой таблицы
22: * @param mixed Номер строки
23: */
24: public function __construct($id, DataItem $dataitem, $number) {
25:
26: $this->number = $number;
27: HtmlComponent::__construct($id . "_" . $this->number);
28: $this->dataitem = $dataitem;
29:
30:
31:
32: }
33:
34: /**
35: * Установить данные в строку
36: * @param DataItem Елемент данных отображаемый строкой таблицы
37: */
38: public function setDataItem($dataitem) {
39: $this->dataitem = $dataitem;
40: }
41:
42: /**
43: * Возвращает уникальный ключ елемента данных
44: * @return string
45: */
46: public function getItemId() {
47: if ($this->dataitem instanceof DataItem) {
48: return $this->dataitem->getID();
49: } else {
50: return null;
51: }
52: }
53:
54: /**
55: * Возвращает елемент данных
56: * @return DataItem
57: */
58: public function getDataItem() {
59: return $this->dataitem;
60: }
61:
62: /**
63: * Добавляет компонент в строку-контейнер
64: * переопределяет родительский метод
65: * @param HtmlComponent добавляемый компонент
66: */
67: public function add(HtmlComponent $component) {
68:
69: if (isset($this->components[$component->id]) || isset($this->components[$component->id . ":" . $this->number])) {
70: // $id = substr($component->id,0,strpos($component->id,':'));
71: // $pid = substr($this->id,0,strpos($this->id,':'));
72: throw new ZE(sprintf(ERROR_DATAROW_COMPONENT_EXISTS, $component->id));
73: }
74: $this->components[$component->id] = $component;
75: $component->setOwner($this);
76: return $component;
77: }
78:
79: /**
80: * Обновляет ID компонентов строки с воответствии с номером строки
81: */
82: public function updateChildId() {
83: $allchild = $this->getChildComponents(true);
84: foreach ($allchild as $component) {
85: $_id = $component->id;
86: $id = "_" . $this->number;
87: $component->id .= $id;
88: $attrid = $component->getAttribute("id");
89:
90: if ($attrid != null) {
91: $component->setAttribute("id", $attrid . $id);
92: }
93: $attrid = $component->getAttribute("name");
94: if ($attrid != null && $component instanceof RadioButton == false) {
95: $component->setAttribute("name", $attrid . $id);
96: }
97: unset($component->owner->components[$_id]);
98: $component->owner->components[$component->id] = $component;
99:
100: if ($component instanceof AbstractList) {
101: $component->Refresh();
102: }
103: }
104: }
105:
106:
107: /**
108: * уникальный идентификатор строки
109: * @return int
110: */
111: public function getNumber() {
112: return $this->number;
113: }
114:
115:
116:
117: /**
118: * Возвращает дочерний элемент по ID
119: * @return
120: */
121: public function getChildElement($id) {
122: return $this->{$id . '_' . $this->getNumber()};
123: }
124:
125: /**
126: * Получить дочерний компонент
127: *
128: * @param string ID компонента
129: * @param boolean Если false - искать только непосредственнно вложенных
130: */
131: public function getComponent($id, $desc = true) {
132: $c = parent::getComponent($id, $desc);
133: if ($c instanceof HtmlComponent) {
134: return $c;
135: }
136: $id = $id . '_' . $this->number;
137: return parent::getComponent($id, $desc);
138: }
139:
140: }
141: