- md5
- 477f8cf3f4db6b16a46ee09d93ed9c2a
- sha1
- ac24096f040ded1c9c15cc2b40a7f2b689b4bba0
- sha256
- 9b7d3584fde62bb421250a7100389df99cd8a5d5ee5a7062862dc5464d84c8e5
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
<?php
/**
* @package Joomla.Administrator
* @subpackage com_languages
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Languages\Administrator\Model;
\defined('_JEXEC') or die;
use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Client\ClientHelper;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\Folder;
use Joomla\CMS\Language\LanguageHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\CMS\Table\Table;
use Joomla\Utilities\ArrayHelper;
/**
* Languages Component Languages Model
*
* @since 1.6
*/
class InstalledModel extends ListModel
{
/**
* @var object user object
*/
protected $user = null;
/**
* @var boolean|\JExeption True, if FTP settings should be shown, or an exception
*/
protected $ftp = null;
/**
* @var string option name
*/
protected $option = null;
/**
* @var array languages description
*/
protected $data = null;
/**
* @var integer total number of languages
*/
protected $total = null;
/**
* @var string language path
*/
protected $path = null;
/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
* @param MVCFactoryInterface $factory The factory.
*
* @see \Joomla\CMS\MVC\Model\BaseDatabaseModel
* @since 3.2
*/
public function __construct($config = array(), MVCFactoryInterface $factory = null)
{
if (empty($config['filter_fields']))
{
$config['filter_fields'] = array(
'name',
'nativeName',
'language',
'author',
'published',
'version',
'creationDate',
'author',
'authorEmail',
'extension_id',
'client_id',
);
}
parent::__construct($config, $factory);
}
/**
* Method to auto-populate the model state.
*
* Note. Calling getState in this method will result in recursion.
*
* @param string $ordering An optional ordering field.
* @param string $direction An optional direction (asc|desc).
*
* @return void
*
* @since 1.6
*/
protected function populateState($ordering = 'name', $direction = 'asc')
{
// Load the filter state.
$this->setState('filter.search', $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search', '', 'string'));
// Special case for client id.
$clientId = (int) $this->getUserStateFromRequest($this->context . '.client_id', 'client_id', 0, 'int');
$clientId = (!in_array($clientId, array (0, 1))) ? 0 : $clientId;
$this->setState('client_id', $clientId);
// Load the parameters.
$params = ComponentHelper::getParams('com_languages');
$this->setState('params', $params);
// List state information.
parent::populateState($ordering, $direction);
}
/**
* Method to get a store id based on model configuration state.
*
* This is necessary because the model is used by the component and
* different modules that might need different sets of data or different
* ordering requirements.
*
* @param string $id A prefix for the store id.
*
* @return string A store id.
*
* @since 1.6
*/
protected function getStoreId($id = '')
{
// Compile the store id.
$id .= ':' . $this->getState('client_id');
$id .= ':' . $this->getState('filter.search');
return parent::getStoreId($id);
}
/**
* Method to get the client object.
*
* @return object
*
* @since 1.6
*/
public function getClient()
{
return ApplicationHelper::getClientInfo($this->getState('client_id', 0));
}
/**
* Method to get the ftp credentials.
*
* @return object
*
* @since 1.6
*/
public function getFtp()
{
if (is_null($this->ftp))
{
$this->ftp = ClientHelper::setCredentialsFromRequest('ftp');
}
return $this->ftp;
}
/**
* Method to get the option.
*
* @return object
*
* @since 1.6
*/
public function getOption()
{
$option = $this->getState('option');
return $option;
}
/**
* Method to get Languages item data.
*
* @return array
*
* @since 1.6
*/
public function getData()
{
// Fetch language data if not fetched yet.
if (is_null($this->data))
{
$this->data = array();
$isCurrentLanguageRtl = Factory::getLanguage()->isRtl();
$params = ComponentHelper::getParams('com_languages');
$installedLanguages = LanguageHelper::getInstalledLanguages(null, true, true, null, null, null);
// Compute all the languages.
foreach ($installedLanguages as $clientId => $languages)
{
$defaultLanguage = $params->get(ApplicationHelper::getClientInfo($clientId)->name, 'en-GB');
foreach ($languages as $lang)
{
$row = new \stdClass;
$row->language = $lang->element;
$row->name = $lang->metadata['name'];
$row->nativeName = $lang->metadata['nativeName'] ?? '-';
$row->client_id = (int) $lang->client_id;
$row->extension_id = (int) $lang->extension_id;
$row->author = $lang->manifest['author'];
$row->creationDate = $lang->manifest['creationDate'];
$row->authorEmail = $lang->manifest['authorEmail'];
$row->version = $lang->manifest['version'];
$row->published = $defaultLanguage === $row->language ? 1 : 0;
$row->checked_out = null;
// Fix wrongly set parentheses in RTL languages
if ($isCurrentLanguageRtl)
{
$row->name = html_entity_decode($row->name . '‎', ENT_QUOTES, 'UTF-8');
$row->nativeName = html_entity_decode($row->nativeName . '‎', ENT_QUOTES, 'UTF-8');
}
$this->data[] = $row;
}
}
}
$installedLanguages = array_merge($this->data);
// Process filters.
$clientId = (int) $this->getState('client_id');
$search = $this->getState('filter.search');
foreach ($installedLanguages as $key => $installedLanguage)
{
// Filter by client id.
if (in_array($clientId, array(0, 1)))
{
if ($installedLanguage->client_id !== $clientId)
{
unset($installedLanguages[$key]);
continue;
}
}
// Filter by search term.
if (!empty($search))
{
if (stripos($installedLanguage->name, $search) === false
&& stripos($installedLanguage->nativeName, $search) === false
&& stripos($installedLanguage->language, $search) === false)
{
unset($installedLanguages[$key]);
continue;
}
}
}
// Process ordering.
$listOrder = $this->getState('list.ordering', 'name');
$listDirn = $this->getState('list.direction', 'ASC');
$installedLanguages = ArrayHelper::sortObjects($installedLanguages, $listOrder, strtolower($listDirn) === 'desc' ? -1 : 1, true, true);
// Process pagination.
$limit = (int) $this->getState('list.limit', 25);
// Sets the total for pagination.
$this->total = count($installedLanguages);
if ($limit !== 0)
{
$start = (int) $this->getState('list.start', 0);
return array_slice($installedLanguages, $start, $limit);
}
return $installedLanguages;
}
/**
* Method to get the total number of Languages items.
*
* @return integer
*
* @since 1.6
*/
public function getTotal()
{
if (is_null($this->total))
{
$this->getData();
}
return $this->total;
}
/**
* Method to set the default language.
*
* @param integer $cid Id of the language to publish.
*
* @return boolean
*
* @since 1.6
*/
public function publish($cid)
{
if ($cid)
{
$client = $this->getClient();
$params = ComponentHelper::getParams('com_languages');
$params->set($client->name, $cid);
$table = Table::getInstance('extension', 'Joomla\\CMS\\Table\\');
$id = $table->find(array('element' => 'com_languages'));
// Load.
if (!$table->load($id))
{
$this->setError($table->getError());
return false;
}
$table->params = (string) $params;
// Pre-save checks.
if (!$table->check())
{
$this->setError($table->getError());
return false;
}
// Save the changes.
if (!$table->store())
{
$this->setError($table->getError());
return false;
}
}
else
{
$this->setError(Text::_('COM_LANGUAGES_ERR_NO_LANGUAGE_SELECTED'));
return false;
}
// Clean the cache of com_languages and component cache.
$this->cleanCache();
$this->cleanCache('_system', 0);
$this->cleanCache('_system', 1);
return true;
}
/**
* Method to get the folders.
*
* @return array Languages folders.
*
* @since 1.6
*/
protected function getFolders()
{
if (is_null($this->folders))
{
$path = $this->getPath();
$this->folders = Folder::folders($path, '.', false, false, array('.svn', 'CVS', '.DS_Store', '__MACOSX', 'pdf_fonts', 'overrides'));
}
return $this->folders;
}
/**
* Method to get the path.
*
* @return string The path to the languages folders.
*
* @since 1.6
*/
protected function getPath()
{
if (is_null($this->path))
{
$client = $this->getClient();
$this->path = LanguageHelper::getLanguagePath($client->path);
}
return $this->path;
}
/**
* Method to switch the administrator language.
*
* @param string $cid The language tag.
*
* @return boolean
*
* @since 3.5
*/
public function switchAdminLanguage($cid)
{
if ($cid)
{
$client = $this->getClient();
if ($client->name == 'administrator')
{
Factory::getApplication()->setUserState('application.lang', $cid);
}
}
else
{
Factory::getApplication()->enqueueMessage(Text::_('COM_LANGUAGES_ERR_NO_LANGUAGE_SELECTED'), 'error');
return false;
}
return true;
}
}
The same content is in 4 releases, Joomla 4.0.0-beta2 to 4.0.0-beta5. See them all, or every version of this file.