[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_banners/src/Table/ -> BannerTable.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_banners
   6   *
   7   * @copyright   (C) 2005 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\Banners\Administrator\Table;
  12  
  13  use Joomla\CMS\Application\ApplicationHelper;
  14  use Joomla\CMS\Component\ComponentHelper;
  15  use Joomla\CMS\Factory;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\CMS\Table\Table;
  18  use Joomla\CMS\Versioning\VersionableTableInterface;
  19  use Joomla\Database\DatabaseDriver;
  20  use Joomla\Database\ParameterType;
  21  use Joomla\Registry\Registry;
  22  use Joomla\Utilities\ArrayHelper;
  23  
  24  // phpcs:disable PSR1.Files.SideEffects
  25  \defined('_JEXEC') or die;
  26  // phpcs:enable PSR1.Files.SideEffects
  27  
  28  /**
  29   * Banner table
  30   *
  31   * @since  1.5
  32   */
  33  class BannerTable extends Table implements VersionableTableInterface
  34  {
  35      /**
  36       * Indicates that columns fully support the NULL value in the database
  37       *
  38       * @var    boolean
  39       * @since  4.0.0
  40       */
  41      protected $_supportNullValue = true;
  42  
  43      /**
  44       * Constructor
  45       *
  46       * @param   DatabaseDriver  $db  Database connector object
  47       *
  48       * @since   1.5
  49       */
  50      public function __construct(DatabaseDriver $db)
  51      {
  52          $this->typeAlias = 'com_banners.banner';
  53  
  54          parent::__construct('#__banners', 'id', $db);
  55  
  56          $this->created = Factory::getDate()->toSql();
  57          $this->setColumnAlias('published', 'state');
  58      }
  59  
  60      /**
  61       * Increase click count
  62       *
  63       * @return  void
  64       */
  65      public function clicks()
  66      {
  67          $id    = (int) $this->id;
  68          $query = $this->_db->getQuery(true)
  69              ->update($this->_db->quoteName('#__banners'))
  70              ->set($this->_db->quoteName('clicks') . ' = ' . $this->_db->quoteName('clicks') . ' + 1')
  71              ->where($this->_db->quoteName('id') . ' = :id')
  72              ->bind(':id', $id, ParameterType::INTEGER);
  73  
  74          $this->_db->setQuery($query);
  75          $this->_db->execute();
  76      }
  77  
  78      /**
  79       * Overloaded check function
  80       *
  81       * @return  boolean
  82       *
  83       * @see     Table::check
  84       * @since   1.5
  85       */
  86      public function check()
  87      {
  88          try {
  89              parent::check();
  90          } catch (\Exception $e) {
  91              $this->setError($e->getMessage());
  92  
  93              return false;
  94          }
  95  
  96          // Set name
  97          $this->name = htmlspecialchars_decode($this->name, ENT_QUOTES);
  98  
  99          // Set alias
 100          if (trim($this->alias) == '') {
 101              $this->alias = $this->name;
 102          }
 103  
 104          $this->alias = ApplicationHelper::stringURLSafe($this->alias, $this->language);
 105  
 106          if (trim(str_replace('-', '', $this->alias)) == '') {
 107              $this->alias = Factory::getDate()->format('Y-m-d-H-i-s');
 108          }
 109  
 110          // Check for a valid category.
 111          if (!$this->catid = (int) $this->catid) {
 112              $this->setError(Text::_('JLIB_DATABASE_ERROR_CATEGORY_REQUIRED'));
 113  
 114              return false;
 115          }
 116  
 117          // Set created date if not set.
 118          if (!(int) $this->created) {
 119              $this->created = Factory::getDate()->toSql();
 120          }
 121  
 122          // Set publish_up, publish_down to null if not set
 123          if (!$this->publish_up) {
 124              $this->publish_up = null;
 125          }
 126  
 127          if (!$this->publish_down) {
 128              $this->publish_down = null;
 129          }
 130  
 131          // Check the publish down date is not earlier than publish up.
 132          if (!\is_null($this->publish_down) && !\is_null($this->publish_up) && $this->publish_down < $this->publish_up) {
 133              $this->setError(Text::_('JGLOBAL_START_PUBLISH_AFTER_FINISH'));
 134  
 135              return false;
 136          }
 137  
 138          // Set ordering
 139          if ($this->state < 0) {
 140              // Set ordering to 0 if state is archived or trashed
 141              $this->ordering = 0;
 142          } elseif (empty($this->ordering)) {
 143              // Set ordering to last if ordering was 0
 144              $this->ordering = self::getNextOrder($this->_db->quoteName('catid') . ' = ' . ((int) $this->catid) . ' AND ' . $this->_db->quoteName('state') . ' >= 0');
 145          }
 146  
 147          // Set modified to created if not set
 148          if (!$this->modified) {
 149              $this->modified = $this->created;
 150          }
 151  
 152          // Set modified_by to created_by if not set
 153          if (empty($this->modified_by)) {
 154              $this->modified_by = $this->created_by;
 155          }
 156  
 157          return true;
 158      }
 159  
 160      /**
 161       * Overloaded bind function
 162       *
 163       * @param   mixed  $array   An associative array or object to bind to the \JTable instance.
 164       * @param   mixed  $ignore  An optional array or space separated list of properties to ignore while binding.
 165       *
 166       * @return  boolean  True on success
 167       *
 168       * @since   1.5
 169       */
 170      public function bind($array, $ignore = array())
 171      {
 172          if (isset($array['params']) && \is_array($array['params'])) {
 173              $registry = new Registry($array['params']);
 174  
 175              if ((int) $registry->get('width', 0) < 0) {
 176                  $this->setError(Text::sprintf('JLIB_DATABASE_ERROR_NEGATIVE_NOT_PERMITTED', Text::_('COM_BANNERS_FIELD_WIDTH_LABEL')));
 177  
 178                  return false;
 179              }
 180  
 181              if ((int) $registry->get('height', 0) < 0) {
 182                  $this->setError(Text::sprintf('JLIB_DATABASE_ERROR_NEGATIVE_NOT_PERMITTED', Text::_('COM_BANNERS_FIELD_HEIGHT_LABEL')));
 183  
 184                  return false;
 185              }
 186  
 187              // Converts the width and height to an absolute numeric value:
 188              $width  = abs((int) $registry->get('width', 0));
 189              $height = abs((int) $registry->get('height', 0));
 190  
 191              // Sets the width and height to an empty string if = 0
 192              $registry->set('width', $width ?: '');
 193              $registry->set('height', $height ?: '');
 194  
 195              $array['params'] = (string) $registry;
 196          }
 197  
 198          if (isset($array['imptotal'])) {
 199              $array['imptotal'] = abs((int) $array['imptotal']);
 200          }
 201  
 202          return parent::bind($array, $ignore);
 203      }
 204  
 205      /**
 206       * Method to store a row
 207       *
 208       * @param   boolean  $updateNulls  True to update fields even if they are null.
 209       *
 210       * @return  boolean  True on success, false on failure.
 211       */
 212      public function store($updateNulls = true)
 213      {
 214          $db = $this->getDbo();
 215  
 216          if (empty($this->id)) {
 217              $purchaseType = $this->purchase_type;
 218  
 219              if ($purchaseType < 0 && $this->cid) {
 220                  $client = new ClientTable($db);
 221                  $client->load($this->cid);
 222                  $purchaseType = $client->purchase_type;
 223              }
 224  
 225              if ($purchaseType < 0) {
 226                  $purchaseType = ComponentHelper::getParams('com_banners')->get('purchase_type');
 227              }
 228  
 229              switch ($purchaseType) {
 230                  case 1:
 231                      $this->reset = null;
 232                      break;
 233                  case 2:
 234                      $date = Factory::getDate('+1 year ' . date('Y-m-d'));
 235                      $this->reset = $date->toSql();
 236                      break;
 237                  case 3:
 238                      $date = Factory::getDate('+1 month ' . date('Y-m-d'));
 239                      $this->reset = $date->toSql();
 240                      break;
 241                  case 4:
 242                      $date = Factory::getDate('+7 day ' . date('Y-m-d'));
 243                      $this->reset = $date->toSql();
 244                      break;
 245                  case 5:
 246                      $date = Factory::getDate('+1 day ' . date('Y-m-d'));
 247                      $this->reset = $date->toSql();
 248                      break;
 249              }
 250  
 251              // Store the row
 252              parent::store($updateNulls);
 253          } else {
 254              // Get the old row
 255              /** @var BannerTable $oldrow */
 256              $oldrow = Table::getInstance('BannerTable', __NAMESPACE__ . '\\', array('dbo' => $db));
 257  
 258              if (!$oldrow->load($this->id) && $oldrow->getError()) {
 259                  $this->setError($oldrow->getError());
 260              }
 261  
 262              // Verify that the alias is unique
 263              /** @var BannerTable $table */
 264              $table = Table::getInstance('BannerTable', __NAMESPACE__ . '\\', array('dbo' => $db));
 265  
 266              if ($table->load(array('alias' => $this->alias, 'catid' => $this->catid)) && ($table->id != $this->id || $this->id == 0)) {
 267                  $this->setError(Text::_('COM_BANNERS_ERROR_UNIQUE_ALIAS'));
 268  
 269                  return false;
 270              }
 271  
 272              // Store the new row
 273              parent::store($updateNulls);
 274  
 275              // Need to reorder ?
 276              if ($oldrow->state >= 0 && ($this->state < 0 || $oldrow->catid != $this->catid)) {
 277                  // Reorder the oldrow
 278                  $this->reorder($this->_db->quoteName('catid') . ' = ' . ((int) $oldrow->catid) . ' AND ' . $this->_db->quoteName('state') . ' >= 0');
 279              }
 280          }
 281  
 282          return \count($this->getErrors()) == 0;
 283      }
 284  
 285      /**
 286       * Method to set the sticky state for a row or list of rows in the database
 287       * table.  The method respects checked out rows by other users and will attempt
 288       * to checkin rows that it can after adjustments are made.
 289       *
 290       * @param   mixed    $pks     An optional array of primary key values to update.  If not set the instance property value is used.
 291       * @param   integer  $state   The sticky state. eg. [0 = unsticked, 1 = sticked]
 292       * @param   integer  $userId  The user id of the user performing the operation.
 293       *
 294       * @return  boolean  True on success.
 295       *
 296       * @since   1.6
 297       */
 298      public function stick($pks = null, $state = 1, $userId = 0)
 299      {
 300          $k = $this->_tbl_key;
 301  
 302          // Sanitize input.
 303          $pks    = ArrayHelper::toInteger($pks);
 304          $userId = (int) $userId;
 305          $state  = (int) $state;
 306  
 307          // If there are no primary keys set check to see if the instance key is set.
 308          if (empty($pks)) {
 309              if ($this->$k) {
 310                  $pks = array($this->$k);
 311              } else {
 312                  // Nothing to set publishing state on, return false.
 313                  $this->setError(Text::_('JLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));
 314  
 315                  return false;
 316              }
 317          }
 318  
 319          // Get an instance of the table
 320          /** @var BannerTable $table */
 321          $table = Table::getInstance('BannerTable', __NAMESPACE__ . '\\', array('dbo' => $this->_db));
 322  
 323          // For all keys
 324          foreach ($pks as $pk) {
 325              // Load the banner
 326              if (!$table->load($pk)) {
 327                  $this->setError($table->getError());
 328              }
 329  
 330              // Verify checkout
 331              if (\is_null($table->checked_out) || $table->checked_out == $userId) {
 332                  // Change the state
 333                  $table->sticky = $state;
 334                  $table->checked_out = null;
 335                  $table->checked_out_time = null;
 336  
 337                  // Check the row
 338                  $table->check();
 339  
 340                  // Store the row
 341                  if (!$table->store()) {
 342                      $this->setError($table->getError());
 343                  }
 344              }
 345          }
 346  
 347          return \count($this->getErrors()) == 0;
 348      }
 349  
 350      /**
 351       * Get the type alias for the history table
 352       *
 353       * @return  string  The alias as described above
 354       *
 355       * @since   4.0.0
 356       */
 357      public function getTypeAlias()
 358      {
 359          return $this->typeAlias;
 360      }
 361  }


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