Skip to content

Commit

Permalink
Simplify the admin menu building
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas K. Dionysopoulos committed Dec 23, 2014
1 parent b5f17a1 commit 616da6d
Showing 1 changed file with 61 additions and 66 deletions.
127 changes: 61 additions & 66 deletions libraries/cms/installer/adapter/component.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,11 @@ protected function storeExtension($deleteExisting = false)
$db = $this->parent->getDBO();

$query = $db->getQuery(true)
->select($db->qn('extension_id'))
->from($db->qn('#__extensions'))
->where($db->qn('name') . ' = ' . $db->q($this->extension->name))
->where($db->qn('type') . ' = ' . $db->q($this->extension->type))
->where($db->qn('element') . ' = ' . $db->q($this->extension->element));
->select($db->qn('extension_id'))
->from($db->qn('#__extensions'))
->where($db->qn('name') . ' = ' . $db->q($this->extension->name))
->where($db->qn('type') . ' = ' . $db->q($this->extension->type))
->where($db->qn('element') . ' = ' . $db->q($this->extension->element));

$db->setQuery($query);

Expand Down Expand Up @@ -1123,8 +1123,8 @@ public function uninstall($id)

// Remove the schema version
$query = $db->getQuery(true)
->delete('#__schemas')
->where('extension_id = ' . $id);
->delete('#__schemas')
->where('extension_id = ' . $id);
$db->setQuery($query);
$db->execute();

Expand All @@ -1138,9 +1138,9 @@ public function uninstall($id)

// Remove categories for this component
$query->clear()
->delete('#__categories')
->where('extension=' . $db->quote($this->element), 'OR')
->where('extension LIKE ' . $db->quote($this->element . '.%'));
->delete('#__categories')
->where('extension=' . $db->quote($this->element), 'OR')
->where('extension LIKE ' . $db->quote($this->element . '.%'));
$db->setQuery($query);
$db->execute();

Expand Down Expand Up @@ -1200,27 +1200,26 @@ public function uninstall($id)
/**
* Method to build menu database entries for a component
*
* @param int|null $component_id The component ID for which I'm building menus
*
* @return boolean True if successful
*
* @since 3.1
*/
protected function _buildAdminMenus()
protected function _buildAdminMenus($component_id = null)
{
$db = $this->parent->getDbo();

/** @var JTableMenu $table */
$table = JTable::getInstance('menu');

$option = $this->get('element');

// If a component exists with this option in the table then we don't need to add menus
$query = $db->getQuery(true)
->select('m.id, e.extension_id')
->from('#__menu AS m')
->join('LEFT', '#__extensions AS e ON m.component_id = e.extension_id')
->where('m.parent_id = 1')
->where('m.client_id = 1')
->where('e.element = ' . $db->quote($option));
->select('m.id, e.extension_id')
->from('#__menu AS m')
->join('LEFT', '#__extensions AS e ON m.component_id = e.extension_id')
->where('m.parent_id = 1')
->where('m.client_id = 1')
->where('e.element = ' . $db->quote($option));

$db->setQuery($query);

Expand Down Expand Up @@ -1248,15 +1247,19 @@ protected function _buildAdminMenus()
}
}

// Lets find the extension id
$query->clear()
->select('e.extension_id')
->from('#__extensions AS e')
->where('e.type = ' . $db->quote('component'))
->where('e.element = ' . $db->quote($option));
// Only try to detect the component ID if it's not provided
if (empty($component_id))
{
// Lets find the extension id
$query->clear()
->select('e.extension_id')
->from('#__extensions AS e')
->where('e.type = ' . $db->quote('component'))
->where('e.element = ' . $db->quote($option));

$db->setQuery($query);
$component_id = $db->loadResult();
$db->setQuery($query);
$component_id = $db->loadResult();
}

// Ok, now its time to handle the menus. Start with the component root menu, then handle submenus.
$menuElement = $this->manifest->administration->menu;
Expand Down Expand Up @@ -1309,22 +1312,11 @@ protected function _buildAdminMenus()
}

// Try to create the menu item in the database
try
{
$table->setLocation(1, 'last-child');
}
catch (InvalidArgumentException $e)
{
JLog::add($e->getMessage(), JLog::WARNING, 'jerror');
$parent_id = $this->_createAdminMenuItem($data, 1);

return false;
}

$result = $this->_createAdminMenuItem($table, $data);

if ($result === false)
if ($parent_id === false)
{
return $result;
return false;
}

/*
Expand All @@ -1337,8 +1329,6 @@ protected function _buildAdminMenus()
return true;
}

$parent_id = $table->id;

foreach ($this->manifest->administration->submenu->menu as $child)
{
$data = array();
Expand Down Expand Up @@ -1396,20 +1386,11 @@ protected function _buildAdminMenus()
$data['link'] = 'index.php?option=' . $option . $qstring;
}

try
{
$table->setLocation($parent_id, 'last-child');
}
catch (InvalidArgumentException $e)
{
return false;
}
$submenuId = $this->_createAdminMenuItem($data, $parent_id);

$result = $this->_createAdminMenuItem($table, $data);

if ($result === false)
if ($submenuId === false)
{
return $result;
return false;
}

/*
Expand Down Expand Up @@ -1440,10 +1421,10 @@ protected function _removeAdminMenus($id)

// Get the ids of the menu items
$query = $db->getQuery(true)
->select('id')
->from('#__menu')
->where($db->quoteName('client_id') . ' = 1')
->where($db->quoteName('component_id') . ' = ' . (int) $id);
->select('id')
->from('#__menu')
->where($db->quoteName('client_id') . ' = 1')
->where($db->quoteName('component_id') . ' = ' . (int) $id);

$db->setQuery($query);

Expand Down Expand Up @@ -1893,15 +1874,29 @@ public function refreshManifestCache()
/**
* Creates the menu item in the database. If the item already exists it tries to remove it and create it afresh.
*
* @param JTableMenu &$table The menu table on which to create the menu item
* @param array &$data The menu item data to create
* @param array &$data The menu item data to create
* @param integer $parentId The parent menu item ID
*
* @return bool True on success
* @return bool|int Menu item ID on success, false on failure
*/
protected function _createAdminMenuItem(JTableMenu &$table, array &$data)
protected function _createAdminMenuItem(array &$data, $parentId)
{
$db = $this->parent->getDbo();

/** @var JTableMenu $table */
$table = JTable::getInstance('menu');

try
{
$table->setLocation($parentId, 'last-child');
}
catch (InvalidArgumentException $e)
{
JLog::add($e->getMessage(), JLog::WARNING, 'jerror');

return false;
}

if ( !$table->bind($data) || !$table->check() || !$table->store())
{
// The menu item already exists. Delete it and retry instead of throwing an error.
Expand Down Expand Up @@ -1933,7 +1928,7 @@ protected function _createAdminMenuItem(JTableMenu &$table, array &$data)
$temporaryTable->rebuild($data['parent_id']);

// Retry creating the menu item
$table->setLocation(1, 'last-child');
$table->setLocation($parentId, 'last-child');

if ( !$table->bind($data) || !$table->check() || !$table->store())
{
Expand All @@ -1945,7 +1940,7 @@ protected function _createAdminMenuItem(JTableMenu &$table, array &$data)
}
}

return true;
return $table->id;
}
}

Expand Down

0 comments on commit 616da6d

Please sign in to comment.