<?php
/**********************************************************************
***********************************************************************/

//----------------------------------------------------------------------------------------

function add_project_gallery_image($project_id, $description, $image_name, $image_date, 
                                    $section_id, $unique_name, $file_type, $filesize)
{
	$id_user = $_SESSION['wa_current_user']->user;
        $datetime = date('Y-m-d H:i:s');
        
        
	$sql = "INSERT INTO ".TB_PREF."project_gallery_images (
                        project_id, description, 
                        image_name, image_date, 
                        section_id, unique_name, 
                        file_type, filesize, 
                        created_by, created_on) 
                VALUES (".db_escape($project_id).",".db_escape($description).", "
                        .db_escape($image_name).",".db_escape(date2sql($image_date)).", "
                        .db_escape($section_id).", ".db_escape($unique_name).", "
                        .db_escape($file_type).", ".db_escape($filesize).", "
                        .db_escape($id_user).", ".db_escape($datetime).")";
	db_query($sql, "add_project_gallery_image");		
}
//----------------------------------------------------------------------------------------

function update_project_gallery_image($selected_id, $description, $image_name, $image_date, 
                                    $section_id, $unique_name, $file_type, $filesize)
{
        $id_user = $_SESSION['wa_current_user']->user;
        $datetime = date('Y-m-d H:i:s');
        

	$sql = "UPDATE ".TB_PREF."project_gallery_images 
                SET
                    section_id=".db_escape($section_id).",
                    image_date=".db_escape(date2sql($image_date)).",
                    description=".db_escape($description).", ";
                    if ($image_name != "")
                    {
                        $sql .= "image_name=".db_escape($image_name).",
                                unique_name=".db_escape($unique_name).",
                                filesize=".db_escape($filesize).",
                                file_type=".db_escape($file_type).",";
                    }
                    
	$sql .= " updated_by=".db_escape($id_user).",
                    updated_on=".db_escape($datetime)."  
                WHERE id=".db_escape($selected_id);
        
	db_query($sql, "update_project_gallery_image");		
}

//----------------------------------------------------------------------------------------

function delete_project_gallery_image($id)
{
	$sql = "DELETE FROM ".TB_PREF."project_gallery_images WHERE id = ".db_escape($id);
	db_query($sql, "Could not delete attachment");
}

//----------------------------------------------------------------------------------------

function get_project_gallery_image_string($type, $id)
{
	global $path_to_root;
    $str_return = "";    
    $sql = "SELECT * FROM ".TB_PREF."project_gallery_images WHERE type_no=".db_escape($type)." AND trans_no=".db_escape($id)." ORDER BY trans_no";
    $return = db_query($sql, "Could not retrieve attachments");
    while ($attachment = db_fetch($return))
    {
        if (strlen($str_return))
            $str_return = $str_return . " \n";    
        $str_return .= _("Attached File:")." <a href='$path_to_root/admin/attachments.php?vw=".$attachment["id"]." ' target='blanc_'> ". 
        	$attachment["id"] . " " . $attachment["description"]. " - ". $attachment["filename"]."</a><br/>";
    }
    return $str_return . "<br/>";
}

//----------------------------------------------------------------------------------------

function get_project_gallery_images($project_id)
{
	$sql = "SELECT pgi.id, 
                    pgi.image_date,
                    ps.section_name, 
                    pgi.image_name,
                    pgi.description,
                    pgi.filesize,
                    pgi.file_type,
                    pgi.created_on,
                    pgi.project_id     
                FROM ".TB_PREF."project_gallery_images pgi 
                    JOIN ".TB_PREF."project_sections ps 
                        ON pgi.section_id = ps.id 
                WHERE pgi.project_id =".db_escape($project_id)." 
                ORDER BY pgi.image_date DESC";
        
	return db_query($sql, "Could not retrieve attachments");
}

function get_project_gallery_section_images($project_id, $section_id)
{
	$sql = "SELECT pgi.id, 
                    pgi.image_date,
                    ps.section_name, 
                    pgi.image_name,
                    pgi.description,
                    pgi.filesize,
                    pgi.file_type,
                    pgi.created_on,
                    pgi.project_id     
                FROM ".TB_PREF."project_gallery_images pgi 
                    JOIN ".TB_PREF."project_sections ps 
                        ON pgi.section_id = ps.id 
                WHERE pgi.project_id =".db_escape($project_id)." 
                    AND pgi.section_id =".db_escape($section_id)." 
                ORDER BY pgi.image_date DESC";
        
	return db_query($sql, "get_project_gallery_section_images");
}

function get_project_gallery_image($id)
{
	$sql = "SELECT * FROM ".TB_PREF."project_gallery_images WHERE id=".db_escape($id);
	$result = db_query($sql, "get_project_gallery_image");
	return db_fetch($result);
}

function has_project_gallery_image($type, $id)
{
	$sql = "SELECT DISTINCT * FROM ".TB_PREF."project_gallery_images WHERE type_no=".db_escape($type)." AND trans_no=".db_escape($id);
	$result = db_query($sql, "Could not retrieve attachments");
	$myrow = db_fetch($result);
	if ($myrow === false)
		return 0;
	else
		return $myrow['id'];
}

function get_sql_for_project_gallery_images($project_id, $section_id, $from_date, $to_date)
{
	$sql = "SELECT pgi.id, 
                    pgi.image_date,
                    ps.section_name, 
                    pgi.image_name,
                    pgi.description,
                    pgi.filesize,
                    pgi.file_type,
                    pgi.created_on,
                    pgi.project_id     
                FROM ".TB_PREF."project_gallery_images pgi 
                    JOIN ".TB_PREF."project_sections ps 
                        ON pgi.section_id = ps.id 
                WHERE pgi.project_id =".db_escape($project_id)." 
                    AND pgi.image_date >=".db_escape($from_date)." 
                    AND pgi.image_date <=".db_escape($to_date);
    
        if($section_id > 0){
            $sql .= " AND pgi.section_id = ".db_escape($section_id);
        }
                
        $sql .= " ORDER BY pgi.image_date DESC";
        
        return $sql;
}

function get_project_gallery_image_sections($project_id,$section_id, $from_date, $to_date)
{
    $sql = "SELECT DISTINCT pgi.section_id,
                    ps.section_name 
            FROM ".TB_PREF."project_gallery_images pgi 
                JOIN ".TB_PREF."project_sections ps 
                        ON pgi.section_id = ps.id 
            WHERE pgi.project_id=".db_escape($project_id)." 
                AND image_date >=".db_escape($from_date)." 
                AND image_date <=".db_escape($to_date);
    
    if($section_id > 0){
        $sql .= " AND pgi.section_id = ".db_escape($section_id);
    }
	
    return db_query($sql, "get_project_gallery_image_sections");
}
?>