gary
11/8/2018 - 2:39 PM

Joomla Create News from app

Joomla Create News from app

<?php

    class newsCron {

        var $externalStart;

        public function __construct($config) {
            $news = $this->fetchNews();
            if ($news) {
                $this->storeNews($news, $config);
            }
        }

        private function fetchNews() {
            $url = "https://webapi.myfitapp.de/news/list/28588";
            $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $result = json_decode(curl_exec($curl));
            curl_close($curl);
            if (is_object($result) && property_exists($result, "data")) {
                if (property_exists($result, "config") && is_object($result->config) && property_exists($result->config, "baseUrl")) {
                    $this->externalStart = $result->config->baseUrl;
                }
                return $result->data;
            }
            return false;
        }

        private function setDb($config) {
            $db = new mysqli($config->host, $config->user, $config->password, $config->db);
            $db->set_charset("utf8");
            return $db;
        }

        private function storeNews($news, $config) {
            $db = $this->setDb($config);
            $dates = array(
                'dateCreated',
                'lastUpdated',
                'publishOn',
                'publishUntil'
            );
            $fields = array(
                'app_id' => 'id',
                'title' => 'title',
                'alias' => 'alias',
                'url' => 'url',
                'introtext' => 'shortDesc',
                'fulltext' => 'content',
                'images' => 'featuredImg',
                'created' => 'dateCreated',
                'modified' => 'lastUpdated',
                'publish_up' => 'publishOn',
                'publish_down' => 'publishUntil'
            );
            $startSql = "INSERT INTO lw2_news_posts (`app_id`, `title`, `alias`, `url`, `introtext`, `fulltext`, `images`, `created`, `modified`, `publish_up`, `publish_down`, `state`) VALUES ";
            foreach($news as $article) {
                if (property_exists($article, 'featuredImg') && $article->featuredImg) {
                    $orig = $article->featuredImg;
                    $article->featuredImg = $this->externalStart.$article->featuredImg;
                    if (preg_match('/'.preg_quote($orig, '/').'/', $article->content)) {
                        $article->content = preg_replace('/'.preg_quote($orig, '/').'/', $article->featuredImg, $article->content);
                        $article->featuredImg = false;
                    }
                }
                if (property_exists($article, 'title')) {
                    $article->alias = preg_replace('/(\s)/', '-', $article->title);
                } else {
                    $article->alias = '';
                }
                $sql = $startSql . "(";
                foreach($fields as $key => $field) {                    
                    if (property_exists($article, $field) && $article->$field) {
                        if (in_array($field, $dates) && $article->$field) {
                            $article->$field = date('Y-m-d H:i:s', strtotime($article->$field));                            
                        }
                        $sql .= "'{$article->$field}',";
                    } else {
                        $sql .= "'',";
                    }
                }
                $state = 0;
                if (property_exists($article, "publishOn")) {
                    if (strtotime($article->publishOn) <= strtotime('now') && (!property_exists($article, 'publishUntil') || !$article->publishUntil)) {
                        $sql .= "'1',";
                        $state = 1;
                    }
                }
                $sql = substr($sql, 0, -1) . ") ON DUPLICATE KEY UPDATE ";
                foreach($fields as $key => $field) {
                    if (property_exists($article, $field) && $article->$field) {                 
                        $sql .= "`$key`='{$article->$field}',";
                    }
                }
                $sql .= "`state`='$state',";
                $sql = substr($sql, 0, -1);
                if ($db->query($sql) !== true) {
                    echo print_r($db->error);
                }
            }
            
        }

    }

    require_once __DIR__.'/../configuration.php';

    new newsCron(new JConfig());