[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/database/table/ -> category.php (source)

   1  <?php
   2  /**
   3   * @package     Joomla.Platform
   4   * @subpackage  Database
   5   *
   6   * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   7   * @license     GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  defined('JPATH_PLATFORM') or die;
  11  
  12  jimport('joomla.database.tablenested');
  13  
  14  /**
  15   * Category table
  16   *
  17   * @package     Joomla.Platform
  18   * @subpackage  Table
  19   * @since       11.1
  20   */
  21  class JTableCategory extends JTableNested
  22  {
  23      /**
  24       * Constructor
  25       *
  26       * @param   JDatabase  &$db  A database connector object
  27       *
  28       * @since   11.1
  29       */
  30  	public function __construct(&$db)
  31      {
  32          parent::__construct('#__categories', 'id', $db);
  33  
  34          $this->access = (int) JFactory::getConfig()->get('access');
  35      }
  36  
  37      /**
  38       * Method to compute the default name of the asset.
  39       * The default name is in the form table_name.id
  40       * where id is the value of the primary key of the table.
  41       *
  42       * @return  string
  43       *
  44       * @since   11.1
  45       */
  46  	protected function _getAssetName()
  47      {
  48          $k = $this->_tbl_key;
  49          return $this->extension . '.category.' . (int) $this->$k;
  50      }
  51  
  52      /**
  53       * Method to return the title to use for the asset table.
  54       *
  55       * @return  string
  56       *
  57       * @since   11.1
  58       */
  59  	protected function _getAssetTitle()
  60      {
  61          return $this->title;
  62      }
  63  
  64      /**
  65       * Get the parent asset id for the record
  66       *
  67       * @param   JTable   $table  A JTable object for the asset parent.
  68       * @param   integer  $id     The id for the asset
  69       *
  70       * @return  integer  The id of the asset's parent
  71       *
  72       * @since   11.1
  73       */
  74  	protected function _getAssetParentId($table = null, $id = null)
  75      {
  76          // Initialise variables.
  77          $assetId = null;
  78  
  79          // This is a category under a category.
  80          if ($this->parent_id > 1)
  81          {
  82              // Build the query to get the asset id for the parent category.
  83              $query = $this->_db->getQuery(true);
  84              $query->select($this->_db->quoteName('asset_id'));
  85              $query->from($this->_db->quoteName('#__categories'));
  86              $query->where($this->_db->quoteName('id') . ' = ' . $this->parent_id);
  87  
  88              // Get the asset id from the database.
  89              $this->_db->setQuery($query);
  90              if ($result = $this->_db->loadResult())
  91              {
  92                  $assetId = (int) $result;
  93              }
  94          }
  95          // This is a category that needs to parent with the extension.
  96          elseif ($assetId === null)
  97          {
  98              // Build the query to get the asset id for the parent category.
  99              $query = $this->_db->getQuery(true);
 100              $query->select($this->_db->quoteName('id'));
 101              $query->from($this->_db->quoteName('#__assets'));
 102              $query->where($this->_db->quoteName('name') . ' = ' . $this->_db->quote($this->extension));
 103  
 104              // Get the asset id from the database.
 105              $this->_db->setQuery($query);
 106              if ($result = $this->_db->loadResult())
 107              {
 108                  $assetId = (int) $result;
 109              }
 110          }
 111  
 112          // Return the asset id.
 113          if ($assetId)
 114          {
 115              return $assetId;
 116          }
 117          else
 118          {
 119              return parent::_getAssetParentId($table, $id);
 120          }
 121      }
 122  
 123      /**
 124       * Override check function
 125       *
 126       * @return  boolean
 127       *
 128       * @see     JTable::check
 129       * @since   11.1
 130       */
 131  	public function check()
 132      {
 133          // Check for a title.
 134          if (trim($this->title) == '')
 135          {
 136              $this->setError(JText::_('JLIB_DATABASE_ERROR_MUSTCONTAIN_A_TITLE_CATEGORY'));
 137              return false;
 138          }
 139          $this->alias = trim($this->alias);
 140          if (empty($this->alias))
 141          {
 142              $this->alias = $this->title;
 143          }
 144  
 145          $this->alias = JApplication::stringURLSafe($this->alias);
 146          if (trim(str_replace('-', '', $this->alias)) == '')
 147          {
 148              $this->alias = JFactory::getDate()->format('Y-m-d-H-i-s');
 149          }
 150  
 151          return true;
 152      }
 153  
 154      /**
 155       * Overloaded bind function.
 156       *
 157       * @param   array   $array   named array
 158       * @param   string  $ignore  An optional array or space separated list of properties
 159       * to ignore while binding.
 160       *
 161       * @return  mixed   Null if operation was satisfactory, otherwise returns an error
 162       *
 163       * @see     JTable::bind
 164       * @since   11.1
 165       */
 166  	public function bind($array, $ignore = '')
 167      {
 168          if (isset($array['params']) && is_array($array['params']))
 169          {
 170              $registry = new JRegistry;
 171              $registry->loadArray($array['params']);
 172              $array['params'] = (string) $registry;
 173          }
 174  
 175          if (isset($array['metadata']) && is_array($array['metadata']))
 176          {
 177              $registry = new JRegistry;
 178              $registry->loadArray($array['metadata']);
 179              $array['metadata'] = (string) $registry;
 180          }
 181  
 182          // Bind the rules.
 183          if (isset($array['rules']) && is_array($array['rules']))
 184          {
 185              $rules = new JAccessRules($array['rules']);
 186              $this->setRules($rules);
 187          }
 188  
 189          return parent::bind($array, $ignore);
 190      }
 191  
 192      /**
 193       * Overridden JTable::store to set created/modified and user id.
 194       *
 195       * @param   boolean  $updateNulls  True to update fields even if they are null.
 196       *
 197       * @return  boolean  True on success.
 198       *
 199       * @since   11.1
 200       */
 201  	public function store($updateNulls = false)
 202      {
 203          $date = JFactory::getDate();
 204          $user = JFactory::getUser();
 205  
 206          if ($this->id)
 207          {
 208              // Existing category
 209              $this->modified_time = $date->toSql();
 210              $this->modified_user_id = $user->get('id');
 211          }
 212          else
 213          {
 214              // New category
 215              $this->created_time = $date->toSql();
 216              $this->created_user_id = $user->get('id');
 217          }
 218          // Verify that the alias is unique
 219          $table = JTable::getInstance('Category', 'JTable', array('dbo' => $this->getDbo()));
 220          if ($table->load(array('alias' => $this->alias, 'parent_id' => $this->parent_id, 'extension' => $this->extension))
 221              && ($table->id != $this->id || $this->id == 0))
 222          {
 223  
 224              $this->setError(JText::_('JLIB_DATABASE_ERROR_CATEGORY_UNIQUE_ALIAS'));
 225              return false;
 226          }
 227          return parent::store($updateNulls);
 228      }
 229  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1