<?php
class sfBlogFrontWebController extends sfFrontWebController {
   public function dispatch() {
     blogStore::init();
     sfFrontWebController::dispatch();
   }

  /**
   * Generates an URL from an array of parameters.
   *
   * @param mixed   An associative array of URL parameters or an internal URI as a string.
   * @param boolean Whether to generate an absolute URL
   *
   * @return string A URL to a symfony resource
   */
  public function genUrl($parameters = array(), $absolute = false) {
    // absolute URL or symfony URL?
    if (!is_array($parameters)) {
      if (substr($parameters, 0, 5)=='http:' || $parameters == '#') {
        return $parameters;
      }
    }

    $url = '';
    if ($sf_relative_url_root = $this->getContext()->getRequest()->getRelativeUrlRoot()) {
      $url = $sf_relative_url_root;
    }

    $route_name = '';
    $fragment = '';

    if (!is_array($parameters)) {
      // strip fragment
      if (false !== ($pos = strpos($parameters, '#'))) {
        $fragment = substr($parameters, $pos + 1);
        $parameters = substr($parameters, 0, $pos);
      }

      list($route_name, $parameters) = $this->convertUrlStringToParameters($parameters);
    }

    $divider = $equals = $querydiv = '/';

    // default module
    if (!isset($parameters['module'])) {
      $parameters['module'] = sfConfig::get('sf_default_module');
    }

    // default action
    if (!isset($parameters['action'])) {
      $parameters['action'] = sfConfig::get('sf_default_action');
    }

    $r = sfRouting::getInstance();
    if ($r->hasRoutes() && $generated_url = $r->generate($route_name, $parameters, $querydiv, $divider, $equals)) {
      $url .= $generated_url;
    } else {
      $url .= strtr(http_build_query($parameters), '?=', '/');
    }

    if (!isset(blogStore::$blogName)) blogStore::$blogName = '';
    if ($absolute) {
      $request = $this->getContext()->getRequest();
      if (blogStore::$isRoute) {
        $url = 'http://'.$request->getHost().$url;
      } else {
        $url = 'http://'.preg_replace('/^[0-9A-Za-z_\.]*/', '\0/'.blogStore::$blogName, $request->getHost().$url);
      }
    } else {
      if (!blogStore::$isRoute)
        $url = '/'.blogStore::$blogName.$url;
    }

    if ($fragment) {
      $url .= '#'.$fragment;
    }

    return $url;
  }
}