[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/administrator/components/com_menus/models/ -> menutypes.php (source)

   1  <?php
   2  /**
   3   * @copyright    Copyright (C) 2005 - 2010 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 Types Model for Menus.
  14   *
  15   * @package        Joomla.Administrator
  16   * @subpackage    com_menus
  17   * @version        1.6
  18   */
  19  class MenusModelMenutypes extends JModel
  20  {
  21      /**
  22       * A reverse lookup of the base link URL to Title
  23       *
  24       * @var    array
  25       */
  26      protected $rlu = array();
  27  
  28      /**
  29       * Method to get the reverse lookup of the base link URL to Title
  30       *
  31       * @return    array    Array of reverse lookup of the base link URL to Title
  32       * @since    1.6
  33       */
  34  	public function getReverseLookup()
  35      {
  36          if (empty($this->rlu)) {
  37              $this->getTypeOptions();
  38          }
  39          return $this->rlu;
  40      }
  41  
  42      /**
  43       * Method to get the available menu item type options.
  44       *
  45       * @return    array    Array of groups with menu item types.
  46       * @since    1.6
  47       */
  48  	public function getTypeOptions()
  49      {
  50          jimport('joomla.filesystem.file');
  51  
  52          // Initialise variables.
  53          $lang = JFactory::getLanguage();
  54          $list = array();
  55  
  56          // Get the list of components.
  57          $db = JFactory::getDBO();
  58          $query = $db->getQuery(true);
  59          $query->select('name, element AS ' . $db->qn('option'));
  60          $query->from('#__extensions');
  61          $query->where('type = ' . $db->q('component'));
  62          $query->where('enabled = 1');
  63          $query->order('name ASC');
  64          $db->setQuery($query);
  65          $components = $db->loadObjectList();
  66  
  67          foreach ($components as $component)
  68          {
  69              if ($options = $this->getTypeOptionsByComponent($component->option)) {
  70                  $list[$component->name] = $options;
  71  
  72                  // Create the reverse lookup for link-to-name.
  73                  foreach ($options as $option)
  74                  {
  75                      if (isset($option->request)) {
  76                          $this->rlu[MenusHelper::getLinkKey($option->request)] = $option->get('title');
  77  
  78                          if (isset($option->request['option'])) {
  79                                  $lang->load($option->request['option'].'.sys', JPATH_ADMINISTRATOR, null, false, false)
  80                              ||    $lang->load($option->request['option'].'.sys', JPATH_ADMINISTRATOR.'/components/'.$option->request['option'], null, false, false)
  81                              ||    $lang->load($option->request['option'].'.sys', JPATH_ADMINISTRATOR, $lang->getDefault(), false, false)
  82                              ||    $lang->load($option->request['option'].'.sys', JPATH_ADMINISTRATOR.'/components/'.$option->request['option'], $lang->getDefault(), false, false);
  83                          }
  84                      }
  85                  }
  86              }
  87          }
  88  
  89          return $list;
  90      }
  91  
  92  	protected function getTypeOptionsByComponent($component)
  93      {
  94          // Initialise variables.
  95          $options = array();
  96  
  97          $mainXML = JPATH_SITE.'/components/'.$component.'/metadata.xml';
  98  
  99          if (is_file($mainXML)) {
 100              $options = $this->getTypeOptionsFromXML($mainXML, $component);
 101          }
 102  
 103          if (empty($options)) {
 104              $options = $this->getTypeOptionsFromMVC($component);
 105          }
 106  
 107          return $options;
 108      }
 109  
 110  	protected function getTypeOptionsFromXML($file, $component)
 111      {
 112          // Initialise variables.
 113          $options = array();
 114  
 115          // Attempt to load the xml file.
 116          if (!$xml = simplexml_load_file($file)) {
 117              return false;
 118          }
 119  
 120          // Look for the first menu node off of the root node.
 121          if (!$menu = $xml->xpath('menu[1]')) {
 122              return false;
 123          }
 124          else {
 125              $menu = $menu[0];
 126          }
 127  
 128          // If we have no options to parse, just add the base component to the list of options.
 129          if (!empty($menu['options']) && $menu['options'] == 'none')
 130          {
 131              // Create the menu option for the component.
 132              $o = new JObject;
 133              $o->title        = (string) $menu['name'];
 134              $o->description    = (string) $menu['msg'];
 135              $o->request        = array('option' => $component);
 136  
 137              $options[] = $o;
 138  
 139              return $options;
 140          }
 141  
 142          // Look for the first options node off of the menu node.
 143          if (!$optionsNode = $menu->xpath('options[1]')) {
 144              return false;
 145          }
 146          else {
 147              $optionsNode = $optionsNode[0];
 148          }
 149  
 150          // Make sure the options node has children.
 151          if (!$children = $optionsNode->children()) {
 152              return false;
 153          }
 154          else {
 155              // Process each child as an option.
 156              foreach ($children as $child)
 157              {
 158                  if ($child->getName() == 'option') {
 159                      // Create the menu option for the component.
 160                      $o = new JObject;
 161                      $o->title        = (string) $child['name'];
 162                      $o->description    = (string) $child['msg'];
 163                      $o->request        = array('option' => $component, (string) $optionsNode['var'] => (string) $child['value']);
 164  
 165                      $options[] = $o;
 166                  }
 167                  elseif ($child->getName() == 'default') {
 168                      // Create the menu option for the component.
 169                      $o = new JObject;
 170                      $o->title        = (string) $child['name'];
 171                      $o->description    = (string) $child['msg'];
 172                      $o->request        = array('option' => $component);
 173  
 174                      $options[] = $o;
 175                  }
 176              }
 177          }
 178  
 179          return $options;
 180      }
 181  
 182  	protected function getTypeOptionsFromMVC($component)
 183      {
 184          // Initialise variables.
 185          $options = array();
 186  
 187          // Get the views for this component.
 188          $path = JPATH_SITE.'/components/'.$component.'/views';
 189  
 190          if (JFolder::exists($path)) {
 191              $views = JFolder::folders($path);
 192          }
 193          else {
 194              return false;
 195          }
 196  
 197          foreach ($views as $view)
 198          {
 199              // Ignore private views.
 200              if (strpos($view, '_') !== 0) {
 201                  // Determine if a metadata file exists for the view.
 202                  $file = $path.'/'.$view.'/metadata.xml';
 203  
 204                  if (is_file($file)) {
 205                      // Attempt to load the xml file.
 206                      if ($xml = simplexml_load_file($file)) {
 207                          // Look for the first view node off of the root node.
 208                          if ($menu = $xml->xpath('view[1]')) {
 209                              $menu = $menu[0];
 210  
 211                              // If the view is hidden from the menu, discard it and move on to the next view.
 212                              if (!empty($menu['hidden']) && $menu['hidden'] == 'true') {
 213                                  unset($xml);
 214                                  continue;
 215                              }
 216  
 217                              // Do we have an options node or should we process layouts?
 218                              // Look for the first options node off of the menu node.
 219                              if ($optionsNode = $menu->xpath('options[1]')) {
 220                                  $optionsNode = $optionsNode[0];
 221  
 222                                  // Make sure the options node has children.
 223                                  if ($children = $optionsNode->children()) {
 224                                      // Process each child as an option.
 225                                      foreach ($children as $child)
 226                                      {
 227                                          if ($child->getName() == 'option') {
 228                                              // Create the menu option for the component.
 229                                              $o = new JObject;
 230                                              $o->title        = (string) $child['name'];
 231                                              $o->description    = (string) $child['msg'];
 232                                              $o->request        = array('option' => $component, 'view' => $view, (string) $optionsNode['var'] => (string) $child['value']);
 233  
 234                                              $options[] = $o;
 235                                          }
 236                                          elseif ($child->getName() == 'default') {
 237                                              // Create the menu option for the component.
 238                                              $o = new JObject;
 239                                              $o->title        = (string) $child['name'];
 240                                              $o->description    = (string) $child['msg'];
 241                                              $o->request        = array('option' => $component, 'view' => $view);
 242  
 243                                              $options[] = $o;
 244                                          }
 245                                      }
 246                                  }
 247                              }
 248                              else {
 249                                  $options = array_merge($options, (array) $this->getTypeOptionsFromLayouts($component, $view));
 250                              }
 251                          }
 252                          unset($xml);
 253                      }
 254  
 255                  }
 256                  else {
 257                      $options = array_merge($options, (array) $this->getTypeOptionsFromLayouts($component, $view));
 258                  }
 259              }
 260          }
 261  
 262          return $options;
 263      }
 264  
 265  	protected function getTypeOptionsFromLayouts($component, $view)
 266      {
 267          // Initialise variables.
 268          $options = array();
 269          $layouts = array();
 270          $layoutNames = array();
 271          $templateLayouts = array();
 272          $lang = JFactory::getLanguage();
 273  
 274          // Get the layouts from the view folder.
 275          $path = JPATH_SITE.'/components/'.$component.'/views/'.$view.'/tmpl';
 276          if (JFolder::exists($path)) {
 277              $layouts = array_merge($layouts, JFolder::files($path, '.xml$', false, true));
 278          }
 279          else {
 280              return $options;
 281          }
 282  
 283          // build list of standard layout names
 284          foreach ($layouts as $layout)
 285          {
 286              // Ignore private layouts.
 287              if (strpos(JFile::getName($layout), '_') === false) {
 288                  $file = $layout;
 289                  // Get the layout name.
 290                  $layoutNames[] = JFile::stripext(JFile::getName($layout));
 291              }
 292          }
 293  
 294          // get the template layouts
 295          // TODO: This should only search one template -- the current template for this item (default of specified)
 296          $folders = JFolder::folders(JPATH_SITE . '/templates', '', false, true);
 297          // Array to hold association between template file names and templates
 298          $templateName = array();
 299          foreach($folders as $folder)
 300          {
 301              if (JFolder::exists($folder . '/html/' . $component . '/' . $view)) {
 302                  $template = JFile::getName($folder);
 303                      $lang->load('tpl_'.$template.'.sys', JPATH_SITE, null, false, false)
 304                  ||    $lang->load('tpl_'.$template.'.sys', JPATH_SITE.'/templates/'.$template, null, false, false)
 305                  ||    $lang->load('tpl_'.$template.'.sys', JPATH_SITE, $lang->getDefault(), false, false)
 306                  ||    $lang->load('tpl_'.$template.'.sys', JPATH_SITE.'/templates/'.$template, $lang->getDefault(), false, false);
 307  
 308                  $templateLayouts = JFolder::files($folder . '/html/' . $component . '/' . $view, '.xml$', false, true);
 309  
 310                  foreach ($templateLayouts as $layout)
 311                  {
 312                      $file = $layout;
 313                      // Get the layout name.
 314                      $templateLayoutName = JFile::stripext(JFile::getName($layout));
 315  
 316                      // add to the list only if it is not a standard layout
 317                      if (array_search($templateLayoutName, $layoutNames) === false) {
 318                          $layouts[] = $layout;
 319                          // Set template name array so we can get the right template for the layout
 320                          $templateName[$layout] = JFile::getName($folder);
 321                      }
 322                  }
 323              }
 324          }
 325  
 326          // Process the found layouts.
 327          foreach ($layouts as $layout)
 328          {
 329              // Ignore private layouts.
 330              if (strpos(JFile::getName($layout), '_') === false) {
 331                  $file = $layout;
 332                  // Get the layout name.
 333                  $layout = JFile::stripext(JFile::getName($layout));
 334  
 335                  // Create the menu option for the layout.
 336                  $o = new JObject;
 337                  $o->title        = ucfirst($layout);
 338                  $o->description    = '';
 339                  $o->request        = array('option' => $component, 'view' => $view);
 340  
 341                  // Only add the layout request argument if not the default layout.
 342                  if ($layout != 'default') {
 343                      // If the template is set, add in format template:layout so we save the template name
 344                      $o->request['layout'] = (isset($templateName[$file])) ? $templateName[$file] . ':' . $layout : $layout;
 345                  }
 346  
 347                  // Load layout metadata if it exists.
 348                  if (is_file($file)) {
 349                      // Attempt to load the xml file.
 350                      if ($xml = simplexml_load_file($file)) {
 351                          // Look for the first view node off of the root node.
 352                          if ($menu = $xml->xpath('layout[1]')) {
 353                              $menu = $menu[0];
 354  
 355                              // If the view is hidden from the menu, discard it and move on to the next view.
 356                              if (!empty($menu['hidden']) && $menu['hidden'] == 'true') {
 357                                  unset($xml);
 358                                  unset($o);
 359                                  continue;
 360                              }
 361  
 362                              // Populate the title and description if they exist.
 363                              if (!empty($menu['title'])) {
 364                                  $o->title = trim((string) $menu['title']);
 365                              }
 366  
 367                              if (!empty($menu->message[0])) {
 368                                  $o->description = trim((string) $menu->message[0]);
 369                              }
 370                          }
 371                      }
 372                  }
 373  
 374                  // Add the layout to the options array.
 375                  $options[] = $o;
 376              }
 377          }
 378  
 379          return $options;
 380      }
 381  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1