[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_languages/src/Model/ -> InstalledModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_languages
   6   *
   7   * @copyright   (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Languages\Administrator\Model;
  12  
  13  use Joomla\CMS\Application\ApplicationHelper;
  14  use Joomla\CMS\Component\ComponentHelper;
  15  use Joomla\CMS\Factory;
  16  use Joomla\CMS\Filesystem\Folder;
  17  use Joomla\CMS\Language\LanguageHelper;
  18  use Joomla\CMS\Language\Text;
  19  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  20  use Joomla\CMS\MVC\Model\ListModel;
  21  use Joomla\CMS\Table\Table;
  22  use Joomla\Utilities\ArrayHelper;
  23  
  24  // phpcs:disable PSR1.Files.SideEffects
  25  \defined('_JEXEC') or die;
  26  // phpcs:enable PSR1.Files.SideEffects
  27  
  28  /**
  29   * Languages Component Languages Model
  30   *
  31   * @since  1.6
  32   */
  33  class InstalledModel extends ListModel
  34  {
  35      /**
  36       * @var object user object
  37       */
  38      protected $user = null;
  39  
  40      /**
  41       * @var string option name
  42       */
  43      protected $option = null;
  44  
  45      /**
  46       * @var array languages description
  47       */
  48      protected $data = null;
  49  
  50      /**
  51       * @var integer total number of languages
  52       */
  53      protected $total = null;
  54  
  55      /**
  56       * @var string language path
  57       */
  58      protected $path = null;
  59  
  60      /**
  61       * Constructor.
  62       *
  63       * @param   array                $config   An optional associative array of configuration settings.
  64       * @param   MVCFactoryInterface  $factory  The factory.
  65       *
  66       * @see     \Joomla\CMS\MVC\Model\BaseDatabaseModel
  67       * @since   3.2
  68       */
  69      public function __construct($config = array(), MVCFactoryInterface $factory = null)
  70      {
  71          if (empty($config['filter_fields'])) {
  72              $config['filter_fields'] = array(
  73                  'name',
  74                  'nativeName',
  75                  'language',
  76                  'author',
  77                  'published',
  78                  'version',
  79                  'creationDate',
  80                  'author',
  81                  'authorEmail',
  82                  'extension_id',
  83                  'client_id',
  84              );
  85          }
  86  
  87          parent::__construct($config, $factory);
  88      }
  89  
  90      /**
  91       * Method to auto-populate the model state.
  92       *
  93       * Note. Calling getState in this method will result in recursion.
  94       *
  95       * @param   string  $ordering   An optional ordering field.
  96       * @param   string  $direction  An optional direction (asc|desc).
  97       *
  98       * @return  void
  99       *
 100       * @since   1.6
 101       */
 102      protected function populateState($ordering = 'name', $direction = 'asc')
 103      {
 104          // Load the filter state.
 105          $this->setState('filter.search', $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search', '', 'string'));
 106  
 107          // Special case for client id.
 108          $clientId = (int) $this->getUserStateFromRequest($this->context . '.client_id', 'client_id', 0, 'int');
 109          $clientId = (!in_array($clientId, array (0, 1))) ? 0 : $clientId;
 110          $this->setState('client_id', $clientId);
 111  
 112          // Load the parameters.
 113          $params = ComponentHelper::getParams('com_languages');
 114          $this->setState('params', $params);
 115  
 116          // List state information.
 117          parent::populateState($ordering, $direction);
 118      }
 119  
 120      /**
 121       * Method to get a store id based on model configuration state.
 122       *
 123       * This is necessary because the model is used by the component and
 124       * different modules that might need different sets of data or different
 125       * ordering requirements.
 126       *
 127       * @param   string  $id  A prefix for the store id.
 128       *
 129       * @return  string  A store id.
 130       *
 131       * @since   1.6
 132       */
 133      protected function getStoreId($id = '')
 134      {
 135          // Compile the store id.
 136          $id .= ':' . $this->getState('client_id');
 137          $id .= ':' . $this->getState('filter.search');
 138  
 139          return parent::getStoreId($id);
 140      }
 141  
 142      /**
 143       * Method to get the client object.
 144       *
 145       * @return  object
 146       *
 147       * @since   1.6
 148       */
 149      public function getClient()
 150      {
 151          return ApplicationHelper::getClientInfo($this->getState('client_id', 0));
 152      }
 153  
 154      /**
 155       * Method to get the option.
 156       *
 157       * @return  object
 158       *
 159       * @since   1.6
 160       */
 161      public function getOption()
 162      {
 163          $option = $this->getState('option');
 164  
 165          return $option;
 166      }
 167  
 168      /**
 169       * Method to get Languages item data.
 170       *
 171       * @return  array
 172       *
 173       * @since   1.6
 174       */
 175      public function getData()
 176      {
 177          // Fetch language data if not fetched yet.
 178          if (is_null($this->data)) {
 179              $this->data = array();
 180  
 181              $isCurrentLanguageRtl = Factory::getLanguage()->isRtl();
 182              $params               = ComponentHelper::getParams('com_languages');
 183              $installedLanguages   = LanguageHelper::getInstalledLanguages(null, true, true, null, null, null);
 184  
 185              // Compute all the languages.
 186              foreach ($installedLanguages as $clientId => $languages) {
 187                  $defaultLanguage = $params->get(ApplicationHelper::getClientInfo($clientId)->name, 'en-GB');
 188  
 189                  foreach ($languages as $lang) {
 190                      $row               = new \stdClass();
 191                      $row->language     = $lang->element;
 192                      $row->name         = $lang->metadata['name'];
 193                      $row->nativeName   = $lang->metadata['nativeName'] ?? '-';
 194                      $row->client_id    = (int) $lang->client_id;
 195                      $row->extension_id = (int) $lang->extension_id;
 196                      $row->author       = $lang->manifest['author'];
 197                      $row->creationDate = $lang->manifest['creationDate'];
 198                      $row->authorEmail  = $lang->manifest['authorEmail'];
 199                      $row->version      = $lang->manifest['version'];
 200                      $row->published    = $defaultLanguage === $row->language ? 1 : 0;
 201                      $row->checked_out  = null;
 202  
 203                      // Fix wrongly set parentheses in RTL languages
 204                      if ($isCurrentLanguageRtl) {
 205                          $row->name       = html_entity_decode($row->name . '&#x200E;', ENT_QUOTES, 'UTF-8');
 206                          $row->nativeName = html_entity_decode($row->nativeName . '&#x200E;', ENT_QUOTES, 'UTF-8');
 207                      }
 208  
 209                      $this->data[] = $row;
 210                  }
 211              }
 212          }
 213  
 214          $installedLanguages = array_merge($this->data);
 215  
 216          // Process filters.
 217          $clientId = (int) $this->getState('client_id');
 218          $search   = $this->getState('filter.search');
 219  
 220          foreach ($installedLanguages as $key => $installedLanguage) {
 221              // Filter by client id.
 222              if (in_array($clientId, array(0, 1))) {
 223                  if ($installedLanguage->client_id !== $clientId) {
 224                      unset($installedLanguages[$key]);
 225                      continue;
 226                  }
 227              }
 228  
 229              // Filter by search term.
 230              if (!empty($search)) {
 231                  if (
 232                      stripos($installedLanguage->name, $search) === false
 233                      && stripos($installedLanguage->nativeName, $search) === false
 234                      && stripos($installedLanguage->language, $search) === false
 235                  ) {
 236                      unset($installedLanguages[$key]);
 237                  }
 238              }
 239          }
 240  
 241          // Process ordering.
 242          $listOrder = $this->getState('list.ordering', 'name');
 243          $listDirn  = $this->getState('list.direction', 'ASC');
 244          $installedLanguages = ArrayHelper::sortObjects($installedLanguages, $listOrder, strtolower($listDirn) === 'desc' ? -1 : 1, true, true);
 245  
 246          // Process pagination.
 247          $limit = (int) $this->getState('list.limit', 25);
 248  
 249          // Sets the total for pagination.
 250          $this->total = count($installedLanguages);
 251  
 252          if ($limit !== 0) {
 253              $start = (int) $this->getState('list.start', 0);
 254  
 255              return array_slice($installedLanguages, $start, $limit);
 256          }
 257  
 258          return $installedLanguages;
 259      }
 260  
 261      /**
 262       * Method to get the total number of Languages items.
 263       *
 264       * @return  integer
 265       *
 266       * @since   1.6
 267       */
 268      public function getTotal()
 269      {
 270          if (is_null($this->total)) {
 271              $this->getData();
 272          }
 273  
 274          return $this->total;
 275      }
 276  
 277      /**
 278       * Method to set the default language.
 279       *
 280       * @param   integer  $cid  Id of the language to publish.
 281       *
 282       * @return  boolean
 283       *
 284       * @since   1.6
 285       */
 286      public function publish($cid)
 287      {
 288          if ($cid) {
 289              $client = $this->getClient();
 290  
 291              $params = ComponentHelper::getParams('com_languages');
 292              $params->set($client->name, $cid);
 293  
 294              $table = Table::getInstance('extension', 'Joomla\\CMS\\Table\\');
 295              $id    = $table->find(array('element' => 'com_languages'));
 296  
 297              // Load.
 298              if (!$table->load($id)) {
 299                  $this->setError($table->getError());
 300  
 301                  return false;
 302              }
 303  
 304              $table->params = (string) $params;
 305  
 306              // Pre-save checks.
 307              if (!$table->check()) {
 308                  $this->setError($table->getError());
 309  
 310                  return false;
 311              }
 312  
 313              // Save the changes.
 314              if (!$table->store()) {
 315                  $this->setError($table->getError());
 316  
 317                  return false;
 318              }
 319          } else {
 320              $this->setError(Text::_('COM_LANGUAGES_ERR_NO_LANGUAGE_SELECTED'));
 321  
 322              return false;
 323          }
 324  
 325          // Clean the cache of com_languages and component cache.
 326          $this->cleanCache();
 327          $this->cleanCache('_system');
 328  
 329          return true;
 330      }
 331  
 332      /**
 333       * Method to get the folders.
 334       *
 335       * @return  array  Languages folders.
 336       *
 337       * @since   1.6
 338       */
 339      protected function getFolders()
 340      {
 341          if (is_null($this->folders)) {
 342              $path = $this->getPath();
 343              $this->folders = Folder::folders($path, '.', false, false, array('.svn', 'CVS', '.DS_Store', '__MACOSX', 'pdf_fonts', 'overrides'));
 344          }
 345  
 346          return $this->folders;
 347      }
 348  
 349      /**
 350       * Method to get the path.
 351       *
 352       * @return  string  The path to the languages folders.
 353       *
 354       * @since   1.6
 355       */
 356      protected function getPath()
 357      {
 358          if (is_null($this->path)) {
 359              $client     = $this->getClient();
 360              $this->path = LanguageHelper::getLanguagePath($client->path);
 361          }
 362  
 363          return $this->path;
 364      }
 365  
 366      /**
 367       * Method to switch the administrator language.
 368       *
 369       * @param   string  $cid  The language tag.
 370       *
 371       * @return  boolean
 372       *
 373       * @since   3.5
 374       */
 375      public function switchAdminLanguage($cid)
 376      {
 377          if ($cid) {
 378              $client = $this->getClient();
 379  
 380              if ($client->name == 'administrator') {
 381                  Factory::getApplication()->setUserState('application.lang', $cid);
 382              }
 383          } else {
 384              Factory::getApplication()->enqueueMessage(Text::_('COM_LANGUAGES_ERR_NO_LANGUAGE_SELECTED'), 'error');
 385  
 386              return false;
 387          }
 388  
 389          return true;
 390      }
 391  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer