You are here

advanced_menu_menu_attributes.module in Advanced Menu 7

Same filename and directory in other branches
  1. 6.x 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.
 */

/**
 * Helper function to retrieve the expiration date of a given menu.
 */
function _advanced_menu_menu_attributes_expire($mlid) {
  $exp_date = db_select('advanced_menu_menu_attributes_expire', 'a')
    ->fields('a', array(
    'expire',
  ))
    ->condition('mlid', $mlid)
    ->execute()
    ->fetchField();
  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_name'];
      $menu_mlid = $menu['mlid'];
      $menu_date = $menu['expire_date'];
      db_delete('advanced_menu_menu_attributes_expire')
        ->condition('mlid', $menu_mlid)
        ->execute();
      if ($menu_date != '') {
        $menu_date = strtotime($menu_date);
        db_insert('advanced_menu_menu_attributes_expire')
          ->fields(array(
          'mlid' => $menu_mlid,
          'expire' => $menu_date,
        ))
          ->execute();
      }
      break;
  }
}

/**
 * Implementation of hook_cron().
 */
function advanced_menu_menu_attributes_cron() {
  $changed = FALSE;
  $menus = db_select('advanced_menu_menu_attributes_expire', 'e')
    ->fields('e')
    ->condition('expire', time(), '<')
    ->execute();
  foreach ($menus as $menu) {
    $changed = TRUE;
    db_update('menu_links')
      ->condition('mlid', $menu->mlid)
      ->fields(array(
      'hidden' => 1,
    ))
      ->execute();
  }
  if ($changed) {
    menu_rebuild();
  }
}

/**
 * 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['mlid']['#value'];
      $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;
  }
}

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.