<?php

/**
 *  Main actividad class to handle all functions for actividades 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 Galeria {

    private $conn;
    private $table_name = "wp_posts";

    /**
     *  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
     * 
     */
    function gallery_list(){
        $sql = "SELECT
        P.ID,
        P.post_title,
        P.post_date,
        P2.guid AS thumbnail
        FROM wp_posts P
        LEFT JOIN wp_postmeta PM ON PM.post_id = P.id AND PM.meta_key = '_thumbnail_id'
        LEFT JOIN wp_posts P2 ON PM.meta_value = P2.ID
        WHERE P.post_type = 'galeria' AND P.post_status = 'publish' 
        ORDER BY P.post_date DESC";
        return $this->execute_sql( $sql );
    }

    function gallery_images($id){
        $sql = "SELECT DISTINCT
        PMI.meta_value AS imagenes,
        PMS.meta_value AS sin_recortar
    FROM wp_postmeta PM
        INNER JOIN wp_postmeta PMI ON PMI.post_id = PM.post_id AND PMI.meta_key = 'imagenes'
        INNER JOIN wp_postmeta PMS ON PMS.post_id = PM.post_id AND PMS.meta_key = 'sin_recortar'
     WHERE PM.post_id = $id";
        return $this->execute_sql( $sql );
    }

    function image($image_id){
        $sql = "SELECT meta_value AS imagedata FROM wp_postmeta WHERE post_id = $image_id AND meta_key = '_wp_attachment_metadata'";
        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;
    }

}