I built a Joomla component a while ago which presents a disclaimer page immediately after logging in whereby the user has to select "I Agree" before going to the restricted site content.
The component is called com_rsdisclaimers
and it has a master controller ~/coontroller.php
and another (sub)controller in ~/controllers/rsdisclaimer.php
defined as class RsdisclaimersControllerRsdisclaimer extends JControllerForm
. The sub controller has a task/method called agree()
which sets a session variable and redirects.
This all works okay in that a disclaimer is displayed and when the user clicks "agree", the sub controller task "agree" is called which sets a session variable and redirects. However I am trying to allow users to bypass this screen (component) if they have previously "agreed" to the terms. I am using JUser::getParam()
/JUser::setParam()
to do provide this persistance.
My design is to add logic at the component root file (~/rsdisclaimer.php
) which the first checks to see if the user has the parameter already set. If so , I am trying to override and run the "agree" task straight away to handle the redirect rather than displaying the component's disclaimer page. However no matter what I do, changing the 'task' to 'agree' is simply not working on the first visit to the component after logging in on account that I and getting the master controller instead of the sub controller. How do I explicitly get a handle to the subcontroller so I can call the agree
task?
Here is my top level php file:
<?php
/**
* @version 1.0.0
* @package com_rsdisclaimers
* @copyright Copyright (C) 2011 Chris Walsh. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
// Include dependancies
jimport('joomla.application.component.controller');
// NOTE TO SELF: If we called JRequest::getVar('task','') at this point, we
// might get a response of "rsdisclaimer.agree". However, when we
// call the same function after performing JController::getInstance('Rsdisclaimers')
// the task returned will now simply become 'agree'.
// 2014-11-20 | Chris Walsh
// Test to see if the user has previously 'agreed'...
$user = JFactory::getUser();
$accepted = $user->getParam('rsdisclaimer.accepted', '0');
echo "<pre>JCOMPONENT_BASE: accepted={$accepted}; task={$task}</pre>";
if($accepted == '1')
{
// Load the sub controller and jump straight to agree (redirects etc.)
// !! THIS IS NOT WORKING!! CAN'T GET THE SUB CONTROLLER
$task = 'agree';
$task = 'rsdisclaimer.agree';
$controller = JController::getInstance('Rsdisclaimers');
$controller->execute($task);
$controller->redirect();
}
else
{
// Execute the task
$controller = JController::getInstance('Rsdisclaimers');
$controller->execute(JRequest::getVar('task',''));
$controller->redirect();
}
The agree method in ~/controllers/rsdisclaimer.php
works IF it actually gets called:
<?php
/**
* @version 1.0.0
* @package com_rsdisclaimers
* @copyright Copyright (C) 2011 Amy Stephen. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
jimport('joomla.application.component.controllerform');
/**
* @package Joomla.Site
* @subpackage com_rsdisclaimers
*/
class RsdisclaimersControllerRsdisclaimer extends JControllerForm
{
(snip)
/**
* Method to deal with the user selecting "agree".
*
* @return void
* @since 1.6.1
*/
function agree()
{
// Check for request forgeries.
JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
// Set the session token which the plugin 'realsensedisclaimer' will check for
$session = JFactory::getSession();
$session->set('RealsenseDisclaimer', true);
// Set the agree parameter if not already set so we don't ask again
$user = JFactory::getUser();
$accepted = $user->getParam('rsdisclaimer.accepted', '0');
if($accepted == '0')
{
$user->setParam('rsdisclaimer.accepted', '1');
$user->setParam('rsdisclaimer.acceptedDate', '2014-11-20');
$accepted = $user->getParam('rsdisclaimer.accepted', '0');
}
// Redirect them to the nominated location
$app = JFactory::getApplication();
$url = 'index.php?option=com_content&view=article&id=1110&Itemid=101';
$app->redirect(JRoute::_($url, false));
}
(snip)
}
Can anyone advise how I can get a handle to the sub controller rather than the master controller?
Thanks.