<?php
// $Id: l10n_project.module,v 1.1.2.5 2009/08/16 15:00:51 goba Exp $

/**
 * @file
 *   Localization community local project module integration
 *
 *   Maintains a copy of project and release listings based on a local database
 *   of drupal.org. Requires the project database to be accessible via the
 *   'projects' database set in settings.php.
 */

// = Core hooks ================================================================

/**
 * Implementation of hook_menu().
 */
function l10n_project_menu() {
  $items = array();
  $items['admin/l10n_server/l10n_project'] = array(
    'title' => 'Local project module integration',
    'description' => 'Configure the project module integration and parsing.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('l10n_project_settings_form'),
    'access arguments' => array('administer localization community local project module integration'),
  );
  $items['admin/l10n_server/l10n_project/configure'] = array(
    'title' => 'Configure',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/l10n_server/l10n_project/scan'] = array(
    'title' => 'Batch parse releases',
    'access arguments' => array('administer localization community local project module integration'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('l10n_project_batch_parse_form'),
    'file' => 'l10n_project.sync.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
  );
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function l10n_project_perm() {
  return array('administer localization community local project module integration');
}

/**
 * Implementation of hook_cron().
 */
function l10n_project_cron() {
  if (variable_get('l10n_project_cron', 0)) {
    module_load_include('inc', 'l10n_project', 'l10n_project.sync');
    l10n_project_sync();

    // Pick the oldest releases we did not parse yet.
    $result = db_query_range("
      SELECT r.*
      FROM {l10n_community_project} p INNER JOIN
           {l10n_community_release} r ON p.pid = r.pid
      WHERE p.connector_module = 'l10n_project' AND p.status = 1 AND r.download_link != '' AND r.last_parsed = 0 ORDER BY r.file_date ASC",
      0, variable_get('l10n_project_limit', 1)
    );
    $return = array();
    while ($release = db_fetch_object($result)) {
      l10n_project_pick_and_parse($release);
    }
  }
}

// = Settings ==================================================================

/**
 * Settings form callback.
 */
function l10n_project_settings_form() {
  $form['l10n_project_cron'] = array(
    '#title' => t('Scan new projects on every cron run'),
    '#type' => 'checkbox',
    '#default_value' => variable_get('l10n_project_cron', 0),
    '#description' => t('It is advised to set up a regular cron run to parse new package files.'),
  );
  $form['l10n_project_limit'] = array(
    '#title' => t('Number of releases to scan at once'),
    '#description' => t('The number of releases to scan on a cron run. Scanning is synchronous, so you need to wait for Drupal to extract and parse the tarball content.'),
    '#type' => 'select',
    '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 20)),
    '#default_value' => variable_get('l10n_project_limit', 1),
  );
  return system_settings_form($form);
}
