<?php

/**
 *  Main message class to handle all functions for messages interaction for the API.
 * 
 *  This class uses the db.php files connection in PDO style and only returns SQL statements.
 * 
 *  @author XFATBoY (xfatboy@carpa.com)
 *  @since v1
 */
class Message{
    
    private $conn;
    private $table_name = "wp_posts";
 
    public $id;
    public $name;
    public $description;
    public $price;
    public $category_id;
    public $category_name;
    public $created;
 
    /**
     *  Main constructor
     *  
     *  Initializes with the db
     */
    public function __construct($db){
        $this->conn = $db;
    }

    /**
     *  List function
     * 
     *  Displays the list of messages with it's pertinent variables
     * 
     *  @param  lcode   Language code to be used for the list
     */

    function list( $lcode , $last_update, $text = false ){
        $sql = "SELECT 
                P.ID,   
                P.post_title AS title,";
        if($text){
            $sql .= "
                P.post_content AS content,";
        }
        $sql .= "P.post_date AS creation_date, 
                P.post_modified as last_updated
                p.post_title
                #IFNULL(NULLIF(TIME_TO_SEC(MD.duration), '' ), 0) AS duration,
                #MD.activity AS no_activity,
                #MD.country, 
                #MD.state, 
                #MD.city,
                #CONCAT(YEAR(P.post_date),'/',MONTH(P.post_date),'/',P.post_name) AS slug
                FROM wp_posts P
                #LEFT JOIN wp_icl_translations T ON T.element_id = P.ID AND T.element_type = 'post_message' 
                #LEFT JOIN wp_icl_translations TS ON T.trid = TS.trid AND TS.language_code = 'es'
                #LEFT JOIN wp_messagedata MD ON TS.element_id = MD.post_id
                WHERE P.post_type = 'conferencias'
                #AND T.language_code = '$lcode'
                AND P.post_status = 'publish'";
                if($last_update!=''){
                    $sql .= " AND UNIX_TIMESTAMP(P.post_modified) > ". $last_update;
                }
        $sql .= " ORDER BY P.post_date DESC";

        return $this->execute_sql( $sql );
    }

    /**
     *  Detail function
     * 
     *  Given an ID returns the message with all details
     *  
     *  @param  id  Int id of the message whose details want to be found.
     */
    function detail($id){
        $sql = "SELECT
                P.ID,
                P.post_title AS title,
                P.post_content AS content,
                P.post_date AS creation_date,
                P.post_modified AS last_updated,
                P.guid,
                IFNULL(NULLIF(TIME_TO_SEC(MD.duration), '' ), 0) AS duration,
                MD.activity AS no_activity,
                MD.country, 
                MD.state, 
                MD.city,
                PMA.guid AS thumbnail
                FROM wp_posts AS P
                LEFT JOIN wp_icl_translations T ON T.element_id = P.ID AND T.element_type = 'post_message' 
                LEFT JOIN wp_icl_translations TS ON T.trid = TS.trid AND TS.language_code = 'es'
                LEFT JOIN wp_messagedata MD ON TS.element_id = MD.post_id
                LEFT JOIN wp_postmeta PM ON PM.post_id = TS.element_id AND PM.meta_key = '_thumbnail_id'
                LEFT JOIN wp_posts PMA ON PM.post_id = PMA.ID
                WHERE P.ID = '$id'";
        return $this->execute_sql( $sql );
    }

    /**
     *  Last update function
     * 
     *  Returns the last updated imtestamp for the message passed in via the @id
     * 
     *  @param  id  Int id for whom the last update should be found
     */
    function last_update($id){
        $sql = "SELECT
                P.post_modified AS last_updated
                FROM wp_posts P
                WHERE P.ID = '$id'";
        return $this->execute_sql( $sql );
    }

    /**
     *  Execute SQL function
     * 
     *  Executes a generic SQL statement and passes back the result.
     * 
     *  @param  string sql SQl statement to be executed
     *  @return 
     */
    function execute_sql( $sql ){
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        return $stmt;
    }

    /**
     *  Relevant conferences function
     * 
     *  Get all relevant conferences and return
     */
    function relevant_conferences(){
        $sql = "SELECT
                option_value
                FROM wp_options
                where option_name = 'options_conference'";
        return $this->execute_sql( $sql );
    }

    function get_post_source($trid){
        $sql = "SELECT
            T.element_id AS post_id
            FROM wp_icl_translations AS T
            WHERE T.trid = '$trid'
            AND T.language_code = 'es'";
        return $this->execute_sql( $sql );
    }
    
    function get_post_language($id){
        $sql = "SELECT
        T.language_code,
        T.trid
        FROM wp_icl_translations AS T
        WHERE T.element_id = '$id'
        AND T.element_type = 'post_message'";
        return $this->execute_sql( $sql );
    }
    
    function get_post_metadata($id){
        $sql = "SELECT
                IFNULL(NULLIF(TIME_TO_SEC(MD.duration), '' ), 0) AS duration,
                MD.country, 
                MD.state, 
                MD.city AS city,
                MD.activity AS no_activity
                FROM wp_messagedata AS MD
                WHERE post_id = '$id'";
        return $this->execute_sql( $sql );
    }
    
    function get_post_files($id){
        global $wpdb;
        $sql = "SELECT
                MF.youtube,
                MF.livestream,
                MF.video,
                MF.audio,
                MF.audio_flac,
                MF.pdf,
                MF.pdf_simple
                FROM wp_messagefiles as MF
                WHERE MF.post_id = '$id'";
        return $this->execute_sql( $sql );
    }
}