You are here

cacheflush_cron.module in CacheFlush 7

Same filename and directory in other branches
  1. 7.2 cacheflush_cron/cacheflush_cron.module

Cacheflush cron job.

File

cacheflush_cron/cacheflush_cron.module
View source
<?php

/**
 * @file
 * Cacheflush cron job.
 */

/**
 * Implements hook_form_alter().
 */
function cacheflush_cron_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id != 'cacheflush_preset_form') {
    return;
  }

  //Cron fieldset
  $form['cron_fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => t('Cron'),
    '#weight' => 2,
  );

  //Set cron checkbox
  $form['cron_fieldset']['cacheflush_cron_sett'] = array(
    '#type' => "checkbox",
    '#title' => t('Cron job'),
    '#weight' => 1,
    '#default_value' => isset($form_state['cacheflush_preset_list'][$form_state['cacheflush_preset_id']]['#cron']['checkbox']) ? $form_state['cacheflush_preset_list'][$form_state['cacheflush_preset_id']]['#cron']['checkbox'] : 0,
    '#description' => t('Eneble cron job for this preset.'),
  );
  if ($form_state['cacheflush_preset_is_edit'] == 'TRUE') {

    //Cron Settings Page link container
    $form['cron_fieldset']['cacheflush_cron_link'] = array(
      '#type' => 'container',
      '#weight' => 3,
      '#states' => array(
        'invisible' => array(
          ':input[name="cacheflush_cron_sett"]' => array(
            'checked' => FALSE,
          ),
        ),
      ),
    );

    //Cron Settings Page link
    $form['cron_fieldset']['cacheflush_cron_link']['markup'] = array(
      '#markup' => l('Cron Settings', 'admin/config/system/cron'),
    );
  }
  else {

    //Cron rule
    $form['cron_fieldset']['cacheflush_cron_rule'] = array(
      '#type' => "textfield",
      '#title' => t('Cron job rule'),
      '#weight' => 2,
      '#default_value' => isset($form_state['cacheflush_preset_list'][$form_state['cacheflush_preset_id']]['#cron']) ? $form_state['cacheflush_preset_list'][$form_state['cacheflush_preset_id']]['#cron']['rule'] : '* * * * *',
      '#description' => t('Cron job rule example ("*/5 * * * *") // Every 5 minutes.'),
      '#states' => array(
        'invisible' => array(
          ':input[name="cacheflush_cron_sett"]' => array(
            'checked' => FALSE,
          ),
        ),
      ),
    );
  }
  if ($form_id == 'cacheflush_preset_form') {
    $form['#validate'][] = 'cacheflush_cron_validate';
  }
}

/**
 * Implements hook_cronapi().
 */
function cacheflush_cron_cronapi($op, $job = NULL) {
  $cache_presets = variable_get('cacheflush_preset_list', array());
  $items = array();
  foreach ($cache_presets as $key => $preset) {
    if (isset($preset['#cron']) && $preset['#cron']['checkbox'] == '1') {
      $items['cacheflush_prest_' . $key] = array(
        'description' => t('Cron for cacheflush preset ') . $preset['#name'],
        'rule' => $preset['#cron']['rule'],
        'callback' => '_cacheflush_clear_preset',
        'arguments' => array(
          $key,
        ),
      );
    }
  }
  return $items;
}

/**
 * Implements hook_validate().
 */
function cacheflush_cron_validate(&$form, &$form_state) {

  //Valide Crone Rule text field
  if ($form_state['values']['cacheflush_cron_sett'] == '1') {
    if ($form_state['cacheflush_preset_is_edit'] == 'TRUE') {
      $form_state['cacheflush_preset_list'][$form_state['cacheflush_preset_id']]['#cron']['checkbox'] = $form_state['values']['cacheflush_cron_sett'];
    }
    else {
      if (!preg_match('/^\\s*([0-9*, \\/-]+[]+[0-9*, \\/-]+[]+[0-9*, \\/-]+[]+[0-9*, \\/-]+[]+[0-9*, \\/-]+)\\s*$/', $form_state['values']['cacheflush_cron_rule'])) {
        form_error($form['cron_fieldset']['cacheflush_cron_rule'], t('Invalid rule: ' . $form_state['values']['cacheflush_cron_rule']));
      }
      else {
        $form_state['cacheflush_preset_list'][$form_state['cacheflush_preset_id']]['#cron'] = array(
          'checkbox' => 1,
          'rule' => $form_state['values']['cacheflush_cron_rule'],
        );
      }
    }
  }
  else {
    if (isset($form_state['cacheflush_preset_list'][$form_state['cacheflush_preset_id']]['#cron'])) {
      $form_state['cacheflush_preset_list'][$form_state['cacheflush_preset_id']]['#cron']['checkbox'] = 0;
    }
  }
}