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_ImportExport
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: * Import frame result block.
29: *
30: * @category Mage
31: * @package Mage_ImportExport
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_ImportExport_Block_Adminhtml_Import_Frame_Result extends Mage_Adminhtml_Block_Template
35: {
36: /**
37: * JavaScript actions for response.
38: *
39: * @var array
40: */
41: protected $_actions = array(
42: 'clear' => array(), // remove element from DOM
43: 'innerHTML' => array(), // set innerHTML property (use: elementID => new content)
44: 'value' => array(), // set value for form element (use: elementID => new value)
45: 'show' => array(), // show specified element
46: 'hide' => array(), // hide specified element
47: 'removeClassName' => array(), // remove specified class name from element
48: 'addClassName' => array() // add specified class name to element
49: );
50:
51: /**
52: * Validation messages.
53: *
54: * @var array
55: */
56: protected $_messages = array(
57: 'error' => array(),
58: 'success' => array(),
59: 'notice' => array()
60: );
61:
62: /**
63: * Add action for response.
64: *
65: * @param string $actionName
66: * @param string $elementId
67: * @param mixed $value OPTIONAL
68: * @return Mage_ImportExport_Block_Adminhtml_Import_Frame_Result
69: */
70: public function addAction($actionName, $elementId, $value = null)
71: {
72: if (isset($this->_actions[$actionName])) {
73: if (null === $value) {
74: if (is_array($elementId)) {
75: foreach ($elementId as $oneId) {
76: $this->_actions[$actionName][] = $oneId;
77: }
78: } else {
79: $this->_actions[$actionName][] = $elementId;
80: }
81: } else {
82: $this->_actions[$actionName][$elementId] = $value;
83: }
84: }
85: return $this;
86: }
87:
88: /**
89: * Add error message.
90: *
91: * @param string $message Error message
92: * @return Mage_ImportExport_Block_Adminhtml_Import_Frame_Result
93: */
94: public function addError($message)
95: {
96: if (is_array($message)) {
97: foreach ($message as $row) {
98: $this->addError($row);
99: }
100: } else {
101: $this->_messages['error'][] = $message;
102: }
103: return $this;
104: }
105:
106: /**
107: * Add notice message.
108: *
109: * @param mixed $message Message text
110: * @param boolean $appendImportButton OPTIONAL Append import button to message?
111: * @return Mage_ImportExport_Block_Adminhtml_Import_Frame_Result
112: */
113: public function addNotice($message, $appendImportButton = false)
114: {
115: if (is_array($message)) {
116: foreach ($message as $row) {
117: $this->addNotice($row);
118: }
119: } else {
120: $this->_messages['notice'][] = $message . ($appendImportButton ? $this->getImportButtonHtml() : '');
121: }
122: return $this;
123: }
124:
125: /**
126: * Add success message.
127: *
128: * @param mixed $message Message text
129: * @param boolean $appendImportButton OPTIONAL Append import button to message?
130: * @return Mage_ImportExport_Block_Adminhtml_Import_Frame_Result
131: */
132: public function addSuccess($message, $appendImportButton = false)
133: {
134: if (is_array($message)) {
135: foreach ($message as $row) {
136: $this->addSuccess($row);
137: }
138: } else {
139: $this->_messages['success'][] = $message . ($appendImportButton ? $this->getImportButtonHtml() : '');
140: }
141: return $this;
142: }
143:
144: /**
145: * Import button HTML for append to message.
146: *
147: * @return string
148: */
149: public function getImportButtonHtml()
150: {
151: return ' <button onclick="editForm.startImport(\'' . $this->getImportStartUrl()
152: . '\', \'' . Mage_ImportExport_Model_Import::FIELD_NAME_SOURCE_FILE . '\');" class="scalable save"'
153: . ' type="button"><span><span><span>' . $this->__('Import') . '</span></span></span></button>';
154: }
155:
156: /**
157: * Import start action URL.
158: *
159: * @return string
160: */
161: public function getImportStartUrl()
162: {
163: return $this->getUrl('*/*/start');
164: }
165:
166: /**
167: * Messages getter.
168: *
169: * @return array
170: */
171: public function getMessages()
172: {
173: return $this->_messages;
174: }
175:
176: /**
177: * Messages rendered HTML getter.
178: *
179: * @return string
180: */
181: public function getMessagesHtml()
182: {
183: /** @var $messagesBlock Mage_Core_Block_Messages */
184: $messagesBlock = $this->_layout->createBlock('core/messages');
185:
186: foreach ($this->_messages as $priority => $messages) {
187: $method = "add{$priority}";
188:
189: foreach ($messages as $message) {
190: $messagesBlock->$method($message);
191: }
192: }
193: return $messagesBlock->toHtml();
194: }
195:
196: /**
197: * Return response as JSON.
198: *
199: * @return string
200: */
201: public function getResponseJson()
202: {
203: // add messages HTML if it is not already specified
204: if (!isset($this->_actions['import_validation_messages'])) {
205: $this->addAction('innerHTML', 'import_validation_messages', $this->getMessagesHtml());
206: }
207: return Mage::helper('core')->jsonEncode($this->_actions);
208: }
209: }
210: