Joomla, как изменить SEF URL, чтобы удалить вид элемента из URL?
Я пишу компонент и создаю router.php по документам в joomla.org . моя проблема в том, как я могу удалить представление элемента из URL, например, у меня есть это представление -> musicses, теперь мой sef такой
/music/99-2016-03-13-06-12-39/musics/3-music77.html?layout=article
я хочу изменить это так: /music/music77.html
music77 - мой заголовок для этого поста,
это мой код
<?php defined('_JEXEC') or die; class NavamusicRouter extends JComponentRouterBase {
/**
* Build the route for the Navamusic component
*
* @param array $query An array of URL arguments
*
* @return array $segments The URL arguments to use to assemble the subsequent URL.
*/
public function build(&$query)
{
$segments = array();
$db = JFactory::getDbo();
$params = JComponentHelper::getParams('com_navamusic');
$advanced = $params->get('sef_advanced_link', 0);
if (empty($query['Itemid']))
{
$menu_item = $this->menu->getActive();
$menu_item_given = false;
}
else
{
$menu_item = $this->menu->getItem($query['Itemid']);
$menu_item_given = true;
}
// Check again
if ($menu_item_given AND isset($menu_item) AND $menu_item->component != 'com_navamusic')
{
$menu_item_given = false;
unset($query['Itemid']);
}
$menu_view = (empty($menu_item->query['view'])) ? null : $menu_item->query['view'];
$menu_cat_id = (empty($menu_item->query['catid'])) ? null : $menu_item->query['catid'];
$menu_id = (empty($menu_item->query['id'])) ? null : $menu_item->query['id'];
if (isset($query['view']))
{
$view = $query['view'];
// For a modal layout force the route to the view in the uri not that of the menu item
if (isset($query['layout']) AND $query['layout'] == 'modal')
{
$segments[] = $query['view'];
unset($query['view']);
unset($query['Itemid']);
}
else
{
if (empty($query['Itemid']) || empty($menu_item) || $menu_item->component != 'com_navamusic')
{
$segments[] = $query['view'];
unset($query['view']);
}
}
}
// Are we dealing with a navamusic that is attached to a menu item?
if (isset($view) AND ($menu_view == $view) AND (isset($query['id'])) AND ($menu_id == intval($query['id'])))
{
unset($query['view']);
if (isset($query['catid']))
{
unset($query['catid']);
}
if (isset($query['layout']) AND $query['layout'] != 'edit')
{
unset($query['layout']);
}
unset($query['id']);
return $segments;
}
if (isset($view) )
{
switch ($view)
{
case 'category':
if (!isset($query['id']) OR $menu_id != (int) $query['id'] OR $menu_view != $view)
{
if (isset($query['catid']))
{
$cat_id = $query['catid'];
}
else
{
if(isset($query['id']))
{
$cat_id = $query['id'];
}
else
{
$cat_id = null;
}
}
$menu_cat_id = $menu_id;
$options['countItems'] = false;
$options['table'] = '';
$categories = JCategories::getInstance('Navamusic',$options);
$category = $categories->get($cat_id);
if (!$category)
{
// We couldn't find the category we were given. Bail.
return $segments;
}
$path = array_reverse($category->getPath());
$array = array();
foreach($path as $id)
{
if((int) $id == (int)$menu_cat_id)
{
break;
}
if($advanced)
{
list($tmp, $id) = explode(':', $id, 2);
}
$array[] = $id;
}
array_splice($array,1, 0, 'category');
$segments = array_reverse($array);
}
unset($query['view']);
unset($query['id']);
unset($query['catid']);
break;
case 'playgroup':
if (!isset($query['id']) OR $menu_id != (int) $query['id'] OR $menu_view != $view)
{
// Make sure we have the id and the alias
if (strpos($query['id'], ':') === false)
{
$dbQuery = $db->getQuery(true)
->select($db->quoteName('alias'))
->from($db->quoteName('#__navamusic_playgroups'))
->where($db->quoteName('id'). '=' . (int) $query['id']);
$db->setQuery($dbQuery);
$alias = $db->loadResult();
$query['id'] = $query['id'] . ':' . $alias;
}
if($advanced)
{
list($tmp, $id) = explode(':', $query['id'], 2);
}
else
{
$id = isset($query['id']) ? $query['id'] : null;
}
$segments[] = $view;
$segments[] = $id;
}
unset($query['view']);
unset($query['id']);
break;
case 'playgroups':
if (!isset($query['id']) OR $menu_id != (int) $query['id'] OR $menu_view != $view)
{
$segments[] = $view;
}
unset($query['view']);
break;
case 'playlist':
if (!isset($query['id']) OR $menu_id != (int) $query['id'] OR $menu_view != $view)
{
// Make sure we have the id and the alias
if (strpos($query['id'], ':') === false)
{
$dbQuery = $db->getQuery(true)
->select($db->quoteName('alias'))
->from($db->quoteName('#__navamusic_playlists'))
->where($db->quoteName('id'). '=' . (int) $query['id']);
$db->setQuery($dbQuery);
$alias = $db->loadResult();
$query['id'] = $query['id'] . ':' . $alias;
}
if($advanced)
{
list($tmp, $id) = explode(':', $query['id'], 2);
}
else
{
$id = isset($query['id']) ? $query['id'] : null;
}
$segments[] = $view;
$segments[] = $id;
}
unset($query['view']);
unset($query['id']);
break;
case 'playlists':
if (!isset($query['id']) OR $menu_id != (int) $query['id'] OR $menu_view != $view)
{
$segments[] = $view;
}
unset($query['view']);
break;
case 'vote':
if (!isset($query['id']) OR $menu_id != (int) $query['id'] OR $menu_view != $view)
{
// Make sure we have the id and the alias
if (strpos($query['id'], ':') === false)
{
$dbQuery = $db->getQuery(true)
->select($db->quoteName('alias'))
->from($db->quoteName('#__navamusic_votes'))
->where($db->quoteName('id'). '=' . (int) $query['id']);
$db->setQuery($dbQuery);
$alias = $db->loadResult();
$query['id'] = $query['id'] . ':' . $alias;
}
if($advanced)
{
list($tmp, $id) = explode(':', $query['id'], 2);
}
else
{
$id = isset($query['id']) ? $query['id'] : null;
}
$segments[] = $view;
$segments[] = $id;
}
unset($query['view']);
unset($query['id']);
break;
case 'votes':
if (!isset($query['id']) OR $menu_id != (int) $query['id'] OR $menu_view != $view)
{
$segments[] = $view;
}
unset($query['view']);
break;
case 'votegroup':
if (!isset($query['id']) OR $menu_id != (int) $query['id'] OR $menu_view != $view)
{
// Make sure we have the id and the alias
if (strpos($query['id'], ':') === false)
{
$dbQuery = $db->getQuery(true)
->select($db->quoteName('alias'))
->from($db->quoteName('#__navamusic_votegroups'))
->where($db->quoteName('id'). '=' . (int) $query['id']);
$db->setQuery($dbQuery);
$alias = $db->loadResult();
$query['id'] = $query['id'] . ':' . $alias;
}
if($advanced)
{
list($tmp, $id) = explode(':', $query['id'], 2);
}
else
{
$id = isset($query['id']) ? $query['id'] : null;
}
$segments[] = $view;
$segments[] = $id;
}
unset($query['view']);
unset($query['id']);
break;
case 'votegroups':
if (!isset($query['id']) OR $menu_id != (int) $query['id'] OR $menu_view != $view)
{
$segments[] = $view;
}
unset($query['view']);
break;
case 'mcategory':
if (!isset($query['id']) OR $menu_id != (int) $query['id'] OR $menu_view != $view)
{
// Make sure we have the id and the alias
if (strpos($query['id'], ':') === false)
{
$dbQuery = $db->getQuery(true)
->select($db->quoteName('alias'))
->from($db->quoteName('#__navamusic_mcategories'))
->where($db->quoteName('id'). '=' . (int) $query['id']);
$db->setQuery($dbQuery);
$alias = $db->loadResult();
$query['id'] = $query['id'] . ':' . $alias;
}
if($advanced)
{
list($tmp, $id) = explode(':', $query['id'], 2);
}
else
{
$id = isset($query['id']) ? $query['id'] : null;
}
$segments[] = $view;
$segments[] = $id;
}
unset($query['view']);
unset($query['id']);
break;
case 'mcategories':
if (!isset($query['id']) OR $menu_id != (int) $query['id'] OR $menu_view != $view)
{
$segments[] = $view;
}
unset($query['view']);
break;
case 'musics':
if (!isset($query['id']) OR $menu_id != (int) $query['id'] OR $menu_view != $view)
{
if (isset($query['catid']))
{
$cat_id = $query['catid'];
}
else
{
if(isset($query['id']))
{
$cat_id = $query['id'];
}
else
{
$cat_id = null;
}
}
$menu_cat_id = $menu_id;
$options['countItems'] = false;
$options['table'] = '';
$categories = JCategories::getInstance('Navamusic',$options);
$category = $categories->get($cat_id);
if($category)
{
//TODO Throw error that the category either not exists or is unpublished
$path = array_reverse($category->getPath());
$array = array();
foreach($path as $id)
{
if((int) $id == (int)$menu_cat_id)
{
break;
}
if($advanced)
{
list($tmp, $id) = explode(':', $id, 2);
}
$array[] = $id;
}
$segments = array_merge($segments, array_reverse($array));
}
// Make sure we have the id and the alias
if (strpos($query['id'], ':') === false)
{
$dbQuery = $db->getQuery(true)
->select($db->quoteName('alias'))
->from($db->quoteName('#__navamusic_musicses'))
->where($db->quoteName('id'). '=' . (int) $query['id']);
$db->setQuery($dbQuery);
$alias = $db->loadResult();
$query['id'] = $query['id'] . ':' . $alias;
}
if($advanced)
{
list($tmp, $id) = explode(':', $query['id'], 2);
}
else
{
$id = isset($query['id']) ? $query['id'] : null;
}
$segments[] = $view;
$segments[] = $id;
}
unset($query['view']);
unset($query['id']);
unset($query['catid']);
break;
case 'musicses':
if (!isset($query['id']) OR $menu_id != (int) $query['id'] OR $menu_view != $view)
{
$segments[] = $view;
}
unset($query['view']);
break;
default:
break;
}
}
if (!isset($query['layout']) OR $query['layout'] == 'default')
{
if (isset($menu_item->query['layout']) AND $menu_item->query['layout'] == 'blog' AND isset($query['id']))
{
$query['layout'] = 'article';
}
};
$total = count($segments);
for ($i = 0; $i < $total; $i++)
{
$segments[$i] = str_replace(':', '-', $segments[$i]);
}
return $segments;
}
/**
* Parse the segments of a URL.
*
* @param array $segments The segments of the URL to parse.
*
* @return array $vars The URL attributes to be used by the application.
*/
public function parse(&$segments)
{
$app = JFactory::getApplication();
$total = count($segments);
$vars = array();
for ($i = 0; $i < $total; $i++)
{
$segments[$i] = preg_replace('/-/', ':', $segments[$i], 1);
}
// Get the request view as this is need for multi view parsing
$view = $app->input->getString('view', '');
//Get the active menu item.
$item = $this->menu->getActive();
$params = JComponentHelper::getParams('com_navamusic');
$advanced = $params->get('sef_advanced_link', 0);
// Count route segments
$count = count($segments);
// Standard routing
if (!isset($item))
{
$vars['view'] = $segments[0];
if (count($segments) > 1)
{
$vars['id'] = $segments[$count - 1];
}
return $vars;
}
if (!$advanced)
{
$vars['id'] = (int) $segments[$count - 1];
$vars['view'] = $segments[$count - 2];
return $vars;
}
$id = (isset($item->query['id']) AND $item->query['id'] > 1) ? $item->query['id'] : 'root';
// From the categories view, we can only jump to a category.
$options['countItems'] = false;
$options['table'] = '';
if ($item->query['view'] == 'categories' OR $item->query['view'] == 'category')
{
$categories = JCategories::getInstance('Navamusic',$options)->get($id)->getChildren();
}
else
{
$categories = array();
}
$vars['catid'] = $id;
$vars['id'] = $id;
$found = 0;
foreach($segments as $segment)
{
$segment = $advanced ? str_replace(':', '-',$segment) : $segment;
foreach($categories as $category)
{
if ($category->slug == $segment
OR $category->alias == $segment
)
{
$vars['id'] = $category->id;
$vars['catid'] = $category->id;
$vars['view'] = 'category';
$categories = $category->getChildren();
$found = 1;
break;
}
}
if ($found == 0)
{
if ($item->query['view'] == 'playgroup' OR $view == 'playgroup')
{
if($advanced)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('id'));
$query->from($db->quoteName('#__navamusic_playgroups'));
$query->where($db->quoteName('alias') . ' = ' . $db->quote($segment));
$db->setQuery($query);
$nid = $db->loadResult();
}
else
{
$nid = $segment;
}
$vars['id'] = $nid;
$vars['view'] = $view;
}
if ($item->query['view'] == 'playgroups'OR $view == 'playgroups')
{
$vars['view'] = $view;
}
if ($item->query['view'] == 'playlist' OR $view == 'playlist')
{
if($advanced)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('id'));
$query->from($db->quoteName('#__navamusic_playlists'));
$query->where($db->quoteName('alias') . ' = ' . $db->quote($segment));
$db->setQuery($query);
$nid = $db->loadResult();
}
else
{
$nid = $segment;
}
$vars['id'] = $nid;
$vars['view'] = $view;
}
if ($item->query['view'] == 'playlists'OR $view == 'playlists')
{
$vars['view'] = $view;
}
if ($item->query['view'] == 'vote' OR $view == 'vote')
{
if($advanced)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('id'));
$query->from($db->quoteName('#__navamusic_votes'));
$query->where($db->quoteName('alias') . ' = ' . $db->quote($segment));
$db->setQuery($query);
$nid = $db->loadResult();
}
else
{
$nid = $segment;
}
$vars['id'] = $nid;
$vars['view'] = $view;
}
if ($item->query['view'] == 'votes'OR $view == 'votes')
{
$vars['view'] = $view;
}
if ($item->query['view'] == 'votegroup' OR $view == 'votegroup')
{
if($advanced)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('id'));
$query->from($db->quoteName('#__navamusic_votegroups'));
$query->where($db->quoteName('alias') . ' = ' . $db->quote($segment));
$db->setQuery($query);
$nid = $db->loadResult();
}
else
{
$nid = $segment;
}
$vars['id'] = $nid;
$vars['view'] = $view;
}
if ($item->query['view'] == 'votegroups'OR $view == 'votegroups')
{
$vars['view'] = $view;
}
if ($item->query['view'] == 'mcategory' OR $view == 'mcategory')
{
if($advanced)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('id'));
$query->from($db->quoteName('#__navamusic_mcategories'));
$query->where($db->quoteName('alias') . ' = ' . $db->quote($segment));
$db->setQuery($query);
$nid = $db->loadResult();
}
else
{
$nid = $segment;
}
$vars['id'] = $nid;
$vars['view'] = $view;
}
if ($item->query['view'] == 'mcategories'OR $view == 'mcategories')
{
$vars['view'] = $view;
}
if ($item->query['view'] == 'musics' OR $view == 'musics')
{
if($advanced)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('id'));
$query->from($db->quoteName('#__navamusic_musicses'));
$query->where($db->quoteName('catid') . ' = ' . (int) $vars['catid']);
$query->where($db->quoteName('alias') . ' = ' . $db->quote($segment));
$db->setQuery($query);
$nid = $db->loadResult();
}
else
{
$nid = $segment;
}
$vars['id'] = $nid;
$vars['view'] = $view;
}
if ($item->query['view'] == 'musicses'OR $view == 'musicses')
{
$vars['view'] = $view;
}
}
$found = 0;
}
return $vars;
}
}