Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
type.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Legacy
4  * @subpackage Table
5  *
6  * @copyright Copyright (C) 2005 - 2013 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 /**
13  * Menu Types table
14  *
15  * @package Joomla.Legacy
16  * @subpackage Table
17  * @since 11.1
18  */
19 class JTableMenuType extends JTable
20 {
21  /**
22  * Constructor
23  *
24  * @param JDatabaseDriver $db Database driver object.
25  *
26  * @since 11.1
27  */
28  public function __construct(JDatabaseDriver $db)
29  {
30  parent::__construct('#__menu_types', 'id', $db);
31  }
32 
33  /**
34  * Overloaded check function
35  *
36  * @return boolean True on success, false on failure
37  *
38  * @see JTable::check()
39  * @since 11.1
40  */
41  public function check()
42  {
43  $this->menutype = JApplication::stringURLSafe($this->menutype);
44 
45  if (empty($this->menutype))
46  {
47  $this->setError(JText::_('JLIB_DATABASE_ERROR_MENUTYPE_EMPTY'));
48 
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  ->select('COUNT(id)')
61  ->from($this->_db->quoteName('#__menu_types'))
62  ->where($this->_db->quoteName('menutype') . ' = ' . $this->_db->quote($this->menutype))
63  ->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 
70  return false;
71  }
72 
73  return true;
74  }
75 
76  /**
77  * Method to store a row in the database from the JTable instance properties.
78  * If a primary key value is set the row with that primary key value will be
79  * updated with the instance property values. If no primary key value is set
80  * a new row will be inserted into the database with the properties from the
81  * JTable instance.
82  *
83  * @param boolean $updateNulls True to update fields even if they are null.
84  *
85  * @return boolean True on success.
86  *
87  * @link http://docs.joomla.org/JTable/store
88  * @since 11.1
89  */
90  public function store($updateNulls = false)
91  {
92  if ($this->id)
93  {
94  // Get the user id
95  $userId = JFactory::getUser()->id;
96 
97  // Get the old value of the table
98  $table = JTable::getInstance('Menutype', 'JTable');
99  $table->load($this->id);
100 
101  // Verify that no items are checked out
102  $query = $this->_db->getQuery(true)
103  ->select('id')
104  ->from('#__menu')
105  ->where('menutype=' . $this->_db->quote($table->menutype))
106  ->where('checked_out !=' . (int) $userId)
107  ->where('checked_out !=0');
108  $this->_db->setQuery($query);
109 
110  if ($this->_db->loadRowList())
111  {
112  $this->setError(
113  JText::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED', get_class($this), JText::_('JLIB_DATABASE_ERROR_MENUTYPE_CHECKOUT'))
114  );
115 
116  return false;
117  }
118 
119  // Verify that no module for this menu are checked out
120  $query->clear()
121  ->select('id')
122  ->from('#__modules')
123  ->where('module=' . $this->_db->quote('mod_menu'))
124  ->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'))
125  ->where('checked_out !=' . (int) $userId)
126  ->where('checked_out !=0');
127  $this->_db->setQuery($query);
128 
129  if ($this->_db->loadRowList())
130  {
131  $this->setError(
132  JText::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED', get_class($this), JText::_('JLIB_DATABASE_ERROR_MENUTYPE_CHECKOUT'))
133  );
134 
135  return false;
136  }
137 
138  // Update the menu items
139  $query->clear()
140  ->update('#__menu')
141  ->set('menutype=' . $this->_db->quote($this->menutype))
142  ->where('menutype=' . $this->_db->quote($table->menutype));
143  $this->_db->setQuery($query);
144  $this->_db->execute();
145 
146  // Update the module items
147  $query->clear()
148  ->update('#__modules')
149  ->set(
150  'params=REPLACE(params,' . $this->_db->quote('"menutype":' . json_encode($table->menutype)) . ',' .
151  $this->_db->quote('"menutype":' . json_encode($this->menutype)) . ')'
152  );
153  $query->where('module=' . $this->_db->quote('mod_menu'))
154  ->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'));
155  $this->_db->setQuery($query);
156  $this->_db->execute();
157  }
158 
159  return parent::store($updateNulls);
160  }
161 
162  /**
163  * Method to delete a row from the database table by primary key value.
164  *
165  * @param mixed $pk An optional primary key value to delete. If not set the instance property value is used.
166  *
167  * @return boolean True on success.
168  *
169  * @link http://docs.joomla.org/JTable/delete
170  * @since 11.1
171  */
172  public function delete($pk = null)
173  {
174  $k = $this->_tbl_key;
175  $pk = (is_null($pk)) ? $this->$k : $pk;
176 
177  // If no primary key is given, return false.
178  if ($pk !== null)
179  {
180  // Get the user id
181  $userId = JFactory::getUser()->id;
182 
183  // Get the old value of the table
184  $table = JTable::getInstance('Menutype', 'JTable');
185  $table->load($pk);
186 
187  // Verify that no items are checked out
188  $query = $this->_db->getQuery(true)
189  ->select('id')
190  ->from('#__menu')
191  ->where('menutype=' . $this->_db->quote($table->menutype))
192  ->where('client_id=0')
193  ->where('(checked_out NOT IN (0,' . (int) $userId . ') OR home=1 AND language=' . $this->_db->quote('*') . ')');
194  $this->_db->setQuery($query);
195 
196  if ($this->_db->loadRowList())
197  {
198  $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_DELETE_FAILED', get_class($this), JText::_('JLIB_DATABASE_ERROR_MENUTYPE')));
199 
200  return false;
201  }
202 
203  // Verify that no module for this menu are checked out
204  $query->clear()
205  ->select('id')
206  ->from('#__modules')
207  ->where('module=' . $this->_db->quote('mod_menu'))
208  ->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'))
209  ->where('checked_out !=' . (int) $userId)
210  ->where('checked_out !=0');
211  $this->_db->setQuery($query);
212 
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 
217  return false;
218  }
219 
220  // Delete the menu items
221  $query->clear()
222  ->delete('#__menu')
223  ->where('menutype=' . $this->_db->quote($table->menutype))
224  ->where('client_id=0');
225  $this->_db->setQuery($query);
226  $this->_db->execute();
227 
228  // Update the module items
229  $query->clear()
230  ->delete('#__modules')
231  ->where('module=' . $this->_db->quote('mod_menu'))
232  ->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'));
233  $this->_db->setQuery($query);
234  $this->_db->execute();
235  }
236 
237  return parent::delete($pk);
238  }
239 }