[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/components/com_config/src/Model/ -> ModulesModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_config
   6   *
   7   * @copyright   (C) 2014 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\Config\Site\Model;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Filesystem\Path;
  15  use Joomla\CMS\Form\Form;
  16  use Joomla\CMS\HTML\HTMLHelper;
  17  use Joomla\CMS\Language\Text;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Config Module model.
  25   *
  26   * @since  3.2
  27   */
  28  class ModulesModel extends FormModel
  29  {
  30      /**
  31       * Method to auto-populate the model state.
  32       *
  33       * Note. Calling getState in this method will result in recursion.
  34       *
  35       * @return  void
  36       *
  37       * @since   3.2
  38       */
  39      protected function populateState()
  40      {
  41          $app = Factory::getApplication();
  42  
  43          // Load the User state.
  44          $pk = $app->input->getInt('id');
  45  
  46          $this->setState('module.id', $pk);
  47      }
  48  
  49      /**
  50       * Method to get the record form.
  51       *
  52       * @param   array    $data      Data for the form.
  53       * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
  54       *
  55       * @return  Form  A Form object on success, false on failure
  56       *
  57       * @since   3.2
  58       */
  59      public function getForm($data = array(), $loadData = true)
  60      {
  61          // Get the form.
  62          $form = $this->loadForm('com_config.modules', 'modules', array('control' => 'jform', 'load_data' => $loadData));
  63  
  64          if (empty($form)) {
  65              return false;
  66          }
  67  
  68          return $form;
  69      }
  70  
  71      /**
  72       * Method to preprocess the form
  73       *
  74       * @param   Form    $form   A form object.
  75       * @param   mixed   $data   The data expected for the form.
  76       * @param   string  $group  The name of the plugin group to import (defaults to "content").
  77       *
  78       * @return  void
  79       *
  80       * @since   3.2
  81       * @throws  \Exception if there is an error loading the form.
  82       */
  83      protected function preprocessForm(Form $form, $data, $group = 'content')
  84      {
  85          $lang     = Factory::getLanguage();
  86          $module   = $this->getState()->get('module.name');
  87          $basePath = JPATH_BASE;
  88  
  89          $formFile = Path::clean($basePath . '/modules/' . $module . '/' . $module . '.xml');
  90  
  91          // Load the core and/or local language file(s).
  92          $lang->load($module, $basePath)
  93              ||   $lang->load($module, $basePath . '/modules/' . $module);
  94  
  95          if (file_exists($formFile)) {
  96              // Get the module form.
  97              if (!$form->loadFile($formFile, false, '//config')) {
  98                  throw new \Exception(Text::_('JERROR_LOADFILE_FAILED'));
  99              }
 100  
 101              // Attempt to load the xml file.
 102              if (!$xml = simplexml_load_file($formFile)) {
 103                  throw new \Exception(Text::_('JERROR_LOADFILE_FAILED'));
 104              }
 105          }
 106  
 107          // Load the default advanced params
 108          Form::addFormPath(JPATH_BASE . '/components/com_config/model/form');
 109          $form->loadFile('modules_advanced', false);
 110  
 111          // Trigger the default form events.
 112          parent::preprocessForm($form, $data, $group);
 113      }
 114  
 115      /**
 116       * Method to get list of module positions in current template
 117       *
 118       * @return  array
 119       *
 120       * @since   3.2
 121       */
 122      public function getPositions()
 123      {
 124          $lang         = Factory::getLanguage();
 125          $templateName = Factory::getApplication()->getTemplate();
 126  
 127          // Load templateDetails.xml file
 128          $path = Path::clean(JPATH_BASE . '/templates/' . $templateName . '/templateDetails.xml');
 129          $currentTemplatePositions = array();
 130  
 131          if (file_exists($path)) {
 132              $xml = simplexml_load_file($path);
 133  
 134              if (isset($xml->positions[0])) {
 135                  // Load language files
 136                  $lang->load('tpl_' . $templateName . '.sys', JPATH_BASE)
 137                  ||  $lang->load('tpl_' . $templateName . '.sys', JPATH_BASE . '/templates/' . $templateName);
 138  
 139                  foreach ($xml->positions[0] as $position) {
 140                      $value = (string) $position;
 141                      $text = preg_replace('/[^a-zA-Z0-9_\-]/', '_', 'TPL_' . strtoupper($templateName) . '_POSITION_' . strtoupper($value));
 142  
 143                      // Construct list of positions
 144                      $currentTemplatePositions[] = self::createOption($value, Text::_($text) . ' [' . $value . ']');
 145                  }
 146              }
 147          }
 148  
 149          $templateGroups = array();
 150  
 151          // Add an empty value to be able to deselect a module position
 152          $option = self::createOption();
 153          $templateGroups[''] = self::createOptionGroup('', array($option));
 154  
 155          $templateGroups[$templateName] = self::createOptionGroup($templateName, $currentTemplatePositions);
 156  
 157          // Add custom position to options
 158          $customGroupText = Text::_('COM_MODULES_CUSTOM_POSITION');
 159  
 160          $editPositions   = true;
 161          $customPositions = self::getActivePositions(0, $editPositions);
 162          $templateGroups[$customGroupText] = self::createOptionGroup($customGroupText, $customPositions);
 163  
 164          return $templateGroups;
 165      }
 166  
 167      /**
 168       * Get a list of modules positions
 169       *
 170       * @param   integer  $clientId       Client ID
 171       * @param   boolean  $editPositions  Allow to edit the positions
 172       *
 173       * @return  array  A list of positions
 174       *
 175       * @since   3.6.3
 176       */
 177      public static function getActivePositions($clientId, $editPositions = false)
 178      {
 179          $db = Factory::getDbo();
 180          $query = $db->getQuery(true)
 181              ->select('DISTINCT position')
 182              ->from($db->quoteName('#__modules'))
 183              ->where($db->quoteName('client_id') . ' = ' . (int) $clientId)
 184              ->order($db->quoteName('position'));
 185  
 186          $db->setQuery($query);
 187  
 188          try {
 189              $positions = $db->loadColumn();
 190              $positions = is_array($positions) ? $positions : array();
 191          } catch (\RuntimeException $e) {
 192              Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
 193  
 194              return;
 195          }
 196  
 197          // Build the list
 198          $options = array();
 199  
 200          foreach ($positions as $position) {
 201              if (!$position && !$editPositions) {
 202                  $options[] = HTMLHelper::_('select.option', 'none', ':: ' . Text::_('JNONE') . ' ::');
 203              } else {
 204                  $options[] = HTMLHelper::_('select.option', $position, $position);
 205              }
 206          }
 207  
 208          return $options;
 209      }
 210  
 211      /**
 212       * Create and return a new Option
 213       *
 214       * @param   string  $value  The option value [optional]
 215       * @param   string  $text   The option text [optional]
 216       *
 217       * @return  object  The option as an object (stdClass instance)
 218       *
 219       * @since   3.6.3
 220       */
 221      private static function createOption($value = '', $text = '')
 222      {
 223          if (empty($text)) {
 224              $text = $value;
 225          }
 226  
 227          $option = new \stdClass();
 228          $option->value = $value;
 229          $option->text  = $text;
 230  
 231          return $option;
 232      }
 233  
 234      /**
 235       * Create and return a new Option Group
 236       *
 237       * @param   string  $label    Value and label for group [optional]
 238       * @param   array   $options  Array of options to insert into group [optional]
 239       *
 240       * @return  array  Return the new group as an array
 241       *
 242       * @since   3.6.3
 243       */
 244      private static function createOptionGroup($label = '', $options = array())
 245      {
 246          $group = array();
 247          $group['value'] = $label;
 248          $group['text']  = $label;
 249          $group['items'] = $options;
 250  
 251          return $group;
 252      }
 253  }


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