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: *
29: *
30: * @category Mage
31: * @package Mage_Adminhtml
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Adminhtml_Block_Widget_Grid_Serializer extends Mage_Core_Block_Template
35: {
36:
37: /**
38: * Store grid input names to serialize
39: *
40: * @var array
41: */
42: private $_inputsToSerialize = array();
43:
44: /**
45: * Set serializer template
46: *
47: * @return Mage_Adminhtml_Block_Widget_Grid_Serializer
48: */
49: public function _construct()
50: {
51: parent::_construct();
52: $this->setTemplate('widget/grid/serializer.phtml');
53: return $this;
54: }
55:
56: /**
57: * Register grid column input name to serialize
58: *
59: * @param string $name
60: */
61: public function addColumnInputName($names)
62: {
63: if (is_array($names)) {
64: foreach ($names as $name) {
65: $this->addColumnInputName($name);
66: }
67: }
68: else {
69: if (!in_array($names, $this->_inputsToSerialize)) {
70: $this->_inputsToSerialize[] = $names;
71: }
72: }
73: }
74:
75: /**
76: * Get grid column input names to serialize
77: *
78: * @return unknown
79: */
80: public function getColumnInputNames($asJSON = false)
81: {
82: if ($asJSON) {
83: return Mage::helper('core')->jsonEncode($this->_inputsToSerialize);
84: }
85: return $this->_inputsToSerialize;
86: }
87:
88: /**
89: * Get object data as JSON
90: *
91: * @return string
92: */
93: public function getDataAsJSON()
94: {
95: $result = array();
96: if ($serializeData = $this->getSerializeData()) {
97: $result = $serializeData;
98: }
99: elseif (!empty($this->_inputsToSerialize)) {
100: return '{}';
101: }
102: return Mage::helper('core')->jsonEncode($result);
103: }
104:
105:
106: /**
107: * Initialize grid block
108: *
109: * Get grid block from layout by specified block name
110: * Get serialize data to manage it (called specified method, that return data to manage)
111: * Also use reload param name for saving grid checked boxes states
112: *
113: *
114: * @param Mage_Adminhtml_Block_Widget_Grid | string $grid grid object or grid block name
115: * @param string $callback block method to retrieve data to serialize
116: * @param string $hiddenInputName hidden input name where serialized data will be store
117: * @param string $reloadParamName name of request parametr that will be used to save setted data while reload grid
118: */
119: public function initSerializerBlock($grid, $callback, $hiddenInputName, $reloadParamName = 'entityCollection')
120: {
121: if (is_string($grid)) {
122: $grid = $this->getLayout()->getBlock($grid);
123: }
124: if ($grid instanceof Mage_Adminhtml_Block_Widget_Grid) {
125: $this->setGridBlock($grid)
126: ->setInputElementName($hiddenInputName)
127: ->setReloadParamName($reloadParamName)
128: ->setSerializeData($grid->$callback());
129: }
130: }
131:
132: }
133: