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

Diff of /contributions/modules/pegevent/pegevent.module

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

revision 1.1.4.1, Wed May 2 12:42:23 2007 UTC revision 1.1.4.2, Wed Mar 12 02:17:00 2008 UTC
# Line 0  Line 1 
1    <?php
2    // $Id$
3    
4    // PEGevent by Joe Golden of Triangul.us`
5    // Developemnt sponsored by CCTV, Chittenden Community TV, cctv.org.
6    
7    // D5 modifications for new underlying table structure
8    // :1,$s/node_content_program/content_type_program/g
9    // :1,$s/node_content_showing/content_type_showing/g
10    // links from sched page added dev
11    // Generaliation 20080227
12    #       changed all content_type_content_showing to content_type_showing
13    #               same w/ programs
14    
15    function pegevent_help($section='') {
16    
17      $output = '';
18    
19      switch ($section) {
20        case "admin/modules#description":
21          $output = t("PEGevent Airtime Scheduling module.");
22          break;
23      }
24    
25      return $output;
26    
27    } // function pegevent_help
28    
29    /**
30     * Valid permissions for this module
31     * @return An array of valid permissions for the cctv module
32     */
33    function pegevent_perm() {
34            return array('access content');
35    } // function pegevent_perm()
36    
37    function pegevent_mark_nid_as_term( $nid, $tid ){
38    # testing
39    #$dbg=1;
40    # mark node nid as term tid.
41            $qs = "insert into term_node set tid=$tid, nid=$nid";
42            $qr = db_query( $qs );
43            if (!$dbg){ return; }
44    
45            print "in pegevent_mark_nid_as_term( $nid, $tid )<br>";
46            print "\$qs is $qs<br>";
47            if ( $qr == DB_OK ){
48                    print "success in pegevent_mark_nid_as_term<br>";
49            } else {
50                    print "failed in pegevent_mark_nid_as_term<br>";
51            }
52    }
53    
54    ############################################################################################
55    
56    function pegevent_set_time_terms( $nid ){
57    # testing
58    # set terms for program or showing
59    #$dbg=1;
60    $production_last_month_tid = 190;
61    $production_this_month_tid = 198;
62    $production_next_month_tid = 199;
63    $production_last_week_tid = 188;
64    $production_this_week_tid = 197;
65    $production_next_week_tid = 189;
66    $production_yesterday = 200;
67    $production_today = 201;
68    $production_tomorrow = 202;
69    # array p_term is terms for production
70    $p_terms = array ( 200, 201, 202, 188, 197, 189, 190, 198, 199
71    # order: yest, today, tomorrow, last week, this week, next week
72    #       last month, this month, next month
73    );
74    
75    $airing_last_month_tid = 194;
76    $airing_this_month_tid = 195;
77    $airing_next_month_tid = 196;
78    $airing_last_week_tid = 193;
79    $airing_this_week_tid = 192;
80    $airing_next_week_tid = 191;
81    $airing_yesterday = 203;
82    $airing_today = 204;
83    $airing_tomorrow = 205;
84    # array s_term is terms for showings
85    $s_terms = array ( 203, 204, 205, 193, 192, 191, 194, 195, 196
86    # order: yest, today, tomorrow, last week, this week, next week
87    #       last month, this month, next month
88    );
89    $terms = array();
90    
91    if ( pegevent_is_program( $nid ) ){
92            if ($dbg){print "dbg>> in pegevent_set_time_terms: is a program<br>";}
93            $terms = $p_terms;
94            $pdate = pegevent_get_pdate( $nid );
95            # date is pdate
96            $date = strtotime( $pdate );
97    
98    } elseif ( pegevent_is_showing( $nid ) ){
99            if ($dbg){print "dbg>> in pegevent_set_time_terms: is a showing<br>";}
100            # date is airtime
101            $terms = $s_terms;
102            $date = pegevent_get_start_time( $nid );
103    } else {
104            die(" In pegevent_set_time_terms passed nid $nid. Neither program nor showing.<br>");
105    }
106            ### boundary times as unixtimes ##
107            ## these should respect DST boundaries ##
108            $yesterday = strtotime("yesterday");
109            $today = strtotime("today");
110            $tomorrow = strtotime("tomorrow");
111            $day_after_tomorrow = strtotime("tomorrow +1 day");
112    
113            $last_week = strtotime("Sunday 2 weeks ago");
114            $this_week = strtotime("Sunday 1 weeks ago");
115            $next_week = strtotime("Sunday");
116            $n_n_week = strtotime("Sunday next week");
117    
118            $last_month = mktime(0, 0, 0, date("m")-1, 1,   date("Y"));
119            $this_month = mktime(0, 0, 0, date("m"), 1,   date("Y"));
120            $next_month = mktime(0, 0, 0, date("m")+1, 1,   date("Y"));
121            $n_n_month = mktime(0, 0, 0, date("m")+2, 1,   date("Y"));
122            ###################################
123    
124    # order: yest, today, tomorrow, last week, this week, next week
125    #       last month, this month, next month
126            # days
127            if ( $yesterday == $date ){
128                    pegevent_mark_nid_as_term( $nid, $terms[0] );
129                    print "$nid produced yesterday<br>";
130            } elseif ( $today == $date ){
131                    print "$nid produced today<br>";
132                    pegevent_mark_nid_as_term( $nid, $terms[1] );
133            } elseif ( $tomorrow == $date ){
134                    print "$nid production tomorrow<br>";
135            }
136    
137            # weeks
138            if ( $last_week<=$date and $date<$this_week){
139                    print "produced last week<br>";
140            } elseif ( $this_week<=$date and $date<$next_week){
141                    print "production this week<br>";
142            } elseif ( $next_week<=$date and $date<$n_n_week){
143                    print "production next week<br>";
144            }
145    
146    
147            # months
148            if ( $last_month<=$date and $date<$this_month ){
149                    print "produced last month<br>";
150            } elseif ( $this_month<=$date and $date<$next_month ){
151                    print "produced this month<br>";
152            } elseif ( $next_month<=$date and $date<$n_n_month ){
153                    print "produced next month<br>";
154            }
155    }
156    ##########################################################################################
157    
158    function pegevent_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
159    # called when other actions are taken
160    #$dbg=1;
161    #$td=1;# tight debug
162            if($dbg){print "dbg>> in pegevent_nodeapi op is $op<br>";}
163    
164            if  ( ($td) and ( $op="update") and ($node->type=="program" or $node->type=="showing") ){
165                    # tag as appropriate time term(s): produced last week, airing next month, etc.
166                    if ($td){
167                    print "dbg>> in pegevent_nodeapi in td nid is $node->nid<br>";
168    #               pegevent_set_time_terms( $node->nid );
169                    }
170            }
171    
172            # updating a program name: need to update showing names also
173            if  ( ( $op=="update" ) and ($node->type == "program") ){
174    
175                    # rename all showings to correspond to program title
176                    # tested and works for modifying TBA live shows 20080115 jg
177                    pegevent_rename_showings_of_program( $node->nid );
178    
179            }
180            # mark as time taxo: prod next week, airs next month, etc.?
181    }
182    
183    ########################################################################################
184    
185    function pegevent_rename_showings_of_program( $program_nid ){
186    #$dbg=1;
187    
188            $title = pegevent_get_title( $program_nid );
189            if ( empty( $title ) ) { return; }
190            $qs = " select nid from content_type_showing where field_program_nid = $program_nid ";
191            if($dbg){print "dbg>> title is $title, \$qs is $qs<br>";}
192            $qr = db_query( $qs );
193            while ( $row= mysql_fetch_row( $qr ) ){
194                    $node = node_load( $row[0] );
195    
196                    $node->title = $title;
197    
198                    if ($dbg){print "dbg >> rename_showings_of_program: pre submit<br><pre>";var_dump( $node ); print "</pre>";}
199                    node_save( $node );
200            }
201    }
202    
203    #############################################################################
204    
205    function pegevent_MV_export_playlist( $date ) {
206    #$dbg=1;
207    # export a MaestroVision playlist for date
208    # only for use with MaestroVision Playback system
209    # Produces a table to be copied into a text file.
210    # example output:
211    # Start Time    Duration        HouseNumber     Path                    Type    \
212    # 00:00:00:00   00:00:30:00     Council 5/13    M:\abc\xyz\name.mpeg2   Mpeg2   \
213    #               Trigger Command         Start Date
214    #               Chain                   2005-05-15
215    # 20080207 BB_Show_Missing to be used for show w/ no MV info
216    
217    # cctv programming day starts at 1 am
218    $start = $date . "T01:00:00";
219    $unixstart = strtotime( $start );
220    $unixend = $unixstart + 24*60*60 - 1;
221    $qs = "select nid, event_start, event_end  from event where event_start <= $unixend and event_start >= $unixstart order by event_start";
222    if ($dbg){print "\$qs is $qs<br>";}
223    $qr = db_query( $qs );
224    
225    # start date formatted for MV
226    $start_date_string = strftime( "%Y-%m-%d", $unixstart );
227    print "<h2>Schedule for $date</h2><br>";
228    print "<table border=1> <th align=center>Start Time</th>";
229    print "<th align=center>Duration</th>";
230    print "<th align=center>House #</th>";
231    print "<th align=center>Path</th>";
232    print "<th align=center>Type</th>";
233    print "<th align=center>Trigger Command</th>";
234    print "<th align=center>Start Date</th>";
235    print "\n";
236    
237    $rowcount=0; // row counter
238    while( $row = mysql_fetch_array( $qr )){
239    
240            $nid = $row[0]; $this_start = $row[1]; $this_end = $row[2];
241            if( !pegevent_is_schedulable( $nid ) ){ continue; } // jump over things not in schedule
242    
243            $gap = pegevent_get_gap( $last_end, $this_start ); # is there a gap?
244            if (0){
245                    $this_start_string = strftime( "%Y%m%dT%T",$this_start );
246                    $this_end_string = strftime( "%Y%m%dT%T", $this_end );
247                    $last_end_string = strftime( "%Y%m%dT%T",$last_end );
248                    print ">> gap is $gap<br>";
249                    print "<br><br>>> top of loop this_start is $this_start_string ($this_start) this_end is $this_end_string ($this_end) last_end is $last_end_string ($last_end)<br>";}
250    
251            print "<tr>";
252    
253            #### OPENING #### deal with gap in line
254            if (  $gap > 0 ){
255                    #if( $dbg ){ print ">> identified opening at $last_end_string ($last_end)<br> ";
256                    #       print ">> calling pegevent_show_mv_opening( $last_end, $this_start)<br>";
257                    #}
258                    pegevent_show_mv_opening( $last_end, $this_start, $start_date_string );
259    
260            }
261    
262            #### SHOWING ####
263            if ( pegevent_is_showing( $nid ) ){
264    
265                    $program_nid = pegevent_get_program_nid_from_showing( $nid );
266                    $node = node_load( $program_nid );
267    
268                    $dur_string = pegevent_get_duration( $program_nid, "string" );
269                    $jw_id = $node->field_jw_meetingid[0]["value"];
270    
271                    if (pegevent_is_live_show( $nid )){
272                            $house_number = "Insert Live Show Here";
273                            $path = "Missing";
274                            $type = "Other";
275                    } else {
276                            $path = $node->field_mv_path[0]["value"];
277                            $house_number = $node->field_mv_house_number[0]["value"];
278                            $type = $node->field_mv_type[0]["value"];
279                            if (!($path&&$house_number&&$type)){
280                                    # if all empty, show not encoded
281                                    $house_number = "BB_Show_Missing";
282                                    $path = "Missing";
283                                    $type = "Other";
284                            }
285                    }
286    
287                    if($dbg){
288                            print ">> program: $program_nid, jw: $jw_id, duration $dur_string<br>";
289                            print ">> mv fields: path: $path, house: $house_number<br><br>";
290                    }
291                    // start time
292                    $time = strftime("%T", $this_start);
293                    # seconds granularity
294                    $mv_string = $time . ":00"; # frames to zero
295                    print "<td align=center>$mv_string</td>";
296    
297                    $dur_string .= ":00"; # set frames to zero
298                    # Duration is in seconds
299                            print "<td align=center>$dur_string</td>";
300                    # House Number
301                            print "<td align=center>$house_number</td>";
302                    # Path
303                            print "<td>$path</td>";
304                    # Type
305                            print "<td>$type</td>";
306            #### DIETHRU ####
307            } else {
308                    die("Neither showing nor BB in pegevent_MV_export_playlist. Error? nid is $nid");
309            }
310    
311    
312            # first MV program is On Time, all others Chain
313            if (! $rowcount++ ){
314                    print "<td>On Time</td>";
315            } else {
316                    print "<td>Chain</td>";
317            }
318    
319            # Start Date
320            print "<td>$start_date_string</td>";
321    
322            $last_end = $this_end;
323            print "</tr>\n";
324    
325    }// end while
326    print "</table>";
327    
328    } # end pegevent_MV_export_playlist
329    
330    function pegevent_update_airtime_durations( $date ){
331    # Updates durations of showings for a week centerd on today
332    # Most accurate info is held in MV and gets migrate to dr hourly (?)
333    #$dbg=1;
334            if (!$date){ # if called w/ "" do 7 days (cron)
335                    $today = date( "Ymd" );
336                    $time = strtotime( $today );
337                    for ($i=-3;$i<4;$i++){ # run 7 days
338                            $date = strftime ( "%Y%m%d",$time + $i*24*60*60 );
339                            print "Updating airtime durations for $date<br>";
340                            pegevent_update_airtime_durations( $date );
341                    }
342                    return;
343            }
344            $start = $date . "T01:00:00";
345            $unixstart = strtotime( $start );
346            $unixend = $unixstart + 24*60*60 - 1;
347            $qs = "select nid, event_start, event_end  from event where event_start <= $unixend and event_start >= $unixstart order by event_start";
348            if ($dbg){print "dbg>> in update air dur \$qs is $qs<br>";}
349            $qr = db_query( $qs );
350            while( $row = mysql_fetch_array( $qr )){
351    
352                    $nid = $row[0]; $this_start = $row[1]; $this_end = $row[2];
353                    if( !pegevent_is_schedulable( $nid ) ){ continue; } // jump over things not in schedule
354    
355                    $dur_in_sched = $this_end - $this_start;
356                    $dur = pegevent_get_duration( $nid, "integer" );
357    
358                    if ($dur!=$dur_in_sched){ # conflict found >> do update
359                            $this_start_string = strftime( "%T",$this_start );
360                            print "Found conflict at $this_start_string.";
361                            $new_end = $this_start + $dur;
362                            $new_end_string = strftime( "%T",$new_end );
363                            print " New end time should be $new_end_string.";
364                            $qs = "update event set event_end = $new_end where nid = $nid";
365                            $rows = db_query( $qs );
366                            print " $rows rows affected in events table.<br>";
367                    }
368            }
369            print "<br>";
370    } # end function
371    
372    # CCTV has special points in day to be treated specially in some cases, ie: export to MV
373    function pegevent_is_schedule_marker( $unix_time ){
374            $h = strftime("%H",$unix_time);
375            $m = strftime("%M",$unix_time);
376    
377            if ( ( ($h=="01") or ($h=="07") or ($h=="13") or ($h=="20") )
378                            and ($m=="00")  ){
379                    return true;
380            }
381    }
382    
383    # choose MV BB based on opening length
384    function pegevent_choose_mv_bb( $end, $start ){
385    #  (end) <  opening < (start)
386            if ( ($start-$end)<=0 ){
387                    die("bad data passed to pegevent_choose_mv_bb<br>\n");
388            }
389            if ( ($start - $end) == 300 ){
390                    return( "BB-5" );
391            }
392            if ( ($start - $end) <= 180 ){
393                    return( "BB1" );
394            }
395            if( pegevent_is_schedule_marker( $start ) ){
396                    return( "BBBBBBBBBBBBBBBBBBBB" );
397            }
398            return( "BB" );
399    }
400    
401    function pegevent_show_mv_opening( $last_end, $this_start, $start_date_string  ){
402    #$dbg = 1;
403            $dur_string = pegevent_get_duration( $last_end, "string" );
404            #$house_number = "BB";
405            $house_number = pegevent_choose_mv_bb( $last_end, $this_start );
406            $path = "Missing";
407            $type = "Other";
408            if($dbg){print ">> in pegevent_show_mv_opening duration is $dur_string";}
409            // start time
410            $time = strftime("%T", $last_end);
411            # seconds granularity
412            $mv_string = $time . ":00"; # frames to zero
413            print "<td align=center>$mv_string</td>";
414    
415            $dur_string .= ":00"; # set frames to zero
416            # Duration is in seconds
417                    print "<td align=center>$dur_string</td>";
418            # House Number
419                    print "<td align=center>$house_number</td>";
420            # Path
421                    print "<td>$path</td>";
422            # Type
423                    print "<td>$type</td>";
424            # Trigger
425                    print "<td>Chain</td>";
426            # Start Date
427                    print "<td>$start_date_string</td>";
428            print "</tr>";
429    }
430    
431    function pegevent_get_num_reruns( $program_nid, $time, $time_delta ){
432    # legacy: consider removal, may come in handy in future
433    #so given args  427, 320090, - 7 * 24 * 60 * 60
434    #       function would return number of reruns from one week before 320090 to 320090
435    #       of program 427
436    #       If called with arg time = 0, get num reruns in whole system at any time
437    #$dbg=1;
438    $reruns=0;
439    $other_time = $time + $time_delta;
440    if ($other_time < $time){
441            $start = $othertime;
442            $end = $time;
443    } elseif ($time < $other_time){
444            $start = $time;
445            $end = $other_time;
446    } elseif ($time==0) {
447            $get_all_reruns = true;
448            if($dbg){print "setting get_all_reruns true<br>";}
449    } else {
450            die( "Bad time delta in pegevent_get_num_reruns" );
451    }# end pegevent_get_num_reruns
452    
453    if ($get_all_reruns){
454            $qs = " select nid from event ";
455    } else {
456            $qs = "select nid from event where event_start < $end and event_start > $start";
457    }
458            if($dbg){ print "\$qs is $qs<br>";}
459    # other method would be look for all showings of program_nid: probably faster
460    #       select nid from content_type_showing where node_ref = $program_nid
461    #       then search on fit in timeslot
462    $qr = db_query( $qs );
463    while(  $row = mysql_fetch_array( $qr )){
464            $event_nid = $row[0];
465            # look at efficiency upping by pulling other info from events
466            if ( pegevent_get_program_nid_from_showing( $event_nid ) == $program_nid ){
467                    $reruns++;
468            }
469    }
470    return $reruns;
471    } // end pegevent_get_num_reruns
472    
473    ###############################################################################################
474    
475    function pegevent_insert_into_schedule ( $focus_opening_start, $program_nid, $time, $date ){
476            # Insert program showing into schedule at given time.
477            #       Empty time defaults to focus_opening_start if it is provided.
478    #$dbg=1;
479            if($dbg){
480                    print "DBG>> insert_into_schedule ( $focus_opening_start: focus_opening_start, $program_nid: program_nid, $time: time, $date: date)<br>";
481                    #debug_print_backtrace();
482                    print "<br><br>";
483            }
484    
485            if (!($time)) {
486                    $time = $focus_opening_start; # default time for insertion
487    
488            #       Following selection from pdate list date/time pair (where time is string like 3:42)
489            #               determine the real insertion time
490            } elseif ( ($date) and ($time) ){
491                    $time = pegevent_normalize_to_unixtime( $date, $time );
492                    if($dbg){print ">> in insert_into_schedule post normalization time is $time<br>";}
493            }
494    
495            if ($time<1000000000){          # sanity
496                    die("Invalid time in pegevent_insert_into_schedule");
497            }
498            # use pegevent_already_inserted for race avoidance: more flexible and callable for reruns:
499            #  doesn't require focus_opening_start and date.
500            # When called by reruns only args passed witll be program and time: NB.
501    
502            $title = pegevent_get_title( $program_nid );
503            if ( pegevent_already_inserted( $title, $time ) ){
504                    return;  # just did it!
505            }
506            # Build showing node.
507            if($dbg){print "DBG>> in insert_into_schedule before call to build_showing: var: time is $time<br>";}
508            $showing = pegevent_build_showing( $time, $program_nid );
509            if($dbg){print "showing is<br>"; print "<pre>"; print_r( $showing ); print "</pre>";}
510    
511            # Save node: insert into schedule. Beware double insertion
512            $new_node_nid = pegevent_insert_showing( $showing );
513            if($dbg){ print "in pegevent_insert_into_schedule new_node_nid is $new_node_nid<br>";}
514    
515            return $new_node_nid;
516    }
517    
518    function pegevent_already_inserted( $title, $time ){
519    # Is this program already inserted at the requested time?
520    # Useful for race avoidance
521    #$dbg=1;
522    
523            $qs = " select nid from event where event_start = $time ";
524            if($dbg){
525                    print "dbg>> qs is $qs<br>";
526            }
527            $qr = db_query( $qs );
528            # Go through
529            while ( $row = mysql_fetch_row( $qr ) ){
530                    $nid = $row[0];
531                    if($dbg){print "found showing $nid<br>";}
532                    if ( pegevent_is_showing( $nid ) ){
533                            if($dbg){print "realized this is a showing<br>";}
534                            $showing_nid = $nid;
535                            $program_nid = pegevent_get_program_nid_from_showing( $showing_nid );
536                            $program_title = pegevent_get_title( $program_nid );
537                            if ( $title == $program_title ){
538                                    if($dbg){print "in inner loop<br>";}
539                                    return true;
540                            }
541                    } # if
542            } # while
543            # default return false
544    }
545    
546    function pegevent_showing_not_in_schedule( $showing ){
547            # Accepts shoing node as its arg NOT nid. Called by insert_into_schedule.
548            # Is this showing in the schedule: title and event start check
549            $start = $showing->event_start;
550            $title = $showing->title;
551            print "DBG>> not_in_schedule start: $start title: $title<br>";
552            return true;
553    }
554    
555    function pegevent_show_opening( $last_end, $next_start, $focus_opening_start, $date, $time ){
556    
557    #       $dbg=1;
558            if($dbg){
559                    print "<br>DBG>> show_opening( last_end, next_start, focus_opening_start, date, time )<br>";
560                    print "DBG>> show_opening( $last_end, $next_start, $focus_opening_start, $date, $time )<br>";
561                    #debug_print_backtrace();
562    
563            }
564            # This works for ONE OPENING ONLY: limited scope.
565            # Insert 5 <td>s to go into scheduler page display.
566            #       start, duration, title, pdate, action (only inserts)
567            # This is all this function needs to do!
568            # Needs to pass time along if it's got it.
569    
570            $duration = $next_start - $last_end;
571    
572            ### color the row
573            print "<tr ";
574            if ( $last_end==$focus_opening_start ){
575                    print " bgcolor=\"#33EE33\""; # this is the focus opening: bright green
576            } else {
577                    if ( $duration <= 900 ){
578                            # short openings green
579                            print " bgcolor=\"#ccffcc\"";
580                    } else {
581                            # long openings yellow
582                            print " bgcolor=\"#ffdd33\"";
583                    }
584            } # end if last_end
585            print ">\n";
586    
587            ### print start
588    
589            $opening_start_string = strftime( "%H:%M:%S",$last_end );
590            print "<td align=center>$opening_start_string</td>\n";
591    
592            ### print duration
593    
594            $duration_string = pegevent_sec2hms( $duration );
595            print "<td align=center>$duration_string</td>\n";
596    
597            ### print title
598    
599            print "<td align=center>";
600            if ( $last_end==$focus_opening_start ){
601                    print "Inserting Here";
602            } else {
603                    print "-Opening-";
604            } # end if last end
605            print "</td>\n";
606    
607            ### print pdate
608    
609            print "<td align=center>--</td>\n";
610    
611            ### ACTION ############################
612            ### print action link for inserts
613            # need times for display and selection
614            # only time to be selected at this step
615    
616            $next_min = pegevent_get_next_whole_minute( 0, "integer", $last_end );
617            $next_min_string = pegevent_get_next_whole_minute( 0, "string", $last_end );
618            $next_next_min = pegevent_get_next_whole_minute( 1, "integer", $last_end );
619            $next_next_min_string = pegevent_get_next_whole_minute( 1, "string", $last_end );
620            # get 5 min slots later if needed
621    
622            # allow for insertion
623            print "\n<td align=center>";
624            $focus_opening_start = $last_end; #if you click the link you will be focusing on this opening
625            #$print_string = pegevent_print_what_we_got("Insert Here",  $focus_opening_start, $time, $production_date, $program_nid );
626            #print "$print_string";
627    
628            # brought back from forked dev: TL requests pr date on front page
629            print "\n<form name=\"input\" action=\"\" >\n"; // defaults to use this page
630            print "Pr. Date ";
631            print "<input type=\"text\" size=10 name=\"production_date\" value=\"$date\">\n";
632            print "<input type=\"hidden\" name=\"focus_opening_start\" value=\"$focus_opening_start\">\n";
633            print "<input type=\"hidden\" name=\"date\" value=\"$date\">\n";
634            print "</form>";
635            print "</td>";
636    
637            #replay at time column
638            print "<td></td>";
639    } # end show_opening
640    
641    function pegevent_print_what_we_got( $string, $focus_opening_start, $time, $production_date, $program_nid ){
642    # prints a link around $string w/ all values available
643    
644            $return =  "<a href=\"?focus_opening_start=$focus_opening_start&time=$time&production_date=$production_date&program_nid=$program_nid\">$string</a>";
645    
646            return $return;
647    }
648    
649    function pegevent_get_next_whole_minute( $offset, $return_type, $time ){
650    
651            # Get next whole minute after $time_string.
652            # Offset is 0 for next whole, 1 for next next, etc.
653            # Return type is string or integer.
654            #       string will return "13:05:21", etc. for use in scheduler insertion links
655            #       integer will return unixtime
656            # Time is in unixtime format.
657    
658    
659            # get last two characters from time string
660            $sec_string = strftime( "%S", $time );
661            $sec_val = $sec_string;
662            if ( $sec_val == 0 ){
663                    $time += 60; # next minute
664            }else{
665                    $time += 60-$sec_val; # add diff from 60
666            }
667            $time += 60 * $offset; # offset in seconds
668    
669            if ( $return_type == "string" ){
670                    $time_string = strftime( "%H:%M:%S", $time );
671                    return $time_string;
672            } elseif ( $return_type == "integer" ){
673                    return $time;
674            }
675            die("Bad return type passed to pegevent_get_next_whole_minute: $return_type");
676    
677    } # end get_next_whole_minute
678    
679    function pegevent_get_gap ( $last_end, $this_start ) {
680    
681            # Is there a gap between "contiguous" schedulable events?
682            # Negative gap ..>>.. overlap: higher levels will warn
683            # Positive gap ..>>.. opening
684            # Zero gap     ..>>.. dead lock synchronized: this starts when that ended
685    
686            if ( ! ($last_end) ) {
687                    return 0; # no gap: synchronized for first program in listing
688            } else {
689                    return $this_start - $last_end;
690            }
691    } # Ah, the beauty and simplicity
692    
693    ################## This is the main function for scheduler actions.    ################
694    ################## It displays the schedule and accesses most actions. ################
695    
696    function pegevent_update_durations( $date ){
697            print "Feature currently under development :-)<br>";
698    
699            # loop through the day
700            #
701            $start = $date . "T01:00:00";
702            $unixstart = strtotime( $start ); $unixend = $unixstart + 24*60*60 - 1;
703            $qs = "select nid, event_start, event_end from event where event_start <= $unixend and event_start >= $unixstart order by event_start";
704            $qr = db_query( $qs );
705            while( $row = mysql_fetch_array( $qr )){
706    
707                    $nid = $row[0];
708                    $start = $row[1];
709                    $end = $row[2];
710    
711                    # get duration for each show
712                    $duration = pegevent_get_duration( $nid, "integer" );
713    
714                    # update in events table
715                    #       no worries if conflict occurs: it will be flagged
716                    # can this be an assignment to $node->event_end?
717                    $new_end = $start + $duration;
718                    if ($new_end!=$end){
719                            $qs = "update event set event_end = $new_end";
720                            # zz check syntax
721                            db_query( $qs );
722                            # check return vals
723                    }
724            } # end while
725    
726            # give message re "all updated"
727            print "<h3>Durations have been updated.</h3>";
728    }
729    
730    function pegevent_normalize_to_unixtime( $date, $time ){
731    # Sometimes string will be passed in time: needs to get converted to unixtime
732    #$dbg=1;
733            if($dbg){
734                    print ">>in pegevent_normalize_to_unixtime( date:$date, time:$time )<br>";
735            }
736            if( is_int( $time ) ){
737                    # already unix time (or perhaps raw day, eg. 20070606?)
738                    return $time;
739            }
740            $time_string = $date ."T". $time; # 20070707 12:34
741            if($dbg){ print ">> pre return time_string is $time_string<br>";}
742            $unixtime = strtotime( $time_string );
743            if($dbg){ print ">> pre return unixtime is $unixtime<br>";}
744            return $unixtime;
745    }
746    
747    function pegevent_prepend_ytt( $i, $str ){
748    # puts Yesterday, Today, Tomorrow on front of date string based on offset
749            if ($i==0){
750                    $str = "Today: ".$str;
751            } elseif ($i==-1){
752                    $str = "Yesterday: ".$str;
753            } elseif ($i==1){
754                    $str = "Tomorrow: ".$str;
755            }
756            return( $str );
757    }
758    
759    function pegevent_public_schedule( ){
760            $i = $_GET["offset"];
761            $offset = $i;
762    
763            $date = date("Y-m-d"); // set default
764            if (!($i)){
765                $show_date = $date;
766                print "<h1>Schedule for Today</h1>";
767            } else {
768                    $day = mktime(0, 0, 0, date("m"), date("d")+$i, date("y"));
769                    $str=date("l F j", $day);
770                    $show_date = $str; # passed to schedule display below
771                    $str = pegevent_prepend_ytt( $i, $str );
772                    print "<h1>Schedule for $str</h1>";
773            }
774    
775            # all the same, except today, tomorrow, yesterday prepended to those dates
776            print "<form>";
777            print "<select name=\"offset\" size=\"1\">\n";
778            for ($i=-2; $i<11; $i++){
779                    $day = mktime(0, 0, 0, date("m"), date("d")+$i, date("y"));
780                    $str=date("l F j", $day);
781                    $str = pegevent_prepend_ytt( $i, $str );
782                    $selected = "";
783                    if( $i == $offset ){
784                            $selected = " selected ";
785                    }
786                    print "<option $selected value=\"$i\">$str</option>\n";
787            }
788            print "<input type=\"submit\" value=\"Show Schedule\">\n";
789            print "</select></form>\n";
790            print "<br>";
791    
792            print "<a href=\"?offset=-1\">Yesterday</a> | ";
793            print "<a href=\"?offset=0\">Today</a> | ";
794            print "<a href=\"?offset=+1\">Tomorrow</a> <br>";
795    
796            $start = $show_date . "T01:00:00";
797            $unixstart = strtotime( $start );
798            $unixend = $unixstart + 24*60*60 - 1;
799    
800            print '<table cellpadding="4" cellspacing="3" border="1"> <th align=center>Start Time</th>';
801            print "<th align=\"center\">Title</th>";
802            print "<th align=center>Production Date</th>";
803            print "<th align=right>Length</th>";
804    
805            $qs = "select nid, event_start, event_end from event where event_start <= $unixend and event_start >= $unixstart order by event_start";
806            if($dbg){print "qs is $qs\n";}
807            $qr = db_query( $qs );
808    
809            while( $row = mysql_fetch_array( $qr )){
810                    print "<tr>";
811                    $nid = $row[0]; $this_start = $row[1]; $this_end = $row[2]; # assign the data
812                    # Start Time: modfiy to be more human friendly
813                    $start = strftime("%r", $this_start );
814                    // ignores seconds so 12:23:45 -> 12:23
815                    $start = substr($start,0,5)." ".substr($start,9);
816                    if (substr($start, 0, 1)=="0"){ $start=substr($start,1);}
817                    print "<td align=center><strong>$start</strong></td>";
818                    # Title w/ Link
819    
820                    $program_nid = $nid;
821                    if (pegevent_is_showing( $nid )){
822                            $program_nid = pegevent_get_program_nid_from_showing( $nid );
823                    }
824                    $title = pegevent_get_title( $program_nid );
825                    $series = pegevent_get_series( $program_nid );
826                    if ($series){
827                      $title = $series.": ".$title;
828                    }
829                    # Altered to remove public program links during pre-launch
830                    #print "<td align=center><a href=\"/node/$program_nid\"><strong>$title</strong></a></td>";
831                    print "<td align=center><strong>$title</strong></td>";
832                    $pdate = pegevent_get_pdate( $program_nid );
833                    $time = strtotime( $pdate );
834                    $str = strftime( "%b %e, %Y", $time );
835                    print "<td align=center><strong>$str</strong></td>";
836                    # Duration
837                    $duration = $this_end - $this_start;
838                    $duration_string = pegevent_sec2hms ( $duration );
839                    $length = substr($duration_string,1,4);
840                    $min = substr( $duration_string, 3,2 );
841                    $hour = substr( $duration_string, 1,1 );
842                    #print "<td align=center><strong>$length $hour:$min</strong></td>";
843                    print "<td align=right><strong>";
844                    if ($hour){
845                            print "$hour hr ";
846                    }
847                    if ( substr( $min,0,1 ) == "0" ){
848                            $min = substr( $min,1,1 );
849                    }
850                    print "$min min";
851                    print "</strong></td>";
852                    print "</tr>";
853            } # while
854            print "</table>";
855    }
856    
857    
858    #################################################################################################
859    
860    function pegevent_scheduler_page(){
861    
862    #  Print page for cctv schedulers with actions
863    #  date is date for air schedule, del_nid is showing nid to be deleted,
864    #  focus_opening_start is opening being focused on
865    #  time is used for insertions: it is in unix time format
866    #  production date is for selection of programs for insertion
867    #  focus_nid is for side block focus
868    #  reruns args are used for single and block reruns
869    #  update_durations is a flag used for updating all durations in *events table*
870    #    based on durations returned by get_duration (which may be more up to date than stored vals)
871    #$dbg=1;
872    if($dbg){print "DBG>> call to scheduler page with vars ( $date, $del_nid, $focus_opening_start, $program_nid,
873            $time, $production_date, $focus_nid, $num_reruns, $num_hours_to_rerun, $update_durations, $sync_to_mv )<br>";}
874    
875    ########## get input from GET variables #########
876    $show_date=$_GET["date"];
877    $date = date("Y-m-d"); // set default
878    if (!($show_date)){
879        print "no date, defaulting to today<br>";
880    } elseif( $time = strtotime ( $show_date ) ){
881        $date = strftime("%Y-%m-%d",$time);
882    } else {
883        print "bad date, defaulting to today<br>";
884    }
885    $del_nid=$_GET["del_nid"];
886    $focus_opening_start = $_GET["focus_opening_start"];
887    $program_nid = $_GET["program_nid"];
888    $production_date = $_GET["production_date"];
889    $time = $_GET["time"];
890    $focus_nid = $_GET["focus_nid"];
891    $num_reruns = $_GET["num_reruns"];
892    $num_hours_to_rerun = $_GET["num_hours_to_rerun"];
893    $update_durations = $_GET["update_durations"];
894    $sync_to_mv = $_GET["sync_to_mv"];
895    ########## end get input from GET variables #########
896    
897    static $just_inserted=false; // stop double insertion
898    
899    # Sometimes string will be passed in time: needs to get converted to unixtime
900    # When time string is inserted into pdate selection, this is the case:
901    #  That time is associated with the high level var date, not today's date as strtotime would normally do.
902    
903    if ( ($time) and ($production_date) and ($date) ){
904            # note time is 12:24 etc. in here
905            $time = pegevent_normalize_to_unixtime( $date, $time );
906    }
907    ###################################
908    ################# ACTIONS #########
909    ###################################
910    
911    if ($sync_to_mv){
912    # request to synchronize data from MV into DR
913    # perl call
914            print "Syncing to MV.<br>";
915            print "This function brings data from MV into Drupal.<br>";
916            print "It is disabled in this general release as the synchronization with MV is detailed.<br>";
917    # print message re action
918    } elseif ($del_nid) {   // if passed nid to delete do it
919    
920            if (pegevent_is_showing( $del_nid )){
921                    pegevent_delete_showing( $del_nid );
922                    $just_inserted=false;
923            } elseif (pegevent_is_program( $del_nid )){
924                    #delete program and showings
925                    pegevent_delete_showings_of_program( $del_nid );
926                    pegevent_node_del( $del_nid );
927                    $just_inserted=false;
928            } else {
929                    die( "Neither program nor showing passed for deletion: error or already deleted nid $del_nid<br>" );
930            }
931    
932    } elseif ($update_durations) { # update durations in day's playlist
933            pegevent_update_airtime_durations( $date );
934    
935    } elseif ($focus_opening_start) {
936            if($dbg){print "DBG>> sched_page: opening was passed in<br>";}
937            # If passed opening, insert program or get pdate for selection listing.
938            # Need opening to descend into insertion.
939    
940            # we've got opening AND ...
941            if ( ($program_nid) and (!($just_inserted)) ){ # maybe add time too
942    
943                    # insert program showing into opening BEWARE double insertion
944    
945                    # time morph here: if day is sat and insert time is 00:23, this is actually sunday
946                    $insertion_date = $date;
947                    if (!($time)){          # time defaults to start of opening
948                            $time = $focus_opening_start;
949                    } elseif ( preg_match( '/^00.*/',$time ) ){
950                            # up date by one
951                            $day_after = strtotime( $date ) + 24*60*60;
952                            $insertion_date = strftime("%Y%m%d", $day_after );
953                            print " insertion_date is $insertion_date.<br>";
954                    }
955                    if($dbg){print "dbg>> pegevent_insert_into_schedule( $focus_opening_start, $program_nid, $time, $insertion_date );<br>";}
956                    pegevent_insert_into_schedule( $focus_opening_start, $program_nid, $time, $insertion_date );
957                    # post insertion need to blank out vars so action state gets refreshed
958                    $focus_opening_start = $program_nid = $time = "";       # keep date, erase all others
959                    $just_inserted=true;
960    
961            } elseif ( $production_date ) {
962    
963                    # have opening and production date: print a pdate listing for insertion
964                    pegevent_scheduler_show_pdate_selection( $date, $focus_opening_start, $production_date, $time );
965                    $showing_pdate_selection=1;
966                    $just_inserted=false;
967    
968            } else {        # first click gets us to here
969                    // have only opening NO pdate, NO program_nid
970    
971                    // display selection box for production date
972                    if($dbg){print "DBG>> sched_page calling sched_get_pdate<br>";}
973                    pegevent_scheduler_get_pdate( $date, $time, $focus_opening_start );
974                    $getting_pdate=1;
975                    $just_inserted=false;
976    
977            } # end if program nid
978    
979    } elseif($num_reruns){
980    
981            # Schedule a block of reruns or a single rerun.
982            # Date should remain same for scheduler view: needs to be passed along.
983            # Program nid is prog to rerun.
984            # Num_hours_to_rerun is how many hours to program reruns for: 1=<x<=6.
985            # If passed time, time is time for insertion of rerun.
986    
987            if($dbg){
988                    print "calling pegevent_schedule_reruns( $date, $num_hours_to_rerun, $num_reruns)";}
989    
990            if ( ($num_reruns==1) and (!($num_hours_to_rerun)) ) {
991                    # Do one rerun as per TL's request.
992                    # One rerun and time signals: one rerun.
993                    #       In this case use num_reruns to be program_nid.
994    
995                    #$program_nid = $num_reruns;
996                    if (!($time)){
997                            die( "use selector in scheduler for single reruns<br>");
998                    }
999                    $time = strtotime( $time ); # convert time string to UNIX time : Beware zz
1000                    if($dbg){print ">> sched_page calling insert_into_schedule( 0, program_nid:$program_nid, $time:time, $date:date )";}
1001                    pegevent_insert_into_schedule( 0, $program_nid, $time, $date );
1002            } else {
1003                    print "This function based on the air schedule and block system at ";
1004                    print "<a href=\"http://cctv.org\">CCTV</a> and may not be useful to the general public.<br>";
1005                    print "Rerun blocks are built on today's schedule and put into tomorrow's.<br>";
1006                    pegevent_schedule_reruns( $date, $num_hours_to_rerun, $num_reruns);
1007            }
1008            $just_inserted=true;
1009    }
1010    
1011    ##################################
1012    ############## DISPLAY  ##########
1013    ##################################
1014    
1015    $start = $date . "T01:00:00";
1016    $unixstart = strtotime( $start );
1017    $unixend = $unixstart + 24*60*60 - 1;
1018    
1019    $qs = "select nid, event_start, event_end from event where event_start <= $unixend and event_start >= $unixstart order by event_start";
1020    if($dbg){print "qs is $qs\n";}
1021    $qr = db_query( $qs );
1022    
1023    #unixstart is start of day
1024    $day_before = $unixstart - 24*60*60;
1025    $day_after = $unixstart + 24*60*60;
1026    ###### Top of Page ##################
1027    pegevent_scheduler_get_date( $date ); // show scheduler date selector at top of page
1028    $day = strftime ( "%A", $unixstart );
1029    $day_before_ymd = strftime( "%Y%m%d", $day_before );
1030    $this_node = $node->nid;
1031    print "<strong><a href=\"?date=$day_before_ymd\">Schedule Day Before</a></strong> | ";
1032    $day_after_ymd = strftime( "%Y%m%d", $day_after );
1033    print " <strong><a href=\"?date=$day_after_ymd\">Schedule Day After</a></strong><br>";
1034    
1035    print "<h2>Schedule for $day, $date</h2>";
1036    
1037    print "<table border=1> <th align=center>Start Time</th>";
1038    print "<th align=center>Duration</th>";
1039    print "<th align=center>Title</th>";
1040    print "<th align=center>Production Date</th>";
1041    print "<th align=center>Remove or Insert</th>";
1042    print "<th align=center>Replay this show at</th>";
1043    print "\n";
1044    
1045    # gap recognition starts at 1am ... may run until 1am next day: empty return on query
1046    # get end of last show from previous day: event_end for event where event start < unixstart
1047    #                                     1am
1048    #                       start------------------end
1049    $last_end = $unixstart;
1050    $qs3 = "select nid, event_start, event_end from event where event_start < $unixstart order by event_start desc limit 1";
1051    if($dbg){print "DBG>> sched_pg: qs3 is $qs3<br>";}
1052    $qr3 = db_query( $qs3 );
1053    if ( $row = mysql_fetch_row( $qr3 ) ){
1054            $last_end = $row[2];
1055    }
1056    
1057    while( $row = mysql_fetch_array( $qr )){
1058    
1059            $nid = $row[0]; $this_start = $row[1]; $this_end = $row[2]; # assign the data
1060            if($dbg){print "DBG>> sched_pg: nid $nid this_start $this_start this_end $this_end<br>";}
1061            # if debugging note: program found first, then gap (opening) is calculated:
1062            #  data may appear in strange order due to this
1063    
1064            # Jump over things not in schedule: other events, etc.
1065            if( !pegevent_is_schedulable( $nid ) ){ continue; }
1066    
1067            $gap = pegevent_get_gap( $last_end, $this_start );
1068    
1069            # Opening?
1070            if ( $gap > 0 ) {
1071                    # Opening is totally handled in this call: no need to worry about openings in this func hereafter
1072                    pegevent_show_opening( $last_end, $this_start, $focus_opening_start, $date, $time );
1073            }
1074            # Color Row
1075            print "<tr";
1076            # Conflict?
1077            if ( $gap < 0 ) {
1078                    # conflict: notify politely
1079                    # this is all that is needed: humans will deal with it!
1080                    print " bgcolor=\"#ff4040\""; # red: warning Will Rogers!
1081            } else {
1082                    #print " bgcolor=\"#aaaa88\"";
1083                    #print " bgcolor=\"#ffeedd\"";
1084                    print " bgcolor=\"#ffeee8\"";
1085            }
1086            print ">";
1087    
1088            # Dead lock follow-on: gap = 0.
1089    
1090            # Start Time
1091            $start_time_string = strftime("%T", $this_start );
1092            print "<td align=center>$start_time_string</td>";
1093    
1094            # Duration
1095            $duration = $this_end - $this_start;
1096            $duration_string = pegevent_sec2hms ( $duration );
1097            print "<td align=center>$duration_string</td>";
1098    
1099            # Title w/ Link
1100            $title = pegevent_get_title( $nid );
1101            $program_nid = $nid;
1102            if (pegevent_is_showing( $nid )){
1103                    $program_nid = pegevent_get_program_nid_from_showing( $nid );
1104            }
1105            $print_title = $title;
1106            $series_name = pegevent_get_series( $program_nid );
1107            if ($series){
1108                   $print_title = $series.": ".$title;
1109            }
1110            #print "<td align=center><a href=\"/node/$program_nid\"><strong>$title</strong></a></td>";
1111            print "<td align=center><a href=\"$program_nid\"><strong>$print_title</strong></a></td>";
1112    
1113            # Production Date
1114            $pdate = pegevent_get_production_date( $program_nid );
1115            print "<td align=center>$pdate</td>";
1116    
1117            // Action
1118            print "<td align=center>";
1119            if( pegevent_is_showing( $nid ) ) {
1120                    print "<a href=\"?date=$date&del_nid=$nid\">remove</a></td>";
1121    
1122                    # rerun selector
1123                    print "<td align=center>";
1124                    pegevent_print_rerun_time_selector( $date, $program_nid );
1125                    # Showing is a function of time and program.
1126                    # if ($focus_nid){ print "&focus_nid=$focus_nid"; }
1127                    print "</td>";
1128            } else {
1129                    print "Error? Not a showing. Whazzup? </td>";
1130            }
1131            # bottom of loop preparing for next runthrough
1132            $last_end = $this_end;
1133    }// end while
1134    # end of loop through events: may still be a gap between last_end and 1am next day: need to display
1135    if ($last_end<$unixend){
1136            pegevent_show_opening( $last_end, $unixend, $focus_opening_start, $date, $time );
1137    }
1138    
1139    print "</table><br><br>";
1140    
1141    ######################### BOTTOM of scheduler page
1142    # unixstart is start of day
1143    
1144    print "<a href=\"?date=$date&update_durations=$date\">Recalculate Program Durations in Air Schedule for this day.</a><br>";
1145    #pegevent_show_rerun_selector($date);
1146    #print "<a href=\"?date=$date&sync_to_mv=1\">Synchronize MV info into Drupal.</a>";
1147    
1148    } // end pegevent_scheduler_page function
1149    
1150    ###########################################################################################
1151    
1152    function pegevent_print_rerun_time_selector( $date, $program_nid ){
1153            # Print out selector form. Have date and program_nid, need time. Assume tomorrow.
1154            # For use in scheduler_page display for generating form.
1155    
1156            print "\n<form name=\"input\" action=\"\" >\n"; // defaults to use current page
1157            #print "Time for rerun:";
1158            print "<input type=\"text\" size=18 name=\"time\" value=\"$date 12:00:00\">\n";
1159            print "<input type=\"hidden\" name=\"date\" value=\"$date\">\n";
1160            print "<input type=\"hidden\" name=\"program_nid\" value=\"$program_nid\">\n";
1161            print "<input type=\"hidden\" name=\"num_reruns\" value=\"1\">\n";
1162            print "</form>";
1163    
1164            # Form will pass along date, program_nid and time.
1165    }
1166    
1167    function pegevent_delete_showing( $nid ){ // delete showing with nid passed
1168            pegevent_node_del( $nid );
1169            # may want to return something here
1170    }
1171    
1172    function pegevent_program_exists( $title, $pdate ){
1173    $dbg=1;
1174    # Return "this function already exists"
1175    # pdate in flex format to be reformatted herein
1176    # pdate format internal to dr is "2006-08-28T00:00:00"
1177            print "dbg>> passed $title, $pdate<br>";
1178            $pdate_unix_time = strftime( $pdate );
1179            $pdate_dr_format = strtotime( "%Y-%m-%dT00:00:00", $pdate_unix_time );
1180            print "dbg>> pdate_dr_format is $pdate_dr_format<br>";
1181            # beware single quote in title
1182            $qs = "select nid from node, content_type_program where node.title = '$title' and content_type_program.field_pdate_value = '$pdate_dr_format'";
1183            print "dbg>> qs is $qs<br>";
1184            $qr = db_query( $qs );
1185            while( $row = mysql_fetch_row( $qr ) ){
1186                    return true;
1187            }
1188    } # function
1189    
1190    function pegevent_showing_exists( $s ){
1191            # title and start and end time match: same showing
1192            $start = $s->event_start;
1193            $end = $s->event_end;
1194            $title = $s->title;
1195            $qs = "select nid from event where event_start = $start and event_end = $end";
1196            $qr = db_query( $qs );
1197            while( $row = mysql_fetch_row( $qr ) ){
1198                    $nid = $row[0];
1199                    $node = node_load( $nid );
1200                    if ($title == $node->title){
1201                            return true;
1202                    }
1203            }
1204    }
1205    
1206    function pegevent_insert_showing( $b ){
1207    #$dbg=1;
1208    # insert the showing into the db # it's so easy
1209            # don't double insert
1210            if (pegevent_showing_exists( $b )){
1211                    if($dbg){print "dbg>> showing exists, returning.<br>";}
1212                    return;
1213            }
1214            if($dbg){print "dbg>> showing does not exist, saving.<br>";}
1215            node_save( $b );
1216            return( pegevent_max_nid() );
1217    }
1218    
1219    function pegevent_show_rerun_selector( $date ){
1220    # Need to pass three args along.
1221    # pegevent_schedule_reruns( $date, $num_hours_to_rerun, $num_reruns) is the call in scheduler_page
1222    
1223            if (! ($date) ){
1224                    $date = date("Y-m-d"); // set default
1225            }
1226            print "\n<form name=\"input\" action=\"\" >\n"; // defaults to use current page
1227            print "<strong>Block Reruns</strong>: ";
1228            print "Number of hours: ";
1229            print "<input type=\"text\" size=1 name=\"num_hours_to_rerun\"> \n";
1230            print "Number of Reruns: ";
1231            print "<input type=\"text\" size=1 name=\"num_reruns\"> \n";
1232            print "<input type=\"hidden\" name=\"date\" value=\"$date\">\n";
1233            print "<input type=\"submit\" value=\"Schedule 'em\">\n";
1234            print "</form>\n";
1235    
1236    } //end pegevent_show_rerun_selector
1237    
1238    function pegevent_schedule_reruns( $date, $num_hours_to_rerun, $num_reruns ){
1239    // schedule reruns on a certain date
1240    // date is date of program: rerun will run on date + 1
1241    // time is time of original: original block runs at 8pm
1242    #$dbg=1;
1243            if ($num_hours_to_rerun>6){
1244                    print "Number of Hours to Rerun must be Six or Less<br>";
1245                    return; }
1246    
1247            $offset = $num_hours_to_rerun*60*60;
1248            $start_time = strtotime($date . "T20:00"); // start rerun block reading at 8pm on date
1249            $end_time = $start_time + $offset;
1250    
1251            $read_start = strftime( "%Y%m%dT%T", $start_time );
1252            $read_end =   strftime( "%Y%m%dT%T", $end_time );
1253    
1254            if($dbg){
1255            $check_read_start = strtotime ($read_start);
1256            print "check_read_start is $check_read_start<br>";
1257            print "Rerunning programs starting at $read_start ";
1258            print "ending at $read_end<br>";}
1259            // time block for reading is determined
1260    
1261            $qs = "select nid from event where event_start>=$start_time and event_end<=$end_time";
1262            if($dbg){print "qs is $qs<br>";}
1263            $qr = db_query( $qs );
1264    
1265            while( $row = mysql_fetch_array( $qr )){
1266                    print "found nid $row[0]<br>";
1267                    // event found, may be show, may be other event
1268                    // if showing, attempt to rerun num_reruns times
1269                    $event_nid = $row[0];
1270    
1271                    if (pegevent_is_showing($event_nid)) {
1272                            if ($dbg){ print "Node $event_nid is a showing<br>";}
1273    
1274                            pegevent_schedule_rerun( $date, $event_nid, $num_reruns );
1275                    } // if (pegevent_is_showing(
1276    
1277            } // while row fetch
1278    } // end pegevent_schedule_reruns
1279    
1280    function pegevent_schedule_rerun( $date, $showing_nid, $num_reruns ){
1281    // singular
1282    // date is date of program: rerun will run on date + 1
1283    #$dbg=1;
1284            // check on date & showing nid compatibility
1285            $offset_hours_stack = array(17,11,5); // offsets from 8 pm previous day
1286            $read_block_start_time = strtotime($date . "T20:00");
1287            if($dbg){print "read_block_start_time is $read_block_start_time<br>";}
1288    
1289            while( $num_reruns-- > 0 ){
1290            // get offset from 8pm for start of showing
1291                    $offset_hours = array_pop( $offset_hours_stack );
1292    
1293                    $start_time = pegevent_get_start_time( $showing_nid );
1294    
1295                    $read_offset = $start_time - $read_block_start_time;
1296                    $air_time = $read_block_start_time + $offset_hours*60*60 + $read_offset;
1297    
1298                    if($dbg){
1299                            print "read_offset is $read_offset<br>";
1300                            $air_time_string = strftime("%Y%m%d %T",$air_time);
1301                            print "air time for rerun is $air_time: $air_time_string<br>";
1302                    }
1303                    $program_nid = pegevent_get_program_nid_from_showing( $showing_nid );
1304    
1305                    if ($dbg){
1306                            print "dbg>> calling pegevent_insert_into_schedule( 0, air_time:$air_time, program_nid:$program_nid, 0 )";
1307                    }
1308                    pegevent_insert_into_schedule ( 0, $program_nid, $air_time, 0 );
1309    
1310            } // while num_reruns
1311    } // end pegevent_schedule_rerun
1312    
1313    function pegevent_build_showing( $time, $program_nid ){
1314    // build a showing of program_nid at time
1315    #$dbg=1;
1316            global $user;
1317    
1318            if($dbg){
1319                    print "DBG>> in pegevent_build_showing: build_showing( $time, $program_nid )<br>";
1320                    #$calling_func = $backtrace[1]['function'];
1321                    #print "DBG>> calling func is $calling_func<br>";
1322                    #debug_print_backtrace();
1323            }
1324            $start_unix = $time;
1325            // get title for showing
1326            $title = pegevent_get_title( $program_nid );
1327    
1328            if($dbg){
1329            $full_program_start_time=strftime("%Y-%m-%d %H:%M",$start_unix);
1330            print "Program will be inserted in schedule at <big>$full_program_start_time</big>.<br>";
1331            print "Program Title is: <big>$title</big><br>";
1332            }
1333            $duration = pegevent_get_duration( $program_nid, "integer" );
1334            if($dbg){
1335            print "Program duration is:<big> $duration</big><br>\n";
1336            }
1337            if ($dbg){print "duration in seconds is $duration<br>";}
1338            $end_unix = $duration + $start_unix;
1339            # if duration blank or bad, bail gracefully here: die or print?
1340            if ( $duration < 0 ){
1341                    die("Can't insert showing with length zero or NULL: try again with valid length");