<?php
class viewpost{
    var $id;
    
    function listpost(){
        $kategori = mysql_query("SELECT * FROM post ORDER BY id_post");

        while ($p = mysql_fetch_array($kategori)){
            $data[$i] = array(
                        'id' => $p[id_post],
                        'id_kategori' => $p[id_kategori],
                        'judul' => $p[judul],
                        'tag' => $p[tag],
                        'isi' => $p[isi],
                        'keterangan' => $p[keterangan],
                        'publish' => $p[publish]
                     );
            
            $i++;
        }
        
        return($data);
        
    }

    function listposttag($tag,$start,$limit){
        $kategori = mysql_query("SELECT post.* FROM post,kategori_post WHERE kategori_post.id_katpost = post.id_kategori AND kategori_post.tag = '$tag' AND post.publish = 'Y' ORDER BY post.id_post DESC LIMIT $start, $limit");

        while ($p = mysql_fetch_array($kategori)){
            $data[$i] = array(
                        'id' => $p[id_post],
                        'id_kategori' => $p[id_kategori],
                        'judul' => $p[judul],
                        'tag' => $p[tag],
                        'isi' => $p[isi],
                        'keterangan' => $p[keterangan],
                        'gambar' => $p[gambar],
                        'publish' => $p[publish],
                        'date' => $p[dateadd]
                     );
            
            $i++;
        }
        
        return($data);
        
    }
    
    function post(){
        $kategori = mysql_query("SELECT * FROM post WHERE id_post = $this->id");
        $p = mysql_fetch_array($kategori);
        
        $data = array(
                        'id' => $p[id_post],
                        'id_kategori' => $p[id_kategori],
                        'judul' => $p[judul],
                        'tag' => $p[tag],
                        'isi' => $p[isi],
                        'keterangan' => $p[keterangan],
                        'gambar' => $p[gambar],
                        'publish' => $p[publish],
                        'date' => $p[dateadd]
                     );
        
        return($data);
        
    }

    function posttag($tag){
        $kategori = mysql_query("SELECT * FROM post WHERE tag = '$tag' AND post.publish = 'Y'");
        $p = mysql_fetch_array($kategori);
        
        $data = array(
                        'id' => $p[id_post],
                        'id_kategori' => $p[id_kategori],
                        'judul' => $p[judul],
                        'tag' => $p[tag],
                        'isi' => $p[isi],
                        'keterangan' => $p[keterangan],
                        'gambar' => $p[gambar],
                        'publish' => $p[publish],
                        'date' => $p[dateadd]
                     );
        
        return($data);
        
    }
    
    function addpost($id_kategori,$judul,$isi,$keterangan,$gambar,$publish){

        $tgl = date("d-m-Y");
        $tag = str_replace(" ", "-", $judul);
        
        mysql_query("INSERT INTO post(  id_kategori,
                                        judul,
                                        tag,
                                        isi,
                                        keterangan,
                                        gambar,
                                        publish,
                                        dateadd,
                                        dateedit) 
                                        VALUES(
                                        '$id_kategori',
                                        '$judul',
                                        '$tag',
                                        '$isi',
                                        '$keterangan',
                                        '$gambar',
                                        '$publish',
                                        STR_TO_DATE('$tgl','%d-%m-%Y'),
                                        STR_TO_DATE('$tgl','%d-%m-%Y'))");
    }
    
    function editpost($id_kategori,$judul,$isi,$keterangan,$gambar,$publish){

        $tgl = date("d-m-Y");
        $tag = str_replace(" ", "-", $judul);
        
        mysql_query("UPDATE post SET    id_kategori = '$id_kategori',
                                        judul       = '$judul',
                                        tag         = '$tag',
                                        isi         = '$isi',
                                        keterangan  = '$keterangan',
                                        gambar      = '$gambar',
                                        publish     = '$publish',
                                        dateedit    = STR_TO_DATE('$tgl','%d-%m-%Y')
                                        WHERE
                                        id_post     = $this->id");
    }
    
    function deletepost(){
        mysql_query("DELETE FROM post where id_post = $this->id");
    }
}