[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_messages/src/Model/ -> ConfigModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_messages
   6   *
   7   * @copyright   (C) 2008 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\Messages\Administrator\Model;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\MVC\Model\FormModel;
  16  use Joomla\CMS\Object\CMSObject;
  17  use Joomla\Database\ParameterType;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Message configuration model.
  25   *
  26   * @since  1.6
  27   */
  28  class ConfigModel extends FormModel
  29  {
  30      /**
  31       * Method to auto-populate the model state.
  32       *
  33       * This method should only be called once per instantiation and is designed
  34       * to be called on the first call to the getState() method unless the model
  35       * configuration flag to ignore the request is set.
  36       *
  37       * Note. Calling getState in this method will result in recursion.
  38       *
  39       * @return  void
  40       *
  41       * @since   1.6
  42       */
  43      protected function populateState()
  44      {
  45          $user = Factory::getUser();
  46  
  47          $this->setState('user.id', $user->get('id'));
  48  
  49          // Load the parameters.
  50          $params = ComponentHelper::getParams('com_messages');
  51          $this->setState('params', $params);
  52      }
  53  
  54      /**
  55       * Method to get a single record.
  56       *
  57       * @return  mixed  Object on success, false on failure.
  58       *
  59       * @since   1.6
  60       */
  61      public function &getItem()
  62      {
  63          $item   = new CMSObject();
  64          $userid = (int) $this->getState('user.id');
  65  
  66          $db = $this->getDatabase();
  67          $query = $db->getQuery(true);
  68          $query->select(
  69              [
  70                  $db->quoteName('cfg_name'),
  71                  $db->quoteName('cfg_value'),
  72              ]
  73          )
  74              ->from($db->quoteName('#__messages_cfg'))
  75              ->where($db->quoteName('user_id') . ' = :userid')
  76              ->bind(':userid', $userid, ParameterType::INTEGER);
  77  
  78          $db->setQuery($query);
  79  
  80          try {
  81              $rows = $db->loadObjectList();
  82          } catch (\RuntimeException $e) {
  83              $this->setError($e->getMessage());
  84  
  85              return false;
  86          }
  87  
  88          foreach ($rows as $row) {
  89              $item->set($row->cfg_name, $row->cfg_value);
  90          }
  91  
  92          $this->preprocessData('com_messages.config', $item);
  93  
  94          return $item;
  95      }
  96  
  97      /**
  98       * Method to get the record form.
  99       *
 100       * @param   array    $data      Data for the form.
 101       * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
 102       *
 103       * @return  \Joomla\CMS\Form\Form|bool  A Form object on success, false on failure
 104       *
 105       * @since   1.6
 106       */
 107      public function getForm($data = array(), $loadData = true)
 108      {
 109          // Get the form.
 110          $form = $this->loadForm('com_messages.config', 'config', array('control' => 'jform', 'load_data' => $loadData));
 111  
 112          if (empty($form)) {
 113              return false;
 114          }
 115  
 116          return $form;
 117      }
 118  
 119      /**
 120       * Method to save the form data.
 121       *
 122       * @param   array  $data  The form data.
 123       *
 124       * @return  boolean  True on success.
 125       *
 126       * @since   1.6
 127       */
 128      public function save($data)
 129      {
 130          $db = $this->getDatabase();
 131  
 132          if ($userId = (int) $this->getState('user.id')) {
 133              $query = $db->getQuery(true)
 134                  ->delete($db->quoteName('#__messages_cfg'))
 135                  ->where($db->quoteName('user_id') . ' = :userid')
 136                  ->bind(':userid', $userId, ParameterType::INTEGER);
 137              $db->setQuery($query);
 138  
 139              try {
 140                  $db->execute();
 141              } catch (\RuntimeException $e) {
 142                  $this->setError($e->getMessage());
 143  
 144                  return false;
 145              }
 146  
 147              if (count($data)) {
 148                  $query = $db->getQuery(true)
 149                      ->insert($db->quoteName('#__messages_cfg'))
 150                      ->columns(
 151                          [
 152                              $db->quoteName('user_id'),
 153                              $db->quoteName('cfg_name'),
 154                              $db->quoteName('cfg_value'),
 155                          ]
 156                      );
 157  
 158                  foreach ($data as $k => $v) {
 159                      $query->values(
 160                          implode(
 161                              ',',
 162                              $query->bindArray(
 163                                  [$userId , $k, $v],
 164                                  [ParameterType::INTEGER, ParameterType::STRING, ParameterType::STRING]
 165                              )
 166                          )
 167                      );
 168                  }
 169  
 170                  $db->setQuery($query);
 171  
 172                  try {
 173                      $db->execute();
 174                  } catch (\RuntimeException $e) {
 175                      $this->setError($e->getMessage());
 176  
 177                      return false;
 178                  }
 179              }
 180  
 181              return true;
 182          } else {
 183              $this->setError('COM_MESSAGES_ERR_INVALID_USER');
 184  
 185              return false;
 186          }
 187      }
 188  }


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