<?php
session_start([0]);
ini_set('display_errors', 1);ini_set('display_startup_errors', 1);error_reporting(E_ALL);
class MainFunction
{
    function __construct()
    {
        
        $host2 = "localhost";
        $user2 = "angeeoyl_oracle_card";        $pw2 = "h$+a[1;{Daeu";        $db2 = "angeeoyl_oracle_card";
        $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 'https://www.angelicprayer.com/';
    }

     function horoscope_sign_menus()
    {
        $horoscope_signs = $this->db->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;
    }

    function checkAuth($username, $password)
    {
        $password = md5($password);
        $to = $this->db2->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)
    {
        // echo "<pre>"; print_r($_POST);die;
        
 // echo "<pre>"; print_r($url);die;
        $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($table,$id)
    {
        $this->db2->query("delete from $table where id = '$id' ");
        return true;
    }

    function update_card($data,$id)
    {
        // $url = $this->Slug($url);
		if (!empty($id)) {
            $stmt = $this->db2->prepare("update cards set general = ?, love = ? , finance = ? , health = ? where id = ? ");
            $general = $data['general'];
            $love = $data['love'];
            $finance = $data['finance'];
            $health = $data['health'];
            return $stmt->execute([$general,$love,$finance,$health, $id]);
        }
    }
	function update_advise($data,$id)
    {
        // $url = $this->Slug($url);
        ///echo "<pre>";print_r($data);die;
		$title = $data['title'];
		$slug = $this->Slug($title);
        $ylink = $data['ylink'];
        if (!empty($id)) {
            $stmt = $this->db2->prepare("update advise_videos set title = ?,slug = ?,ylink = ? where id = ? ");
            $ylink = $data['ylink'];
            $stmt->execute([$title,$slug,$ylink, $id]);
        }
    }
	function add_advise($data)
    {
        $title = $data['title'];
		$slug = $this->Slug($title);
        $ylink = $data['ylink'];
        if (!empty($ylink)) {
            $stmt = $this->db2->prepare("insert into advise_videos (title,slug,ylink) values(?,?,?)");
            return $stmt->execute([$title,$slug,$ylink]);
        }
    }

    function getArticleById($id)
    {
        return $this->db->query("select * from articles where id = '$id' ")->fetch(PDO::FETCH_ASSOC);
    }
	
	function getRowById($table,$id)
    {
        return $this->db2->query("select * from $table where id = '$id' ")->fetch(PDO::FETCH_ASSOC);
    }
	
	function getCardById($id)
    {
        return $this->db2->query("select * from cards where id = '$id' ")->fetch(PDO::FETCH_ASSOC);
    }
	
	function getAdviseById($id)
    {
        return $this->db2->query("select * from advise_videos 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 getAllCards()
    {
        return $this->db2->query('select * from cards order by id desc')->fetchAll(PDO::FETCH_ASSOC);
    }
	
	function getAllAdvises()
    {
        return $this->db2->query('select * from advise_videos 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 randomAdviseVideo()
    {
        return $this->db2->query("SELECT * FROM advise_videos ORDER BY RAND() LIMIT 0,1")->fetchAll(PDO::FETCH_ASSOC);
    }	
	function getCardBySlug($slug)    {	        
	
		return $this->db2->query("SELECT * FROM cards where slug = '$slug'")->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]
            ];
        }
        // echo "<pre>"; print_r($newArticle); die;
        return $newArticle;
    }

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

$use = new MainFunction();
