<?php

/**
 * @file
 * Manifest module.
 * Keep lists of users.
 *
 */

module_load_include('inc', 'manifest', 'manifest.crud');

/**
 * Implements hook_permission().
 */
function manifest_permission() {
  return array(
    'administer manifest' => array(
      'title' => t('Administer manifest'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function manifest_menu() {
  $items = array();
  // Level 1
  $items['admin/people/manifest'] = array(
    'title' => 'Manifests',
    'page callback' => 'manifest_admin_list',
    'access arguments' => array('administer manifest'),
    'type' => MENU_LOCAL_TASK,
    'description' => 'Configure Manifest user lists.',
    'weight' => 0,
    'file' => 'manifest.pages.inc',
  );

  // Level 2
  $items['admin/people/manifest/list'] = array(
    'title' => 'Manifests',
    'page callback' => 'manifest_admin_list',
    'access arguments' => array('administer manifest'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'description' => 'Configure Manifest user lists.',
    'weight' => 1,
    'file' => 'manifest.pages.inc',
  );
  $items['admin/people/manifest/add'] = array(
    'title' => 'Add manifest',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('manifest_admin_edit'),
    'access arguments' => array('administer manifest'),
    'type' => MENU_LOCAL_ACTION,
    'description' => 'Create Manifest user list.',
    'weight' => 2,
    'file' => 'manifest.pages.inc',
    'context' => MENU_CONTEXT_PAGE,
  );

  // Level 3
  $items['admin/people/manifest/%manifest'] = array(
    'title' => 'Edit manifest',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('manifest_admin_edit', 3),
    'access arguments' => array('administer manifest'),
    'type' => MENU_NORMAL_ITEM,
    'weight' => 7,
    'file' => 'manifest.pages.inc',
  );
  $items['admin/people/manifest/%manifest/edit'] = array(
    'title' => 'Edit manifest',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('manifest_admin_edit', 3),
    'access arguments' => array('administer manifest'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 7,
    'file' => 'manifest.pages.inc',
    'context' => MENU_CONTEXT_PAGE,
  );
  $items['admin/people/manifest/%manifest/role'] = array(
    'title' => 'Manifest roles',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('manifest_admin_role', 3),
    'access arguments' => array('administer manifest'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 4,
    'file' => 'manifest.pages.inc',
    'context' => MENU_CONTEXT_PAGE,
  );
  $items['admin/people/manifest/%manifest/delete'] = array(
    'title' => 'Delete manifest',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('manifest_admin_delete', 3),
    'access arguments' => array('administer manifest'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 8,
    'file' => 'manifest.pages.inc',
    'context' => MENU_CONTEXT_PAGE,
  );
  $items['admin/people/manifest/%manifest/ip'] = array(
    'title' => 'Manifest IP addresses',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('manifest_admin_ip', 3),
    'access arguments' => array('administer manifest'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 5,
    'file' => 'manifest.pages.inc',
    'context' => MENU_CONTEXT_PAGE,
  );

  // Level 4
  $items['admin/people/manifest/%manifest/ip/%'] = array(
    'title' => 'Manifest IP addresses',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('manifest_admin_ip', 3),
    'access arguments' => array('administer manifest'),
    'type' => MENU_NORMAL_ITEM,
    'weight' => 6,
    'file' => 'manifest.pages.inc',
    'context' => MENU_CONTEXT_PAGE,
  );
  $items['admin/people/manifest/%manifest/ip/%/delete'] = array(
    'title' => 'Delete IP address',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('manifest_admin_ip_delete', 3, 5),
    'access arguments' => array('administer manifest'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 6,
    'file' => 'manifest.pages.inc',
    'context' => MENU_CONTEXT_PAGE,
  );

  return $items;
}

/**
 * Implements hook_user_operations().
 */
function manifest_user_operations() {
  $operations = array();
  $manifests = manifest_load_multiple();
  if ($manifests) {
     foreach ($manifests as $manifest) {
       if (!empty($manifest->settings['user']['operations'])) {
         $operations['manifest_add_' . $manifest->name] = array(
           'label' => t('Add users to !manifest manifest', array('!manifest' => manifest_title($manifest))),
           'callback' => 'manifest_user_operations_add',
           'callback arguments' => array($manifest->name),
         );
         $operations['manifest_remove_' . $manifest->name] = array(
           'label' => t('Remove users from !manifest manifest', array('!manifest' => manifest_title($manifest))),
           'callback' => 'manifest_user_operations_remove',
           'callback arguments' => array($manifest->name),
         );
       }
     }
  }
  return $operations;
}

/**
 * Callback function for hook_user_operations().
 */
function manifest_user_operations_add($accounts, $manifest_name) {
  foreach ($accounts as $uid) {
    manifest_account_save($uid, $manifest_name);
  }
}

/**
 * Callback function for hook_user_operations().
 */
function manifest_user_operations_remove($accounts, $manifest_name) {
  foreach ($accounts as $uid) {
    manifest_account_delete($uid, $manifest_name);
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function manifest_form_user_account_form_alter(&$form, &$form_state) {
  if (user_access('administer manifest')) {
    $account = $form['#user'];
    $manifests = manifest_load_multiple();
    $manifest_options = array();
    if ($manifests) {
      foreach ($manifests as $manifest) {
        if (!empty($manifest->settings['user']['profile'])) {
          $manifest_options[$manifest->name] = manifest_title($manifest);
        }
      }
    }
    if (!empty($manifest_options)) {
      $default = array();
      $entries = manifest_account_load($account->uid);
      if ($entries) {
        foreach ($entries as $entry) {
          $default[] = $entry->manifest;
        }
      }
      $form['manifest'] = array(
        '#type' => 'fieldset',
        '#title' => t('Manifest'),
      );
      $form['manifest']['manifests'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Manifests'),
        '#options' => $manifest_options,
        '#default_value' => $default,
        '#description' => t('Add this user to manifests.'),
      );
    }
  }
}

/**
 * Implements hook_user_update().
 */
function hook_user_update(&$edit, $account, $category) {
  if (!empty($edit['manifests'])) {
    if (is_string($edit['manifests'])) {
      $edit['manifests'] = array($edit['manifests'] => $edit['manifests']);
    }
    foreach ($edit['manifests'] as $manifest_name => $manifest) {
      if ($manifest) {
        manifest_account_save($account->uid, $manifest_name);
      }
      else {
        manifest_account_delete($account->uid, $manifest_name);
      }
    }
    unset($edit['manifests']);
  }
}

/**
 * Get the display title of a manifest.
 */
function manifest_title($manifest) {
  if (!empty($manifest->settings['title'])) {
    return check_plain($manifest->settings['title']);
  }
  return drupal_ucfirst(check_plain($manifest->name));
}

// @todo Make a page that lists users.