1 <?php
  2   3   4   5   6   7   8 
  9 
 10 defined('JPATH_PLATFORM') or die;
 11 
 12 use Joomla\Registry\Registry;
 13 
 14  15  16  17  18 
 19 class  extends JTableNested
 20 {
 21      22  23  24  25  26  27 
 28     public function __construct(JDatabaseDriver $db)
 29     {
 30         parent::__construct('#__menu', 'id', $db);
 31 
 32         
 33         $this->access = (int) JFactory::getConfig()->get('access');
 34     }
 35 
 36      37  38  39  40  41  42  43  44  45  46 
 47     public function bind($array, $ignore = '')
 48     {
 49         
 50         if ($this->home == '1' && $this->language == '*' && ($array['home'] == '0'))
 51         {
 52             $this->setError(JText::_('JLIB_DATABASE_ERROR_MENU_CANNOT_UNSET_DEFAULT_DEFAULT'));
 53 
 54             return false;
 55         }
 56 
 57         
 58         if ($this->home == '1' && $this->language == '*' && ($array['language'] != '*'))
 59         {
 60             $this->setError(JText::_('JLIB_DATABASE_ERROR_MENU_CANNOT_UNSET_DEFAULT'));
 61 
 62             return false;
 63         }
 64 
 65         
 66         if ($this->home == '1' && $this->language == '*' && $array['published'] != '1')
 67         {
 68             $this->setError(JText::_('JLIB_DATABASE_ERROR_MENU_UNPUBLISH_DEFAULT_HOME'));
 69 
 70             return false;
 71         }
 72 
 73         if (isset($array['params']) && is_array($array['params']))
 74         {
 75             $registry = new Registry($array['params']);
 76             $array['params'] = (string) $registry;
 77         }
 78 
 79         return parent::bind($array, $ignore);
 80     }
 81 
 82      83  84  85  86  87  88  89 
 90     public function check()
 91     {
 92         
 93         if (trim($this->title) == '')
 94         {
 95             $this->setError(JText::_('JLIB_DATABASE_ERROR_MUSTCONTAIN_A_TITLE_MENUITEM'));
 96 
 97             return false;
 98         }
 99 
100         
101         if (trim($this->path) == '')
102         {
103             $this->path = $this->alias;
104         }
105         
106         if (trim($this->params) == '')
107         {
108             $this->params = '{}';
109         }
110         
111         if (trim($this->img) == '')
112         {
113             $this->img = ' ';
114         }
115 
116         
117         $this->home = (int) $this->home;
118 
119         
120         if ($this->home && $this->type != 'component')
121         {
122             $this->setError(JText::_('JLIB_DATABASE_ERROR_MENU_HOME_NOT_COMPONENT'));
123 
124             return false;
125         }
126 
127         return true;
128     }
129 
130     131 132 133 134 135 136 137 138 139 
140     public function store($updateNulls = false)
141     {
142         $db = JFactory::getDbo();
143 
144         
145         $table = JTable::getInstance('Menu', 'JTable', array('dbo' => $this->getDbo()));
146 
147         $originalAlias = trim($this->alias);
148         $this->alias   = !$originalAlias ? $this->title : $originalAlias;
149         $this->alias   = JApplicationHelper::stringURLSafe(trim($this->alias), $this->language);
150 
151         if ($this->parent_id == 1 && $this->client_id == 0)
152         {
153             
154             if ( $this->alias == 'component')
155             {
156                 $this->setError(JText::_('JLIB_DATABASE_ERROR_MENU_ROOT_ALIAS_COMPONENT'));
157 
158                 return false;
159             }
160 
161             
162             jimport('joomla.filesystem.folder');
163 
164             if (in_array($this->alias, JFolder::folders(JPATH_ROOT)))
165             {
166                 $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_MENU_ROOT_ALIAS_FOLDER', $this->alias, $this->alias));
167 
168                 return false;
169             }
170         }
171 
172         
173         if (empty($this->alias))
174         {
175             $this->alias = JFactory::getDate()->format('Y-m-d-H-i-s');
176         }
177         else
178         {
179             $itemSearch = array('alias' => $this->alias, 'parent_id' => $this->parent_id, 'client_id' => (int) $this->client_id);
180             $error      = false;
181 
182             
183             if (JLanguageMultilang::isEnabled() && (int) $this->client_id == 0)
184             {
185                 
186                 if (($table->load(array_replace($itemSearch, array('language' => '*'))) && ($table->id != $this->id || $this->id == 0))
187                     || ($table->load(array_replace($itemSearch, array('language' => $this->language))) && ($table->id != $this->id || $this->id == 0))
188                     || ($this->language === '*' && $this->id == 0 && $table->load($itemSearch)))
189                 {
190                     $error = true;
191                 }
192                 
193                 elseif ($this->language === '*' && $this->id != 0)
194                 {
195                     $query = $db->getQuery(true)
196                         ->select('id')
197                         ->from($db->quoteName('#__menu'))
198                         ->where($db->quoteName('parent_id') . ' = 1')
199                         ->where($db->quoteName('client_id') . ' = 0')
200                         ->where($db->quoteName('id') . ' != ' . (int) $this->id)
201                         ->where($db->quoteName('alias') . ' = ' . $db->quote($this->alias));
202 
203                     $otherMenuItemId = (int) $db->setQuery($query)->loadResult();
204 
205                     if ($otherMenuItemId)
206                     {
207                         $table->load(array('id' => $otherMenuItemId));
208                         $error = true;
209                     }
210                 }
211             }
212             
213             else
214             {
215                 
216                 if ($table->load($itemSearch) && ($table->id != $this->id || $this->id == 0))
217                 {
218                     $error = true;
219                 }
220             }
221 
222             
223             if ($error)
224             {
225                 $menuTypeTable = JTable::getInstance('MenuType', 'JTable', array('dbo' => $this->getDbo()));
226                 $menuTypeTable->load(array('menutype' => $table->menutype));
227                 $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_MENU_UNIQUE_ALIAS', $this->alias, $table->title, $menuTypeTable->title));
228 
229                 return false;
230             }
231         }
232 
233         if ($this->home == '1')
234         {
235             
236             if ($table->load(
237                     array(
238                     'menutype' => $this->menutype,
239                     'client_id' => (int) $this->client_id,
240                     'home' => '1',
241                     )
242                 )
243                 && ($table->language != $this->language))
244             {
245                 $this->setError(JText::_('JLIB_DATABASE_ERROR_MENU_HOME_NOT_UNIQUE_IN_MENU'));
246 
247                 return false;
248             }
249 
250             
251             if ($table->load(array('home' => '1', 'language' => $this->language, 'client_id' => (int) $this->client_id)))
252             {
253                 if ($table->checked_out && $table->checked_out != $this->checked_out)
254                 {
255                     $this->setError(JText::_('JLIB_DATABASE_ERROR_MENU_DEFAULT_CHECKIN_USER_MISMATCH'));
256 
257                     return false;
258                 }
259 
260                 $table->home = 0;
261                 $table->checked_out = 0;
262                 $table->checked_out_time = $db->getNullDate();
263                 $table->store();
264             }
265         }
266 
267         if (!parent::store($updateNulls))
268         {
269             return false;
270         }
271 
272         
273         $pathNodes = $this->getPath();
274         $segments = array();
275 
276         foreach ($pathNodes as $node)
277         {
278             
279             if ($node->alias != 'root')
280             {
281                 $segments[] = $node->alias;
282             }
283         }
284 
285         $newPath = trim(implode('/', $segments), ' /\\');
286 
287         
288         
289         return $this->rebuild($this->{$this->_tbl_key}, $this->lft, $this->level, $newPath) > 0;
290     }
291 }
292