civicrm_cron.module in CiviCRM Cron 7.2
Same filename and directory in other branches
CiviCRM Cron Module
File
civicrm_cron.moduleView source
<?php
/**
* @file
* CiviCRM Cron Module
*/
/**
* Implementation of hook_menu().
*/
function civicrm_cron_menu() {
$items = array();
$items['admin/config/civicrm/civicrm-cron'] = array(
'title' => 'CiviCRM Cron Configuration',
'description' => 'Settings to call CiviCRM\'s cron from Drupal\'s cron.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'civicrm_cron_admin_settings',
),
'access arguments' => array(
'administer CiviCRM',
),
'type' => MENU_NORMAL_ITEM,
);
$items['civicrm-cron/passthrough'] = array(
'title' => 'CiviCRM Cron Passthrough',
'description' => 'Runs CiviCRM\'s cron within Drupal passing the site key as CiviCRM expects',
'page callback' => 'civicrm_cron_passthrough',
'access arguments' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Builds the civicrm_cron admininstration settings form.
*/
function civicrm_cron_admin_settings($form, &$form_state) {
global $base_url;
$form = array();
if (!civicrm_initialize()) {
drupal_set_message(t('Failed to initialize CiviCRM'), 'error');
return;
}
$key = variable_get('civicrm_cron_sitekey', NULL);
if (empty($form_state['input']['civicrm_cron_sitekey']) && $key != NULL) {
$result = drupal_http_request($base_url . '/civicrm-cron/passthrough?key=' . $key);
//look for the CiviCRM error in response... successful processing returns nothing
if (!empty($result->data)) {
drupal_set_message($result->data, 'error');
}
else {
drupal_set_message(t('CiviCRM Cron Successfully Run'));
}
}
//if it's still NULL at this point, set to site key constant
if ($key == NULL) {
$key = CIVICRM_SITE_KEY;
if (empty($form_state['input']['civicrm_cron_sitekey'])) {
drupal_set_message(t('Save the Configuration to Test CiviCRM Cron'), 'warning');
}
}
$form['civicrm_cron_sitekey'] = array(
'#type' => 'textfield',
'#title' => t('Sitekey'),
'#default_value' => $key,
'#description' => t('Must match the sitekey found in the civicrm-settings.php file. Leave this field empty to attempt to lookup the current site key.'),
);
$form['civicrm_cron_advanced'] = array(
'#type' => 'fieldset',
'#title' => t('CiviMail Settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
// CiviMail job is the only job appears to be the only job that requires authenitcation
// added username and pass back to resolve https://drupal.org/node/2088595
$cron_values['name'] = variable_get('civicrm_cron_username', NULL);
$cron_values['pass'] = variable_get('civicrm_cron_password', NULL);
if ($cron_values['name']) {
$account = user_load_by_name($cron_values['name']);
if ($account && user_access('view all contacts', $account) && user_access('access CiviCRM', $account) && user_access('access CiviMail', $account)) {
drupal_set_message(t('User has correct permissions to run CiviMail job'));
}
else {
drupal_set_message(t('User does NOT have correct permissions to run CiviMail job'), 'error');
}
}
$form['civicrm_cron_advanced']['help'] = array(
'#type' => 'markup',
'#markup' => 'A username and password of a Drupal user with the permission to view all contacts,
access CiviCRM, and access CiviMail is required for CiviMail. Passing the username and password
is less secure. ONLY configure this if you are using CiviMail.',
);
$form['civicrm_cron_advanced']['civicrm_cron_username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => $cron_values['name'],
'#description' => t('CiviCRM runs cron as a specific user. This user should have MINIMAL permissions since the password will be saved in the database and seen in the logs.'),
);
$form['civicrm_cron_advanced']['civicrm_cron_password'] = array(
'#type' => 'password',
'#title' => t('Password'),
'#default_value' => $cron_values['pass'],
'#description' => t('The password for user defined above. This will appear blank after it is saved.'),
);
return system_settings_form($form);
}
/**
* Implementation of hook_cron().
*/
function civicrm_cron_cron() {
global $base_url;
$key = variable_get('civicrm_cron_sitekey', NULL);
// we use drupal_http_request because any errors from
// $facility->execute() are printed to screen
$result = drupal_http_request($base_url . 'civicrm-cron/passthrough?key=' . $key);
//look for the CiviCRM error in response... successful processing returns nothing
if (!empty($result->data)) {
watchdog('civirm_cron', $result->data, NULL, WATCHDOG_ERROR);
}
else {
watchdog('civirm_cron', t('CiviCRM Cron: Successfully Run'), NULL, WATCHDOG_NOTICE);
}
}
function civicrm_cron_passthrough() {
if (!civicrm_initialize()) {
//watchdog('civirm_cron', t('Failed to initialize CiviCRM during Cron'), NULL, WATCHDOG_ERROR);
return t('Failed to initialize CiviCRM during Cron');
}
if (variable_get('civicrm_cron_username', NULL)) {
CRM_Utils_System::authenticateScript(TRUE, variable_get('civicrm_cron_username', NULL), variable_get('civicrm_cron_password', NULL));
}
else {
CRM_Utils_System::authenticateScript(FALSE);
}
require_once 'CRM/Core/JobManager.php';
$facility = new CRM_Core_JobManager();
return $facility
->execute();
}
Functions
Name | Description |
---|---|
civicrm_cron_admin_settings | Builds the civicrm_cron admininstration settings form. |
civicrm_cron_cron | Implementation of hook_cron(). |
civicrm_cron_menu | Implementation of hook_menu(). |
civicrm_cron_passthrough |