[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Table/ -> Asset.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2008 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\Table;
  11  
  12  use Joomla\CMS\Language\Text;
  13  use Joomla\Database\DatabaseDriver;
  14  
  15  // phpcs:disable PSR1.Files.SideEffects
  16  \defined('JPATH_PLATFORM') or die;
  17  // phpcs:enable PSR1.Files.SideEffects
  18  
  19  /**
  20   * Table class supporting modified pre-order tree traversal behavior.
  21   *
  22   * @since  1.7.0
  23   */
  24  class Asset extends Nested
  25  {
  26      /**
  27       * The primary key of the asset.
  28       *
  29       * @var    integer
  30       * @since  1.7.0
  31       */
  32      public $id = null;
  33  
  34      /**
  35       * The unique name of the asset.
  36       *
  37       * @var    string
  38       * @since  1.7.0
  39       */
  40      public $name = null;
  41  
  42      /**
  43       * The human readable title of the asset.
  44       *
  45       * @var    string
  46       * @since  1.7.0
  47       */
  48      public $title = null;
  49  
  50      /**
  51       * The rules for the asset stored in a JSON string
  52       *
  53       * @var    string
  54       * @since  1.7.0
  55       */
  56      public $rules = null;
  57  
  58      /**
  59       * Constructor
  60       *
  61       * @param   DatabaseDriver  $db  Database driver object.
  62       *
  63       * @since   1.7.0
  64       */
  65      public function __construct(DatabaseDriver $db)
  66      {
  67          parent::__construct('#__assets', 'id', $db);
  68      }
  69  
  70      /**
  71       * Method to load an asset by its name.
  72       *
  73       * @param   string  $name  The name of the asset.
  74       *
  75       * @return  integer
  76       *
  77       * @since   1.7.0
  78       */
  79      public function loadByName($name)
  80      {
  81          return $this->load(array('name' => $name));
  82      }
  83  
  84      /**
  85       * Assert that the nested set data is valid.
  86       *
  87       * @return  boolean  True if the instance is sane and able to be stored in the database.
  88       *
  89       * @since   1.7.0
  90       */
  91      public function check()
  92      {
  93          try {
  94              parent::check();
  95          } catch (\Exception $e) {
  96              $this->setError($e->getMessage());
  97  
  98              return false;
  99          }
 100  
 101          $this->parent_id = (int) $this->parent_id;
 102  
 103          if (empty($this->rules)) {
 104              $this->rules = '{}';
 105          }
 106  
 107          // Nested does not allow parent_id = 0, override this.
 108          if ($this->parent_id > 0) {
 109              // Get the DatabaseQuery object
 110              $query = $this->_db->getQuery(true)
 111                  ->select('1')
 112                  ->from($this->_db->quoteName($this->_tbl))
 113                  ->where($this->_db->quoteName('id') . ' = ' . $this->parent_id);
 114  
 115              $query->setLimit(1);
 116  
 117              if ($this->_db->setQuery($query)->loadResult()) {
 118                  return true;
 119              }
 120  
 121              $this->setError(Text::_('JLIB_DATABASE_ERROR_INVALID_PARENT_ID'));
 122  
 123              return false;
 124          }
 125  
 126          return true;
 127      }
 128  
 129      /**
 130       * Method to recursively rebuild the whole nested set tree.
 131       *
 132       * @param   integer  $parentId  The root of the tree to rebuild.
 133       * @param   integer  $leftId    The left id to start with in building the tree.
 134       * @param   integer  $level     The level to assign to the current nodes.
 135       * @param   string   $path      The path to the current nodes.
 136       *
 137       * @return  integer  1 + value of root rgt on success, false on failure
 138       *
 139       * @since   3.5
 140       * @throws  \RuntimeException on database error.
 141       */
 142      public function rebuild($parentId = null, $leftId = 0, $level = 0, $path = null)
 143      {
 144          // If no parent is provided, try to find it.
 145          if ($parentId === null) {
 146              // Get the root item.
 147              $parentId = $this->getRootId();
 148  
 149              if ($parentId === false) {
 150                  return false;
 151              }
 152          }
 153  
 154          $query = $this->_db->getQuery(true);
 155  
 156          // Build the structure of the recursive query.
 157          if (!isset($this->_cache['rebuild.sql'])) {
 158              $query->clear()
 159                  ->select($this->_tbl_key)
 160                  ->from($this->_tbl)
 161                  ->where('parent_id = %d');
 162  
 163              // If the table has an ordering field, use that for ordering.
 164              if ($this->hasField('ordering')) {
 165                  $query->order('parent_id, ordering, lft');
 166              } else {
 167                  $query->order('parent_id, lft');
 168              }
 169  
 170              $this->_cache['rebuild.sql'] = (string) $query;
 171          }
 172  
 173          // Make a shortcut to database object.
 174  
 175          // Assemble the query to find all children of this node.
 176          $this->_db->setQuery(sprintf($this->_cache['rebuild.sql'], (int) $parentId));
 177  
 178          $children = $this->_db->loadObjectList();
 179  
 180          // The right value of this node is the left value + 1
 181          $rightId = $leftId + 1;
 182  
 183          // Execute this function recursively over all children
 184          foreach ($children as $node) {
 185              /*
 186               * $rightId is the current right value, which is incremented on recursion return.
 187               * Increment the level for the children.
 188               * Add this item's alias to the path (but avoid a leading /)
 189               */
 190              $rightId = $this->rebuild($node->{$this->_tbl_key}, $rightId, $level + 1);
 191  
 192              // If there is an update failure, return false to break out of the recursion.
 193              if ($rightId === false) {
 194                  return false;
 195              }
 196          }
 197  
 198          // We've got the left value, and now that we've processed
 199          // the children of this node we also know the right value.
 200          $query->clear()
 201              ->update($this->_tbl)
 202              ->set('lft = ' . (int) $leftId)
 203              ->set('rgt = ' . (int) $rightId)
 204              ->set('level = ' . (int) $level)
 205              ->where($this->_tbl_key . ' = ' . (int) $parentId);
 206          $this->_db->setQuery($query)->execute();
 207  
 208          // Return the right value of this node + 1.
 209          return $rightId + 1;
 210      }
 211  }


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