[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_modules/src/Helper/ -> ModulesHelper.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_modules
   6   *
   7   * @copyright   (C) 2017 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\Modules\Administrator\Helper;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\HTML\HTMLHelper;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\Database\ParameterType;
  17  use Joomla\Utilities\ArrayHelper;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Modules component helper.
  25   *
  26   * @since  1.6
  27   */
  28  abstract class ModulesHelper
  29  {
  30      /**
  31       * Get a list of filter options for the state of a module.
  32       *
  33       * @return  array  An array of \JHtmlOption elements.
  34       */
  35      public static function getStateOptions()
  36      {
  37          // Build the filter options.
  38          $options   = array();
  39          $options[] = HTMLHelper::_('select.option', '1', Text::_('JPUBLISHED'));
  40          $options[] = HTMLHelper::_('select.option', '0', Text::_('JUNPUBLISHED'));
  41          $options[] = HTMLHelper::_('select.option', '-2', Text::_('JTRASHED'));
  42          $options[] = HTMLHelper::_('select.option', '*', Text::_('JALL'));
  43  
  44          return $options;
  45      }
  46  
  47      /**
  48       * Get a list of filter options for the application clients.
  49       *
  50       * @return  array  An array of \JHtmlOption elements.
  51       */
  52      public static function getClientOptions()
  53      {
  54          // Build the filter options.
  55          $options   = array();
  56          $options[] = HTMLHelper::_('select.option', '0', Text::_('JSITE'));
  57          $options[] = HTMLHelper::_('select.option', '1', Text::_('JADMINISTRATOR'));
  58  
  59          return $options;
  60      }
  61  
  62      /**
  63       * Get a list of modules positions
  64       *
  65       * @param   integer  $clientId       Client ID
  66       * @param   boolean  $editPositions  Allow to edit the positions
  67       *
  68       * @return  array  A list of positions
  69       */
  70      public static function getPositions($clientId, $editPositions = false)
  71      {
  72          $db       = Factory::getDbo();
  73          $clientId = (int) $clientId;
  74          $query    = $db->getQuery(true)
  75              ->select('DISTINCT ' . $db->quoteName('position'))
  76              ->from($db->quoteName('#__modules'))
  77              ->where($db->quoteName('client_id') . ' = :clientid')
  78              ->order($db->quoteName('position'))
  79              ->bind(':clientid', $clientId, ParameterType::INTEGER);
  80  
  81          $db->setQuery($query);
  82  
  83          try {
  84              $positions = $db->loadColumn();
  85              $positions = is_array($positions) ? $positions : array();
  86          } catch (\RuntimeException $e) {
  87              Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
  88  
  89              return;
  90          }
  91  
  92          // Build the list
  93          $options = array();
  94  
  95          foreach ($positions as $position) {
  96              if (!$position && !$editPositions) {
  97                  $options[] = HTMLHelper::_('select.option', 'none', Text::_('COM_MODULES_NONE'));
  98              } elseif (!$position) {
  99                  $options[] = HTMLHelper::_('select.option', '', Text::_('COM_MODULES_NONE'));
 100              } else {
 101                  $options[] = HTMLHelper::_('select.option', $position, $position);
 102              }
 103          }
 104  
 105          return $options;
 106      }
 107  
 108      /**
 109       * Return a list of templates
 110       *
 111       * @param   integer  $clientId  Client ID
 112       * @param   string   $state     State
 113       * @param   string   $template  Template name
 114       *
 115       * @return  array  List of templates
 116       */
 117      public static function getTemplates($clientId = 0, $state = '', $template = '')
 118      {
 119          $db       = Factory::getDbo();
 120          $clientId = (int) $clientId;
 121  
 122          // Get the database object and a new query object.
 123          $query = $db->getQuery(true);
 124  
 125          // Build the query.
 126          $query->select($db->quoteName(['element', 'name', 'enabled']))
 127              ->from($db->quoteName('#__extensions'))
 128              ->where($db->quoteName('client_id') . ' = :clientid')
 129              ->where($db->quoteName('type') . ' = ' . $db->quote('template'));
 130  
 131          if ($state != '') {
 132              $query->where($db->quoteName('enabled') . ' = :state')
 133                  ->bind(':state', $state);
 134          }
 135  
 136          if ($template != '') {
 137              $query->where($db->quoteName('element') . ' = :element')
 138                  ->bind(':element', $template);
 139          }
 140  
 141          $query->bind(':clientid', $clientId, ParameterType::INTEGER);
 142  
 143          // Set the query and load the templates.
 144          $db->setQuery($query);
 145          $templates = $db->loadObjectList('element');
 146  
 147          return $templates;
 148      }
 149  
 150      /**
 151       * Get a list of the unique modules installed in the client application.
 152       *
 153       * @param   int  $clientId  The client id.
 154       *
 155       * @return  array  Array of unique modules
 156       */
 157      public static function getModules($clientId)
 158      {
 159          $db    = Factory::getDbo();
 160          $query = $db->getQuery(true)
 161              ->select('element AS value, name AS text')
 162              ->from('#__extensions as e')
 163              ->where('e.client_id = ' . (int) $clientId)
 164              ->where('type = ' . $db->quote('module'))
 165              ->join('LEFT', '#__modules as m ON m.module=e.element AND m.client_id=e.client_id')
 166              ->where('m.module IS NOT NULL')
 167              ->group('element,name');
 168  
 169          $db->setQuery($query);
 170          $modules = $db->loadObjectList();
 171          $lang = Factory::getLanguage();
 172  
 173          foreach ($modules as $i => $module) {
 174              $extension = $module->value;
 175              $path = $clientId ? JPATH_ADMINISTRATOR : JPATH_SITE;
 176              $source = $path . "/modules/$extension";
 177                  $lang->load("$extension.sys", $path)
 178              ||  $lang->load("$extension.sys", $source);
 179              $modules[$i]->text = Text::_($module->text);
 180          }
 181  
 182          $modules = ArrayHelper::sortObjects($modules, 'text', 1, true, true);
 183  
 184          return $modules;
 185      }
 186  
 187      /**
 188       * Get a list of the assignment options for modules to menus.
 189       *
 190       * @param   int  $clientId  The client id.
 191       *
 192       * @return  array
 193       */
 194      public static function getAssignmentOptions($clientId)
 195      {
 196          $options = array();
 197          $options[] = HTMLHelper::_('select.option', '0', 'COM_MODULES_OPTION_MENU_ALL');
 198          $options[] = HTMLHelper::_('select.option', '-', 'COM_MODULES_OPTION_MENU_NONE');
 199  
 200          if ($clientId == 0) {
 201              $options[] = HTMLHelper::_('select.option', '1', 'COM_MODULES_OPTION_MENU_INCLUDE');
 202              $options[] = HTMLHelper::_('select.option', '-1', 'COM_MODULES_OPTION_MENU_EXCLUDE');
 203          }
 204  
 205          return $options;
 206      }
 207  
 208      /**
 209       * Return a translated module position name
 210       *
 211       * @param   integer  $clientId  Application client id 0: site | 1: admin
 212       * @param   string   $template  Template name
 213       * @param   string   $position  Position name
 214       *
 215       * @return  string  Return a translated position name
 216       *
 217       * @since   3.0
 218       */
 219      public static function getTranslatedModulePosition($clientId, $template, $position)
 220      {
 221          // Template translation
 222          $lang = Factory::getLanguage();
 223          $path = $clientId ? JPATH_ADMINISTRATOR : JPATH_SITE;
 224  
 225          $loaded = $lang->getPaths('tpl_' . $template . '.sys');
 226  
 227          // Only load the template's language file if it hasn't been already
 228          if (!$loaded) {
 229              $lang->load('tpl_' . $template . '.sys', $path, null, false, false)
 230              ||  $lang->load('tpl_' . $template . '.sys', $path . '/templates/' . $template, null, false, false)
 231              ||  $lang->load('tpl_' . $template . '.sys', $path, $lang->getDefault(), false, false)
 232              ||  $lang->load('tpl_' . $template . '.sys', $path . '/templates/' . $template, $lang->getDefault(), false, false);
 233          }
 234  
 235          $langKey = strtoupper('TPL_' . $template . '_POSITION_' . $position);
 236          $text = Text::_($langKey);
 237  
 238          // Avoid untranslated strings
 239          if (!self::isTranslatedText($langKey, $text)) {
 240              // Modules component translation
 241              $langKey = strtoupper('COM_MODULES_POSITION_' . $position);
 242              $text = Text::_($langKey);
 243  
 244              // Avoid untranslated strings
 245              if (!self::isTranslatedText($langKey, $text)) {
 246                  // Try to humanize the position name
 247                  $text = ucfirst(preg_replace('/^' . $template . '\-/', '', $position));
 248                  $text = ucwords(str_replace(array('-', '_'), ' ', $text));
 249              }
 250          }
 251  
 252          return $text;
 253      }
 254  
 255      /**
 256       * Check if the string was translated
 257       *
 258       * @param   string  $langKey  Language file text key
 259       * @param   string  $text     The "translated" text to be checked
 260       *
 261       * @return  boolean  Return true for translated text
 262       *
 263       * @since   3.0
 264       */
 265      public static function isTranslatedText($langKey, $text)
 266      {
 267          return $text !== $langKey;
 268      }
 269  
 270      /**
 271       * Create and return a new Option
 272       *
 273       * @param   string  $value  The option value [optional]
 274       * @param   string  $text   The option text [optional]
 275       *
 276       * @return  object  The option as an object (\stdClass instance)
 277       *
 278       * @since   3.0
 279       */
 280      public static function createOption($value = '', $text = '')
 281      {
 282          if (empty($text)) {
 283              $text = $value;
 284          }
 285  
 286          $option = new \stdClass();
 287          $option->value = $value;
 288          $option->text  = $text;
 289  
 290          return $option;
 291      }
 292  
 293      /**
 294       * Create and return a new Option Group
 295       *
 296       * @param   string  $label    Value and label for group [optional]
 297       * @param   array   $options  Array of options to insert into group [optional]
 298       *
 299       * @return  array  Return the new group as an array
 300       *
 301       * @since   3.0
 302       */
 303      public static function createOptionGroup($label = '', $options = array())
 304      {
 305          $group = array();
 306          $group['value'] = $label;
 307          $group['text']  = $label;
 308          $group['items'] = $options;
 309  
 310          return $group;
 311      }
 312  }


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