aboutsummaryrefslogtreecommitdiff
path: root/bridges/BMDSystemhausBlogBridge.php
blob: 98fb2d63553624c0dfd7ac2e17297a9cf6ddb0b2 (plain) (blame)
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
<?php

class BMDSystemhausBlogBridge extends BridgeAbstract
{
    const MAINTAINER = 'cn-tools';
    const NAME = 'BMD SYSTEMHAUS GesmbH';
    const CACHE_TIMEOUT = 21600; //6h
    const URI = 'https://www.bmd.com';
    const DONATION_URI = 'https://paypal.me/cntools';
    const DESCRIPTION = 'BMD Systemhaus - We make business easy';
    const BMD_FAV_ICON = 'https://www.bmd.com/favicon.ico';

    const ITEMSTYLE = [
        'ilcr' => '<table width="100%"><tr><td style="vertical-align: top;">{data_img}</td><td style="vertical-align: top;">{data_content}</td></tr></table>',
        'clir' => '<table width="100%"><tr><td style="vertical-align: top;">{data_content}</td><td style="vertical-align: top;">{data_img}</td></tr></table>',
        'itcb' => '<div>{data_img}<br />{data_content}</div>',
        'ctib' => '<div>{data_content}<br />{data_img}</div>',
        'co' => '{data_content}',
        'io' => '{data_img}'
    ];

    const PARAMETERS = [
        'Blog' => [
            'country' => [
                'name' => 'Country',
                'type' => 'list',
                'values' => [
                    'Österreich' => 'at',
                    'Deutschland' => 'de',
                    'Schweiz' => 'ch',
                    'Slovensko' => 'sk',
                    'Cesko' => 'cz',
                    'Hungary' => 'hu',
                ],
                'defaultValue' => 'at',
            ],
            'style' => [
                'name' => 'Style',
                'type' => 'list',
                'values' => [
                    'Image left, content right' => 'ilcr',
                    'Content left, image right' => 'clir',
                    'Image top, content bottom' => 'itcb',
                    'Content top, image bottom' => 'ctib',
                    'Content only' => 'co',
                    'Image only' => 'io',
                ],
                'defaultValue' => 'ilcr',
            ]
        ]
    ];

    //-----------------------------------------------------
    public function collectData()
    {
        // get website content
        $html = getSimpleHTMLDOM($this->getURI());

        // Convert relative links in HTML into absolute links
        $html = defaultLinkTo($html, self::URI);

        // Convert lazy-loading images and frames (video embeds) into static elements
        $html = convertLazyLoading($html);

        foreach ($html->find('div#bmdNewsList div#bmdNewsList-Item') as $element) {
            $itemScope = $element->find('div[itemscope=itemscope]', 0);

            $item = [];

            // set base article data
            $item['title'] = $this->getMetaItemPropContent($itemScope, 'headline');
            $item['timestamp'] = strtotime($this->getMetaItemPropContent($itemScope, 'datePublished'));
            $item['author'] = $this->getMetaItemPropContent($itemScope->find('div[itemprop=author]', 0), 'name');

            // find article image
            $imageTag = '';
            $image = $element->find('div.mediaelement.mediaelement-image img', 0);
            if ((!is_null($image)) and ($image->src != '')) {
                $item['enclosures'] = [$image->src];
                $imageTag = '<img src="' . $image->src . '"/>';
            }

            // begin with right style
            $content = self::ITEMSTYLE[$this->getInput('style')];

            // render placeholder
            $content = str_replace('{data_content}', $this->getMetaItemPropContent($itemScope, 'description'), $content);
            $content = str_replace('{data_img}', $imageTag, $content);

            // set finished content
            $item['content'] = $content;

            // get link to article
            $link = $element->find('div#bmdNewsList-Text div#bmdNewsList-Title a', 0);
            if (!is_null($link)) {
                $item['uri'] = $link->href;
            }

            // init categories
            $categories = [];
            $tmpOne = [];
            $tmpTwo = [];

            // search first categorie span
            $catElem = $element->find('div#bmdNewsList-Text div#bmdNewsList-Category span.news-list-category', 0);
            $txt = trim($catElem->innertext);
            $tmpOne = explode('/', $txt);

            // split by 2 spaces
            foreach ($tmpOne as $tmpElem) {
                $tmpElem = trim($tmpElem);
                $tmpData = preg_split('/  /', $tmpElem);
                $tmpTwo = array_merge($tmpTwo, $tmpData);
            }

            // split by tabulator
            foreach ($tmpTwo as $tmpElem) {
                $tmpElem = trim($tmpElem);
                $tmpData = preg_split('/\t+/', $tmpElem);
                $categories = array_merge($categories, $tmpData);
            }

            // trim each categorie entries
            $categories = array_map('trim', $categories);

            // remove empty entries
            $categories = array_filter($categories, function ($value) {
                return !is_null($value) && $value !== '';
            });

            // set categories
            if (count($categories) > 0) {
                $item['categories'] = $categories;
            }

            // add item
            if (($item['title'] != '') and ($item['content'] != '') and ($item['uri'] != '')) {
                $this->items[] = $item;
            }
        }
    }

    //-----------------------------------------------------
    public function detectParameters($url)
    {
        try {
            $parsedUrl = Url::fromString($url);
        } catch (UrlException $e) {
            return null;
        }

        if (!in_array($parsedUrl->getHost(), ['www.bmd.com', 'bmd.com'])) {
            return null;
        }

        $lang = '';

        // extract language from url
        $path = explode('/', $parsedUrl->getPath());
        if (count($path) > 1) {
            $lang = $path[1];

            // validate data
            if ($this->getURIbyCountry($lang) == '') {
                $lang = '';
            }
        }

        // if no country available, find language by browser
        if ($lang == '') {
            $srvLanguages = explode(';', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
            if (count($srvLanguages) > 0) {
                $languages = explode(',', $srvLanguages[0]);
                if (count($languages) > 0) {
                    for ($i = 0; $i < count($languages); $i++) {
                        $langDetails = explode('-', $languages[$i]);
                        if (count($langDetails) > 1) {
                            $lang = $langDetails[1];
                        } else {
                            $lang = substr($srvLanguages[0], 0, 2);
                        }

                        // validate data
                        if ($this->getURIbyCountry($lang) == '') {
                            $lang = '';
                        }

                        if ($lang != '') {
                            break;
                        }
                    }
                }
            }
        }

        // if no URL found by language, use AT as default
        if ($this->getURIbyCountry($lang) == '') {
            $lang = 'at';
        }

        $params = [];
        $params['country'] = strtolower($lang);

        return $params;
    }

    //-----------------------------------------------------
    public function getURI()
    {
        $country = $this->getInput('country') ?? '';
        $lURI = $this->getURIbyCountry($country);
        return $lURI != '' ? $lURI : parent::getURI();
    }

    //-----------------------------------------------------
    public function getIcon()
    {
        return self::BMD_FAV_ICON;
    }

    //-----------------------------------------------------
    private function getMetaItemPropContent($elem, $key)
    {
        if (($key != '') and (!is_null($elem))) {
            $metaElem = $elem->find('meta[itemprop=' . $key . ']', 0);
            if (!is_null($metaElem)) {
                return $metaElem->getAttribute('content');
            }
        }

        return '';
    }

    //-----------------------------------------------------
    private function getURIbyCountry($country)
    {
        switch (strtolower($country)) {
            case 'at':
                return 'https://www.bmd.com/at/ueber-bmd/blog-ohne-filter.html';
            case 'de':
                return 'https://www.bmd.com/de/das-ist-bmd/blog.html';
            case 'ch':
                return 'https://www.bmd.com/ch/das-ist-bmd/blog.html';
            case 'sk':
                return 'https://www.bmd.com/sk/firma/blog.html';
            case 'cz':
                return 'https://www.bmd.com/cz/firma/news-blog.html';
            case 'hu':
                return 'https://www.bmd.com/hu/rolunk/hirek.html';
            default:
                return '';
        }
    }
}