[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Helper/ -> ContentHelper.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\Helper;
  11  
  12  use Joomla\CMS\Access\Access;
  13  use Joomla\CMS\Application\ApplicationHelper;
  14  use Joomla\CMS\Component\ComponentHelper;
  15  use Joomla\CMS\Factory;
  16  use Joomla\CMS\Language\LanguageHelper;
  17  use Joomla\CMS\Language\Multilanguage;
  18  use Joomla\CMS\Language\Text;
  19  use Joomla\CMS\Log\Log;
  20  use Joomla\CMS\Object\CMSObject;
  21  use Joomla\CMS\Plugin\PluginHelper;
  22  use Joomla\CMS\Table\Table;
  23  use Joomla\Registry\Registry;
  24  
  25  // phpcs:disable PSR1.Files.SideEffects
  26  \defined('JPATH_PLATFORM') or die;
  27  // phpcs:enable PSR1.Files.SideEffects
  28  
  29  /**
  30   * Helper for standard content style extensions.
  31   * This class mainly simplifies static helper methods often repeated in individual components
  32   *
  33   * @since  3.1
  34   */
  35  class ContentHelper
  36  {
  37      /**
  38       * Configure the Linkbar. Must be implemented by each extension.
  39       *
  40       * @param   string  $vName  The name of the active view.
  41       *
  42       * @return  void
  43       *
  44       * @since   3.1
  45       */
  46      public static function addSubmenu($vName)
  47      {
  48      }
  49  
  50      /**
  51       * Adds Count relations for Category and Tag Managers
  52       *
  53       * @param   \stdClass[]  &$items  The category or tag objects
  54       * @param   \stdClass    $config  Configuration object allowing to use a custom relations table
  55       *
  56       * @return  \stdClass[]
  57       *
  58       * @since   3.9.1
  59       */
  60      public static function countRelations(&$items, $config)
  61      {
  62          $db = Factory::getDbo();
  63  
  64          // Allow custom state / condition values and custom column names to support custom components
  65          $counter_names = isset($config->counter_names) ? $config->counter_names : array(
  66              '-2' => 'count_trashed',
  67              '0'  => 'count_unpublished',
  68              '1'  => 'count_published',
  69              '2'  => 'count_archived',
  70          );
  71  
  72          // Index category objects by their ID
  73          $records = array();
  74  
  75          foreach ($items as $item) {
  76              $records[(int) $item->id] = $item;
  77          }
  78  
  79          // The relation query does not return a value for cases without relations of a particular state / condition, set zero as default
  80          foreach ($items as $item) {
  81              foreach ($counter_names as $n) {
  82                  $item->{$n} = 0;
  83              }
  84          }
  85  
  86          // Table alias for related data table below will be 'c', and state / condition column is inside related data table
  87          $related_tbl = '#__' . $config->related_tbl;
  88          $state_col   = 'c.' . $config->state_col;
  89  
  90          // Supported cases
  91          switch ($config->relation_type) {
  92              case 'tag_assigments':
  93                  $recid_col = 'ct.' . $config->group_col;
  94  
  95                  $query = $db->getQuery(true)
  96                      ->from($db->quoteName('#__contentitem_tag_map', 'ct'))
  97                      ->join(
  98                          'INNER',
  99                          $db->quoteName($related_tbl, 'c'),
 100                          $db->quoteName('ct.content_item_id') . ' = ' . $db->quoteName('c.id')
 101                          . ' AND ' . $db->quoteName('ct.type_alias') . ' = :extension'
 102                      )
 103                      ->bind(':extension', $config->extension);
 104                  break;
 105  
 106              case 'category_or_group':
 107                  $recid_col = 'c.' . $config->group_col;
 108  
 109                  $query = $db->getQuery(true)
 110                      ->from($db->quoteName($related_tbl, 'c'));
 111                  break;
 112  
 113              default:
 114                  return $items;
 115          }
 116  
 117          /**
 118           * Get relation counts for all category objects with single query
 119           * NOTE: 'state IN', allows counting specific states / conditions only, also prevents warnings with custom states / conditions, do not remove
 120           */
 121          $query
 122              ->select(
 123                  [
 124                      $db->quoteName($recid_col, 'catid'),
 125                      $db->quoteName($state_col, 'state'),
 126                      'COUNT(*) AS ' . $db->quoteName('count'),
 127                  ]
 128              )
 129              ->whereIn($db->quoteName($recid_col), array_keys($records))
 130              ->whereIn($db->quoteName($state_col), array_keys($counter_names))
 131              ->group($db->quoteName([$recid_col, $state_col]));
 132  
 133          $relationsAll = $db->setQuery($query)->loadObjectList();
 134  
 135          // Loop through the DB data overwriting the above zeros with the found count
 136          foreach ($relationsAll as $relation) {
 137              // Sanity check in case someone removes the state IN above ... and some views may start throwing warnings
 138              if (isset($counter_names[$relation->state])) {
 139                  $id = (int) $relation->catid;
 140                  $cn = $counter_names[$relation->state];
 141  
 142                  $records[$id]->{$cn} = $relation->count;
 143              }
 144          }
 145  
 146          return $items;
 147      }
 148  
 149      /**
 150       * Gets a list of the actions that can be performed.
 151       *
 152       * @param   string   $component  The component name.
 153       * @param   string   $section    The access section name.
 154       * @param   integer  $id         The item ID.
 155       *
 156       * @return  CMSObject
 157       *
 158       * @since   3.2
 159       */
 160      public static function getActions($component = '', $section = '', $id = 0)
 161      {
 162          $assetName = $component;
 163  
 164          if ($section && $id) {
 165              $assetName .= '.' . $section . '.' . (int) $id;
 166          }
 167  
 168          $result = new CMSObject();
 169  
 170          $user = Factory::getUser();
 171  
 172          $actions = Access::getActionsFromFile(
 173              JPATH_ADMINISTRATOR . '/components/' . $component . '/access.xml',
 174              '/access/section[@name="component"]/'
 175          );
 176  
 177          if ($actions === false) {
 178              Log::add(
 179                  Text::sprintf('JLIB_ERROR_COMPONENTS_ACL_CONFIGURATION_FILE_MISSING_OR_IMPROPERLY_STRUCTURED', $component),
 180                  Log::ERROR,
 181                  'jerror'
 182              );
 183  
 184              return $result;
 185          }
 186  
 187          foreach ($actions as $action) {
 188              $result->set($action->name, $user->authorise($action->name, $assetName));
 189          }
 190  
 191          return $result;
 192      }
 193  
 194      /**
 195       * Gets the current language
 196       *
 197       * @param   boolean  $detectBrowser  Flag indicating whether to use the browser language as a fallback.
 198       *
 199       * @return  string  The language string
 200       *
 201       * @since   3.1
 202       * @note    CmsHelper::getCurrentLanguage is the preferred method
 203       */
 204      public static function getCurrentLanguage($detectBrowser = true)
 205      {
 206          $app = Factory::getApplication();
 207          $langCode = null;
 208  
 209          // Get the languagefilter parameters
 210          if (Multilanguage::isEnabled()) {
 211              $plugin       = PluginHelper::getPlugin('system', 'languagefilter');
 212              $pluginParams = new Registry($plugin->params);
 213  
 214              if ((int) $pluginParams->get('lang_cookie', 1) === 1) {
 215                  $langCode = $app->input->cookie->getString(ApplicationHelper::getHash('language'));
 216              } else {
 217                  $langCode = $app->getSession()->get('plg_system_languagefilter.language');
 218              }
 219          }
 220  
 221          // No cookie - let's try to detect browser language or use site default
 222          if (!$langCode) {
 223              if ($detectBrowser) {
 224                  $langCode = LanguageHelper::detectLanguage();
 225              } else {
 226                  $langCode = ComponentHelper::getParams('com_languages')->get('site', 'en-GB');
 227              }
 228          }
 229  
 230          return $langCode;
 231      }
 232  
 233      /**
 234       * Gets the associated language ID
 235       *
 236       * @param   string  $langCode  The language code to look up
 237       *
 238       * @return  integer  The language ID
 239       *
 240       * @since   3.1
 241       * @note    CmsHelper::getLanguage() is the preferred method.
 242       */
 243      public static function getLanguageId($langCode)
 244      {
 245          $db    = Factory::getDbo();
 246          $query = $db->getQuery(true)
 247              ->select($db->quoteName('lang_id'))
 248              ->from($db->quoteName('#__languages'))
 249              ->where($db->quoteName('lang_code') . ' = :language')
 250              ->bind(':language', $langCode);
 251          $db->setQuery($query);
 252  
 253          return $db->loadResult();
 254      }
 255  
 256      /**
 257       * Gets a row of data from a table
 258       *
 259       * @param   Table  $table  Table instance for a row.
 260       *
 261       * @return  array  Associative array of all columns and values for a row in a table.
 262       *
 263       * @since   3.1
 264       */
 265      public function getRowData(Table $table)
 266      {
 267          $data = new CMSHelper();
 268  
 269          return $data->getRowData($table);
 270      }
 271  }


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