[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_installer/src/Model/ -> LanguagesModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_installer
   6   * @copyright   (C) 2006 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\Component\Installer\Administrator\Model;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\Http\HttpFactory;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  16  use Joomla\CMS\MVC\Model\ListModel;
  17  use Joomla\String\StringHelper;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Languages Installer Model
  25   *
  26   * @since  2.5.7
  27   */
  28  class LanguagesModel extends ListModel
  29  {
  30      /**
  31       * Language count
  32       *
  33       * @var     integer
  34       * @since   3.7.0
  35       */
  36      private $languageCount;
  37  
  38      /**
  39       * Constructor.
  40       *
  41       * @param   array                $config   An optional associative array of configuration settings.
  42       * @param   MVCFactoryInterface  $factory  The factory.
  43       *
  44       * @see     \Joomla\CMS\MVC\Model\ListModel
  45       * @since   1.6
  46       */
  47      public function __construct($config = array(), MVCFactoryInterface $factory = null)
  48      {
  49          if (empty($config['filter_fields'])) {
  50              $config['filter_fields'] = array(
  51                  'name',
  52                  'element',
  53              );
  54          }
  55  
  56          parent::__construct($config, $factory);
  57      }
  58  
  59      /**
  60       * Get the Update Site
  61       *
  62       * @since   3.7.0
  63       *
  64       * @return  string  The URL of the Accredited Languagepack Updatesite XML
  65       */
  66      private function getUpdateSite()
  67      {
  68          $db    = $this->getDatabase();
  69          $query = $db->getQuery(true)
  70              ->select($db->quoteName('us.location'))
  71              ->from($db->quoteName('#__extensions', 'e'))
  72              ->where($db->quoteName('e.type') . ' = ' . $db->quote('package'))
  73              ->where($db->quoteName('e.element') . ' = ' . $db->quote('pkg_en-GB'))
  74              ->where($db->quoteName('e.client_id') . ' = 0')
  75              ->join(
  76                  'LEFT',
  77                  $db->quoteName('#__update_sites_extensions', 'use')
  78                  . ' ON ' . $db->quoteName('use.extension_id') . ' = ' . $db->quoteName('e.extension_id')
  79              )
  80              ->join(
  81                  'LEFT',
  82                  $db->quoteName('#__update_sites', 'us')
  83                  . ' ON ' . $db->quoteName('us.update_site_id') . ' = ' . $db->quoteName('use.update_site_id')
  84              );
  85  
  86          return $db->setQuery($query)->loadResult();
  87      }
  88  
  89      /**
  90       * Method to get an array of data items.
  91       *
  92       * @return  mixed  An array of data items on success, false on failure.
  93       *
  94       * @since   3.7.0
  95       */
  96      public function getItems()
  97      {
  98          // Get a storage key.
  99          $store = $this->getStoreId();
 100  
 101          // Try to load the data from internal storage.
 102          if (isset($this->cache[$store])) {
 103              return $this->cache[$store];
 104          }
 105  
 106          try {
 107              // Load the list items and add the items to the internal cache.
 108              $this->cache[$store] = $this->getLanguages();
 109          } catch (\RuntimeException $e) {
 110              $this->setError($e->getMessage());
 111  
 112              return false;
 113          }
 114  
 115          return $this->cache[$store];
 116      }
 117  
 118      /**
 119       * Gets an array of objects from the updatesite.
 120       *
 121       * @return  object[]  An array of results.
 122       *
 123       * @since   3.0
 124       * @throws  \RuntimeException
 125       */
 126      protected function getLanguages()
 127      {
 128          $updateSite = $this->getUpdateSite();
 129  
 130          // Check whether the updateserver is found
 131          if (empty($updateSite)) {
 132              Factory::getApplication()->enqueueMessage(Text::_('COM_INSTALLER_MSG_WARNING_NO_LANGUAGES_UPDATESERVER'), 'warning');
 133  
 134              return;
 135          }
 136  
 137          try {
 138              $response = HttpFactory::getHttp()->get($updateSite);
 139          } catch (\RuntimeException $e) {
 140              $response = null;
 141          }
 142  
 143          if ($response === null || $response->code !== 200) {
 144              Factory::getApplication()->enqueueMessage(Text::sprintf('COM_INSTALLER_MSG_ERROR_CANT_CONNECT_TO_UPDATESERVER', $updateSite), 'error');
 145  
 146              return;
 147          }
 148  
 149          $updateSiteXML = simplexml_load_string($response->body);
 150  
 151          if (!$updateSiteXML) {
 152              Factory::getApplication()->enqueueMessage(Text::sprintf('COM_INSTALLER_MSG_ERROR_CANT_RETRIEVE_XML', $updateSite), 'error');
 153  
 154              return;
 155          }
 156  
 157          $languages     = array();
 158          $search        = strtolower($this->getState('filter.search'));
 159  
 160          foreach ($updateSiteXML->extension as $extension) {
 161              $language = new \stdClass();
 162  
 163              foreach ($extension->attributes() as $key => $value) {
 164                  $language->$key = (string) $value;
 165              }
 166  
 167              if ($search) {
 168                  if (
 169                      strpos(strtolower($language->name), $search) === false
 170                      && strpos(strtolower($language->element), $search) === false
 171                  ) {
 172                      continue;
 173                  }
 174              }
 175  
 176              $languages[$language->name] = $language;
 177          }
 178  
 179          // Workaround for php 5.3
 180          $that = $this;
 181  
 182          // Sort the array by value of subarray
 183          usort(
 184              $languages,
 185              function ($a, $b) use ($that) {
 186                  $ordering = $that->getState('list.ordering');
 187  
 188                  if (strtolower($that->getState('list.direction')) === 'asc') {
 189                      return StringHelper::strcmp($a->$ordering, $b->$ordering);
 190                  } else {
 191                      return StringHelper::strcmp($b->$ordering, $a->$ordering);
 192                  }
 193              }
 194          );
 195  
 196          // Count the non-paginated list
 197          $this->languageCount = count($languages);
 198          $limit               = ($this->getState('list.limit') > 0) ? $this->getState('list.limit') : $this->languageCount;
 199  
 200          return array_slice($languages, $this->getStart(), $limit);
 201      }
 202  
 203      /**
 204       * Returns a record count for the updatesite.
 205       *
 206       * @param   \Joomla\Database\DatabaseQuery|string  $query  The query.
 207       *
 208       * @return  integer  Number of rows for query.
 209       *
 210       * @since   3.7.0
 211       */
 212      protected function _getListCount($query)
 213      {
 214          return $this->languageCount;
 215      }
 216  
 217      /**
 218       * Method to get a store id based on model configuration state.
 219       *
 220       * @param   string  $id  A prefix for the store id.
 221       *
 222       * @return  string  A store id.
 223       *
 224       * @since   2.5.7
 225       */
 226      protected function getStoreId($id = '')
 227      {
 228          // Compile the store id.
 229          $id .= ':' . $this->getState('filter.search');
 230  
 231          return parent::getStoreId($id);
 232      }
 233  
 234      /**
 235       * Method to auto-populate the model state.
 236       *
 237       * Note. Calling getState in this method will result in recursion.
 238       *
 239       * @param   string  $ordering   list order
 240       * @param   string  $direction  direction in the list
 241       *
 242       * @return  void
 243       *
 244       * @since   2.5.7
 245       */
 246      protected function populateState($ordering = 'name', $direction = 'asc')
 247      {
 248          $this->setState('filter.search', $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search', '', 'string'));
 249  
 250          $this->setState('extension_message', Factory::getApplication()->getUserState('com_installer.extension_message'));
 251  
 252          parent::populateState($ordering, $direction);
 253      }
 254  
 255      /**
 256       * Method to compare two languages in order to sort them.
 257       *
 258       * @param   object  $lang1  The first language.
 259       * @param   object  $lang2  The second language.
 260       *
 261       * @return  integer
 262       *
 263       * @since   3.7.0
 264       */
 265      protected function compareLanguages($lang1, $lang2)
 266      {
 267          return strcmp($lang1->name, $lang2->name);
 268      }
 269  }


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