[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Helper/ -> CMSHelper.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\Application\ApplicationHelper;
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Language\LanguageHelper;
  16  use Joomla\CMS\Language\Multilanguage;
  17  use Joomla\CMS\Plugin\PluginHelper;
  18  use Joomla\CMS\Table\TableInterface;
  19  use Joomla\Registry\Registry;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('JPATH_PLATFORM') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * Base Helper class.
  27   *
  28   * @since  3.2
  29   */
  30  class CMSHelper
  31  {
  32      /**
  33       * Gets the current language
  34       *
  35       * @param   boolean  $detectBrowser  Flag indicating whether to use the browser language as a fallback.
  36       *
  37       * @return  string  The language string
  38       *
  39       * @since   3.2
  40       */
  41      public function getCurrentLanguage($detectBrowser = true)
  42      {
  43          $app = Factory::getApplication();
  44          $langCode = null;
  45  
  46          // Get the languagefilter parameters
  47          if (Multilanguage::isEnabled()) {
  48              $plugin       = PluginHelper::getPlugin('system', 'languagefilter');
  49              $pluginParams = new Registry($plugin->params);
  50  
  51              if ((int) $pluginParams->get('lang_cookie', 1) === 1) {
  52                  $langCode = $app->input->cookie->getString(ApplicationHelper::getHash('language'));
  53              } else {
  54                  $langCode = $app->getSession()->get('plg_system_languagefilter.language');
  55              }
  56          }
  57  
  58          // No cookie - let's try to detect browser language or use site default
  59          if (!$langCode) {
  60              if ($detectBrowser) {
  61                  $langCode = LanguageHelper::detectLanguage();
  62              } else {
  63                  $langCode = ComponentHelper::getParams('com_languages')->get('site', 'en-GB');
  64              }
  65          }
  66  
  67          return $langCode;
  68      }
  69  
  70      /**
  71       * Gets the associated language ID
  72       *
  73       * @param   string  $langCode  The language code to look up
  74       *
  75       * @return  integer  The language ID
  76       *
  77       * @since   3.2
  78       */
  79      public function getLanguageId($langCode)
  80      {
  81          $db    = Factory::getDbo();
  82          $query = $db->getQuery(true)
  83              ->select($db->quoteName('lang_id'))
  84              ->from($db->quoteName('#__languages'))
  85              ->where($db->quoteName('lang_code') . ' = :language')
  86              ->bind(':language', $langCode);
  87          $db->setQuery($query);
  88  
  89          return $db->loadResult();
  90      }
  91  
  92      /**
  93       * Gets a row of data from a table
  94       *
  95       * @param   TableInterface  $table  Table instance for a row.
  96       *
  97       * @return  array  Associative array of all columns and values for a row in a table.
  98       *
  99       * @since   3.2
 100       */
 101      public function getRowData(TableInterface $table)
 102      {
 103          $fields = $table->getFields();
 104          $data = array();
 105  
 106          foreach ($fields as &$field) {
 107              $columnName = $field->Field;
 108              $value = $table->$columnName;
 109              $data[$columnName] = $value;
 110          }
 111  
 112          return $data;
 113      }
 114  
 115      /**
 116       * Method to get an object containing all of the table columns and values.
 117       *
 118       * @param   TableInterface  $table  Table object.
 119       *
 120       * @return  \stdClass  Contains all of the columns and values.
 121       *
 122       * @since   3.2
 123       */
 124      public function getDataObject(TableInterface $table)
 125      {
 126          $fields = $table->getFields();
 127          $dataObject = new \stdClass();
 128  
 129          foreach ($fields as $field) {
 130              $fieldName = $field->Field;
 131              $dataObject->$fieldName = $table->get($fieldName);
 132          }
 133  
 134          return $dataObject;
 135      }
 136  }


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