<?php

/**
 * @file
 *   Install hooks for cron_control.
 *
 * @see cron_control.module
 *
 * @author
 *   Markus Kalkbrenner | bio.logis GmbH
 */

/**
 * Implements hook_enable().
 */
function cron_control_enable() {
  // inform the user that he has to configure cron control
  $link = l(st('cron control configuration'), 'admin/config/system/cron/cron_control');
  drupal_set_message(st('Right after the installation of <em>Cron Control</em> all cron jobs are disabled! See the !link page for more information.',
    array('!link' => filter_xss($link))), 'warning');
}

/**
 * Implements hook_schema().
 */
function cron_control_schema() {
  $schema = array();

  $schema['cron_control_jobs'] = array(
    'description' => 'Stores which cron jobs are enabled on which server.',
    'fields' => array(
      'module' => array(
        'description' => 'Module name.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'server_addr' => array(
        'description' => "Server's IP address.",
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
      ),
      'active' => array(
        'description' => 'Boolean to indicate if job is active on a server',
        'type' => 'int',
        'length' => 1,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
    ),
    'primary key' => array('module', 'server_addr'),
  );

  return $schema;
}

/**
 * Implements hook_requirements().
 *
 * @return
 * A keyed array of requirements. Each requirement is, itself, an array
 * with the following items:
 *  - 'title': the name of the requirement.
 *  - 'value': the current value (e.g. version, time, level, ...).
 *  - 'description': description of the requirement/status.
 *  - 'severity': the requirement's result/severity level, one of:
 *         o REQUIREMENT_INFO: For info only.
 *         o REQUIREMENT_OK: The requirement is satisfied.
 *         o REQUIREMENT_WARNING: The requirement failed with a warning.
 *         o REQUIREMENT_ERROR: The requirement failed with an error.
 */
function cron_control_requirements($phase) {
  $requirements = array();

  if ($phase == 'runtime') {

    $requirements['cron_control_status'] = array(
      'title' => t('Cron Control Status'),
      'value' => t('All cron jobs are enabled.'),
      'severity' => REQUIREMENT_OK,
      'description' => '',
    );

    if (($result1 = db_query('SELECT COUNT(DISTINCT(module)) FROM {cron_control_jobs} WHERE active = :active', array(':active' => 1))) &&
        ($result2 = db_query('SELECT COUNT(DISTINCT(module)) FROM {cron_control_jobs}'))) {

      $m1 = $result1->fetchField();
      $m2 = $result2->fetchField();

      if ($m1 < $m2) {
        if (0 == $m1) {
          $requirements['cron_control_status']['value'] = t('All cron jobs are disabled.');
          $requirements['cron_control_status']['severity'] = REQUIREMENT_ERROR;
        }
        else {
          $requirements['cron_control_status']['value'] = t('At least one cron job is disabled on all servers.');
          $requirements['cron_control_status']['severity'] = REQUIREMENT_WARNING;
        }
        $requirements['cron_control_status']['description'] = t('See the !link page for more information.', array('!link' => l('cron control configuration', 'admin/config/system/cron/cron_control')));
      }
    }
    else {
      $requirements['cron_control_status']['value'] = t('Unknown');
      $requirements['cron_control_status']['severity'] = REQUIREMENT_WARNING;
    }
  }

  return $requirements;
}
