/[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.4, Mon Nov 14 01:12:48 2005 UTC revision 1.5, Mon Jan 8 16:25:56 2007 UTC
# Line 17  function schedule_help($section = "admin Line 17  function schedule_help($section = "admin
17    
18  // ********** FORMS *****************************  // ********** FORMS *****************************
19    
20  function schedule_form_admin($schedule, $include = array()) {  // deprecated for above individual functions
21    function schedule_form_admin(&$form) {
22    
23      $output .= _schedule_form_defaults($schedule);      if (!empty($form['schedule_id']['#value'])) $schedule = schedule_select_schedule($form['schedule_id']['#value']);
24      if (!$include || in_array('frequency', $include)) $output .= _schedule_form_frequency($schedule);  
25      if (!$include || in_array('include', $include)) $output .= _schedule_form_include($schedule);      /*
26      if (!$include || in_array('next_send', $include)) $output .= _schedule_form_next_send($schedule);      // required form elements
27      if (!$include || in_array('send_now', $include)) $output .= _schedule_form_send_now($schedule);      */
28      return $output;  
29  }      $form['schedule_id'] = array(
30          '#type' => 'value',
31  function _schedule_form_defaults($settings = NULL) {        '#value' => $schedule['schedule_id'],
32        );
33      if ($settings->publication_id) $output .= form_hidden('publication_id', $settings->publication_id);  
34      if ($settings->first) $output .= form_hidden('first', $settings->first);      $form['first'] = array(
35      if ($settings->last) $output .= form_hidden('last', $settings->last);        '#type' => 'value',
36      if ($settings->title) $output .= form_hidden('title', $settings->title);        '#value' => $schedule['first'],
37      if (!$settings->frequency) $output .= form_hidden('frequency', 'manual');      );
38      return $output;  
39  }      $form['last'] = array(
40          '#type' => 'value',
41  function _schedule_form_frequency($settings = NULL) {        '#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    
     if (!$settings->frequency) $settings->frequency = 'manual';  
     $frequency .= form_textfield (t('Every'), 'every' , $settings->every, 5, 3);  
     $frequency .= form_radios(t('Frequency'), 'frequency', $settings->frequency, array('hour' => t('Hour(s)'), 'day' => t('Day(s)'), 'month' => t('Month(s)'), 'manual' => t('Manual')));  
     $output .= form_group(t('Frequency'), $frequency, t('How often this email newsletter should be sent'));  
     return $output;  
 }  
   
 function _schedule_form_include($settings = NULL) {  
   
128      // Schedule has already started - no need to display include field      // Schedule has already started - no need to display include field
129      if ($settings->last && $settings->last != 0 && $settings->start) return form_hidden ('start', $settings->start);      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 = (!$settings->start OR $settings->start == 0) ? '1/1/90' : date ('m/d/y', $settings->start);      $include_date = (empty($schedule['start'])) ? '1/1/99' : date ('m/d/y', $schedule['start']);
138      $include_start = form_textfield (t('Include Nodes From (date)'), 'start', $include_date, 10, 10, t('Leave blank to include all. (mm/dd/yy)'));  
139      return form_group(t('Include'), $include_start, t('On first send out, include nodes stating from this date.'));      $form['include'] = array(
140  }        '#type' => 'fieldset',
141          '#title' => t('Include'),
142  function _schedule_form_next_send($settings = NULL) {        '#description' => t('On first send out, include nodes stating from this date.'),
143        );
144      if (!$settings->next OR $settings->next == 0) {  
145          $start_hour = ($settings->next_hour) ? $settings->next_hour : '8';      $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());          $start_date = date ("m/d/y", time());
162          $start_day = date ("l, M j Y", time());          $start_day = date ("l, M j Y", time());
163      } else {      } else {
164          $start_hour = date ('G', $settings->next);          $start_hour = ($schedule['next']) ? date ('G', $schedule['next']) : $schedule['next_hour'];
165          $start_date = date ('m/d/y', $settings->next);          $start_date = ($schedule['next']) ? date ('m/d/y', $schedule['next']) : $schedule['next_date'];
166          $start_day = date ('l, M j Y', $settings->next);          $start_day = ($schedule['next']) ? date ('l, M j Y', $schedule['next']) : date ('l, M j Y', strtotime($schedule['next_date']));
167      }      }
168    
169      $publish .= form_textfield (t('Send (date)'), 'next_date', $start_date, 10, 10, t('%start_day (mm/dd/yy)', array('%start_day' => $start_day)));      $form['next'] = array(
170      $publish .= form_select(t('Send (hour)'), 'next_hour', $start_hour, 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.'));        '#type' => 'fieldset',
171      return form_group(t('Next Send'), $publish, t('Date and time to send next publication.'));        '#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($settings) {  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      $output = '<h2>' . t('send now') . '</h2>';      $form['schedules'] = array(
222      $output .= '<p>' . t('%publication will be sent on next cron run', array('%publication' => $settings->title)) . '</p>';        '#type' => 'checkboxes',
223      $output .= form_hidden ('publication_id', $settings->publication_id);        '#title' => t('Send Now'),
224      $output .= form_hidden ('title', $settings->title);        '#default_value' => array(),
225      $output .= form_submit(t('Send'));        '#options' => $options,
226      $output .= form_submit(t('Cancel'));        '#description' => t('Select %publication schedules to be sent now (next cron run).', array('%publication' => $publication['title'])),
227      return $output;        '#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    
# Line 91  function _schedule_form_send_now($settin Line 257  function _schedule_form_send_now($settin
257    
258  function schedule_insert_schedule($type, $schedule) {  function schedule_insert_schedule($type, $schedule) {
259    
260    db_query("INSERT INTO {schedules} (schedule_id, type, publication_id, start, first, next, last, every, frequency) VALUES (NULL, '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%s')", $type, $schedule->publication_id, $schedule->start, $schedule->first, $schedule->next, $schedule->last, $schedule->every, $schedule->frequency);    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()"));    return db_result(db_query("SELECT LAST_INSERT_ID()"));
268  }  }
269    
270    
271  function schedule_update_schedule($type, $schedule) {  function schedule_update_schedule($type, $schedule) {
272    
273    db_query("UPDATE {schedules} SET start = '%d', first = '%d', next = '%d', last = '%d', every = '%d', frequency = '%s' WHERE publication_id = '%d' AND type = '%s'", $schedule->start, $schedule->first, $schedule->next, $schedule->last, $schedule->every, $schedule->frequency, $schedule->publication_id, $type);    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;    return;
279  }  }
280    
281  function schedule_update_send_now($type, $publication_id) {  function schedule_update_send_now($type, $publication_id, $schedules, $time = NULL) {
282    
283    db_query("UPDATE {schedules} SET next = '%d' WHERE publication_id = '%d' AND type = '%s'", time(), $publication_id, $type);      if (empty($time)) $time = time();
284    return;  
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) (all)  // Get schedule from database (single)
305  function schedule_select_schedules($type, $publication_id = NULL) {  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 ($publication_id) {    if (!empty($type) && empty($publication_id)) {
       $schedules = db_fetch_object(db_query("SELECT * FROM {schedules} WHERE publication_id = '%d' AND type = '%s' LIMIT 1", $publication_id, $type));  
   } else {  
326        $results = db_query("SELECT * FROM {schedules} WHERE type = '%s'", $type);        $results = db_query("SELECT * FROM {schedules} WHERE type = '%s'", $type);
       while ($row = db_fetch_object($results)) {  
         $schedules[] = $row;  
       }  
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;    return $schedules;
339  }  }
340    
341  function schedule_delete_schedule($type, $publication_id) {  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);      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;      return;
352  }  }
353    
354    
355  // **********   MISC.   *****************************  // **********   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) {  function schedule_validate_frequency($edit) {
375    
376      if ($edit->frequency == 'manual') {      if ($edit['frequency'] == 'manual') return;
       $edit->every = 0;  
       $edit->next_date = 0;  
       return $edit;  
     }  
377    
378      // Check frequency amount has been set correctly      // Check frequency amount has been set correctly
379      if (!$edit->every || !is_numeric($edit->every) || $edit->every == 0) form_set_error('every', t("Send Schedule: Please set a number for 'Every'"));      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 $edit;      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) {  function schedule_validate_next_send($edit) {
400    
401      if ($edit->frequency == 'manual') {      if ($edit['frequency'] == 'manual') return;
402          $edit->next_date = 0;  
         return $edit;  
     }  
   
403      // check there is a publication date      // check there is a publication date
404      if (!$edit->next_date || $edit->next_date == 0) {      if (!isset($edit['next_date']) || $edit['next_date'] == 0) return form_set_error('next_date', t("Next Send: 'Send (date)' must be set."));
         form_set_error('next_date', t("Next Send: 'Send (date)' must be set."));  
         $edit->next_date = date ('m/d/y', time());  
         return $edit;  
     }  
405    
406      $next = strtotime ($edit->next_date);      $next = strtotime ($edit['next_date']);
407      $next_array = getdate($next);      $next_array = getdate($next);
408    
409      // check date is valid      // check date is valid
410      if (checkdate( $next_array['mon'], $next_array['mday'], $next_array['year'] ) == FALSE) {      if (checkdate( $next_array['mon'], $next_array['mday'], $next_array['year'] ) == FALSE) {
411          drupal_set_message (t("The 'Send Start' date is not correct, please use the mm/dd/yy format."), 'error');          return form_set_error('next_date', t("The 'Send Start' date is not correct, please use the mm/dd/yy format."));
         $edit->next = time();  
         return $edit;  
412      }      }
413    
414      // convert time to unix timestamp      // convert time to unix timestamp
415      $edit->next = mktime ($edit->next_hour, 0, 0, $next_array['mon'], $next_array['mday'], $next_array['year'], -1);      $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      // check publication time hasn't passed
418      if ( $edit->next <= time() ) form_set_error('next_hour', t("The 'Send Start' date has already passed."));      if ($edit['next'] <= time()) form_set_error('next_hour', t("The 'Send Start' date has already passed."));
419    
420      return $edit;      return;
421  }  }
422    
423  function schedule_validate_included($edit) {  function schedule_validate_included($edit) {
424    
425      // Check this is first publication      // Check this is first publication
426      if ($edit->last && $edit->last != 0) return $edit;      if (isset($edit['last']) && $edit['last'] != 0) return;
427    
428      // default start date      // default start date
429      if (!$edit->start  || $edit->start == 0) {      if (!isset($edit['start']) || $edit['start'] == 0) return;
         $edit->start = mktime (1, 0, 0, 1, 1, 1999, -1);  
         return $edit;  
     }  
430    
431      // convert time to unix timestamp      // convert time to unix timestamp
432      $edit->start = strtotime($edit->start);      $edit['start'] = strtotime($edit['start']);
433    
434      // check date is valid      // check date is valid
435      $include_nodes_date = getdate($edit->start);      $include_nodes_date = getdate($edit['start']);
436      if (!checkdate( $include_nodes_date['mon'], $include_nodes_date['mday'], $include_nodes_date['year'] )) {      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."));          form_set_error('start', t("The 'Include Nodes From' date is not correct, please use the mm/dd/yy format."));
         $edit->start = mktime (1, 0, 0, 1, 1, 1999, -1);  
438      }      }
439    
440      // check date is in past      // check date is in past
441      if ($edit->start > time()) {      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."));          form_set_error('start', t("The 'Include Nodes From' date cannot be in future, has been reset to today's date."));
         $edit->start = mktime (1, 0, 0, 1, 1, 1999, -1);  
443      }      }
444    
445      return $edit;      return;
446  }  }
447    
448  // check if ready to send on next cron run  // check if ready to send on next cron run
449  function schedule_check_send_ready($type, $publication_id) {  function schedule_check_send_ready($type, $publication_id) {
450    
451      $schedule = schedule_select_schedules($type, $publication_id);      $schedule = schedule_select_schedules($type, $publication_id);
452      $time = ($schedule->next < time()) ? TRUE : FALSE;      $time = ($schedule['next'] < time()) ? TRUE : FALSE;
453      return $ready;      return $ready;
454  }  }
455    
456  // get count of sent  // get count of sent
457  function schedule_count_sent($type, $publication_id, $publication_timestamp) {  // ????
458    function schedule_count_sent($type, $publication_id, $publication_timestamp, $schedule_id) {
459    
460      // publication hasn't started sending      // publication hasn't started sending
461      if ($publication_timestamp > time() || $publication_timestamp == 0) return 0;      if ($publication_timestamp > time() || $publication_timestamp == 0) return 0;
462    
463      $sent_count = db_result(db_query("      $sent_count = db_result(db_query("
464      SELECT count(*)      SELECT count(s.sent)
465      FROM {schedules_sent}      FROM {schedules_action} a
466      WHERE type = '%s'      LEFT JOIN {subscribed_sent} s ON a.action_id = s.action_id
467      AND publication_id = '%d'      WHERE a.type = '%s'
468      AND pub_time = '%d'      AND a.publication_id = %d
469      ", $type, $publication_id, $publication_timestamp));      AND a.schedule_id = %d
470        AND a.pub_time = %d
471        ", $type, $publication_id, $schedule_id, $publication_timestamp));
472    
473      return $sent_count;      return $sent_count;
474  }  }
475    
476  // Set time for next scheduled send out  // Calculate time of next scheduled send out
477  function schedule_publication_time($previous, $frequency, $every) {  function schedule_publication_time($previous, $frequency, $every) {
478    
479    if ($frequency == 'manual') return 0;    if ($frequency == 'manual') return 0;
480    
481    $previous = getdate ($previous);    $previous = getdate ($previous);
# Line 251  function schedule_publication_time($prev Line 490  function schedule_publication_time($prev
490      case 'month':      case 'month':
491        $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);
492        break;        break;
       default:  
493      }      }
494    
495      return $publication_time;      return $publication_time;
496  }  }
497    
498    
499  // get count of queued  // get count of queued
500  function schedule_count_queued($type, $publication_id, $publication_timestamp) {  // ????
501    function schedule_count_queued($type, $publication_id, $publication_timestamp, $schedule_id) {
502    
503      // publication hasn't started sending      // publication hasn't started sending
504      if ($publication_timestamp > time() || $publication_timestamp == 0) return 0;      if ($publication_timestamp > time() || $publication_timestamp == 0) return 0;
# Line 269  function schedule_count_queued($type, $p Line 508  function schedule_count_queued($type, $p
508      FROM {subscribed} s      FROM {subscribed} s
509      LEFT JOIN {users} u ON s.uid = u.uid      LEFT JOIN {users} u ON s.uid = u.uid
510      LEFT JOIN {bounced_email_count} b ON u.mail = b.email      LEFT JOIN {bounced_email_count} b ON u.mail = b.email
511      LEFT JOIN {schedules_sent} m ON (m.uid = s.uid AND m.type = s.type AND m.publication_id = s.publication_id AND m.pub_time = '%d')      LEFT JOIN {schedules_action} a ON (a.type = s.type AND a.publication_id = s.publication_id AND a.pub_time = %d)
512      WHERE s.publication_id = '%d'      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'      AND s.type = '%s'
516      AND (b.bounces IS NULL OR b.bounces < '%d')      AND (b.bounces IS NULL OR b.bounces < %d)
517      AND m.sent IS NULL      AND m.sent IS NULL
518      ORDER BY u.name      ORDER BY u.name
519    ", $publication_timestamp, $publication_id, $type, variable_get('bounce_limit', 1)));    ", $publication_timestamp, $publication_id, $type, variable_get('bounce_limit', 1), $schedule_id));
520    
521    return $queued_count;    return $queued_count;
522    
523  }  }
524    
525    // update schedule - unless relative
526  function schedule_set_next_publication_time($schedule) {  function schedule_set_next_publication_time($schedule) {
527    
528      // next publication date already set      // check time for update or relative
529      if ($schedule->next > time()) return;      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      // set last sent to this current send out
535      $schedule->last = $schedule->next;      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    
     $schedule->next = schedule_publication_time($schedule->next, $schedule->frequency, $schedule->every);  
     schedule_update_schedule($schedule->type, $schedule);  
539      return;      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.4  
changed lines
  Added in v.1.5

  ViewVC Help
Powered by ViewVC 1.1.2