[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package Joomla.Administrator 5 * @subpackage com_menus 6 * 7 * @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org> 8 * @license GNU General Public License version 2 or later; see LICENSE.txt 9 */ 10 11 namespace Joomla\Component\Menus\Administrator\Model; 12 13 use Joomla\CMS\Component\ComponentHelper; 14 use Joomla\CMS\Factory; 15 use Joomla\CMS\Form\Form; 16 use Joomla\CMS\MVC\Model\FormModel; 17 use Joomla\CMS\Object\CMSObject; 18 use Joomla\CMS\Plugin\PluginHelper; 19 use Joomla\CMS\Table\Table; 20 use Joomla\Registry\Registry; 21 use Joomla\Utilities\ArrayHelper; 22 23 // phpcs:disable PSR1.Files.SideEffects 24 \defined('_JEXEC') or die; 25 // phpcs:enable PSR1.Files.SideEffects 26 27 /** 28 * Menu Item Model for Menus. 29 * 30 * @since 1.6 31 */ 32 class MenuModel extends FormModel 33 { 34 /** 35 * The prefix to use with controller messages. 36 * 37 * @var string 38 * @since 1.6 39 */ 40 protected $text_prefix = 'COM_MENUS_MENU'; 41 42 /** 43 * Model context string. 44 * 45 * @var string 46 */ 47 protected $_context = 'com_menus.menu'; 48 49 /** 50 * Method to test whether a record can be deleted. 51 * 52 * @param object $record A record object. 53 * 54 * @return boolean True if allowed to delete the record. Defaults to the permission set in the component. 55 * 56 * @since 1.6 57 */ 58 protected function canDelete($record) 59 { 60 return Factory::getUser()->authorise('core.delete', 'com_menus.menu.' . (int) $record->id); 61 } 62 63 /** 64 * Method to test whether the state of a record can be edited. 65 * 66 * @param object $record A record object. 67 * 68 * @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component. 69 * 70 * @since 1.6 71 */ 72 protected function canEditState($record) 73 { 74 return Factory::getUser()->authorise('core.edit.state', 'com_menus.menu.' . (int) $record->id); 75 } 76 77 /** 78 * Returns a Table object, always creating it 79 * 80 * @param string $type The table type to instantiate 81 * @param string $prefix A prefix for the table class name. Optional. 82 * @param array $config Configuration array for model. Optional. 83 * 84 * @return Table A database object 85 * 86 * @since 1.6 87 */ 88 public function getTable($type = 'MenuType', $prefix = '\JTable', $config = array()) 89 { 90 return Table::getInstance($type, $prefix, $config); 91 } 92 93 /** 94 * Auto-populate the model state. 95 * 96 * Note. Calling getState in this method will result in recursion. 97 * 98 * @return void 99 * 100 * @since 1.6 101 */ 102 protected function populateState() 103 { 104 $app = Factory::getApplication(); 105 106 // Load the User state. 107 $id = $app->input->getInt('id'); 108 $this->setState('menu.id', $id); 109 110 // Load the parameters. 111 $params = ComponentHelper::getParams('com_menus'); 112 $this->setState('params', $params); 113 114 // Load the clientId. 115 $clientId = $app->getUserStateFromRequest('com_menus.menus.client_id', 'client_id', 0, 'int'); 116 $this->setState('client_id', $clientId); 117 } 118 119 /** 120 * Method to get a menu item. 121 * 122 * @param integer $itemId The id of the menu item to get. 123 * 124 * @return mixed Menu item data object on success, false on failure. 125 * 126 * @since 1.6 127 */ 128 public function &getItem($itemId = null) 129 { 130 $itemId = (!empty($itemId)) ? $itemId : (int) $this->getState('menu.id'); 131 132 // Get a menu item row instance. 133 $table = $this->getTable(); 134 135 // Attempt to load the row. 136 $return = $table->load($itemId); 137 138 // Check for a table object error. 139 if ($return === false && $table->getError()) { 140 $this->setError($table->getError()); 141 142 return false; 143 } 144 145 $properties = $table->getProperties(1); 146 $value = ArrayHelper::toObject($properties, CMSObject::class); 147 148 return $value; 149 } 150 151 /** 152 * Method to get the menu item form. 153 * 154 * @param array $data Data for the form. 155 * @param boolean $loadData True if the form is to load its own data (default case), false if not. 156 * 157 * @return Form|boolean A Form object on success, false on failure 158 * 159 * @since 1.6 160 */ 161 public function getForm($data = array(), $loadData = true) 162 { 163 // Get the form. 164 $form = $this->loadForm('com_menus.menu', 'menu', array('control' => 'jform', 'load_data' => $loadData)); 165 166 if (empty($form)) { 167 return false; 168 } 169 170 if (!$this->getState('client_id', 0)) { 171 $form->removeField('preset'); 172 } 173 174 return $form; 175 } 176 177 /** 178 * Method to get the data that should be injected in the form. 179 * 180 * @return mixed The data for the form. 181 * 182 * @since 1.6 183 */ 184 protected function loadFormData() 185 { 186 // Check the session for previously entered form data. 187 $data = Factory::getApplication()->getUserState('com_menus.edit.menu.data', array()); 188 189 if (empty($data)) { 190 $data = $this->getItem(); 191 192 if (empty($data->id)) { 193 $data->client_id = $this->state->get('client_id', 0); 194 } 195 } else { 196 unset($data['preset']); 197 } 198 199 $this->preprocessData('com_menus.menu', $data); 200 201 return $data; 202 } 203 204 /** 205 * Method to validate the form data. 206 * 207 * @param Form $form The form to validate against. 208 * @param array $data The data to validate. 209 * @param string $group The name of the field group to validate. 210 * 211 * @return array|boolean Array of filtered data if valid, false otherwise. 212 * 213 * @see JFormRule 214 * @see JFilterInput 215 * @since 3.9.23 216 */ 217 public function validate($form, $data, $group = null) 218 { 219 if (!Factory::getUser()->authorise('core.admin', 'com_menus')) { 220 if (isset($data['rules'])) { 221 unset($data['rules']); 222 } 223 } 224 225 return parent::validate($form, $data, $group); 226 } 227 228 /** 229 * Method to save the form data. 230 * 231 * @param array $data The form data. 232 * 233 * @return boolean True on success. 234 * 235 * @since 1.6 236 */ 237 public function save($data) 238 { 239 $id = (!empty($data['id'])) ? $data['id'] : (int) $this->getState('menu.id'); 240 $isNew = true; 241 242 // Get a row instance. 243 $table = $this->getTable(); 244 245 // Include the plugins for the save events. 246 PluginHelper::importPlugin('content'); 247 248 // Load the row if saving an existing item. 249 if ($id > 0) { 250 $isNew = false; 251 $table->load($id); 252 } 253 254 // Bind the data. 255 if (!$table->bind($data)) { 256 $this->setError($table->getError()); 257 258 return false; 259 } 260 261 // Check the data. 262 if (!$table->check()) { 263 $this->setError($table->getError()); 264 265 return false; 266 } 267 268 // Trigger the before event. 269 $result = Factory::getApplication()->triggerEvent('onContentBeforeSave', array($this->_context, &$table, $isNew, $data)); 270 271 // Store the data. 272 if (in_array(false, $result, true) || !$table->store()) { 273 $this->setError($table->getError()); 274 275 return false; 276 } 277 278 // Trigger the after save event. 279 Factory::getApplication()->triggerEvent('onContentAfterSave', array($this->_context, &$table, $isNew)); 280 281 $this->setState('menu.id', $table->id); 282 283 // Clean the cache 284 $this->cleanCache(); 285 286 return true; 287 } 288 289 /** 290 * Method to delete groups. 291 * 292 * @param array $itemIds An array of item ids. 293 * 294 * @return boolean Returns true on success, false on failure. 295 * 296 * @since 1.6 297 */ 298 public function delete($itemIds) 299 { 300 // Sanitize the ids. 301 $itemIds = ArrayHelper::toInteger((array) $itemIds); 302 303 // Get a group row instance. 304 $table = $this->getTable(); 305 306 // Include the plugins for the delete events. 307 PluginHelper::importPlugin('content'); 308 309 // Iterate the items to delete each one. 310 foreach ($itemIds as $itemId) { 311 if ($table->load($itemId)) { 312 // Trigger the before delete event. 313 $result = Factory::getApplication()->triggerEvent('onContentBeforeDelete', array($this->_context, $table)); 314 315 if (in_array(false, $result, true) || !$table->delete($itemId)) { 316 $this->setError($table->getError()); 317 318 return false; 319 } 320 321 // Trigger the after delete event. 322 Factory::getApplication()->triggerEvent('onContentAfterDelete', array($this->_context, $table)); 323 324 // @todo: Delete the menu associations - Menu items and Modules 325 } 326 } 327 328 // Clean the cache 329 $this->cleanCache(); 330 331 return true; 332 } 333 334 /** 335 * Gets a list of all mod_mainmenu modules and collates them by menutype 336 * 337 * @return array 338 * 339 * @since 1.6 340 */ 341 public function &getModules() 342 { 343 $db = $this->getDatabase(); 344 345 $query = $db->getQuery(true) 346 ->select( 347 [ 348 $db->quoteName('a.id'), 349 $db->quoteName('a.title'), 350 $db->quoteName('a.params'), 351 $db->quoteName('a.position'), 352 $db->quoteName('ag.title', 'access_title'), 353 ] 354 ) 355 ->from($db->quoteName('#__modules', 'a')) 356 ->join('LEFT', $db->quoteName('#__viewlevels', 'ag'), $db->quoteName('ag.id') . ' = ' . $db->quoteName('a.access')) 357 ->where($db->quoteName('a.module') . ' = ' . $db->quote('mod_menu')); 358 $db->setQuery($query); 359 360 $modules = $db->loadObjectList(); 361 362 $result = array(); 363 364 foreach ($modules as &$module) { 365 $params = new Registry($module->params); 366 367 $menuType = $params->get('menutype'); 368 369 if (!isset($result[$menuType])) { 370 $result[$menuType] = array(); 371 } 372 373 $result[$menuType][] = & $module; 374 } 375 376 return $result; 377 } 378 379 /** 380 * Returns the extension elements for the given items 381 * 382 * @param array $itemIds The item ids 383 * 384 * @return array 385 * 386 * @since 4.2.0 387 */ 388 public function getExtensionElementsForMenuItems(array $itemIds): array 389 { 390 $db = $this->getDatabase(); 391 $query = $db->getQuery(true); 392 393 $query 394 ->select($db->quoteName('e.element')) 395 ->from($db->quoteName('#__extensions', 'e')) 396 ->join('INNER', $db->quoteName('#__menu', 'm'), $db->quoteName('m.component_id') . ' = ' . $db->quoteName('e.extension_id')) 397 ->whereIn($db->quoteName('m.id'), ArrayHelper::toInteger($itemIds)); 398 399 return $db->setQuery($query)->loadColumn(); 400 } 401 402 /** 403 * Custom clean the cache 404 * 405 * @param string $group Cache group name. 406 * @param integer $clientId @deprecated 5.0 No Longer used. 407 * 408 * @return void 409 * 410 * @since 1.6 411 */ 412 protected function cleanCache($group = null, $clientId = 0) 413 { 414 parent::cleanCache('com_menus'); 415 parent::cleanCache('com_modules'); 416 parent::cleanCache('mod_menu'); 417 } 418 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |