<?php
// $Id$

$path = drupal_get_path('module', 'mapi_admin');
require_once($path .'/forms.inc');


/**
 * Implementation of hook_perm()
 */
function mapi_admin_perm() {
  return array(
    'administer mapi',  // can administer the mapi module settings
  );
}

/**
 * Implementation of hook_menu()
 */
function mapi_admin_menu($may_cache) {
  $items = array();

  // for those menu items which can be cached
  if ($may_cache) {
    // base for settings for media-related modules.
    $items = mapi_admin_invoke('menu', false);

    // and the default
    $items[] = array(
      'path' => MAPI_ADMIN_PATH,
      'title' => t('Media management'),
      'description' => t("Manage your site's media."),
      'position' => 'left',
      'weight' => -10,
      'callback' => 'system_admin_menu_block_page',
      'access' => user_access('administer mapi')
    );
  }
  else {
    $items = mapi_admin_invoke('menu', true);
  }

  return $items;
}

/**
 * Implementation of hook_mapi_admin()
 */
function mapi_admin_mapi_admin($op, $data) {
  switch ($op) {
    case 'menu':
      // define sections and their descriptions
      $sections = array(
        'setting'   => array('title' => t('Settings'), 'description' => t('Modifies the general settings for the media API.')),
        'extension' => array('title' => t('Extensions'), 'description' => t('List and manages extensions available.')),
        'type'      => array('title' => t('Types'), 'description' => t('Shows the types, and their associated extensions.')),
        'presenter' => array('title' => t('Presenters'), 'description' => t('Manages the presenters available to users, to present the media files.')),
        'profile'  => array('title' => t('Profiles'), 'description' => t('Profiles determine consistant presentation throughout a website.')),
        'program'   => array('title' => t('Programs'), 'description' => t('Provides use of external programs, for transformations and other actions regarding media.')),
      );

      // create both sections
      foreach ($sections as $section => $details) {
        // section menu item
        if (!$data) {
          $items[] = array(
            'path' => MAPI_ADMIN_PATH ."/{$section}s",
            'title' => $details['title'],
            'description' => $details['description'],
            'callback arguments' => array("mapi_admin_{$section}s_form", null),
          );
        }
        // sub-section menu items
        else {
          $func = "mapi_{$section}_list";
          if (function_exists($func)) {
            foreach ($func() as $key) {
              $items[] = array(
                'path' => MAPI_ADMIN_PATH ."/{$section}s/{$key}",
                'title' => $key,
                'callback arguments' => array("mapi_admin_{$section}s_form", $key),
              );
            }
          }
        }
      }

      // special handling for the profiles
      if (!$data) {
        $items[] = array(
          'path' => MAPI_ADMIN_PATH ."/profiles/add",
          'title' => t('Add Profile'),
          'description' => $details['description'],
          'callback arguments' => array("mapi_admin_profiles_add_form", null),
        );
      }
      else {
        foreach (mapi_profile_list() as $profile) {
          $items[] = array(
            'path' => MAPI_ADMIN_PATH ."/profiles/". $profile .'/delete',
            'title' => t('Delete Profile'),
            'callback arguments' => array("mapi_admin_profiles_delete_form", $profile),
            'type' => MENU_CALLBACK
          );
        }
      }

      return $items;
  }
}

/**
 *
 */
function mapi_admin_invoke($op, $data = null) {
  $options = module_invoke_all('mapi_admin', $op, $data);
  foreach ($options as $key => $value) {
    // make sure that it matches what we're expecting
    switch ($op) {
      case 'menu':
        // a path is required to be a default menu item
        if (!empty($value['path'])) {
          // defaults to MENU_NORMAL_ITEM
          $options[$key]['type'] = (isset($value['type']) ? $value['type'] : MENU_NORMAL_ITEM);

          // all access should be checked against 'administer mapi'
          $options[$key]['access'] = (isset($value['access']) ? $value['access'] : 1) && user_access('administer mapi');

          // default callback is drupal_get_form, this is possible to override
          $options[$key]['callback'] = (isset($value['callback']) ? $value['callback'] : 'drupal_get_form');

          // allows a simple string for callback arguments in hook menu, defaults to no extra arguments
          $options[$key]['callback arguments'] = (is_string($value['callback arguments']) ? array($value['callback arguments']) : $value['callback arguments']);
        }
        // remove, it doesn't have a path
        else {
          unset($options[$key]);
        }
        break;
      case 'paths':
        if (!is_string($key) || !is_dir($value)) {
          unset($options[$key]);
        }
        break;
    }
  }
  return $options;
}
