[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_mails/src/Model/ -> TemplatesModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_mails
   6   *
   7   * @copyright   (C) 2019 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\Mails\Administrator\Model;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Language\LanguageHelper;
  15  use Joomla\CMS\MVC\Model\ListModel;
  16  use Joomla\Database\QueryInterface;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('_JEXEC') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Methods supporting a list of mail template records.
  24   *
  25   * @since  4.0.0
  26   */
  27  class TemplatesModel extends ListModel
  28  {
  29      /**
  30       * Constructor.
  31       *
  32       * @param   array  $config  An optional associative array of configuration settings.
  33       *
  34       * @since   4.0.0
  35       * @throws  \Exception
  36       */
  37      public function __construct($config = array())
  38      {
  39          if (empty($config['filter_fields'])) {
  40              $config['filter_fields'] = array(
  41                  'template_id', 'a.template_id',
  42                  'language', 'a.language',
  43                  'subject', 'a.subject',
  44                  'body', 'a.body',
  45                  'htmlbody', 'a.htmlbody',
  46                  'extension'
  47              );
  48          }
  49  
  50          parent::__construct($config);
  51      }
  52  
  53      /**
  54       * Method to auto-populate the model state.
  55       *
  56       * This method should only be called once per instantiation and is designed
  57       * to be called on the first call to the getState() method unless the model
  58       * configuration flag to ignore the request is set.
  59       *
  60       * Note. Calling getState in this method will result in recursion.
  61       *
  62       * @param   string  $ordering   An optional ordering field.
  63       * @param   string  $direction  An optional direction (asc|desc).
  64       *
  65       * @return  void
  66       *
  67       * @since   4.0.0
  68       */
  69      protected function populateState($ordering = null, $direction = null)
  70      {
  71          // Load the parameters.
  72          $params = ComponentHelper::getParams('com_mails');
  73          $this->setState('params', $params);
  74  
  75          // List state information.
  76          parent::populateState('a.template_id', 'asc');
  77      }
  78  
  79      /**
  80       * Get a list of mail templates
  81       *
  82       * @return  array
  83       *
  84       * @since   4.0.0
  85       */
  86      public function getItems()
  87      {
  88          $items = parent::getItems();
  89          $id    = '';
  90  
  91          $db = $this->getDatabase();
  92          $query = $db->getQuery(true)
  93              ->select($db->quoteName('language'))
  94              ->from($db->quoteName('#__mail_templates'))
  95              ->where($db->quoteName('template_id') . ' = :id')
  96              ->where($db->quoteName('language') . ' != ' . $db->quote(''))
  97              ->order($db->quoteName('language') . ' ASC')
  98              ->bind(':id', $id);
  99  
 100          foreach ($items as $item) {
 101              $id = $item->template_id;
 102              $db->setQuery($query);
 103              $item->languages = $db->loadColumn();
 104          }
 105  
 106          return $items;
 107      }
 108  
 109      /**
 110       * Build an SQL query to load the list data.
 111       *
 112       * @return  QueryInterface
 113       *
 114       * @since   4.0.0
 115       */
 116      protected function getListQuery()
 117      {
 118          // Create a new query object.
 119          $db = $this->getDatabase();
 120          $query = $db->getQuery(true);
 121  
 122          // Select the required fields from the table.
 123          $query->select(
 124              $this->getState(
 125                  'list.select',
 126                  $db->quoteName('a') . '.*'
 127              )
 128          );
 129          $query->from($db->quoteName('#__mail_templates', 'a'))
 130              ->where($db->quoteName('a.language') . ' = ' . $db->quote(''));
 131  
 132          // Filter by search in title.
 133          if ($search = trim($this->getState('filter.search', ''))) {
 134              if (stripos($search, 'id:') === 0) {
 135                  $search = substr($search, 3);
 136                  $query->where($db->quoteName('a.template_id') . ' = :search')
 137                      ->bind(':search', $search);
 138              } else {
 139                  $search = '%' . str_replace(' ', '%', $search) . '%';
 140                  $query->where(
 141                      '(' . $db->quoteName('a.template_id') . ' LIKE :search1'
 142                      . ' OR ' . $db->quoteName('a.subject') . ' LIKE :search2'
 143                      . ' OR ' . $db->quoteName('a.body') . ' LIKE :search3'
 144                      . ' OR ' . $db->quoteName('a.htmlbody') . ' LIKE :search4)'
 145                  )
 146                      ->bind([':search1', ':search2', ':search3', ':search4'], $search);
 147              }
 148          }
 149  
 150          // Filter on the extension.
 151          if ($extension = $this->getState('filter.extension')) {
 152              $query->where($db->quoteName('a.extension') . ' = :extension')
 153                  ->bind(':extension', $extension);
 154          } else {
 155              // Only show mail template from enabled extensions
 156              $subQuery = $db->getQuery(true)
 157                  ->select($db->quoteName('name'))
 158                  ->from($db->quoteName('#__extensions'))
 159                  ->where($db->quoteName('enabled') . ' = 1');
 160  
 161              $query->where($db->quoteName('a.extension') . ' IN(' . $subQuery . ')');
 162          }
 163  
 164          // Filter on the language.
 165          if ($language = $this->getState('filter.language')) {
 166              $query->join(
 167                  'INNER',
 168                  $db->quoteName('#__mail_templates', 'b'),
 169                  $db->quoteName('b.template_id') . ' = ' . $db->quoteName('a.template_id')
 170                  . ' AND ' . $db->quoteName('b.language') . ' = :language'
 171              )
 172                  ->bind(':language', $language);
 173          }
 174  
 175          // Add the list ordering clause
 176          $listOrdering  = $this->state->get('list.ordering', 'a.template_id');
 177          $orderDirn     = $this->state->get('list.direction', 'ASC');
 178  
 179          $query->order($db->escape($listOrdering) . ' ' . $db->escape($orderDirn));
 180  
 181          return $query;
 182      }
 183  
 184      /**
 185       * Get list of extensions which are using mail templates
 186       *
 187       * @return array
 188       *
 189       * @since   4.0.0
 190       */
 191      public function getExtensions()
 192      {
 193          $db       = $this->getDatabase();
 194          $subQuery = $db->getQuery(true)
 195              ->select($db->quoteName('name'))
 196              ->from($db->quoteName('#__extensions'))
 197              ->where($db->quoteName('enabled') . ' = 1');
 198  
 199          $query = $db->getQuery(true)
 200              ->select('DISTINCT ' . $db->quoteName('extension'))
 201              ->from($db->quoteName('#__mail_templates'))
 202              ->where($db->quoteName('extension') . ' IN (' . $subQuery . ')');
 203          $db->setQuery($query);
 204  
 205          return $db->loadColumn();
 206      }
 207  
 208      /**
 209       * Get a list of the current content languages
 210       *
 211       * @return  array
 212       *
 213       * @since   4.0.0
 214       */
 215      public function getLanguages()
 216      {
 217          return LanguageHelper::getContentLanguages(array(0,1));
 218      }
 219  }


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