Location: PHPKode > projects > eCart > library/Ecart.php
<?php
/**
 * Ecart
 * 
 * This file is part of Ecart.
 * 
 * Ecart is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Ecart 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.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Ecart.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @category    Ecart
 * @package     Ecart_Core
 * @copyright   Copyright 2008-2009 E-Cart LLC
 * @license     GNU Public License V3.0
 */

/**
 * 
 * @category    Ecart
 * @package     Ecart_Core
 * @author      Ecart Core Team <hide@address.com>
 */
class Ecart
{
    static $siteId = 0;
    
    static $template;
    
    /**
     * Checks is Ecart is installed already
     *
     * @static
     * @return bool
     */
    public static function isInstalled()
    {
        return file_exists('../app/etc/config.php');
    }
    
    /**
     * Return current template
     *
     * @static
     * @param string ['front' || 'admin']
     * @return array 
     */
    public static function getTemplate($app = 'front')
    {
        if (null === self::$template) {
            if ($app == 'admin') {
                $templateId = self::config()->design->main->adminTemplateId;
            } else {
                $templateId = self::config()->design->main->frontTemplateId;
            }
            
            self::$template = self::model('core/template')->fetchRow(
                self::db()->quoteInto('id = ? ', $templateId)
            )->toArray(); 
        }
        return self::$template;
    }

    /**
     * @static
     * @param bool $withParams
     * @return string
     */
    public static function getCurrentUrl($withParams = true)
    {
         // Filter php_self to avoid a security vulnerability.
        $phpRequestUri = htmlentities(
            substr(
                $_SERVER['REQUEST_URI'],
                0,
                strcspn($_SERVER['REQUEST_URI'], "\n\r")
            ),
            ENT_QUOTES
        );

        if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
            $protocol = 'https://';
        } else {
            $protocol = 'http://';
        }
        $host = $_SERVER['HTTP_HOST'];
        if (isset($_SERVER['HTTP_PORT']) && $_SERVER['HTTP_PORT'] != '' &&
            (($protocol == 'http://' && $_SERVER['HTTP_PORT'] != '80') ||
            ($protocol == 'https://' && $_SERVER['HTTP_PORT'] != '443'))) {
            $port = ':' . $_SERVER['HTTP_PORT'];
        } else {
            $port = '';
        }
        $url = $protocol . $host . $port . $phpRequestUri;
        if (!$withParams && $end = strpos($url, '?')) {
            $url = substr($url, 0, $end);
        }
        return $url;
    }

    /**
     * Return current site id
     *
     * @static
     * @return int 
     */
    public static function getSiteId()
    {
        if (!self::$siteId) {
            $url = Ecart::getCurrentUrl();
            if (!self::$siteId = self::single('core/site')->getIdByUrl($url)) {
                // provide site access through ip-address
                if (!self::$siteId = self::single('core/site')->fetchOne('id')) {
                    throw new Ecart_Exception(
                        "There is no site linked with url " . $url
                    );
                }
            }
        }
        return self::$siteId;
    }
    
    /**
     * Return used language id
     *
     * @static
     * @return int
     * @deprecated use Ecart_Locale::getLanguageId() instead
     */
    public static function getLanguageId()
    {
        return Ecart_Locale::getLanguageId();
    }

    /**
     * Returns singleton object
     *
     * @static
     * @param string $class
     * @param array $arguments [optional]
     * @return Ecart_Db_Table_Abstract
     */
    public static function single($class, $arguments = array())
    {
        $class = self::_getClass($class);
        
        if (!Zend_Registry::isRegistered($class)) {
            $instance = new $class($arguments);
            Zend_Registry::set($class, $instance);
        }
        return Zend_Registry::get($class);
    }
    
    /**
     * Return requested model instance
     *
     * @static
     * @param string $model
     * @param array $arguments class arguments
     * @return Ecart_Db_Table_Abstract
     */
    public static function model($model, $arguments = array())
    {
        $class = self::_getClass($model, 'model');
        
        return new $class($arguments);
    }

    /**
     * Return class name by shortname
     *
     * @static
     * @param string $name
     * @param string $type
     * @return string
     */
    private static function _getClass($name, $type = 'model')
    {
        if (false === strpos($name, '/')) {
            return $name;
        }

        list($module, $class) = explode('/', $name);

        // @todo
        /* if (null === $class) {
            return new Ecart_Db_Table(
                array_merge($arguments, array('name' => $model))
            );
        }*/
        $class = str_replace(' ', '_', ucwords(str_replace('_', ' ', $class)));

        return 'Ecart_'
            . ucfirst($module) . '_'
            . ucfirst($type) . '_'
            . $class;
    }
    
    /**
     * Retrieve config object or config value,
     * if name is requested
     *
     * @static
     * @param string $name[optional] config value to load
     * @param string $default[optional] default value to return
     * @return object Ecart_Config|mixed
     */
    public static function config($name = null, $default = null)
    {
        if (!Zend_Registry::isRegistered('config')) {
            throw new Ecart_Exception(
                Ecart::translate('core')->__(
                    'Config is not initialized'
                )
            );
        }
        if (null === $name) {
            return Zend_Registry::get('config');
        } else {
            return Zend_Registry::get('config')->get($name, $default);
        }
    }
    
    /**
     * Create and return table object (Ecart_Db_Table)
     *
     * @static
     * @param string table name
     * @param array() arguments
     * @return Ecart_Db_Table object
     */
    public static function table($tableName, $arguments = array())
    {
        return new Ecart_Db_Table(array_merge(
            $arguments, array('name' => $tableName)
        ));   
    }
    
    /**
     * Return customer id or false
     *
     * @static
     * @return mixed (int|bool)
     */
    public static function getCustomerId()
    {
        if (!Zend_Auth::getInstance()->hasIdentity()) {
            return false;
        }
        return Zend_Auth::getInstance()->getIdentity();
    }
    
    /**
     * Retrieve database adapter object
     *
     * @static
     * @return Zend_Db  
     */
    public static function db()
    {
        return Zend_Registry::get('db');
    }
    
    /**
     * Retrieve session object
     *
     * @static
     * @return Zend_Session_Namespace
     */
    public static function session() 
    {
        return Zend_Registry::get('nsMain');
    }
    
    /**
     * Retrieve cache object
     *
     * @static
     * @return Zend_Cache_Core
     */
    public static function cache() 
    {
        return Zend_Registry::get('cache');
    }
    
    /**
     * Retrieve Ecart_Message object
     *
     * @static
     * @return Ecart_Message
     */
    public static function message($namespace = 'messenger')
    {
        return Ecart_Message::getInstance($namespace);
    }

    /**
     * Retrieve the list of active, installed modules
     *
     * @static
     * @return array code => path pairs
     */
    public static function getModules()
    {
        if (Zend_Registry::isRegistered('modules')) {
            return Zend_Registry::get('modules');
        }
        if (!$modules = self::cache()->load('modules_list')) {
            $list = Ecart::single('core/module')->getList('is_active = 1');
            $result = array();
            foreach ($list as $moduleCode => $values) {
                list($category, $module) = explode('_', $moduleCode, 2);
                $modules[$moduleCode] = Ecart::config()->system->path
                    . '/app/code/' . $category . '/' . $module;
            }
            self::cache()->save($modules, 'modules_list', array('modules'));
        }
        Zend_Registry::set('modules', $modules);
        return Zend_Registry::get('modules');
    }
    
    /**
     * Retrieve the controllers paths
     *
     * @static
     * @return array code => path pairs
     */
    public static function getControllers()
    {
        if (!$result = self::cache()->load('controllers_list')) {
            $modules = self::getModules();
            $result = array();
            foreach ($modules as $moduleCode => $path) {
                if (is_readable($path . '/controllers')) {
                    $result[$moduleCode] = $path . '/controllers';
                }
            }
            self::cache()->save(
                $result, 'controllers_list', array('modules')
            );
        }
        return $result;        
    }
    
    /**
     * Retrieve array of paths to route files
     *
     * @static
     * @return array 
     */
    public static function getRoutes()
    {
        if (!($routes = self::cache()->load('routes_list'))) {
            $modules = self::getModules();
            $routes = array();
            foreach ($modules as $moduleCode => $path) {
                if (file_exists($path . '/etc/routes.php') 
                    && is_readable($path . '/etc/routes.php')) {
                    
                    $routes[] = $path . '/etc/routes.php';
                }
            }
            self::cache()->save(
                $routes, 'routes_list', array('modules')
            );
        }
        return $routes;
    }
    
    /**
     * Dispatch event
     * 
     * Calls all of the methods linked to dispatched event
     * 
     * @static
     * @param string $name
     * @param array $data [optional]
     * @return Ecart_Event_Observer
     */
    public static function dispatch($name, array $data = array())
    {
        return Ecart_Event_Observer::getInstance()->dispatch($name, $data);
    }

    /**
     *
     * @param string $module
     * @return Ecart_Translate
     */
    public static function translate($module = 'Ecart_Core')
    {
        if (false === strpos($module, '_')) {
            $module = 'Ecart_' . $module;
        }
        $module = str_replace(
            ' ', '_', ucwords(str_replace('_', ' ', $module))
        );

        return Ecart_Translate::getInstance($module);
    }

        
    /**
     * Return current ecart version
     *
     * @static
     * @return string
     */
    public static function getVersion()
    {
        return '0.6.5';
    }
}
Return current item: eCart