Commit f3526a34 authored by Dries's avatar Dries

- Patch #394476 by Berdir, pwolanin, Crell: converted the module to the new...

- Patch #394476 by Berdir, pwolanin, Crell: converted the module to the new database abstraction layer.
parent ba40a416
......@@ -10,10 +10,10 @@
* Menu callback which shows an overview page of all the custom menus and their descriptions.
*/
function menu_overview_page() {
$result = db_query("SELECT * FROM {menu_custom} ORDER BY title");
$result = db_query("SELECT * FROM {menu_custom} ORDER BY title", array(), array('fetch' => PDO::FETCH_ASSOC));
$header = array(t('Title'), array('data' => t('Operations'), 'colspan' => '3'));
$rows = array();
while ($menu = db_fetch_array($result)) {
foreach ($result as $menu) {
$row = array(theme('menu_admin_overview', $menu['title'], $menu['menu_name'], $menu['description']));
$row[] = array('data' => l(t('list links'), 'admin/build/menu-customize/' . $menu['menu_name']));
$row[] = array('data' => l(t('edit menu'), 'admin/build/menu-customize/' . $menu['menu_name'] . '/edit'));
......@@ -45,9 +45,9 @@ function menu_overview_form(&$form_state, $menu) {
$sql = "
SELECT m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.title, m.title_callback, m.title_arguments, m.type, m.description, ml.*
FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
WHERE ml.menu_name = '%s'
WHERE ml.menu_name = :menu
ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC";
$result = db_query($sql, $menu['menu_name']);
$result = db_query($sql, array(':menu' => $menu['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
$tree = menu_tree_data($result);
$node_links = array();
menu_tree_collect_node_links($tree, $node_links);
......@@ -477,7 +477,7 @@ function menu_delete_menu_page($menu) {
function menu_delete_menu_confirm(&$form_state, $menu) {
$form['#menu'] = $menu;
$caption = '';
$num_links = db_result(db_query("SELECT COUNT(*) FROM {menu_links} WHERE menu_name = '%s'", $menu['menu_name']));
$num_links = db_query("SELECT COUNT(*) FROM {menu_links} WHERE menu_name = :menu", array(':menu' => $menu['menu_name']))->fetchField();
if ($num_links) {
$caption .= '<p>' . format_plural($num_links, '<strong>Warning:</strong> There is currently 1 menu link in %title. It will be deleted (system-defined items will be reset).', '<strong>Warning:</strong> There are currently @count menu links in %title. They will be deleted (system-defined links will be reset).', array('%title' => $menu['title'])) . '</p>';
}
......@@ -493,26 +493,36 @@ function menu_delete_menu_confirm_submit($form, &$form_state) {
$form_state['redirect'] = 'admin/build/menu';
// System-defined menus may not be deleted - only menus defined by this module.
$system_menus = menu_list_system_menus();
if (isset($system_menus[$menu['menu_name']]) || !(db_query("SELECT 1 FROM {menu_custom} WHERE menu_name = :name", array('name' => $menu['menu_name']))->fetchField())) {
if (isset($system_menus[$menu['menu_name']]) || !(db_query("SELECT 1 FROM {menu_custom} WHERE menu_name = :menu", array(':menu' => $menu['menu_name']))->fetchField())) {
return;
}
// Reset all the menu links defined by the system via hook_menu.
$result = db_query("SELECT * FROM {menu_links} ml INNER JOIN {menu_router} m ON ml.router_path = m.path WHERE ml.menu_name = '%s' AND ml.module = 'system' ORDER BY m.number_parts ASC", $menu['menu_name']);
while ($item = db_fetch_array($result)) {
$result = db_query("SELECT * FROM {menu_links} ml INNER JOIN {menu_router} m ON ml.router_path = m.path WHERE ml.menu_name = :menu AND ml.module = 'system' ORDER BY m.number_parts ASC", array(':menu' => $menu['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
foreach ($result as $item) {
menu_reset_item($item);
}
// Delete all links to the overview page for this menu.
$result = db_query("SELECT mlid FROM {menu_links} ml WHERE ml.link_path = '%s'", 'admin/build/menu-customize/' . $menu['menu_name']);
while ($m = db_fetch_array($result)) {
$result = db_query("SELECT mlid FROM {menu_links} ml WHERE ml.link_path = :link", array(':link' => 'admin/build/menu-customize/' . $menu['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
foreach ($result as $m) {
menu_link_delete($m['mlid']);
}
// Delete all the links in the menu and the menu from the list of custom menus.
db_query("DELETE FROM {menu_links} WHERE menu_name = '%s'", $menu['menu_name']);
db_query("DELETE FROM {menu_custom} WHERE menu_name = '%s'", $menu['menu_name']);
db_delete('menu_links')
->condition('menu_name', $menu['menu_name'])
->execute();
db_delete('menu_custom')
->condition('menu_name', $menu['menu_name'])
->execute();
// Delete all the blocks for this menu.
if (module_exists('block')) {
db_query("DELETE FROM {block} WHERE module = 'menu' AND delta = '%s'", $menu['menu_name']);
db_query("DELETE FROM {block_role} WHERE module = 'menu' AND delta = '%s'", $menu['menu_name']);
db_delete('block')
->condition('module', 'menu')
->condition('delta', $menu['menu_name'])
->execute();
db_delete('block_role')
->condition('module', 'menu')
->condition('delta', $menu['menu_name'])
->execute();
}
menu_cache_clear_all();
cache_clear_all();
......@@ -535,8 +545,9 @@ function menu_edit_menu_validate($form, &$form_state) {
if ($form['#insert']) {
// We will add 'menu-' to the menu name to help avoid name-space conflicts.
$item['menu_name'] = 'menu-' . $item['menu_name'];
if (db_result(db_query("SELECT menu_name FROM {menu_custom} WHERE menu_name = '%s'", $item['menu_name'])) ||
db_result(db_query_range("SELECT menu_name FROM {menu_links} WHERE menu_name = '%s'", $item['menu_name'], 0, 1))) {
$custom_exists = db_query('SELECT menu_name FROM {menu_custom} WHERE menu_name = :menu', array(':menu' => $item['menu_name']))->fetchField();
$link_exists = db_query_range("SELECT menu_name FROM {menu_links} WHERE menu_name = :menu", array(':menu' => $item['menu_name']), 0, 1)->fetchField();
if ($custom_exists || $link_exists) {
form_set_error('menu_name', t('The menu already exists.'));
}
}
......@@ -555,14 +566,31 @@ function menu_edit_menu_submit($form, &$form_state) {
$link['link_path'] = $path . $menu['menu_name'];
$link['router_path'] = $path . '%';
$link['module'] = 'menu';
$link['plid'] = db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path = '%s' AND module = '%s'", 'admin/build/menu', 'system'));
$link['plid'] = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :link AND module = :module", array(
':link' => 'admin/build/menu',
':module' => 'system'
))
->fetchField();
menu_link_save($link);
db_query("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('%s', '%s', '%s')", $menu['menu_name'], $menu['title'], $menu['description']);
db_insert('menu_custom')
->fields(array(
'menu_name' => $menu['menu_name'],
'title' => $menu['title'],
'description' => $menu['description'],
))
->execute();
}
else {
db_query("UPDATE {menu_custom} SET title = '%s', description = '%s' WHERE menu_name = '%s'", $menu['title'], $menu['description'], $menu['menu_name']);
$result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = '%s'", $path . $menu['menu_name']);
while ($m = db_fetch_array($result)) {
db_update('menu_custom')
->fields(array(
'title' => $menu['title'],
'description' => $menu['description'],
))
->condition('menu_name', $menu['menu_name'])
->execute();
$result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path", array(':path' => $path . $menu['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
foreach ($result as $m) {
$link = menu_link_load($m['mlid']);
$link['link_title'] = $menu['title'];
menu_link_save($link);
......
......@@ -158,17 +158,22 @@ function menu_theme() {
*/
function menu_enable() {
menu_rebuild();
$base_link = db_fetch_array(db_query("SELECT mlid AS plid, menu_name from {menu_links} WHERE link_path = 'admin/build/menu' AND module = 'system'"));
$base_link = db_query("SELECT mlid AS plid, menu_name from {menu_links} WHERE link_path = 'admin/build/menu' AND module = 'system'")->fetchAssoc();
$base_link['router_path'] = 'admin/build/menu-customize/%';
$base_link['module'] = 'menu';
$result = db_query("SELECT * FROM {menu_custom}");
while ($menu = db_fetch_array($result)) {
$result = db_query("SELECT * FROM {menu_custom}", array(), array('fetch' => PDO::FETCH_ASSOC));
foreach ($result as $menu) {
// $link is passed by reference to menu_link_save(), so we make a copy of $base_link.
$link = $base_link;
$link['mlid'] = 0;
$link['link_title'] = $menu['title'];
$link['link_path'] = 'admin/build/menu-customize/' . $menu['menu_name'];
if (!db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path = '%s' AND plid = %d", $link['link_path'], $link['plid']))) {
$menu_link = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path AND plid = :plid", array(
':path' => $link['link_path'],
':plid' => $link['plid']
))
->fetchField();
if (!$menu_link) {
menu_link_save($link);
}
}
......@@ -186,7 +191,7 @@ function menu_overview_title($menu) {
* Load the data for a single custom menu.
*/
function menu_load($menu_name) {
return db_fetch_array(db_query("SELECT * FROM {menu_custom} WHERE menu_name = '%s'", $menu_name));
return db_query("SELECT * FROM {menu_custom} WHERE menu_name = :menu", array(':menu' => $menu_name))->fetchAssoc();
}
/**
......@@ -335,8 +340,8 @@ function menu_node_update(&$node) {
*/
function menu_node_delete(&$node) {
// Delete all menu module links that point to this node.
$result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu'", array(':path' => 'node/'. $node->nid));
while ($m = db_fetch_array($result)) {
$result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu'", array(':path' => 'node/'. $node->nid), array('fetch' => PDO::FETCH_ASSOC));
foreach ($result as $m) {
menu_link_delete($m['mlid']);
}
}
......@@ -351,15 +356,17 @@ function menu_node_prepare(&$node) {
$item = array();
if (isset($node->nid)) {
// Give priority to the default menu
$mlid = db_result(db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND menu_name = :menu_name AND module = 'menu' ORDER BY mlid ASC", array(
$mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND menu_name = :menu_name AND module = 'menu' ORDER BY mlid ASC", array(
':path' => 'node/'. $node->nid,
':menu_name' => $menu_name,
), 0, 1));
), 0, 1)
->fetchField();
// Check all menus if a link does not exist in the default menu.
if (!$mlid) {
$mlid = db_result(db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu' ORDER BY mlid ASC", array(
$mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu' ORDER BY mlid ASC", array(
':path' => 'node/'. $node->nid,
), 0, 1));
), 0, 1)
->fetchField();
}
if ($mlid) {
$item = menu_link_load($mlid);
......
......@@ -165,7 +165,7 @@ class MenuTestCase extends DrupalWebTestCase {
$this->drupalPost('admin/build/menu-customize/'. $item1['menu_name'], $edit, t('Save configuration'));
// Verify in the database.
$hidden = db_result(db_query("SELECT hidden FROM {menu_links} WHERE mlid = :mlid", array('mlid' => $item1['mlid'])));
$hidden = db_query("SELECT hidden FROM {menu_links} WHERE mlid = :mlid", array(':mlid' => $item1['mlid']))->fetchField();
$this->assertEqual($hidden, 0, t('Link is not hidden in the database table when enabled via the overview form'));
// Save menu links for later tests.
......@@ -205,7 +205,7 @@ class MenuTestCase extends DrupalWebTestCase {
$this->assertText($title, 'Menu link was added');
// Retrieve menu link.
$item = db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE link_title = '%s'", $title));
$item = db_query("SELECT * FROM {menu_links} WHERE link_title = :title", array(':title' => $title))->fetchAssoc();
// Check the structure in the DB of the two menu links.
// In general, if $n = $item['depth'] then $item['p'. $n] == $item['mlid'] and $item['p' . ($n - 1)] == $item['plid'] (unless depth == 0).
......@@ -389,7 +389,7 @@ class MenuTestCase extends DrupalWebTestCase {
$this->drupalPost("admin/build/menu/item/$mlid/edit", $edit, t('Save'));
// Verify in the database.
$hidden = db_result(db_query("SELECT hidden FROM {menu_links} WHERE mlid = :mlid", array('mlid' => $mlid)));
$hidden = db_query("SELECT hidden FROM {menu_links} WHERE mlid = :mlid", array(':mlid' => $mlid))->fetchField();
$this->assertEqual($hidden, 0, t('Link is not hidden in the database table'));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment