Skip to content

Instantly share code, notes, and snippets.

@AgelxNash
Last active September 13, 2019 09:35
Show Gist options
  • Save AgelxNash/7377194 to your computer and use it in GitHub Desktop.
Save AgelxNash/7377194 to your computer and use it in GitHub Desktop.
Плагин для кастомной маршрутизации в MODX Evolution + сниппет для формирования ссылок. Данная реализация работает только при кэше с учетом ID и $_GET
//<?php
/**
* Onetable Route
*
* Плагин для кастомной маршрутизации
*
* @license GNU General Public License (GPL), http://www.gnu.org/copyleft/gpl.html
* @author Agel_Nash <Agel_Nash@xaker.ru>
* @version 0.1
*
* @internal @events OnPageNotFound,OnLoadWebDocument,OnLoadWebPageCache
* @internal @properties &docid=ID документа;int;2 &tablename=Имя таблицы;text;content &fieldname=Уникальное поле в таблице;text;id &prefix=Префикс плейсхолдеров документа;text;custom &pkname=PrimaryKey;text;id &sendparent=При просмотре документа перенаправлять на родителя?;list;true,false;true
*/
switch($modx->event->name){
case 'OnPageNotFound':{
$brand = '';
$q = explode('/', ltrim($_SERVER['REQUEST_URI'], '/'));
$url = $modx->makeURL($docid);
$url = rtrim($url, $modx->config['friendly_url_suffix']);
$url = ltrim($url, '/');
$tmp = explode('/', $url);
if(!isset($modx->customDocID) && count($q)==(count($tmp)+1)){
$endKey = explode("?", end($q), 2);
$endKey = $endKey[0];
$find = rtrim($endKey, $modx->config['friendly_url_suffix']);
$q = $modx->db->query("SELECT `".$pkname."` FROM ".$modx->getFullTableName($tablename)." WHERE `".$fieldname."`='".$modx->db->escape($find)."'");
if($modx->db->getRecordCount($q)==1 && $find.$modx->config['friendly_url_suffix'] == $endKey){
$modx->customDocID = (int)$modx->db->getValue($q);
$modx->sendForward($docid);
}
}
break;
}
case 'OnLoadWebDocument':
case 'OnLoadWebPageCache':{
if($modx->documentObject['id']==$docid){
$flag = true;
if(isset($modx->customDocID) && (int)$modx->customDocID>0){
$q = $modx->db->query("SELECT * FROM ".$modx->getFullTableName($tablename)." WHERE `".$pkname."`='".$modx->customDocID."'");
$flag = ($modx->db->getRecordCount($q)==1);
}else{
$flag = false;
}
if($flag){
$out = $modx->db->getRow($q);
$plh = array();
foreach($out as $key => $data){
$plh[$prefix."_".$key] = $data;
}
$modx->customDocID = (isset($plh[$prefix."_".$pkname])) ? $plh[$prefix."_".$pkname] : false;
$modx->documentObject = array_merge($modx->documentObject,$plh);
}else{
$modx->customDocID = false;
if($sendparent){
$url = $modx->makeUrl($modx->documentObject['parent'], '', '', 'full');
$modx->sendRedirect($url, 0, 'REDIRECT_HEADER', 'HTTP/1.1 301 Moved Permanently');
}else{
$modx->sendErrorPage();
}
}
}
break;
}
}
<?php
$id = isset($id) ? (int)$id : $modx->documentObject['id'];
$alias = isset($alias) ? $alias : '';
$url = $modx->makeUrl($id);
$url = rtrim($url,$modx->config['friendly_url_suffix']);
return $url."/".$alias.$modx->config['friendly_url_suffix'];
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment