Project

General

Profile

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Job Scheduler Trigger admin pages.
6
 */
7

    
8
/**
9
 * Admin Overview.
10
 */
11
function job_scheduler_trigger_admin_overview() {
12
  $base_path = 'admin/config/system/job_scheduler';
13
  $destination = drupal_get_destination();
14
  $header = array(
15
    'title' => t('Title'),
16
    'crontab' => t('Cron tab'),
17
    'status' => t('Status'),
18
    'last' => t('Last'),
19
    'next' => t('Next'),
20
    'operations' => t('Operations'),
21
  );
22

    
23
  $options = array();
24
  foreach (job_scheduler_trigger_list() as $trigger) {
25
    $next_time = job_scheduler_trigger_next_time($trigger->crontab);
26
    $options[$trigger->trid] = array(
27
      'title' => check_plain($trigger->title),
28
      'crontab' => check_plain($trigger->crontab),
29
      'status' => $trigger->status ? t('Enabled') : t('Disabled'),
30
      'last' => $trigger->last ? format_date($trigger->last) : t('Never'),
31
      'next' => $next_time ? format_date($next_time) : t('Never'),
32
    );
33
    $operations['edit'] = array(
34
      'title' => t('edit'),
35
      'href' => $base_path . '/' . $trigger->trid,
36
      'query' => $destination,
37
    );
38
    $options[$trigger->trid]['operations'] = array(
39
      'data' => array(
40
        '#theme' => 'links',
41
        '#links' => $operations,
42
        '#attributes' => array('class' => array('links', 'inline')),
43
      ),
44
    );
45
  }
46

    
47
  $form['triggers'] = array(
48
    '#theme' => 'table',
49
    '#header' => $header,
50
    '#rows' => $options,
51
    '#empty' => t('No triggers created yet.'),
52
  );
53

    
54
  $form['add']['#markup'] = l(t('Add a new trigger'), $base_path . '/add', array('query' => $destination));
55

    
56
  return $form;
57
}
58

    
59
/**
60
 * Edit form for a trigger.
61
 */
62
function job_scheduler_trigger_edit_form($form, &$form_state, $trigger) {
63
  if (!$trigger) {
64
    $trigger = (object) array(
65
      'trid' => NULL,
66
      'title' => t('New trigger'),
67
      'crontab' => '* * * * *',
68
      'status' => 0,
69
      'last' => 0,
70
    );
71
  }
72
  $form['trigger'] = array('#tree' => TRUE);
73
  $form['trigger']['trid'] = array('#type' => 'value', '#value' => $trigger->trid);
74
  $form['trigger']['title'] = array(
75
    '#title' => t('Title'),
76
    '#type' => 'textfield',
77
    '#default_value' => $trigger->title,
78
    '#required' => TRUE,
79
    '#description' => t('This is the name of the trigger that will be created when enabled.'),
80
  );
81
  $form['trigger']['crontab'] = array(
82
    '#type' => 'textfield',
83
    '#title' => t('Cron tab'),
84
    '#default_value' => $trigger->crontab,
85
    '#required' => TRUE,
86
    '#description' => t('Unix-style crontab line with: <i>minutes</i> <i>hours</i> <i>day-of-month</i> <i>month</i> <i>day-of-week</i>. Read <a href=@man-crontab>more about crontab</a>.', array('@man-crontab' => url('http://linux.die.net/man/5/crontab'))),
87
  );
88
  $form['trigger']['status'] = array(
89
    '#title' => t('Status'),
90
    '#type' => 'radios',
91
    '#options' => array(t('disabled'), t('enabled')),
92
    '#default_value' => $trigger->status,
93
    '#description' => t('When enabled, a trigger will be created with this crontab and jobs will be scheduled for it. Check everything is OK before enabling the trigger.'),
94
  );
95
  $form['trigger']['last'] = array(
96
    '#type' => 'item',
97
    '#title' => t('Last triggered'),
98
    '#markup' => format_date($trigger->last),
99
  );
100
  if ($trigger->trid && $trigger->crontab) {
101
    $next = job_scheduler_trigger_next_time($trigger->crontab);
102
    $form['trigger']['next'] = array(
103
      '#type' => 'item',
104
      '#title' => t('Next trigger'),
105
      '#markup' => $next ? format_date($next) : t('Never in the next year.'),
106
    );
107
  }
108
  $form['buttons']['update'] = array('#type' => 'submit', '#value' => t('Save'));
109

    
110
  if ($trigger->trid) {
111
    $form['buttons']['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
112
  }
113

    
114
  return $form;
115
}
116

    
117
/**
118
 * Validate trigger.
119
 */
120
function job_scheduler_trigger_edit_form_validate($form, &$form_state) {
121
  $trigger = $form_state['values']['trigger'];
122
  if ($trigger['crontab']) {
123
    if (!JobSchedulerCronTab::parse($trigger['crontab'])) {
124
      form_set_error('trigger][crontab', t('Invalid cron parameters.'));
125
    }
126
  }
127
}
128

    
129
/**
130
 * Submit trigger.
131
 */
132
function job_scheduler_trigger_edit_form_submit($form, &$form_state) {
133
  $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
134
  $trigger = $form_state['values']['trigger'];
135
  if ($op == t('Save')) {
136
    if (empty($trigger['trid'])) {
137
      // Create and then assign trigger name and create trigger.
138
      drupal_write_record('job_scheduler_trigger', $trigger);
139
      $trigger['hook'] = 'job_scheduler_' . $trigger['trid'];
140
      drupal_write_record('job_scheduler_trigger', $trigger, 'trid');
141
      drupal_set_message(t('A new trigger has been created.'));
142
    }
143
    else {
144
      drupal_write_record('job_scheduler_trigger', $trigger, 'trid');
145
      drupal_set_message(t('The trigger has been updated.'));
146
    }
147
  }
148
  elseif ($op == t('Delete')) {
149
    db_delete('job_scheduler_trigger')
150
      ->condition('trid', $trigger['trid'])
151
      ->execute();
152
    drupal_set_message(t('The trigger has been deleted.'));
153
    // @todo Delete scheduled jobs
154
  }
155
  // Synchronize triggers, actions, scheduled jobs, etc..
156
  actions_synchronize();
157
  job_scheduler_rebuild_all();
158
}
159

    
160
/**
161
 * Quick calculate next time cron.
162
 */
163
function job_scheduler_trigger_next_time($crontab) {
164
  $crontab = new JobSchedulerCronTab($crontab);
165
  return $crontab->cron ? $crontab->nextTime(REQUEST_TIME) : 0;
166
}