-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbrafton.module
More file actions
1275 lines (1112 loc) · 40.3 KB
/
brafton.module
File metadata and controls
1275 lines (1112 loc) · 40.3 KB
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
/**
* Call to the Adfero APIs.
*/
require_once dirname(__FILE__) . '/SampleAPIClientLibrary/ApiHandler.php';
require_once dirname(__FILE__) . '/RCClientLibrary/AdferoArticlesVideoExtensions/AdferoVideoClient.php';
require_once dirname(__FILE__) . '/RCClientLibrary/AdferoArticles/AdferoClient.php';
require_once dirname(__FILE__) . '/RCClientLibrary/AdferoPhotos/AdferoPhotoClient.php';
/**
* Implementation of hook_menu() to create the blog pages and the admin settings page.
*/
function brafton_menu() {
$items = array();
if( variable_get( 'brafton_blog_page' ) == 1 ) {
$items['news'] = array(
'title' => t( 'Latest News' ),
'page callback' => 'brafton_blog_landing',
'page arguments' => array( 'b_news' ),
'access arguments' => array( 'view brafton blog' ),
'type' => MENU_CALLBACK,
);
}
if( variable_get( 'brafton_video_page' ) == 1 ) {
$items['video'] = array(
'title' => t( 'Video News' ),
'page callback' => 'brafton_blog_landing',
'page arguments' => array( 'b_video' ),
'access arguments' => array( 'view brafton blog' ),
'type' => MENU_CALLBACK,
);
}
if( variable_get( 'brafton_blog_archives' ) == 1 ) {
$items['news/archive'] = array(
//'title' => t( 'News Archives' ),
//'title callback' => 'brafton_archives_title', */ I could not get this to work for some reason. It always shows December 1999. I opted to programatically add the title through the brafton_archives_load function */
'page callback' => 'brafton_archives_load',
'page arguments' => array( 'b_news' ),
'title arguments' => array( 'b_news',2,3 ),
'access arguments' => array( 'view brafton blog' ),
'type' => MENU_CALLBACK,
);
}
if( variable_get( 'brafton_video_archives' ) == 1 ) {
$items['video/archive'] = array(
//'title' => t( 'News Archives' ),
//'title callback' => 'brafton_archives_title', */ I could not get this to work for some reason. It always shows December 1999. I opted to programatically add the title through the brafton_archives_load function */
'page callback' => 'brafton_archives_load',
'page arguments' => array( 'b_video' ),
'title arguments' => array( 2,3 ),
'access arguments' => array( 'view brafton blog' ),
'type' => MENU_CALLBACK,
);
}
$items['admin/config/content/brafton-settings'] = array(
'title' => t( 'All in One Brafton Content Integrator' ),
'description' => t( 'The settings for the All in One Brafton Content Integrator.' ),
'page callback' => 'drupal_get_form',
'page arguments' => array( 'brafton_admin_form' ),
'access arguments' => array( 'administer comments' ),
);
return $items;
}
/**
* Implementation of hook_block_info() to register the blog blocks.
*/
function brafton_block_info() {
$blocks = array();
if( variable_get( 'brafton_blog_headlines' ) == 1 ) {
$blocks['headlines'] = array(
'info' => t( 'Brafton Blog Headlines' ),
'cache' => DRUPAL_NO_CACHE,
);
}
if( variable_get( 'brafton_video_headlines' ) == 1 ) {
$blocks['headlines'] = array(
'info' => t( 'Brafton Video Headlines' ),
'cache' => DRUPAL_NO_CACHE,
);
}
if( variable_get( 'brafton_blog_categories' ) == 1 ) {
$blocks['categories'] = array(
'info' => t( 'Brafton Blog Categories' ),
'cache' => DRUPAL_NO_CACHE,
);
}
if( variable_get( 'brafton_video_categories' ) == 1 ) {
$blocks['video_categories'] = array(
'info' => t( 'Brafton Video Categories' ),
'cache' => DRUPAL_NO_CACHE,
);
}
if( variable_get( 'brafton_blog_archives' ) == 1 ) {
$blocks['archives'] = array(
'info' => t( 'Brafton Blog Archives' ),
'cache' => DRUPAL_NO_CACHE,
);
}
if( variable_get( 'brafton_video_archives' ) == 1 ) {
$blocks['video_archives'] = array(
'info' => t( 'Brafton Video Archives' ),
'cache' => DRUPAL_NO_CACHE,
);
}
return $blocks;
}
/**
* Implementation of hook_block_view() to render the blog blocks.
*/
function brafton_block_view( $delta = '' ) {
$block = array();
switch( $delta ) {
case 'headlines':
$block['subject'] = '<h3>' . l( t( 'Latest News' ),'news' ) . '</h3>';
$block['content'] = brafton_headlines( 'b_news' );
break;
case 'video_headlines':
$block['subject'] = '<h3>' . l( t( 'Latest Video News' ),'video' ) . '</h3>';
$block['content'] = brafton_headlines( 'b_video' );
break;
case 'categories':
$block['subject'] = '<h3>Categories</h3>';
$block['content'] = brafton_categories( 'b_news' );
break;
case 'video_categories':
$block['subject'] = '<h3>Video Categories</h3>';
$block['content'] = brafton_categories( 'b_video' );
break;
case 'archives':
$block['subject'] = '<h3>Archives</h3>';
$block['content'] = brafton_archives( 'b_news' );
break;
case 'video_archives':
$block['subject'] = '<h3>Video Archives</h3>';
$block['content'] = brafton_archives( 'b_video' );
break;
}
return $block;
}
/**
* Implementation of hook_node_info() to create the News and Video Article content creation forms.
*/
function brafton_node_info() {
$brafton_nodes = array(
'b_news' => array(
'name' => t( 'News Article' ),
'base' => 'node_content',
'description' => t( 'Use <em>news articles</em> for your Brafton, ContentLEAD, or Castleford blog.' ),
),
'b_video' => array(
'name' => t( 'Video Article' ),
'base' => 'node_content',
'description' => t( 'Use <em>Video articles</em> for your Brafton, ContentLEAD, or Castleford video blog.' ),
),
);
return $brafton_nodes;
}
/**
* Implementation of hook_node_view_alter to add related posts to the News Article node view.
*/
function brafton_node_view_alter( &$build ) {
if( variable_get( 'brafton_related_articles' ) == 1 ) {
if( $build['#bundle'] == 'b_news' && $build['#view_mode'] == 'full' ){
$build['#post_render'] = array( 'brafton_related_posts' );
}
}
if( variable_get( 'brafton_related_videos' ) == 1 ) {
if( $build['#bundle'] == 'b_video' && $build['#view_mode'] == 'full' ){
$build['#post_render'] = array( 'brafton_related_posts' );
}
}
}
/**
* Implementation of hook_init to include atlantis scripts to the head of pages.
*/
function brafton_init() {
/*
drupal_add_css('http://p.ninjacdn.co.uk/atlantisjs/v0.11.7/atlantisjs.css', array('type' => 'external'));
drupal_add_js('http://p.ninjacdn.co.uk/atlantisjs/v0.11.7/atlantis.js', array(
'type' => 'external',
'scope' => 'header',
'group' => JS_THEME,
'every_page' => TRUE,
'weight' => -1,
));
*/
drupal_add_css('http://atlantisjs.brafton.com/v1/atlantisjsv1.3.css', array('type' => 'external'));
drupal_add_js('http://atlantisjs.brafton.com/v1/atlantis.min.v1.3.js', array(
'type' => 'external',
'scope' => 'header',
'group' => JS_THEME,
'every_page' => TRUE,
'weight' => -1,
));
}
/**
* Implementation of hook_permission to create toggleable access permissions for the blog page.
*/
function brafton_permission() {
$permissions = array(
'view brafton blog' => array(
'title' => t( 'View the Brafton blog' ),
'description' => t( 'Allow/Disallow the user to view the Brafton blog.' ),
),
);
return $permissions;
}
/**
* Creation of the admin form.
*/
function brafton_admin_form() {
$form = array();
//Gets the users as an array for the author dropdown
$results = db_query( "SELECT uid, name FROM {users} WHERE status=1" );
$user_array = $results->fetchAllKeyed();
//Add option for getting dynamic author.
//0 is also the id for anonymous author as a fall back if no author is set in the feed
$user_array[0] = 'Get Author from Article';
//Renders the admin form using the Drupal forms API.
$form['brafton_feed_type'] = array(
'#type' => 'select',
'#title' => t( 'Type of Content' ),
'#description' => t( 'The type(s) of content you are integrating.' ),
'#options' => array(
'articles' => 'Articles',
'videos' => 'Videos',
'both' => 'Both',
),
'#default_value' => variable_get( 'brafton_feed_type','articles' ),
'#prefix' => '<h2>Choose Content Types</h2>',
);
$form['brafton_api_root'] = array(
'#type' => 'select',
'#title' => t( 'API Root' ),
'#description' => t( 'The root domain of your Api key (i.e, api.brafton.com).' ),
'#options' => array(
'http://api.brafton.com' => 'Brafton',
'http://api.contentlead.com' => 'ContentLEAD',
'http://api.castleford.com.au' => 'Castleford',
),
'#default_value' => variable_get( 'brafton_api_root','http://api.brafton.com' ),
'#prefix' => '<h2>For Articles</h2>',
);
$form['brafton_api_key'] = array(
'#type' => 'textfield',
'#title' => t( 'Api Key' ),
'#description' => t( 'Your API key (of the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).' ),
'#default_value' => variable_get( 'brafton_api_key' ),
'#size' => 36,
'#maxlength' => 36,
);
$form['brafton_archive_file'] = array(
'#type' => 'managed_file',
'#title' => t( 'Article Archive File' ),
'#description' => t( 'Your article archive file.' ),
'#default_value' => variable_get( 'brafton_archive_file' ),
'#upload_validators' => array(
'file_validate_extensions' => array(0 => 'xml'),
),
);
$form['brafton_video_public_key'] = array(
'#type' => 'textfield',
'#title' => t( 'Video Public Key' ),
'#description' => t( 'Your video Public Key.' ),
'#default_value' => variable_get( 'brafton_video_public_key' ),
'#size' => 8,
'#maxlength' => 8,
'#prefix' => '<h2>For Videos</h2>',
);
$form['brafton_video_secret_key'] = array(
'#type' => 'textfield',
'#title' => t( 'Video Secret Key' ),
'#description' => t( 'Your video Secret Key.' ),
'#default_value' => variable_get( 'brafton_video_secret_key' ),
'#size' => 36,
'#maxlength' => 36,
);
$form['brafton_video_feednum'] = array(
'#type' => 'textfield',
'#title' => t( 'Video Feed Number' ),
'#description' => t( 'Your video feed number.' ),
'#default_value' => variable_get( 'brafton_video_feednum',0 ),
'#size' => 10,
'#maxlength' => 10,
);
$form['brafton_author'] = array(
'#type' => 'select',
'#title' => t( 'Content Author' ),
'#description' => t( 'The author of the content.' ),
'#options' => $user_array,
'#default_value' => variable_get( 'brafton_author',1 ),
'#prefix' => '<h2>Import Options</h2>',
);
$form['brafton_import_date'] = array(
'#type' => 'select',
'#title' => t( 'Import Date' ),
'#description' => t( 'The date that the content is marked as having been published.' ),
'#options' => array(
'published' => 'Published Date',
'created' => 'Created Date',
'lastmodified' => 'Last Modified Date',
),
'#default_value' => variable_get( 'brafton_import_date','published' ),
);
$form['brafton_comments'] = array(
'#type' => 'select',
'#title' => t( 'Enable Comments?' ),
'#description' => t( 'Enable, Hide, or Disable Comments' ),
'#options' => array(
0 => 'Disabled',
1 => 'Hidden',
2 => 'Enabled',
),
'#default_value' => variable_get( 'brafton_comments',0 ),
);
$form['brafton_overwrite'] = array(
'#type' => 'checkbox',
'#title' => t( 'Overwrite any changes made to existing content.' ),
'#default_value' => variable_get( 'brafton_overwrite',0 ),
);
$form['brafton_published'] = array(
'#type' => 'checkbox',
'#title' => t( 'Import articles as unpublished.' ),
'#default_value' => variable_get( 'brafton_published',0 ),
);
$form['brafton_blog_page'] = array(
'#type' => 'checkbox',
'#title' => t( 'Create a News Page at "mydomain.com/news".' ),
'#default_value' => variable_get( 'brafton_blog_page',0 ),
'#prefix' => '<h2>Integration Options for Articles</h2>',
);
$form['brafton_blog_archives'] = array(
'#type' => 'checkbox',
'#title' => t( 'Create archives pages at "mydomain.com/news/archive/year/month" and an archives block.' ),
'#default_value' => variable_get( 'brafton_blog_archives',0 ),
);
$form['brafton_blog_categories'] = array(
'#type' => 'checkbox',
'#title' => t( 'Create a categories block.' ),
'#default_value' => variable_get( 'brafton_blog_categories',0 ),
);
$form['brafton_blog_headlines'] = array(
'#type' => 'checkbox',
'#title' => t( 'Create a headlines block.' ),
'#default_value' => variable_get( 'brafton_blog_headlines',0 ),
);
$form['brafton_related_articles'] = array(
'#type' => 'checkbox',
'#title' => t( 'Add related articles to Brafton posts.' ),
'#default_value' => variable_get( 'brafton_related_articles',0 ),
);
$form['brafton_video_page'] = array(
'#type' => 'checkbox',
'#title' => t( 'Create a Video Page at "mydomain.com/video".' ),
'#default_value' => variable_get( 'brafton_video_page',0 ),
'#prefix' => '<h2>Integration Options for Videos</h2>',
);
$form['brafton_video_archives'] = array(
'#type' => 'checkbox',
'#title' => t( 'Create video archives pages at "mydomain.com/video/archive/year/month" and a video archives block.' ),
'#default_value' => variable_get( 'brafton_video_archives',0 ),
);
$form['brafton_video_categories'] = array(
'#type' => 'checkbox',
'#title' => t( 'Create a video categories block.' ),
'#default_value' => variable_get( 'brafton_video_categories',0 ),
);
$form['brafton_video_headlines'] = array(
'#type' => 'checkbox',
'#title' => t( 'Create a video headlines block.' ),
'#default_value' => variable_get( 'brafton_video_headlines',0 ),
);
$form['brafton_related_videos'] = array(
'#type' => 'checkbox',
'#title' => t( 'Add related videos to Brafton videos.' ),
'#default_value' => variable_get( 'brafton_related_videos',"" ),
);
$form['brafton_video_ctas'] = array(
'#type' => 'checkbox',
'#title' => t( 'Add video CTAs' ),
'#default_value' => variable_get( 'brafton_video_ctas',0 ),
);
$form['brafton_video_pause_cta_text'] = array(
'#type' => 'textfield',
'#title' => t( 'Atlantis Pause CTA Text' ),
'#description' => t( 'Default video pause cta text every article imports' ),
'#default_value' => variable_get( 'brafton_video_pause_cta_text',"" ),
'#size' => 20,
'#maxlength' => 500,
);
$form['brafton_video_pause_cta_link'] = array(
'#type' => 'textfield',
'#title' => t( 'Atlantis Pause CTA Link' ),
'#description' => t( 'Default video pause cta link every article imports' ),
'#default_value' => variable_get( 'brafton_video_pause_cta_link',"" ),
'#size' => 20,
'#maxlength' => 500,
);
$form['brafton_video_end_cta_title'] = array(
'#type' => 'textfield',
'#title' => t( 'Atlantis End CTA Title' ),
'#description' => t( 'Default video end cta title every article imports' ),
'#default_value' => variable_get( 'brafton_video_end_cta_title',"" ),
'#size' => 20,
'#maxlength' => 140,
);
$form['brafton_video_end_cta_subtitle'] = array(
'#type' => 'textfield',
'#title' => t( 'Atlantis End CTA Subtitle' ),
'#description' => t( 'Default video end cta subtitle every article imports' ),
'#default_value' => variable_get( 'brafton_video_end_cta_subtitle',"" ),
'#size' => 20,
'#maxlength' => 140,
);
$form['brafton_video_end_cta_link'] = array(
'#type' => 'textfield',
'#title' => t( 'Atlantis End CTA Link' ),
'#description' => t( 'Default video end cta link every article imports. Requires http://' ),
'#default_value' => variable_get( 'brafton_video_end_cta_link',"" ),
'#size' => 20,
'#maxlength' => 500,
);
$form['brafton_video_end_cta_text'] = array(
'#type' => 'textfield',
'#title' => t( 'Atlantis End CTA Text' ),
'#description' => t( 'Default video end cta text every article imports' ),
'#default_value' => variable_get( 'brafton_video_end_cta_text',"" ),
'#size' => 20,
'#maxlength' => 20,
);
return system_settings_form($form);
}
/**
* Rendering of the blog page.
*/
function brafton_blog_landing( $bundle ) {
$query = new EntityFieldQuery();
$query->entityCondition( 'entity_type','node' );
$query->entityCondition( 'bundle',$bundle );
$query->propertyCondition( 'status', 1 );
$query->propertyOrderBy( 'created', 'DESC' );
$query->pager();
$result = $query->execute();
$content = '';
if ( !empty( $result ) ) {
foreach ( $result['node'] as $key => $value ) {
$node = node_view( node_load( $key ),'teaser' );
$content .= drupal_render( $node );
}
}
return $content . theme( 'pager' );
}
//Rendering of the Archives Pages
function brafton_archives_load( $bundle,$year,$month ) {
//Formats url arguments into a unix date range
$unix_start = strtotime( $year . '-' . $month );
if( $month == 12 ){
$unix_end = strtotime( ( $year + 1 ) . '-1' );
}
else {
$unix_end = strtotime( $year . '-' . ( $month + 1 ) );
}
$query = new EntityFieldQuery();
$query->entityCondition( 'entity_type','node' );
$query->entityCondition( 'bundle',$bundle );
$query->propertyCondition( 'status', 1 );
$query->propertyCondition( 'created',array($unix_start,$unix_end),'BETWEEN' );
$query->propertyOrderBy( 'created', 'DESC' );
$query->pager();
$result = $query->execute();
$content = '<h1 class="title" id="page-title">' . brafton_archives_title( $year,$month ) . '</h4>';
if ( !empty( $result ) ) {
foreach ( $result['node'] as $key => $value ) {
$node = node_view( node_load( $key ),'teaser' );
$content .= drupal_render( $node );
}
}
return $content . theme( 'pager' );
}
function brafton_archives_title( $year,$month ) {
$unix_time = mktime( 0,0,0,intval( $month ),10,intval( $year ) );
$archive_date = format_date( $unix_time,'custom','F Y' );
$title = t( 'News Archives - ' . $archive_date );
return $title;
}
//Renders the Headlines Block
function brafton_headlines( $bundle ) {
$query = new EntityFieldQuery();
$query->entityCondition( 'entity_type','node' );
$query->entityCondition( 'bundle',$bundle );
$query->propertyCondition( 'status', 1 );
$query->propertyOrderBy( 'created', 'DESC' );
$query->range(0,3);
$result = $query->execute();
$content = '<ul>';
if ( !empty( $result ) ) {
foreach ( $result['node'] as $key => $value ) {
$node = node_load( $key );
$content .= '<li>' . l( t( $node->title ),'node/' . $node->nid ) . '<br /><span class="headlines-date">' . format_date( $node->created,'custom','F j, Y' ) . '</span></li>';
}
}
$content .= '</ul>';
return $content;
}
//Renders the Categories Block
function brafton_categories( $bundle ) {
//Loads the Brafton Vocabulary Object
if ( $bundle == 'b_news' ) {
$brafton_vocabulary = taxonomy_vocabulary_machine_name_load( 'b_news_t' );
}
else {
$brafton_vocabulary = taxonomy_vocabulary_machine_name_load( 'b_news_v' );
}
$vid = $brafton_vocabulary->vid;
$query = new EntityFieldQuery();
$query->entityCondition( 'entity_type','taxonomy_term' );
$query->propertyCondition( 'vid',$vid );
$result = $query->execute();
$content = '<ul>';
if ( !empty( $result ) ) {
foreach ( $result['taxonomy_term'] as $key => $value ) {
$term = taxonomy_term_load( $key );
$content .= '<li>' . l( t( $term->name ),'taxonomy/term/' . $term->tid ) . '</li>';
}
}
$content .= '</ul>';
return $content;
}
//Renders the Archives Block.
function brafton_archives( $bundle ) {
$query = new EntityFieldQuery();
$query->entityCondition( 'entity_type','node' );
$query->entityCondition( 'bundle',$bundle );
$query->propertyCondition( 'status', 1 );
$query->propertyOrderBy( 'created', 'DESC' );
$result = $query->execute();
$date_array = array();
$content = '<ul>';
if ( !empty( $result ) ) {
foreach ( $result['node'] as $key => $value ) {
$node = node_load( $key );
$year_month = format_date( $node->created,'custom','Y' ) . format_date( $node->created,'custom','m' );
array_push( $date_array,$year_month );
}
$date_array = array_unique( $date_array );
foreach( $date_array as $value ){
$year = str_split( $value,4 );
$unix_time = mktime( 0,0,0,$year[1],10,$year[0] );
$date_string = format_date( $unix_time,'custom','F Y' );
if( $bundle == 'b_news' ) {
$content .= '<li>' . l( t( $date_string ),'news/archive/' . $year[0] . '/' . $year[1] ) . '</li>';
}
else {
$content .= '<li>' . l( t( $date_string ),'video/archive/' . $year[0] . '/' . $year[1] ) . '</li>';
}
}
}
$content .= '</ul>';
return $content;
}
//Renders the Related Posts Widget
function brafton_related_posts( &$output,$pre_render ) {
$node = $pre_render['#node'];
$language = $node->language;
$bundle = $node->type;
if( $bundle == 'b_news' ){
$content = '<h3>Related Articles</h3><ul>';
@$term_array = $node->field_brafton_term[ $language ];
$field_condition = 'field_brafton_term';
}
else{
$content = '<h3>Related Video</h3><ul>';
@$term_array = $node->field_brafton_video_term[ $language ];
$field_condition = 'field_brafton_video_term';
}
@$tid = $term_array[ array_rand( $term_array ) ]['tid'];
$query = new EntityFieldQuery();
$query->entityCondition( 'entity_type','node' );
$query->entityCondition( 'bundle',$bundle );
$query->propertyCondition( 'status', 1 );
$query->propertyOrderBy( 'created', 'DESC' );
$query->range(0,3);
$query->fieldCondition( $field_condition,'tid',$tid );
$result = $query->execute();
if ( !empty( $result ) ) {
foreach ( $result['node'] as $key => $value ) {
$node = node_load( $key );
$content .= '<li>' . l( t( $node->title ),'node/' . $node->nid ) . '<br /><span class="related-posts-date">' . format_date( $node->created,'custom','F j, Y' ) . '</span></li>';
}
}
$content .= '</ul>';
return $output . $content;
}
/**
* Helper Functions.
*/
function get_date_setting() {
//Gets the date settings from the module settings page.
$date_setting = variable_get( 'brafton_import_date' );
switch( $date_setting ) {
case 'published':
$date = 'getPublishDate';
break;
case 'created':
$date = 'getCreatedDate';
break;
case 'lastmodified':
$date = 'getLastModifiedDate';
break;
default:
$date = 'getPublishDate';
}
return $date;
}
function get_image_attributes( $articleobj,$feedtype = NULL,$photoClient = NULL,$photos = NULL,$id = NULL ) {
if( $feedtype == 'video' ) {
$thisPhotos = $photos->ListForArticle( $id,0,100 );
$photoId = $photos->Get( $thisPhotos->items[0]->id )->sourcePhotoId;
$image_info = array(
'url' => $photoClient->Photos()->GetLocationUrl( $photoId )->locationUri,
'alt' => $photos->Get( $thisPhotos->items[0]->id )->fields['caption'],
'title' => $photos->Get( $thisPhotos->items[0]->id )->fields['caption'],
);
return $image_info;
}
else {
//Grabs the image attributes from the feed.
$images = $articleobj->getPhotos();
if( !empty( $images ) ) {
$image_array = $images[0];
if( $image_array ) {
$image_large = $image_array->getLarge();
$image_info = array(
'url' => $image_large->getUrl(),
'alt' => $image_array->getAlt(),
'title' => $image_array->getCaption(),
);
return $image_info;
}
else {
$image_info = NULL;
return $image_info;
}
}
}
}
function get_category( $categories, $i ){
$name = $categories[$i]->getName();
return $name;
}
function set_article_categories( $articleobj,$bundle,$categoryObj = NULL ) {
//Grabs the categories from the feed.
if( $bundle == 'b_video' ) {
$categories = array( $categoryObj );
$vocab = 'b_news_v';
}
else {
$categories = $articleobj->getCategories();
$vocab = 'b_news_t';
}
//Checks to see if the terms already exist in the Brafton Taxonomy Vocabulary. If they do not, new terms are created.
$i = 0;
$brafton_vocabulary = taxonomy_vocabulary_machine_name_load( $vocab );
$vid = $brafton_vocabulary->vid;
$cat_array = array();
foreach($categories as $category) {
if( $bundle == 'b_video' ) {
$name = $categories[0]->name;
}
else {
$name = get_category( $categories, $i );
$i = $i + 1;
}
$check_cat = taxonomy_get_term_by_name( $name );
$found = 0;
foreach( $check_cat as $term ) {
if( $term->vid == $vid ) {
$tid = $term->tid;
$found = 1;
}
}
if($found == 0) {
$new_term = array(
'vid' => $vid,
'name' => $name,
);
$new_term = ( object ) $new_term;
taxonomy_term_save( $new_term );
$tid = $new_term->tid;
}
array_push( $cat_array,$tid );
}
//Returns an array of valid term ids for the given article.
return $cat_array;
}
function check_if_article_exists( $id,$bundle ) {
//Queries Brafton ID, and returns an array of the results, or an empty array, if there are none.
$query = new EntityFieldQuery();
$query->fieldCondition( 'field_brafton_id','value',$id,'=' );
$result = $query->execute();
return $result;
}
/*if Get author from article is selected checks for an author in the element byLine of the feed. If an author exsists checks that user author name against list of users in the db and creates them if they do not exsist. Defaults to anonymous if if no author in byline and get author from article is selected
*/
function checkAuthor($author, $byLine){
$user_id = $author;
if($user_id){
return $user_id;
}
else{
if(!empty($byLine)){
if(!($user = user_load_by_name($d_uid_name))){
//create user programatically
$password = user_password(8);
$fields = array(
'name' => $d_uid_name,
'mail' => $d_uid_name.rand().'@example.com',
'pass' => $password,
'status' => 1,
'init' => 'email address',
'roles' => array(
DRUPAL_AUTHENTICATED_RID => 'authenticated user',
),
);
$user = user_save('', $fields);
}
return $user->uid;
}
else{
return $user_id;
}
}
}
function load_article_array( $is_archive ) {
//Checks for an archive file, and if it finds one, loads the contents of the archive file into the API. Otherwise, loads the live feed. All articles are parsed from the XML into an array.
if( $is_archive ) {
$archive_file = file_load( variable_get( 'brafton_archive_file' ) );
$tmp_name = drupal_realpath( $archive_file->uri );
$article_array = NewsItem::getNewsList( $tmp_name,'html' );
variable_del( 'brafton_archive_file' );
}
else {
$api_root = variable_get( 'brafton_api_root' );
$api_key = variable_get( 'brafton_api_key' );
$summon_api = new ApiHandler( $api_key,$api_root );
$article_array = $summon_api->getNewsHTML();
}
return $article_array;
}
/**
* Implementation of hook_cron, which runs the importer and saves the articles into the Drupal database as node type b_news.
*/
function brafton_cron() {
//Gathers feed type, Api and Video Keys, and archive file information from the Brafton module settings page.
$feed_type = variable_get( 'brafton_feed_type' );
$is_api = variable_get( 'brafton_api_key' );
$is_video_public = variable_get( 'brafton_video_public_key' );
$is_video_secret = variable_get( 'brafton_video_secret_key' );
$is_archive = variable_get( 'brafton_archive_file' );
$overwrite = variable_get( 'brafton_overwrite' );
$is_published = variable_get( 'brafton_published' );
if( $feed_type == 'articles' || $feed_type == 'both' ) {
if( $is_api || $is_archive ) {
//Loads the date and overwrite settings.
$date = get_date_setting();
//Loads the article objects from the feed into an array.
$article_array = load_article_array( $is_archive );
//Loops through the article array
foreach( $article_array as $value ) {
//Checks to see if the article already exists. If it does not, a new node is created of type b_news. If it does, then depending upon the overwrite settings the existing node is either loaded, or we iterate to the next article in the feed
$id = $value->getId();
$check = check_if_article_exists( $id,'b_news' );
if( !empty( $check ) && $overwrite == 1 ) {
$nid = key($check['node']);
$node = node_load( $nid );
}
elseif( empty( $check ) ) {
$node = new stdClass();
}
else {
continue;
}
//Gets an array of image information from the feed.
$image = get_image_attributes( $value );
//Gets the article categories as an array of valid and unique term ids.
$categories = set_article_categories( $value,'b_news' );
//Instantiation of each article component as a field in the node object.
if ($is_published==1){
$node->status = 0;
}
else {
$node->status = 1;
}
$node->type = 'b_news';
$node->language = LANGUAGE_NONE;
$node->title = $value->getHeadline();
$node->uid = checkAuthor(variable_get( 'brafton_author' ), $value->getByLine());
//$node->status = 1;
$node->created = strtotime( $value->$date() );
$node->promote = 0;
$node->sticky = 0;
$node->comment = variable_get( 'brafton_comments' );
$node->body[ $node->language ][0] = array(
'value' => $value->getText(),
'summary' => $value->getExtract(),
'format' => 'full_html',
);
if ( $image ) {
$node->field_brafton_image[ $node->language ][0] = ( array ) system_retrieve_file( $image['url'],NULL,TRUE,FILE_EXISTS_REPLACE );
$node->field_brafton_image[ $node->language ][0]['alt'] = $image['alt'];
$node->field_brafton_image[ $node->language ][0]['title'] = $image['title'];
}
$node->field_brafton_id[ $node->language ][0]['value'] = $id;
//Setting the article pause cta text
//ensure categories don't get added twice
$cats=false;
$oldcats;
if($overwrite && isset($node->field_brafton_term[$node->language])){
$oldcats = $node->field_brafton_term[$node->language];
$cats=true;
}
foreach( $categories as $category ) {
if($cats){
foreach( $oldcats as $oldcat ) {
if($oldcat['tid']!=$category){
$node->field_brafton_term[ $node->language ][]['tid'] = $category;
}
}
} else $node->field_brafton_term[ $node->language ][]['tid'] = $category;
}
//end category code
node_save( $node );
taxonomy_node_insert( $node );
$nid=$node->nid;
$alias = drupal_get_path_alias("node/" . $nid);
}
}
}
if( $feed_type == 'videos' || $feed_type == 'both' ) {
if( $is_video_public && $is_video_secret ) {
$domain = variable_get( 'brafton_api_root' );
switch ($domain) {
case 'http://api.brafton.com':
$baseURL = 'http://livevideo.api.brafton.com/v2/';
$photoURI = "http://pictures.brafton.com/v2/";
break;
case 'http://api.contentlead.com':
$baseURL = 'http://livevideo.api.contentlead.com/v2/';
$photoURI = "http://pictures.contentlead.com/v2/";
break;
case 'http://api.castleford.com.au':
$baseURL = 'http://livevideo.api.castleford.com.au/v2/';
$photoURI = "http://pictures.castleford.com.au/v2/";
break;
default:
$baseURL = 'http://livevideo.api.brafton.com/v2/';
$photoURI = "http://pictures.brafton.com/v2/";
break;
}
$videoClient = new AdferoVideoClient( $baseURL,$is_video_public,$is_video_secret );
$videoOutClient = $videoClient->videoOutputs();
$client = new AdferoClient( $baseURL,$is_video_public,$is_video_secret );
$photoClient = new AdferoPhotoClient( $photoURI );
$photos = $client->ArticlePhotos();
$feeds = $client->Feeds();
$feedList = $feeds->ListFeeds( 0,10 );
$feedNum = variable_get( 'brafton_video_feednum' );
$articles = $client->Articles();
$articleList = $articles->ListForFeed( $feedList->items[ $feedNum ]->id,'live',0,100 );
$sitemap=array();
foreach( $articleList->items as $value ) {
$id = $value->id;
$categories = $client->Categories();
$check = check_if_article_exists( $id,'b_video' );
if( !empty( $check ) && $overwrite == 1 ) {
$nid = key($check['node']);
$node = node_load( $nid );
}
elseif( empty( $check ) ) {
$node = new stdClass();
}