/**
  * Up
  **/
 public function up()
 {
     $rparams = $this->getParams('com_register');
     if (!empty($rparams)) {
         $values = $rparams->toArray();
         $this->db->setQuery("SELECT * FROM `#__extensions` WHERE `type`='component' AND `element`='com_members' LIMIT 1");
         if ($data = $this->db->loadAssoc()) {
             $component = new \JTableExtension($this->db);
             $component->bind($data);
             $mparams = new \Hubzero\Config\Registry($component->params);
             foreach ($values as $key => $value) {
                 $mparams->set($key, $value);
             }
             $component->params = $mparams->toString();
             $component->store();
         }
     }
     // Get the default menu identifier
     //$this->db->setQuery("SELECT menutype FROM `#__menu` WHERE home='1' LIMIT 1;");
     //$menutype = $this->db->loadResult();
     $this->db->setQuery("SELECT extension_id FROM `#__extensions` WHERE `type`='component' AND `element`='com_members'");
     $component = $this->db->loadResult();
     // Check if there's a menu item for com_register
     $this->db->setQuery("SELECT id FROM `#__menu` WHERE `alias`='register' AND `path`='register'");
     //" AND menutype=" . $this->db->quote($menutype));
     if ($id = $this->db->loadResult()) {
         // There is!
         // So, just update its link
         $this->db->setQuery("UPDATE `#__menu` SET `link`='index.php?option=com_members&view=register&layout=create', `component_id`=" . $this->db->quote($component) . " WHERE `id`=" . $this->db->quote($id));
         $this->db->query();
     } else {
         $this->db->setQuery("SELECT menutype FROM `#__menu` WHERE `home`='1' LIMIT 1;");
         $menutype = $this->db->loadResult();
         $this->db->setQuery("SELECT ordering FROM `#__menu` WHERE `menutype`=" . $this->db->quote($menutype) . " ORDER BY ordering DESC LIMIT 1");
         $ordering = intval($this->db->loadResult());
         $ordering++;
         // No menu item for com_register so we need to create one for the new com_members controler
         $query = "INSERT INTO `#__menu` (`id`, `menutype`, `title`, `alias`, `note`, `path`, `link`, `type`, `published`, `parent_id`, `level`, `component_id`, `ordering`, `checked_out`, `checked_out_time`, `browserNav`, `access`, `img`, `template_style_id`, `params`, `lft`, `rgt`, `home`, `language`, `client_id`)\n";
         $query .= "VALUES ('', '{$menutype}', 'Register', 'register', '', 'register', 'index.php?option=com_members&view=register&layout=create', 'component', 1, 1, 1, {$component}, {$ordering}, 0, '0000-00-00 00:00:00', 0, 1, '', 0, '', 0, 0, 0, '*', 0);";
         $this->db->setQuery($query);
         $this->db->query();
         // If we have the nested set class available, use it to rebuild lft/rgt
         if (class_exists('JTableNested') && method_exists('JTableNested', 'rebuild')) {
             // Use the MySQL driver for this
             $config = \JFactory::getConfig();
             $database = \JDatabase::getInstance(array('driver' => 'mysql', 'host' => $config->getValue('host'), 'user' => $config->getValue('user'), 'password' => $config->getValue('password'), 'database' => $config->getValue('db')));
             $table = new \JTableMenu($database);
             $table->rebuild();
             unset($database);
         }
     }
     $this->deleteComponentEntry('register');
 }
示例#2
0
 /**
  * Create menu structure
  *
  * @return void
  */
 public static function createMenuStructure()
 {
     /* @var $db NenoDatabaseDriverMysqlx */
     $db = JFactory::getDbo();
     $query = $db->getQuery(true);
     $languages = self::getTargetLanguages();
     $defaultLanguage = NenoSettings::get('source_language');
     // Delete all the menus trashed
     $query->delete('#__menu')->where('published = -2');
     $db->setQuery($query);
     $db->execute();
     // Delete all the associations left
     $query->clear()->delete('a USING #__associations AS a')->where(array('context = ' . $db->quote('com_menus.item'), 'NOT EXISTS (SELECT 1 FROM #__menu AS m WHERE a.id = m.id)'));
     $db->setQuery($query);
     $db->execute();
     // Delete associations with just one item
     $query->clear()->select('DISTINCT id')->from('#__associations')->group('`key`')->having('COUNT(*) = 1');
     $db->setQuery($query);
     $ids = $db->loadArray();
     $query->clear()->delete('#__associations')->where(array('context = ' . $db->quote('com_menus.item'), 'id IN (' . implode(', ', $db->quote($ids)) . ')'));
     $db->setQuery($query);
     $db->execute();
     $query->clear()->update('#__modules')->set('language = ' . $db->quote($defaultLanguage))->where(array('published = 1', 'module = ' . $db->quote('mod_menu'), 'client_id = 0', 'language  = ' . $db->quote('*')));
     $db->setQuery($query);
     $db->execute();
     // Set all the menus items from '*' to default language
     $query->clear()->update('#__menu AS m')->set('language = ' . $db->quote($defaultLanguage))->where(array('client_id = 0', 'level <> 0', 'language = ' . $db->quote('*')));
     $db->setQuery($query);
     $db->execute();
     $query->clear()->select(array('m.*'))->from('#__menu_types AS mt')->leftJoin('#__menu AS m ON mt.menutype = m.menutype')->where(array('NOT EXISTS(SELECT 1 FROM #__associations AS a WHERE a.id = m.id AND a.`key` = ' . $db->quote('com_menus.item') . ')', 'client_id = 0', 'level <> 0', 'published <> -2'))->order('level');
     $db->setQuery($query);
     $nonAssociatedMenuItems = $db->loadObjectList();
     $menuAssociations = array();
     $query->clear()->select('DISTINCT m1.menutype AS m1')->from('#__associations a1')->innerJoin('#__menu AS m1 ON a1.id = m1.id')->innerJoin('#__associations AS a2 ON a1.key = a2.key')->innerJoin('#__menu AS m2 ON a2.id = m2.id')->where(array('a1.context = ' . $db->quote('com_menus.item'), 'a2.context = ' . $db->quote('com_menus.item'), 'a1.id <> a2.id', 'm1.client_id = 0', 'm1.level <> 0', 'm1.published <> -2', 'm2.client_id = 0', 'm2.level <> 0', 'm2.published <> -2'));
     $db->setQuery($query);
     $menuTypes = $db->loadArray();
     foreach ($menuTypes as $menuType) {
         $query->clear()->select(array('DISTINCT m2.menutype', 'm2.language'))->from('#__associations a1')->innerJoin('#__menu AS m1 ON a1.id = m1.id')->innerJoin('#__associations AS a2 ON a1.key = a2.key')->innerJoin('#__menu AS m2 ON a2.id = m2.id')->where(array('a1.context = ' . $db->quote('com_menus.item'), 'a2.context = ' . $db->quote('com_menus.item'), 'a1.id <> a2.id', 'm1.client_id = 0', 'm1.level <> 0', 'm1.published <> -2', 'm2.client_id = 0', 'm2.level <> 0', 'm2.published <> -2', 'm1.menutype = ' . $db->quote($menuType)));
         $db->setQuery($query);
         $menuAssociations[$menuType] = $db->loadAssocList('language');
     }
     $menuItemsCreated = array();
     $modulesDuplicated = array();
     foreach ($nonAssociatedMenuItems as $key => $menuItem) {
         if (!isset($menuAssociations[$menuItem->menutype])) {
             $menuAssociations[$menuItem->menutype] = array();
         }
         $associations = array();
         $insert = false;
         $insertQuery = $db->getQuery(true);
         $insertQuery->insert('#__associations')->columns(array('id', $db->quoteName('context'), $db->quoteName('key')));
         foreach ($languages as $language) {
             if ($language->lang_code !== $menuItem->language) {
                 $menuItemsCreated[$language->lang_code] = array();
                 // If there's no menu associated
                 if (empty($menuAssociations[$menuItem->menutype][$language->lang_code])) {
                     if (!isset($menuAssociations[$menuItem->menutype][$language->lang_code])) {
                         $menuAssociations[$menuItem->menutype][$language->lang_code] = array();
                     }
                     $newMenuType = new stdClass();
                     $newMenuType->menutype = $menuItem->menutype;
                     $newMenuType->title = $menuItem->menutype;
                     $newMenuType = self::createMenu($language->lang_code, $newMenuType, $defaultLanguage);
                     // If the menu has been inserted properly, let's save into the data structure
                     if (!empty($newMenuType)) {
                         $menuAssociations[$menuItem->menutype][$language->lang_code]['menutype'] = $newMenuType->menutype;
                         $menuAssociations[$menuItem->menutype][$language->lang_code]['language'] = $language->lang_code;
                     }
                 }
                 $newMenuItem = clone $menuItem;
                 unset($newMenuItem->id);
                 $newMenuItem->menutype = $menuAssociations[$menuItem->menutype][$language->lang_code]['menutype'];
                 $newMenuItem->alias = JFilterOutput::stringURLSafe($newMenuItem->alias . '-' . $language->lang_code);
                 $newMenuItem->language = $language->lang_code;
                 // If the menu item has been inserted properly, let's execute some actions
                 if ($db->insertObject('#__menu', $newMenuItem, 'id')) {
                     $menuItemsCreated[$language->lang_code][] = $newMenuItem->id;
                     // Assign all the modules to this item
                     $query = 'INSERT INTO #__modules_menu (moduleid,menuid) SELECT moduleid,' . $db->quote($newMenuItem->id) . ' FROM  #__modules_menu WHERE menuid = ' . $db->quote($menuItem->id);
                     $db->setQuery($query);
                     $db->execute();
                     $query = $db->getQuery(true);
                     $associations[] = $newMenuItem->id;
                 }
             }
         }
         // Get all the modules assigned to this menu item using a different language from *
         $query->clear()->select('m.*')->from('#__modules AS m')->innerJoin('#__modules_menu AS mm ON m.id = mm.moduleid')->where(array('mm.menuid = ' . (int) $menuItem->id, 'm.language <> ' . $db->quote('*')));
         $db->setQuery($query);
         $modules = $db->loadObjectList();
         if (!empty($modules)) {
             $query->clear()->insert('#__modules_menu')->columns(array('moduleid', 'menuid'));
             foreach ($menuItemsCreated as $language => $newMenuItems) {
                 foreach ($modules as $module) {
                     $previousId = $module->id;
                     if (!isset($modulesDuplicated[$previousId . $language]) && $module->language != $language) {
                         unset($module->id);
                         $module->language = $language;
                         $module->title = $module->title . '(' . $language . ')';
                         if ($db->insertObject('#__modules', $module, 'id')) {
                             $modulesDuplicated[$previousId . $language] = $module->id;
                         }
                     }
                     foreach ($newMenuItems as $newMenuItem) {
                         $query->values($modulesDuplicated[$previousId . $language] . ',' . $newMenuItem->id);
                     }
                 }
             }
             $db->setQuery($query);
             $db->execute();
         }
         $query->clear()->select($db->quoteName('key', 'associationKey'))->from('#__associations')->where(array('id IN (' . implode(',', array_merge($associations, array($menuItem->id))) . ')', 'context = ' . $db->quote('com_menus.item')));
         $db->setQuery($query);
         $associationKey = $db->loadResult();
         if (empty($associationKey)) {
             if (!in_array($menuItem->id, $associations)) {
                 $associations[] = $menuItem->id;
             }
             $associations = array_unique($associations);
             $associationKey = md5(json_encode($associations));
         } else {
             $query->clear()->select('id')->from('#__associations')->where($db->quoteName('key') . ' = ' . $db->quote($associationKey));
             $db->setQuery($query);
             $alreadyInserted = $db->loadArray();
             $associations = array_diff($associations, $alreadyInserted);
         }
         foreach ($associations as $association) {
             $insertQuery->values($association . ',' . $db->quote('com_menus.item') . ',' . $db->quote($associationKey));
             $insert = true;
         }
         if ($insert) {
             $db->setQuery($insertQuery);
             $db->execute();
         }
     }
     // Get all the modules with the language as default
     $query->clear()->select('m.*')->from('#__modules AS m')->where('m.language = ' . $db->quote($defaultLanguage));
     $db->setQuery($query);
     $modules = $db->loadObjectList();
     foreach ($modules as $module) {
         foreach ($languages as $language) {
             if ($language->lang_code != $defaultLanguage) {
                 if (!isset(self::$menuModuleReplicated[$language->lang_code])) {
                     self::$menuModuleReplicated[$language->lang_code] = array();
                 }
                 if (!in_array($module->id, self::$menuModuleReplicated[$language->lang_code])) {
                     self::$menuModuleReplicated[] = $module->id;
                     $previousId = $module->id;
                     $previousTitle = $module->title;
                     $module->id = 0;
                     $module->title = $previousTitle . ' (' . $language->lang_code . ')';
                     $module->language = $language->lang_code;
                     // If the module has been inserted correctly, let's assign it
                     if ($db->insertObject('#__modules', $module, 'id')) {
                         $insert = false;
                         $insertQuery = $db->getQuery(true);
                         $insertQuery->clear()->insert('#__modules_menu')->columns(array('moduleid', 'menuid'));
                         // Check if the previous module is assigned to all
                         $query->clear()->select('1')->from('#__modules_menu')->where(array('moduleid = ' . $previousId, 'menuid = 0'));
                         $db->setQuery($query);
                         $result = $db->loadResult();
                         if ($result == 1) {
                             $insertQuery->values($module->id . ', 0');
                             $insert = true;
                         } else {
                             // Check if the module has assigned selected
                             $query->clear()->select('DISTINCT m2.id')->from('#__modules_menu AS mm')->innerJoin('#__menu AS m1 ON mm.menuid = m1.id')->innerJoin('#__associations AS a1 ON a1.id = m1.id')->innerJoin('#__associations AS a2 ON a1.key = a2.key')->innerJoin('#__menu AS m2 ON a2.id = m2.id')->where(array('a1.context = ' . $db->quote('com_menus.item'), 'a2.context = ' . $db->quote('com_menus.item'), 'a1.id <> a2.id', 'm1.client_id = 0', 'm1.level <> 0', 'm1.published <> -2', 'm2.client_id = 0', 'm2.level <> 0', 'm2.published <> -2', 'mm.moduleid = ' . $previousId, 'm1.language = ' . $db->quote($defaultLanguage), 'm2.language = ' . $db->quote($language->lang_code)));
                             $db->setQuery($query);
                             $menuIds = $db->loadArray();
                             if (!empty($menuIds)) {
                                 $insert = true;
                                 foreach ($menuIds as $menuId) {
                                     $insertQuery->values($module->id . ',' . $menuId);
                                 }
                             }
                         }
                         if ($insert) {
                             $db->setQuery($insertQuery);
                             $db->execute();
                         }
                     }
                 }
             }
         }
     }
     // Fixing levels issue
     foreach ($languages as $language) {
         if ($language->lang_code !== $defaultLanguage) {
             $query->clear()->select('m1.*')->from('#__menu AS m1')->where(array('client_id = 0', 'level <> 0', 'level <> 1', 'EXISTS (SELECT 1 FROM #__menu AS m2 WHERE m2.id = m1.parent_id AND m2.language <> m1.language)'));
             $db->setQuery($query);
             $menuItemsThatNeedsToBeFixed = $db->loadObjectList();
             foreach ($menuItemsThatNeedsToBeFixed as $menuItem) {
                 $query->clear()->select('m2.id')->from('#__menu AS m1')->innerJoin('#__associations AS a1 ON a1.id = m1.id')->innerJoin('#__associations AS a2 ON a1.key = a2.key')->innerJoin('#__menu AS m2 ON a2.id = m2.id')->where(array('a1.context = ' . $db->quote('com_menus.item'), 'a2.context = ' . $db->quote('com_menus.item'), 'a1.id <> a2.id', 'm1.id = ' . $menuItem->parent_id, 'm2.language = ' . $db->quote($menuItem->language)));
                 $db->setQuery($query);
                 $parentId = (int) $db->loadResult();
                 if (!empty($parentId)) {
                     $menuItem->parent_id = $parentId;
                     $db->updateObject('#__menu', $menuItem, 'id');
                 }
             }
         }
     }
     // Once we finish restructuring menus, let's rebuild them
     $menuTable = new JTableMenu($db);
     $menuTable->rebuild();
 }
示例#3
0
 /**
  * Remove component entries from the appropriate table, depending on the Joomla version
  *
  * @param   string  $name  Component name
  * @return  bool
  **/
 public function deleteComponentEntry($name)
 {
     if ($this->baseDb->tableExists('#__components')) {
         // Delete component entry
         $query = "DELETE FROM `#__components` WHERE `name` = " . $this->baseDb->quote($name);
         $this->baseDb->setQuery($query);
         $this->baseDb->query();
     } else {
         $name = 'com_' . strtolower($name);
         // Delete component entry
         $query = "DELETE FROM `#__extensions` WHERE `name` = " . $this->baseDb->quote($name);
         $this->baseDb->setQuery($query);
         $this->baseDb->query();
         // Remove the component container in the assets table
         $asset = \JTable::getInstance('Asset');
         if ($asset->loadByName($name)) {
             $asset->delete();
         }
         // Check for an admin menu entry...if it's not there, create it
         $query = "DELETE FROM `#__menu` WHERE `menutype` = 'main' AND `title` = " . $this->baseDb->quote($name);
         $this->baseDb->setQuery($query);
         $this->baseDb->query();
         // If we have the nested set class available, use it to rebuild lft/rgt
         if (class_exists('JTableNested') && method_exists('JTableNested', 'rebuild')) {
             // Use the MySQL driver for this
             $database = \JDatabase::getInstance(array('driver' => 'mysql', 'host' => \Config::get('host'), 'user' => \Config::get('user'), 'password' => \Config::get('password'), 'database' => \Config::get('db')));
             $table = new \JTableMenu($database);
             $table->rebuild();
         }
     }
 }
示例#4
0
 function ValidateMenus()
 {
     $log = "";
     if (FSFJ3Helper::IsJ3()) {
     } elseif ($this->DBIs16()) {
         // no need at moment, as no added items for a 1.6 install
         $db = JFactory::getDBO();
         $db->setQuery("SELECT * FROM #__menu WHERE link = 'index.php?option=com_fsf' AND menutype = 'main'");
         $component = $db->loadObjectList();
         $componentid = $component[0]->id;
         $componentid16 = $component[0]->component_id;
         if (file_exists(JPATH_COMPONENT . DS . 'fsf.xml')) {
             //echo "<pre>";
             $order = 1;
             $xml = simplexml_load_file(JPATH_COMPONENT . DS . 'fsf.xml');
             foreach ($xml->administration->submenu->menu as $item) {
                 $name = (string) $item;
                 //echo $name."<br>";
                 $arr = $item->attributes();
                 $link = $arr['link'];
                 //echo $link."<br>";
                 $alias = strtolower(str_replace("_", "", $name));
                 $qry = "SELECT * FROM #__menu WHERE link = 'index.php?{$link}' AND menutype = 'main'";
                 //echo $qry."<br>";
                 $db->setQuery($qry);
                 $componentitem = $db->loadObject();
                 if (!$componentitem) {
                     //echo "Missing<br>";
                     // item missing, create it
                     $qry = "INSERT INTO #__menu (menutype, title, alias, path, link, type, parent_id, level, component_id, ordering, img, client_id) VALUES (";
                     $qry .= " 'main', '{$name}', '{$alias}', 'freestylesupportportal/{$alias}', 'index.php?{$link}', 'component', {$componentid}, 2, {$componentid16}, {$order}, 'images/blank.png', 1)";
                     $db->setQuery($qry);
                     $db->Query();
                     $log .= "Adding menu item {$name}<Br>";
                 } else {
                     //print_r($componentitem);
                     $qry = "UPDATE #__menu SET title = '{$name}', ordering = {$order} WHERE id = " . $componentitem->id;
                     //echo $qry."<br>";
                     $db->setQuery($qry);
                     $db->Query();
                 }
                 $order++;
             }
             //echo "</pre>";
             jimport('joomla.database.table.menu');
             require JPATH_SITE . DS . "libraries" . DS . "joomla" . DS . "database" . DS . "table" . DS . "menu.php";
             $table = new JTableMenu($db);
             $table->rebuild();
         }
     } else {
         // find base item
         $db = JFactory::getDBO();
         $db->setQuery("SELECT * FROM #__components WHERE link = 'option=com_fsf' AND parent = 0");
         $component = $db->loadObjectList();
         $componentid = $component[0]->id;
         if (file_exists(JPATH_COMPONENT . DS . 'fsf.xml')) {
             //echo "<pre>";
             $order = 1;
             $xml = simplexml_load_file(JPATH_COMPONENT . DS . 'fsf.xml');
             foreach ($xml->administration->submenu->menu as $item) {
                 $name = (string) $item;
                 //$log .= $name."\n";
                 $arr = $item->attributes();
                 $link = $arr['link'];
                 //$log .= $link."\n";
                 $db->setQuery("SELECT * FROM #__components WHERE admin_menu_link = '{$link}'");
                 $componentitem = $db->loadObject();
                 if (!$componentitem) {
                     // item missing, create it
                     //echo "MISSING<br>";
                     $qry = "INSERT INTO #__components (name, parent, admin_menu_link, admin_menu_alt, `option`, ordering, admin_menu_img, iscore, enabled) VALUES (";
                     $qry .= " '{$name}', {$componentid}, '{$link}', '{$name}', 'com_fsf', {$order}, 'images/blank.png', 0, 1)";
                     $db->setQuery($qry);
                     $db->Query();
                     $log .= "Adding menu item {$name}<Br>";
                 } else {
                     //print_r($componentitem);
                     $qry = "UPDATE #__components SET name = '{$name}', ordering = {$order} WHERE id = " . $componentitem->id;
                     //$log .= $qry."<br>";
                     $db->setQuery($qry);
                     $db->Query();
                 }
                 $order++;
             }
             //echo "</pre>";
             $log .= "Base component id : {$componentid}<br>" . $log;
         }
     }
     if ($log == "") {
         $log = "All admin menu items are ok<br>";
     }
     return $log;
 }
 /**
  * Up
  **/
 public function up()
 {
     $query = "ALTER TABLE `#__menu` ENGINE = MYISAM;";
     $this->db->setQuery($query);
     $this->db->query();
     $first = false;
     if ($this->db->tableHasField('#__menu', 'pollid')) {
         $query = "ALTER TABLE `#__menu` DROP COLUMN `pollid`;";
         $this->db->setQuery($query);
         $this->db->query();
         $first = true;
     }
     if ($this->db->tableHasField('#__menu', 'utaccess')) {
         $query = "ALTER TABLE `#__menu` DROP COLUMN `utaccess`;";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu', 'menutype')) {
         $query = "ALTER TABLE `#__menu` MODIFY COLUMN `menutype` VARCHAR(24) NOT NULL COMMENT 'The type of menu this item belongs to. FK to #__menu_types.menutype';";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu', 'name') && !$this->db->tableHasField('#__menu', 'title')) {
         $query = "ALTER TABLE `#__menu` CHANGE COLUMN `name` `title` VARCHAR(255) NOT NULL COMMENT 'The display title of the menu item.';";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu', 'alias')) {
         $query = "ALTER TABLE `#__menu` MODIFY COLUMN `alias` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL COMMENT 'The SEF alias of the menu item.';";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if (!$this->db->tableHasField('#__menu', 'note') && $this->db->tableHasField('#__menu', 'alias')) {
         $query = "ALTER TABLE `#__menu` ADD COLUMN `note` VARCHAR(255) NOT NULL DEFAULT '' AFTER `alias`;";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu', 'link')) {
         $query = "ALTER TABLE `#__menu` MODIFY COLUMN `link` VARCHAR(1024) NOT NULL COMMENT 'The actually link the menu item refers to.';";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu', 'type')) {
         $query = "ALTER TABLE `#__menu` MODIFY COLUMN `type` VARCHAR(16) NOT NULL COMMENT 'The type of link: Component, URL, Alias, Separator';";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu', 'published')) {
         $query = "ALTER TABLE `#__menu` MODIFY COLUMN `published` TINYINT NOT NULL DEFAULT 0 COMMENT 'The published state of the menu link.';";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu', 'parent') && !$this->db->tableHasField('#__menu', 'parent_id')) {
         $query = "ALTER TABLE `#__menu` CHANGE COLUMN `parent` `parent_id` INT(10) UNSIGNED NOT NULL DEFAULT '1' COMMENT 'The parent menu item in the menu tree.';";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if (!$this->db->tableHasField('#__menu', 'level') && $this->db->tableHasField('#__menu', 'parent_id') && $this->db->tableHasField('#__menu', 'sublevel')) {
         $query = "ALTER TABLE `#__menu` CHANGE COLUMN `sublevel` `level` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'The relative level in the tree.' AFTER `parent_id`;";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu', 'componentid') && !$this->db->tableHasField('#__menu', 'component_id')) {
         $query = "ALTER TABLE `#__menu` CHANGE COLUMN `componentid` `component_id` INTEGER UNSIGNED NOT NULL DEFAULT 0 COMMENT 'FK to #__components.id';";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu', 'ordering')) {
         $query = "ALTER TABLE `#__menu` MODIFY COLUMN `ordering` INTEGER NOT NULL DEFAULT 0 COMMENT 'The relative ordering of the menu item in the tree.';";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu', 'checked_out')) {
         $query = "ALTER TABLE `#__menu` MODIFY COLUMN `checked_out` INTEGER UNSIGNED NOT NULL DEFAULT 0 COMMENT 'FK to #__users.id';";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu', 'checked_out_time')) {
         $query = "ALTER TABLE `#__menu` MODIFY COLUMN `checked_out_time` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'The time the menu item was checked out.';";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu', 'browserNav')) {
         $query = "ALTER TABLE `#__menu` MODIFY COLUMN `browserNav` TINYINT NOT NULL DEFAULT 0 COMMENT 'The click behaviour of the link.';";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu', 'access')) {
         $query = "ALTER TABLE `#__menu` CHANGE COLUMN `access` `access` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'The access level required to view the menu item.';";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu', 'params')) {
         $query = "ALTER TABLE `#__menu` CHANGE COLUMN `params` `params` TEXT NOT NULL COMMENT 'JSON encoded data for the menu item.';";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu', 'lft')) {
         $query = "ALTER TABLE `#__menu` MODIFY COLUMN `lft` INTEGER NOT NULL DEFAULT 0 COMMENT 'Nested set lft.';";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu', 'rgt')) {
         $query = "ALTER TABLE `#__menu` MODIFY COLUMN `rgt` INTEGER NOT NULL DEFAULT 0 COMMENT 'Nested set rgt.';";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu', 'home')) {
         $query = "ALTER TABLE `#__menu` MODIFY COLUMN `home` TINYINT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Indicates if this menu item is the home or default page.';";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if (!$this->db->tableHasField('#__menu', 'path') && $this->db->tableHasField('#__menu', 'note')) {
         $query = "ALTER TABLE `#__menu` ADD COLUMN `path` VARCHAR(1024) NOT NULL COMMENT 'The computed path of the menu item based on the alias field.' AFTER `note`;";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if (!$this->db->tableHasField('#__menu', 'img') && $this->db->tableHasField('#__menu', 'access')) {
         $query = "ALTER TABLE `#__menu` ADD COLUMN `img` varchar(255) NOT NULL COMMENT 'The image of the menu item.' AFTER `access`;";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if (!$this->db->tableHasField('#__menu', 'template_style_id') && $this->db->tableHasField('#__menu', 'img')) {
         $query = "ALTER TABLE `#__menu` ADD COLUMN `template_style_id` INT(10) UNSIGNED NOT NULL DEFAULT '0' AFTER `img`;";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if (!$this->db->tableHasField('#__menu', 'language') && $this->db->tableHasField('#__menu', 'home')) {
         $query = "ALTER TABLE `#__menu` ADD COLUMN `language` char(7) NOT NULL DEFAULT '' AFTER `home`;";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if (!$this->db->tableHasField('#__menu', 'client_id') && $this->db->tableHasField('#__menu', 'language')) {
         $query = "ALTER TABLE `#__menu` ADD COLUMN `client_id` TINYINT(4) NOT NULL DEFAULT 0 AFTER `language`;";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasKey('#__menu', 'componentid')) {
         $query = "ALTER TABLE `#__menu` DROP INDEX `componentid`;";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasKey('#__menu', 'menutype')) {
         $query = "ALTER TABLE `#__menu` DROP INDEX `menutype`;";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if (!$this->db->tableHasKey('#__menu', 'idx_componentid') && $this->db->tableHasField('#__menu', 'component_id') && $this->db->tableHasField('#__menu', 'menutype') && $this->db->tableHasField('#__menu', 'published') && $this->db->tableHasField('#__menu', 'access')) {
         $query = "ALTER TABLE `#__menu` ADD INDEX `idx_componentid` (`component_id`,`menutype`,`published`,`access`);";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if (!$this->db->tableHasKey('#__menu', 'idx_menutype') && $this->db->tableHasField('#__menu', 'menutype')) {
         $query = "ALTER TABLE `#__menu` ADD INDEX `idx_menutype` (`menutype`);";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if (!$this->db->tableHasKey('#__menu', 'idx_left_right') && $this->db->tableHasField('#__menu', 'lft') && $this->db->tableHasField('#__menu', 'rgt')) {
         $query = "ALTER TABLE `#__menu` ADD INDEX `idx_left_right` (`lft`,`rgt`);";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if (!$this->db->tableHasKey('#__menu', 'idx_alias') && $this->db->tableHasField('#__menu', 'alias')) {
         $query = "ALTER TABLE `#__menu` ADD INDEX `idx_alias` (`alias`);";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if (!$this->db->tableHasKey('#__menu', 'idx_path') && $this->db->tableHasField('#__menu', 'path')) {
         $query = "ALTER TABLE `#__menu` ADD INDEX `idx_path` (`path`(333) ASC);";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if (!$this->db->tableHasKey('#__menu', 'idx_language') && $this->db->tableHasField('#__menu', 'language')) {
         $query = "ALTER TABLE `#__menu` ADD INDEX idx_language(`language`);";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if (!$this->db->tableHasKey('#__menu', 'idx_language') && $this->db->tableHasField('#__menu', 'language')) {
         $query = "ALTER TABLE `#__menu` ADD INDEX idx_language(`language`);";
         $this->db->setQuery($query);
         $this->db->query();
     }
     $query = "ALTER TABLE `#__menu_types` ENGINE = MYISAM;";
     $this->db->setQuery($query);
     $this->db->query();
     if ($this->db->tableHasField('#__menu_types', 'menutype')) {
         $query = "ALTER TABLE `#__menu_types` MODIFY COLUMN `menutype` VARCHAR(24) NOT NULL;";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasField('#__menu_types', 'title')) {
         $query = "ALTER TABLE `#__menu_types` MODIFY COLUMN `title` VARCHAR(48) NOT NULL;";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($this->db->tableHasKey('#__menu_types', 'menutype')) {
         $query = "ALTER TABLE `#__menu_types` DROP INDEX `menutype`;";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if (!$this->db->tableHasKey('#__menu_types', 'idx_menutype') && $this->db->tableHasField('#__menu_types', 'menutype')) {
         $query = "ALTER TABLE `#__menu_types` ADD UNIQUE INDEX `idx_menutype` (`menutype` ASC) ;";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($first) {
         // Joomla seems to expect the root item to be 1...blah!
         // So, if id 1 is taken, we need to clear it out
         $query = "SELECT * FROM `#__menu` WHERE `id` = 1;";
         $this->db->setQuery($query);
         $result = $this->db->loadObject();
         if ($result) {
             $result->id = NULL;
             $this->db->insertObject('#__menu', $result);
             $id = $this->db->insertid();
             $query = "UPDATE `#__menu` SET `parent_id` = '{$id}' WHERE `parent_id` = '1';";
             $this->db->setQuery($query);
             $this->db->query();
             $query = "UPDATE `#__modules_menu` SET `menuid` = '{$id}' WHERE `menuid` = '1';";
             $this->db->setQuery($query);
             $this->db->query();
             $query = "DELETE FROM `#__menu` WHERE `id` = '1';";
             $this->db->setQuery($query);
             $this->db->query();
         }
         // Insert new root menu item
         $query = "INSERT INTO `#__menu` (`id`, `menutype`, `title`, `alias`, `note`, `path`, `link`, `type`, `published`, `parent_id`, `level`, `component_id`, `ordering`, `checked_out`, `checked_out_time`, `browserNav`, `access`, `img`, `template_style_id`, `params`, `lft`, `rgt`, `home`, `language`, `client_id`)\n";
         $query .= "VALUES ('1', '', 'Menu_Item_Root', 'root', '', '', '', '', 1, 0, 0, 0, 0, 0, '0000-00-00 00:00:00', 0, 0, '', 0, '', 0, 0, 0, '*', 0);";
         $this->db->setQuery($query);
         $this->db->query();
         // Get the id of the new root menu item
         $query = "SELECT id FROM `#__menu` WHERE alias = 'root';";
         $this->db->setQuery($query);
         $id = $this->db->loadResult();
         // Shift the parent_id's of the existing menus to relate to the new root
         $query = "UPDATE `#__menu` SET `parent_id` = {$id} WHERE `parent_id` = 0 AND `alias` != 'root';";
         $this->db->setQuery($query);
         $this->db->query();
         // Also increment the level 1
         $query = "UPDATE `#__menu` SET `level` = `level` + 1 WHERE `alias` != 'root';";
         $this->db->setQuery($query);
         $this->db->query();
         // Build paths
         $query = "UPDATE `#__menu` SET `path` = `alias` WHERE `alias` != 'root';";
         $this->db->setQuery($query);
         $this->db->query();
         // Get max depth
         $query = "SELECT max(level) AS level FROM `#__menu`;";
         $this->db->setQuery($query);
         $maxlevel = $this->db->loadResult();
         for ($i = 2; $i <= $maxlevel; $i++) {
             $query = "SELECT * FROM `#__menu` WHERE level >= {$i};";
             $this->db->setQuery($query);
             $results = $this->db->loadObjectList();
             if (count($results) > 0) {
                 foreach ($results as $r) {
                     $query = "SELECT `alias` FROM `#__menu` WHERE `id` = {$r->parent_id};";
                     $this->db->setQuery($query);
                     $alias = $this->db->loadResult();
                     $path = $alias . '/' . $r->path;
                     $query = "UPDATE `#__menu` SET `path` = \"{$path}\" WHERE `id` = {$r->id};";
                     $this->db->setQuery($query);
                     $this->db->query();
                 }
             }
         }
         // Add entries for components menu on backend
         $query = "SELECT * FROM `#__components` WHERE `parent` = '0' AND `iscore` = '0' AND `enabled` = '1' AND `admin_menu_link` != '' AND `admin_menu_link` IS NOT NULL;";
         $this->db->setQuery($query);
         $results = $this->db->loadObjectList();
         if (count($results) > 0) {
             foreach ($results as $r) {
                 $alias = substr($r->option, 4);
                 $link = 'index.php?' . $r->admin_menu_link;
                 // Insert item
                 $query = "INSERT INTO `#__menu` (`menutype`, `title`, `alias`, `path`, `link`, `type`, `published`, `parent_id`, `level`, `component_id`, `language`, `client_id`)\n";
                 $query .= "VALUES ('main', '{$r->option}', '{$alias}', '{$alias}', '{$link}', 'component', 1, 1, 1, {$r->id}, '*', 1);";
                 $this->db->setQuery($query);
                 $this->db->query();
             }
         }
         // If we have the nested set class available, use it to rebuild lft/rgt
         if (class_exists('JTableNested') && method_exists('JTableNested', 'rebuild')) {
             // Use the MySQL driver for this
             $config = \JFactory::getConfig();
             $database = \JDatabase::getInstance(array('driver' => 'mysql', 'host' => $config->getValue('host'), 'user' => $config->getValue('user'), 'password' => $config->getValue('password'), 'database' => $config->getValue('db')));
             $table = new \JTableMenu($database);
             $table->rebuild();
         }
         // Update menu params (specifically to fix menu_image)
         $query = "SELECT `id`, `params`, `link` FROM `#__menu`;";
         $this->db->setQuery($query);
         $results = $this->db->loadObjectList();
         if (count($results) > 0) {
             foreach ($results as $r) {
                 $params = trim($r->params);
                 if (empty($params) || $params == '{}') {
                     continue;
                 }
                 $array = array();
                 $ar = explode("\n", $params);
                 foreach ($ar as $a) {
                     $a = trim($a);
                     if (empty($a)) {
                         continue;
                     }
                     $ar2 = explode("=", $a, 2);
                     if ($ar2[0] == 'menu_image' && $ar2[1] == "-1") {
                         $ar2[1] = "0";
                     }
                     $array[$ar2[0]] = isset($ar2[1]) ? $ar2[1] : '';
                 }
                 // Check to see if this menu item points to an article
                 preg_match('/index\\.php\\?option=com_content&view=article&id=([0-9]+)/', $r->link, $matches);
                 // Need to merge in content params (if applicable), as menu item params now take precidence
                 if (isset($matches[1]) && !empty($matches[1])) {
                     $query = "SELECT `attribs` FROM `#__content` WHERE `id` = '{$matches[1]}';";
                     $this->db->setQuery($query);
                     $art_params = json_decode($this->db->loadResult());
                     foreach ($art_params as $k => $v) {
                         if ($v !== null && $v !== '' && array_key_exists($k, $array)) {
                             $array[$k] = $v;
                         }
                     }
                 }
                 $query = "UPDATE `#__menu` SET `params` = " . $this->db->Quote(json_encode($array)) . " WHERE `id` = {$r->id};";
                 $this->db->setQuery($query);
                 $this->db->query();
             }
         }
         // Update component_id -> extension_id
         $query = "SELECT `id`, `link`, `component_id` FROM `#__menu` WHERE `component_id` != '0';";
         $this->db->setQuery($query);
         $results = $this->db->loadObjectList();
         if (count($results) > 0) {
             foreach ($results as $r) {
                 preg_match('/index\\.php\\?option=([a-z0-9_]+)/', $r->link, $matches);
                 if (isset($matches[1]) && !empty($matches[1])) {
                     $query = "SELECT `extension_id` FROM `#__extensions` WHERE `element` = '{$matches[1]}' AND `type` = 'component' ORDER BY `client_id` ASC LIMIT 1;";
                     $this->db->setQuery($query);
                     $id = $this->db->loadResult();
                     $id = !is_null($id) ? $id : '0';
                     $query = "UPDATE `#__menu` SET `component_id` = '{$id}' WHERE `id` = '{$r->id}';";
                     $this->db->setQuery($query);
                     $this->db->query();
                 }
             }
         }
         // Set language for all menu items
         $query = "UPDATE `#__menu` SET `language` = '*';";
         $this->db->setQuery($query);
         $this->db->query();
         // Fix com_user->com_users in menu items
         $query = "SELECT `extension_id` FROM `#__extensions` WHERE `element` = 'com_users';";
         $this->db->setQuery($query);
         $id = $this->db->loadResult();
         $query = "SELECT * FROM `#__menu` WHERE `menutype` = 'default' AND (`alias` = 'login' OR `alias` = 'logout' OR `alias` = 'remind' OR `alias` = 'reset');";
         $this->db->setQuery($query);
         if ($results = $this->db->loadObjectList()) {
             foreach ($results as $r) {
                 $link = preg_replace('/(index\\.php\\?option=com_user)(&view=[a-z]+)/', '${1}s${2}', $r->link);
                 $params = json_decode($r->params);
                 if ($r->alias == 'login') {
                     $params->login_redirect_url = $params->login;
                     unset($params->login);
                 }
                 $query = "UPDATE `#__menu` SET `link` = " . $this->db->quote($link) . ", `component_id` = '{$id}', `params` = " . $this->db->quote(json_encode($params)) . " WHERE `id` = '{$r->id}';";
                 $this->db->setQuery($query);
                 $this->db->query();
             }
         }
         // Fix menu link type menu items to be alias type
         $query = "UPDATE `#__menu` SET `type` = 'alias', `link` = 'index.php?Itemid=', `params` = REPLACE(`params`, 'menu_item', 'aliasoptions') WHERE `type` = 'menulink'";
         $this->db->setQuery($query);
         $this->db->query();
     }
     // Now we can get rid of the components table as well
     if ($this->db->tableExists('#__components')) {
         $query = "DROP TABLE IF EXISTS `#__components`;";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if (!$this->db->tableHasKey('#__menu', 'idx_client_id_parent_id_alias_language') && $this->db->tableHasField('#__menu', 'client_id') && $this->db->tableHasField('#__menu', 'parent_id') && $this->db->tableHasField('#__menu', 'alias') && $this->db->tableHasField('#__menu', 'language')) {
         $query = "ALTER TABLE `#__menu` ADD INDEX `idx_client_id_parent_id_alias_language` (`client_id` ASC, `parent_id` ASC, `alias` ASC, `language` ASC);";
         $this->db->setQuery($query);
         $this->db->query();
     }
 }