Poprzedni problem udało mi się rozwiązać tudzież przy pomocy:
Kod:
$itemID = JSite::getMenu()->getActive()->tree[0];
ale postanowiłem stosować JRoute.
Wszystko wydawało się działać w porządku, ale ...
Mam dwa widoki - tophits i proposals, które używają jednego kontrolera (kontroler: tophits).
W katalogu głównym, w pliku tophits.php dodałem taki oto kod:
Kod:
$controller = JRequest::getWord('view','tophits');
if ($controller == 'proposals')
$controller = 'tophits';
Chodzi o to, by nie powielać funkcji, które już zawarłem w kontrolerze tophits.
I teraz w widoku proposals mam link:
Kod:
<a href="<?php echo JRoute::_('index.php?option=' . $option . '&view=proposals&task=addVote&pid=' . $row->sid, false); ?>" title="<?php echo JText::_('VOTE'); ?>">
<img src="components/com_tophits/images/add.png" alt="<?php echo JText::_('VOTE'); ?>" />
</a>
Kod w kontrolerze tophits:
Kod:
<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');
require_once(JPATH_COMPONENT . DS . 'helpers' . DS . 'global.php');
class TopHitsControllerTopHits extends JController {
function __construct($config = array()) {
parent::__construct($config);
}
function display() {
$view = JRequest::getVar('view');
if ($view == 'proposals')
JRequest::setVar('view', 'proposals');
else
JRequest::setVar('view', 'tophits');
parent::display();
}
function addVote() {
global $mainframe, $option, $Itemid;
//check the view variable
$view = JRequest::getString('view');
if($view != 'tophits' && $view != 'proposals') $view = 'tophits';
//define params for voting
$user = & JFactory::getUser();
$params = JComponentHelper::getParams('com_tophits');
$allowGuest = $params->get('allow_guest_vote');
$allowMulti = $params->get('allow_multi_votes');
//check if user can vote
if (($allowGuest == 0 and !$user->guest) or ($allowGuest == 1)) {
//get the current record ID
$currentRecord = getCurrentRecord();
//get the song ID
$cid = JRequest::getInt('pid');
if ($cid != 0 && !is_null($cid)) {
$db =& JFactory::getDBO();
$query = '';
if ($allowGuest == 0) {
........
}
else {
........
}
$db->setQuery($query);
$db->query();
$num_rows = $db->getNumRows();
//check if user voted or if multi votes option is allowed
if ($num_rows == 0 or $allowMulti == 0) {
......
if (!$row->store()) {
echo "<script>alert('" . $row->getError() . "');window.history.go(-1);</script>\n";
exit();
}
$mainframe->redirect(JRoute::_('index.php?option=' . $option . '&view=' . $view . '&Itemid=' . $Itemid, false), JText::_('ADD_VOTE_SUCCESS'));
} else {
$mainframe->redirect(JRoute::_('index.php?option=' . $option . '&view=' . $view. '&Itemid=' . $Itemid, false), JText::_('ADD_VOTE_ALREADY_VOTED'));
}
} else {
$mainframe->redirect(JRoute::_('index.php?option=' . $option . '&view=' . $view. '&Itemid=' . $Itemid, false), JText::_('ADD_VOTE_ERROR'));
}
} else {
$mainframe->redirect(JRoute::_('index.php?option=' . $option . '&view=' . $view. '&Itemid=' . $Itemid, false), JText::_('ADD_MUST_LOGIN'));
}
}
}
?>
I teraz cała zabawa polega na tym, iż ...
W obu widokach mam link, który wywołuje funkcję addVote, z tym, iż różnią się parametrem view (tophits albo proposals).
Gdy kliknę w widoku proposals, zostaję prawidłowo przekierowany na adres z parametrem &Itemid=##.
Natomiast z widoku tophits, parametr ten nie jest w ogóle uwzględniany - tj. przekierowanie bez &Itemid=##.
---------- Post dodany o 14:52 ---------- Poprzedni post był o 14:42 ----------
Zauważyłem, że Itemid nie jest przesyłane, gdy pozycja komponentu w menu jest jako pierwsza (nie podrzędna).
Top Lista -> Itemid = 89
--- Propozycje -> Itemid = 91
Czyli 89 jest gdzieś gubione, ale dlaczego?
---------- Post dodany o 16:28 ---------- Poprzedni post był o 14:52 ----------
Hmm, po wykasowaniu ". '&view=' . $view. '&Itemid=' . $Itemid" zaczęło wszystko trybić ... ciekawe