Example #1
0
/**
 * Method to parse Route
 *
 * @param array $segments
 * @return array
 */
function CrowdFundingParseRoute($segments)
{
    $vars = array();
    //Get the active menu item.
    $app = JFactory::getApplication();
    $menu = $app->getMenu();
    $item = $menu->getActive();
    // Count route segments
    $count = count($segments);
    // Standard routing for articles.  If we don't pick up an Itemid then we get the view from the segments
    // the first segment is the view and the last segment is the id of the details, category or payment.
    if (!isset($item)) {
        $vars['view'] = $segments[0];
        $vars['catid'] = $segments[$count - 1];
        return $vars;
    }
    // COUNT == 1
    // Category ( Discover )
    if ($count == 1) {
        // we check to see if an alias is given.  If not, we assume it is a project,
        // because categories have always alias.
        if (false == strpos($segments[0], ':')) {
            $vars['view'] = 'details';
            $vars['id'] = (int) $segments[0];
            return $vars;
        }
        list($id, $alias) = explode(':', $segments[0], 2);
        // first we check if it is a category
        $category = JCategories::getInstance('CrowdFunding')->get($id);
        if ($category and strcmp($category->alias, $alias) == 0) {
            $vars['view'] = 'discover';
            $vars['id'] = $id;
            return $vars;
        } else {
            $project = CrowdFundingHelperRoute::getProject($id);
            if ($project) {
                if ($project->alias == $alias) {
                    $vars['view'] = 'details';
                    $vars['catid'] = (int) $project->catid;
                    $vars['id'] = (int) $id;
                    return $vars;
                }
            }
        }
    }
    // COUNT >= 2
    if ($count >= 2) {
        $view = $segments[$count - 1];
        switch ($view) {
            case "backing":
                $itemId = (int) $segments[$count - 2];
                // Get catid from menu item
                if (!empty($item->query["id"])) {
                    $catId = (int) $item->query["id"];
                } else {
                    $catId = (int) $segments[$count - 3];
                }
                $vars['view'] = 'backing';
                $vars['id'] = (int) $itemId;
                $vars['catid'] = (int) $catId;
                break;
            case "embed":
                // Backing without reward
                $itemId = (int) $segments[$count - 2];
                // Get catid from menu item
                if (!empty($item->query["id"])) {
                    $catId = (int) $item->query["id"];
                } else {
                    $catId = (int) $segments[$count - 3];
                }
                $vars['view'] = 'embed';
                $vars['id'] = (int) $itemId;
                $vars['catid'] = (int) $catId;
                break;
            case "updates":
                // Screens of details - "updates", "comments", "funders"
            // Screens of details - "updates", "comments", "funders"
            case "comments":
            case "funders":
                $itemId = (int) $segments[$count - 2];
                // Get catid from menu item
                if (!empty($item->query["id"])) {
                    $catId = (int) $item->query["id"];
                } else {
                    $catId = (int) $segments[$count - 3];
                }
                $vars['view'] = 'details';
                $vars['id'] = (int) $itemId;
                $vars['catid'] = (int) $catId;
                // Get screen
                $screen = $segments[$count - 1];
                $allowedScreens = array("updates", "comments", "funders");
                if (in_array($screen, $allowedScreens)) {
                    $vars['screen'] = $screen;
                }
                break;
            default:
                // if there was more than one segment, then we can determine where the URL points to
                // because the first segment will have the target category id prepended to it.  If the
                // last segment has a number prepended, it is details, otherwise, it is a category.
                $catId = (int) $segments[$count - 2];
                $id = (int) $segments[$count - 1];
                if ($id > 0 and $catId > 0) {
                    $vars['view'] = 'details';
                    $vars['catid'] = $catId;
                    $vars['id'] = $id;
                } else {
                    $vars['view'] = 'category';
                    $vars['id'] = $id;
                }
                break;
        }
    }
    return $vars;
}