1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 13 14 15 16
17 class extends JTable
18 {
19 20 21 22 23 24 25
26 public function __construct(JDatabaseDriver $db)
27 {
28 parent::__construct('#__menu_types', 'id', $db);
29 }
30
31 32 33 34 35 36 37 38
39 public function check()
40 {
41 $this->menutype = JApplicationHelper::stringURLSafe($this->menutype);
42
43 if (empty($this->menutype))
44 {
45 $this->setError(JText::_('JLIB_DATABASE_ERROR_MENUTYPE_EMPTY'));
46
47 return false;
48 }
49
50
51 if (trim($this->title) == '')
52 {
53 $this->title = $this->menutype;
54 }
55
56
57 $query = $this->_db->getQuery(true)
58 ->select('COUNT(id)')
59 ->from($this->_db->quoteName('#__menu_types'))
60 ->where($this->_db->quoteName('menutype') . ' = ' . $this->_db->quote($this->menutype))
61 ->where($this->_db->quoteName('id') . ' <> ' . (int) $this->id);
62 $this->_db->setQuery($query);
63
64 if ($this->_db->loadResult())
65 {
66 $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_MENUTYPE_EXISTS', $this->menutype));
67
68 return false;
69 }
70
71 return true;
72 }
73
74 75 76 77 78 79 80 81 82 83 84 85
86 public function store($updateNulls = false)
87 {
88 if ($this->id)
89 {
90
91 $userId = JFactory::getUser()->id;
92
93
94 $table = JTable::getInstance('Menutype', 'JTable', array('dbo' => $this->getDbo()));
95 $table->load($this->id);
96
97
98 $query = $this->_db->getQuery(true)
99 ->select('id')
100 ->from('#__menu')
101 ->where('menutype=' . $this->_db->quote($table->menutype))
102 ->where('checked_out !=' . (int) $userId)
103 ->where('checked_out !=0');
104 $this->_db->setQuery($query);
105
106 if ($this->_db->loadRowList())
107 {
108 $this->setError(
109 JText::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED', get_class($this), JText::_('JLIB_DATABASE_ERROR_MENUTYPE_CHECKOUT'))
110 );
111
112 return false;
113 }
114
115
116 $query->clear()
117 ->select('id')
118 ->from('#__modules')
119 ->where('module=' . $this->_db->quote('mod_menu'))
120 ->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'))
121 ->where('checked_out !=' . (int) $userId)
122 ->where('checked_out !=0');
123 $this->_db->setQuery($query);
124
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
131 return false;
132 }
133
134
135 $query->clear()
136 ->update('#__menu')
137 ->set('menutype=' . $this->_db->quote($this->menutype))
138 ->where('menutype=' . $this->_db->quote($table->menutype));
139 $this->_db->setQuery($query);
140 $this->_db->execute();
141
142
143 $query->clear()
144 ->update('#__modules')
145 ->set(
146 'params=REPLACE(params,' . $this->_db->quote('"menutype":' . json_encode($table->menutype)) . ',' .
147 $this->_db->quote('"menutype":' . json_encode($this->menutype)) . ')'
148 );
149 $query->where('module=' . $this->_db->quote('mod_menu'))
150 ->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'));
151 $this->_db->setQuery($query);
152 $this->_db->execute();
153 }
154
155 return parent::store($updateNulls);
156 }
157
158 159 160 161 162 163 164 165 166
167 public function delete($pk = null)
168 {
169 $k = $this->_tbl_key;
170 $pk = is_null($pk) ? $this->$k : $pk;
171
172
173 if ($pk !== null)
174 {
175
176 $userId = JFactory::getUser()->id;
177
178
179 $table = JTable::getInstance('Menutype', 'JTable', array('dbo' => $this->getDbo()));
180 $table->load($pk);
181
182
183 $query = $this->_db->getQuery(true)
184 ->select('id')
185 ->from('#__menu')
186 ->where('menutype=' . $this->_db->quote($table->menutype))
187 ->where('(checked_out NOT IN (0,' . (int) $userId . ') OR home=1 AND language=' . $this->_db->quote('*') . ')');
188 $this->_db->setQuery($query);
189
190 if ($this->_db->loadRowList())
191 {
192 $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_DELETE_FAILED', get_class($this), JText::_('JLIB_DATABASE_ERROR_MENUTYPE')));
193
194 return false;
195 }
196
197
198 $query->clear()
199 ->select('id')
200 ->from('#__modules')
201 ->where('module=' . $this->_db->quote('mod_menu'))
202 ->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'))
203 ->where('checked_out !=' . (int) $userId)
204 ->where('checked_out !=0');
205 $this->_db->setQuery($query);
206
207 if ($this->_db->loadRowList())
208 {
209 $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_DELETE_FAILED', get_class($this), JText::_('JLIB_DATABASE_ERROR_MENUTYPE')));
210
211 return false;
212 }
213
214
215 $query->clear()
216 ->delete('#__menu')
217 ->where('menutype=' . $this->_db->quote($table->menutype));
218 $this->_db->setQuery($query);
219 $this->_db->execute();
220
221
222 $query->clear()
223 ->delete('#__modules')
224 ->where('module=' . $this->_db->quote('mod_menu'))
225 ->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'));
226 $this->_db->setQuery($query);
227 $this->_db->execute();
228 }
229
230 return parent::delete($pk);
231 }
232
233 234 235 236 237 238 239 240 241
242 protected function _getAssetName()
243 {
244 return 'com_menus.menu.' . $this->id;
245 }
246
247 248 249 250 251 252 253
254 protected function _getAssetTitle()
255 {
256 return $this->title;
257 }
258
259 260 261 262 263 264 265 266 267 268 269 270 271 272
273 protected function _getAssetParentId(JTable $table = null, $id = null)
274 {
275 $assetId = null;
276 $asset = JTable::getInstance('asset');
277
278 if ($asset->loadByName('com_menus'))
279 {
280 $assetId = $asset->id;
281 }
282
283 return is_null($assetId) ? parent::_getAssetParentId($table, $id) : $assetId;
284 }
285 }
286