[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Table/ -> MenuType.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2009 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\CMS\Table;
  11  
  12  use Joomla\CMS\Application\ApplicationHelper;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\Database\DatabaseDriver;
  16  use Joomla\Database\ParameterType;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('JPATH_PLATFORM') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Menu Types table
  24   *
  25   * @since  1.6
  26   */
  27  class MenuType extends Table
  28  {
  29      /**
  30       * Constructor
  31       *
  32       * @param   DatabaseDriver  $db  Database driver object.
  33       *
  34       * @since   1.6
  35       */
  36      public function __construct(DatabaseDriver $db)
  37      {
  38          parent::__construct('#__menu_types', 'id', $db);
  39      }
  40  
  41      /**
  42       * Overloaded check function
  43       *
  44       * @return  boolean  True on success, false on failure
  45       *
  46       * @see     Table::check()
  47       * @since   1.6
  48       */
  49      public function check()
  50      {
  51          try {
  52              parent::check();
  53          } catch (\Exception $e) {
  54              $this->setError($e->getMessage());
  55  
  56              return false;
  57          }
  58  
  59          $this->menutype = ApplicationHelper::stringURLSafe($this->menutype);
  60  
  61          if (empty($this->menutype)) {
  62              $this->setError(Text::_('JLIB_DATABASE_ERROR_MENUTYPE_EMPTY'));
  63  
  64              return false;
  65          }
  66  
  67          // Sanitise data.
  68          if (trim($this->title) === '') {
  69              $this->title = $this->menutype;
  70          }
  71  
  72          $id = (int) $this->id;
  73  
  74          // Check for unique menutype.
  75          $query = $this->_db->getQuery(true)
  76              ->select('COUNT(id)')
  77              ->from($this->_db->quoteName('#__menu_types'))
  78              ->where($this->_db->quoteName('menutype') . ' = :menutype')
  79              ->where($this->_db->quoteName('id') . ' <> :id')
  80              ->bind(':menutype', $this->menutype)
  81              ->bind(':id', $id, ParameterType::INTEGER);
  82          $this->_db->setQuery($query);
  83  
  84          if ($this->_db->loadResult()) {
  85              $this->setError(Text::sprintf('JLIB_DATABASE_ERROR_MENUTYPE_EXISTS', $this->menutype));
  86  
  87              return false;
  88          }
  89  
  90          return true;
  91      }
  92  
  93      /**
  94       * Method to store a row in the database from the Table instance properties.
  95       *
  96       * If a primary key value is set the row with that primary key value will be updated with the instance property values.
  97       * If no primary key value is set a new row will be inserted into the database with the properties from the Table instance.
  98       *
  99       * @param   boolean  $updateNulls  True to update fields even if they are null.
 100       *
 101       * @return  boolean  True on success.
 102       *
 103       * @since   1.6
 104       */
 105      public function store($updateNulls = false)
 106      {
 107          if ($this->id) {
 108              // Get the user id
 109              $userId = (int) Factory::getUser()->id;
 110              $notIn  = [0, $userId];
 111  
 112              // Get the old value of the table
 113              $table = Table::getInstance('Menutype', 'JTable', array('dbo' => $this->getDbo()));
 114              $table->load($this->id);
 115  
 116              // Verify that no items are checked out
 117              $query = $this->_db->getQuery(true)
 118                  ->select($this->_db->quoteName('id'))
 119                  ->from($this->_db->quoteName('#__menu'))
 120                  ->where($this->_db->quoteName('menutype') . ' = :menutype')
 121                  ->whereNotIn($this->_db->quoteName('checked_out'), $notIn)
 122                  ->bind(':menutype', $table->menutype);
 123              $this->_db->setQuery($query);
 124  
 125              if ($this->_db->loadRowList()) {
 126                  $this->setError(
 127                      Text::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED', \get_class($this), Text::_('JLIB_DATABASE_ERROR_MENUTYPE_CHECKOUT'))
 128                  );
 129  
 130                  return false;
 131              }
 132  
 133              // Verify that no module for this menu are checked out
 134              $searchParams = '%"menutype":' . json_encode($table->menutype) . '%';
 135              $query->clear()
 136                  ->select($this->_db->quoteName('id'))
 137                  ->from($this->_db->quoteName('#__modules'))
 138                  ->where($this->_db->quoteName('module') . ' = ' . $this->_db->quote('mod_menu'))
 139                  ->where($this->_db->quoteName('params') . ' LIKE :params')
 140                  ->whereNotIn($this->_db->quoteName('checked_out'), $notIn)
 141                  ->bind(':params', $searchParams);
 142              $this->_db->setQuery($query);
 143  
 144              if ($this->_db->loadRowList()) {
 145                  $this->setError(
 146                      Text::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED', \get_class($this), Text::_('JLIB_DATABASE_ERROR_MENUTYPE_CHECKOUT'))
 147                  );
 148  
 149                  return false;
 150              }
 151  
 152              // Update the menu items
 153              $query->clear()
 154                  ->update($this->_db->quoteName('#__menu'))
 155                  ->set($this->_db->quoteName('menutype') . ' = :setmenutype')
 156                  ->where($this->_db->quoteName('menutype') . ' = :menutype')
 157                  ->bind(':setmenutype', $this->menutype)
 158                  ->bind(':menutype', $table->menutype);
 159              $this->_db->setQuery($query);
 160              $this->_db->execute();
 161  
 162              // Update the module items
 163              $whereParams   = '%"menutype":' . json_encode($table->menutype) . '%';
 164              $searchParams  = '"menutype":' . json_encode($table->menutype);
 165              $replaceParams = '"menutype":' . json_encode($this->menutype);
 166              $query->clear()
 167                  ->update($this->_db->quoteName('#__modules'))
 168                  ->set(
 169                      $this->_db->quoteName('params') . ' = REPLACE(' . $this->_db->quoteName('params') . ', :search, :value)'
 170                  );
 171              $query->where($this->_db->quoteName('module') . ' = ' . $this->_db->quote('mod_menu'))
 172                  ->where($this->_db->quoteName('params') . ' LIKE :whereparams')
 173                  ->bind(':search', $searchParams)
 174                  ->bind(':value', $replaceParams)
 175                  ->bind(':whereparams', $whereParams);
 176              $this->_db->setQuery($query);
 177              $this->_db->execute();
 178          }
 179  
 180          return parent::store($updateNulls);
 181      }
 182  
 183      /**
 184       * Method to delete a row from the database table by primary key value.
 185       *
 186       * @param   mixed  $pk  An optional primary key value to delete.  If not set the instance property value is used.
 187       *
 188       * @return  boolean  True on success.
 189       *
 190       * @since   1.6
 191       */
 192      public function delete($pk = null)
 193      {
 194          $k = $this->_tbl_key;
 195          $pk = $pk === null ? $this->$k : $pk;
 196  
 197          // If no primary key is given, return false.
 198          if ($pk !== null) {
 199              // Get the user id
 200              $userId = (int) Factory::getUser()->id;
 201              $notIn  = [0, $userId];
 202              $star   = '*';
 203  
 204              // Get the old value of the table
 205              $table = Table::getInstance('Menutype', 'JTable', array('dbo' => $this->getDbo()));
 206              $table->load($pk);
 207  
 208              // Verify that no items are checked out
 209              $query = $this->_db->getQuery(true)
 210                  ->select($this->_db->quoteName('id'))
 211                  ->from($this->_db->quoteName('#__menu'))
 212                  ->where($this->_db->quoteName('menutype') . ' = :menutype')
 213                  ->where('(' .
 214                      $this->_db->quoteName('checked_out') . ' NOT IN (NULL, :id)' .
 215                      ' OR ' . $this->_db->quoteName('home') . ' = 1' .
 216                      ' AND ' . $this->_db->quoteName('language') . ' = :star' .
 217                      ')')
 218                  ->bind(':menutype', $table->menutype)
 219                  ->bind(':id', $userId, ParameterType::INTEGER)
 220                  ->bind(':star', $star);
 221              $this->_db->setQuery($query);
 222  
 223              if ($this->_db->loadRowList()) {
 224                  $this->setError(Text::sprintf('JLIB_DATABASE_ERROR_DELETE_FAILED', \get_class($this), Text::_('JLIB_DATABASE_ERROR_MENUTYPE')));
 225  
 226                  return false;
 227              }
 228  
 229              // Verify that no module for this menu are checked out
 230              $searchParams = '%"menutype":' . json_encode($table->menutype) . '%';
 231              $query->clear()
 232                  ->select($this->_db->quoteName('id'))
 233                  ->from($this->_db->quoteName('#__modules'))
 234                  ->where($this->_db->quoteName('module') . ' = ' . $this->_db->quote('mod_menu'))
 235                  ->where($this->_db->quoteName('params') . ' LIKE :menutype')
 236                  ->whereNotIn($this->_db->quoteName('checked_out'), $notIn)
 237                  ->bind(':menutype', $searchParams);
 238              $this->_db->setQuery($query);
 239  
 240              if ($this->_db->loadRowList()) {
 241                  $this->setError(Text::sprintf('JLIB_DATABASE_ERROR_DELETE_FAILED', \get_class($this), Text::_('JLIB_DATABASE_ERROR_MENUTYPE')));
 242  
 243                  return false;
 244              }
 245  
 246              // Delete the menu items
 247              $query->clear()
 248                  ->delete('#__menu')
 249                  ->where('menutype=' . $this->_db->quote($table->menutype));
 250              $this->_db->setQuery($query);
 251              $this->_db->execute();
 252  
 253              // Update the module items
 254              $query->clear()
 255                  ->delete('#__modules')
 256                  ->where('module=' . $this->_db->quote('mod_menu'))
 257                  ->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'));
 258              $this->_db->setQuery($query);
 259              $this->_db->execute();
 260          }
 261  
 262          return parent::delete($pk);
 263      }
 264  
 265      /**
 266       * Method to compute the default name of the asset.
 267       * The default name is in the form table_name.id
 268       * where id is the value of the primary key of the table.
 269       *
 270       * @return  string
 271       *
 272       * @since   3.6
 273       */
 274      protected function _getAssetName()
 275      {
 276          return 'com_menus.menu.' . $this->id;
 277      }
 278  
 279      /**
 280       * Method to return the title to use for the asset table.
 281       *
 282       * @return  string
 283       *
 284       * @since   3.6
 285       */
 286      protected function _getAssetTitle()
 287      {
 288          return $this->title;
 289      }
 290  
 291      /**
 292       * Method to get the parent asset under which to register this one.
 293       * By default, all assets are registered to the ROOT node with ID,
 294       * which will default to 1 if none exists.
 295       * The extended class can define a table and id to lookup.  If the
 296       * asset does not exist it will be created.
 297       *
 298       * @param   Table    $table  A Table object for the asset parent.
 299       * @param   integer  $id     Id to look up
 300       *
 301       * @return  integer
 302       *
 303       * @since   3.6
 304       */
 305      protected function _getAssetParentId(Table $table = null, $id = null)
 306      {
 307          $assetId = null;
 308          $asset = Table::getInstance('asset');
 309  
 310          if ($asset->loadByName('com_menus')) {
 311              $assetId = $asset->id;
 312          }
 313  
 314          return $assetId === null ? parent::_getAssetParentId($table, $id) : $assetId;
 315      }
 316  }


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