Project

General

Profile

Paste
Download (4.57 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / job_scheduler / modules / job_scheduler_trigger / job_scheduler_trigger.module @ 082b75eb

1
<?php
2

    
3
/**
4
 * @file
5
 * Job scheduler module.
6
 */
7

    
8
/**
9
 * Implements hook_help().
10
 */
11
function job_scheduler_trigger_help($path, $arg) {
12
  switch ($path) {
13
    case 'admin/structure/trigger/job_scheduler':
14
      // The first line is the very same text trigger module provides. Do not
15
      // edit.
16
      $output = '<p>' . t('Triggers are events on your site, such as new content being added or a user logging in. The Trigger module associates these triggers with actions (functional tasks), such as unpublishing content containing certain keywords or e-mailing an administrator. The <a href="@url">Actions settings page</a> contains a list of existing actions and provides the ability to create and configure advanced actions (actions requiring configuration, such as an e-mail address or a list of banned words).', array('@url' => url('admin/config/system/actions'))) . '</p>';
17
      $output .= '<p>' . t('Below you can assign actions to run on a periodic basis. To create more triggers of this type use the <a href="@url">Job Scheduler configuration</a> page.', array('@url' => url('admin/config/system/job_scheduler'))) . '</p>';
18
      return $output;
19

    
20
    case 'admin/config/system/job_scheduler':
21
      $output = '<p>' . t('You can create any number of jobs that are triggered depending on a crontab.') . '</p>';
22
      $output .= '<p>' . t('To set actions for these triggers, go to the <a href="@url">Job Scheduler Triggers</a> page.', array('@url' => url('admin/structure/trigger/job_scheduler'))) . '</p>';
23
      return $output;
24
  }
25
}
26

    
27
/**
28
 * Implements hook_menu().
29
 */
30
function job_scheduler_trigger_menu() {
31
  $items['admin/config/system/job_scheduler'] = array(
32
    'title' => 'Trigger scheduler',
33
    'description' => 'Configure timely triggers',
34
    'page callback' => 'job_scheduler_trigger_admin_overview',
35
    'access arguments' => array('administer site configuration'),
36
    'file' => 'job_scheduler_trigger.admin.inc',
37
  );
38
  $items['admin/config/system/job_scheduler/add'] = array(
39
    'title' => 'Add a new trigger',
40
    'description' => 'Configure timely triggers',
41
    'page callback' => 'drupal_get_form',
42
    'page arguments' => array('job_scheduler_trigger_edit_form', NULL),
43
    'access arguments' => array('administer site configuration'),
44
    'file' => 'job_scheduler_trigger.admin.inc',
45
  );
46
  $items['admin/config/system/job_scheduler/%job_scheduler_trigger'] = array(
47
    'title' => 'Trigger scheduler',
48
    'description' => 'Configure timely triggers',
49
    'page callback' => 'drupal_get_form',
50
    'page arguments' => array('job_scheduler_trigger_edit_form', 4),
51
    'access arguments' => array('administer site configuration'),
52
    'file' => 'job_scheduler_trigger.admin.inc',
53
  );
54
  return $items;
55
}
56

    
57
/**
58
 * Implements hook_trigger_info().
59
 */
60
function job_scheduler_trigger_trigger_info() {
61
  $triggers = array();
62
  foreach (job_scheduler_trigger_list() as $trigger) {
63
    $label = $trigger->status ? $trigger->title : $trigger->title . ' ' . t('(Disabled)');
64
    $triggers['job_scheduler'][$trigger->hook] = array(
65
      'label' => $label,
66
    );
67
  }
68
  return $triggers;
69
}
70

    
71
/**
72
 * Implements hook_job_scheduler_info().
73
 */
74
function job_scheduler_trigger_cron_job_scheduler_info() {
75
  foreach (job_scheduler_trigger_list() as $trigger) {
76
    if ($trigger->status) {
77
      $jobs[$trigger->trid] = array(
78
        'id' => $trigger->trid,
79
        'type' => $trigger->hook,
80
        'crontab' => $trigger->crontab,
81
        'periodic' => TRUE,
82
      );
83
    }
84
  }
85
  if (!empty($jobs)) {
86
    $scheduler['job_scheduler_trigger'] = array(
87
      'worker callback' => 'job_scheduler_trigger_worker',
88
      'jobs' => $jobs,
89
    );
90
    return $scheduler;
91
  }
92
}
93

    
94
/**
95
 * Get job list for job scheduler.
96
 */
97
function job_scheduler_trigger_list() {
98
  return db_select('job_scheduler_trigger', 't')
99
    ->fields('t')
100
    ->orderBy('status', 'DESC')
101
    ->execute()->fetchAll();
102
}
103

    
104
/**
105
 * Fire up a scheduled trigger.
106
 */
107
function job_scheduler_trigger_worker($job) {
108
  // Mark last time triggered for the records.
109
  db_update('job_scheduler_trigger')
110
    ->fields(array('last' => REQUEST_TIME))
111
    ->condition('trid', $job['id'])
112
    ->execute();
113
  if ($aids = trigger_get_assigned_actions($job['type'])) {
114
    $context = array(
115
      'group' => 'scheduler',
116
      'hook' => $job['type'],
117
      'job' => $job,
118
    );
119
    // Scheduler's object is the job from job_scheduler.
120
    $object = (object) $job;
121
    actions_do(array_keys($aids), $object, $context);
122
  }
123
}
124

    
125
/**
126
 * Menu loader.
127
 */
128
function job_scheduler_trigger_load($trid) {
129
  return db_select('job_scheduler_trigger', 't')
130
    ->fields('t')
131
    ->condition('trid', $trid)
132
    ->execute()->fetchObject();
133
}