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_Dataflow
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: * Convert action abstract
30: *
31: * Instances of this class are used as actions in profile
32: *
33: * @category Mage
34: * @package Mage_Dataflow
35: * @author Magento Core Team <core@magentocommerce.com>
36: */
37: abstract class Mage_Dataflow_Model_Convert_Action_Abstract
38: implements Mage_Dataflow_Model_Convert_Action_Interface
39: {
40:
41: /**
42: * Action parameters
43: *
44: * Hold information about action container
45: *
46: * @var array
47: */
48: protected $_params;
49:
50: /**
51: * Reference to profile this action belongs to
52: *
53: * @var Mage_Dataflow_Model_Convert_Profile_Abstract
54: */
55: protected $_profile;
56:
57: protected $_actions = array();
58:
59: /**
60: * Action's container
61: *
62: * @var Mage_Dataflow_Model_Convert_Container_Abstract
63: */
64: protected $_container;
65:
66: protected $_actionDefaultClass = 'Mage_Dataflow_Model_Convert_Action';
67:
68: /**
69: * Get action parameter
70: *
71: * @param string $key
72: * @param mixed $default
73: * @return mixed
74: */
75: public function getParam($key, $default=null)
76: {
77: if (!isset($this->_params[$key])) {
78: return $default;
79: }
80: return $this->_params[$key];
81: }
82:
83: /**
84: * Set action parameter
85: *
86: * @param string $key
87: * @param mixed $value
88: * @return Mage_Dataflow_Model_Convert_Action_Abstract
89: */
90: public function setParam($key, $value=null)
91: {
92: if (is_array($key) && is_null($value)) {
93: $this->_params = $key;
94: } else {
95: $this->_params[$key] = $value;
96: }
97: return $this;
98: }
99:
100: /**
101: * Get all action parameters
102: *
103: * @return array
104: */
105: public function getParams()
106: {
107: return $this->_params;
108: }
109:
110: /**
111: * Set all action parameters
112: *
113: * @param array $params
114: * @return Mage_Dataflow_Model_Convert_Action_Abstract
115: */
116: public function setParams($params)
117: {
118: $this->_params = $params;
119: return $this;
120: }
121:
122: /**
123: * Get profile instance the action belongs to
124: *
125: * @return Mage_Dataflow_Model_Convert_Profile_Abstract
126: */
127: public function getProfile()
128: {
129: return $this->_profile;
130: }
131:
132: /**
133: * Set profile instance the action belongs to
134: *
135: * @param Mage_Dataflow_Model_Convert_Profile_Abstract $profile
136: * @return Mage_Dataflow_Model_Convert_Action_Abstract
137: */
138: public function setProfile(Mage_Dataflow_Model_Convert_Profile_Interface $profile)
139: {
140: $this->_profile = $profile;
141: return $this;
142: }
143:
144: public function addAction(Mage_Dataflow_Model_Convert_Action_Interface $action=null)
145: {
146: if (is_null($action)) {
147: $action = new $this->_actionDefaultClass();
148: }
149: $this->_actions[] = $action;
150: $action->setProfile($this->getProfile());
151: return $action;
152: }
153:
154: /**
155: * Set action's container
156: *
157: * @param Mage_Dataflow_Model_Convert_Container_Interface $container
158: * @return Mage_Dataflow_Model_Convert_Action_Abstract
159: */
160: public function setContainer(Mage_Dataflow_Model_Convert_Container_Interface $container)
161: {
162: $this->_container = $container;
163: $this->_container->setProfile($this->getProfile());
164: $this->_container->setAction($this);
165: return $this;
166: }
167:
168: /**
169: * Get action's container
170: *
171: * @param string $name
172: * @return Mage_Dataflow_Model_Convert_Container_Abstract
173: */
174: public function getContainer($name=null)
175: {
176: if (!is_null($name)) {
177: return $this->getProfile()->getContainer($name);
178: }
179:
180: if (!$this->_container) {
181: $class = $this->getParam('class');
182: $this->setContainer(new $class());
183: }
184: return $this->_container;
185: }
186:
187: public function importXml(Varien_Simplexml_Element $actionNode)
188: {
189: foreach ($actionNode->attributes() as $key=>$value) {
190: $this->setParam($key, (string)$value);
191: }
192:
193: if ($actionNode['use']) {
194: $container = $this->getProfile()->getContainer((string)$actionNode['use']);
195: } else {
196: $this->setParam('class', $this->getClassNameByType((string)$actionNode['type']));
197: $container = $action->getContainer();
198: }
199: $this->setContainer($container);
200: if ($this->getParam('name')) {
201: $this->getProfile()->addContainer($this->getParam('name'), $container);
202: }
203: foreach ($actionNode->var as $varNode) {
204: $container->setVar((string)$varNode['name'], (string)$varNode);
205: }
206: foreach ($actionNode->action as $actionSubnode) {
207: $action = $this->addAction();
208: $action->importXml($actionSubnode);
209: }
210:
211: return $this;
212: }
213:
214: /**
215: * Run current action
216: *
217: * @return Mage_Dataflow_Model_Convert_Action_Abstract
218: */
219: public function run(array $args=array())
220: {
221: if ($method = $this->getParam('method')) {
222: // print $method;
223: if (!is_callable(array($this->getContainer(), $method))) {
224: $this->getContainer()->addException('Unable to run action method: '.$method, Mage_Dataflow_Model_Convert_Exception::FATAL);
225: }
226:
227: // printf('<pre>call %s::%s()</pre>', __CLASS__, __FUNCTION__);
228: // printf('<pre>call %s::%s()</pre>', get_class($this->getContainer()), $method);
229:
230: // print '<pre>CONTAINER = ';
231: // print get_class($this->getContainer());
232: // print '</pre>';
233:
234: $this->getContainer()->addException('Starting '.get_class($this->getContainer()).' :: '.$method);
235:
236: if ($this->getParam('from')) {
237: // print '$this->getParam(\'from\') = ' . $this->getParam('from');
238: $this->getContainer()->setData($this->getContainer($this->getParam('from'))->getData());
239: }
240:
241:
242: $this->getContainer()->$method($args);
243:
244: if ($this->getParam('to')) {
245: $this->getContainer($this->getParam('to'))->setData($this->getContainer()->getData());
246: }
247: } else {
248: $this->addException('No method specified', Mage_Dataflow_Model_Convert_Exception::FATAL);
249: }
250: return $this;
251: }
252:
253: public function runActions(array $args=array())
254: {
255: if (empty($this->_actions)) {
256: return $this;
257: }
258: foreach ($this->_actions as $action) {
259: $action->run($args);
260: }
261: return $this;
262: }
263: }
264: