1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17: 18: 19: 20: 21: 22: 23:
24: class Horde_View_Helper_Form_InstanceTag_Base extends Horde_View_Helper_Tag
25: {
26: protected $_defaultFieldOptions = array('size' => 30);
27: protected $_defaultRadioOptions = array();
28: protected $_defaultTextAreaOptions = array('cols' => 40, 'rows' => 20);
29: protected $_defaultDateOptions = array('discardType' => true);
30:
31: protected $objectName;
32: protected $objectProperty;
33: protected $object;
34: protected $autoIndex;
35:
36: 37:
38: public function __construct($objectName, $objectProperty, $view,
39: $object = null)
40: {
41: $this->_view = $view;
42: $this->objectProperty = $objectProperty;
43: $this->object = $object;
44:
45: if (strpos($objectName, '[]')) {
46: $objectName = rtrim($objectName, '[]');
47: if (!isset($object)) {
48: $object = $view->{$objectName};
49: }
50: if (isset($object) && isset($object->id_before_type_cast)) {
51: $this->autoIndex = $object->id_before_type_cast;
52: } else {
53: throw new InvalidArgumentException("object[] naming but object param and @object var don't exist or don't respond to id_before_type_cast");
54: }
55: }
56:
57: $this->objectName = $objectName;
58: }
59:
60: public function object()
61: {
62: if (isset($this->object)) {
63: return $this->object;
64: } else {
65: return $this->_view->{$this->objectName};
66: }
67: }
68:
69: public function value($object)
70: {
71: if (is_object($object)) {
72: return $object->{$this->objectProperty};
73: } else {
74: return null;
75: }
76: }
77:
78: protected function valueBeforeTypeCast($object)
79: {
80: if (!is_object($object)) {
81: return null;
82: }
83: if (isset($object->{"{$this->objectProperty}_before_type_cast"})) {
84: return $object->{"{$this->objectProperty}_before_type_cast"};
85: }
86: if (isset($object->{$this->objectProperty})) {
87: return $object->{$this->objectProperty};
88: }
89: return null;
90: }
91:
92: protected function addDefaultNameAndId($options)
93: {
94: if (isset($options['index'])) {
95: if (!isset($options['name'])) {
96: $options['name'] = $this->tagNameWithIndex($options['index']);
97: }
98: if (!isset($options['id'])) {
99: $options['id'] = $this->tagIdWithIndex($options['index']);
100: }
101: unset($options['index']);
102: } elseif (isset($this->autoIndex)) {
103: if (!isset($options['name'])) {
104: $options['name'] = $this->tagNameWithIndex($this->autoIndex);
105: }
106: if (!isset($options['id'])) {
107: $options['id'] = $this->tagIdWithIndex($this->autoIndex);
108: }
109: } else {
110: if (!isset($options['name'])) {
111: $options['name'] = $this->tagName()
112: . (isset($options['multiple']) ? '[]' : '');
113: }
114: if (!isset($options['id'])) {
115: $options['id'] = $this->tagId();
116: }
117: }
118: return $options;
119: }
120:
121: protected function tagName()
122: {
123: return "{$this->objectName}[$this->objectProperty]";
124: }
125:
126: protected function tagNameWithIndex($index)
127: {
128: return "{$this->objectName}[$index][$this->objectProperty]";
129: }
130:
131: protected function tagId()
132: {
133: return $this->sanitizedObjectName() . "_{$this->objectProperty}";
134: }
135:
136: protected function tagIdWithIndex($index)
137: {
138: return $this->sanitizedObjectName() . "_{$index}_{$this->objectProperty}";
139: }
140:
141: protected function sanitizedObjectName()
142: {
143: $name = preg_replace('/[^-a-zA-Z0-9:.]/', '_', $this->objectName);
144: $name = preg_replace('/_$/', '', $name);
145: return $name;
146: }
147: }
148: