/[drupal]/contributions/modules/schedule/schedule.module
ViewVC logotype

Diff of /contributions/modules/schedule/schedule.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.6, Fri Mar 9 21:58:11 2007 UTC revision 1.7, Sun Aug 30 21:52:30 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: schedule.module,v 1.3 2005/09/25 22:08:28 MegaGrunt Exp $  // $Id: schedule.module,v 1.3 2005/09/25 22:08:28 MegaGrunt Exp $
3    
4   $GLOBALS['installed_modules']['schedule'] = 1.4;  /**
5     * Implementation of hook_help();
6     */
7  function schedule_help($section = "admin/help#schedule") {  function schedule_help($section = "admin/help#schedule") {
   
8    switch ($section) {    switch ($section) {
9      case 'admin/modules#description':      case 'admin/help#schedule' :
       return t('Component: Manages scheduling information.');  
     case 'admin/help#schedule':  
10        return t('Component: Manages scheduling information. Provides functionality to other modules.');        return t('Component: Manages scheduling information. Provides functionality to other modules.');
11    }    }
12  }  }
13    
14    /**
15  // ********** FORMS *****************************   *
16     */
17  function schedule_form_admin(&$form) {  function schedule_form_admin(&$form) {
18      if (!empty($form['schedule_id']['#value'])) {
19        $schedule = schedule_select_schedule($form['schedule_id']['#value']);
20      }
21    
22      if (!empty($form['schedule_id']['#value'])) $schedule = schedule_select_schedule($form['schedule_id']['#value']);    // Required form elements.
23      $form['schedule_id'] = array(
24      /*      '#type' => 'value',
25      // required form elements      '#value' => $schedule['schedule_id'],
26      */    );
27      $form['first'] = array(
28      $form['schedule_id'] = array(      '#type' => 'value',
29        '#type' => 'value',      '#value' => $schedule['first'],
30        '#value' => $schedule['schedule_id'],    );
31      );    $form['last'] = array(
32        '#type' => 'value',
33      $form['first'] = array(      '#value' => $schedule['last'],
34        '#type' => 'value',    );
35        '#value' => $schedule['first'],    $form['frequency'] = array(
36      );      '#type' => 'value',
37        '#value' => 'manual',
38      $form['last'] = array(    );
39        '#type' => 'value',    $form['schedule_title'] = array(
40        '#value' => $schedule['last'],      '#type' => 'textfield',
41      );      '#title' => t('Schedule Title'),
42        '#default_value' => $schedule['schedule_title'],
43      $form['frequency'] = array(      '#size' => 25,
44        '#type' => 'value',      '#maxlength' => 100,
45        '#value' => 'manual',      '#description' => t("Example: 'Hourly', 'Daily', 'Weekly'. More schedules can be added later."),
46      );      '#attributes' => NULL,
47        '#required' => TRUE,
48      $form['schedule_title'] = array(    );
49        '#type' => 'textfield',  
50        '#title' => t('Schedule Title'),    // Frequency.
51        '#default_value' => $schedule['schedule_title'],    $form['period'] = array(
52        '#size' => 25,      '#type' => 'fieldset',
53        '#maxlength' => 100,      '#title' => t('Frequency'),
54        '#description' => t("Example: 'Hourly', 'Daily', 'Weekly'. More schedules can be added later."),      '#description' => t('How often this publication should be sent'),
55        '#attributes' => NULL,    );
56        '#required' => TRUE,    $form['period']['every'] = array(
57      );      '#type' => 'textfield',
58        '#title' => t('Every'),
59        '#default_value' => $schedule['every'],
60      /*      '#size' => 5, '#maxlength' => 3,
61      // frequency    );
62      */    $form['period']['frequency'] = array(
63      $form['period'] = array(      '#type' => 'radios',
64        '#type' => 'fieldset',      '#title' => t('Frequency'),
65        '#title' => t('Frequency'),      '#default_value' => $schedule['frequency'],
66        '#description' => t('How often this publication should be sent'),      '#options' => array(
67      );        'hour' => t('Hour(s)'),
68          'day' => t('Day(s)'),
69      $form['period']['every'] = array(        'month' => t('Month(s)'),
70        '#type' => 'textfield',        'manual' => t('Manual'),
71        '#title' => t('Every'),      ),
72        '#default_value' => $schedule['every'],    );
       '#size' => 5,  
       '#maxlength' => 3,  
     );  
   
     $form['period']['frequency'] = array(  
       '#type' => 'radios',  
       '#title' => t('Frequency'),  
       '#default_value' => $schedule['frequency'],  
       '#options' => array('hour' => t('Hour(s)'), 'day' => t('Day(s)'), 'month' => t('Month(s)'), 'manual' => t('Manual')),  
     );  
73    
74      /*    // Relative controls.
75      // relative controls    $dates[0] = '-------';
     */  
   
     $dates[0] = '-------';  
     // profile date fields array  
     $profile_dates = _schedule_profile_date_fields();  
     if (!empty($profile_dates)) $dates = $dates + $profile_dates;  
   
     // TO DO  
     if (empty($schedule['relative'])) $schedule['relative'] = 'on';  
76    
77      $form['relative'] = array(    // Profile date fields array.
78        '#type' => 'fieldset',    $profile_dates = _schedule_profile_date_fields();
       '#title' => t('Date Relative'),  
       '#description' => t('Make send out relative to selected date.'),  
     );  
   
     $form['relative']['relative_date'] = array(  
       '#type' => 'select',  
       '#title' => t('Date'),  
       '#default_value' => $schedule['relative_date'],  
       '#options' => $dates,  
       '#description' => NULL,  
       '#extra' => 0,  
       '#multiple' => NULL,  
     );  
   
     $form['relative']['relative'] = array(  
       '#type' => 'select',  
       '#title' => t('Send'),  
       '#default_value' => $schedule['relative'],  
       '#options' => array('before' => t('Before'), 'on' => t('On'), 'after' => t('After')),  
       '#description' => t("If 'On' is selected the frequency options above have no effect."),  
       '#extra' => 0,  
       '#multiple' => NULL,  
     );  
79    
80      if (!empty($profile_dates)) {
81      /*      $dates = $dates + $profile_dates;
82      // Next send out    }
     */  
   
     if (!isset($schedule['next']) || $schedule['next'] == 0) {  
         $start_hour = ($schedule['next_hour']) ? $schedule['next_hour'] : '8';  
         $start_date = date ("m/d/y", time());  
         $start_day = date ("l, M j Y", time());  
     } else {  
         $start_hour = ($schedule['next']) ? date ('G', $schedule['next']) : $schedule['next_hour'];  
         $start_date = ($schedule['next']) ? date ('m/d/y', $schedule['next']) : $schedule['next_date'];  
         $start_day = ($schedule['next']) ? date ('l, M j Y', $schedule['next']) : date ('l, M j Y', strtotime($schedule['next_date']));  
     }  
   
     $form['next'] = array(  
       '#type' => 'fieldset',  
       '#title' => t('Next Send'),  
       '#description' => t('Date and time to send next publication.'),  
     );  
   
     $form['next']['next_date'] = array(  
       '#type' => 'textfield',  
       '#title' => t('Send (date)'),  
       '#default_value' => $start_date,  
       '#size' => 10,  
       '#maxlength' => 10,  
       '#description' => t('%start_day (mm/dd/yy)', array('%start_day' => $start_day)),  
     );  
   
     $form['next']['next_hour'] = array(  
       '#type' => 'select',  
       '#title' => t('Send (hour)'),  
       '#default_value' => $start_hour,  
       '#options' => array('1' => '1 a.m.', '2' => '2 a.m.', '3' => '3 a.m.', '4' => '4 a.m.', '5' => '5 a.m.', '6' => '6 a.m.', '7' => '7 a.m.', '8' => '8 a.m.', '9' => '9 a.m.', '10' => '10 a.m.', '11' => '11 a.m.', '12' => '12 a.m.', '13' => '1 p.m.', '14' => '2 p.m.',  '15' => '3 p.m.',  '16' => '4 p.m.',  '17' => '5 p.m.',  '18' => '6 p.m.',  '19' => '7 p.m.',  '20' => '8 p.m.',  '21' => '9 p.m.',  '22' => '10 p.m.',  '23' => '11 p.m.', '0' => '12 p.m.'),  
     );  
   
     /*  
     // Time out  
     */  
   
     $form['timeout'] = array(  
       '#type' => 'fieldset',  
       '#title' => t('Timeout'),  
       '#description' => t("If send out doesn't happen within the timeout period, don't send."),  
     );  
   
     $form['timeout']['timeout'] = array(  
       '#type' => 'textfield',  
       '#title' => t('Hours'),  
       '#default_value' => empty($schedule['timeout']) ? 24 : $schedule['timeout'],  
       '#size' => 5,  
       '#maxlength' => 4,  
       '#required' => TRUE,  
     );  
   
     /*  
     // date to start including from  
     */  
   
     // Schedule has already started - no need to display include field  
     if (!empty($schedule['last']) && !empty($schedule['start'])) {  
         $form['start'] = array(  
           '#type' => 'hidden',  
           '#value' => date('m/d/y', $schedule['start']),  
         );  
         return $form;  
     }  
   
     $include_date = (empty($schedule['start'])) ? '1/1/99' : date ('m/d/y', $schedule['start']);  
   
     $form['include'] = array(  
       '#type' => 'fieldset',  
       '#title' => t('Include'),  
       '#description' => t('On first send out, include nodes stating from this date.'),  
     );  
   
     $form['include']['start'] = array(  
       '#type' => 'textfield',  
       '#title' => t('Include Nodes From (date)'),  
       '#default_value' => $include_date,  
       '#size' => 10,  
       '#maxlength' => 10,  
       '#description' => t('Leave blank to include all. (mm/dd/yy)'),  
     );  
   
 }  
83    
84      // TODO.
85      if (empty($schedule['relative'])) {
86        $schedule['relative'] = 'on';
87      }
88    
89  function schedule_form_send_now($publication) {    $form['relative'] = array(
90        '#type' => 'fieldset',
91      $schedules = schedule_select_schedules($publication['type'], $publication['publication_id']);      '#title' => t('Date Relative'),
92        '#description' => t('Make send out relative to selected date.'),
93      );
94      $form['relative']['relative_date'] = array(
95        '#type' => 'select',
96        '#title' => t('Date'),
97        '#default_value' => $schedule['relative_date'],
98        '#options' => $dates,
99        '#description' => NULL,
100        '#extra' => 0,
101        '#multiple' => NULL,
102      );
103      $form['relative']['relative'] = array(
104        '#type' => 'select',
105        '#title' => t('Send'),
106        '#default_value' => $schedule['relative'],
107        '#options' => array(
108          'before' => t('Before'),
109          'on' => t('On'),
110          'after' => t('After'),
111        ),
112        '#description' => t("If 'On' is selected the frequency options above have no effect."),
113        '#extra' => 0,
114        '#multiple' => NULL,
115      );
116    
117      // Next send out.
118      if (!isset($schedule['next']) || $schedule['next'] == 0) {
119        $start_hour = ($schedule['next_hour']) ? $schedule['next_hour'] : '8';
120        $start_date = date("m/d/y", time());
121        $start_day = date("l, M j Y", time());
122      }
123      else {
124        $start_hour = ($schedule['next']) ? date('G', $schedule['next']) : $schedule['next_hour'];
125        $start_date = ($schedule['next']) ? date('m/d/y', $schedule['next']) : $schedule['next_date'];
126        $start_day = ($schedule['next']) ? date('l, M j Y', $schedule['next']) : date('l, M j Y', strtotime($schedule['next_date']));
127      }
128    
129      foreach ($schedules as $schedule) {    $form['next'] = array(
130        '#type' => 'fieldset',
131          $options[ $schedule['schedule_id'] ] = $schedule['schedule_title'];      '#title' => t('Next Send'),
132      }      '#description' => t('Date and time to send next publication.'),
133      );
134      $form['schedules'] = array(    $form['next']['next_date'] = array(
135        '#type' => 'checkboxes',      '#type' => 'textfield',
136        '#title' => t('Send Now'),      '#title' => t('Send (date)'),
137        '#default_value' => array(),      '#default_value' => $start_date,
138        '#options' => $options,      '#size' => 10,
139        '#description' => t('Select %publication schedules to be sent now (next cron run).', array('%publication' => $publication['title'])),      '#maxlength' => 10,
140        '#attributes' => NULL,      '#description' => t('%start_day (mm/dd/yy)', array('%start_day' => $start_day)),
141        '#required' => TRUE,    );
142      );  
143      $form['next']['next_hour'] = array(
144      $form['publication_id'] = array(      '#type' => 'select',
145        '#type' => 'hidden',      '#title' => t('Send (hour)'),
146        '#value' => $publication['publication_id'],      '#default_value' => $start_hour,
147      );      '#options' => array(
148          '1' => '1 a.m.',
149      $form['title'] = array(        '2' => '2 a.m.',
150          '3' => '3 a.m.',
151          '4' => '4 a.m.',
152          '5' => '5 a.m.',
153          '6' => '6 a.m.',
154          '7' => '7 a.m.',
155          '8' => '8 a.m.',
156          '9' => '9 a.m.',
157          '10' => '10 a.m.',
158          '11' => '11 a.m.',
159          '12' => '12 a.m.',
160          '13' => '1 p.m.',
161          '14' => '2 p.m.',
162          '15' => '3 p.m.',
163          '16' => '4 p.m.',
164          '17' => '5 p.m.',
165          '18' => '6 p.m.',
166          '19' => '7 p.m.',
167          '20' => '8 p.m.',
168          '21' => '9 p.m.',
169          '22' => '10 p.m.',
170          '23' => '11 p.m.',
171          '0' => '12 p.m.',
172        ),
173      );
174    
175      // Time out.
176      $form['timeout'] = array(
177        '#type' => 'fieldset',
178        '#title' => t('Timeout'),
179        '#description' => t("If send out doesn't happen within the timeout period, don't send."),
180      );
181      $form['timeout']['timeout'] = array(
182        '#type' => 'textfield',
183        '#title' => t('Hours'),
184        '#default_value' => empty($schedule['timeout']) ? 24 : $schedule['timeout'],
185        '#size' => 5,
186        '#maxlength' => 4,
187        '#required' => TRUE,
188      );
189    
190      // Date to start including from.
191    
192      // Schedule has already started - no need to display include field.
193      if (!empty($schedule['last']) && !empty($schedule['start'])) {
194        $form['start'] = array(
195        '#type' => 'hidden',        '#type' => 'hidden',
196        '#value' => $publication['title'],        '#value' => date('m/d/y', $schedule['start']),
     );  
   
     $form[] = array(  
       '#type' => 'submit',  
       '#value' => t('Send'),  
197      );      );
   
     $form[] = array(  
       '#type' => 'submit',  
       '#value' => t('Cancel'),  
     );  
   
198      return $form;      return $form;
199  }    }
200    
201      $include_date = (empty($schedule['start'])) ? '1/1/99' : date('m/d/y', $schedule['start']);
202    
203  // **********   DATABASE CALLS   *****************************    $form['include'] = array(
204        '#type' => 'fieldset',
205        '#title' => t('Include'),
206        '#description' => t('On first send out, include nodes stating from this date.'),
207      );
208      $form['include']['start'] = array(
209        '#type' => 'textfield',
210        '#title' => t('Include Nodes From (date)'),
211        '#default_value' => $include_date,
212        '#size' => 10,
213        '#maxlength' => 10,
214        '#description' => t('Leave blank to include all. (mm/dd/yy)'),
215      );
216    }
217    
218    /**
219     *
220     */
221    function schedule_form_send_now($publication) {
222      $schedules = schedule_select_schedules($publication->type, $publication->publication_id);
223    
224      foreach ($schedules as $schedule) {
225        $options[$schedule['schedule_id']] = $schedule['schedule_title'];
226      }
227    
228      $form['schedules'] = array(
229        '#type' => 'checkboxes',
230        '#title' => t('Send Now'),
231        '#default_value' => array(),
232        '#options' => $options,
233        '#description' => t('Select %publication schedules to be sent now (next cron run).', array('%publication' => $publication->title)),
234        '#attributes' => NULL,
235        '#required' => TRUE,
236      );
237      $form['publication_id'] = array(
238        '#type' => 'hidden',
239        '#value' => $publication->publication_id,
240      );
241      $form['title'] = array(
242        '#type' => 'hidden',
243        '#value' => $publication->title,
244      );
245      $form['send'] = array(
246        '#type' => 'submit',
247        '#value' => t('Send'),
248      );
249      $form['cancel'] = array(
250        '#type' => 'submit',
251        '#value' => t('Cancel'),
252      );
253    
254      return $form;
255    }
256    
257    /**
258     *
259     */
260  function schedule_insert_schedule($type, $schedule) {  function schedule_insert_schedule($type, $schedule) {
261      if (isset ($schedule['next_date']) && !is_numeric($schedule['next_date'])) {
262        $schedule = schedule_convert_form_data($schedule);
263      }
264    
265      db_query("INSERT INTO {schedule} (schedule_id, schedule_title, type, publication_id, start, first, next, last, every, frequency, relative, relative_date, timeout) VALUES (NULL, '%s', '%s', %d, %d, %d, %d, %d, %d, '%s', '%s', '%s', %d)", $schedule['schedule_title'], $type, $schedule['publication_id'], $schedule['start'], $schedule['first'], $schedule['next'], $schedule['last'], $schedule['every'], $schedule['frequency'], $schedule['relative'], $schedule['relative_date'], $schedule['timeout']);
266    
   if (isset($schedule['next_date']) && !is_numeric($schedule['next_date'])) $schedule = schedule_convert_form_data($schedule);  
   
   db_query("INSERT INTO {schedule}  
     (schedule_id, schedule_title, type, publication_id, start, first, next, last, every, frequency, relative, relative_date, timeout) VALUES  
     (NULL, '%s', '%s', %d, %d, %d, %d, %d, %d, '%s', '%s', '%s', %d)",  
     $schedule['schedule_title'], $type, $schedule['publication_id'], $schedule['start'], $schedule['first'], $schedule['next'], $schedule['last'], $schedule['every'], $schedule['frequency'], $schedule['relative'], $schedule['relative_date'], $schedule['timeout']);  
   
267    return db_result(db_query("SELECT LAST_INSERT_ID()"));    return db_result(db_query("SELECT LAST_INSERT_ID()"));
268  }  }
269    
270    /**
271     *
272     */
273  function schedule_update_schedule($type, $schedule) {  function schedule_update_schedule($type, $schedule) {
274      if (isset ($schedule['next_date']) && !is_numeric($schedule['next_date'])) {
275        $schedule = schedule_convert_form_data($schedule);
276      }
277    
278    if (isset($schedule['next_date']) && !is_numeric($schedule['next_date'])) $schedule = schedule_convert_form_data($schedule);    db_query("UPDATE {schedule} SET schedule_title = '%s', start = %d, first = %d, next = %d, last = %d, previous = %d, every = %d, frequency = '%s', relative = '%s', relative_date = '%s', timeout = %d WHERE schedule_id = %d AND type = '%s'", $schedule['schedule_title'], $schedule['start'], $schedule['first'], $schedule['next'], $schedule['last'], $schedule['previous'], $schedule['every'], $schedule['frequency'], $schedule['relative'], $schedule['relative_date'], $schedule['timeout'], $schedule['schedule_id'], $type);
   
   db_query("UPDATE {schedule} SET  
     schedule_title = '%s', start = %d, first = %d, next = %d, last = %d, every = %d, frequency = '%s', relative = '%s', relative_date = '%s', timeout = %d WHERE schedule_id = %d AND type = '%s'",  
     $schedule['schedule_title'], $schedule['start'], $schedule['first'], $schedule['next'], $schedule['last'], $schedule['every'], $schedule['frequency'], $schedule['relative'], $schedule['relative_date'], $schedule['timeout'], $schedule['schedule_id'], $type);  
   return;  
279  }  }
280    
281    /**
282     *
283     */
284  function schedule_update_send_now($type, $publication_id, $schedules, $time = NULL) {  function schedule_update_send_now($type, $publication_id, $schedules, $time = NULL) {
285      if (empty($time)) {
286        $time = time();
287      }
288    
289      if (empty($time)) $time = time();    foreach ($schedules as $schedule_id) {
290        $schedule = schedule_select_schedule($schedule_id);
291      foreach ($schedules as $schedule_id) {      $templates = templates_usage_get('schedule', $schedule['schedule_id']);
292        $templates = templates_convert_nid_vid($templates);
293          $schedule = schedule_select_schedule($schedule_id);      $schedule['vids'] = $templates;
294          $schedules_updated[] = $schedule;      $schedule['next'] = $time;
295        schedule_set_next_publication_time($schedule);
296          if ($schedule['frequency'] == 'manual') {      $schedules_updated[] = $schedule;
297      }
298              schedule_set_action($type, $publication_id, $schedule_id, $time);  
299              db_query("UPDATE {schedule} SET last = %d WHERE publication_id = %d AND schedule_id = %d AND type = '%s'", $time, $publication_id, $schedule_id, $type);    return $schedules_updated;
   
         } else {  
   
             db_query("UPDATE {schedule} SET next = %d WHERE publication_id = %d AND schedule_id = %d AND type = '%s'", $time, $publication_id, $schedule_id, $type);  
         }  
     }  
   
     return $schedules_updated;  
300  }  }
301    
302  // Get schedule from database (single)  /**
303     * Get schedule from database (single).
304     */
305  function schedule_select_schedule($schedule_id, $un_cache = FALSE) {  function schedule_select_schedule($schedule_id, $un_cache = FALSE) {
306      static $schedules_static;
307    
308      static $schedules_static;    if ($un_cache) {
309      if ($un_cache) unset($schedules_static[$schedule_id]);      unset($schedules_static[$schedule_id]);
310      }
     // avoid making more than one database call for a schedule  
     if (empty($schedules_static[$schedule_id])) {  
         $schedules_static[$schedule_id] = db_fetch_array(db_query("SELECT * FROM {schedule} WHERE schedule_id = %d LIMIT 1", $schedule_id));  
     }  
311    
312      return $schedules_static[$schedule_id];    // Avoid making more than one database call for a schedule.
313      if (empty($schedules_static[$schedule_id])) {
314        $schedules_static[$schedule_id] = db_fetch_array(db_query("SELECT * FROM {schedule} WHERE schedule_id = %d LIMIT 1", $schedule_id));
315      }
316    
317      return $schedules_static[$schedule_id];
318  }  }
319    
320  // Get schedule from database (all)  /**
321     * Get schedule from database (all).
322     */
323  function schedule_select_schedules($type = NULL, $publication_id = NULL) {  function schedule_select_schedules($type = NULL, $publication_id = NULL) {
324    
325    if (!empty($publication_id)) {    if (!empty($publication_id)) {
326        $results = db_query("SELECT * FROM {schedule} WHERE publication_id = %d AND type = '%s'", $publication_id, $type);      $results = db_query("SELECT * FROM {schedule} WHERE publication_id = %d AND type = '%s'", $publication_id, $type);
327    }    }
328    
329    if (!empty($type) && empty($publication_id)) {    if (!empty($type) && empty($publication_id)) {
330        $results = db_query("SELECT * FROM {schedule} WHERE type = '%s'", $type);      $results = db_query("SELECT * FROM {schedule} WHERE type = '%s'", $type);
331    }    }
332    
333    if (empty($type) && empty($publication_id)) {    if (empty($type) && empty($publication_id)) {
334        $results = db_query("SELECT * FROM {schedule}");      $results = db_query("SELECT * FROM {schedule}");
335    }    }
336    
337    while ($row = db_fetch_array($results)) {    while ($row = db_fetch_array($results)) {
338          list ($row['source_table'], $row['source_id']) = explode ('|', $row['relative_date']);      list($row['source_table'], $row['source_id']) = explode('|', $row['relative_date']);
339          $schedules[ $row['schedule_id'] ] = $row;      $schedules[$row['schedule_id']] = $row;
340    }    }
341    
342    return $schedules;    return $schedules;
343  }  }
344    
345    /**
346     *
347     */
348  function schedule_delete_publication_schedules($type, $publication_id) {  function schedule_delete_publication_schedules($type, $publication_id) {
349      db_query("DELETE FROM {schedule} WHERE publication_id = %d AND type = '%s'", $publication_id, $type);
     db_query("DELETE FROM {schedule} WHERE publication_id = %d AND type = '%s'", $publication_id, $type);  
     return;  
 }  
   
 function schedule_delete($schedule_id) {  
   
     db_query("DELETE FROM {schedule} WHERE schedule_id = %d", $schedule_id);  
     db_query("DELETE FROM {schedule_action} WHERE schedule_id = %d", $schedule_id);  
     return;  
350  }  }
351    
352    /**
353  // **********   MISC.   *****************************   *
354     */
355    function schedule_delete($schedule_id) {
356  // Check it's time to send this publication    db_query("DELETE FROM {schedule} WHERE schedule_id = %d", $schedule_id);
357  // Publication time 0 means schedule is set to manual    db_query("DELETE FROM {schedule_action} WHERE schedule_id = %d", $schedule_id);
 function schedule_activate($next) {  
   
         $activate = ($next != 0 && $next <= time()) ? TRUE : FALSE;  
         return $activate;  
358  }  }
359    
360    /**
361     * Get revision IDs for template.
362     * TODO: might move this to templates?
363     */
364    function schedule_revision_select_db($type, $publication_id, $schedule_id, $pub_time) {
365      $vid = db_result(db_query("SELECT vid FROM {schedule_action} WHERE publication_id = %d AND schedule_id = %d AND pub_time = %d AND type = '%s'", $publication_id, $schedule_id, $pub_time, $type));
366      $vid = unserialize($vid);
367      return $vid;
368    }
369    
370    /**
371     * Check it's time to display this schedule.
372     * Publication time 0 means schedule is set to manual.
373     */
374    function schedule_activate($next) {
375      $activate = ($next != 0 && $next <= time()) ? TRUE : FALSE;
376      return $activate;
377    }
378    
379    /**
380     *
381     */
382  function schedule_validate_title($form_values) {  function schedule_validate_title($form_values) {
383      // Check title has been set.
384      // Check title has been set    if (!$form_values['schedule_title']) {
385      if (!$form_values['schedule_title']) form_set_error('schedule_title', t("Schedule Title: must be set."));      form_set_error('schedule_title', t("Schedule Title: must be set."));
386      return;    }
387  }  }
388    
389    /**
390     *
391     */
392  function schedule_validate_frequency($edit) {  function schedule_validate_frequency($edit) {
393      if ($edit['frequency'] == 'manual') {
     if ($edit['frequency'] == 'manual') return;  
   
     // Check frequency amount has been set correctly  
     if (!isset($edit['every']) || !is_numeric($edit['every']) || $edit['every'] == 0) form_set_error('every', t("Send Schedule: Please set a number for 'Every'"));  
   
394      return;      return;
395      }
396    
397      // Check frequency amount has been set correctly.
398      if (!isset ($edit['every']) || !is_numeric($edit['every']) || $edit['every'] == 0) {
399        form_set_error('every', t("Send Schedule: Please set a number for 'Every'"));
400      }
401  }  }
402    
403    /**
404     *
405     */
406  function schedule_validate_timeout($edit) {  function schedule_validate_timeout($edit) {
407      // Check timeout amount has been set correctly.
408      // Check timeout amount has been set correctly    if (empty($edit['timeout']) || !is_numeric($edit['timeout']) || $edit['timeout'] == 0) {
409      if (empty($edit['timeout']) || !is_numeric($edit['timeout']) || $edit['timeout'] == 0) form_set_error('timeout', t("Timeout: the number of hours must be set"));      form_set_error('timeout', t("Timeout: the number of hours must be set"));
410      }
     return;  
411  }  }
412    
413    /**
414     *
415     */
416  function schedule_validate_relative($edit) {  function schedule_validate_relative($edit) {
417      if (empty($edit['relative_date'])) {
418      if (empty($edit['relative_date'])) form_set_error('relative_date', t('Date: must be set'));      form_set_error('relative_date', t('Date: must be set'));
419      if (empty($edit['relative'])) form_set_error('relative', t('Send: option must be set'));    }
420      return;    if (empty($edit['relative'])) {
421        form_set_error('relative', t('Send: option must be set'));
422      }
423  }  }
424    
425    /**
426     *
427     */
428  function schedule_validate_next_send($edit) {  function schedule_validate_next_send($edit) {
429      if ($edit['frequency'] == 'manual') {
     if ($edit['frequency'] == 'manual') return;  
   
     // check there is a publication date  
     if (!isset($edit['next_date']) || $edit['next_date'] == 0) return form_set_error('next_date', t("Next Send: 'Send (date)' must be set."));  
   
     $next = strtotime ($edit['next_date']);  
     $next_array = getdate($next);  
   
     // check date is valid  
     if (checkdate( $next_array['mon'], $next_array['mday'], $next_array['year'] ) == FALSE) {  
         return form_set_error('next_date', t("The 'Send Start' date is not correct, please use the mm/dd/yy format."));  
     }  
   
     // convert time to unix timestamp  
     $edit['next'] = mktime ($edit['next_hour'], 0, 0, $next_array['mon'], $next_array['mday'], $next_array['year'], -1);  
   
     // check publication time hasn't passed  
     if ($edit['next'] <= time()) form_set_error('next_hour', t("The 'Send Start' date has already passed."));  
   
430      return;      return;
431      }
432    
433      // Check there is a publication date.
434      if (!isset ($edit['next_date']) || $edit['next_date'] == 0) {
435        return form_set_error('next_date', t("Next Send: 'Send (date)' must be set."));
436      }
437    
438      $next = strtotime($edit['next_date']);
439      $next_array = getdate($next);
440    
441      // Check date is valid.
442      if (checkdate($next_array['mon'], $next_array['mday'], $next_array['year']) == FALSE) {
443        return form_set_error('next_date', t("The 'Send Start' date is not correct, please use the mm/dd/yy format."));
444      }
445    
446      // Convert time to unix timestamp.
447      $edit['next'] = mktime($edit['next_hour'], 0, 0, $next_array['mon'], $next_array['mday'], $next_array['year'], -1);
448    
449      // Check publication time hasn't passed.
450      if ($edit['next'] <= time()) {
451        form_set_error('next_hour', t("The 'Send Start' date has already passed."));
452      }
453  }  }
454    
455    /**
456     *
457     */
458  function schedule_validate_included($edit) {  function schedule_validate_included($edit) {
459      // Check this is first publication.
460      if (isset ($edit['last']) && $edit['last'] != 0) {
461        return;
462      }
463    
464      // Check this is first publication    // Default start date.
465      if (isset($edit['last']) && $edit['last'] != 0) return;    if (!isset ($edit['start']) || $edit['start'] == 0) {
   
     // default start date  
     if (!isset($edit['start']) || $edit['start'] == 0) return;  
   
     // convert time to unix timestamp  
     $edit['start'] = strtotime($edit['start']);  
   
     // check date is valid  
     $include_nodes_date = getdate($edit['start']);  
     if (!checkdate( $include_nodes_date['mon'], $include_nodes_date['mday'], $include_nodes_date['year'] )) {  
         form_set_error('start', t("The 'Include Nodes From' date is not correct, please use the mm/dd/yy format."));  
     }  
   
     // check date is in past  
     if ($edit['start'] > time()) {  
         form_set_error('start', t("The 'Include Nodes From' date cannot be in future, has been reset to today's date."));  
     }  
   
466      return;      return;
467      }
468    
469      // Convert time to unix timestamp.
470      $edit['start'] = strtotime($edit['start']);
471    
472      // Check date is valid.
473      $include_nodes_date = getdate($edit['start']);
474      if (!checkdate($include_nodes_date['mon'], $include_nodes_date['mday'], $include_nodes_date['year'])) {
475        form_set_error('start', t("The 'Include Nodes From' date is not correct, please use the mm/dd/yy format."));
476      }
477    
478      // Check date is in past.
479      if ($edit['start'] > time()) {
480        form_set_error('start', t("The 'Include Nodes From' date cannot be in future, has been reset to today's date."));
481      }
482  }  }
483    
484  // check if ready to send on next cron run  /**
485     * Check if ready to send on next cron run.
486     */
487  function schedule_check_send_ready($type, $publication_id) {  function schedule_check_send_ready($type, $publication_id) {
488      $schedule = schedule_select_schedules($type, $publication_id);
489      $schedule = schedule_select_schedules($type, $publication_id);    $time = ($schedule['next'] < time()) ? TRUE : FALSE;
490      $time = ($schedule['next'] < time()) ? TRUE : FALSE;    return $ready;
     return $ready;  
491  }  }
492    
493  // get count of sent  /**
494  // ????   * Get count of sent.
495     */
496  function schedule_count_sent($type, $publication_id, $publication_timestamp, $schedule_id) {  function schedule_count_sent($type, $publication_id, $publication_timestamp, $schedule_id) {
497      // Publication hasn't started sending.
498      // publication hasn't started sending    if ($publication_timestamp > time() || $publication_timestamp == 0) {
499      if ($publication_timestamp > time() || $publication_timestamp == 0) return 0;      return 0;
500      }
501      $sent_count = db_result(db_query("  
502      SELECT count(s.sent)    $sent_count = db_result(db_query("SELECT count(s.sent) FROM {schedule_action} a LEFT JOIN {subscribed_sent} s ON a.action_id = s.action_id WHERE a.type = '%s' AND a.publication_id = %d AND a.schedule_id = %d AND a.pub_time = %d", $type, $publication_id, $schedule_id, $publication_timestamp));
503      FROM {schedule_action} a  
504      LEFT JOIN {subscribed_sent} s ON a.action_id = s.action_id    return $sent_count;
     WHERE a.type = '%s'  
     AND a.publication_id = %d  
     AND a.schedule_id = %d  
     AND a.pub_time = %d  
     ", $type, $publication_id, $schedule_id, $publication_timestamp));  
   
     return $sent_count;  
505  }  }
506    
507  // Calculate time of next scheduled send out  /**
508     * Calculate time of next scheduled send out.
509     */
510  function schedule_publication_time($previous, $frequency, $every) {  function schedule_publication_time($previous, $frequency, $every) {
511      if ($frequency == 'manual') {
512        return 0;
513      }
514    
515    if ($frequency == 'manual') return 0;    $previous = getdate($previous);
   
   $previous = getdate ($previous);  
516    
517    switch ($frequency) {    switch ($frequency) {
518      case 'hour':      case 'hour' :
519        $publication_time = mktime ($previous['hours'] + $every, $previous['minutes'], $previous['seconds'], $previous['mon'], ($previous['mday']), $previous['year'], -1);        $publication_time = mktime($previous['hours'] + $every, $previous['minutes'], $previous['seconds'], $previous['mon'], ($previous['mday']), $previous['year'], -1);
520        break;        break;
521      case 'day':      case 'day' :
522        $publication_time = mktime ($previous['hours'], $previous['minutes'], $previous['seconds'], $previous['mon'], ($previous['mday'] + $every), $previous['year'], -1);        $publication_time = mktime($previous['hours'], $previous['minutes'], $previous['seconds'], $previous['mon'], ($previous['mday'] + $every), $previous['year'], -1);
523        break;        break;
524      case 'month':      case 'month' :
525        $publication_time = mktime ($previous['hours'], $previous['minutes'], $previous['seconds'], ($previous['mon'] + $every), $previous['mday'], $previous['year'], -1);        $publication_time = mktime($previous['hours'], $previous['minutes'], $previous['seconds'], ($previous['mon'] + $every), $previous['mday'], $previous['year'], -1);
526        break;        break;
527      }    }
528    
529      return $publication_time;    return $publication_time;
530  }  }
531    
532    /**
533  // get count of queued   * Get count of queued.
534  // ????   */
535  function schedule_count_queued($type, $publication_id, $publication_timestamp, $schedule_id) {  function schedule_count_queued($type, $publication_id, $publication_timestamp, $schedule_id) {
536      // Publication hasn't started sending.
537      // publication hasn't started sending    if ($publication_timestamp > time() || $publication_timestamp == 0) {
538      if ($publication_timestamp > time() || $publication_timestamp == 0) return 0;      return 0;
539      }
     $queued_count = db_result(db_query("  
     SELECT COUNT(*)  
     FROM {subscribed} s  
     LEFT JOIN {users} u ON s.uid = u.uid  
     LEFT JOIN {bounced_email_count} b ON u.mail = b.email  
     LEFT JOIN {schedule_action} a ON (a.type = s.type AND a.publication_id = s.publication_id AND a.pub_time = %d)  
     LEFT JOIN {subscribed_sent} m ON (m.uid = s.uid AND m.action_id = a.action_id)  
     WHERE s.publication_id = %d  
     AND s.schedule_id = %d  
     AND s.type = '%s'  
     AND (b.bounces IS NULL OR b.bounces < %d)  
     AND m.sent IS NULL  
     ORDER BY u.name  
   ", $publication_timestamp, $publication_id, $type, variable_get('bounce_limit', 1), $schedule_id));  
540    
541    return $queued_count;    $queued_count = db_result(db_query("SELECT COUNT(*) FROM {subscribed} s LEFT JOIN {users} u ON s.uid = u.uid LEFT JOIN {bounced_email_count} b ON u.mail = b.email
542        LEFT JOIN {schedule_action} a ON (a.type = s.type AND a.publication_id = s.publication_id AND a.pub_time = %d) LEFT JOIN {subscribed_sent} m ON (m.uid = s.uid AND m.action_id = a.action_id)
543        WHERE s.publication_id = %d AND s.schedule_id = %d AND s.type = '%s' AND (b.bounces IS NULL OR b.bounces < %d) AND m.sent IS NULL ORDER BY u.name ",
544        $publication_timestamp, $publication_id, $type, variable_get('bounce_limit', 1), $schedule_id));
545    
546      return $queued_count;
547  }  }
548    
549  // update schedule - unless relative  /**
550     * Update schedule - unless relative.
551     */
552  function schedule_set_next_publication_time($schedule) {  function schedule_set_next_publication_time($schedule) {
553      // Check time for update or relative.
554      if (empty($schedule['next']) || $schedule['next'] > time() || !empty($schedule['relative'])) {
555        return;
556      }
557    
558      // check time for update or relative    // Set record of schedule before it gets updated.
559      if ($schedule['next'] > time() || !empty($schedule['relative'])) return;    schedule_action_set($schedule['type'], $schedule['publication_id'], $schedule['schedule_id'], $schedule['next'], $schedule['vids']);
   
     // set record of schedule before it gets updated  
     schedule_set_action($schedule['type'], $schedule['publication_id'], $schedule['schedule_id'], $schedule['next']);  
560    
561      // set last sent to this current send out    // Update timestamp fields.
562      if ($schedule['frequency'] != 'manual') $schedule['last'] = $schedule['next'];    $schedule['previous'] = $schedule['last'];
563      $schedule['next'] = schedule_publication_time($schedule['next'], $schedule['frequency'], $schedule['every']);    $schedule['last'] = $schedule['next'];
564      schedule_update_schedule($schedule['type'], $schedule);    $schedule['next'] = schedule_publication_time($schedule['next'], $schedule['frequency'], $schedule['every']);
   
     return;  
 }  
565    
566  function schedule_get_publication_time($type, $schedule_id, $uid) {    schedule_update_schedule($schedule['type'], $schedule);
567    
568      $schedule = schedule_select_schedule($schedule_id);    return $schedule;
569    }
570    
571      if ($schedule['frequency'] == 'manual') $publication_time = 0;  /**
572      if ($schedule['frequency'] != 'manual' && empty($schedule['relative_date'])) $publication_time = $schedule['next'];   *
573     */
574    function schedule_get_publication_time($type, $schedule_id, $uid) {
575      $schedule = schedule_select_schedule($schedule_id);
576    
577      if (!empty($schedule['relative_date']) && $schedule['frequency'] != 'manual') {    if ($schedule['frequency'] == 'manual') {
578        $publication_time = 0;
579          $result = db_query('SELECT f.fid, f.type, v.value FROM {profile_fields} f INNER JOIN {profile_values} v ON f.fid = v.fid WHERE uid = %d', $uid);    }
580      if ($schedule['frequency'] != 'manual' && empty($schedule['relative_date'])) {
581          while ($field = db_fetch_object($result)) {      $publication_time = $schedule['next'];
582              if (empty($profile[ $field->fid ])) {    }
                 $profile[ $field->fid ] = $field->value;  
             }  
         }  
583    
584          $publication_time = schedule_relative_time($schedule['relative_date'], $schedule['relative'], $schedule['frequency'], $schedule['every'], $schedule['timeout'], $profile);    if (!empty($schedule['relative_date']) && $schedule['frequency'] != 'manual') {
     }  
585    
586      return $publication_time;      if (module_exists('profile')) {
587          $result = db_query('SELECT f.fid, f.type, v.value FROM {profile_fields} f INNER JOIN {profile_values} v ON f.fid = v.fid WHERE uid = %d', $uid);
588    
589          while ($field = db_fetch_object($result)) {
590            if (empty($profile[$field->fid])) {
591              $profile[$field->fid] = $field->value;
592            }
593          }
594    
595  }      }
596    
597        $publication_time = schedule_relative_time($schedule['relative_date'], $schedule['relative'], $schedule['frequency'], $schedule['every'], $schedule['timeout'], $profile);
598      }
599    
600  function schedule_check_relative_update($type, $schedule_id, $uid, $value_new) {    return $publication_time;
601    }
602    
603      $schedule = schedule_select_schedule($schedule_id);  /**
604      list ($source_table, $id) = explode ('|', $schedule['relative_date']);   *
605     */
606    function schedule_check_relative_update($type, $schedule_id, $uid, $value_new) {
607      $schedule = schedule_select_schedule($schedule_id);
608      list($source_table, $id) = explode('|', $schedule['relative_date']);
609      if (module_exists('profile')) {
610      $value_old = db_query('SELECT v.value {profile_values} v WHERE v.uid = %d AND v.fid = %d LIMIT 1', $uid, $id);      $value_old = db_query('SELECT v.value {profile_values} v WHERE v.uid = %d AND v.fid = %d LIMIT 1', $uid, $id);
611      $updated = ($value_new == $value_old) ? FALSE : TRUE;    }
612      return $updated;    $updated = ($value_new == $value_old) ? FALSE : TRUE;
613      return $updated;
614  }  }
615    
616    /**
617     *
618     */
619  function schedule_convert_form_data($schedule) {  function schedule_convert_form_data($schedule) {
620      if ($schedule['frequency'] == 'manual') {
621        $schedule['every'] = 0;
622        $schedule['next_date'] = 0;
623      }
624    
625      if ($schedule['frequency'] == 'manual') {    if ($schedule['frequency'] != 'manual') {
626        $schedule['every'] = 0;      // Convert 'next' time to unix timestamp.
627        $schedule['next_date'] = 0;      $next = strtotime($schedule['next_date']);
628      }      $next_array = getdate($next);
629        $schedule['next'] = mktime($schedule['next_hour'], 0, 0, $next_array['mon'], $next_array['mday'], $next_array['year'], -1);
     if ($schedule['frequency'] != 'manual') {  
   
         // convert 'next' time to unix timestamp  
         $next = strtotime ($schedule['next_date']);  
         $next_array = getdate($next);  
         $schedule['next'] = mktime ($schedule['next_hour'], 0, 0, $next_array['mon'], $next_array['mday'], $next_array['year'], -1);  
630    
631          // convert time to start including nodes from (for first publication)      // Convert time to start including nodes from (for first publication).
632          $schedule['start'] = (!isset($schedule['start'])  || $schedule['start'] == 0) ? mktime (1, 0, 0, 1, 1, 1999, -1) : strtotime($schedule['start']);      $schedule['start'] = (!isset ($schedule['start']) || $schedule['start'] == 0) ? mktime(1, 0, 0, 1, 1, 1999, -1) : strtotime($schedule['start']);
633      }
634      }  
635      return $schedule;
     return $schedule;  
636  }  }
637    
638    /**
639     *
640     */
641  function _schedule_profile_date_fields() {  function _schedule_profile_date_fields() {
642      $all_fields = _schedule_profile_fields();    $all_fields = _schedule_profile_fields();
643    
644      foreach ($all_fields as $field) {    foreach ($all_fields as $field) {
645          if ($field->type == 'date') $date_fields['profile|' . $field->fid] =  t('Profile: ') . $field->name;      if ($field->type == 'date') {
646          $date_fields['profile|'. $field->fid] = t('Profile: ') . $field->name;
647      }      }
648      }
     return $date_fields;  
 }  
649    
650      return $date_fields;
651    }
652    
653  /*  /**
654  // get info about profile fields   * Get info about profile fields.
655  // $key should be an attribute of profile fields, e.g. name, fid   *
656  // an array is returned   * @param $key
657  // $fields[$key] = all profile field info (object)   *   An attribute of profile fields, e.g. name, fid.
658  // or   * @param $return_value
659  // $fields[$key] = an element of the profile field (specified by $return_value)   *   Specifies the element to be returned.
660  */   * @return
661     *  Array of objects wit all profile field info. Or an element of the profile
662     *   field specified by $return_value.
663     */
664  function _schedule_profile_fields($key = 'fid', $return_value = NULL) {  function _schedule_profile_fields($key = 'fid', $return_value = NULL) {
665    
666      if (!module_exist('profile')) return;    if (!module_exists('profile')) {
667        return array();
668      static $fields_static = array();    }
669    
670      // avoid making more than one database call for profile info    static $fields_static = array();
671      if (empty($fields_static)) {  
672      // Avoid making more than one database call for profile info.
673          $results = db_query("SELECT * FROM {profile_fields}");    if (empty($fields_static)) {
674        $results = db_query("SELECT * FROM {profile_fields}");
675          while ($row = db_fetch_object($results)) {  
676              // don't include private fields      while ($row = db_fetch_object($results)) {
677              if (user_access('administer users') || $row->visibility != PROFILE_PRIVATE) {        // Don't include private fields.
678                  $fields_static[] = $row;        if (user_access('administer users') || $row->visibility != PROFILE_PRIVATE) {
679              }          $fields_static[] = $row;
680          }        }
   
681      }      }
682      }
683      // return all profile fields info, or just specific type  
684      if (empty($return_value)) {    // Return all profile fields info, or just specific type.
685          foreach ($fields_static as $field) {    $fields = array();
686              $fields[$field->{$key}] = $field;    if (empty($return_value)) {
687          }      foreach ($fields_static as $field) {
688      } else {        $fields[$field->{$key}] = $field;
         foreach ($fields_static as $field) {  
             $fields[$field->{$key}] = $field->{$return_value};  
         }  
689      }      }
690      }
691      asort($fields);    else {
692      return $fields;      foreach ($fields_static as $field) {
693          $fields[$field->{$key}] = $field->{$return_value};
694        }
695      }
696    
697      asort($fields);
698      return $fields;
699  }  }
700    
701    /**
702     * Set record of schedule - before it gets updated.
703     */
704    function schedule_action_set($type, $publication_id, $schedule_id, $pub_time, $vids) {
705      db_query("INSERT INTO {schedule_action} (type, publication_id, schedule_id, pub_time, vid) VALUES ('%s', %d, %d, %d, '%s')", $type, $publication_id, $schedule_id, $pub_time, serialize($vids));
706    
707  // Set record of schedule - before it gets updated    $action_id = db_result(db_query("SELECT LAST_INSERT_ID()"));
 function schedule_set_action($type, $publication_id, $schedule_id, $pub_time) {  
   
     // check if action exists  
     $action_id = db_result(db_query("SELECT action_id FROM {schedule_action} WHERE publication_id = %d AND schedule_id = %d AND pub_time = %d AND type = '%s'", $publication_id, $schedule_id, $pub_time, $type));  
708    
709      // if not create action    return $action_id;
710      if (empty($action_id)) {  }
711    
712          db_query("INSERT INTO {schedule_action}  /**
713                          (action_id, type, publication_id, schedule_id, pub_time) VALUES   * Get last action id of schedule.
714                          (NULL, '%s', %d, %d, %d)",   */
715                          $type, $publication_id, $schedule_id, $pub_time);  function schedule_action_select_db($type, $publication_id, $schedule_id) {
716      $action_id = db_result(db_query("SELECT action_id FROM {schedule_action} WHERE publication_id = %d AND schedule_id = %d AND type = '%s' ORDER BY action_id DESC LIMIT 1", $publication_id, $schedule_id, $type));
717          $action_id = db_result(db_query("SELECT LAST_INSERT_ID()"));    return $action_id;
   
     } else {  
   
         db_query("UPDATE {schedule_action} SET  
                         type = '%s', publication_id = %d, schedule_id = %d, pub_time = %d WHERE action_id = %d",  
                         $type, $publication_id, $schedule_id, $pub_time, $action_id);  
     }  
   
     return $action_id;  
718  }  }
719    
720    /**
721     * Based on a publication time - get next recorded publication time of a schedule.
722     */
723    function schedule_action_next($type, $publication_id, $schedule_id, $pub_time) {
724      static $actions = array();
725    
726      if (!empty($actions[$type][$publication_id][$schedule_id][$pub_time])) {
727        $next = ($actions[$type][$publication_id][$schedule_id][$pub_time] == 'none') ? 0 : $actions[$type][$publication_id][$schedule_id][$pub_time];
728      }
729      else {
730        // Current action id.
731        $action_id = db_result(db_query("SELECT action_id FROM {schedule_action} WHERE publication_id = %d AND schedule_id = %d AND pub_time = %d AND type = '%s'", $publication_id, $schedule_id, $pub_time, $type));
732        $next = db_result(db_query("SELECT pub_time FROM {schedule_action} WHERE publication_id = %d AND schedule_id = %d AND type = '%s' AND action_id > %d ORDER BY action_id LIMIT 1", $publication_id, $schedule_id, $type, $action_id));
733        $actions[$type][$publication_id][$schedule_id][$pub_time] = empty($next) ? 'none' : $next;
734      }
735    
736  // Date relative schedule    return $next;
737    }
738    
739    /**
740     * Date relative schedule.
741     */
742  function schedule_relative_time($source, $relation, $units, $amount, $timeout, $time_data) {  function schedule_relative_time($source, $relation, $units, $amount, $timeout, $time_data) {
743    
744    list ($source_table, $id) = explode ('|', $source);    list($source_table, $id) = explode('|', $source);
745    
746    switch ($source_table) {    switch ($source_table) {
747      case 'profile':      case 'profile':
748          $time_data = $time_data[$id];
749        $time_data = $time_data[$id];        if (empty($time_data)) {
750        if (empty($time_data)) return;          return;
751        $send_time = schedule_relative_to_profile($relation, $units, $amount, $time_data);        }
752        break;        $send_time = schedule_relative_to_profile($relation, $units, $amount, $time_data);
753          break;
754      case 'event':      case 'event':
755          break;
756        break;      default :
     default:  
757        break;        break;
758    }    }
759    
760      $timeout = $send_time + ($timeout * 3600);    $timeout = $send_time + ($timeout * 3600);
     if (time() > $timeout) return;  
   
     return $send_time;  
 }  
761    
762      if (time() > $timeout) {
763        return;
764      }
765    
766      return $send_time;
767    }
768    
769  //  /**
770     *
771     */
772  function schedule_relative_to_profile($relation, $units, $amount, $time_data) {  function schedule_relative_to_profile($relation, $units, $amount, $time_data) {
773    
774    if (empty($time_data)) return;    if (empty($time_data)) {
775        return;
776      }
777    
778    $time = unserialize($time_data);    $time = unserialize($time_data);
779    
# Line 714  function schedule_relative_to_profile($r Line 781  function schedule_relative_to_profile($r
781    
782    switch ($relation) {    switch ($relation) {
783      case 'before':      case 'before':
784        if ($units == 'hour') $time['hour'] = $time['hour'] - $amount;        if ($units == 'hour')
785        if ($units == 'day') $time['day'] = $time['day'] - $amount;          $time['hour'] = $time['hour'] - $amount;
786        if ($units == 'month') $time['month'] = $time['month'] - $amount;        if ($units == 'day')
787            $time['day'] = $time['day'] - $amount;
788          if ($units == 'month')
789            $time['month'] = $time['month'] - $amount;
790        break;        break;
791        case 'after' :
792      case 'after':        if ($units == 'hour')
793        if ($units == 'hour') $time['hour'] = $time['hour'] + $amount;          $time['hour'] = $time['hour'] + $amount;
794        if ($units == 'day') $time['day'] = $time['day'] + $amount;        if ($units == 'day')
795        if ($units == 'month') $time['month'] = $time['month'] + $amount;          $time['day'] = $time['day'] + $amount;
796          if ($units == 'month')
797            $time['month'] = $time['month'] + $amount;
798        break;        break;
799        default :
     default:  
800        break;        break;
801    }    }
802    
803    $send_time = mktime ( $time['hour'], 0, 0, $time['month'], $time['day'], $time['year'] );    $send_time = mktime($time['hour'], 0, 0, $time['month'], $time['day'], $time['year']);
804    
805    return $send_time;    return $send_time;
806  }  }
807    
808  // update all the schedules - if they need updating  /**
809  //   *
810     */
811  function schedule_update_schedules($uncache = FALSE) {  function schedule_update_schedules($uncache = FALSE) {
812      // Update only once per request - unless specified.
813      static $updated;
814      if ($uncache) {
815        $updated = FALSE;
816      }
817      if ($updated == TRUE) {
818        return;
819      }
820    
821      // update only once per request - unless specified    // Get all schedules.
822      static $updated;    $schedules = schedule_select_schedules();
     if ($un_cache) $updated = FALSE;  
     if ($updated == TRUE) return;  
   
     // get all schedules  
     $schedules = schedule_select_schedules();  
   
     // loop through schedules  
     foreach ($schedules as $schedule) {  
   
         // update schedule  
         schedule_set_next_publication_time($schedule);  
   
     }  
   
     $updated == TRUE;  
     return;  
 }  
823    
824  function schedule_cron() {    // Loop through schedules.
825      foreach ($schedules as $schedule) {
826        // Update schedule.
827        schedule_set_next_publication_time($schedule);
828      }
829    
830      schedule_update_schedules();    $updated = TRUE;
831      return;
832  }  }
833    
834    /**
835  ?>   * Implementation of hook_cron().
836     */
837    function schedule_cron() {
838      schedule_update_schedules();
839    }

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

  ViewVC Help
Powered by ViewVC 1.1.2