HTTPQuery =& $this->Application->recallObject('HTTPQuery'); } function SetDefaultValues() { $defaults = Array('m_cat_id' => 0, 'm_cat_page' => 1, 'm_opener' => 's'); foreach ($defaults as $default_key => $default_value) { if ($this->HTTPQuery->Get($default_key) == null) { $this->HTTPQuery->Set($default_key, $default_value); } } } function ProcessLanguage(&$url_parts) { if (!isset($url_parts[0])) return false; $res = false; $url_part = array_shift($url_parts); $sql = 'SELECT LanguageId FROM '.TABLE_PREFIX.'Language WHERE LOWER(PackName) = '.$this->Conn->qstr($url_part).' AND Enabled = 1'; $language_id = $this->Conn->GetOne($sql); $this->Application->Phrases = new PhrasesCache(); if($language_id) { $this->HTTPQuery->Set('m_lang', $language_id); $res = true; } $this->Application->VerifyLanguageId(); if (!$res) { array_unshift($url_parts, $url_part); } return $res; } function ProcessTheme(&$url_parts) { if (!isset($url_parts[0])) return false; $res = false; $url_part = array_shift($url_parts); $sql = 'SELECT ThemeId FROM '.TABLE_PREFIX.'Theme WHERE LOWER(Name) = '.$this->Conn->qstr($url_part).' AND Enabled = 1'; $theme_id = $this->Conn->GetOne($sql); if($theme_id) { $this->HTTPQuery->Set('m_theme', $theme_id); $res = true; } $this->Application->VerifyThemeId(); // verify anyway - will set default if not found!!! if (!$res) { array_unshift($url_parts, $url_part); } return $res; } function ProcessCategory(&$url_parts) { if (!isset($url_parts[0])) return false; $res = false; $url_part = array_shift($url_parts); $category_id = 0; $last_category_id = 0; $category_path = ''; do { $category_path = trim($category_path.'/'.$url_part, '/'); if( preg_match('/(.*)_([\d]+)$/', $category_path, $rets) ) { $category_path = $rets[1]; $this->HTTPQuery->Set('m_cat_page', $rets[2]); } $category_id = $this->Conn->GetOne( 'SELECT CategoryId FROM '.TABLE_PREFIX.'Category WHERE NamedParentPath = '.$this->Conn->qstr($category_path)); if ($category_id !== false) { $last_category_id = $category_id; $url_part = array_shift($url_parts); $res = true; } } while ($category_id !== false && $url_part); $this->HTTPQuery->Set('m_cat_id', $last_category_id); if ($url_part) { array_unshift($url_parts, $url_part); } return $res; } function ProcessModuleIndex(&$url_parts) { if ( count($url_parts) != 0 ) return false; $sql = 'SELECT CachedCategoryTemplate FROM '.TABLE_PREFIX.'Category WHERE CategoryId = '.$this->Application->GetVar('m_cat_id'); $this->HTTPQuery->Set('t', $this->HTTPQuery->getDefaultTemplate( $this->Conn->GetOne($sql) ) ); return true; } function ProcessModuleItem(&$url_parts) { if (!isset($url_parts[0])) return false; if ( count($url_parts) != 1 ) return false; $url_part = array_shift($url_parts); // match module reviews page $page = 1; if( preg_match('/(.*)_([\d]+)$/', $url_part, $rets) ) { $url_part = $rets[1]; $page = $rets[2]; } $cat_item = $this->Conn->GetRow(' SELECT ci.ItemPrefix, c.ParentPath, ci.CategoryId FORM '.TABLE_PREFIX.'CategoryItems AS ci LEFT JOIN '.TABLE_PREFIX.'Category AS c ON c.CategoryId = ci.CategoryId WHERE CategoryId = '.$this->Application->GetVar('m_cat_id').' AND Filename = '.$this->Conn->qstr($url_part)); if ($cat_item !== false) { $module_prefix = $cat_item['ItemPrefix']; $item_template = $category_data['CachedItemTemplate']; if ($item_template) { $url_parts = explode('/', $item_template); array_push($url_parts, $url_part); // save item's filename as not processed $url_part = array_shift($url_parts); } $this->Application->SetVar($cat_item['ItemPrefix'].'_id', $item_id); } $this->Application->SetVar( $event->getPrefixSpecial().'_Reviews_Page', $page); // only filename left, no other parts $process_module = false; $sql = 'SELECT ParentPath, CachedItemTemplate, CachedCategoryTemplate FROM '.TABLE_PREFIX.'Category WHERE CategoryId = '.$this->Application->GetVar('m_cat_id'); $category_data = $this->Conn->GetRow($sql); $root_category_id = array_shift( explode('|', substr($category_data['ParentPath'], 1, -1)) ); $module_info = $this->Application->findModule('RootCat', $root_category_id); if ($module_info) { $module_prefix = $module_info['Var']; $module_event = new kEvent($module_prefix.':ParseEnv', Array('url_parts' => array_merge(Array($url_part), $url_parts)) ); $this->Application->HandleEvent($module_event); if ($module_event->status == erSUCCESS && $this->HTTPQuery->Get($module_prefix.'_id')) { $item_template = $category_data['CachedItemTemplate']; if ($item_template) { $this->HTTPQuery->Set('t', $item_template ); return true; } } } array_unshift($url_parts, $url_part); return false; } function ProcessPhisycalTemplate(&$url_parts) { if (!isset($url_parts[0])) return false; $remaining = array(); do { $template_path = implode('/', $url_parts); $t_parts['path'] = dirname($template_path) == '.' ? '' : '/'.dirname($template_path); $t_parts['file'] = basename($template_path); $sql = 'SELECT FileId FROM '.TABLE_PREFIX.'ThemeFiles WHERE (FilePath = '.$this->Conn->qstr($t_parts['path']).') AND (FileName = '.$this->Conn->qstr($t_parts['file'].'.tpl').')'; // $sql = 'SELECT FileId FROM '.TABLE_PREFIX.'ThemeFiles WHERE CONCAT(FilePath, "/", FileName) = '.$this->Conn->qstr('/'.$template_path.'.tpl'); $template_found = $this->Conn->GetOne($sql); if(!$template_found) { array_unshift($remaining, array_pop($url_parts)); } } while (!$template_found && $url_parts); $url_parts = $remaining; if ($template_found) { $this->HTTPQuery->Set('t', $template_path ); return true; } return false; } /** * Checks if whole url_parts matches a whole In-CMS page * * @param array $url_parts * @return boolean */ function ProcessVirtualTemplate(&$url_parts) { if (!isset($url_parts[0]) || !$this->Application->isModuleEnabled('In-CMS')) return false; $template_path = implode('/', $url_parts); $sql = 'SELECT p.PageId, ci.CategoryId FROM '.TABLE_PREFIX.'Pages AS p LEFT JOIN '.TABLE_PREFIX.'CategoryItems AS ci ON ci.ItemResourceId = p.ResourceId WHERE Path = '.$this->Conn->qstr($template_path).' AND ci.PrimaryCat = 1'; $template_found = $this->Conn->GetRow($sql); if ($template_found) { $this->Application->SetVar('m_cat_id', $template_found['CategoryId']); $this->HTTPQuery->Set('t', $template_path ); return true; } return false; } function processRewriteURL() { $url = $this->HTTPQuery->Get('_mod_rw_url_'); if( substr($url, -5) == '.html' ) $url = substr($url, 0, strlen($url) - 5 ); $url_parts = $url ? explode('/', $url) : Array(); $process_module = true; if($this->HTTPQuery->Get('rewrite') == 'on' || !$url_parts) { $this->SetDefaultValues(); } if(!$url_parts) { $this->Application->Phrases = new PhrasesCache(); $this->Application->VerifyLanguageId(); $this->Application->Phrases->Init('phrases'); $this->Application->VerifyThemeId(); $this->HTTPQuery->Set('t', $this->HTTPQuery->getDefaultTemplate('') ); $this->HTTPQuery->finalizeParsing(Array('m')); return false; } else { $this->HTTPQuery->Set('t', ''); } $this->ProcessLanguage($url_parts); $this->ProcessTheme($url_parts); if ( $this->ProcessVirtualTemplate($url_parts) ) { return true; } $this->ProcessCategory($url_parts); if ( $this->ProcessModuleIndex($url_parts) ) { return ; } if ( $this->ProcessModuleItem($url_parts) ) { return ; } /*// match module $next_template = $this->HTTPQuery->Get('next_template'); if($url_part || $next_template) { if($next_template) { $next_template_parts = explode('/', $next_template); $module_folder = array_shift($next_template_parts); } else { $module_folder = $url_part; } foreach ($this->Application->ModuleInfo as $module_name => $module_data) { if( trim($module_data['TemplatePath'], '/') == $module_folder ) { $module_prefix = $module_data['Var']; break; } } }*/ if ( $this->ProcessPhisycalTemplate($url_parts) ) { if (!$url_parts) { return true; } } // pass params left to module $this->Application->Phrases->Init('phrases'); $passed = Array('m'); $module_params = Array(); if ( isset($module_prefix) ) { $passed[] = $module_prefix; $module_event = new kEvent($module_prefix.':ParseEnv', Array('url_parts' => $url_parts) ); if ($process_module) { $this->Application->HandleEvent($module_event); } $item_id = $this->HTTPQuery->Get($module_prefix.'_id'); $module_params = Array($module_prefix.'_id' => $item_id ? $item_id : '0' ); if ($module_event->status == erFAIL) { $not_found = $this->Application->ConfigValue('ErrorTemplate'); $this->HTTPQuery->Set('t', $not_found ? $not_found : 'error_notfound' ); } } $this->HTTPQuery->finalizeParsing($passed, $module_params); } } ?>