<?php
session_start([0]);

class MainFunction
{
    function __construct()
    {
        $host = "localhost";
        $user = "root";
        $pw = "";
        $db = "universe_message";
        $this->db = new PDO("mysql:$host", $user, $pw);
        $this->db->exec("set names utf8");
        $this->db->exec("CREATE DATABASE IF NOT EXISTS $db");
        $this->db->exec("USE $db");


        $host2 = "localhost";
        $user2 = "root";
        $pw2 = "";
        $db2 = "trusted_tellers";
        $this->db2 = new PDO("mysql:$host2", $user2, $pw2);
        $this->db2->exec("set names utf8");
        $this->db2->exec("CREATE DATABASE IF NOT EXISTS $db2");
        $this->db2->exec("USE $db2");
    }

    function base_url(){
        return 'http://localhost/astrologyvalley/';
    }

     function horoscope_sign_menus()
    {
        $horoscope_signs = $this->db2->query("SELECT horoscope_sign.*, upload_images.file FROM `horoscope_sign` INNER JOIN upload_images ON upload_images.id=horoscope_sign.file_id;")->fetchAll(PDO::FETCH_ASSOC);

        return $horoscope_signs;die;
    }

    function checkAuth($username, $password)
    {
        $password = md5($password);
        $to = $this->db->query("SELECT * FROM `users` WHERE `username` ='$username' AND `password` ='$password'  ");
        if ($to->rowCount() > 0) {
            $_SESSION['admin'] = $to->fetch(PDO::FETCH_ASSOC);
            return "ok";
        } else {
            return "bad";
        }
    }

    function store($title, $image)
    {
        $url = $this->Slug($title);

        $stmt = $this->db->prepare("insert into articles (title,image,url) values (?,?,?) ");
        if (!empty($image['tmp_name'])) {
            $imageName = date('Y-m-d') . "-" . $image['name'];
            $path = "../images/thumb/";
            move_uploaded_file($image['tmp_name'], $path . $imageName);
            $stmt->execute([$title, $imageName,$url]);
        } else {
            $stmt->execute([$title, null,$url]);
        }

    }

    function Slug($string)
    {
        return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
    }

    function delete($id)
    {
        $this->db->query("delete from articles where id = '$id' ");
        return true;
    }

    function update($title, $image, $id)
    {
        $url = $this->Slug($title);

        if (!empty($image['tmp_name'])) {
            $stmt = $this->db->prepare("update articles set title = ?, url = ? , image = ? where id = ? ");
            $imageName = date('Y-m-d') . "-" . $image['name'];
            $path = "../images/thumb/";
            move_uploaded_file($image['tmp_name'], $path . $imageName);
            $stmt->execute([$title, $url, $imageName, $id]);
        } else {
            $stmt = $this->db->prepare("update articles set title = ?,url = ? where id = ? ");
            $stmt->execute([$title,$url, $id]);
        }
    }

    function getArticleById($id)
    {
        return $this->db->query("select * from articles where id = '$id' ")->fetch(PDO::FETCH_ASSOC);
    }

    function getArticleByUrl($url)
    {
        return $this->db->query("select * from articles where url = '$url' ")->fetch(PDO::FETCH_ASSOC);
    }

    function getAllArticles()
    {
        return $this->db->query('select * from articles order by id desc')->fetchAll(PDO::FETCH_ASSOC);
    }

    function angels()
    {
        $data = [
           'death', 
            'judgement',
            'justice', 
            'strength',
            'temperance',
            'the-chariot',
            'the-devil',
            'the-emperor',
            'the-empress',
            'the-fool',
            'the-hanged-man',
            'the-hermit',
            'the-hierophant',
            'the-high-priestess',
            'the-lovers',
            'the-magician',
            'the-moon',
            'the-star',
            'the-sun',
            'the-tower',
            'the-world',
            'wheel-of-fortune',           
        ];
        return $data;
    }

    function randomArticleSeven()
    {
        return $this->db->query("SELECT * FROM articles ORDER BY RAND() LIMIT 0,1")->fetchAll(PDO::FETCH_ASSOC);
    }

    function popularArticles()
    {
        return $this->db->query("SELECT * FROM articles ORDER BY articles.views desc LIMIT 0,3")->fetchAll(PDO::FETCH_ASSOC);
    }

    function randomArticleOne()
    {
        return $this->db->query("SELECT * FROM articles ORDER BY RAND() LIMIT 1")->fetch(PDO::FETCH_ASSOC);
    }
    function mergeArray($angels,$articles){
        $newArticle = [];
        foreach ($articles as $key => $val){
            $newArticle[] = [
                'id' => $val['id'],
                'title' => $val['title'],
                'content' => $val['content'],
                'image' => $val['image'],
                'url' => $val['url'],
                'angel' => $angels[$key]
            ];
        }
        return $newArticle;
    }

    function updateView($id, $views)
    {
        $stmt = $this->db->prepare("update articles set views = ? where id = ? ");
        $stmt->execute([$views,$id]);
    }
}

$use = new MainFunction();
