-
Notifications
You must be signed in to change notification settings - Fork 0
/
olivia_common.news_helper.inc
147 lines (122 loc) · 5.5 KB
/
olivia_common.news_helper.inc
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
<?php
/**
* Created by PhpStorm.
* User: dennis
*/
function olivia_common_get_news_data()
{
// set feed URL
$variables_array = array();
$url = 'http://www.mynewsdesk.com/services/pressroom/list/ac0923e8dc2db44f1a4a8a4b636e2507/?type_of_media=news&limit=100&offset=0&order=published&format=json';
$content = file_get_contents($url);
if ($content) {
$json = json_decode($content, true);
$total_count = $json['items']['total_count'];
$limit = 100;
$ItemsPerRequest = 100;
if ($total_count > $limit)
{
$total_requests = intval($total_count/$limit);
//add one more in requests
if($total_count % $limit > 0) {
$total_requests = $total_requests + 1;
}
for($i=1; $i<=$total_requests; $i++)
{
$offset = ($i - 1) * $ItemsPerRequest;
$url = "http://www.mynewsdesk.com/services/pressroom/list/ac0923e8dc2db44f1a4a8a4b636e2507/?type_of_media=news&limit=".$limit."&offset=".$offset."&order=published&format=json";
$content = file_get_contents($url);
$json = json_decode($content, true);
$variables_array = array_merge($variables_array, get_news_variables_array($json));
}
}
else
{
$variables_array = get_news_variables_array($json);
}
} else {
watchdog('mynewsdesk', 'The import from MyNewsdesk could not find any data.', 'error');
}
return isset($variables_array) ? $variables_array : array();
}
function get_news_variables_array($json)
{
//Loop through all the members of the Item array
foreach($json['items']['item'] as $key => $Item)
{
//Now you can access the 'row' data using $Item in this case
$tags = isset($Item['tags']) ? fetch_news_values($Item['tags']) : array();
$subjects = isset($Item['subjects']) ? fetch_news_values($Item['subjects']) : array();
$related_items = isset($Item['related_items']) ? fetch_news_values($Item['related_items']) : array();
$variables_array[$key] = array(
"id" => $Item['id'],
"url" => $Item['url'],
"published_at" => strtotime($Item['published_at']),
"created_at" => strtotime($Item['created_at']),
"updated_at" => strtotime($Item['updated_at']),
"header" => $Item['header'],
"summary" => str_replace('<br>', '', str_replace('<br />', '', $Item['summary'])),
"body" => $Item['body'],
"image" => isset($Item['image']) ? $Item['image'] : '',
"image_small" => isset($Item['image_small']) ? $Item['image_small'] : '',
"image_medium" => isset($Item['image_medium']) ? $Item['image_medium'] : '',
"image_thumbnail_large" => isset($Item['image_thumbnail_large']) ? $Item['image_thumbnail_large'] : '',
"image_thumbnail_medium" => isset($Item['image_thumbnail_medium']) ? $Item['image_thumbnail_medium'] : '',
"image_thumbnail_small" => isset($Item['image_thumbnail_small']) ? $Item['image_thumbnail_medium'] : '',
"tags" => $tags,
"subjects" => $subjects,
"contact_people" => array("contact_person" => fetch_news_contact_people($Item['contact_people'])),
"related_items" => $related_items
);
if (count($related_items) > 0) {
foreach($related_items as $r) {
if ($r['type_of_media'] == 'contact_person') {
$has_nid = intval(db_query("SELECT n.nid FROM {node} n INNER JOIN {field_data_field_contact_person_id} c ON c.entity_id = n.nid AND c.bundle = 'staff' WHERE c.field_contact_person_id_value = " . $r['item_id'])->fetchField());
if ($has_nid > 0) {
$variables_array[$key]['contact_people_nids'][] = $has_nid;
}
}
}
}
}
return isset($variables_array) ? $variables_array : array();
}
function fetch_news_contact_people($contacts)
{
foreach($contacts as $contact)
{
$contacts_array = array(
"id" => $contact['id'],
"url" => $contact['url'],
"published_at" => $contact['published_at'],
"created_at" => $contact['created_at'],
"updated_at" => $contact['updated_at'],
"position" => $contact['position'],
"name" => $contact['name'],
"title" => $contact['title'],
"specialist" => $contact['specialist'],
"phone" => $contact['phone'],
"phone_alternative" => $contact['phone_alternative'],
"email" => $contact['email'],
"pressroom_contact" => $contact['pressroom_contact'],
"summary" => $contact['summary'],
"body" => $contact['body'],
"image" => $contact['image'],
"image_small" => $contact['image_small'],
"image_medium" => $contact['image_medium'],
"image_thumbnail_large" => $contact['image_thumbnail_large'],
"image_thumbnail_medium" => $contact['image_thumbnail_medium'],
"image_thumbnail_small" => $contact['image_thumbnail_small']
);
}
return isset($contacts_array) ? $contacts_array : array();
}
function fetch_news_values($values)
{
foreach($values as $key => $value)
{
$values_array[$key] = $value;
}
return $values_array;
}
?>