/
BraftonImporter.php
363 lines (327 loc) · 14.5 KB
/
BraftonImporter.php
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
<?php
function BraftonArticleImporter(){
$errors = new BraftonErrorReport(variable_get('brafton_api_key'), variable_get( 'brafton_api_root' ), (bool)variable_get('brafton_debug_mode') );
//Gathers feed type, Api and Video Keys, and archive file information from the Brafton module settings page.
$import_list = array();
$feed_type = variable_get( 'brafton_feed_type' );
$is_api = variable_get( 'brafton_api_key' );
$is_archive = variable_get( 'brafton_archive_file' );
$overwrite = variable_get( 'brafton_overwrite' );
$is_published = variable_get( 'brafton_published' );
$counter = 0;
//Define default fields for b_news type_url_form_media
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 );
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.
$node->status = $is_published == 1? 0: 1;
$types = array(
'body' => 'body',
'image' => 'field_brafton_image',
'tax' => 'field_brafton_term'
);
if(variable_get('brafton_existing_type') != 'b_news'){
$types['body'] = variable_get('brafton_custom_body');
$types['image'] = variable_get('brafton_custom_image');
$types['tax'] = variable_get('brafton_custom_taxonomy');
}
$node->type = variable_get('brafton_existing_type');
$node->language = LANGUAGE_NONE;
$node->title = $value->getHeadline();
$import_list['title'][] = $node->title;
$node->uid = checkAuthor(variable_get( 'brafton_author' ), $value->getByLine());
//$nodestatus = 1;
$node->created = strtotime( $value->$date() );
$node->updated = $node->created;
$node->promote = 0;
$node->sticky = 0;
$node->comment = variable_get( 'brafton_comments' );
$node->{$types['body']}[ $node->language ][0] = array(
'value' => $value->getText(),
'summary' => $value->getExtract(),
'format' => 'full_html',
);
if ( $image ) {
$node->{$types['image']}[ $node->language ][0] = ( array ) system_retrieve_file( $image['url'],NULL,TRUE,FILE_EXISTS_REPLACE );
$node->{$types['image']}[ $node->language ][0]['alt'] = $image['alt'];
$node->{$types['image']}[ $node->language ][0]['title'] = $image['title'];
}
if(field_info_instance('node', 'field_brafton_id', $node->type) == NULL){
$brafton_id_field = array(
'field_name' => 'field_brafton_id',
'entity_type' => 'node',
'bundle' => $node->type,
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'hidden',
)
)
);
field_create_instance($brafton_id_field);
}
$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->{$types['tax']}[$node->language])){
$oldcats = $node->{$types['tax']}[$node->language];
$cats=true;
}
foreach( $categories as $category ) {
if($cats){
foreach( $oldcats as $oldcat ) {
if($oldcat['tid']!=$category){
$node->{$types['tax']}[ $node->language ][]['tid'] = $category;
}
}
} else $node->{$types['tax']}[ $node->language ][]['tid'] = $category;
}
//end category code
node_save( $node );
var_dump($node);
taxonomy_node_insert( $node );
$nid=$node->nid;
$alias = drupal_get_path_alias("node/" . $nid);
$counter++;
++$errors->level;
}
}
$import_list['counter'] = $counter;
return $import_list;
}
function BraftonVideoImporter(){
if(!isset($errors)){
$errors = new BraftonErrorReport(variable_get('brafton_api_key'), variable_get( 'brafton_api_root' ),(bool)variable_get('brafton_debug_mode') );
}else{
$errors->level = 1;
}
//Gathers feed type, Api and Video Keys, and archive file information from the Brafton module settings page.
$import_list = array();
$feed_type = variable_get( 'brafton_feed_type' );
$is_video_public = variable_get( 'brafton_video_public_key' );
$is_video_secret = variable_get( 'brafton_video_secret_key' );
$overwrite = variable_get( 'brafton_overwrite' );
$is_published = variable_get( 'brafton_published' );
if( $is_video_public && $is_video_secret ) {
$domain = 'http://' . 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();
}
else {
continue;
}
$thisArticle = $client->Articles()->Get( $id );
if( $categories->ListForArticle( $id,0,100 )->items ) {
$categoryId = $categories->ListForArticle( $id,0,100 )->items[0]->id;
$category = $categories->Get( $categoryId );
$categories = set_article_categories( $value,'b_video',$category );
}
$presplash = $thisArticle->fields['preSplash'];
$postsplash = $thisArticle->fields['postSplash'];
$cta_option = variable_get( 'brafton_video_ctas' );
$pause_cta_text = variable_get( 'brafton_video_pause_cta_text' );
$pause_cta_link = variable_get( 'brafton_video_pause_cta_link' );
$end_cta_title = variable_get( 'brafton_video_end_cta_title' );
$end_cta_subtitle = variable_get( 'brafton_video_end_cta_subtitle' );
$end_cta_link = variable_get( 'brafton_video_end_cta_link' );
$end_cta_text = variable_get( 'brafton_video_end_cta_text' );
$videoList=$videoOutClient->ListForArticle($id,0,10);
$list=$videoList->items;
$embedCode = sprintf( "<video id='video-%s' class=\"ajs-default-skin atlantis-js\" controls preload=\"auto\" width='512' height='288' poster='%s' >", $id, $presplash );
foreach($list as $listItem){
$output=$videoOutClient->Get($listItem->id);
$type = $output->type;
$path = $output->path;
$resolution = $output->height;
$source = generate_source_tag( $path, $resolution );
$embedCode .= $source;
}
$embedCode .= '</video>';
$script = '<script type="text/javascript">';
$script .= 'var atlantisVideo = AtlantisJS.Init({';
$script .= 'videos: [{';
$script .='id: "video-' . $id . '"';
if($cta_option){
$marpro = '';
$pause_asset_id = variable_get('brafton_video_pause_cta_asset_gateway_id');
if($pause_asset_id != ''){
$marpro = "assetGateway: { id: '$pause_asset_id' },";
}
$endingBackground = '';
$end_background_image = variable_get('brafton_video_end_cta_background_url');
if($end_background_image != ''){
$end_background_image = file_create_url($end_background_image);
$endingBackground = "background: '$end_background_image',";
}
$end_asset_id = variable_get('brafton_video_end_cta_asset_gateway_id');
if($end_asset_id != ''){
$endingBackground .= "assetGateway: { id: '$end_asset_id' },";
}
$buttonImage = '';
$button_image_url = variable_get('brafton_video_end_cta_button_image_url');
if($button_image_url != ''){
$button_image_url = file_create_url($button_image_url);
$buttonImage = "image: '$button_image_url',";
}
$button_image_postition = variable_get('brafton_video_end_cta_button_placement');
if($button_image_postition){
switch($button_image_postition){
case 'tl':
$postion = '{pos: "top", val: "15px"},{pos: "left", val: "15px"}';
break;
case 'tr':
$postion = '{pos: "top", val: "15px"},{pos: "right", val: "15px"}';
break;
case 'br':
$postion = '{pos: "bottom", val: "15px"},{pos: "right", val: "15px"}';
break;
case 'bl':
$postion = '{pos: "bottom", val: "15px"},{pos: "lelft", val: "15px"}';
}
$buttonImage .= "position: [ " . $postion . " ]";
}
$script .=',';
$script .= <<<EOT
pauseCallToAction: {
$marpro
link: "$pause_cta_link",
text: "$pause_cta_text"
},
endOfVideoOptions: {
$endingBackground
callToAction: {
title: "$end_cta_title",
subtitle: "$end_cta_subtitle",
button: {
link: "$end_cta_link",
text: "$end_cta_text",
$buttonImage
}
}
}
EOT;
}
$script .= '}]';
$script .= '});';
$script .= '</script>';
$embedCode .= $script;
//Wraps a Div around the embed code
$embed_code = "<div id='post-single-video'>" . $embedCode . "</div>";
//Gets the image data from the feed
$photoCheckId = $photos->ListForArticle( $id,0,100 );
if($photoCheckId->items[0]->id){
$image = get_image_attributes( NULL,'video',$photoClient,$photos,$id );
}else{
$image = array('url' => '','alt' => '','title' => '',);
}
//Creates the video node and inserts the values from the feed
$node->type = 'b_video';
$node->language = LANGUAGE_NONE;
$node->title = $thisArticle->fields['title'];
$import_list[]['title'] = $node->title;
$node->uid = variable_get( 'brafton_author' );
$node->status = $is_published == 1? 0: 1;;
$node->created = strtotime( $thisArticle->fields['lastModifiedDate'] );
$node->promote = 0;
$node->sticky = 0;
$node->comment = variable_get( 'brafton_comments' );
$node->body[ $node->language ][0] = array(
'value' => $thisArticle->fields['content'],
'summary' => $thisArticle->fields['extract'],
'format' => 'full_html',
);
$node->field_brafton_video[ $node->language ][0] = array(
'value' => $embed_code,
'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;
if( $categories ) {
foreach( $categories as $category ) {
$node->field_brafton_video_term[ $node->language ][]['tid'] = $category;
}
}
node_save( $node );
taxonomy_node_insert( $node );
$nid=$node->nid;
$alias = drupal_get_path_alias("node/" . $nid);
$sitemap_url = $GLOBALS['base_url'].'/'.$alias;
$sitemapaddition = array(
"url" => $sitemap_url,
"location" => $path,
"title" => $node->title,
"thumbnail" => $presplash,
"description" =>$thisArticle->fields['content'],
"publication" =>$thisArticle->fields['lastModifiedDate'],
);
$sitemap[]=$sitemapaddition;
++$errors->level;
}
}
brafton_add_URLs($sitemap);
return $import_list;
}