Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
menu.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 table
14  *
15  * @package Joomla.Legacy
16  * @subpackage Table
17  * @since 11.1
18  */
19 class JTableMenu extends JTableNested
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', 'id', $db);
31 
32  // Set the default access level.
33  $this->access = (int) JFactory::getConfig()->get('access');
34  }
35 
36  /**
37  * Overloaded bind function
38  *
39  * @param array $array Named array
40  * @param mixed $ignore An optional array or space separated list of properties to ignore while binding.
41  *
42  * @return mixed Null if operation was satisfactory, otherwise returns an error
43  *
44  * @see JTable::bind()
45  * @since 11.1
46  */
47  public function bind($array, $ignore = '')
48  {
49  // Verify that the default home menu is not unset
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  // Verify that the default home menu set to "all" languages" is not unset
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  // Verify that the default home menu is not unpublished
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 JRegistry;
76  $registry->loadArray($array['params']);
77  $array['params'] = (string) $registry;
78  }
79 
80  return parent::bind($array, $ignore);
81  }
82 
83  /**
84  * Overloaded check function
85  *
86  * @return boolean True on success
87  *
88  * @see JTable::check()
89  * @since 11.1
90  */
91  public function check()
92  {
93  // Set correct component id to ensure proper 404 messages with separator items
94  if ($this->type == "separator")
95  {
96  $this->component_id = 0;
97  }
98 
99  // If the alias field is empty, set it to the title.
100  $this->alias = trim($this->alias);
101 
102  if ((empty($this->alias)) && ($this->type != 'alias' && $this->type != 'url'))
103  {
104  $this->alias = $this->title;
105  }
106 
107  // Make the alias URL safe.
108  $this->alias = JApplication::stringURLSafe($this->alias);
109 
110  if (trim(str_replace('-', '', $this->alias)) == '')
111  {
112  $this->alias = JFactory::getDate()->format('Y-m-d-H-i-s');
113  }
114 
115  // Cast the home property to an int for checking.
116  $this->home = (int) $this->home;
117 
118  // Verify that a first level menu item alias is not 'component'.
119  if ($this->parent_id == 1 && $this->alias == 'component')
120  {
121  $this->setError(JText::_('JLIB_DATABASE_ERROR_MENU_ROOT_ALIAS_COMPONENT'));
122 
123  return false;
124  }
125 
126  // Verify that a first level menu item alias is not the name of a folder.
127  jimport('joomla.filesystem.folder');
128 
129  if ($this->parent_id == 1 && in_array($this->alias, JFolder::folders(JPATH_ROOT)))
130  {
131  $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_MENU_ROOT_ALIAS_FOLDER', $this->alias, $this->alias));
132 
133  return false;
134  }
135 
136  // Verify that the home item a component.
137  if ($this->home && $this->type != 'component')
138  {
139  $this->setError(JText::_('JLIB_DATABASE_ERROR_MENU_HOME_NOT_COMPONENT'));
140 
141  return false;
142  }
143 
144  return true;
145  }
146 
147  /**
148  * Overloaded store function
149  *
150  * @param boolean $updateNulls True to update fields even if they are null.
151  *
152  * @return mixed False on failure, positive integer on success.
153  *
154  * @see JTable::store()
155  * @since 11.1
156  */
157  public function store($updateNulls = false)
158  {
159  $db = JFactory::getDbo();
160 
161  // Verify that the alias is unique
162  $table = JTable::getInstance('Menu', 'JTable', array('dbo' => $this->getDbo()));
163 
164  if ($table->load(array('alias' => $this->alias, 'parent_id' => $this->parent_id, 'client_id' => (int) $this->client_id, 'language' => $this->language))
165  && ($table->id != $this->id || $this->id == 0))
166  {
167  if ($this->menutype == $table->menutype)
168  {
169  $this->setError(JText::_('JLIB_DATABASE_ERROR_MENU_UNIQUE_ALIAS'));
170  }
171  else
172  {
173  $this->setError(JText::_('JLIB_DATABASE_ERROR_MENU_UNIQUE_ALIAS_ROOT'));
174  }
175 
176  return false;
177  }
178 
179  // Verify that the home page for this language is unique
180  if ($this->home == '1')
181  {
182  $table = JTable::getInstance('Menu', 'JTable', array('dbo' => $this->getDbo()));
183 
184  if ($table->load(array('home' => '1', 'language' => $this->language)))
185  {
186  if ($table->checked_out && $table->checked_out != $this->checked_out)
187  {
188  $this->setError(JText::_('JLIB_DATABASE_ERROR_MENU_DEFAULT_CHECKIN_USER_MISMATCH'));
189 
190  return false;
191  }
192 
193  $table->home = 0;
194  $table->checked_out = 0;
195  $table->checked_out_time = $db->getNullDate();
196  $table->store();
197  }
198 
199  // Verify that the home page for this menu is unique.
200  if ($table->load(array('home' => '1', 'menutype' => $this->menutype)) && ($table->id != $this->id || $this->id == 0))
201  {
202  $this->setError(JText::_('JLIB_DATABASE_ERROR_MENU_HOME_NOT_UNIQUE_IN_MENU'));
203 
204  return false;
205  }
206  }
207 
208  if (!parent::store($updateNulls))
209  {
210  return false;
211  }
212 
213  // Get the new path in case the node was moved
214  $pathNodes = $this->getPath();
215  $segments = array();
216 
217  foreach ($pathNodes as $node)
218  {
219  // Don't include root in path
220  if ($node->alias != 'root')
221  {
222  $segments[] = $node->alias;
223  }
224  }
225 
226  $newPath = trim(implode('/', $segments), ' /\\');
227 
228  // Use new path for partial rebuild of table
229  // Rebuild will return positive integer on success, false on failure
230  return ($this->rebuild($this->{$this->_tbl_key}, $this->lft, $this->level, $newPath) > 0);
231  }
232 }