From 5f733698550d7e250d548ef5725d16f9d2ec8d2e Mon Sep 17 00:00:00 2001
From: NROTC_Webmaster <NROTC_Webmaster@1030166.no-reply.drupal.org>
Date: Sat, 28 Jan 2012 20:49:03 -0500
Subject: [PATCH] cronplus-add-on-demand-363809-8

Signed-off-by: NROTC_Webmaster <NROTC_Webmaster@1030166.no-reply.drupal.org>
---
 cronplus.module |  100 +++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 68 insertions(+), 32 deletions(-)

diff --git a/cronplus.module b/cronplus.module
index 007507e..694f0b4 100644
--- a/cronplus.module
+++ b/cronplus.module
@@ -2,18 +2,20 @@
 /**
  *
  * CronPlus enhances the Drupal cron feature to provide specific calls to
- * other modules at hourly, daily, weekly, monthly, and yearly intervals.
+ * other modules at on demand, hourly, daily, weekly, monthly, and yearly intervals.
  *
  * Author: Syscrusher (Scott Courtney)    License: GPL
  */
 
+$stop_cron = 0;
+
 /**
  * Implements hook_help().
  */
 function cronplus_help($section) {
   switch ($section) {
     case 'admin/modules#description':
-      return t('Calls module-specific hourly, daily, weekly, monthly, and yearly functions.');
+      return t('Calls module-specific on demand, hourly, daily, weekly, monthly, and yearly functions.');
       break;
   }
 }
@@ -24,7 +26,7 @@ function cronplus_help($section) {
 function cronplus_menu() {
   $items = array();
   $items['admin/settings/cronplus'] = array(
-    'title' => t('cronplus'),
+    'title' => 'Cronplus',
     'page callback' => 'cronplus_settings_main',
     'description' => 'Select which hour and on what day you would like houly and daily crons to run. Also see the status of cronplus',
     'access callback' => 'user_access',
@@ -32,18 +34,28 @@ function cronplus_menu() {
 //    'type' => MENU_LOCAL_TASK,
   );
   $items['admin/settings/cronplus/main'] = array(
-    'title' => t('configuration'),
+    'title' => 'Configuration',
     'type' => MENU_DEFAULT_LOCAL_TASK,
     'access callback' => 'user_access',
     'access arguments' => array('administer site configuration'),
     'weight' => -10,
   );
   $items['admin/settings/cronplus/status'] = array(
-    'title' => t('status report'),
-    'page callback' => 'cronplus_settings_status', 'access' => user_access('administer site configuration'),
+    'title' => 'Status report',
+    'page callback' => 'cronplus_settings_status',
+    'access' => user_access('administer site configuration'),
     'access callback' => 'user_access',
     'access arguments' => array('administer site configuration'),
-    'type' => MENU_LOCAL_TASK, 'weight' => 10
+    'type' => MENU_LOCAL_TASK,
+    'weight' => 10
+  );
+  $items['admin/settings/cronplus/run'] = array(
+    'title' => 'Run now',
+    'page callback' => 'cronplus_run_on_demand',
+    'access callback' => 'user_access',
+    'access arguments' => array('administer site configuration'),
+    'type' => MENU_LOCAL_TASK,
+    'weight' => 20
   );
   return $items;
 }
@@ -71,16 +83,15 @@ function cronplus_settings_form_validate($form_id, $form_values) {
 function cronplus_settings_form_submit($form_id, $form_values) {
   $cronplus_preferred_hour = intval($form_values['cronplus_preferred_hour']);
   $cronplus_preferred_wkdy = intval($form_values['cronplus_preferred_wkdy']);
-  $cronplus_test_mode = intval($form_values['cronplus_test_mode']);
   variable_set('cronplus_preferred_hour', $cronplus_preferred_hour);
   variable_set('cronplus_preferred_wkdy', $cronplus_preferred_wkdy);
-  variable_set('cronplus_test_mode', $cronplus_test_mode);
+  drupal_set_message(t('Cronplus settings saved'), 'info');
 }
 
 /**
  * Handle the module's settings.
  */
-function cronplus_settings_main() {  
+function cronplus_settings_main() {
   return drupal_get_form('cronplus_settings_form');
 }
 
@@ -100,12 +111,6 @@ function cronplus_settings_form() {
     '#default_value' => intval(variable_get('cronplus_preferred_wkdy', 0)),
     '#options' => _cronplus_int_options(0, 6, array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'), TRUE),
   );
-  $form['cronplus_test_mode'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Enable test mode'),
-    '#description' => t('This will cause all cronplus hooks to fire on every cron run, for testing purposes. Turn this off for normal operation.'),
-    '#default_value' => intval(variable_get('cronplus_test_mode', 0)),
-  );
   $form['cronplus_submit'] = array(
     '#type' => 'submit',
     '#value' => t('Save settings'),
@@ -113,6 +118,28 @@ function cronplus_settings_form() {
   return system_settings_form($form);
 }
 
+function cronplus_run_on_demand() {
+  return drupal_get_form('cronplus_run_on_demand_form');
+}
+
+function cronplus_run_on_demand_form() {
+  $form_now = array();
+  $form_now['run_now_submit'] = array(
+    '#type' => 'submit',
+    '#title' => t('Run now'),
+    '#description' => t('This will force a cron run right now.'),
+    '#value' => t('Run now'),
+  );
+  return $form_now;
+}
+
+function cronplus_run_on_demand_form_submit() {
+  global $stop_cron;
+  $stop_cron = 1;
+  drupal_set_message(t('Cronplus ran successfully.'), 'info');
+  return drupal_cron_run();
+}
+
 /**
  * Returns an array of the time periods supported by the module. This can
  * be used by other modules to offer administrators or users options of
@@ -124,7 +151,7 @@ function cronplus_settings_form() {
  */
 function cronplus_periods() {
   $periods = array();
-  foreach (array('hourly', 'daily', 'weekly', 'monthly', 'yearly') as $period) {
+  foreach (array('on_demand', 'hourly', 'daily', 'weekly', 'monthly', 'yearly') as $period) {
     $periods[$period] = t($period);
   }
   return $periods;
@@ -135,10 +162,10 @@ function cronplus_periods() {
  */
 function cronplus_logview_form() {
   $form = array(
-     'submit' => array(
+    'submit' => array(
        '#type' => 'submit',
        '#value' => t('View cronplus log'),
-     ),
+    ),
   );
   return $form;
 }
@@ -165,7 +192,7 @@ function cronplus_settings_status() {
     $html .= '<h2>'. $period_t .'</h2>';
     $html .= '<div class="cronplus_status">';
     $hook = ($period == 'cronplus') ? 'cronplus' : 'cronplus_'. $period;
-    $group = '<p>'. t('Hook: {module}_%hook()', array('%hook' => $hook)) .'<p>'. t('Implemented by: ');
+    $group = '<p>'. t('Hook: {module}_%hook()', array('%hook' => $hook)) .'<p>'. t('Implemented by:');
     $modules = module_implements($hook);
     if (count($modules) > 0) {
       $group .= '<span class="cronplus_modules">'. implode(', ', $modules) .'</span>';
@@ -173,7 +200,7 @@ function cronplus_settings_status() {
       else {
       $group .= t('None');
     }
-    $group .= '<p>'. t('Last invoked: ');
+    $group .= '<p>'. t('Last invoked:');
     if ($period == 'cronplus') {
       $group .= t('Invoked along with each of the above');
     }
@@ -193,8 +220,8 @@ function cronplus_settings_status() {
  * time have changed since the last run.
  */
 function cronplus_cron() {
+  global $stop_cron;
   // Gather information...
-  $test_mode = intval(variable_get('cronplus_test_mode', 0));
   $last_cron = intval(variable_get('cronplus_last_cron', 0));
   $now = time();
   $now_wkdy  = intval(gmdate('w', $now)); // 0=Sunday
@@ -206,20 +233,23 @@ function cronplus_cron() {
   $now_year  = intval(gmdate('Y', $now));
   $now_date  = gmdate('Y-m-d', $now);
   $now_hour  = intval(gmdate('H', $now));
-  
+  $now_second  = intval(gmdate('s', $now));
+
   // Gather the last time each cronplus hook was invoked
+  $cronplus_on_demand_last  = intval(variable_get('cronplus_on_demand_last', 0));
   $cronplus_hourly_last  = intval(variable_get('cronplus_hourly_last', 0));
   $cronplus_daily_last   = intval(variable_get('cronplus_daily_last', 0));
   $cronplus_weekly_last  = intval(variable_get('cronplus_weekly_last', 0));
   $cronplus_monthly_last = intval(variable_get('cronplus_monthly_last', 0));
   $cronplus_yearly_last  = intval(variable_get('cronplus_yearly_last', 0));
-  
+
+  $last_cron_second = intval(gmdate('s', $cronplus_on_demand_last));
   $last_cron_hour = intval(gmdate('H', $cronplus_hourly_last));
   $last_cron_day  = gmdate('Y-m-d', $cronplus_daily_last);
   $last_cron_week = intval(gmdate('W', $cronplus_weekly_last));
   $last_cron_month = gmdate('Y-m', $cronplus_monthly_last);
   $last_cron_year = intval(gmdate('Y', $cronplus_yearly_last));
-  
+
   // Preferred hour affects daily, weekly, monthly, and yearly runs
   $prefer_hour = intval(variable_get('cronplus_preferred_hour', 0));
   // Preferred wkdy affects weekly, monthly, and yearly runs
@@ -233,12 +263,18 @@ function cronplus_cron() {
   // real length of the corresponding time interval so that the module will
   // tend to converge onto the preferred timings over the long term.
 
+  // On Demand
+  if ($now_second != $last_cron_second || $interval >= 10 && $stop_cron == 1) {
+    cronplus_invoke_hook('on_demand', $now, $last_cron);
+  }
+  $stop_cron++;
+
   // Hourly
-  if ($test_mode || ($now_hour != $last_cron_hour || $interval >= 4000)) {
+  if ($now_hour != $last_cron_hour || $interval >= 4000) {
     cronplus_invoke_hook('hourly', $now, $last_cron);
   }
   // Daily
-  if ($test_mode || (substr($last_cron_day, 0, 10) != $now_date && ($now_hour >= $prefer_hour || $interval > 96000))) {
+  if (substr($last_cron_day, 0, 10) != $now_date && ($now_hour >= $prefer_hour || $interval > 96000)) {
     // The sleep() calls in the following logic ensure that calls to cronplus_invoke_hook()
     // cannot occur multiple times in the same second, no matter how fast the system is.
     // This is needed because log entries have a precision of only seconds, and we want
@@ -251,17 +287,17 @@ function cronplus_cron() {
     cronplus_invoke_hook('daily', $now, $last_cron);
   }
   // Weekly
-  if ($test_mode || ($now_week != $last_cron_week && (($now_wkdy >= $prefer_wkdy && $now_hour >= $prefer_hour) || $interval > (86400*8)))) {
+  if ($now_week != $last_cron_week && (($now_wkdy >= $prefer_wkdy && $now_hour >= $prefer_hour) || $interval > (86400*8))) {
     sleep(1);
     cronplus_invoke_hook('weekly', $now, $last_cron);
   }
   // Monthly
-  if ($test_mode || ($now_month != $last_cron_month && (($now_wkdy >= $prefer_wkdy && $now_hour >= $prefer_hour) || $interval > 86400*35))) {
+  if ($now_month != $last_cron_month && (($now_wkdy >= $prefer_wkdy && $now_hour >= $prefer_hour) || $interval > 86400*35)) {
     sleep(1);
     cronplus_invoke_hook('monthly', $now, $last_cron);
   }
   // Yearly
-  if ($test_mode || ($now_year > $last_cron_year && (($now_wkdy >= $prefer_wkdy && $now_hour >= $prefer_hour) || $interval > (86400*380)))) {
+  if ($now_year > $last_cron_year && (($now_wkdy >= $prefer_wkdy && $now_hour >= $prefer_hour) || $interval > (86400*380))) {
     sleep(1);
     cronplus_invoke_hook('yearly', $now, $last_cron);
   }
@@ -283,7 +319,7 @@ function cronplus_invoke_hook($period, $now, $last_cron) {
   watchdog('cronplus', 'Processing %period cron for %d UTC, calling {module}_%func() for all modules', array('%d' => $now_fmt, '%period' => t($period), '%func' => $function_name), WATCHDOG_NOTICE);
   @module_invoke_all($function_name, $now, $last_cron, $last_this);
   @module_invoke_all('cronplus', $period, $now, $last_cron, $last_this);
-} 
+}
 
 /**
  * Returns an associative array filled with integers pointing to themselves, or
@@ -301,4 +337,4 @@ function _cronplus_int_options($min, $max, $values=NULL, $translate=FALSE) {
     }
   }
   return $options;
-}
+}
\ No newline at end of file
-- 
1.7.7.GIT

