PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Adminhtml/Block/System/Config/Form/Field/Array/Abstract.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 208 lines | 92 code | 15 blank | 101 comment | 11 complexity | 00b2060b40a561793444e9b3e3012bbc MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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) 2010 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. * Adminhtml system config array field renderer
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract extends Mage_Adminhtml_Block_System_Config_Form_Field
  34. {
  35. /**
  36. * Grid columns
  37. *
  38. * @var array
  39. */
  40. protected $_columns = array();
  41. /**
  42. * Enable the "Add after" button or not
  43. *
  44. * @var bool
  45. */
  46. protected $_addAfter = true;
  47. /**
  48. * Label of add button
  49. *
  50. * @var unknown_type
  51. */
  52. protected $_addButtonLabel;
  53. /**
  54. * Rows cache
  55. *
  56. * @var array|null
  57. */
  58. private $_arrayRowsCache;
  59. /**
  60. * Indication whether block is prepared to render or no
  61. *
  62. * @var bool
  63. */
  64. protected $_isPreparedToRender = false;
  65. /**
  66. * Check if columns are defined, set template
  67. *
  68. */
  69. public function __construct()
  70. {
  71. if (!$this->_addButtonLabel) {
  72. $this->_addButtonLabel = Mage::helper('adminhtml')->__('Add');
  73. }
  74. parent::__construct();
  75. if (!$this->getTemplate()) {
  76. $this->setTemplate('system/config/form/field/array.phtml');
  77. }
  78. }
  79. /**
  80. * Add a column to array-grid
  81. *
  82. * @param string $name
  83. * @param array $params
  84. */
  85. public function addColumn($name, $params)
  86. {
  87. $this->_columns[$name] = array(
  88. 'label' => empty($params['label']) ? 'Column' : $params['label'],
  89. 'size' => empty($params['size']) ? false : $params['size'],
  90. 'style' => empty($params['style']) ? null : $params['style'],
  91. 'class' => empty($params['class']) ? null : $params['class'],
  92. 'renderer' => false,
  93. );
  94. if ((!empty($params['renderer'])) && ($params['renderer'] instanceof Mage_Core_Block_Abstract)) {
  95. $this->_columns[$name]['renderer'] = $params['renderer'];
  96. }
  97. }
  98. /**
  99. * Get the grid and scripts contents
  100. *
  101. * @param Varien_Data_Form_Element_Abstract $element
  102. * @return string
  103. */
  104. protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
  105. {
  106. $this->setElement($element);
  107. $html = $this->_toHtml();
  108. $this->_arrayRowsCache = null; // doh, the object is used as singleton!
  109. return $html;
  110. }
  111. /**
  112. * Prepare existing row data object
  113. *
  114. * @param Varien_Object
  115. */
  116. protected function _prepareArrayRow(Varien_Object $row)
  117. {
  118. // override in descendants
  119. }
  120. /**
  121. * Obtain existing data from form element
  122. *
  123. * Each row will be instance of Varien_Object
  124. *
  125. * @return array
  126. */
  127. public function getArrayRows()
  128. {
  129. if (null !== $this->_arrayRowsCache) {
  130. return $this->_arrayRowsCache;
  131. }
  132. $result = array();
  133. /** @var Varien_Data_Form_Element_Abstract */
  134. $element = $this->getElement();
  135. if ($element->getValue() && is_array($element->getValue())) {
  136. foreach ($element->getValue() as $rowId => $row) {
  137. foreach ($row as $key => $value) {
  138. $row[$key] = $this->htmlEscape($value);
  139. }
  140. $row['_id'] = $rowId;
  141. $result[$rowId] = new Varien_Object($row);
  142. $this->_prepareArrayRow($result[$rowId]);
  143. }
  144. }
  145. $this->_arrayRowsCache = $result;
  146. return $this->_arrayRowsCache;
  147. }
  148. /**
  149. * Render array cell for prototypeJS template
  150. *
  151. * @param string $columnName
  152. * @return string
  153. */
  154. protected function _renderCellTemplate($columnName)
  155. {
  156. if (empty($this->_columns[$columnName])) {
  157. throw new Exception('Wrong column name specified.');
  158. }
  159. $column = $this->_columns[$columnName];
  160. $inputName = $this->getElement()->getName() . '[#{_id}][' . $columnName . ']';
  161. if ($column['renderer']) {
  162. return $column['renderer']->setInputName($inputName)->setColumnName($columnName)->setColumn($column)
  163. ->toHtml();
  164. }
  165. return '<input type="text" name="' . $inputName . '" value="#{' . $columnName . '}" ' .
  166. ($column['size'] ? 'size="' . $column['size'] . '"' : '') . ' class="' .
  167. (isset($column['class']) ? $column['class'] : 'input-text') . '"'.
  168. (isset($column['style']) ? ' style="'.$column['style'] . '"' : '') . '/>';
  169. }
  170. /**
  171. * Prepare to render
  172. */
  173. protected function _prepareToRender()
  174. {
  175. // Override in descendants to add columns, change add button label etc
  176. }
  177. /**
  178. * Render block HTML
  179. *
  180. * @return string
  181. */
  182. protected function _toHtml()
  183. {
  184. if (!$this->_isPreparedToRender) {
  185. $this->_prepareToRender();
  186. $this->_isPreparedToRender = true;
  187. }
  188. if (empty($this->_columns)) {
  189. throw new Exception('At least one column must be defined.');
  190. }
  191. return parent::_toHtml();
  192. }
  193. }