Source of file Abstract.php
Size: 17,852 Bytes - Last Modified: 2015-05-15T11:18:15+02:00
/www-data/git/Hackathon_MageMonitoring/src/app/code/community/FireGento/MageMonitoring/Model/Widget/Abstract.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639 | <?php /** * This file is part of a FireGento e.V. module. * * This FireGento e.V. module is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version 3 as * published by the Free Software Foundation. * * This script is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * PHP version 5 * * @category FireGento * @package FireGento_MageMonitoring * @author FireGento Team <team@firegento.com> * @copyright 2015 FireGento Team (http://www.firegento.com) * @license http://opensource.org/licenses/gpl-3.0 GNU General Public License, version 3 (GPLv3) */ /** * class FireGento_MageMonitoring_Model_Widget_Abstract * * @category FireGento * @package FireGento_MageMonitoring * @author FireGento Team <team@firegento.com> */ abstract class FireGento_MageMonitoring_Model_Widget_Abstract implements FireGento_MageMonitoring_Model_Widget { // define config keys const CONFIG_START_COLLAPSED = 'collapsed'; const CONFIG_DISPLAY_PRIO = 'display_prio'; // base node for all config keys const CONFIG_PRE_KEY = 'widgets'; // callback marker const CALLBACK = 'cb:'; // global default values protected $_defStartCollapsed = 0; protected $_defDisplayPrio = 10; protected $_dbConfigKey = null; protected $_tabId = null; protected $_output = array(); protected $_config = array(); protected $_report = array(); /** @var FireGento_MageMonitoring_Block_Widget_Multi_Renderer_Abstract $_renderer */ protected $_renderer; /** * Used to render the widget, returns array of classes that have a ->toHtml() method. * Extending from FireGento_MageMonitoring_Model_Widget_Abstract will give you . * * @return array */ public function getOutput() { $block = $this->newMultiBlock(); /** @var FireGento_MageMonitoring_Block_Widget_Multi_Renderer_Table $renderer */ $renderer = $block->newContentRenderer('table'); $this->setRenderer($renderer); /** @var FireGento_MageMonitoring_Helper_Data $helper */ $helper = Mage::helper('magemonitoring'); $renderer->setHeaderRow( array( $helper->__('Test aspect'), $helper->__('Test Details'), $helper->__('Test result'), $helper->__('Recommendation') ) ); try { $this->_renderConfigChecks(); $this->_renderMoreChecks(); } catch (Exception $e) { Mage::logException($e); } return $this->_output; } /** * New Multi Block * * @return FireGento_MageMonitoring_Block_Widget_Multi Block */ public function newMultiBlock() { $block = Mage::app()->getLayout()->createBlock('magemonitoring/widget_multi'); $block->setTabId($this->getTabId()); $block->setWidgetId($this->getConfigId()); return $block; } /** * Get Tab Id * * @return string */ public function getTabId() { return $this->_tabId; } /** * Returns db config key, returns last 2 parts of classname with appended random string as default. * * @return string */ public function getConfigId() { if (!$this->_dbConfigKey) { $regOut = array(); if (preg_match("/.*_(.*_.*)/", $this->getId(), $regOut)) { $this->_dbConfigKey = strtolower($regOut[1] . '_' . substr(md5(rand()), 0, 6)); } } return $this->_dbConfigKey; } /** * Returns unique widget id. You really don't want to override is. ;) * * @return string */ public function getId() { return get_called_class(); } /** * Render configuration checks defined in config.xml * * @return $this */ protected function _renderConfigChecks() { foreach ($this->_getConfigValues() as $_config) { $title = (string)$_config->title; $path = (string)$_config->path; $recommendation = (string)$_config->recommendation; $this->_checkConfigValue($title, $path, $recommendation); } return $this; } /** * Return configuration params from config.xml * * @return array|SimpleXMLElement */ protected function _getConfigValues() { $nodes = Mage::getConfig()->getNode( 'default/magemonitoring/tabs/monitoring_check/widgets/' . $this->_getNodeName() . '/shop_configuration/values' ); if ($nodes) { return $nodes->children(); } return array(); } /** * Returns the name of the widgets xml node * * @return string */ abstract protected function _getNodeName(); /** * Check a given configuration value concerning an optional recommendation * * @param string $title The title of the configuration value. * @param string $configPath The configuration value path. * @param string $recommendation The recommended value for the given config value. * * @return $this */ protected function _checkConfigValue($title, $configPath, $recommendation = null) { $renderer = $this->getRenderer(); if (is_null($renderer)) { Mage::throwException('No Renderer set for Monitoring'); } /** @var FireGento_MageMonitoring_Helper_Data $helper */ $helper = $this->_getHelper(); $configValue = Mage::getStoreConfig($configPath); if (is_null($configValue)) { $configValue = '---'; } if (is_null($recommendation)) { $recommendation = '---'; } $this->getRenderer()->addRow( array($helper->__($title), $configPath, $configValue, $recommendation), $this->_getRowConfig($configValue == $recommendation) ); $this->_output[] = $this->getRenderer(); return $this; } /** * Get the renderer. * * @return FireGento_MageMonitoring_Block_Widget_Multi_Renderer_Abstract */ public function getRenderer() { return $this->_renderer; } /** * Set the renderer. * * @param FireGento_MageMonitoring_Block_Widget_Multi_Renderer_Abstract $renderer A renderer */ public function setRenderer(FireGento_MageMonitoring_Block_Widget_Multi_Renderer_Abstract $renderer) { $this->_renderer = $renderer; } /** * Get an instance of the module's data helper. * * @return FireGento_MageMonitoring_Helper_Data */ protected function _getHelper() { return Mage::helper('magemonitoring'); } /** * Get a row configuration. * * @param bool $conditionMet A flag indicating whether the condition was met or not. * * @return array */ protected function _getRowConfig($conditionMet) { if ($conditionMet) { $rowConfig = array('_cssClasses' => FireGento_MageMonitoring_Helper_Data::WARN_TYPE_OK); } else { $rowConfig = array('_cssClasses' => FireGento_MageMonitoring_Helper_Data::WARN_TYPE_WARNING); } return $rowConfig; } /** * Render additional checks. * * @return $this */ protected function _renderMoreChecks() { return $this; } /** * Returns true if this widget is active. * * @see FireGento_MageMonitoring_Model_Widget::isActive() */ public function isActive() { return true; } /** * Returns true if widget should start collapsed, speeds up loading times as * the widget won't render it's content on page load. * * @see FireGento_MageMonitoring_Model_Widget::displayCollapsed() */ public function displayCollapsed() { return $this->getConfig(self::CONFIG_START_COLLAPSED); } /** * Get Config * * @param null $configKey Config Key * @param bool $valueOnly Value Only * @return array|bool * @see FireGento_MageMonitoring_Model_Widget::getConfig() */ public function getConfig($configKey = null, $valueOnly = true) { if (empty($this->_config)) { $this->_config = $this->initConfig(); } if ($configKey && array_key_exists($configKey, $this->_config)) { if ($valueOnly) { return $this->_config[$configKey]['value']; } else { return $this->_config[$configKey]; } } else { if ($configKey) { return false; } } return $this->_config; } /** * Returns array with default config data for this widget or false if not implemented. * * @see FireGento_MageMonitoring_Model_Widget::initConfig() */ public function initConfig() { $this->addConfigHeader('Widget Configuration'); $this->addConfig( self::CONFIG_START_COLLAPSED, 'Do not render widget on pageload?', $this->_defStartCollapsed, 'widget', 'checkbox', false ); $this->addConfig( self::CONFIG_DISPLAY_PRIO, 'Display priority (0=top):', $this->_defDisplayPrio, 'widget', 'text', false ); return $this->_config; } /** * Add empty or header row to config modal output. * * @param string $header Header * @return FireGento_MageMonitoring_Model_Widget */ public function addConfigHeader($header = null) { $this->_config[] = array('label' => $header); return $this; } /** * Add Configuration * * @param string $configKey Config Key * @param string $label Label * @param string|int $value Value * @param string $scope Scope * @param string $inputType Input Type * @param bool $required Is Required * @param null|string $tooltip Tooltip * @return $this * @see FireGento_MageMonitoring_Model_Widget::addConfig() */ public function addConfig( $configKey, $label, $value, $scope = 'global', $inputType = 'text', $required = false, $tooltip = null ) { $this->_config[$configKey] = array( 'scope' => $scope, 'label' => $label, 'value' => $value, 'type' => $inputType, 'required' => $required, 'tooltip' => $tooltip ); return $this; } /** * Returns true if widget should start collapsed, speeds up loading times as * the widget won't render it's content on page load. * * @see FireGento_MageMonitoring_Model_Widget::displayCollapsed() */ public function getDisplayPrio() { return $this->getConfig(self::CONFIG_DISPLAY_PRIO); } /** * New Monitoring Block * * @return FireGento_MageMonitoring_Block_Widget_Monitoring Block */ public function newMonitoringBlock() { $block = Mage::app()->getLayout()->createBlock('magemonitoring/widget_monitoring'); $block->setTabId($this->getTabId()); $block->setWidgetId($this->getConfigId()); return $block; } /** * Adds $string to output. * * @param string $string String * @return FireGento_MageMonitoring_Model_Widget_Abstract */ public function dump($string) { $this->_output[] = Mage::app()->getLayout()->createBlock('magemonitoring/widget_dump')->setOutput($string); return $this; } /** * Load Config * * @param null|string|int $configKey Config Key * @param null|string|int $tabId Tab Id * @param null|string|int $widgetDbId Widget DB Id * @return array * @see FireGento_MageMonitoring_Model_Widget::loadConfig() */ public function loadConfig($configKey = null, $tabId = null, $widgetDbId = null) { $config = array(); $this->_tabId = $tabId; if ($widgetDbId !== null) { $this->_dbConfigKey = $widgetDbId; } if ($configKey) { $config[$configKey] = array('value' => null); } else { $config = $this->getConfig(); } foreach ($config as $key => $conf) { $ck = $this->getHelper()->getConfigKey($key, $this); $value = Mage::getStoreConfig($ck); if ($value != null) { $this->_config[$key]['value'] = $value; } } return $this->_config; } /** * Get Helper * * @return FireGento_MageMonitoring_Helper_Data */ public function getHelper() { return Mage::helper('magemonitoring'); } /** * Save config in $post to core_config_data, can handle raw $_POST * or widget config arrays if $postOnly is true. * * @param array $post Post * @param bool $postOnly Post Only * @return $this * @see FireGento_MageMonitoring_Model_Widget::saveConfig() */ public function saveConfig($post, $postOnly = false) { $config = null; if (array_key_exists('widget_id', $post)) { $this->_dbConfigKey = $post['widget_id']; } if ($postOnly) { $config = $post; } else { $c = Mage::getModel('core/config'); if (array_key_exists('class_name', $post)) { $c->saveConfig( $this->getHelper()->getConfigKeyById('impl', $this->_dbConfigKey, 'tabs/' . $this->getTabId()), $post['class_name'], 'default', 0 ); } $config = $this->getConfig(); } foreach ($config as $key => $conf) { if (is_numeric($key)) { // skip header entries continue; } // handle checkbox states if (array_key_exists('type', $conf) && $conf['type'] == 'checkbox') { if (!array_key_exists($key, $post)) { $post[$key] = 0; } else { $post[$key] = 1; } } $value = null; if (array_key_exists($key, $post)) { if (!$postOnly) { $value = $post[$key]; } else { $value = $post[$key]['value']; } } //@todo: batch save $c = Mage::getModel('core/config'); $c->saveConfig( $this->getHelper()->getConfigKey($key, $this), $value, 'default', 0 ); } return $this; } /** * Delete Config * * @param string|null $tabId Tab Id * @return $this * @see FireGento_MageMonitoring_Model_Widget::deleteConfig() */ public function deleteConfig($tabId = null) { $this->_tabId = $tabId; foreach ($this->getConfig() as $key => $conf) { $c = Mage::getModel('core/config'); $c->deleteConfig( $this->getHelper()->getConfigKey($key, $this), 'default', 0 ); } return $this; } /** * Adds another row to watch dog report output. * * Format of $attachments array: * array(array('filename' => $name, 'content' => $content), ...) * * @param string $cssId Css Id * @param string $label Label * @param string $value Value * @param array|null $attachments Attachments * @return $this */ public function addReportRow($cssId, $label, $value, $attachments = null) { $this->_report[] = array( 'css_id' => $cssId, 'label' => $label, 'value' => $value, 'attachments' => $attachments ); return $this; } /** * Get Version * * @return string */ public function getVersion() { return '0.0.1'; } /** * Check Versions * * @return bool */ protected function _checkVersions() { if ($this->getSupportedMagentoVersions() === '*') { return true; } // @todo: do proper merge, things will go probably south for code below. $mageVersion = Mage::getVersion(); // retrieve supported versions from config.xml $versions = $this->getHelper()->extractVersions($this->getSupportedMagentoVersions()); // iterate on versions to find a fitting one foreach ($versions as $_version) { $quotedVersion = preg_quote($_version); // build regular expression with wildcard to check magento version $pregExpr = '#\A' . str_replace('\*', '.*', $quotedVersion) . '\z#ims'; if (preg_match($pregExpr, $mageVersion)) { return true; } } return false; } /** * Get Supported Magento Versions * * @see FireGento_MageMonitoring_Model_Widget::getSupportedMagentoVersions() * @return string */ public function getSupportedMagentoVersions() { return '*'; } } |