[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_categories/src/Controller/ -> AjaxController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_categories
   6   *
   7   * @copyright   (C) 2005 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\Categories\Administrator\Controller;
  12  
  13  use Joomla\CMS\Language\Associations;
  14  use Joomla\CMS\Language\LanguageHelper;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\MVC\Controller\BaseController;
  17  use Joomla\CMS\Response\JsonResponse;
  18  use Joomla\CMS\Session\Session;
  19  use Joomla\CMS\Table\Table;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('_JEXEC') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * The categories controller for ajax requests
  27   *
  28   * @since  3.9.0
  29   */
  30  class AjaxController extends BaseController
  31  {
  32      /**
  33       * Method to fetch associations of a category
  34       *
  35       * The method assumes that the following http parameters are passed in an Ajax Get request:
  36       * token: the form token
  37       * assocId: the id of the category whose associations are to be returned
  38       * excludeLang: the association for this language is to be excluded
  39       *
  40       * @return  void
  41       *
  42       * @since  3.9.0
  43       */
  44      public function fetchAssociations()
  45      {
  46          if (!Session::checkToken('get')) {
  47              echo new JsonResponse(null, Text::_('JINVALID_TOKEN'), true);
  48          } else {
  49              $extension = $this->input->get('extension');
  50  
  51              $assocId   = $this->input->getInt('assocId', 0);
  52  
  53              if ($assocId == 0) {
  54                  echo new JsonResponse(null, Text::sprintf('JLIB_FORM_VALIDATE_FIELD_INVALID', 'assocId'), true);
  55  
  56                  return;
  57              }
  58  
  59              $excludeLang = $this->input->get('excludeLang', '', 'STRING');
  60  
  61              $associations = Associations::getAssociations($extension, '#__categories', 'com_categories.item', (int) $assocId, 'id', 'alias', '');
  62  
  63              unset($associations[$excludeLang]);
  64  
  65              // Add the title to each of the associated records
  66              Table::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_categories/tables');
  67              $categoryTable = Table::getInstance('Category', 'JTable');
  68  
  69              foreach ($associations as $lang => $association) {
  70                  $categoryTable->load($association->id);
  71                  $associations[$lang]->title = $categoryTable->title;
  72              }
  73  
  74              $countContentLanguages = \count(LanguageHelper::getContentLanguages(array(0, 1), false));
  75  
  76              if (\count($associations) == 0) {
  77                  $message = Text::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_MESSAGE_NONE');
  78              } elseif ($countContentLanguages > \count($associations) + 2) {
  79                  $tags    = implode(', ', array_keys($associations));
  80                  $message = Text::sprintf('JGLOBAL_ASSOCIATIONS_PROPAGATE_MESSAGE_SOME', $tags);
  81              } else {
  82                  $message = Text::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_MESSAGE_ALL');
  83              }
  84  
  85              echo new JsonResponse($associations, $message);
  86          }
  87      }
  88  }


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