00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 abstract class Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract extends Mage_Adminhtml_Block_System_Config_Form_Field
00035 {
00036
00037
00038
00039
00040
00041 protected $_columns = array();
00042
00043
00044
00045
00046
00047
00048 protected $_addAfter = true;
00049
00050
00051
00052
00053
00054
00055 protected $_addButtonLabel;
00056
00057 private $_arrayRowsCache;
00058
00059
00060
00061
00062
00063 public function __construct()
00064 {
00065 if (empty($this->_columns)) {
00066 throw new Exception('At least one column must be defined.');
00067 }
00068 if (!$this->_addButtonLabel) {
00069 $this->_addButtonLabel = Mage::helper('adminhtml')->__('Add');
00070 }
00071 parent::__construct();
00072 if (!$this->getTemplate()) {
00073 $this->setTemplate('system/config/form/field/array.phtml');
00074 }
00075 }
00076
00077
00078
00079
00080
00081
00082
00083 public function addColumn($name, $params)
00084 {
00085 $this->_columns[$name] = array(
00086 'label' => empty($params['label']) ? 'Column' : $params['label'],
00087 'size' => empty($params['size']) ? false : $params['size'],
00088 'style' => empty($params['style']) ? null : $params['style'],
00089 'class' => empty($params['class']) ? null : $params['class'],
00090 'renderer' => false,
00091 );
00092 if ((!empty($params['renderer'])) && ($params['renderer'] instanceof Mage_Core_Block_Abstract)) {
00093 $this->_columns[$name]['renderer'] = $params['renderer'];
00094 }
00095 }
00096
00097
00098
00099
00100
00101
00102
00103 protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
00104 {
00105 $this->setElement($element);
00106 $html = $this->_toHtml();
00107 $this->_arrayRowsCache = null;
00108 return $html;
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118 public function getArrayRows()
00119 {
00120 if (null !== $this->_arrayRowsCache) {
00121 return $this->_arrayRowsCache;
00122 }
00123 $result = array();
00124
00125 $element = $this->getElement();
00126 if ($element->getValue() && is_array($element->getValue())) {
00127 foreach ($element->getValue() as $rowId => $row) {
00128 foreach ($row as $key => $value) {
00129 $row[$key] = $this->htmlEscape($value);
00130 }
00131 $row['_id'] = $rowId;
00132 $result[$rowId] = new Varien_Object($row);
00133 }
00134 }
00135 $this->_arrayRowsCache = $result;
00136 return $this->_arrayRowsCache;
00137 }
00138
00139
00140
00141
00142
00143
00144
00145 protected function _renderCellTemplate($columnName)
00146 {
00147 if (empty($this->_columns[$columnName])) {
00148 throw new Exception('Wrong column name specified.');
00149 }
00150 $column = $this->_columns[$columnName];
00151 $inputName = $this->getElement()->getName() . '[#{_id}][' . $columnName . ']';
00152
00153 if ($column['renderer']) {
00154 return $column['renderer']->setInputName($inputName)->setColumnName($columnName)->setColumn($column)
00155 ->toHtml();
00156 }
00157
00158 return '<input type="text" name="' . $inputName . '" value="#{' . $columnName . '}" ' .
00159 ($column['size'] ? 'size="' . $column['size'] . '"' : '') . ' class="' .
00160 (isset($column['class']) ? $column['class'] : 'input-text') . '"'.
00161 (isset($column['style']) ? ' style="'.$column['style'] . '"' : '') . '/>';
00162 }
00163 }