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

Legend:
Removed from v.1.5.2.1  
changed lines
  Added in v.1.5.2.2

  ViewVC Help
Powered by ViewVC 1.1.2