1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22:
23: namespace picon;
24:
25: 26: 27: 28: 29: 30: 31:
32: class Check extends LabeledMarkupContainer
33: {
34: private $group;
35:
36: public function getLabel()
37: {
38: return $this->getModelObjectAsString();
39: }
40:
41: private function getGroup()
42: {
43: if($this->group!=null)
44: {
45: return $this->group;
46: }
47:
48: $group = null;
49:
50: $callback = function(Component &$component) use (&$group)
51: {
52: $group = $component;
53: return Component::VISITOR_STOP_TRAVERSAL;
54: };
55: $this->visitParents(CheckBoxGroup::getIdentifier(), $callback);
56:
57: $this->group = $group;
58:
59: if($group==null)
60: {
61: throw new \RuntimeException('A check must be a child of CheckBoxGroup');
62: }
63:
64: return $group;
65: }
66:
67: public function getValue()
68: {
69: return $this->getComponentPath();
70: }
71:
72: public function getName()
73: {
74: $group = $this->getGroup();
75: return $group->getName().'[]';
76: }
77:
78: protected function isSelected($value)
79: {
80: $group = $this->getGroup();
81: if(count($group->getRawInputArray())==0)
82: {
83: if($group->isEmptyInput())
84: {
85: return false;
86: }
87: else
88: {
89: return in_array($this->getModelObject(), $group->getModelObject());
90: }
91: }
92: else
93: {
94: return in_array($value, $group->getRawInputArray());
95: }
96: }
97:
98: protected function onComponentTag(ComponentTag $tag)
99: {
100: parent::onComponentTag($tag);
101: $this->checkComponentTag($tag, 'input');
102: $this->checkComponentTagAttribute($tag, 'type', 'checkbox');
103: $tag->put('value', $this->getValue());
104: $tag->put('name', $this->getName());
105:
106: $tag->remove('checked');
107: if($this->isSelected($this->getValue()))
108: {
109: $tag->put('checked', 'checked');
110: }
111: }
112: }
113: ?>
114: