[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_menus/src/Controller/ -> ItemsController.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\Application\CMSApplication;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\Log\Log;
  16  use Joomla\CMS\MVC\Controller\AdminController;
  17  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  18  use Joomla\CMS\Response\JsonResponse;
  19  use Joomla\CMS\Router\Route;
  20  use Joomla\Input\Input;
  21  use Joomla\Utilities\ArrayHelper;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('_JEXEC') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * The Menu Item Controller
  29   *
  30   * @since  1.6
  31   */
  32  class ItemsController extends AdminController
  33  {
  34      /**
  35       * Constructor.
  36       *
  37       * @param   array                $config   An optional associative array of configuration settings.
  38       * @param   MVCFactoryInterface  $factory  The factory.
  39       * @param   CMSApplication       $app      The Application for the dispatcher
  40       * @param   Input                $input    Input
  41       *
  42       * @since  1.6
  43       */
  44      public function __construct($config = array(), MVCFactoryInterface $factory = null, $app = null, $input = null)
  45      {
  46          parent::__construct($config, $factory, $app, $input);
  47  
  48          $this->registerTask('unsetDefault', 'setDefault');
  49      }
  50  
  51      /**
  52       * Proxy for getModel.
  53       *
  54       * @param   string  $name    The model name. Optional.
  55       * @param   string  $prefix  The class prefix. Optional.
  56       * @param   array   $config  Configuration array for model. Optional.
  57       *
  58       * @return  object  The model.
  59       *
  60       * @since   1.6
  61       */
  62      public function getModel($name = 'Item', $prefix = 'Administrator', $config = array('ignore_request' => true))
  63      {
  64          return parent::getModel($name, $prefix, $config);
  65      }
  66  
  67      /**
  68       * Method to get the number of published frontend menu items for quickicons
  69       *
  70       * @return  void
  71       *
  72       * @since   4.0.0
  73       */
  74      public function getQuickiconContent()
  75      {
  76          $model = $this->getModel('Items');
  77  
  78          $model->setState('filter.published', 1);
  79          $model->setState('filter.client_id', 0);
  80  
  81          $amount = (int) $model->getTotal();
  82  
  83          $result = [];
  84  
  85          $result['amount'] = $amount;
  86          $result['sronly'] = Text::plural('COM_MENUS_ITEMS_N_QUICKICON_SRONLY', $amount);
  87          $result['name'] = Text::plural('COM_MENUS_ITEMS_N_QUICKICON', $amount);
  88  
  89          echo new JsonResponse($result);
  90      }
  91  
  92      /**
  93       * Rebuild the nested set tree.
  94       *
  95       * @return  boolean  False on failure or error, true on success.
  96       *
  97       * @since   1.6
  98       */
  99      public function rebuild()
 100      {
 101          $this->checkToken();
 102  
 103          $this->setRedirect('index.php?option=com_menus&view=items&menutype=' . $this->input->getCmd('menutype'));
 104  
 105          /** @var \Joomla\Component\Menus\Administrator\Model\ItemModel $model */
 106          $model = $this->getModel();
 107  
 108          if ($model->rebuild()) {
 109              // Reorder succeeded.
 110              $this->setMessage(Text::_('COM_MENUS_ITEMS_REBUILD_SUCCESS'));
 111  
 112              return true;
 113          } else {
 114              // Rebuild failed.
 115              $this->setMessage(Text::sprintf('COM_MENUS_ITEMS_REBUILD_FAILED'), 'error');
 116  
 117              return false;
 118          }
 119      }
 120  
 121      /**
 122       * Method to set the home property for a list of items
 123       *
 124       * @return  void
 125       *
 126       * @since   1.6
 127       */
 128      public function setDefault()
 129      {
 130          // Check for request forgeries
 131          $this->checkToken('request');
 132  
 133          $app = $this->app;
 134  
 135          // Get items to publish from the request.
 136          $cid   = (array) $this->input->get('cid', array(), 'int');
 137          $data  = array('setDefault' => 1, 'unsetDefault' => 0);
 138          $task  = $this->getTask();
 139          $value = ArrayHelper::getValue($data, $task, 0, 'int');
 140  
 141          // Remove zero values resulting from input filter
 142          $cid = array_filter($cid);
 143  
 144          if (empty($cid)) {
 145              $this->setMessage(Text::_($this->text_prefix . '_NO_ITEM_SELECTED'), 'warning');
 146          } else {
 147              // Get the model.
 148              $model = $this->getModel();
 149  
 150              // Publish the items.
 151              if (!$model->setHome($cid, $value)) {
 152                  $this->setMessage($model->getError(), 'warning');
 153              } else {
 154                  if ($value == 1) {
 155                      $ntext = 'COM_MENUS_ITEMS_SET_HOME';
 156                  } else {
 157                      $ntext = 'COM_MENUS_ITEMS_UNSET_HOME';
 158                  }
 159  
 160                  $this->setMessage(Text::plural($ntext, count($cid)));
 161              }
 162          }
 163  
 164          $this->setRedirect(
 165              Route::_(
 166                  'index.php?option=' . $this->option . '&view=' . $this->view_list
 167                  . '&menutype=' . $app->getUserState('com_menus.items.menutype'),
 168                  false
 169              )
 170          );
 171      }
 172  
 173      /**
 174       * Method to publish a list of items
 175       *
 176       * @return  void
 177       *
 178       * @since   3.6.0
 179       */
 180      public function publish()
 181      {
 182          // Check for request forgeries
 183          $this->checkToken();
 184  
 185          // Get items to publish from the request.
 186          $cid = (array) $this->input->get('cid', array(), 'int');
 187          $data = array('publish' => 1, 'unpublish' => 0, 'trash' => -2, 'report' => -3);
 188          $task = $this->getTask();
 189          $value = ArrayHelper::getValue($data, $task, 0, 'int');
 190  
 191          // Remove zero values resulting from input filter
 192          $cid = array_filter($cid);
 193  
 194          if (empty($cid)) {
 195              try {
 196                  Log::add(Text::_($this->text_prefix . '_NO_ITEM_SELECTED'), Log::WARNING, 'jerror');
 197              } catch (\RuntimeException $exception) {
 198                  $this->setMessage(Text::_($this->text_prefix . '_NO_ITEM_SELECTED'), 'warning');
 199              }
 200          } else {
 201              // Get the model.
 202              $model = $this->getModel();
 203  
 204              // Publish the items.
 205              try {
 206                  $model->publish($cid, $value);
 207                  $errors      = $model->getErrors();
 208                  $messageType = 'message';
 209  
 210                  if ($value == 1) {
 211                      if ($errors) {
 212                          $messageType = 'error';
 213                          $ntext       = $this->text_prefix . '_N_ITEMS_FAILED_PUBLISHING';
 214                      } else {
 215                          $ntext = $this->text_prefix . '_N_ITEMS_PUBLISHED';
 216                      }
 217                  } elseif ($value == 0) {
 218                      $ntext = $this->text_prefix . '_N_ITEMS_UNPUBLISHED';
 219                  } else {
 220                      $ntext = $this->text_prefix . '_N_ITEMS_TRASHED';
 221                  }
 222  
 223                  $this->setMessage(Text::plural($ntext, count($cid)), $messageType);
 224              } catch (\Exception $e) {
 225                  $this->setMessage($e->getMessage(), 'error');
 226              }
 227          }
 228  
 229          $this->setRedirect(
 230              Route::_(
 231                  'index.php?option=' . $this->option . '&view=' . $this->view_list . '&menutype=' .
 232                  $this->app->getUserState('com_menus.items.menutype'),
 233                  false
 234              )
 235          );
 236      }
 237  
 238      /**
 239       * Gets the URL arguments to append to a list redirect.
 240       *
 241       * @return  string  The arguments to append to the redirect URL.
 242       *
 243       * @since   4.0.0
 244       */
 245      protected function getRedirectToListAppend()
 246      {
 247          return '&menutype=' . $this->app->getUserState('com_menus.items.menutype');
 248      }
 249  }


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