[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_languages
   6   *
   7   * @copyright   (C) 2011 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\Factory;
  14  use Joomla\CMS\Filesystem\File;
  15  use Joomla\CMS\Language\LanguageHelper;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  18  use Joomla\CMS\MVC\Model\ListModel;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('_JEXEC') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Languages Overrides Model
  26   *
  27   * @since  2.5
  28   */
  29  class OverridesModel extends ListModel
  30  {
  31      /**
  32       * Constructor.
  33       *
  34       * @param   array                $config   An optional associative array of configuration settings.
  35       * @param   MVCFactoryInterface  $factory  The factory.
  36       *
  37       * @see     \Joomla\CMS\MVC\Model\BaseDatabaseModel
  38       * @since   2.5
  39       */
  40      public function __construct($config = array(), MVCFactoryInterface $factory = null)
  41      {
  42          if (empty($config['filter_fields'])) {
  43              $config['filter_fields'] = array(
  44                  'key',
  45                  'text',
  46              );
  47          }
  48  
  49          parent::__construct($config, $factory);
  50      }
  51  
  52      /**
  53       * Retrieves the overrides data
  54       *
  55       * @param   boolean  $all  True if all overrides shall be returned without considering pagination, defaults to false
  56       *
  57       * @return  array  Array of objects containing the overrides of the override.ini file
  58       *
  59       * @since   2.5
  60       */
  61      public function getOverrides($all = false)
  62      {
  63          // Get a storage key.
  64          $store = $this->getStoreId();
  65  
  66          // Try to load the data from internal storage.
  67          if (!empty($this->cache[$store])) {
  68              return $this->cache[$store];
  69          }
  70  
  71          $client = strtoupper($this->getState('filter.client'));
  72  
  73          // Parse the override.ini file in order to get the keys and strings.
  74          $fileName = constant('JPATH_' . $client) . '/language/overrides/' . $this->getState('filter.language') . '.override.ini';
  75          $strings  = LanguageHelper::parseIniFile($fileName);
  76  
  77          // Delete the override.ini file if empty.
  78          if (file_exists($fileName) && $strings === array()) {
  79              File::delete($fileName);
  80          }
  81  
  82          // Filter the loaded strings according to the search box.
  83          $search = $this->getState('filter.search');
  84  
  85          if ($search != '') {
  86              $search = preg_quote($search, '~');
  87              $matchvals = preg_grep('~' . $search . '~i', $strings);
  88              $matchkeys = array_intersect_key($strings, array_flip(preg_grep('~' . $search . '~i', array_keys($strings))));
  89              $strings = array_merge($matchvals, $matchkeys);
  90          }
  91  
  92          // Consider the ordering
  93          if ($this->getState('list.ordering') == 'text') {
  94              if (strtoupper($this->getState('list.direction')) == 'DESC') {
  95                  arsort($strings);
  96              } else {
  97                  asort($strings);
  98              }
  99          } else {
 100              if (strtoupper($this->getState('list.direction')) == 'DESC') {
 101                  krsort($strings);
 102              } else {
 103                  ksort($strings);
 104              }
 105          }
 106  
 107          // Consider the pagination.
 108          if (!$all && $this->getState('list.limit') && $this->getTotal() > $this->getState('list.limit')) {
 109              $strings = array_slice($strings, $this->getStart(), $this->getState('list.limit'), true);
 110          }
 111  
 112          // Add the items to the internal cache.
 113          $this->cache[$store] = $strings;
 114  
 115          return $this->cache[$store];
 116      }
 117  
 118      /**
 119       * Method to get the total number of overrides.
 120       *
 121       * @return  integer  The total number of overrides.
 122       *
 123       * @since   2.5
 124       */
 125      public function getTotal()
 126      {
 127          // Get a storage key.
 128          $store = $this->getStoreId('getTotal');
 129  
 130          // Try to load the data from internal storage
 131          if (!empty($this->cache[$store])) {
 132              return $this->cache[$store];
 133          }
 134  
 135          // Add the total to the internal cache.
 136          $this->cache[$store] = count($this->getOverrides(true));
 137  
 138          return $this->cache[$store];
 139      }
 140  
 141      /**
 142       * Method to auto-populate the model state.
 143       *
 144       * Note. Calling getState in this method will result in recursion.
 145       *
 146       * @param   string  $ordering   An optional ordering field.
 147       * @param   string  $direction  An optional direction (asc|desc).
 148       *
 149       * @return  void
 150       *
 151       * @since   2.5
 152       */
 153      protected function populateState($ordering = 'key', $direction = 'asc')
 154      {
 155          // We call populate state first so that we can then set the filter.client and filter.language properties in afterwards
 156          parent::populateState($ordering, $direction);
 157  
 158          $app = Factory::getApplication();
 159  
 160          if ($app->isClient('api')) {
 161              return;
 162          }
 163  
 164          $language_client = $this->getUserStateFromRequest('com_languages.overrides.language_client', 'language_client', '', 'cmd');
 165          $client          = substr($language_client, -1);
 166          $language        = substr($language_client, 0, -1);
 167  
 168          // Sets the search filter.
 169          $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
 170          $this->setState('filter.search', $search);
 171  
 172          $this->setState('language_client', $language . $client);
 173          $this->setState('filter.client', $client ? 'administrator' : 'site');
 174          $this->setState('filter.language', $language);
 175  
 176          // Add the 'language_client' value to the session to display a message if none selected
 177          $app->setUserState('com_languages.overrides.language_client', $language . $client);
 178  
 179          // Add filters to the session because they won't be stored there by 'getUserStateFromRequest' if they aren't in the current request.
 180          $app->setUserState('com_languages.overrides.filter.client', $client);
 181          $app->setUserState('com_languages.overrides.filter.language', $language);
 182      }
 183  
 184      /**
 185       * Method to delete one or more overrides.
 186       *
 187       * @param   array  $cids  Array of keys to delete.
 188       *
 189       * @return  integer  Number of successfully deleted overrides, boolean false if an error occurred.
 190       *
 191       * @since   2.5
 192       */
 193      public function delete($cids)
 194      {
 195          // Check permissions first.
 196          if (!Factory::getUser()->authorise('core.delete', 'com_languages')) {
 197              $this->setError(Text::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED'));
 198  
 199              return false;
 200          }
 201  
 202          $app = Factory::getApplication();
 203  
 204          if ($app->isClient('api')) {
 205              $cids = (array) $cids;
 206              $client = $this->getState('filter.client');
 207          } else {
 208              $filterclient = Factory::getApplication()->getUserState('com_languages.overrides.filter.client');
 209              $client = $filterclient == 0 ? 'site' : 'administrator';
 210          }
 211  
 212          // Parse the override.ini file in order to get the keys and strings.
 213          $fileName = constant('JPATH_' . strtoupper($client)) . '/language/overrides/' . $this->getState('filter.language') . '.override.ini';
 214          $strings  = LanguageHelper::parseIniFile($fileName);
 215  
 216          // Unset strings that shall be deleted
 217          foreach ($cids as $key) {
 218              if (isset($strings[$key])) {
 219                  unset($strings[$key]);
 220              }
 221          }
 222  
 223          // Write override.ini file with the strings.
 224          if (LanguageHelper::saveToIniFile($fileName, $strings) === false) {
 225              return false;
 226          }
 227  
 228          $this->cleanCache();
 229  
 230          return count($cids);
 231      }
 232  
 233      /**
 234       * Removes all of the cached strings from the table.
 235       *
 236       * @return  boolean  result of operation
 237       *
 238       * @since   3.4.2
 239       */
 240      public function purge()
 241      {
 242          $db = $this->getDatabase();
 243  
 244          // Note: TRUNCATE is a DDL operation
 245          // This may or may not mean depending on your database
 246          try {
 247              $db->truncateTable('#__overrider');
 248          } catch (\RuntimeException $e) {
 249              return $e;
 250          }
 251  
 252          Factory::getApplication()->enqueueMessage(Text::_('COM_LANGUAGES_VIEW_OVERRIDES_PURGE_SUCCESS'));
 253      }
 254  }


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