[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_menus/src/Controller/ -> MenusController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_menus
   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\Menus\Administrator\Controller;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\MVC\Controller\BaseController;
  16  use Joomla\Database\ParameterType;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('_JEXEC') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * The Menu List Controller
  24   *
  25   * @since  1.6
  26   */
  27  class MenusController extends BaseController
  28  {
  29      /**
  30       * Display the view
  31       *
  32       * @param   boolean  $cachable   If true, the view output will be cached.
  33       * @param   array    $urlparams  An array of safe URL parameters and their variable types, for valid values see {@link \JFilterInput::clean()}.
  34       *
  35       * @return  void
  36       *
  37       * @since   1.6
  38       */
  39      public function display($cachable = false, $urlparams = false)
  40      {
  41      }
  42  
  43      /**
  44       * Method to get a model object, loading it if required.
  45       *
  46       * @param   string  $name    The model name. Optional.
  47       * @param   string  $prefix  The class prefix. Optional.
  48       * @param   array   $config  Configuration array for model. Optional.
  49       *
  50       * @return  object  The model.
  51       *
  52       * @since   1.6
  53       */
  54      public function getModel($name = 'Menu', $prefix = 'Administrator', $config = array('ignore_request' => true))
  55      {
  56          return parent::getModel($name, $prefix, $config);
  57      }
  58  
  59      /**
  60       * Remove an item.
  61       *
  62       * @return  void
  63       *
  64       * @since   1.6
  65       */
  66      public function delete()
  67      {
  68          // Check for request forgeries
  69          $this->checkToken();
  70  
  71          $user = $this->app->getIdentity();
  72          $cids = (array) $this->input->get('cid', array(), 'int');
  73  
  74          // Remove zero values resulting from input filter
  75          $cids = array_filter($cids);
  76  
  77          if (empty($cids)) {
  78              $this->setMessage(Text::_('COM_MENUS_NO_MENUS_SELECTED'), 'warning');
  79          } else {
  80              // Access checks.
  81              foreach ($cids as $i => $id) {
  82                  if (!$user->authorise('core.delete', 'com_menus.menu.' . (int) $id)) {
  83                      // Prune items that you can't change.
  84                      unset($cids[$i]);
  85                      $this->app->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED'), 'error');
  86                  }
  87              }
  88  
  89              if (count($cids) > 0) {
  90                  // Get the model.
  91                  /** @var \Joomla\Component\Menus\Administrator\Model\MenuModel $model */
  92                  $model = $this->getModel();
  93  
  94                  // Remove the items.
  95                  if (!$model->delete($cids)) {
  96                      $this->setMessage($model->getError(), 'error');
  97                  } else {
  98                      $this->setMessage(Text::plural('COM_MENUS_N_MENUS_DELETED', count($cids)));
  99                  }
 100              }
 101          }
 102  
 103          $this->setRedirect('index.php?option=com_menus&view=menus');
 104      }
 105  
 106      /**
 107       * Temporary method. This should go into the 1.5 to 1.6 upgrade routines.
 108       *
 109       * @return  void
 110       *
 111       * @since   1.6
 112       *
 113       * @deprecated  5.0 Will be removed without replacement as it was only used for the 1.5 to 1.6 upgrade
 114       */
 115      public function resync()
 116      {
 117          $db = Factory::getDbo();
 118          $query = $db->getQuery(true);
 119          $parts = null;
 120  
 121          try {
 122              $query->select(
 123                  [
 124                      $db->quoteName('element'),
 125                      $db->quoteName('extension_id'),
 126                  ]
 127              )
 128                  ->from($db->quoteName('#__extensions'))
 129                  ->where($db->quoteName('type') . ' = ' . $db->quote('component'));
 130              $db->setQuery($query);
 131  
 132              $components = $db->loadAssocList('element', 'extension_id');
 133          } catch (\RuntimeException $e) {
 134              $this->setMessage($e->getMessage(), 'warning');
 135  
 136              return;
 137          }
 138  
 139          // Load all the component menu links
 140          $query->select(
 141              [
 142                  $db->quoteName('id'),
 143                  $db->quoteName('link'),
 144                  $db->quoteName('component_id'),
 145              ]
 146          )
 147              ->from($db->quoteName('#__menu'))
 148              ->where($db->quoteName('type') . ' = ' . $db->quote('component.item'));
 149              $db->setQuery($query);
 150  
 151          try {
 152              $items = $db->loadObjectList();
 153          } catch (\RuntimeException $e) {
 154              $this->setMessage($e->getMessage(), 'warning');
 155  
 156              return;
 157          }
 158  
 159          $query = $db->getQuery(true)
 160              ->update($db->quoteName('#__menu'))
 161              ->set($db->quoteName('component_id') . ' = :componentId')
 162              ->where($db->quoteName('id') . ' = :itemId')
 163              ->bind(':componentId', $componentId, ParameterType::INTEGER)
 164              ->bind(':itemId', $itemId, ParameterType::INTEGER);
 165  
 166          foreach ($items as $item) {
 167              // Parse the link.
 168              parse_str(parse_url($item->link, PHP_URL_QUERY), $parts);
 169              $itemId = $item->id;
 170  
 171              // Tease out the option.
 172              if (isset($parts['option'])) {
 173                  $option = $parts['option'];
 174  
 175                  // Lookup the component ID
 176                  if (isset($components[$option])) {
 177                      $componentId = $components[$option];
 178                  } else {
 179                      // Mismatch. Needs human intervention.
 180                      $componentId = -1;
 181                  }
 182  
 183                  // Check for mis-matched component ids in the menu link.
 184                  if ($item->component_id != $componentId) {
 185                      // Update the menu table.
 186                      $log = "Link $item->id refers to $item->component_id, converting to $componentId ($item->link)";
 187                      echo "<br>$log";
 188  
 189                      try {
 190                          $db->setQuery($query)->execute();
 191                      } catch (\RuntimeException $e) {
 192                          $this->setMessage($e->getMessage(), 'warning');
 193  
 194                          return;
 195                      }
 196                  }
 197              }
 198          }
 199      }
 200  }


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