Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.2] Move the logic to get the menu item language on login to the model #37614

Merged
merged 12 commits into from
Jun 18, 2022
125 changes: 11 additions & 114 deletions components/com_users/src/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
\defined('_JEXEC') or die;

use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Multilanguage;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Uri\Uri;
use Joomla\Database\ParameterType;

/**
* Registration controller class for Users.
Expand Down Expand Up @@ -52,50 +50,13 @@ public function login()
// Check for a simple menu item id
if (is_numeric($data['return']))
{
if (Multilanguage::isEnabled())
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('language'))
->from($db->quoteName('#__menu'))
->where($db->quoteName('client_id') . ' = 0')
->where($db->quoteName('id') . ' = :id')
->bind(':id', $data['return'], ParameterType::INTEGER);

$db->setQuery($query);

try
{
$language = $db->loadResult();
}
catch (\RuntimeException $e)
{
return;
}

if ($language !== '*')
{
$lang = '&lang=' . $language;
}
else
{
$lang = '';
}
}
else
{
$lang = '';
}

$data['return'] = 'index.php?Itemid=' . $data['return'] . $lang;
$language = $this->getModel('Login', 'Site')->getMenuLanguage($data['return']);
$data['return'] = 'index.php?Itemid=' . $data['return'] . ($language !== '*' ? '&lang=' . $language : '');
}
else
// Don't redirect to an external URL.
elseif (!Uri::isInternal($data['return']))
{
// Don't redirect to an external URL.
if (!Uri::isInternal($data['return']))
{
$data['return'] = '';
}
$data['return'] = '';
}

// Set the return URL if empty.
Expand Down Expand Up @@ -176,50 +137,12 @@ public function logout()
// Check for a simple menu item id
if (is_numeric($return))
{
if (Multilanguage::isEnabled())
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('language'))
->from($db->quoteName('#__menu'))
->where($db->quoteName('client_id') . ' = 0')
->where($db->quoteName('id') . ' = :id')
->bind(':id', $return, ParameterType::INTEGER);

$db->setQuery($query);

try
{
$language = $db->loadResult();
}
catch (\RuntimeException $e)
{
return;
}

if ($language !== '*')
{
$lang = '&lang=' . $language;
}
else
{
$lang = '';
}
}
else
{
$lang = '';
}

$return = 'index.php?Itemid=' . $return . $lang;
$language = $this->getModel('Login', 'Site')->getMenuLanguage($return);
$return = 'index.php?Itemid=' . $return . ($language !== '*' ? '&lang=' . $language : '');
}
else
elseif (!Uri::isInternal($return))
{
// Don't redirect to an external URL.
if (!Uri::isInternal($return))
{
$return = '';
}
$return = '';
}

// In case redirect url is not set, redirect user to homepage
Expand Down Expand Up @@ -251,36 +174,10 @@ public function menulogout()
{
if ($itemid)
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('language'))
->from($db->quoteName('#__menu'))
->where($db->quoteName('client_id') . ' = 0')
->where($db->quoteName('id') . ' = :id')
->bind(':id', $itemid, ParameterType::INTEGER);

$db->setQuery($query);

try
{
$language = $db->loadResult();
}
catch (\RuntimeException $e)
{
return;
}

if ($language !== '*')
{
$lang = '&lang=' . $language;
}
else
{
$lang = '';
}
$language = $this->getModel('Login', 'Site')->getMenuLanguage($itemid);

// URL to redirect after logout
$url = 'index.php?Itemid=' . $itemid . $lang;
$url = 'index.php?Itemid=' . $itemid . ($language !== '*' ? '&lang=' . $language : '');
}
else
{
Expand Down
34 changes: 34 additions & 0 deletions components/com_users/src/Model/LoginModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Language\Multilanguage;
use Joomla\CMS\MVC\Model\FormModel;
use Joomla\CMS\Uri\Uri;
use Joomla\Database\ParameterType;

/**
* Login model class for Users.
Expand Down Expand Up @@ -118,4 +120,36 @@ protected function preprocessForm(Form $form, $data, $group = 'user')
{
parent::preprocessForm($form, $data, $group);
}

/**
* Returns the language for the given menu id.
*
* @param int $id The menu id
*/
public function getMenuLanguage(int $id): string
{
if (!Multilanguage::isEnabled())
{
return '';
}

$db = $this->getDatabase();
$query = $db->getQuery(true)
->select($db->quoteName('language'))
->from($db->quoteName('#__menu'))
->where($db->quoteName('client_id') . ' = 0')
->where($db->quoteName('id') . ' = :id')
->bind(':id', $id, ParameterType::INTEGER);

$db->setQuery($query);

try
{
return $db->loadResult();
}
catch (\RuntimeException $e)
{
return '';
}
}
}