[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Language/ -> Associations.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2013 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Language;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\Plugin\PluginHelper;
  14  use Joomla\Database\ParameterType;
  15  use Joomla\Registry\Registry;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('JPATH_PLATFORM') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Utility class for associations in multilang
  23   *
  24   * @since  3.1
  25   */
  26  class Associations
  27  {
  28      /**
  29       * Get the associations.
  30       *
  31       * @param   string   $extension   The name of the component.
  32       * @param   string   $tablename   The name of the table.
  33       * @param   string   $context     The context
  34       * @param   integer  $id          The primary key value.
  35       * @param   string   $pk          The name of the primary key in the given $table.
  36       * @param   string   $aliasField  If the table has an alias field set it here. Null to not use it
  37       * @param   string   $catField    If the table has a catid field set it here. Null to not use it
  38       * @param   array    $advClause   Additional advanced 'where' clause; use c as parent column key, c2 as associations column key
  39       *
  40       * @return  array  The associated items
  41       *
  42       * @since   3.1
  43       *
  44       * @throws  \Exception
  45       */
  46      public static function getAssociations(
  47          $extension,
  48          $tablename,
  49          $context,
  50          $id,
  51          $pk = 'id',
  52          $aliasField = 'alias',
  53          $catField = 'catid',
  54          $advClause = array()
  55      ) {
  56          // To avoid doing duplicate database queries.
  57          static $multilanguageAssociations = array();
  58  
  59          // Cast before creating cache key.
  60          $id = (int) $id;
  61  
  62          // Multilanguage association array key. If the key is already in the array we don't need to run the query again, just return it.
  63          $queryKey = md5(serialize(array_merge(array($extension, $tablename, $context, $id), $advClause)));
  64  
  65          if (!isset($multilanguageAssociations[$queryKey])) {
  66              $multilanguageAssociations[$queryKey] = array();
  67  
  68              $db                 = Factory::getDbo();
  69              $query              = $db->getQuery(true);
  70              $categoriesExtraSql = '';
  71  
  72              if ($tablename === '#__categories') {
  73                  $categoriesExtraSql = ' AND c2.extension = :extension1';
  74                  $query->bind(':extension1', $extension);
  75              }
  76  
  77              $query->select($db->quoteName('c2.language'))
  78                  ->from($db->quoteName($tablename, 'c'))
  79                  ->join(
  80                      'INNER',
  81                      $db->quoteName('#__associations', 'a'),
  82                      $db->quoteName('a.id') . ' = ' . $db->quoteName('c.' . $pk)
  83                      . ' AND ' . $db->quoteName('a.context') . ' = :context'
  84                  )
  85                  ->bind(':context', $context)
  86                  ->join('INNER', $db->quoteName('#__associations', 'a2'), $db->quoteName('a.key') . ' = ' . $db->quoteName('a2.key'))
  87                  ->join(
  88                      'INNER',
  89                      $db->quoteName($tablename, 'c2'),
  90                      $db->quoteName('a2.id') . ' = ' . $db->quoteName('c2.' . $pk) . $categoriesExtraSql
  91                  );
  92  
  93              // Use alias field ?
  94              if (!empty($aliasField)) {
  95                  $query->select(
  96                      $query->concatenate(
  97                          [
  98                              $db->quoteName('c2.' . $pk),
  99                              $db->quoteName('c2.' . $aliasField),
 100                          ],
 101                          ':'
 102                      ) . ' AS ' . $db->quoteName($pk)
 103                  );
 104              } else {
 105                  $query->select($db->quoteName('c2.' . $pk));
 106              }
 107  
 108              // Use catid field ?
 109              if (!empty($catField)) {
 110                  $query->join(
 111                      'INNER',
 112                      $db->quoteName('#__categories', 'ca'),
 113                      $db->quoteName('c2.' . $catField) . ' = ' . $db->quoteName('ca.id') . ' AND ' . $db->quoteName('ca.extension') . ' = :extension2'
 114                  )
 115                      ->bind(':extension2', $extension)
 116                      ->select(
 117                          $query->concatenate(
 118                              [
 119                                  $db->quoteName('ca.id'),
 120                                  $db->quoteName('ca.alias'),
 121                              ],
 122                              ':'
 123                          ) . ' AS ' . $db->quoteName($catField)
 124                      );
 125              }
 126  
 127              $query->where($db->quoteName('c.' . $pk) . ' = :id')
 128                  ->bind(':id', $id, ParameterType::INTEGER);
 129  
 130              if ($tablename === '#__categories') {
 131                  $query->where($db->quoteName('c.extension') . ' = :extension3')
 132                      ->bind(':extension3', $extension);
 133              }
 134  
 135              // Advanced where clause
 136              if (!empty($advClause)) {
 137                  foreach ($advClause as $clause) {
 138                      $query->where($clause);
 139                  }
 140              }
 141  
 142              $db->setQuery($query);
 143  
 144              try {
 145                  $items = $db->loadObjectList('language');
 146              } catch (\RuntimeException $e) {
 147                  throw new \Exception($e->getMessage(), 500, $e);
 148              }
 149  
 150              if ($items) {
 151                  foreach ($items as $tag => $item) {
 152                      // Do not return itself as result
 153                      if ((int) $item->{$pk} !== $id) {
 154                          $multilanguageAssociations[$queryKey][$tag] = $item;
 155                      }
 156                  }
 157              }
 158          }
 159  
 160          return $multilanguageAssociations[$queryKey];
 161      }
 162  
 163      /**
 164       * Method to determine if the language filter Associations parameter is enabled.
 165       * This works for both site and administrator.
 166       *
 167       * @return  boolean  True if the parameter is implemented; false otherwise.
 168       *
 169       * @since   3.2
 170       */
 171      public static function isEnabled()
 172      {
 173          // Flag to avoid doing multiple database queries.
 174          static $tested = false;
 175  
 176          // Status of language filter parameter.
 177          static $enabled = false;
 178  
 179          if (Multilanguage::isEnabled()) {
 180              // If already tested, don't test again.
 181              if (!$tested) {
 182                  $plugin = PluginHelper::getPlugin('system', 'languagefilter');
 183  
 184                  if (!empty($plugin)) {
 185                      $params = new Registry($plugin->params);
 186                      $enabled  = (bool) $params->get('item_associations', true);
 187                  }
 188  
 189                  $tested = true;
 190              }
 191          }
 192  
 193          return $enabled;
 194      }
 195  }


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