<?php

App::uses('Router', 'Routing');

/**
 * BemmelhorRouter
 *
 * NOTE: Do not use this class as a substitute of Router class.
 * Use it only for BemmelhorRouter::connect()
 *
 * @package  Bemmelhor
 * @version  1.0
 * @author   Fabricio S Costa <fabriciojf@gmail.com>
 *
 * @link     http://www.bemmelhor.org
 */
class BemmelhorRouter {

    /**
     * If Translate plugin is active,
     * an extra Route will be created for locale-based URLs
     *
     * For example,
     * http://yoursite.com/blog/post-title, and
     * http://yoursite.com/eng/blog/post-title
     *
     * Returns this object's routes array. Returns false if there are no routes available.
     *
     * @param string $route         An empty string, or a route string "/"
     * @param array $default        NULL or an array describing the default route
     * @param array $params         An array matching the named elements in the route to regular expressions which that element should match.
     * @return void
     */
    public static function connect($route, $default = array(), $params = array()) {
        $localizedRoute = $route == '/' ? '' : $route;
        if (Configure::read('Translate')) {
            Router::connect('/:locale' . $localizedRoute, $default, array_merge(array('locale' => '[a-z]{3}'), $params));
        }
        Router::connect($route, $default, $params);
    }

    /**
     * If you want your non-routed controler actions (like /users/add) to support locale based urls,
     * this method must be called AFTER all the routes.
     *
     * @return void
     */
    public static function localize() {
        if (Configure::read('Translate')) {
            Router::connect('/:locale/:controller/:action/*', array(), array('locale' => '[a-z]{3}'));
        }
    }

    /**
     * Load plugin routes
     *
     * @return void
     */
    public static function plugins() {
        $pluginRoutes = Configure::read('Hook.routes');
        if (!$pluginRoutes || !is_array(Configure::read('Hook.routes'))) {
            return;
        }

        $plugins = Configure::read('Hook.routes');
        foreach ($plugins as $plugin) {
            $path = App::pluginPath($plugin) . 'Config' . DS . 'routes.php';
            if (file_exists($path)) {
                include $path;
            }
        }
    }

    /**
     * Routes for content types
     *
     * @param string $alias
     * @return void
     */
    public static function contentType($alias) {
        BemmelhorRouter::connect('/' . $alias, array('controller' => 'nodes', 'action' => 'index', 'type' => $alias));
        BemmelhorRouter::connect('/' . $alias . '/archives/*', array('controller' => 'nodes', 'action' => 'index', 'type' => $alias));
        BemmelhorRouter::connect('/' . $alias . '/:slug', array('controller' => 'nodes', 'action' => 'view', 'type' => $alias));
        BemmelhorRouter::connect('/' . $alias . '/term/:slug/*', array('controller' => 'nodes', 'action' => 'term', 'type' => $alias));
    }

    /**
     * Apply routes for content types with routes enabled
     *
     * @return void
     */
    public static function routableContentTypes() {
        try {
            $types = ClassRegistry::init('Type')->find('all', array(
                'cache' => array(
                    'name' => 'bemmelhor_types',
                    'config' => 'bemmelhor_types',
                ),
            ));
            foreach ($types as $type) {
                if (isset($type['Params']['routes']) && $type['Params']['routes']) {
                    BemmelhorRouter::contentType($type['Type']['alias']);
                }
            }
        } catch (MissingConnectionException $e) {
            CakeLog::write('critical', __('Unable to get routeable content types: %s', $e->getMessage()));
        }
    }

}
