<?php
/**
 * @file
 * Contains hooks and functions.
 */

define('WEBFORM_ARCHIVE_DIRECTORY', 'webform_archive_reports');
define('WEBFORM_ARCHIVE_BATCH_LIMIT', 10);

/**
 * Implements hook_permission().
 */
function webform_archive_permission() {
  return array(
    'administer webform archive' => array(
      'title' => t('Administer Webfrom Archive'),
      'description' => t('Access Webfrom Archive administration pages.'),
      'restrict access' => TRUE,
    ),
    'download webform archive reports' => array(
      'title' => t('Download Webfrom Archive Reports'),
      'description' => t('Download Webfrom Archive Reports.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function webform_archive_menu() {
  // UI page for webform archive.
  $items['admin/content/webform/archive-reports'] = array(
    'title' => 'Archived Webforms',
    'page callback' => 'webform_archive_report_page',
    'page arguments' => array('active'),
    'access arguments' => array('administer webform archive'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 100,
    'file' => 'includes/webform_archive.reports.inc',
  );

  $items['admin/config/content/webform/archive'] = array(
    'title' => 'Archived Webforms Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('webform_archive_admin_settings'),
    'access arguments' => array('administer webform archive'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 100,
    'file' => 'includes/webform_archive.admin.inc',
  );

  $items['admin/content/webform/archive-active'] = array(
    'title' => 'Archive Active Webforms',
    'page callback' => 'webform_archive_ui_page',
    'page arguments' => array('active'),
    'access arguments' => array('administer webform archive'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/webform_archive.admin.inc',
  );

  $items['admin/content/webform/archive-closed'] = array(
    'title' => 'Archive Closed Webforms',
    'page callback' => 'webform_archive_ui_page',
    'page arguments' => array('closed'),
    'access arguments' => array('administer webform archive'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/webform_archive.admin.inc',
  );

  // Download report link for the webforms.
  $items['node/%webform_menu/webform-results/download-reports'] = array(
    'title' => 'Archive Download',
    'page callback' => 'webform_archive_download_reports',
    'page arguments' => array(1),
    'access arguments' => array('download webform archive reports'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 100,
    'file' => 'includes/webform_archive.download.inc',
  );
  return $items;
}

/**
 * Gives Archive webform details.
 *
 * @return array
 *   Array containing webform archive details.
 */
function webform_archive_get_archived_webforms() {
  $archived_webforms = db_select('archive_webform', 'aw')
    ->fields('aw')
    ->execute()
    ->fetchAll();
  return $archived_webforms;
}

/**
 * Get All webforms according to their status.
 *
 * @param int $status
 *   Status of webform, 0 = closed, 1 = Active.
 *
 * @return array
 *   All webforms.
 */
function webform_archive_get_all_webforms($status = 1) {
  $webforms = array();
  $webform = db_select('webform', 'w');
  $webform->join('node', 'n', 'w.nid = n.nid');
  $webform->fields('n', array('nid', 'title'));
  $webform->condition('n.type', 'webform');
  $webform->condition('w.status', $status);
  $webform = $webform->execute();
  while ($data = $webform->fetchAssoc()) {
    $webforms[$data['nid']] = $data['title'];
  }
  return $webforms;
}

/**
 * Gives basic submission information required for report generation.
 *
 * @return array
 *   Basic submission information required for report generation.
 */
function webform_archive_webform_basic_information_fields() {
  $submission_information = &drupal_static(__FUNCTION__);
  if (!isset($submission_information)) {
    // Basic Submission information.
    $submission_information = array(
      'nid' => t('Title'),
      'sid' => t('SID'),
      'submitted' => t('Submission Time'),
      'remote_addr' => t('IP Address'),
      'uid' => t('UID'),
    );
    $webform_info = system_get_info('module', 'webform');
    $version = $webform_info['version'];
    if (version_compare($version, '7.x-4.0') == 1) {
      $submission_information['serial'] = t('Serial');
    }
  }
  return $submission_information;
}
