[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_menus/src/Controller/ -> MenuController.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\ApplicationHelper;
  14  use Joomla\CMS\Filter\InputFilter;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\MVC\Controller\FormController;
  17  use Joomla\CMS\Router\Route;
  18  use Joomla\Component\Menus\Administrator\Helper\MenusHelper;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('_JEXEC') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * The Menu Type Controller
  26   *
  27   * @since  1.6
  28   */
  29  class MenuController extends FormController
  30  {
  31      /**
  32       * Dummy method to redirect back to standard controller
  33       *
  34       * @param   boolean  $cachable   If true, the view output will be cached.
  35       * @param   array    $urlparams  An array of safe URL parameters and their variable types, for valid values see {@link \JFilterInput::clean()}.
  36       *
  37       * @return  void
  38       *
  39       * @since   1.5
  40       */
  41      public function display($cachable = false, $urlparams = false)
  42      {
  43          $this->setRedirect(Route::_('index.php?option=com_menus&view=menus', false));
  44      }
  45  
  46      /**
  47       * Method to save a menu item.
  48       *
  49       * @param   string  $key     The name of the primary key of the URL variable.
  50       * @param   string  $urlVar  The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
  51       *
  52       * @return  boolean  True if successful, false otherwise.
  53       *
  54       * @since   1.6
  55       */
  56      public function save($key = null, $urlVar = null)
  57      {
  58          // Check for request forgeries.
  59          $this->checkToken();
  60  
  61          $app      = $this->app;
  62          $data     = $this->input->post->get('jform', array(), 'array');
  63          $context  = 'com_menus.edit.menu';
  64          $task     = $this->getTask();
  65          $recordId = $this->input->getInt('id');
  66  
  67          // Prevent using 'main' as menutype as this is reserved for backend menus
  68          if (strtolower($data['menutype']) == 'main') {
  69              $this->setMessage(Text::_('COM_MENUS_ERROR_MENUTYPE'), 'error');
  70  
  71              // Redirect back to the edit screen.
  72              $this->setRedirect(Route::_('index.php?option=com_menus&view=menu&layout=edit' . $this->getRedirectToItemAppend($recordId), false));
  73  
  74              return false;
  75          }
  76  
  77          $data['menutype'] = InputFilter::getInstance()->clean($data['menutype'], 'TRIM');
  78  
  79          // Populate the row id from the session.
  80          $data['id'] = $recordId;
  81  
  82          // Get the model and attempt to validate the posted data.
  83          /** @var \Joomla\Component\Menus\Administrator\Model\MenuModel $model */
  84          $model = $this->getModel('Menu', '', ['ignore_request' => false]);
  85          $form  = $model->getForm();
  86  
  87          if (!$form) {
  88              throw new \Exception($model->getError(), 500);
  89          }
  90  
  91          $validData = $model->validate($form, $data);
  92  
  93          // Check for validation errors.
  94          if ($validData === false) {
  95              // Get the validation messages.
  96              $errors = $model->getErrors();
  97  
  98              // Push up to three validation messages out to the user.
  99              for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) {
 100                  if ($errors[$i] instanceof \Exception) {
 101                      $app->enqueueMessage($errors[$i]->getMessage(), 'warning');
 102                  } else {
 103                      $app->enqueueMessage($errors[$i], 'warning');
 104                  }
 105              }
 106  
 107              // Save the data in the session.
 108              $app->setUserState($context . '.data', $data);
 109  
 110              // Redirect back to the edit screen.
 111              $this->setRedirect(Route::_('index.php?option=com_menus&view=menu&layout=edit' . $this->getRedirectToItemAppend($recordId), false));
 112  
 113              return false;
 114          }
 115  
 116          if (isset($validData['preset'])) {
 117              $preset = trim($validData['preset']) ?: null;
 118  
 119              unset($validData['preset']);
 120          }
 121  
 122          // Attempt to save the data.
 123          if (!$model->save($validData)) {
 124              // Save the data in the session.
 125              $app->setUserState($context . '.data', $validData);
 126  
 127              // Redirect back to the edit screen.
 128              $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()), 'error');
 129              $this->setRedirect(Route::_('index.php?option=com_menus&view=menu&layout=edit' . $this->getRedirectToItemAppend($recordId), false));
 130  
 131              return false;
 132          }
 133  
 134          // Import the preset selected
 135          if (isset($preset) && $data['client_id'] == 1) {
 136              // Menu Type has not been saved yet. Make sure items get the real menutype.
 137              $menutype = ApplicationHelper::stringURLSafe($data['menutype']);
 138  
 139              try {
 140                  MenusHelper::installPreset($preset, $menutype);
 141  
 142                  $this->setMessage(Text::_('COM_MENUS_PRESET_IMPORT_SUCCESS'));
 143              } catch (\Exception $e) {
 144                  // Save was successful but the preset could not be loaded. Let it through with just a warning
 145                  $this->setMessage(Text::sprintf('COM_MENUS_PRESET_IMPORT_FAILED', $e->getMessage()));
 146              }
 147          } else {
 148              $this->setMessage(Text::_('COM_MENUS_MENU_SAVE_SUCCESS'));
 149          }
 150  
 151          // Redirect the user and adjust session state based on the chosen task.
 152          switch ($task) {
 153              case 'apply':
 154                  // Set the record data in the session.
 155                  $recordId = $model->getState($this->context . '.id');
 156                  $this->holdEditId($context, $recordId);
 157                  $app->setUserState($context . '.data', null);
 158  
 159                  // Redirect back to the edit screen.
 160                  $this->setRedirect(Route::_('index.php?option=com_menus&view=menu&layout=edit' . $this->getRedirectToItemAppend($recordId), false));
 161                  break;
 162  
 163              case 'save2new':
 164                  // Clear the record id and data from the session.
 165                  $this->releaseEditId($context, $recordId);
 166                  $app->setUserState($context . '.data', null);
 167  
 168                  // Redirect back to the edit screen.
 169                  $this->setRedirect(Route::_('index.php?option=com_menus&view=menu&layout=edit', false));
 170                  break;
 171  
 172              default:
 173                  // Clear the record id and data from the session.
 174                  $this->releaseEditId($context, $recordId);
 175                  $app->setUserState($context . '.data', null);
 176  
 177                  // Redirect to the list screen.
 178                  $this->setRedirect(Route::_('index.php?option=com_menus&view=menus', false));
 179                  break;
 180          }
 181      }
 182  
 183      /**
 184       * Method to display a menu as preset xml.
 185       *
 186       * @return  boolean  True if successful, false otherwise.
 187       *
 188       * @since   3.8.0
 189       */
 190      public function exportXml()
 191      {
 192          // Check for request forgeries.
 193          $this->checkToken();
 194  
 195          $cid = (array) $this->input->get('cid', array(), 'int');
 196  
 197          // We know the first element is the one we need because we don't allow multi selection of rows
 198          $id = empty($cid) ? 0 : reset($cid);
 199  
 200          if ($id === 0) {
 201              $this->setMessage(Text::_('COM_MENUS_SELECT_MENU_FIRST_EXPORT'), 'warning');
 202  
 203              $this->setRedirect(Route::_('index.php?option=com_menus&view=menus', false));
 204  
 205              return false;
 206          }
 207  
 208          $model = $this->getModel('Menu');
 209          $item  = $model->getItem($id);
 210  
 211          if (!$item->menutype) {
 212              $this->setMessage(Text::_('COM_MENUS_SELECT_MENU_FIRST_EXPORT'), 'warning');
 213  
 214              $this->setRedirect(Route::_('index.php?option=com_menus&view=menus', false));
 215  
 216              return false;
 217          }
 218  
 219          $this->setRedirect(Route::_('index.php?option=com_menus&view=menu&menutype=' . $item->menutype . '&format=xml', false));
 220  
 221          return true;
 222      }
 223  }


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