Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Widget_Adminhtml_Widget_InstanceController
  • Mage_Widget_Adminhtml_WidgetController
  • Mage_Widget_Block_Adminhtml_Widget_Form
  • Mage_Widget_Block_Adminhtml_Widget_Instance
  • Mage_Widget_Block_Adminhtml_Widget_Instance_Edit
  • Mage_Widget_Block_Adminhtml_Widget_Instance_Edit_Chooser_Block
  • Mage_Widget_Block_Adminhtml_Widget_Instance_Edit_Chooser_Layout
  • Mage_Widget_Block_Adminhtml_Widget_Instance_Edit_Chooser_Template
  • Mage_Widget_Block_Adminhtml_Widget_Instance_Edit_Form
  • Mage_Widget_Block_Adminhtml_Widget_Instance_Edit_Tab_Main
  • Mage_Widget_Block_Adminhtml_Widget_Instance_Edit_Tab_Main_Layout
  • Mage_Widget_Block_Adminhtml_Widget_Instance_Edit_Tab_Properties
  • Mage_Widget_Block_Adminhtml_Widget_Instance_Edit_Tab_Settings
  • Mage_Widget_Block_Adminhtml_Widget_Instance_Edit_Tabs
  • Mage_Widget_Block_Adminhtml_Widget_Instance_Grid
  • Mage_Widget_Block_Adminhtml_Widget_Options
  • Mage_Widget_Helper_Data
  • Mage_Widget_Model_Mysql4_Widget
  • Mage_Widget_Model_Mysql4_Widget_Instance
  • Mage_Widget_Model_Mysql4_Widget_Instance_Collection
  • Mage_Widget_Model_Observer
  • Mage_Widget_Model_Resource_Widget
  • Mage_Widget_Model_Resource_Widget_Instance
  • Mage_Widget_Model_Resource_Widget_Instance_Collection
  • Mage_Widget_Model_Template_Filter
  • Mage_Widget_Model_Widget
  • Mage_Widget_Model_Widget_Config
  • Mage_Widget_Model_Widget_Instance

Interfaces

  • Mage_Widget_Block_Interface
  • Overview
  • Package
  • Class
  • Tree
  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_Widget
 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:  * Widget model for different purposes
 29:  *
 30:  * @category    Mage
 31:  * @package     Mage_Widget
 32:  * @author      Magento Core Team <core@magentocommerce.com>
 33:  */
 34: class Mage_Widget_Model_Widget extends Varien_Object
 35: {
 36:     /**
 37:      * Load Widgets XML config from widget.xml files and cache it
 38:      *
 39:      * @return Varien_Simplexml_Config
 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:      * Return widget XML config element based on its type
 61:      *
 62:      * @param string $type Widget type
 63:      * @return null|Varien_Simplexml_Element
 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:      * Wrapper for getXmlElementByType method
 76:      *
 77:      * @param string $type Widget type
 78:      * @return null|Varien_Simplexml_Element
 79:      */
 80:     public function getConfigAsXml($type)
 81:     {
 82:         return $this->getXmlElementByType($type);
 83:     }
 84: 
 85:     /**
 86:      * Return widget XML configuration as Varien_Object and makes some data preparations
 87:      *
 88:      * @param string $type Widget type
 89:      * @return Varien_Object
 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:         // Save all nodes to object data
101:         $object->setType($type);
102:         $object->setData($xml->asCanonicalArray());
103: 
104:         // Set module for translations etc.
105:         $module = $object->getData('@/module');
106:         if ($module) {
107:             $object->setModule($module);
108:         }
109: 
110:         // Correct widget parameters and convert its data to objects
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:                     // prepare values (for drop-dawns) specified directly in configuration
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:                     // prepare helper block object
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:      * Return filtered list of widgets as SimpleXml object
156:      *
157:      * @param array $filters Key-value array of filters for widget node properties
158:      * @return Varien_Simplexml_Element
159:      */
160:     public function getWidgetsXml($filters = array())
161:     {
162:         $widgets = $this->getXmlConfig()->getNode();
163:         $result = clone $widgets;
164: 
165:         // filter widgets by params
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:      * Return list of widgets as array
187:      *
188:      * @param array $filters Key-value array of filters for widget node properties
189:      * @return array
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:      * Return widget presentation code in WYSIWYG editor
213:      *
214:      * @param string $type Widget Type
215:      * @param array $params Pre-configured Widget Params
216:      * @param bool $asIs Return result as widget directive(true) or as placeholder image(false)
217:      * @return string Widget directive ready to parse
218:      */
219:     public function getWidgetDeclaration($type, $params = array(), $asIs = true)
220:     {
221:         $directive = '{{widget type="' . $type . '"';
222: 
223:         foreach ($params as $name => $value) {
224:             // Retrieve default option value if pre-configured
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:      * Return list of required JS files to be included on the top of the page before insertion plugin loaded
261:      *
262:      * @return array
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:      * Encode string to valid HTML id element, based on base64 encoding
279:      *
280:      * @param string $string
281:      * @return string
282:      */
283:     protected function _idEncode($string)
284:     {
285:         return strtr(base64_encode($string), '+/=', ':_-');
286:     }
287: 
288:     /**
289:      * User-defined widgets sorting by Name
290:      *
291:      * @param array $a
292:      * @param array $b
293:      * @return boolean
294:      */
295:     protected function _sortWidgets($a, $b)
296:     {
297:         return strcmp($a["name"], $b["name"]);
298:     }
299: 
300:     /**
301:      * Widget parameters sort callback
302:      *
303:      * @param Varien_Object $a
304:      * @param Varien_Object $b
305:      * @return int
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: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0