[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_redirect/src/Controller/ -> LinksController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_redirect
   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\Redirect\Administrator\Controller;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\MVC\Controller\AdminController;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('_JEXEC') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Redirect link list controller class.
  23   *
  24   * @since  1.6
  25   */
  26  class LinksController extends AdminController
  27  {
  28      /**
  29       * Method to update a record.
  30       *
  31       * @return  void
  32       *
  33       * @since   1.6
  34       */
  35      public function activate()
  36      {
  37          // Check for request forgeries.
  38          $this->checkToken();
  39  
  40          $ids = (array) $this->input->get('cid', array(), 'int');
  41  
  42          // Remove zero values resulting from input filter
  43          $ids = array_filter($ids);
  44  
  45          if (empty($ids)) {
  46              $this->app->enqueueMessage(Text::_('COM_REDIRECT_NO_ITEM_SELECTED'), 'warning');
  47          } else {
  48              $newUrl  = $this->input->getString('new_url');
  49              $comment = $this->input->getString('comment');
  50  
  51              // Get the model.
  52              $model = $this->getModel();
  53  
  54              // Remove the items.
  55              if (!$model->activate($ids, $newUrl, $comment)) {
  56                  $this->app->enqueueMessage($model->getError(), 'warning');
  57              } else {
  58                  $this->setMessage(Text::plural('COM_REDIRECT_N_LINKS_UPDATED', count($ids)));
  59              }
  60          }
  61  
  62          $this->setRedirect('index.php?option=com_redirect&view=links');
  63      }
  64  
  65      /**
  66       * Method to duplicate URLs in records.
  67       *
  68       * @return  void
  69       *
  70       * @since   3.6.0
  71       */
  72      public function duplicateUrls()
  73      {
  74          // Check for request forgeries.
  75          $this->checkToken();
  76  
  77          $ids = (array) $this->input->get('cid', array(), 'int');
  78  
  79          // Remove zero values resulting from input filter
  80          $ids = array_filter($ids);
  81  
  82          if (empty($ids)) {
  83              $this->app->enqueueMessage(Text::_('COM_REDIRECT_NO_ITEM_SELECTED'), 'warning');
  84          } else {
  85              $newUrl  = $this->input->getString('new_url');
  86              $comment = $this->input->getString('comment');
  87  
  88              // Get the model.
  89              $model = $this->getModel();
  90  
  91              // Remove the items.
  92              if (!$model->duplicateUrls($ids, $newUrl, $comment)) {
  93                  $this->app->enqueueMessage($model->getError(), 'warning');
  94              } else {
  95                  $this->setMessage(Text::plural('COM_REDIRECT_N_LINKS_UPDATED', count($ids)));
  96              }
  97          }
  98  
  99          $this->setRedirect('index.php?option=com_redirect&view=links');
 100      }
 101  
 102      /**
 103       * Proxy for getModel.
 104       *
 105       * @param   string  $name    The name of the model.
 106       * @param   string  $prefix  The prefix of the model.
 107       * @param   array   $config  An array of settings.
 108       *
 109       * @return  \Joomla\CMS\MVC\Model\BaseDatabaseModel The model instance
 110       *
 111       * @since   1.6
 112       */
 113      public function getModel($name = 'Link', $prefix = 'Administrator', $config = array('ignore_request' => true))
 114      {
 115          return parent::getModel($name, $prefix, $config);
 116      }
 117  
 118      /**
 119       * Executes the batch process to add URLs to the database
 120       *
 121       * @return  void
 122       */
 123      public function batch()
 124      {
 125          // Check for request forgeries.
 126          $this->checkToken();
 127  
 128          $batch_urls_request = $this->input->post->get('batch_urls', array(), 'array');
 129          $batch_urls_lines   = array_map('trim', explode("\n", $batch_urls_request[0]));
 130  
 131          $batch_urls = array();
 132  
 133          foreach ($batch_urls_lines as $batch_urls_line) {
 134              if (!empty($batch_urls_line)) {
 135                  $params = ComponentHelper::getParams('com_redirect');
 136                  $separator = $params->get('separator', '|');
 137  
 138                  // Basic check to make sure the correct separator is being used
 139                  if (!\Joomla\String\StringHelper::strpos($batch_urls_line, $separator)) {
 140                      $this->setMessage(Text::sprintf('COM_REDIRECT_NO_SEPARATOR_FOUND', $separator), 'error');
 141                      $this->setRedirect('index.php?option=com_redirect&view=links');
 142  
 143                      return;
 144                  }
 145  
 146                  $batch_urls[] = array_map('trim', explode($separator, $batch_urls_line));
 147              }
 148          }
 149  
 150          // Set default message on error - overwrite if successful
 151          $this->setMessage(Text::_('COM_REDIRECT_NO_ITEM_ADDED'), 'error');
 152  
 153          if (!empty($batch_urls)) {
 154              $model = $this->getModel('Links');
 155  
 156              // Execute the batch process
 157              if ($model->batchProcess($batch_urls)) {
 158                  $this->setMessage(Text::plural('COM_REDIRECT_N_LINKS_ADDED', count($batch_urls)));
 159              }
 160          }
 161  
 162          $this->setRedirect('index.php?option=com_redirect&view=links');
 163      }
 164  
 165      /**
 166       * Clean out the unpublished links.
 167       *
 168       * @return  void
 169       *
 170       * @since   3.5
 171       */
 172      public function purge()
 173      {
 174          // Check for request forgeries.
 175          $this->checkToken();
 176  
 177          $model = $this->getModel('Links');
 178  
 179          if ($model->purge()) {
 180              $message = Text::_('COM_REDIRECT_CLEAR_SUCCESS');
 181          } else {
 182              $message = Text::_('COM_REDIRECT_CLEAR_FAIL');
 183          }
 184  
 185          $this->setRedirect('index.php?option=com_redirect&view=links', $message);
 186      }
 187  }


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