Commit 23684410 authored by profix898's avatar profix898

- feature: ability to move gallery links out of nav menu (#226622)

- task: reanimate GalleryMenu after router patch (#224548)
parent 45f63bd6
......@@ -5,4 +5,4 @@ description = "Gallery2 albums => Drupal menu items"
package = "Gallery2"
dependencies[] = gallery
;core = 6.x
core = 6.x
<?php
// $Id$
/**
* gallery_menu.module : gallery_menu.install
* Install/Uninstall functions
*/
/**
* Implementation of hook_install().
*/
function gallery_menu_install() {
}
/**
* Implementation of hook_update_N().
*/
function gallery_menu_update_6000() {
$ret = array();
// Remove all 'gallery_menu_*' variables except 'gallery_menu_depth'
$depth = variable_get('gallery_menu_depth', 3);
$ret[] = update_sql("DELETE FROM {variable} WHERE name LIKE 'gallery_menu_%'");
variable_set('gallery_menu_depth', $depth);
return $ret;
}
/**
* Implementation of hook_uninstall().
*/
function gallery_menu_uninstall() {
db_query("DELETE FROM {variable} WHERE name LIKE '%s'", 'gallery_menu_%');
}
......@@ -5,17 +5,6 @@
* gallery_menu.module
*/
/**
* Implementation of hook_theme().
*/
function gallery_menu_theme() {
return array(
'gallery_menu_settings_table' => array(
'arguments' => array('form' => NULL, 'rows' => NULL),
)
);
}
/**
* Class G2DrupalMenuEventListener {}.
*/
......@@ -35,7 +24,7 @@ class G2DrupalMenuEventListener /* extends GalleryEventListener */ {
*/
function gallery_menu_gallery_init_alter($params, $context) {
if (!$context['fullInit'] || !$context['ready']['half']) {
// We need to rebuild the menu if a G2 album is being updated
// We want to update the menu links everytime a G2 album changes
$params['eventListeners'][] = array(
'class' => 'G2DrupalMenuEventListener',
'path' => drupal_get_path('module', 'gallery_menu') .'/gallery_menu.module',
......@@ -116,20 +105,44 @@ function gallery_menu_gallery_page_alter(&$result) {
/**
* Function gallery_menu_build_links().
* (check for existing items and insert/update/delete as suitable)
*/
function gallery_menu_build_links() {
// Delete all gallery_menu link items
db_query("DELETE FROM {menu_links} WHERE link_path LIKE '%s/%'", variable_get('gallery_base', 'gallery'));
// Rebuild the link items
// Derive array of link items from G2 album tree
$items = array();
$depth = variable_get('gallery_menu_depth', 3);
$tree = gallery_album_tree(NULL, $depth ? $depth : NULL);
_gallery_menu_traverse($tree);
_gallery_menu_traverse($tree, $items);
// Fetch all menu links currently present in Drupal
$olditems = array();
$result = db_query("SELECT link_path FROM {menu_links} WHERE module = '%s' AND link_path LIKE '%s'", 'gallery_menu', variable_get('gallery_base', 'gallery') .'/%');
while ($item = db_fetch_object($result)) {
$olditems[$item->link_path] = $item->link_path;
}
// Insert/update/delete link items
foreach ($items as $item) {
if (in_array($item['link_path'], $olditems)) {
// Update existing item
menu_link_maintain('gallery_menu', 'update', $item['link_path'], $item['link_title']);
unset($olditems[$item['link_path']]);
}
else {
// Add new item
menu_link_maintain('gallery_menu', 'insert', $item['link_path'], $item['link_title']);
}
}
// Remove obsolete items
foreach ($olditems as $item) {
menu_link_maintain('gallery_menu', 'delete', $item, '');
}
}
/**
* Function _gallery_menu_traverse().
*/
function _gallery_menu_traverse($tree) {
function _gallery_menu_traverse($tree, &$items) {
static $parents = array();
foreach (array_keys($tree) as $id) {
if (variable_get('gallery_menu_show_'. $id, 1)) {
......@@ -146,10 +159,10 @@ function _gallery_menu_traverse($tree) {
'link_title' => $album['title'],
'link_path' => $path
);
menu_link_save($item);
$items[] = $item;
if (count($tree[$id])) {
array_push($parents, $id);
_gallery_menu_traverse($tree[$id]);
_gallery_menu_traverse($tree[$id], $items);
array_pop($parents);
}
}
......@@ -163,11 +176,12 @@ function _gallery_menu_album_path($id) {
global $language;
$path = urldecode(gallery_generate_url(array('itemId' => $id), FALSE, FALSE));
// Strip off the base path ... and additional parameter (e.g. session id, etc.)
// Strip off the base path ... and additional parameters (e.g. session id, etc.)
$path = substr($path, strlen(base_path()));
if (($pos = strrpos($path, '/')) !== FALSE) {
$path = substr($path, 0, $pos + 1);
}
// Remove the language-prefix
if (function_exists('language_url_rewrite')) {
$args = explode('/', $path);
......@@ -181,49 +195,22 @@ function _gallery_menu_album_path($id) {
}
/**
* Implementation of hook_menu_link_alter().
* (reset properties of link items if user changed them via DnD)
* Implementation of hook_form_alter().
*/
function gallery_menu_menu_link_alter(&$item, $menu) {
$gallery_base = variable_get('gallery_base', 'gallery');
if (preg_match('/^'. preg_quote($gallery_base) .'\//i', $item['link_path']) && isset($item['mlid'])) {
$result = db_query("SELECT m.*, ml.* FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path WHERE ml.mlid = %d", $item['mlid']);
if ($old_item = db_fetch_array($result)) {
$item['plid'] = $old_item['plid'];
$item['customized'] = FALSE;
}
}
}
function gallery_menu_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'menu_overview_form') {
// Hide 'expanded', 'enabled', 'weight' and 'operation' elements in menu administration
$result = db_query("SELECT mlid FROM {menu_links} WHERE link_path LIKE '%s/%'", variable_get('gallery_base', 'gallery'));
while ($mlid = db_result($result)) {
$id = 'mlid:'. $mlid;
if (isset($form[$id])) {
$form[$id]['expanded']['#type'] = 'value';
$form[$id]['hidden']['#type'] = 'value';
$form[$id]['weight']['#type'] = 'value';
$form[$id]['operations'] = array();
}
}
}
else if ($form_id == '_gallery_settings_general') {
if ($form_id == '_gallery_settings_general' && _gallery_init(TRUE)) {
// Add menu settings to the general settings form
_gallery_menu_settings(&$form);
array_splice($form, 3, 0, _gallery_menu_settings());
$form['#submit'][] = '_gallery_menu_settings_submit';
}
}
/**
* Function _gallery_menu_settings().
*/
function _gallery_menu_settings(&$form) {
function _gallery_menu_settings() {
require_once(drupal_get_path('module', 'gallery') .'/gallery_settings.inc');
if (!_gallery_init(TRUE)) {
return array();
}
// Gallery menu settings
$form['menu'] = array(
'#type' => 'fieldset',
'#title' => t('Gallery menu settings'),
......@@ -238,13 +225,14 @@ function _gallery_menu_settings(&$form) {
'#description' => 'Depth of album hierarchy to include.'
);
$form['#submit'][] = '_gallery_menu_settings_submit';
return $form;
}
/**
* Function _gallery_menu_settings_submit().
*/
function _gallery_menu_settings_submit($form_id, $form_values) {
// Rebuild album links (router items first, then the actual menu links)
menu_rebuild();
gallery_menu_build_links();
}
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