[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_languages/src/Model/ -> LanguageModel.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\Language\Text;
  17  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  18  use Joomla\CMS\MVC\Model\AdminModel;
  19  use Joomla\CMS\Object\CMSObject;
  20  use Joomla\CMS\Plugin\PluginHelper;
  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 Language Model
  30   *
  31   * @since  1.5
  32   */
  33  class LanguageModel extends AdminModel
  34  {
  35      /**
  36       * Constructor.
  37       *
  38       * @param   array                $config   An optional associative array of configuration settings.
  39       * @param   MVCFactoryInterface  $factory  The factory.
  40       *
  41       * @see     \Joomla\CMS\MVC\Model\BaseDatabaseModel
  42       * @since   3.2
  43       */
  44      public function __construct($config = array(), MVCFactoryInterface $factory = null)
  45      {
  46          $config = array_merge(
  47              array(
  48                  'event_after_save'  => 'onExtensionAfterSave',
  49                  'event_before_save' => 'onExtensionBeforeSave',
  50                  'events_map'        => array(
  51                      'save' => 'extension'
  52                  )
  53              ),
  54              $config
  55          );
  56  
  57          parent::__construct($config, $factory);
  58      }
  59  
  60      /**
  61       * Override to get the table.
  62       *
  63       * @param   string  $name     Name of the table.
  64       * @param   string  $prefix   Table name prefix.
  65       * @param   array   $options  Array of options.
  66       *
  67       * @return  Table
  68       *
  69       * @since   1.6
  70       */
  71      public function getTable($name = '', $prefix = '', $options = array())
  72      {
  73          return Table::getInstance('Language', 'Joomla\\CMS\\Table\\');
  74      }
  75  
  76      /**
  77       * Method to auto-populate the model state.
  78       *
  79       * Note. Calling getState in this method will result in recursion.
  80       *
  81       * @return  void
  82       *
  83       * @since   1.6
  84       */
  85      protected function populateState()
  86      {
  87          $app    = Factory::getApplication();
  88          $params = ComponentHelper::getParams('com_languages');
  89  
  90          // Load the User state.
  91          $langId = $app->input->getInt('lang_id');
  92          $this->setState('language.id', $langId);
  93  
  94          // Load the parameters.
  95          $this->setState('params', $params);
  96      }
  97  
  98      /**
  99       * Method to get a member item.
 100       *
 101       * @param   integer  $langId  The id of the member to get.
 102       *
 103       * @return  mixed  User data object on success, false on failure.
 104       *
 105       * @since   1.0
 106       */
 107      public function getItem($langId = null)
 108      {
 109          $langId = (!empty($langId)) ? $langId : (int) $this->getState('language.id');
 110  
 111          // Get a member row instance.
 112          $table = $this->getTable();
 113  
 114          // Attempt to load the row.
 115          $return = $table->load($langId);
 116  
 117          // Check for a table object error.
 118          if ($return === false && $table->getError()) {
 119              $this->setError($table->getError());
 120  
 121              return false;
 122          }
 123  
 124          // Set a valid accesslevel in case '0' is stored due to a bug in the installation SQL (was fixed with PR 2714).
 125          if ($table->access == '0') {
 126              $table->access = (int) Factory::getApplication()->get('access');
 127          }
 128  
 129          $properties = $table->getProperties(1);
 130          $value      = ArrayHelper::toObject($properties, CMSObject::class);
 131  
 132          return $value;
 133      }
 134  
 135      /**
 136       * Method to get the group form.
 137       *
 138       * @param   array    $data      Data for the form.
 139       * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
 140       *
 141       * @return  \Joomla\CMS\Form\Form|bool  A Form object on success, false on failure.
 142       *
 143       * @since   1.6
 144       */
 145      public function getForm($data = array(), $loadData = true)
 146      {
 147          // Get the form.
 148          $form = $this->loadForm('com_languages.language', 'language', array('control' => 'jform', 'load_data' => $loadData));
 149  
 150          if (empty($form)) {
 151              return false;
 152          }
 153  
 154          return $form;
 155      }
 156  
 157      /**
 158       * Method to get the data that should be injected in the form.
 159       *
 160       * @return  mixed  The data for the form.
 161       *
 162       * @since   1.6
 163       */
 164      protected function loadFormData()
 165      {
 166          // Check the session for previously entered form data.
 167          $data = Factory::getApplication()->getUserState('com_languages.edit.language.data', array());
 168  
 169          if (empty($data)) {
 170              $data = $this->getItem();
 171          }
 172  
 173          $this->preprocessData('com_languages.language', $data);
 174  
 175          return $data;
 176      }
 177  
 178      /**
 179       * Method to save the form data.
 180       *
 181       * @param   array  $data  The form data.
 182       *
 183       * @return  boolean  True on success.
 184       *
 185       * @since   1.6
 186       */
 187      public function save($data)
 188      {
 189          $langId = (!empty($data['lang_id'])) ? $data['lang_id'] : (int) $this->getState('language.id');
 190          $isNew  = true;
 191  
 192          PluginHelper::importPlugin($this->events_map['save']);
 193  
 194          $table   = $this->getTable();
 195          $context = $this->option . '.' . $this->name;
 196  
 197          // Load the row if saving an existing item.
 198          if ($langId > 0) {
 199              $table->load($langId);
 200              $isNew = false;
 201          }
 202  
 203          // Prevent white spaces, including East Asian double bytes.
 204          $spaces = array('/\xE3\x80\x80/', ' ');
 205  
 206          $data['lang_code'] = str_replace($spaces, '', $data['lang_code']);
 207  
 208          // Prevent saving an incorrect language tag
 209          if (!preg_match('#\b([a-z]{2,3})[-]([A-Z]{2})\b#', $data['lang_code'])) {
 210              $this->setError(Text::_('COM_LANGUAGES_ERROR_LANG_TAG'));
 211  
 212              return false;
 213          }
 214  
 215          $data['sef'] = str_replace($spaces, '', $data['sef']);
 216          $data['sef'] = ApplicationHelper::stringURLSafe($data['sef']);
 217  
 218          // Prevent saving an empty url language code
 219          if ($data['sef'] === '') {
 220              $this->setError(Text::_('COM_LANGUAGES_ERROR_SEF'));
 221  
 222              return false;
 223          }
 224  
 225          // Bind the data.
 226          if (!$table->bind($data)) {
 227              $this->setError($table->getError());
 228  
 229              return false;
 230          }
 231  
 232          // Check the data.
 233          if (!$table->check()) {
 234              $this->setError($table->getError());
 235  
 236              return false;
 237          }
 238  
 239          // Trigger the before save event.
 240          $result = Factory::getApplication()->triggerEvent($this->event_before_save, array($context, &$table, $isNew));
 241  
 242          // Check the event responses.
 243          if (in_array(false, $result, true)) {
 244              $this->setError($table->getError());
 245  
 246              return false;
 247          }
 248  
 249          // Store the data.
 250          if (!$table->store()) {
 251              $this->setError($table->getError());
 252  
 253              return false;
 254          }
 255  
 256          // Trigger the after save event.
 257          Factory::getApplication()->triggerEvent($this->event_after_save, array($context, &$table, $isNew));
 258  
 259          $this->setState('language.id', $table->lang_id);
 260  
 261          // Clean the cache.
 262          $this->cleanCache();
 263  
 264          return true;
 265      }
 266  
 267      /**
 268       * Custom clean cache method.
 269       *
 270       * @param   string   $group     Optional cache group name.
 271       * @param   integer  $clientId  @deprecated   5.0   No longer used.
 272       *
 273       * @return  void
 274       *
 275       * @since   1.6
 276       */
 277      protected function cleanCache($group = null, $clientId = 0)
 278      {
 279          parent::cleanCache('_system');
 280          parent::cleanCache('com_languages');
 281      }
 282  }


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