1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Widget_Model_Widget extends Varien_Object
35: {
36: 37: 38: 39: 40:
41: public function getXmlConfig()
42: {
43: $cachedXml = Mage::app()->loadCache('widget_config');
44: if ($cachedXml) {
45: $xmlConfig = new Varien_Simplexml_Config($cachedXml);
46: } else {
47: $config = new Varien_Simplexml_Config();
48: $config->loadString('<?xml version="1.0"?><widgets></widgets>');
49: Mage::getConfig()->loadModulesConfiguration('widget.xml', $config);
50: $xmlConfig = $config;
51: if (Mage::app()->useCache('config')) {
52: Mage::app()->saveCache($config->getXmlString(), 'widget_config',
53: array(Mage_Core_Model_Config::CACHE_TAG));
54: }
55: }
56: return $xmlConfig;
57: }
58:
59: 60: 61: 62: 63: 64:
65: public function getXmlElementByType($type)
66: {
67: $elements = $this->getXmlConfig()->getXpath('*[@type="' . $type . '"]');
68: if (is_array($elements) && isset($elements[0]) && $elements[0] instanceof Varien_Simplexml_Element) {
69: return $elements[0];
70: }
71: return null;
72: }
73:
74: 75: 76: 77: 78: 79:
80: public function getConfigAsXml($type)
81: {
82: return $this->getXmlElementByType($type);
83: }
84:
85: 86: 87: 88: 89: 90:
91: public function getConfigAsObject($type)
92: {
93: $xml = $this->getConfigAsXml($type);
94:
95: $object = new Varien_Object();
96: if ($xml === null) {
97: return $object;
98: }
99:
100:
101: $object->setType($type);
102: $object->setData($xml->asCanonicalArray());
103:
104:
105: $module = $object->getData('@/module');
106: if ($module) {
107: $object->setModule($module);
108: }
109:
110:
111: $params = $object->getData('parameters');
112: $newParams = array();
113: if (is_array($params)) {
114: $sortOrder = 0;
115: foreach ($params as $key => $data) {
116: if (is_array($data)) {
117: $data['key'] = $key;
118: $data['sort_order'] = isset($data['sort_order']) ? (int)$data['sort_order'] : $sortOrder;
119:
120:
121: $values = array();
122: if (isset($data['values']) && is_array($data['values'])) {
123: foreach ($data['values'] as $value) {
124: if (isset($value['label']) && isset($value['value'])) {
125: $values[] = $value;
126: }
127: }
128: }
129: $data['values'] = $values;
130:
131:
132: if (isset($data['helper_block'])) {
133: $helper = new Varien_Object();
134: if (isset($data['helper_block']['data']) && is_array($data['helper_block']['data'])) {
135: $helper->addData($data['helper_block']['data']);
136: }
137: if (isset($data['helper_block']['type'])) {
138: $helper->setType($data['helper_block']['type']);
139: }
140: $data['helper_block'] = $helper;
141: }
142:
143: $newParams[$key] = new Varien_Object($data);
144: $sortOrder++;
145: }
146: }
147: }
148: uasort($newParams, array($this, '_sortParameters'));
149: $object->setData('parameters', $newParams);
150:
151: return $object;
152: }
153:
154: 155: 156: 157: 158: 159:
160: public function getWidgetsXml($filters = array())
161: {
162: $widgets = $this->getXmlConfig()->getNode();
163: $result = clone $widgets;
164:
165:
166: if (is_array($filters) && count($filters) > 0) {
167: foreach ($widgets as $code => $widget) {
168: try {
169: $reflection = new ReflectionObject($widget);
170: foreach ($filters as $field => $value) {
171: if (!$reflection->hasProperty($field) || (string)$widget->{$field} != $value) {
172: throw new Exception();
173: }
174: }
175: } catch (Exception $e) {
176: unset($result->{$code});
177: continue;
178: }
179: }
180: }
181:
182: return $result;
183: }
184:
185: 186: 187: 188: 189: 190:
191: public function getWidgetsArray($filters = array())
192: {
193: if (!$this->_getData('widgets_array')) {
194: $result = array();
195: foreach ($this->getWidgetsXml($filters) as $widget) {
196: $helper = $widget->getAttribute('module') ? $widget->getAttribute('module') : 'widget';
197: $helper = Mage::helper($helper);
198: $result[$widget->getName()] = array(
199: 'name' => $helper->__((string)$widget->name),
200: 'code' => $widget->getName(),
201: 'type' => $widget->getAttribute('type'),
202: 'description' => $helper->__((string)$widget->description)
203: );
204: }
205: usort($result, array($this, "_sortWidgets"));
206: $this->setData('widgets_array', $result);
207: }
208: return $this->_getData('widgets_array');
209: }
210:
211: 212: 213: 214: 215: 216: 217: 218:
219: public function getWidgetDeclaration($type, $params = array(), $asIs = true)
220: {
221: $directive = '{{widget type="' . $type . '"';
222:
223: foreach ($params as $name => $value) {
224:
225: if (is_array($value)) {
226: $value = implode(',', $value);
227: } elseif (trim($value) == '') {
228: $widget = $this->getConfigAsObject($type);
229: $parameters = $widget->getParameters();
230: if (isset($parameters[$name]) && is_object($parameters[$name])) {
231: $value = $parameters[$name]->getValue();
232: }
233: }
234: if ($value) {
235: $directive .= sprintf(' %s="%s"', $name, $value);
236: }
237: }
238: $directive .= '}}';
239:
240: if ($asIs) {
241: return $directive;
242: }
243:
244: $config = Mage::getSingleton('widget/widget_config');
245: $imageName = str_replace('/', '__', $type) . '.gif';
246: if (is_file($config->getPlaceholderImagesBaseDir() . DS . $imageName)) {
247: $image = $config->getPlaceholderImagesBaseUrl() . $imageName;
248: } else {
249: $image = $config->getPlaceholderImagesBaseUrl() . 'default.gif';
250: }
251: $html = sprintf('<img id="%s" src="%s" title="%s">',
252: $this->_idEncode($directive),
253: $image,
254: Mage::helper('core')->urlEscape($directive)
255: );
256: return $html;
257: }
258:
259: 260: 261: 262: 263:
264: public function getWidgetsRequiredJsFiles()
265: {
266: $result = array();
267: foreach ($this->getWidgetsXml() as $widget) {
268: if ($widget->js) {
269: foreach (explode(',', (string)$widget->js) as $js) {
270: $result[] = $js;
271: }
272: }
273: }
274: return $result;
275: }
276:
277: 278: 279: 280: 281: 282:
283: protected function _idEncode($string)
284: {
285: return strtr(base64_encode($string), '+/=', ':_-');
286: }
287:
288: 289: 290: 291: 292: 293: 294:
295: protected function _sortWidgets($a, $b)
296: {
297: return strcmp($a["name"], $b["name"]);
298: }
299:
300: 301: 302: 303: 304: 305: 306:
307: protected function _sortParameters($a, $b)
308: {
309: $aOrder = (int)$a->getData('sort_order');
310: $bOrder = (int)$b->getData('sort_order');
311: return $aOrder < $bOrder ? -1 : ($aOrder > $bOrder ? 1 : 0);
312: }
313: }
314: