Page MenuHomeIn-Portal Phabricator

WidgetRouter.php
No OneTemporary

File Metadata

Created
Wed, Sep 24, 6:44 AM

WidgetRouter.php

<?php
/**
* @version $Id: WidgetRouter.php 16253 2015-09-27 06:36:29Z alex $
* @package Custom
* @copyright Copyright (C) 1997 - 2015 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class WidgetRouter extends AbstractRouter
{
/**
* Returns unit config's prefix, that is associated with this router.
*
* @return string
*/
public function getPrefix()
{
return 'widget';
}
/**
* Builds url part.
*
* @return boolean Return true to continue to next router; return false not to rewrite given prefix.
*/
protected function build()
{
$template = $this->getBuildTemplate();
if ( $template == 'widgets/widget_detail' ) {
// This is default template for this prefix, so don't add it to resulting url.
$this->setBuildParam('pass_template', false);
}
$build_params = $this->extractBuildParams();
if ( $build_params === false ) {
return '';
}
$ret = array('', '');
$filename = $this->getBuildParam('@filename');
$this->setBuildParam('@filename');
if ( $build_params[$this->buildPrefix . '_id'] > 0 ) {
// Add id.
if ( !$filename ) {
$sql = 'SELECT Title
FROM ' . TABLE_PREFIX . 'Widgets
WHERE WidgetId = ' . $build_params[$this->buildPrefix . '_id'];
$filename = $this->Conn->GetOne($sql);
}
$ret[0] .= 'widgets/' . $filename . '/';
}
elseif ( $build_params[$this->buildPrefix . '_Page'] > 1 ) {
// Add page, only when ID is missing.
$ret[1] .= $build_params[$this->buildPrefix . '_Page'] . '/';
}
$ret[0] = rtrim($ret[0], '/');
$ret[1] = rtrim($ret[1], '/');
return array_map('mb_strtolower', $ret);
}
/**
* Parses url part.
*
* @param array $url_parts Url parts to parse.
* @param array $params Parameters, that are used for url building or created during url parsing.
*
* @return boolean Return true to continue to next router; return false to stop processing at this router.
*/
public function parse(array &$url_parts, array &$params)
{
$widget_id = 0;
$widget_filename = '';
$widget_index = array_search('widgets', $url_parts);
if ( $widget_index !== false && isset($url_parts[$widget_index + 1]) ) {
$widget_filename = $url_parts[$widget_index + 1];
}
if ( $widget_filename ) {
$sql = 'SELECT WidgetId
FROM ' . TABLE_PREFIX . 'Widgets
WHERE Title = ' . $this->Conn->qstr($widget_filename);
$widget_id = $this->Conn->GetOne($sql);
if ( $widget_id ) {
$params[$this->buildPrefix . '_id'] = $widget_id;
$params[$this->buildPrefix . '_filename'] = $widget_filename;
$params['pass'][] = $this->buildPrefix;
$this->partParsed('widgets');
$this->partParsed($widget_filename);
}
}
if ( $widget_id && !$this->moreToParse() ) {
// Widget was last url part - use default template.
$params['t'] = 'widgets/widget_detail';
}
return true;
}
}

Event Timeline