[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/database/table/ -> menutype.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.table');
  13  
  14  /**
  15   * Menu Types table
  16   *
  17   * @package     Joomla.Platform
  18   * @subpackage  Table
  19   * @since       11.1
  20   */
  21  class JTableMenuType extends JTable
  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('#__menu_types', 'id', $db);
  33      }
  34  
  35      /**
  36       * Overloaded check function
  37       *
  38       * @return  boolean  True on success, false on failure
  39       *
  40       * @see     JTable::check
  41       * @since   11.1
  42       */
  43  	public function check()
  44      {
  45          $this->menutype = JApplication::stringURLSafe($this->menutype);
  46          if (empty($this->menutype))
  47          {
  48              $this->setError(JText::_('JLIB_DATABASE_ERROR_MENUTYPE_EMPTY'));
  49              return false;
  50          }
  51  
  52          // Sanitise data.
  53          if (trim($this->title) == '')
  54          {
  55              $this->title = $this->menutype;
  56          }
  57  
  58          // Check for unique menutype.
  59          $query = $this->_db->getQuery(true);
  60          $query->select('COUNT(id)');
  61          $query->from($this->_db->quoteName('#__menu_types'));
  62          $query->where($this->_db->quoteName('menutype') . ' = ' . $this->_db->quote($this->menutype));
  63          $query->where($this->_db->quoteName('id') . ' <> ' . (int) $this->id);
  64          $this->_db->setQuery($query);
  65  
  66          if ($this->_db->loadResult())
  67          {
  68              $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_MENUTYPE_EXISTS', $this->menutype));
  69              return false;
  70          }
  71  
  72          return true;
  73      }
  74  
  75      /**
  76       * Method to store a row in the database from the JTable instance properties.
  77       * If a primary key value is set the row with that primary key value will be
  78       * updated with the instance property values.  If no primary key value is set
  79       * a new row will be inserted into the database with the properties from the
  80       * JTable instance.
  81       *
  82       * @param   boolean  $updateNulls  True to update fields even if they are null.
  83       *
  84       * @return  boolean  True on success.
  85       *
  86       * @link    http://docs.joomla.org/JTable/store
  87       * @since   11.1
  88       */
  89  	public function store($updateNulls = false)
  90      {
  91          if ($this->id)
  92          {
  93              // Get the user id
  94              $userId = JFactory::getUser()->id;
  95  
  96              // Get the old value of the table
  97              $table = JTable::getInstance('Menutype', 'JTable');
  98              $table->load($this->id);
  99  
 100              // Verify that no items are checked out
 101              $query = $this->_db->getQuery(true);
 102              $query->select('id');
 103              $query->from('#__menu');
 104              $query->where('menutype=' . $this->_db->quote($table->menutype));
 105              $query->where('checked_out !=' . (int) $userId);
 106              $query->where('checked_out !=0');
 107              $this->_db->setQuery($query);
 108              if ($this->_db->loadRowList())
 109              {
 110                  $this->setError(
 111                      JText::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED', get_class($this), JText::_('JLIB_DATABASE_ERROR_MENUTYPE_CHECKOUT'))
 112                  );
 113                  return false;
 114              }
 115  
 116              // Verify that no module for this menu are checked out
 117              $query = $this->_db->getQuery(true);
 118              $query->select('id');
 119              $query->from('#__modules');
 120              $query->where('module=' . $this->_db->quote('mod_menu'));
 121              $query->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'));
 122              $query->where('checked_out !=' . (int) $userId);
 123              $query->where('checked_out !=0');
 124              $this->_db->setQuery($query);
 125              if ($this->_db->loadRowList())
 126              {
 127                  $this->setError(
 128                      JText::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED', get_class($this), JText::_('JLIB_DATABASE_ERROR_MENUTYPE_CHECKOUT'))
 129                  );
 130                  return false;
 131              }
 132  
 133              // Update the menu items
 134              $query = $this->_db->getQuery(true);
 135              $query->update('#__menu');
 136              $query->set('menutype=' . $this->_db->quote($this->menutype));
 137              $query->where('menutype=' . $this->_db->quote($table->menutype));
 138              $this->_db->setQuery($query);
 139              if (!$this->_db->query())
 140              {
 141                  $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED', get_class($this), $this->_db->getErrorMsg()));
 142                  return false;
 143              }
 144  
 145              // Update the module items
 146              $query = $this->_db->getQuery(true);
 147              $query->update('#__modules');
 148              $query->set(
 149                  'params=REPLACE(params,' . $this->_db->quote('"menutype":' . json_encode($table->menutype)) . ',' .
 150                  $this->_db->quote('"menutype":' . json_encode($this->menutype)) . ')'
 151              );
 152              $query->where('module=' . $this->_db->quote('mod_menu'));
 153              $query->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'));
 154              $this->_db->setQuery($query);
 155              if (!$this->_db->query())
 156              {
 157                  $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED', get_class($this), $this->_db->getErrorMsg()));
 158                  return false;
 159              }
 160          }
 161          return parent::store($updateNulls);
 162      }
 163  
 164      /**
 165       * Method to delete a row from the database table by primary key value.
 166       *
 167       * @param   mixed  $pk  An optional primary key value to delete.  If not set the instance property value is used.
 168       *
 169       * @return  boolean  True on success.
 170       *
 171       * @link    http://docs.joomla.org/JTable/delete
 172       * @since   11.1
 173       */
 174  	public function delete($pk = null)
 175      {
 176          // Initialise variables.
 177          $k = $this->_tbl_key;
 178          $pk = (is_null($pk)) ? $this->$k : $pk;
 179  
 180          // If no primary key is given, return false.
 181          if ($pk !== null)
 182          {
 183              // Get the user id
 184              $userId = JFactory::getUser()->id;
 185  
 186              // Get the old value of the table
 187              $table = JTable::getInstance('Menutype', 'JTable');
 188              $table->load($pk);
 189  
 190              // Verify that no items are checked out
 191              $query = $this->_db->getQuery(true);
 192              $query->select('id');
 193              $query->from('#__menu');
 194              $query->where('menutype=' . $this->_db->quote($table->menutype));
 195              $query->where('client_id=0');
 196              $query->where('(checked_out NOT IN (0,' . (int) $userId . ') OR home=1 AND language=' . $this->_db->quote('*') . ')');
 197              $this->_db->setQuery($query);
 198              if ($this->_db->loadRowList())
 199              {
 200                  $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_DELETE_FAILED', get_class($this), JText::_('JLIB_DATABASE_ERROR_MENUTYPE')));
 201                  return false;
 202              }
 203  
 204              // Verify that no module for this menu are checked out
 205              $query = $this->_db->getQuery(true);
 206              $query->select('id');
 207              $query->from('#__modules');
 208              $query->where('module=' . $this->_db->quote('mod_menu'));
 209              $query->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'));
 210              $query->where('checked_out !=' . (int) $userId);
 211              $query->where('checked_out !=0');
 212              $this->_db->setQuery($query);
 213              if ($this->_db->loadRowList())
 214              {
 215                  $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_DELETE_FAILED', get_class($this), JText::_('JLIB_DATABASE_ERROR_MENUTYPE')));
 216                  return false;
 217              }
 218  
 219              // Delete the menu items
 220              $query = $this->_db->getQuery(true);
 221              $query->delete();
 222              $query->from('#__menu');
 223              $query->where('menutype=' . $this->_db->quote($table->menutype));
 224              $query->where('client_id=0');
 225              $this->_db->setQuery($query);
 226              if (!$this->_db->query())
 227              {
 228                  $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_DELETE_FAILED', get_class($this), $this->_db->getErrorMsg()));
 229                  return false;
 230              }
 231  
 232              // Update the module items
 233              $query = $this->_db->getQuery(true);
 234              $query->delete();
 235              $query->from('#__modules');
 236              $query->where('module=' . $this->_db->quote('mod_menu'));
 237              $query->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'));
 238              $this->_db->setQuery($query);
 239              if (!$this->_db->query())
 240              {
 241                  $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_DELETE_FAILED', get_class($this), $this->_db->getErrorMsg()));
 242                  return false;
 243              }
 244          }
 245          return parent::delete($pk);
 246      }
 247  }


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