This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpbs_tv_schedule.module
1069 lines (962 loc) · 28.6 KB
/
pbs_tv_schedule.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Core functions and hooks for the PBS TV Schedule module.
*/
const PBS_TV_SCHEDULE_SHOW_ID_NONE = 'noshowid';
/**
* Implements hook_menu().
*
* @see pbs_tv_schedule_date_load()
*/
function pbs_tv_schedule_menu() {
$items = array();
$items['pbs-tv-schedule/schedule'] = array(
'title' => 'Schedule',
'description' => 'Date-based schedule page.',
'page callback' => 'drupal_get_form',
'page arguments' => array('pbs_tv_schedule_schedule'),
'access arguments' => array('access content'),
'type' => MENU_SUGGESTED_ITEM,
);
$items['pbs-tv-schedule/schedule/week'] = array(
'title' => 'Week-based schedule page,',
'page callback' => 'pbs_tv_schedule_schedule_week',
'access arguments' => array('access content'),
'type' => MENU_SUGGESTED_ITEM,
);
$items['pbs-tv-schedule/upcoming/nojs'] = array(
'page callback' => 'pbs_tv_schedule_upcoming',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['pbs-tv-schedule/upcoming/ajax'] = array(
'delivery callback' => 'ajax_deliver',
) + $items['pbs-tv-schedule/upcoming/nojs'];
$items['admin/config/system/pbs-tv-schedule'] = array(
'title' => 'PBS TV Schedule',
'description' => 'PBS TV Schedule settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('pbs_tv_schedule_settings'),
'access arguments' => array('administer pbs tv schedule'),
'file' => 'pbs_tv_schedule.admin.inc',
);
$items['pbs-tv-schedule/autocomplete/program'] = array(
'title' => 'Autocomplete for programs',
'page callback' => '_pbs_tv_schedule_autocomplete_program',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_help().
*/
function pbs_tv_schedule_help($path, $arg) {
switch ($path) {
case 'admin/help#pbs_tv_schedule':
$output = file_get_contents(
drupal_get_path('module', 'pbs_tv_schedule') . '/README.md'
);
if (module_exists('markdown')) {
$output = filter_xss_admin(module_invoke(
'markdown',
'filter',
'process',
0,
-1,
$output
));
}
else {
$output = '<pre>' . check_plain($output) . '</pre>';
}
return $output;
}
}
/**
* Implements hook_permission().
*/
function pbs_tv_schedule_permission() {
return array(
'administer pbs tv schedule' => array(
'title' => t('Administer PBS TV Schedule settings.'),
),
);
}
/**
* Implements hook_theme().
*/
function pbs_tv_schedule_theme() {
$module_path = drupal_get_path('module', 'pbs_tv_schedule');
$path = $module_path . '/theme';
return array(
'pbs_tv_schedule_schedule' => array(
'render element' => 'form',
'template' => 'pbs-tv-schedule-schedule',
'file' => 'theme.inc',
'path' => $path,
),
'pbs_tv_schedule_schedule_week_filter' => array(
'render element' => 'form',
'template' => 'pbs-tv-schedule-schedule-week-filter',
'file' => 'theme.inc',
'path' => $path,
),
'pbs_tv_schedule_schedule_date' => array(
'template' => 'pbs-tv-schedule-schedule-date',
'variables' => array(
'date' => '',
'items' => array(),
),
'file' => 'theme.inc',
'path' => $path,
),
'pbs_tv_schedule_schedule_search' => array(
'template' => 'pbs-tv-schedule-schedule-search',
'variables' => array(
'program' => '',
'items' => array(),
),
'file' => 'theme.inc',
'path' => $path,
),
'pbs_tv_schedule_schedule_date_item' => array(
'template' => 'pbs-tv-schedule-schedule-date-item',
'variables' => array(
'feed' => '',
'show_id' => '',
'start_time' => '',
'title' => '',
'subtitle' => '',
'description' => '',
'parent_description' => '',
'duration' => 0,
'closed_captions' => FALSE,
'hd' => FALSE,
'stereo' => FALSE,
'animated' => FALSE,
'upcoming_url' => '',
'listing' => array(),
),
'file' => 'theme.inc',
'path' => $path,
),
'pbs_tv_schedule_schedule_week' => array(
'template' => 'pbs-tv-schedule-schedule-week',
'variables' => array(
'title' => '',
'link_previous' => NULL,
'link_next' => NULL,
'headers' => array(),
'rows' => array(),
),
'file' => 'theme.inc',
'path' => $path,
),
'pbs_tv_schedule_schedule_week_item' => array(
'template' => 'pbs-tv-schedule-schedule-week-item',
'variables' => array(
'show_id' => '',
'title' => '',
'subtitle' => '',
'description' => '',
'duration' => 0,
'closed_captions' => FALSE,
'hd' => FALSE,
'stereo' => FALSE,
'animated' => FALSE,
'flex_basis' => 0,
'listing' => array(),
),
'file' => 'theme.inc',
'path' => $path,
),
'pbs_tv_schedule_schedule_no_results' => array(
'template' => 'pbs-tv-schedule-schedule-no-results',
'variables' => array(
'message' => t('No results found.'),
),
'file' => 'theme.inc',
'path' => $path,
),
'pbs_tv_schedule_settings' => array(
'render element' => 'form',
),
);
}
/**
* Implements hook_views_api().
*/
function pbs_tv_schedule_views_api() {
return array(
'api' => 3.0,
'path' => drupal_get_path('module', 'pbs_tv_schedule') . '/includes/views',
);
}
/**
* Initiate a new API client instance.
*
* @param string $api_key
* The API key to use for the client (defaults to system variable).
* @param string $call_sign
* The call sign to use for the client (defaults to system variable).
* @param bool $use_cache
* Whether or not to use/create cached responses with this client.
*
* @return PbsTvScheduleClient
* TVSS API client.
*
* @see PbsTvScheduleClient
*/
function pbs_tv_schedule_get_api_client($api_key = NULL, $call_sign = NULL, $use_cache = TRUE) {
if (empty($api_key)) {
$api_key = variable_get('pbs_tv_schedule_api_key');
}
if (empty($call_sign)) {
$call_sign = variable_get('pbs_tv_schedule_call_sign');
}
module_load_include('inc', 'pbs_tv_schedule', 'classes/PbsTvScheduleClient');
$client = new PbsTvScheduleClient($api_key, $call_sign, $use_cache);
return $client;
}
/**
* Display date-based schedule page.
*
* This page also handles Program based search on the full schedule.
*/
function pbs_tv_schedule_schedule($form, &$form_state) {
$base_path = drupal_get_path('module', 'pbs_tv_schedule');
$form['#attached'] = array(
'css' => array($base_path . '/css/pbs_tv_schedule.css'),
'js' => array($base_path . '/js/pbs_tv_schedule.js'),
);
$date = _pbs_tv_schedule_filter_get_date();
$date_default = $date->format('Y-m-d');
try {
$date->sub(new DateInterval('P7D'));
}
catch (Exception $e) {
watchdog_exception('pbs_tv_schedule', $e);
}
$date_options = array();
$today = new DateTime();
for ($i = 0; $i < 13; $i++) {
try {
$date->add(new DateInterval('P1D'));
$key = $date->format('Y-m-d');
$value = $date->format('D, M. j');
if ($key == $today->format('Y-m-d')) {
$value = t('@date (today)', array('@date' => $value));
}
$date_options[$key] = $value;
}
catch (Exception $e) {
watchdog_exception('pbs_tv_schedule', $e);
}
}
$form['date'] = array(
'#type' => 'select',
'#title' => 'Date',
'#options' => $date_options,
'#default_value' => $date_default,
);
if (isset($form_state['values']['date'])) {
$form['date']['#default_value'] = $form_state['values']['date'];
}
/* @var PbsTvScheduleClient $client */
$client = pbs_tv_schedule_get_api_client(
variable_get('pbs_tv_schedule_api_key'),
variable_get('pbs_tv_schedule_call_sign')
);
$form_state['storage']['client'] = $client;
$form['channel'] = _pbs_tv_schedule_channel_filter();
if (isset($form_state['values']['channel'])) {
$form['channel']['#default_value'] = $form_state['values']['channel'];
}
$form['program'] = array(
'#type' => 'textfield',
'#title' => t('Search by Program'),
'#autocomplete_path' => 'pbs-tv-schedule/autocomplete/program',
'#default_value' => '',
'#attributes' => array(
'placeholder' => t('Enter a program name...'),
),
);
if (isset($_GET['program'])) {
$form['program']['#default_value'] = $_GET['program'];
}
if (isset($form_state['values']['program'])) {
$form['program']['#default_value'] = $form_state['values']['program'];
}
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('View'),
'#ajax' => array(
'callback' => '_pbs_tv_schedule_schedule_update',
'progress' => array(
'type' => 'throbber',
'message' => t('Updating schedule...'),
),
),
);
$form['actions']['reset'] = array(
'#type' => 'submit',
'#value' => t('Reset'),
'#submit' => array('pbs_tv_schedule_schedule_reset'),
);
$form['schedule'] = pbs_tv_schedule_schedule_results($form, $client);
return $form;
}
/**
* Submit handler for pbs_tv_schedule_schedule().
*
* @see pbs_tv_schedule_schedule()
*/
function pbs_tv_schedule_schedule_submit($form, &$form_state) {
// Attempt to format any type of date. This is useful in situations where a
// site overrides the default date select element (e.g. with date_popup).
$date = &$form_state['values']['date'];
$time = FALSE;
if (is_array($date)) {
if (isset($date['date'])) {
$time = strtotime($date['date']);
}
}
else {
$time = strtotime($date);
}
if (!$time) {
$time = time();
}
$date = date('Y-m-d', $time);
$form_state['rebuild'] = TRUE;
}
/**
* Reset handler for pbs_tv_schedule_schedule().
*
* @see pbs_tv_schedule_schedule()
*/
function pbs_tv_schedule_schedule_reset($form, &$form_state) {
$form_state['rebuild'] = FALSE;
}
/**
* AJAX handler for pbs_tv_schedule_schedule().
*
* Updates the date schedule results and page URL based on form values.
*
* @return array
* Updated schedule results element.
*/
function _pbs_tv_schedule_schedule_update($form, $form_state) {
$commands = array();
$element = pbs_tv_schedule_schedule_results(
$form,
$form_state['storage']['client']
);
$commands[] = ajax_command_html(
'#pbs-tv-schedule-schedule-results',
render($element)
);
$commands[] = array(
'command' => 'pbsTvScheduleUpdateScheduleUri',
'values' => $form_state['values'],
);
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Build schedule data results for the form.
*
* @param array $form
* Current form element.
* @param PbsTvScheduleClient $client
* TVSS API client for program search.
*
* @return array
* Schedule results element for the form.
*
* @see pbs_tv_schedule_schedule()
*/
function pbs_tv_schedule_schedule_results(array $form, PbsTvScheduleClient $client) {
$date = DateTime::createFromFormat('Y-m-d', $form['date']['#default_value']);
$feed = $form['channel']['#default_value'];
$program = $form['program']['#default_value'];
$element = array(
'#tree' => TRUE,
'#prefix' => '<div id="pbs-tv-schedule-schedule-results">',
'#suffix' => '</div>',
);
try {
// If a program search term has been set, provide search results instead of
// a specific date schedule.
if (!empty($program)) {
$listings = pbs_tv_schedule_program_upcoming_episodes($program, $client, $feed);
$response[$feed] = array('listings' => $listings);
$element['results'] = array(
'#theme' => 'pbs_tv_schedule_schedule_search',
'#program' => $program,
'#items' => array(),
);
}
else {
$response = $client->getFullDay($date->format('Ymd'));
$element['results'] = array(
'#theme' => 'pbs_tv_schedule_schedule_date',
'#date' => $date,
'#items' => array(),
);
}
}
catch (PbsTvScheduleClientException $e) {
watchdog_exception('pbs_tv_schedule', $e);
$element['results'] = array(
'#theme' => 'pbs_tv_schedule_schedule_no_results',
'#message' => t('An error occurred. Please try again later.'),
);
return $element;
}
foreach ($response[$feed]['listings'] as $listing) {
_pbs_tv_schedule_format_listing($listing);
if (isset($listing['day'])) {
$date = DateTime::createFromFormat('Ymd', $listing['day']);
}
$date->setTime(
substr($listing['start_time'], 0, 2),
substr($listing['start_time'], 2, 2)
);
$upcoming_url = url(
'pbs-tv-schedule/upcoming/nojs',
array(
'query' => array(
'show_id' => $listing['show_id'],
'feed' => $feed,
'after' => $date->format('YmdHi'),
),
)
);
$element['results']['#items'][] = array(
'#theme' => 'pbs_tv_schedule_schedule_date_item',
'#feed' => $feed,
'#show_id' => $listing['show_id'],
'#start_time' => DateTimeImmutable::createFromMutable($date),
'#title' => $listing['title'],
'#subtitle' => $listing['subtitle'],
'#description' => $listing['description'],
'#parent_description' => $listing['parent_description'],
'#duration' => $listing['minutes'],
'#closed_captions' => $listing['closed_captions'],
'#hd' => $listing['hd'],
'#stereo' => $listing['stereo'],
'#animated' => $listing['animated'],
'#upcoming_url' => $upcoming_url,
'#listing' => $listing,
);
}
if (empty($element['results']['#items'])) {
$element['results'] = array(
'#theme' => 'pbs_tv_schedule_schedule_no_results',
);
if (!empty($program)) {
$element['results']['#message'] = t(
'No results found for %program.',
array('%program' => $program)
);
}
}
return $element;
}
/**
* Channel filter select element.
*
* Accounts for module configuration with default overridable by by a URL
* parameter.
*
* @return array
* Select form element with channel options.
*
* @see pbs_tv_schedule_settings()
*/
function _pbs_tv_schedule_channel_filter() {
$feeds = pbs_tv_schedule_get_feeds();
uasort($feeds, 'drupal_sort_weight');
$feed_options = array();
foreach ($feeds as $feed) {
if (isset($feed['enabled']) && !$feed['enabled']) {
continue;
}
$feed_options[$feed['short_name']] = $feed['full_name'];
}
$feed_default = variable_get(
'pbs_tv_schedule_feed_default',
key($feed_options)
);
if (isset($_GET['channel'])) {
$feed_default = $_GET['channel'];
}
return array(
'#type' => 'select',
'#title' => t('Channel'),
'#options' => $feed_options,
'#default_value' => $feed_default,
);
}
/**
* Gets an array of feeds with config data merged in, if available.
*
* @return array
* Feed information and configuration keyed by feed short name.
*
* @see pbs_tv_schedule_settings()
*/
function pbs_tv_schedule_get_feeds() {
/* @var PbsTvScheduleClient $client */
$client = pbs_tv_schedule_get_api_client(
variable_get('pbs_tv_schedule_api_key'),
variable_get('pbs_tv_schedule_call_sign')
);
$feeds = array();
try {
$feeds = $client->getFeeds();
if ($feeds_config = variable_get('pbs_tv_schedule_feeds_config')) {
$feeds = array_merge_recursive($feeds, $feeds_config);
}
}
catch (PbsTvScheduleClientException $e) {
watchdog_exception('pbs_tv_schedule', $e);
}
return $feeds;
}
/**
* Attempt to create a date object from the `date` URL parameter.
*
* @return bool|DateTime
* A DateTime object is the date is valid, FALSE otherwise.
*/
function _pbs_tv_schedule_filter_get_date() {
$date = date('Y-m-d');
if (isset($_GET['date'])) {
$date = $_GET['date'];
}
$date = DateTime::createFromFormat('Y-m-d', $date);
if ($date === FALSE) {
$date = new DateTime();
drupal_set_message(t('Invalid date provided.'), 'warning');
}
$date->setTimezone(new DateTimeZone(drupal_get_user_timezone()));
return $date;
}
/**
* Autocomplete handler for programs.
*
* Titles are not unique, so it is assumed that any processes dealing with the
* result of this autocomplete will perform a further query to get and filter
* any necessary related data for a selected program.
*
* @param string $string
* The string to use for search.
*/
function _pbs_tv_schedule_autocomplete_program($string) {
$levenshtein = array();
$matches = array();
if (strlen($string) > 2) {
$result = db_select('pbs_tv_schedule_program', 'ptsp')
->fields('ptsp', array('title'))
->condition('title', '%' . db_like($string) . '%', 'LIKE')
->orderBy('title', 'ASC')
->execute();
foreach ($result as $row) {
$levenshtein[$row->title] = levenshtein($string, $row->title);
}
asort($levenshtein, SORT_NUMERIC);
foreach (array_slice($levenshtein, 0, 10) as $word => $value) {
$matches[$word] = check_plain($word);
}
}
drupal_json_output($matches);
}
/**
* Get a list of upcoming episodes for a searched program.
*
* @param string $program_title
* Full title of the program to find upcoming episodes for.
* @param PbsTvScheduleClient $client
* TVSS client for querying the API.
* @param string $feed
* Feed to find upcoming episodes on.
*
* @return array
* Minimal listings data for all upcoming episodes.
*/
function pbs_tv_schedule_program_upcoming_episodes($program_title, PbsTvScheduleClient $client, $feed) {
$listings = array();
$program_ids = db_select('pbs_tv_schedule_program', 'ptsp')
->fields('ptsp', array('program_id'))
->condition('title', trim($program_title))
->execute()
->fetchCol();
if (empty($program_ids)) {
return $listings;
}
foreach ($program_ids as $program_id) {
try {
$response = $client->getProgramInfo($program_id);
foreach ($response['upcoming_episodes'] as $listing) {
if ($listing['feed']['short_name'] == $feed) {
$listing['title'] = $response['title'];
$listing['description'] = $response['description'];
$listing['program_id'] = $response['program_id'];
$listings[] = $listing;
}
}
}
catch (PbsTvScheduleClientException $e) {
watchdog_exception('pbs_tv_schedule', $e);
}
}
return $listings;
}
/**
* Upcoming airings of a show.
*
* @param string $request_method
* Request method of "ajax" or "nojs" depending on how this function was
* called.
*
* @return array
* Render array for a page OR an array of AJAX commands.
*/
function pbs_tv_schedule_upcoming($request_method) {
$page = array();
// The show_id, feed and "after" parameters are required. The "after"
// parameter contains the date and time (YmdHi) of the show used as a basis
// for episodes considered to be "upcoming".
if (isset($_GET['show_id']) && isset($_GET['feed'])
&& isset($_GET['after'])) {
$show_id = $_GET['show_id'];
$feed = $_GET['feed'];
$after = DateTime::createFromFormat('YmdHi', $_GET['after']);
}
else {
drupal_set_message(t('Required parameters missing.'), 'error');
return $page;
}
/* @var PbsTvScheduleClient $client */
$client = pbs_tv_schedule_get_api_client(
variable_get('pbs_tv_schedule_api_key'),
variable_get('pbs_tv_schedule_call_sign')
);
try {
$response = $client->getShowInfo($show_id);
}
catch (PbsTvScheduleClientException $e) {
watchdog_exception('pbs_tv_schedule', $e);
return $page;
}
$list = array(
'#theme' => 'item_list',
'#list_type' => 'ul',
'#title' => t('Upcoming Airings'),
'#items' => array(),
);
foreach ($response['upcoming_shows'] as $show) {
// The endpoint returns data for all feeds and cannot be filtered.
if ($show['feed']['short_name'] != $feed) {
continue;
}
$date = DateTime::createFromFormat('YmdHi', $show['day'] . $show['start_time']);
if ($date > $after) {
$list['#items'][] = $date->format('D, M. j g:i a');
}
}
if (empty($list['#items'])) {
$list = array(
'#type' => 'html_tag',
'#tag' => 'em',
'#value' => t('No upcoming airings currently scheduled.'),
);
}
if ($request_method == 'ajax') {
$commands = array();
$commands[] = ajax_command_replace(
"a[data-show-id='" . $show_id . "']",
drupal_render($list)
);
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}
drupal_set_title(t('@show - Upcoming Airings', array(
'@show' => $response['title'],
)));
$page['description'] = array(
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $response['description'],
);
$page['upcoming'] = $list;
$page['link'] = array(
'#theme' => 'link',
'#text' => t('Return to Schedule'),
'#path' => 'pbs-tv-schedule/schedule',
'#options' => array(
'attributes' => array(),
'html' => FALSE,
),
);
return $page;
}
/**
* Display week-based schedule page.
*
* @return array
* Page render array.
*/
function pbs_tv_schedule_schedule_week() {
$base_path = drupal_get_path('module', 'pbs_tv_schedule');
$page = array();
$page['#attached'] = array(
'library' => array(array('system', 'ui.dialog')),
'css' => array($base_path . '/css/pbs_tv_schedule.css'),
'js' => array($base_path . '/js/pbs_tv_schedule.js'),
);
try {
/* @see pbs_tv_schedule_schedule_week_filter() */
$form = drupal_get_form('pbs_tv_schedule_schedule_week_filter');
$feed = $form['channel']['#default_value'];
$date = DateTime::createFromFormat('Y-m-d', $form['date']['#value']);
$date_initial = clone $date;
// Prevent form elements from appearing in URL.
$form['form_build_id']['#access'] = FALSE;
$form['form_token']['#access'] = FALSE;
$form['form_id']['#access'] = FALSE;
$page['filter_form'] = $form;
// Adjust $date to beginning of week.
$day = $date->format('N') - 1;
$date->sub(new DateInterval('P' . $day . 'D'));
$schedule_title = t(
'Week of @date', array('@date' => $date->format('M. j'))
);
drupal_set_title($schedule_title);
/* @var PbsTvScheduleClient $client */
$client = pbs_tv_schedule_get_api_client(
variable_get('pbs_tv_schedule_api_key'),
variable_get('pbs_tv_schedule_call_sign')
);
// Prepare intervals for 30-minute window processing.
$pt30m = new DateInterval('PT30M');
$p1d = new DateInterval('P1D');
$rows = array();
$prefix_cell = NULL;
for ($i = 1; $i <= 7; $i++) {
$rows[$i] = array();
$rows[$i][] = array(
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => '<span class="schedule-row-header-day">'
. $date->format('D')
. '</span><span class="schedule-row-header-date">'
. $date->format('M. j')
. '</span>',
'#attributes' => array(
'class' => array('schedule-cell', 'schedule-row-header'),
),
);
if (!empty($prefix_cell)) {
$rows[$i][] = $prefix_cell;
$prefix_cell = NULL;
}
$response = $client->getFullDay($date->format('Ymd'));
$listings = $response[$feed]['listings'];
foreach ($listings as $listing) {
_pbs_tv_schedule_format_listing($listing);
$render_array = array(
'#theme' => 'pbs_tv_schedule_schedule_week_item',
'#show_id' => $listing['show_id'],
'#title' => $listing['title'],
'#subtitle' => $listing['subtitle'],
'#description' => $listing['description'],
'#duration' => $listing['minutes'],
'#closed_captions' => $listing['closed_captions'],
'#hd' => $listing['hd'],
'#stereo' => $listing['stereo'],
'#animated' => $listing['animated'],
'#flex_basis' => $listing['minutes'],
'#listing' => $listing,
);
// Prepare extra cell for the next row if a program goes over midnight.
if ($listing['start_time'] + $listing['minutes'] > 2360) {
$prefix_cell = $render_array;
$render_array['#flex_basis'] = 2360 - $listing['start_time'];
$prefix_cell['#flex_basis'] = $listing['start_time'] + $listing['minutes'] - 2360;
}
$rows[$i][] = $render_array;
}
$date->add($p1d);
}
// Create headers.
$headers = array(t('Day'));
$date->setTime(0, 0, 0, 0);
for ($i = 0; $i < 48; $i++) {
$headers[] = $date->format('g:i a');
$date->add($pt30m);
}
// Create links for previous and next week.
$date_initial->sub(new DateInterval('P7D'));
$link_previous = array(
'#theme' => 'link',
'#text' => '',
'#path' => 'pbs-tv-schedule/schedule/week',
'#options' => array(
'attributes' => array(
'title' => t('Previous week'),
),
'query' => array(
'date' => $date_initial->format('Y-m-d'),
'channel' => $feed,
),
'html' => FALSE,
),
);
$date_initial->add(new DateInterval('P14D'));
$link_next = array(
'#theme' => 'link',
'#text' => '',
'#path' => 'pbs-tv-schedule/schedule/week',
'#options' => array(
'attributes' => array(
'title' => t('Next week'),
),
'query' => array(
'date' => $date_initial->format('Y-m-d'),
'channel' => $feed,
),
'html' => FALSE,
),
);
}
catch (Exception $e) {
watchdog_exception('pbs_tv_schedule', $e);
return $page;
}
$page['schedule'] = array(
'#theme' => 'pbs_tv_schedule_schedule_week',
'#title' => $schedule_title,
'#link_previous' => $link_previous,
'#link_next' => $link_next,
'#headers' => $headers,
'#rows' => $rows,
);
return $page;
}
/**
* Filter for the week-based schedule page.
*
* @return array
* Form render array.
*
* @see pbs_tv_schedule_schedule_date()
*/
function pbs_tv_schedule_schedule_week_filter($form, &$form_state) {
$form['#method'] = 'get';
$form['#token'] = FALSE;
if ($date = _pbs_tv_schedule_filter_get_date()) {
$form['date'] = array(
'#type' => 'hidden',
'#value' => $date->format('Y-m-d'),
);
}
/* @var PbsTvScheduleClient $client */
$client = pbs_tv_schedule_get_api_client(
variable_get('pbs_tv_schedule_api_key'),
variable_get('pbs_tv_schedule_call_sign')
);
$form_state['storage']['client'] = $client;
$form['channel'] = _pbs_tv_schedule_channel_filter();
$form['actions']['submit'] = array(
'#type' => 'submit',
'#name' => 'view',
'#value' => t('View'),
);
$form['actions']['reset'] = array(
'#type' => 'submit',
'#name' => 'reset',
'#value' => t('Reset'),
);
return $form;
}
/**
* Format data for a listing item from the API.
*
* @param array $listing
* A raw API listing response.
*/
function _pbs_tv_schedule_format_listing(array &$listing) {
$title = $listing['title'];
if (empty($title)) {
$listing['title'] = t('Untitled');
}