Index: branches/1.3.x/install/cache/class_structure.php =================================================================== diff -u -N -r16164 -r16172 --- branches/1.3.x/install/cache/class_structure.php (.../class_structure.php) (revision 16164) +++ branches/1.3.x/install/cache/class_structure.php (.../class_structure.php) (revision 16172) @@ -22,6 +22,7 @@ 'EUserTagProcessor' => '/modules/custom/units/sections/users/e_user_tp.php', 'SampleHelper' => '/modules/custom/units/helpers/sample_helper.php', 'WidgetEventHandler' => '/modules/custom/units/widgets/widget_eh.php', + 'WidgetRouter' => '/modules/custom/units/widgets/WidgetRouter.php', 'WidgetTagProcessor' => '/modules/custom/units/widgets/widget_tp.php', ), 'class_info' => array( @@ -130,6 +131,13 @@ 0 => 'kDBEventHandler', ), ), + 'WidgetRouter' => array( + 'type' => 1, + 'modifiers' => 0, + 'extends' => array( + 0 => 'AbstractRouter', + ), + ), 'WidgetTagProcessor' => array( 'type' => 1, 'modifiers' => 0, Index: branches/1.3.x/units/widgets/widgets_config.php =================================================================== diff -u -N -r16103 -r16172 --- branches/1.3.x/units/widgets/widgets_config.php (.../widgets_config.php) (revision 16103) +++ branches/1.3.x/units/widgets/widgets_config.php (.../widgets_config.php) (revision 16172) @@ -27,12 +27,6 @@ 5 => 'mode', ), - // in case, when one method does everything - 'RewriteListener' => 'WidgetRewriteListener', - - // in case, when building and parsing is done by separate methods -// 'RewriteListener' => Array ('WidgetRewriteBuilder', 'WidgetRewriteParser'), - 'IDField' => 'WidgetId', 'StatusField' => Array ('Status'), @@ -216,4 +210,4 @@ 'PerPage' => 'Perpage_Widgets', 'ShortListPerPage' => 'Perpage_Widgets_Short', ),*/ -); \ No newline at end of file +); Index: branches/1.3.x/units/widgets/widget_eh.php =================================================================== diff -u -N -r15496 -r16172 --- branches/1.3.x/units/widgets/widget_eh.php (.../widget_eh.php) (revision 15496) +++ branches/1.3.x/units/widgets/widget_eh.php (.../widget_eh.php) (revision 16172) @@ -193,127 +193,4 @@ } - /** - * Builds/parses widget part of url - * - * @param int $rewrite_mode Mode in what rewrite listener was called. Possbile two modes: REWRITE_MODE_BUILD, REWRITE_MODE_PARSE. - * @param string $prefix Prefix, that listener uses for system integration - * @param Array $params Params, that are used for url building or created during url parsing. - * @param Array $url_parts Url parts to parse (only for parsing). - * @param bool $keep_events Keep event names in resulting url (only for building). - * @return bool Return true to continue to next listener; return false (when building) not to rewrite given prefix; return false (when parsing) to stop processing at this listener. - */ - function WidgetRewriteListener($rewrite_mode = REWRITE_MODE_BUILD, $prefix, &$params, &$url_parts, $keep_events = false) - { - if ($rewrite_mode == REWRITE_MODE_BUILD) { - return $this->WidgetRewriteBuilder($rewrite_mode, $prefix, $params, $url_parts, $keep_events); - } - - if ($rewrite_mode == REWRITE_MODE_PARSE) { - return $this->WidgetRewriteParser($rewrite_mode, $prefix, $params, $url_parts); - } - - return true; - } - - /** - * Builds/parses widget part of url - * - * @param int $rewrite_mode Mode in what rewrite listener was called. Possbile two modes: REWRITE_MODE_BUILD, REWRITE_MODE_PARSE. - * @param string $prefix Prefix, that listener uses for system integration - * @param Array $params Params, that are used for url building or created during url parsing. - * @param Array $url_parts Url parts to parse (only for parsing). - * @param bool $keep_events Keep event names in resulting url (only for building). - * @return bool Return true to continue to next listener; return false (when building) not to rewrite given prefix; return false (when parsing) to stop processing at this listener. - */ - function WidgetRewriteBuilder($rewrite_mode = REWRITE_MODE_BUILD, $prefix, &$params, &$url_parts, $keep_events = false) - { - $template = $this->Application->getPhysicalTemplate($params['t']); - - if ( $template == 'widgets/widget_detail' ) { - // this is default template for this prefix, so don't add it to resulting url - $params['pass_template'] = false; - } - - $rewrite_processor = $this->Application->recallObject('kRewriteUrlProcessor'); - /* @var $rewrite_processor kRewriteUrlProcessor */ - - $processed_params = $rewrite_processor->getProcessedParams($prefix, $params, $keep_events); - - if ( $processed_params === false ) { - return ''; - } - - $ret = Array ('', ''); - - $filename = isset($params[$prefix . '_filename']) ? $params[$prefix . '_filename'] : $this->Application->GetVar($prefix . '_filename'); - unset($params[$prefix . '_filename']); - - if ( $processed_params[$prefix . '_id'] > 0 ) { - // add id - if ( !$filename ) { - $sql = 'SELECT Title - FROM ' . TABLE_PREFIX . 'Widgets - WHERE WidgetId = ' . $processed_params[$prefix . '_id']; - $filename = $this->Conn->GetOne($sql); - } - - $ret[0] .= 'widgets/' . $filename . '/'; - } - elseif ( $processed_params[$prefix . '_Page'] > 1 ) { - // add page, only when ID is missing - $ret[1] .= $processed_params[$prefix . '_Page'] . '/'; - } - - $ret[0] = rtrim($ret[0], '/'); - $ret[1] = rtrim($ret[1], '/'); - - return array_map('mb_strtolower', $ret); - } - - /** - * Builds/parses widget part of url - * - * @param int $rewrite_mode Mode in what rewrite listener was called. Possbile two modes: REWRITE_MODE_BUILD, REWRITE_MODE_PARSE. - * @param string $prefix Prefix, that listener uses for system integration - * @param Array $params Params, that are used for url building or created during url parsing. - * @param Array $url_parts Url parts to parse (only for parsing). - * @return bool Return true to continue to next listener; return false (when building) not to rewrite given prefix; return false (when parsing) to stop processing at this listener. - */ - function WidgetRewriteParser($rewrite_mode = REWRITE_MODE_BUILD, $prefix, &$params, &$url_parts) - { - $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]; - } - - $rewrite_processor = $this->Application->recallObject('kRewriteUrlProcessor'); - /* @var $rewrite_processor kRewriteUrlProcessor */ - - 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->Prefix . '_id'] = $widget_id; - $params[$this->Prefix . '_filename'] = $widget_filename; - $params['pass'][] = $this->Prefix; - - $rewrite_processor->partParsed('widgets'); - $rewrite_processor->partParsed($widget_filename); - } - } - - if ( $widget_id && !$rewrite_processor->moreToParse() ) { - // widget was last url part - use default template - $params['t'] = 'widgets/widget_detail'; - } - - return true; - } -} \ No newline at end of file +} Index: branches/1.3.x/units/widgets/WidgetRouter.php =================================================================== diff -u -N --- branches/1.3.x/units/widgets/WidgetRouter.php (revision 0) +++ branches/1.3.x/units/widgets/WidgetRouter.php (revision 16172) @@ -0,0 +1,119 @@ +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; + } + +}