Source for file menu.php

Documentation is available at menu.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_menus
  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.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Menu Item Model for Menus.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_menus
  17.  * @since       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    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    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 $app->input->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.         $itemId (!empty($itemId)) $itemId : (int) $this->getState('menu.id');
  107.         $false false;
  108.  
  109.         // Get a menu item row instance.
  110.         $table $this->getTable();
  111.  
  112.         // Attempt to load the row.
  113.         $return $table->load($itemId);
  114.  
  115.         // Check for a table object error.
  116.         if ($return === false && $table->getError())
  117.         {
  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.         {
  141.             return false;
  142.         }
  143.  
  144.         return $form;
  145.     }
  146.  
  147.     /**
  148.      * Method to get the data that should be injected in the form.
  149.      *
  150.      * @return  mixed  The data for the form.
  151.      * @since   1.6
  152.      */
  153.     protected function loadFormData()
  154.     {
  155.         // Check the session for previously entered form data.
  156.         $data JFactory::getApplication()->getUserState('com_menus.edit.menu.data'array());
  157.  
  158.         if (empty($data))
  159.         {
  160.             $data $this->getItem();
  161.         }
  162.  
  163.         $this->preprocessData('com_menus.menu'$data);
  164.  
  165.         return $data;
  166.     }
  167.  
  168.     /**
  169.      * Method to save the form data.
  170.      *
  171.      * @param   array  The form data.
  172.      * @return  boolean  True on success.
  173.      */
  174.     public function save($data)
  175.     {
  176.         $id (!empty($data['id'])) $data['id': (int) $this->getState('menu.id');
  177.  
  178.         // Get a row instance.
  179.         $table $this->getTable();
  180.  
  181.         // Load the row if saving an existing item.
  182.         if ($id 0)
  183.         {
  184.             $table->load($id);
  185.         }
  186.  
  187.         // Bind the data.
  188.         if (!$table->bind($data))
  189.         {
  190.             $this->setError($table->getError());
  191.             return false;
  192.         }
  193.  
  194.         // Check the data.
  195.         if (!$table->check())
  196.         {
  197.             $this->setError($table->getError());
  198.             return false;
  199.         }
  200.  
  201.         // Store the data.
  202.         if (!$table->store())
  203.         {
  204.             $this->setError($table->getError());
  205.             return false;
  206.         }
  207.  
  208.         $this->setState('menu.id'$table->id);
  209.  
  210.         // Clean the cache
  211.         $this->cleanCache();
  212.  
  213.         return true;
  214.     }
  215.  
  216.     /**
  217.      * Method to delete groups.
  218.      *
  219.      * @param   array  An array of item ids.
  220.      * @return  boolean  Returns true on success, false on failure.
  221.      */
  222.     public function delete($itemIds)
  223.     {
  224.         // Sanitize the ids.
  225.         $itemIds = (array) $itemIds;
  226.         JArrayHelper::toInteger($itemIds);
  227.  
  228.         // Get a group row instance.
  229.         $table $this->getTable();
  230.  
  231.         // Iterate the items to delete each one.
  232.         foreach ($itemIds as $itemId)
  233.         {
  234.             // TODO: Delete the menu associations - Menu items and Modules
  235.  
  236.             if (!$table->delete($itemId))
  237.             {
  238.                 $this->setError($table->getError());
  239.                 return false;
  240.             }
  241.         }
  242.  
  243.         // Clean the cache
  244.         $this->cleanCache();
  245.  
  246.         return true;
  247.     }
  248.  
  249.     /**
  250.      * Gets a list of all mod_mainmenu modules and collates them by menutype
  251.      *
  252.      * @return  array 
  253.      */
  254.     public function &getModules()
  255.     {
  256.         $db $this->getDbo();
  257.  
  258.         $query $db->getQuery(true)
  259.             ->from('#__modules as a')
  260.             ->select('a.id, a.title, a.params, a.position')
  261.             ->where('module = ' $db->quote('mod_menu'))
  262.             ->select('ag.title AS access_title')
  263.             ->join('LEFT''#__viewlevels AS ag ON ag.id = a.access');
  264.         $db->setQuery($query);
  265.  
  266.         $modules $db->loadObjectList();
  267.  
  268.         $result array();
  269.  
  270.         foreach ($modules as &$module)
  271.         {
  272.             $params new JRegistry;
  273.             $params->loadString($module->params);
  274.  
  275.             $menuType $params->get('menutype');
  276.             if (!isset($result[$menuType]))
  277.             {
  278.                 $result[$menuTypearray();
  279.             }
  280.             $result[$menuType][$module;
  281.         }
  282.  
  283.         return $result;
  284.     }
  285.  
  286.     /**
  287.      * Custom clean cache method
  288.      *
  289.      * @since   1.6
  290.      */
  291.     protected function cleanCache($group null$client_id 0)
  292.     {
  293.         parent::cleanCache('com_modules');
  294.         parent::cleanCache('mod_menu');
  295.     }
  296. }

Documentation generated on Tue, 19 Nov 2013 15:07:53 +0100 by phpDocumentor 1.4.3