You are here

advanced_menu_menu_attributes.module in Advanced Menu 6.x

Same filename and directory in other branches
  1. 7 menu_attributes/advanced_menu_menu_attributes.module

Provides advanced menu attributes.

File

menu_attributes/advanced_menu_menu_attributes.module
View source
<?php

// $Id: advanced_menu_menu_attributes.module $

/**
 * @file
 * Provides advanced menu attributes.
 */

/**
 * Implementation of hook_form_alter().
 */
function advanced_menu_menu_attributes_form_alter(&$form, $form_state, $form_id) {
  $menu = $form;
  switch ($form_id) {
    case 'menu_edit_item':
      $item = $form['menu']['#item'] ? $form['menu']['#item'] : menu_load($form['menu']['original_item']['#value']['menu_name']);
      $form['menu']['expire_date'] = array(
        '#type' => 'textfield',
        '#title' => t('Expiration Date'),
        '#default_value' => _advanced_menu_menu_attributes_expire($item),
        '#description' => t('MM/DD/YYYY HH:MM:SS'),
        '#weight' => 1,
      );
      $form['submit']['#weight'] = 2;
      $form['#submit'][] = 'advanced_menu_menu_attributes_form_submit';
      break;
  }
}

/**
 * Helper function to retrieve the expiration date of a given menu.
 */
function _advanced_menu_menu_attributes_expire($menus = array()) {
  $exp_date = db_result(db_query("SELECT expire from {advanced_menu_menu_attributes_expire} WHERE mlid = %d", $menus['mlid']));
  if (!empty($exp_date)) {
    return date('m/d/Y H:i:s', $exp_date);
  }
}

/**
 * Save attribute information for this menu.
 */
function advanced_menu_menu_attributes_form_submit(&$form, $form_state) {
  $menu = $form_state['values'];
  $menu_name = '';
  $menu_mlid = 0;
  switch ($menu['form_id']) {
    case 'menu_edit_item':
      $menu_name = $menu['menu']['menu_name'];
      $menu_mlid = $menu['menu']['mlid'];
      $menu_date = $menu['menu']['expire_date'];
      db_query('DELETE FROM {advanced_menu_menu_attributes_expire} WHERE mlid=%d', $menu_mlid);
      if ($menu_date != '') {
        $menu_date = strtotime($menu_date);
        db_query('INSERT INTO {advanced_menu_menu_attributes_expire} (mlid, expire) VALUES (%d, %d)', $menu_mlid, $menu_date);
      }
      break;
  }
}

/**
 * Implementation of hook_cron().
 */
function advanced_menu_menu_attributes_cron() {
  $expired = db_result(db_query("SELECT COUNT(1) AS cnt FROM {advanced_menu_menu_attributes_expire} WHERE expire < UNIX_TIMESTAMP()"));
  if ($expired > 0) {
    db_query("UPDATE {menu_links} m, {advanced_menu_menu_attributes_expire} e set m.hidden=1 WHERE (e.mlid = m.mlid) AND (e.expire < UNIX_TIMESTAMP())");
    menu_rebuild();
  }
}

Functions

Namesort descending Description
advanced_menu_menu_attributes_cron Implementation of hook_cron().
advanced_menu_menu_attributes_form_alter Implementation of hook_form_alter().
advanced_menu_menu_attributes_form_submit Save attribute information for this menu.
_advanced_menu_menu_attributes_expire Helper function to retrieve the expiration date of a given menu.