| [ Index ] |
PHP Cross Reference of Joomla 2.5.4 DE |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. 4 * @license GNU General Public License version 2 or later; see LICENSE.txt 5 */ 6 7 // No direct access 8 defined('_JEXEC') or die; 9 10 jimport('joomla.application.component.modelform'); 11 12 /** 13 * Menu Item Model for Menus. 14 * 15 * @package Joomla.Administrator 16 * @subpackage com_menus 17 * @version 1.6 18 */ 19 class MenusModelMenu extends JModelForm 20 { 21 /** 22 * @var string The prefix to use with controller messages. 23 * @since 1.6 24 */ 25 protected $text_prefix = 'COM_MENUS_MENU'; 26 27 /** 28 * Model context string. 29 * 30 * @var string 31 */ 32 protected $_context = 'com_menus.menu'; 33 34 /** 35 * Method to test whether a record can be deleted. 36 * 37 * @param object A record object. 38 * 39 * @return boolean True if allowed to delete the record. Defaults to the permission set in the component. 40 * @since 1.6 41 */ 42 protected function canDelete($record) 43 { 44 $user = JFactory::getUser(); 45 46 return $user->authorise('core.delete', 'com_menus.menu.'.(int) $record->id); 47 } 48 49 /** 50 * Method to test whether a record can be deleted. 51 * 52 * @param object A record object. 53 * 54 * @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component. 55 * @since 1.6 56 */ 57 protected function canEditState($record) 58 { 59 $user = JFactory::getUser(); 60 61 return $user->authorise('core.edit.state', 'com_menus.menu.'.(int) $record->id); 62 } 63 64 /** 65 * Returns a Table object, always creating it 66 * 67 * @param type The table type to instantiate 68 * @param string A prefix for the table class name. Optional. 69 * @param array Configuration array for model. Optional. 70 * @return JTable A database object 71 */ 72 public function getTable($type = 'MenuType', $prefix = 'JTable', $config = array()) 73 { 74 return JTable::getInstance($type, $prefix, $config); 75 } 76 77 /** 78 * Method to auto-populate the model state. 79 * 80 * Note. Calling getState in this method will result in recursion. 81 * 82 * @since 1.6 83 */ 84 protected function populateState() 85 { 86 $app = JFactory::getApplication('administrator'); 87 88 // Load the User state. 89 $id = (int) JRequest::getInt('id'); 90 $this->setState('menu.id', $id); 91 92 // Load the parameters. 93 $params = JComponentHelper::getParams('com_menus'); 94 $this->setState('params', $params); 95 } 96 97 /** 98 * Method to get a menu item. 99 * 100 * @param integer The id of the menu item to get. 101 * 102 * @return mixed Menu item data object on success, false on failure. 103 */ 104 public function &getItem($itemId = null) 105 { 106 // Initialise variables. 107 $itemId = (!empty($itemId)) ? $itemId : (int)$this->getState('menu.id'); 108 $false = false; 109 110 // Get a menu item row instance. 111 $table = $this->getTable(); 112 113 // Attempt to load the row. 114 $return = $table->load($itemId); 115 116 // Check for a table object error. 117 if ($return === false && $table->getError()) { 118 $this->setError($table->getError()); 119 return $false; 120 } 121 122 $properties = $table->getProperties(1); 123 $value = JArrayHelper::toObject($properties, 'JObject'); 124 return $value; 125 } 126 127 /** 128 * Method to get the menu item form. 129 * 130 * @param array $data Data for the form. 131 * @param boolean $loadData True if the form is to load its own data (default case), false if not. 132 * @return JForm A JForm object on success, false on failure 133 * @since 1.6 134 */ 135 public function getForm($data = array(), $loadData = true) 136 { 137 // Get the form. 138 $form = $this->loadForm('com_menus.menu', 'menu', array('control' => 'jform', 'load_data' => $loadData)); 139 if (empty($form)) { 140 return false; 141 } 142 143 return $form; 144 } 145 146 /** 147 * Method to get the data that should be injected in the form. 148 * 149 * @return mixed The data for the form. 150 * @since 1.6 151 */ 152 protected function loadFormData() 153 { 154 // Check the session for previously entered form data. 155 $data = JFactory::getApplication()->getUserState('com_menus.edit.menu.data', array()); 156 157 if (empty($data)) { 158 $data = $this->getItem(); 159 } 160 161 return $data; 162 } 163 164 /** 165 * Method to save the form data. 166 * 167 * @param array The form data. 168 * @return boolean True on success. 169 */ 170 public function save($data) 171 { 172 $id = (!empty($data['id'])) ? $data['id'] : (int)$this->getState('menu.id'); 173 $isNew = true; 174 175 // Get a row instance. 176 $table = $this->getTable(); 177 178 // Load the row if saving an existing item. 179 if ($id > 0) { 180 $table->load($id); 181 $isNew = false; 182 } 183 184 // Bind the data. 185 if (!$table->bind($data)) { 186 $this->setError($table->getError()); 187 return false; 188 } 189 190 // Check the data. 191 if (!$table->check()) { 192 $this->setError($table->getError()); 193 return false; 194 } 195 196 // Store the data. 197 if (!$table->store()) { 198 $this->setError($table->getError()); 199 return false; 200 } 201 202 $this->setState('menu.id', $table->id); 203 204 // Clean the cache 205 $this->cleanCache(); 206 207 return true; 208 } 209 210 /** 211 * Method to delete groups. 212 * 213 * @param array An array of item ids. 214 * @return boolean Returns true on success, false on failure. 215 */ 216 public function delete($itemIds) 217 { 218 // Sanitize the ids. 219 $itemIds = (array) $itemIds; 220 JArrayHelper::toInteger($itemIds); 221 222 // Get a group row instance. 223 $table = $this->getTable(); 224 225 // Iterate the items to delete each one. 226 foreach ($itemIds as $itemId) { 227 // TODO: Delete the menu associations - Menu items and Modules 228 229 if (!$table->delete($itemId)) 230 { 231 $this->setError($table->getError()); 232 return false; 233 } 234 } 235 236 // Clean the cache 237 $this->cleanCache(); 238 239 return true; 240 } 241 242 /** 243 * Gets a list of all mod_mainmenu modules and collates them by menutype 244 * 245 * @return array 246 */ 247 public function &getModules() 248 { 249 $db = $this->getDbo(); 250 251 $query = $db->getQuery(true); 252 $query->from('#__modules as a'); 253 $query->select('a.id, a.title, a.params, a.position'); 254 $query->where('module = '.$db->quote('mod_menu')); 255 $query->select('ag.title AS access_title'); 256 $query->join('LEFT', '#__viewlevels AS ag ON ag.id = a.access'); 257 $db->setQuery($query); 258 259 $modules = $db->loadObjectList(); 260 261 $result = array(); 262 263 foreach ($modules as &$module) { 264 $params = new JRegistry; 265 $params->loadString($module->params); 266 267 $menuType = $params->get('menutype'); 268 if (!isset($result[$menuType])) { 269 $result[$menuType] = array(); 270 } 271 $result[$menuType][] = &$module; 272 } 273 274 return $result; 275 } 276 277 /** 278 * Custom clean cache method 279 * 280 * @since 1.6 281 */ 282 protected function cleanCache($group = null, $client_id = 0) { 283 parent::cleanCache('com_modules'); 284 parent::cleanCache('mod_menu'); 285 } 286 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue Apr 3 11:40:28 2012 | Cross-referenced by PHPXref 0.7.1 |