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: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Adminhtml_Block_Customer_Form_Element_File extends Varien_Data_Form_Element_Abstract
36: {
37: 38: 39: 40: 41:
42: public function __construct($attributes = array())
43: {
44: parent::__construct($attributes);
45: $this->setType('file');
46: }
47:
48: 49: 50: 51: 52:
53: public function getElementHtml()
54: {
55: $this->addClass('input-file');
56: if ($this->getRequired()) {
57: $this->removeClass('required-entry');
58: $this->addClass('required-file');
59: }
60:
61: $element = sprintf('<input id="%s" name="%s" %s />%s%s',
62: $this->getHtmlId(),
63: $this->getName(),
64: $this->serialize($this->getHtmlAttributes()),
65: $this->getAfterElementHtml(),
66: $this->_getHiddenInput()
67: );
68:
69: return $this->_getPreviewHtml() . $element . $this->_getDeleteCheckboxHtml();
70: }
71:
72: 73: 74: 75: 76:
77: protected function _getDeleteCheckboxHtml()
78: {
79: $html = '';
80: if ($this->getValue() && !$this->getRequired() && !is_array($this->getValue())) {
81: $checkboxId = sprintf('%s_delete', $this->getHtmlId());
82: $checkbox = array(
83: 'type' => 'checkbox',
84: 'name' => sprintf('%s[delete]', $this->getName()),
85: 'value' => '1',
86: 'class' => 'checkbox',
87: 'id' => $checkboxId
88: );
89: $label = array(
90: 'for' => $checkboxId
91: );
92: if ($this->getDisabled()) {
93: $checkbox['disabled'] = 'disabled';
94: $label['class'] = 'disabled';
95: }
96:
97: $html .= '<span class="' . $this->_getDeleteCheckboxSpanClass() . '">';
98: $html .= $this->_drawElementHtml('input', $checkbox) . ' ';
99: $html .= $this->_drawElementHtml('label', $label, false) . $this->_getDeleteCheckboxLabel() . '</label>';
100: $html .= '</span>';
101: }
102: return $html;
103: }
104:
105: 106: 107: 108: 109:
110: protected function _getDeleteCheckboxSpanClass()
111: {
112: return 'delete-file';
113: }
114:
115: 116: 117: 118: 119:
120: protected function _getDeleteCheckboxLabel()
121: {
122: return Mage::helper('adminhtml')->__('Delete File');
123: }
124:
125: 126: 127: 128: 129:
130: protected function _getPreviewHtml()
131: {
132: $html = '';
133: if ($this->getValue() && !is_array($this->getValue())) {
134: $image = array(
135: 'alt' => Mage::helper('adminhtml')->__('Download'),
136: 'title' => Mage::helper('adminhtml')->__('Download'),
137: 'src' => Mage::getDesign()->getSkinUrl('images/fam_bullet_disk.gif'),
138: 'class' => 'v-middle'
139: );
140: $url = $this->_getPreviewUrl();
141: $html .= '<span>';
142: $html .= '<a href="' . $url . '">' . $this->_drawElementHtml('img', $image) . '</a> ';
143: $html .= '<a href="' . $url . '">' . Mage::helper('adminhtml')->__('Download') . '</a>';
144: $html .= '</span>';
145: }
146: return $html;
147: }
148:
149: 150: 151: 152: 153:
154: protected function _getHiddenInput()
155: {
156: return $this->_drawElementHtml('input', array(
157: 'type' => 'hidden',
158: 'name' => sprintf('%s[value]', $this->getName()),
159: 'id' => sprintf('%s_value', $this->getHtmlId()),
160: 'value' => $this->getEscapedValue()
161: ));
162: }
163:
164: 165: 166: 167: 168:
169: protected function _getPreviewUrl()
170: {
171: return Mage::helper('adminhtml')->getUrl('adminhtml/customer/viewfile', array(
172: 'file' => Mage::helper('core')->urlEncode($this->getValue()),
173: ));
174: }
175:
176: 177: 178: 179: 180: 181: 182: 183:
184: protected function _drawElementHtml($element, array $attributes, $closed = true)
185: {
186: $parts = array();
187: foreach ($attributes as $k => $v) {
188: $parts[] = sprintf('%s="%s"', $k, $v);
189: }
190:
191: return sprintf('<%s %s%s>', $element, implode(' ', $parts), $closed ? ' /' : '');
192: }
193:
194: 195: 196: 197: 198: 199:
200: public function getEscapedValue($index = null)
201: {
202: if (is_array($this->getValue())) {
203: return false;
204: }
205: $value = $this->getValue();
206: if (is_array($value) && is_null($index)) {
207: $index = 'value';
208: }
209:
210: return parent::getEscapedValue($index);
211: }
212: }
213: