[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_menus/src/View/Menu/ -> XmlView.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_menus
   6   *
   7   * @copyright   (C) 2005 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\View\Menu;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\Log\Log;
  16  use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
  17  use Joomla\CMS\Router\Route;
  18  use Joomla\Component\Menus\Administrator\Helper\MenusHelper;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('_JEXEC') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * The HTML Menus Menu Item View.
  26   *
  27   * @since  3.8.0
  28   */
  29  class XmlView extends BaseHtmlView
  30  {
  31      /**
  32       * @var  \stdClass[]
  33       *
  34       * @since  3.8.0
  35       */
  36      protected $items;
  37  
  38      /**
  39       * @var    \Joomla\CMS\Object\CMSObject
  40       *
  41       * @since  3.8.0
  42       */
  43      protected $state;
  44  
  45      /**
  46       * Display the view
  47       *
  48       * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
  49       *
  50       * @return  void
  51       *
  52       * @since   3.8.0
  53       */
  54      public function display($tpl = null)
  55      {
  56          $app      = Factory::getApplication();
  57          $menutype = $app->input->getCmd('menutype');
  58  
  59          if ($menutype) {
  60              $root = MenusHelper::getMenuItems($menutype, true);
  61          }
  62  
  63          if (!$root->hasChildren()) {
  64              Log::add(Text::_('COM_MENUS_SELECT_MENU_FIRST_EXPORT'), Log::WARNING, 'jerror');
  65  
  66              $app->redirect(Route::_('index.php?option=com_menus&view=menus', false));
  67  
  68              return;
  69          }
  70  
  71          $this->items = $root->getChildren(true);
  72  
  73          $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><menu ' .
  74              'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' .
  75              'xmlns="urn:joomla.org"    xsi:schemaLocation="urn:joomla.org menu.xsd"' .
  76              '></menu>');
  77  
  78          foreach ($this->items as $item) {
  79              $this->addXmlChild($xml, $item);
  80          }
  81  
  82          if (headers_sent($file, $line)) {
  83              Log::add("Headers already sent at $file:$line.", Log::ERROR, 'jerror');
  84  
  85              return;
  86          }
  87  
  88          header('content-type: application/xml');
  89          header('content-disposition: attachment; filename="' . $menutype . '.xml"');
  90          header("Cache-Control: no-cache, must-revalidate");
  91          header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  92  
  93          $dom = new \DOMDocument();
  94          $dom->preserveWhiteSpace = true;
  95          $dom->formatOutput = true;
  96          $dom->loadXML($xml->asXML());
  97  
  98          echo $dom->saveXML();
  99  
 100          $app->close();
 101      }
 102  
 103      /**
 104       * Add a child node to the xml
 105       *
 106       * @param   \SimpleXMLElement  $xml   The current XML node which would become the parent to the new node
 107       * @param   \stdClass          $item  The menuitem object to create the child XML node from
 108       *
 109       * @return  void
 110       *
 111       * @since   3.8.0
 112       */
 113      protected function addXmlChild($xml, $item)
 114      {
 115          $node = $xml->addChild('menuitem');
 116  
 117          $node['type'] = $item->type;
 118  
 119          if ($item->title) {
 120              $node['title'] = htmlentities($item->title, ENT_XML1);
 121          }
 122  
 123          if ($item->link) {
 124              $node['link'] = $item->link;
 125          }
 126  
 127          if ($item->element) {
 128              $node['element'] = $item->element;
 129          }
 130  
 131          if (isset($item->class) && $item->class) {
 132              $node['class'] = htmlentities($item->class, ENT_XML1);
 133          }
 134  
 135          if ($item->access) {
 136              $node['access'] = $item->access;
 137          }
 138  
 139          if ($item->browserNav) {
 140              $node['target'] = '_blank';
 141          }
 142  
 143          if ($item->getParams() && $hideitems = $item->getParams()->get('hideitems')) {
 144              $item->getParams()->set('hideitems', $this->getModel('Menu')->getExtensionElementsForMenuItems($hideitems));
 145  
 146              $node->addChild('params', htmlentities((string) $item->getParams(), ENT_XML1));
 147          }
 148  
 149          if (isset($item->submenu)) {
 150              foreach ($item->submenu as $sub) {
 151                  $this->addXmlChild($node, $sub);
 152              }
 153          }
 154      }
 155  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer