1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Adminhtml
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Adminhtml system config array field renderer
29: *
30: * @category Mage
31: * @package Mage_Adminhtml
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: abstract class Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract extends Mage_Adminhtml_Block_System_Config_Form_Field
35: {
36: /**
37: * Grid columns
38: *
39: * @var array
40: */
41: protected $_columns = array();
42:
43: /**
44: * Enable the "Add after" button or not
45: *
46: * @var bool
47: */
48: protected $_addAfter = true;
49:
50: /**
51: * Label of add button
52: *
53: * @var unknown_type
54: */
55: protected $_addButtonLabel;
56:
57: /**
58: * Rows cache
59: *
60: * @var array|null
61: */
62: private $_arrayRowsCache;
63:
64: /**
65: * Indication whether block is prepared to render or no
66: *
67: * @var bool
68: */
69: protected $_isPreparedToRender = false;
70:
71: /**
72: * Check if columns are defined, set template
73: *
74: */
75: public function __construct()
76: {
77: if (!$this->_addButtonLabel) {
78: $this->_addButtonLabel = Mage::helper('adminhtml')->__('Add');
79: }
80: parent::__construct();
81: if (!$this->getTemplate()) {
82: $this->setTemplate('system/config/form/field/array.phtml');
83: }
84: }
85:
86: /**
87: * Add a column to array-grid
88: *
89: * @param string $name
90: * @param array $params
91: */
92: public function addColumn($name, $params)
93: {
94: $this->_columns[$name] = array(
95: 'label' => empty($params['label']) ? 'Column' : $params['label'],
96: 'size' => empty($params['size']) ? false : $params['size'],
97: 'style' => empty($params['style']) ? null : $params['style'],
98: 'class' => empty($params['class']) ? null : $params['class'],
99: 'renderer' => false,
100: );
101: if ((!empty($params['renderer'])) && ($params['renderer'] instanceof Mage_Core_Block_Abstract)) {
102: $this->_columns[$name]['renderer'] = $params['renderer'];
103: }
104: }
105:
106: /**
107: * Get the grid and scripts contents
108: *
109: * @param Varien_Data_Form_Element_Abstract $element
110: * @return string
111: */
112: protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
113: {
114: $this->setElement($element);
115: $html = $this->_toHtml();
116: $this->_arrayRowsCache = null; // doh, the object is used as singleton!
117: return $html;
118: }
119:
120: /**
121: * Prepare existing row data object
122: *
123: * @param Varien_Object
124: */
125: protected function _prepareArrayRow(Varien_Object $row)
126: {
127: // override in descendants
128: }
129:
130: /**
131: * Obtain existing data from form element
132: *
133: * Each row will be instance of Varien_Object
134: *
135: * @return array
136: */
137: public function getArrayRows()
138: {
139: if (null !== $this->_arrayRowsCache) {
140: return $this->_arrayRowsCache;
141: }
142: $result = array();
143: /** @var Varien_Data_Form_Element_Abstract */
144: $element = $this->getElement();
145: if ($element->getValue() && is_array($element->getValue())) {
146: foreach ($element->getValue() as $rowId => $row) {
147: foreach ($row as $key => $value) {
148: $row[$key] = $this->htmlEscape($value);
149: }
150: $row['_id'] = $rowId;
151: $result[$rowId] = new Varien_Object($row);
152: $this->_prepareArrayRow($result[$rowId]);
153: }
154: }
155: $this->_arrayRowsCache = $result;
156: return $this->_arrayRowsCache;
157: }
158:
159: /**
160: * Render array cell for prototypeJS template
161: *
162: * @param string $columnName
163: * @return string
164: */
165: protected function _renderCellTemplate($columnName)
166: {
167: if (empty($this->_columns[$columnName])) {
168: throw new Exception('Wrong column name specified.');
169: }
170: $column = $this->_columns[$columnName];
171: $inputName = $this->getElement()->getName() . '[#{_id}][' . $columnName . ']';
172:
173: if ($column['renderer']) {
174: return $column['renderer']->setInputName($inputName)->setColumnName($columnName)->setColumn($column)
175: ->toHtml();
176: }
177:
178: return '<input type="text" name="' . $inputName . '" value="#{' . $columnName . '}" ' .
179: ($column['size'] ? 'size="' . $column['size'] . '"' : '') . ' class="' .
180: (isset($column['class']) ? $column['class'] : 'input-text') . '"'.
181: (isset($column['style']) ? ' style="'.$column['style'] . '"' : '') . '/>';
182: }
183:
184: /**
185: * Prepare to render
186: */
187: protected function _prepareToRender()
188: {
189: // Override in descendants to add columns, change add button label etc
190: }
191:
192: /**
193: * Render block HTML
194: *
195: * @return string
196: */
197: protected function _toHtml()
198: {
199: if (!$this->_isPreparedToRender) {
200: $this->_prepareToRender();
201: $this->_isPreparedToRender = true;
202: }
203: if (empty($this->_columns)) {
204: throw new Exception('At least one column must be defined.');
205: }
206: return parent::_toHtml();
207: }
208: }
209: