<?php

// error_reporting(E_ALL); // Report all PHP errors
// ini_set('display_errors', '1'); // Display errors in the browser
// ini_set('display_startup_errors', '1'); 

ini_set('max_execution_time', 300);
ini_set('memory_limit', -1);

ob_start();

    // specify your own database credentials
    $host = "localhost";
    $db_name = "wp0075397";
    $username = "wpuser15904";
    $password = "NYhu7fjlB8H0uCa";
    $conn;
    
    try{
        $conn = new PDO("mysql:host=" . $host . ";dbname=" . $db_name, $username, $password);
        $conn->exec("set names utf8");
    }catch(PDOException $exception){
        echo "Connection error: " . $exception->getMessage();
    }

    function translation_list(){ //Default last updated
        //Check locale and change accordingly to the right DB prefix for that locale
        $locale = "fr";

        $prefix = get_blog_by_locale( $locale );
        
        $sql = "SELECT  
                P.ID,
                P.post_title AS title,
                P.post_date AS cdate,
                P.post_name AS slug,
                P.post_content AS content,
                P.post_status AS status,
                MD.activity AS duration,
                MD.activity AS activity,
                MD.city AS city,
                MD.state AS state,
                MD.country AS country,
                MD.private AS private,
                PMA.meta_value AS thumbnail
                FROM
                    ".$prefix."posts P
                LEFT JOIN wp_messagedata MD ON
                    P.id = MD.post_id
                LEFT JOIN wp_postmeta PM ON
                    PM.post_id = P.ID AND PM.meta_key = '_thumbnail_id'
                LEFT JOIN wp_postmeta PMA ON
                    PM.meta_value = PMA.post_id AND PMA.meta_key = '_wp_attached_file'
                WHERE
                    P.post_type = 'conferencias'";
        //return $this->execute_sql( $sql );
        return $sql;
    }    

    /**
     *  Helper function to get all blogs and their languages
     */
    function get_blog_by_locale($locale){
        $r = '';
        switch($locale){
            case "fr_FR":
            case "fr":
                $r = "wp_4_";
                break;
            case "en":
            case "en_US":
                $r = "wp_2_";
                break;
            case "pt":
            case "pt-br":
            case "pt_br":
            case "pt_BR":    
                $r = "wp_3_";
                break;
            case "es":
            case "es_ES":
            default:
                $r = "wp_";
                break;
        }
        return $r;
    }

    function hmsToSeconds($hms) {
        $parts = explode(':', $hms);
        // Use type casting to ensure the values are treated as numbers
        $hours = (int)$parts[0];
        $minutes = (int)$parts[1];
        $seconds = (int)$parts[2];
        
        // Calculate total seconds
        $totalSeconds = ($hours * 3600) + ($minutes * 60) + $seconds;
        
        return $totalSeconds;
    }


    function send_response( $data ){
        // set response code - 200 OK
	    header('Content-Type: application/json; charset=utf-8');
	
        http_response_code(200);
       
        echo json_encode(
            $data, JSON_UNESCAPED_UNICODE
        );

        $output = ob_get_contents();
        ob_end_clean();

        echo $output;
    }

    $stmt = $conn->prepare( translation_list() );
    $stmt->execute();
    
    $num = $stmt->rowCount();

    if($num>0){
            $conferences_arr=[];
        
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
                extract($row);
                
                $response_item=array(
                    "id" => (string)$ID,
                    "type" => "conferences",
                    "title" => $title,
                    "date" => strtotime($cdate),
                    "activity" => (int)$activity,
                    "slug" => $slug,
                    "private" => ($private?true:false),
                    "city" => $city,
                    "state" => $state,
                    "country" => $country,
                    "thumbnail" => $thumbnail,
                    "slug" => $slug,
                    "duration" => hmsToSeconds($duration),
                    "year" => substr($cdate,0,4),
                    "month" => substr($cdate,0,4) . " > " .substr($cdate,5,2),
                    "body" => $content
                );

                array_push( $conferences_arr, $response_item );
            }
    }

    send_response( $conferences_arr );

        //   nitem.youtube = item.youtube
        //   nitem.video = item.files?.videos?.file
        //   nitem.audio = item.files?.audios[0]?.[0]?.file2
        //   nitem.booklet = item.files?.textos[0]?.[1]?.file2
        //   nitem.simple = item.files?.textos[0]?.[0]?.file2