-
Notifications
You must be signed in to change notification settings - Fork 0
/
cm_cablecast.module
1197 lines (1069 loc) · 36.7 KB
/
cm_cablecast.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
* Tightrope Cablecast integration for the Community Media Startertkit
* Drupal profile
*/
define('CM_CABLECAST_CHANNEL_TID', variable_get('cm_cablecast_channel_vid', 12));
/**
* Implements hook_menu()
*
* Provides a menu item for a settings page
*
* @return array
*/
function cm_cablecast_menu() {
$items = array();
$items['admin/config/cablecast/cm'] = array(
'title' => 'Community Media Configuration',
'description' => 'Configuration options for the Community Media Cablecast Integration.',
'page callback' => 'drupal_get_form',
'page arguments' => array('cm_cablecast_admin_settings'),
'access arguments' => array('administer cablecast'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Menu Item page callback function.
*
* Provides Cablecast configuration options. Includes booleans on whether
* creating/editing/deleting nodes/entities will push data to Cablecast.
* Also provides a way to provide an alternate show field on drupal Airing
* entities
*/
function cm_cablecast_admin_settings($form, &$form_state) {
$form = array();
$form['cm_cablecast_push_project_on_save'] = array(
'#type' => 'checkbox',
'#title' => t('Push Project Metadata to Cablecast when Project is Created or Updated'),
'#default_value' => variable_get('cm_cablecast_push_project_on_save', 0),
'#description' => t('When enabled pushes Project metadata to the Cablecast
system. Disabling this option will prevent any Project information from being
sent to Cablecast.'),
);
$form['cm_cablecast_push_show_on_save'] = array(
'#type' => 'checkbox',
'#title' => t('Push Show Metadata to Cablecast when Show is Created or Updated'),
'#default_value' => variable_get('cm_cablecast_push_show_on_save', 0),
'#description' => t('When enabled pushes Show metadata to the Cablecast
system. Disabling this option will prevent any Show information from being
sent to Cablecast'),
);
$form['cm_cablecast_push_airing_on_save'] = array(
'#type' => 'checkbox',
'#title' => t('Push Airing to Cablecast when Airing is Created or Updated'),
'#default_value' => variable_get('cm_cablecast_push_airing_on_save', 0),
'#description' => t('When enabled pushes Airing to the Cablecast
system. Disabling this option will prevent any Airing information from being
sent to Cablecast'),
);
$form['cm_cablecast_alternate_show_form_field'] =
array(
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 255,
'#required' => TRUE,
'#title' => t('Alternate Show Form Field'),
'#default_value' =>
variable_get('cm_cablecast_alternate_show_form_field', 0),
'#description' => t('This allows modules to use a custom show '.
'field on the airing form via custom code.'),
);
$form = system_settings_form($form);
return ($form);
}
/**
* Implements hook_form_alter
*
* For forms for:
* -Project Nodes
* -Show Nodes
* -User Profiles
* It will disable the associated cablecast ID field for users who don’t
* have ‘administer cablecast’ privs
*
* @param $form
* @param $form_state
* @param $form_id
*/
function cm_cablecast_form_alter(&$form, &$form_state, $form_id) {
// hide the fields from entity add/edit forms
switch($form_id) {
case 'cm_project_node_form':
if(!user_access('administer cablecast')) {
$form['field_cablecast_project_id']['#access'] = FALSE;
}
break;
case 'cm_show_node_form':
if(!user_access('administer cablecast')) {
$form['field_cablecast_show_id']['#access'] = FALSE;
}
break;
case 'user_profile_form':
if(!user_access('administer cablecast')) {
$form['field_cablecast_producer_id']['#access'] = FALSE;
}
break;
}
}
/**
* Implements hook_node_presave for project and show nodes and will call
* the following sync functions:
*
* -Project : cm_cablecast_sync_project()
* -Show : cm_cablecast_sync_show()
*/
function cm_cablecast_node_presave($node) {
switch($node->type) {
case 'cm_project':
if (variable_get('cm_cablecast_push_project_on_save', 0) == '1') {
cm_cablecast_sync_project($node);
}
break;
case 'cm_show':
if (variable_get('cm_cablecast_push_show_on_save', 0) == '1') {
cm_cablecast_sync_show($node);
}
break;
}
}
/**
* Implments hook_entity_presave() and will call cm_cablecast_sync_airing()
* for airing entities if pushes are enabled
*/
function cm_cablecast_entity_presave($entity, $type) {
//http://test.pcmtv.org/Cablecast/Schedule.Runs.Advanced.aspx?scheduleID=59251
if($type == 'airing') {
if (variable_get('cm_cablecast_push_airing_on_save', 0) == '1') {
$cablecast_airing_id = cm_cablecast_sync_airing($entity);
$entity->field_cablecast_airing_id[LANGUAGE_NONE][0]['value'] =
$cablecast_airing_id;
}
}
}
/**
* Implements hook_entity_delete() for show nodes and airing entities and
* type will call it’s subsequent delete function.
*
* -Show cablecast_api_delete_show()
* -Airing cablecast_api_delete_airing()
*/
function cm_cablecast_entity_delete($entity, $type) {
if($type == 'airing') {
if (variable_get('cm_cablecast_push_airing_on_save', 0) == '1') {
$status = cablecast_api_delete_airing($entity);
}
}
if ($type == 'node' && $entity->type == 'cm_show') {
$status = cablecast_api_delete_show($entity);
}
}
/**
* Called from cm_cablecast_entity_presave() and determines if we are
* adding/editing an airing and then adds or deletes/re-adds Runs of the Show
* in Cablecast
*
* @param $show
* The Show $node
*
*/
function cm_cablecast_sync_airing($airing) {
global $cm_cablecast_skip_confirmation_messages_flag;
if(!user_access('administer cablecast')) {
$cm_cablecast_skip_confirmation_messages_flag = TRUE;
}
$title = cm_cablecast_get_single_field_value($airing,
'field_airing_title');
//dsm($airing, "syncing airing ". $title . " " . $airing->airing_id);
// get the Cablecast Show ID from the related Show node
$d_show_id = cm_cablecast_get_single_field_value($airing,
'field_airing_show_ref',
'target_id');
//if the show id can't be found, look for an alternate field
if (!$d_show_id) {
$field = variable_get('cm_cablecast_alternate_show_form_field', NULL);
if ($field) {
$d_show_id = isset($airing->{$field}) ? $airing->{$field} : NULL;
}
}
//if we have a show id, load the drupal object and get the cablecast id
if ($d_show_id) {
$show_node = node_load($d_show_id);
if ($data = field_get_items('node', $show_node,
'field_cablecast_show_id', $langcode = NULL)) {
$show_cablecast_id = $data[0]['value'];
}
}
//fetch the new channel info for both drupal and cablecast
$tid = cm_cablecast_get_single_field_value($airing, 'field_airing_channel',
'tid');
$channel_name = cm_cablecast_convert_channel_tid_to_name($tid);
//fetch cablecast Event
$cc_id = cm_cablecast_get_single_field_value($airing,
'field_cablecast_airing_id',
'value');
$cc_airing = $cc_id ? cablecast_api_get_airing($airing) : NULL;
//DOES THIS HAVE AIRING ALREADY HAVE A CABLECAST ID?
if ($cc_airing) {
//find tid for cc event channel
$cc_tid = $cc_airing->drupal_channel_tid;
//since there is no cablecast function to update a ScheduleEvent for a show
//we will delete the old one and create a new one
$should_edit =
_cm_cablecast_is_airing_changed($airing, $cc_airing, $cc_tid);
if ($should_edit) {
cablecast_api_delete_airing($airing);
if (isset($show_cablecast_id) && $show_cablecast_id) {
$ret = cablecast_api_add_airing($airing, $show_cablecast_id,
$channel_name);
$message = t("Successfully updated airing on Cablecast");
}
}
else {
$ret = $cc_airing->ScheduleID;
//do nothing, airing still matches cablecast
}
}
//only create the airing if we actually have a show for it
else if (isset($show_cablecast_id) && $show_cablecast_id) {
$ret = cablecast_api_add_airing($airing, $show_cablecast_id,
$channel_name);
$message = t("Successfully created airing on Cablecast");
}
if (isset($message) &&
(!isset($cm_cablecast_skip_confirmation_messages_flag) ||
!$cm_cablecast_skip_confirmation_messages_flag )) {
drupal_set_message($message);
}
$ret = isset($ret) && $ret ? $ret : NULL;
return $ret;
}
/**
* @param $project
* The $project to push to Cablecast
*
*
*/
function cm_cablecast_sync_project($node) {
//dsm($node, "syncing node ". $node->title . " " . $node->nid);
// project needs to be an og_group with an active status, and published, if not, return without processing
if (!og_is_group('node', $node) || !$node->status) {
return;
}
global $cm_cablecast_skip_confirmation_messages_flag;
if(!user_access('administer cablecast')) {
$cm_cablecast_skip_confirmation_messages_flag = TRUE;
}
// need producer id for project
$cc_producer_id = cm_cablecast_sync_producer($node->uid);
$cc_producer_id = $cc_producer_id ? $cc_producer_id : 0;
$cc_project_id =
cm_cablecast_get_single_field_value($node, 'field_cablecast_project_id');
if ($cc_project_id && $cc_project_id = cablecast_api_get_projects($node)) {
// existing project_id, update
if (cablecast_api_update_project($node)) {
// successful update project
$message = t('Updated <em>!title</em> on Cablecast',
array('!title' => check_plain($node->title)));
}
else {
// failed updating project
drupal_set_message(t('There was an error while updating Project '.
'<em>!title</em> on Cablecast',
array('!title' => check_plain($node->title))),
'error');
}
}
else {
$cc_project_id = cablecast_api_add_project($node);
if ($cc_project_id) {
// successfully added new project
// add the cablecast project_id to the project node
$node->field_cablecast_project_id[LANGUAGE_NONE][0]['value'] =
$cc_project_id;
$message = t('Saved new Project <em>!title</em> on Cablecast',
array('!title' => check_plain($node->title)));
}
else {
// failed adding new project
drupal_set_message(t('There was an error saving new Project'.
'<em>!title</em> on Cablecast',
array('!title' => check_plain($node->title))),
'error');
}
}
//set the success message unless the global flag says not to, for things
//like batch scheduling
if (isset($message) &&
(!isset($cm_cablecast_skip_confirmation_messages_flag) ||
!$cm_cablecast_skip_confirmation_messages_flag )) {
drupal_set_message($message);
}
}
/**
* Called from cm_cablecast_node_presave() and either adds a new show to
* cablecast or updates an existing one.
* @param $show
* The Show $node
*
*/
function cm_cablecast_sync_show($show) {
//dsm($show, "syncing show ". $show->title . " " . $show->nid);
// show needs to be published
if (!$show->status) {
return;
}
global $cm_cablecast_skip_confirmation_messages_flag;
if(!user_access('administer cablecast')) {
$cm_cablecast_skip_confirmation_messages_flag = TRUE;
}
// fetch the cablecast project id
$d_project_id =
cablecast_api_get_single_field_value($show, 'og_group_ref', 'target_id');
$d_project = $d_project_id ? node_load($d_project_id) : NULL;
if ($d_project) {
//FIXME DO I WANT TO DO THIS?
//cm_cablecast_sync_project($d_project);
$cc_project_id =
cablecast_api_get_single_field_value($d_project,
'field_cablecast_project_id',
'value');
$cc_project_id = $cc_project_id ? $cc_project_id : 0;
}
// fetch the cablecast producer id
$d_producer =$d_project ? user_load($d_project->uid) : user_load($show->uid);
$cc_producer_id = $d_producer ?
cablecast_api_get_single_field_value($d_producer,
'field_cablecast_producer_id',
'value') : 0;
$cc_producer_id = cm_cablecast_sync_producer($d_producer->uid);
if (!$cc_producer_id || !$cc_project_id) {
//FIXME, DO WE REALLY WANT TO SKIP?
watchdog('cm_cablecast', "Error Code 3456: Drupal Show ".$show->nid.
' has no cc_project_id', array(), WATCHDOG_ERROR);
return;
}
//fetch the cablecast show id
$cc_show_id =
cablecast_api_get_single_field_value($show, 'field_cablecast_show_id');
//CHECK TO MAKE SURE THERE REALLY EXISTS A CABLECAST SHOW WITH $cc_show_id
if ($cc_show_id && $cc_show_id = cablecast_api_get_show($show)) {
// existing show_id, update
if (cablecast_api_update_show($show, $cc_producer_id, $cc_project_id)) {
$message = t('Updated <em>!title</em> on Cablecast',
array('!title' => check_plain($show->title)));
}
else {
// failed updating show
drupal_set_message(t("There was an error while updating Show ".
"<em>!title</em> on Cablecast",
array('!title' =>
check_plain($show->title))), 'error');
}
}
else {
$cc_show_id = cablecast_api_add_show($show);
if ($cc_show_id) {
// successfully added new show
// add the show_id to the $show->field_cablecast_show_id field
$show->field_cablecast_show_id[LANGUAGE_NONE][0]['value'] = $cc_show_id;
$message = t('Saved new Show <em>!title</em> on Cablecast',
array('!title' => check_plain($show->title)));
}
else {
// failed adding new project
drupal_set_message(t('There was an error saving new Show '.
'<em>!title</em> on Cablecast',
array('!title' => check_plain($show->title))),
'error');
}
}
//if we got a $cc_show_id, set it on the show node
if ($cc_show_id) {
$show->field_cablecast_show_id[LANGUAGE_NONE][0]['value'] = $cc_show_id;
}
//set the success message unless the global flag says not to, for things
//like batch scheduling
if (isset($message) &&
(!isset($cm_cablecast_skip_confirmation_messages_flag) ||
!$cm_cablecast_skip_confirmation_messages_flag )) {
drupal_set_message($message);
}
}
/**
* Currently not called.
*
* Written to be called from hook_cron() to update Cablecast Projects using
* all of the Drupal project nodes
*/
function _cm_cablecast_sync_projects($channels) {
watchdog('CM Cablecast', 'Syncing Projects');
if(!is_array($channels)) {
$channels = array($channels);
}
$data = array();
$data['ChannelID'] = $channels[0]->ChannelID;
$projects = cm_cablecast_get_projects($data);
if(!is_array($projects)) {
$projects = array($projects);
}
$mapping = _cm_cablecast_project_info();
foreach($projects as $project) {
_cm_cablecast_sync_cablecast_object($project, $mapping);
}
}
/**
* Currently not called.
*
* Written to be called from hook_cron() to update Drupal Show nodes using
* Cablecast Shows updated/created past a certain date.
*/
function _cm_cablecast_sync_shows() {
watchdog('CM Cablecast', 'Syncing Shows');
try {
$locations = cm_cablecast_get_locations();
foreach ($locations as $location) {
$cm_cablecast_location_id = $location->Location->LocationID;
}
$cm_cablecast_last_sync = variable_get("cablecast_last_sync_location_" . $cm_cablecast_location_id,
'1900-01-01T12:00:00');
$data = array(
'LocationID' => $cm_cablecast_location_id,
'SearchDate' => $cm_cablecast_last_sync,
'DateComparator' => ">",
);
$cablecast_updated_shows = cablecast_api_last_modified_search($data);
//Guard against no results.
if($cablecast_updated_shows == NULL) {
return;
}
//Guard against 1 result.
if(!is_array($cablecast_updated_shows)) {
$cablecast_updated_shows = array($cablecast_updated_shows);
}
foreach($cablecast_updated_shows as $show) {
// do we even have a Show in Drupal
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'cm_show')
->fieldCondition('field_cablecast_show_id', 'value', $show->ShowID);
$result = $query->execute();
if ($result['node']) {
// existing show
foreach ($result['node'] as $show_node) {
$nid = $show_node->nid;
}
} else {
$nid = false;
}
if($nid == false) {
$node = new stdClass();
$node->type = 'cm_show';
node_object_prepare($node);
$node->language = LANGUAGE_NONE;
}
// Load node for shows that do exist
else {
$node = node_load($nid);
}
$node->title = $show->Title;
$node->field_cablecast_show_id['und'][0]['value'] = $show->ShowID;
$node->title = $show->Title;
// if ($show->ProjectID >= 1) {
// $node->og_group_ref['und'][0]['target_id'] = _cm_cablecast_get_project_nid($show->ProjectID);
// }
watchdog("CM Cablecast", "Syncing Show: @showName", array('@showName' => $node->title));
//Give Other modules a chance to extract data, specifically custom fields
module_invoke_all('cm_cablecast_show_presave', $node, $show);
node_save($node);
$cm_cablecast_last_sync = $show->LastModified;
}
variable_set("cablecast_last_sync_location_".$cm_cablecast_location_id, $cm_cablecast_last_sync);
}
catch(SoapFault $fault) {
watchdog("Cablecast", "Communicating to server caused the following error: ".$fault->faultstring);
}
}
/**
* Currently not called.
*
* Written to be called from hook_cron() to update Drupal Airings by looking
* up the Cablecast Schedule for a range spanning 3 days in past to 30 days in
* future. Airings will be created or updated for each of the cablecast Runs.
* Shows will not be auto created, that happens in cm_cablecast_sync_shows()
*/
function _cm_cablecast_sync_schedule($channels) {
watchdog('CM Cablecast', 'Syncing Schedule');
// This shouldn't happen, but its possible there are no channels
if($channels == null) {
return;
}
$start_time = date('Y-m-d 00:00:00', time() - 60*60*24*3); // 3 days ago
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'airing')
->entityCondition('bundle', 'airing')
->fieldCondition('field_airing_date', 'value', $start_time, '>=');
$result = $query->execute();
$schedule_airing_ids = null;
if ($result['airing']) {
foreach ($result['airing'] as $airing) {
$schedule_airing_ids[$airing->airing_id] = $airing->airing_id;
}
}
foreach ($channels as $channel) {
// we will need this later
$channel_tid = _cm_cablecast_get_channel_tid($channel->ChannelID);
$params = array(
'ChannelID' => $channel->ChannelID,
'FromDate' => date('Y-m-d\T00:00:00', time() - 60*60*24*3), // 3 days ago
'ToDate' => date('Y-m-d\T23:59:59', time() + 60*60*24*30), // 30 days from now
'restrictToShowID' => 0,
);
$schedule_events = cm_cablecast_get_schedule($params);
// Schedule events can be an array, single run, or NULL
// We need to normalize to an array
if(!is_array($schedule_events)) {
if($schedule_events == NULL) {
$schedule_events = array();
} else {
$schedule_events = array($schedule_events);
}
}
foreach ($schedule_events as $run) {
//Check if Show even exists. If it doesn't well sync this later once we have the show
$show_nid = _cm_cablecast_get_show_nid($run->ShowID);
if ($show_nid == false) {
continue;
}
// convert the Cablecast dates to UTC since the Drupal date field expects this
$run->StartTime = _cm_cablecast_alter_date_tz($run->StartTime);
$run->EndTime = _cm_cablecast_alter_date_tz($run->EndTime);
//Check if Schedule event exists (airing)
$airing_id = _cm_cablecast_get_airing_eid($run->ScheduleID);
if ($airing_id == false) {
// no existing airing found, create a new airing
$airing = entity_create('airing', array('type' => 'airing'));
} else {
// existing airing found, load the airing
$airing = entity_load('airing', array($airing_id));
$airing = array_shift($airing);
if($key = array_search($airing_id, $schedule_airing_ids)) {
unset($schedule_airing_ids[$key]);
}
}
// save the airing if it has changed
if ($airing_id == false || _cm_cablecast_is_airing_changed($airing, $run, $channel_tid)) {
// update the values of the airing
$airing->field_airing_title[LANGUAGE_NONE][0]['value'] = $run->ShowTitle;
$airing->field_cablecast_airing_id[LANGUAGE_NONE][0]['value'] = $run->ScheduleID;
$airing->field_airing_channel[LANGUAGE_NONE][0]['tid'] = $channel_tid;
$airing->field_airing_date[LANGUAGE_NONE][0]['value'] = $run->StartTime;
$airing->field_airing_date[LANGUAGE_NONE][0]['value2'] = $run->EndTime;
if (!$show_nid == false) {
$airing->field_airing_show_ref[LANGUAGE_NONE][0]['nid'] = $show_nid;
}
entity_save('airing', $airing);
}
}
}
// Remove deleted runs
entity_delete_multiple('airing', $schedule_airing_ids);
}
/**
* Gets a Drupal user ID from a Cablecast Producer ID if it exists
*
* Duplicate of function in cablecast_api: cablecast_api.module
* @param $producer_id
*
* @return mixed
*/
function _cm_cablecast_get_user_id($producer_id) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'user')
->fieldCondition('field_cablecast_producer_id', 'value', $producer_id, '=')
->addMetaData('account', user_load(1)); // Run the query as user 1.
$result = $query->execute();
if ($result) {
foreach ($result['user'] as $user) {
return $user->uid;
}
}
return 1;
}
/**
* Called from cm_cablecast_sync_show() and cm_cablecast_sync_project()
*
* It will either add a new Cablecast Producer or update an existing one
*
* @param $user (int)
* The user id of the Drupal user to sync to Cablecast
*
* @return $producer_id (int)
* The Cablecast producer id
*/
function cm_cablecast_sync_producer($user_id) {
if (!$user_id) {
drupal_set_message(t("Error Code 342587: Missing user!"), 'error');
return;
}
global $cm_cablecast_skip_confirmation_messages_flag;
if(!user_access('administer cablecast')) {
$cm_cablecast_skip_confirmation_messages_flag = TRUE;
}
$d_producer = user_load($user_id);
$cc_producer_id = $d_producer ?
cablecast_api_get_single_field_value($d_producer,
'field_cablecast_producer_id',
'value') : 0;
// existing producer, ensure record exists on Cablecast
if ($cc_producer_id &&
$cc_producer_id = cablecast_api_get_producer($d_producer)) {
$cc_producer_id = cablecast_api_update_producer($d_producer);
if ($cc_producer_id) {
// successful update of producer record
$name = isset($d_producer->display_name) ? $d_producer->display_name :
$d_producer->name;
$message = t('Updated producer <em>!name</em> on Cablecast',
array('!name' => $name));
}
else {
// something broke
drupal_set_message(t('There was an error updating producer '.
'<em>!name</em> on Cablecast',
array('!name' => $d_producer->display_name)),
'error');
}
}
else {
// new producer, create
$cc_producer_id = cablecast_api_add_producer($d_producer);
if ($cc_producer_id) {
// successful create new producer
$message = t('Saved new producer <em>!name</em> on Cablecast',
array('!name' => $d_producer->display_name));
// add the producer_id to the cm_cablecast_producer_id field
$d_producer->field_cablecast_producer_id[LANGUAGE_NONE][0]['value'] =
$cc_producer_id;
user_save($d_producer);
}
else {
// something broke
drupal_set_message(t('There was an error saving new producer '.
'<em>!name</em> on Cablecast',
array('!name' => $d_producer->display_name)),
'error');
}
}
//set the success message unless the global flag says not to, for things
//like batch scheduling
if (isset($message) &&
(!isset($cm_cablecast_skip_confirmation_messages_flag) ||
!$cm_cablecast_skip_confirmation_messages_flag )) {
drupal_set_message($message);
}
return $cc_producer_id;
}
/**
* Only used in the disabled cron auto sync functionality.
*
* Duplicate of function in cablecast_api: cablecast_api.module
*
* Returns a Drupal Project entity id from a Cablecast object ID
*
*
* @param $project_id
*
* @param $info
*
* @return null
*/
function _cm_cablecast_get_drupal_eid($cablecast_id, $info, $key) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', $info['entity_type'])
->entityCondition('bundle', $info['bundle'])
->fieldCondition($info['fields'][$info['cablecast_id']]['drupal_field'], 'value', $cablecast_id, '=');
$result = $query->execute();
if ($result[$info['entity_type']]) {
foreach ($result[$info['entity_type']] as $entity) {
// there should only be one entity so return on the first one
return $entity->$key;
}
}
return null;
}
/**
* Only used in the disabled cron auto sync functionality.
*
* Duplicate of function in cablecast_api: cablecast_api.module
*
*
* Written to get a cablecast channel ID from a Drupal taxonomy term
* @param $channel_id
*
* @param $info
* @param $key
*
* @return mixed
*/
function _cm_cablecast_get_channel_eid($channel_id, $info, $key) {
$channel_tid = null;
$taxonomyQuery = new EntityFieldQuery();
$result = $taxonomyQuery->entityCondition('entity_type', $info['entity_type'])
->entityCondition('bundle', $info['bundle'])
->fieldCondition($info['fields']['ChannelID'], 'value', $channel_id)
->execute();
if ($result[$info['entity_type']]) {
foreach ($result[$info['entity_type']] as $entity) {
// there should only be one project so return on the first one
return $entity->$key;
}
}
return null;
}
/**
* Returns a Drupal Show NID using the value for field field_cablecast_show_id
* as a search term
* @param $show_id
*
* @return mixed
*/
function _cm_cablecast_get_show_nid($show_id) {
$show_nid = false;
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'cm_show')
->fieldCondition('field_cablecast_show_id', 'value', $show_id, '=');
$result = $query->execute();
if (isset($result['node'])) {
$show_nid = array_keys($result['node']);
}
return $show_nid[0];
}
/**
* Only used in the disabled cron auto sync functionality.
*
* Returns a Drupal Airing entity id by searching for an entity with a
* matching value set for the field field_cablecast_airing_id
* @param $schedule_id
*
* @return mixed
*/
function _cm_cablecast_get_airing_eid($schedule_id) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'airing')
->entityCondition('bundle', 'airing')
->fieldCondition('field_cablecast_airing_id', 'value', $schedule_id, '=');
$result = $query->execute();
if (isset($result['airing'])) {
$airing_eid = array_keys($result['airing']);
}
return $airing_eid[0];
}
/**
* Only used in the disabled cron auto sync functionality.
*
* Duplicate of function in cablecast_api: cablecast_api.module
*
* Helper function to determine if a selected Drupal channel (term) is
* different from the Cablecast channel.
*
* @param $channel_term
* @param $channel
*
* @return bool
*/
function _cm_cablecast_is_channel_changed($entity_wrapper, $project, $mapping) {
foreach ($mapping['fields'] as $dest_field => $source_field) {
$field_info = $entity_wrapper->$source_field->info();
if (isset($field_info['property info'])) {
// a multi-valued field.
$value = $entity_wrapper->$source_field->value->value();
} else {
// a single-valued field
$value = $entity_wrapper->$source_field->raw();
}
if ($value != $project->$dest_field ) {
return true;
}
}
return false;
}
/**
* Only used in the disabled cron auto sync functionality.
*
* Duplicate of function in cablecast_api: cablecast_api.module
*
* Helper function to determine if a selected Drupal project is different
* from the Cablecast Project
*
* @param $node
* @param $project
*
* @return bool
*/
function _cm_cablecast_is_cablecast_object_changed($entity_wrapper, $cablecast_object, $mapping) {
foreach ($mapping['fields'] as $cablecast_field => $field_settings) {
if ($field_settings['drupal_field']) {
$field_info = $entity_wrapper->{$field_settings['drupal_field']}->info();
if (isset($field_info['property info'])) {
// a multi-valued field.
$value = $entity_wrapper->{$field_settings['drupal_field']}->value->value();
} else {
// a single-valued field
$value = $entity_wrapper->{$field_settings['drupal_field']}->raw();
}
if ($value != $cablecast_object->$cablecast_field ) {
return true;
}
}
}
return false;
}
/**
* Helper function to determine if a selected Drupal airing is different
* from the Cablecast run.
*
* @param $airing
* @param $run
*
* @param $channel_tid
*
* @internal param $channel_id
*
* @return bool
*/
function _cm_cablecast_is_airing_changed($airing, $run, $channel_tid) {
//fetch all drupal fields
$d_title = cm_cablecast_get_single_field_value($airing,
'field_airing_title',
'value');
$d_channel = cm_cablecast_get_single_field_value($airing,
'field_airing_channel',
'tid');
$d_start = cm_cablecast_get_single_field_value($airing,
'field_airing_date',
'value');
$d_end = cm_cablecast_get_single_field_value($airing,
'field_airing_date',
'value2');
$d_show_id = cm_cablecast_get_single_field_value($airing,
'field_airing_show_ref',
'target_id');
$d_cc_id = cm_cablecast_get_single_field_value($airing,
'field_cablecast_airing_id',
'value');
//date processing
$offset = date('Z', strtotime($d_start));
$start_ts = strtotime($d_start) + $offset;
$d_start = date('Y-m-d', $start_ts) . 'T' . date('H:i:s', $start_ts);
$end_ts = strtotime($d_end) + $offset;
$d_end = date('Y-m-d', $end_ts) . 'T' . date('H:i:s', $end_ts);
//grab drupal show for title
$show = $d_show_id ? node_load($d_show_id) : NULL;
$d_title = $show ? $show->title : $d_title;
$title_match = ($d_title == $run->ShowTitle) ? true : false;
$channel_match = ($d_channel == $channel_tid) ? true : false;
$start_match = ($d_start == $run->StartTime) ? true : false;
//DON'T THINK WE SHOULD CHECK ENDTIME BECAUSE ITS ALWAYS BASED ON SHOW
//RUNTIME,AND IF WE HAVE DIFFS ON THE SHOW RUNTIME WE SHOULD JUST RELY
//ON CABLEAST AS ITS SHOW REEL DATA IS AUTHORATATIVE
//$end_match = ($d_end == $run->EndTime) ? true : false;
$end_match = TRUE;
$show_match = ($d_show_id == _cm_cablecast_get_show_nid($run->ShowID)) ?
true : false;
$id_match = ($d_cc_id == $run->ScheduleID) ? true : false;
/*
dsm("$title_match? \n $d_title \n ".$run->ShowTitle);
dsm("$channel_match? \n $d_channel \n ".$channel_tid);
dsm("$start_match? \n $d_start \n ".$run->StartTime);
dsm("$end_match? \n $d_end \n ".$run->EndTime);
dsm("$show_match? \n $d_show_id \n ".
_cm_cablecast_get_show_nid($run->ShowID));
dsm("$id_match? \n $d_cc_id \n ".$run->ScheduleID);