<?php
class M_album extends CI_Model{

	function get_all_album(){
		$sql = "SELECT tbl_album.album_id, tbl_album.album_nama, tbl_album.album_tanggal, tbl_album.album_pengguna_id, tbl_album.album_author, tbl_album.album_cover,
			DATE_FORMAT(tbl_album.album_tanggal,'%d/%m/%Y') AS tanggal,
			(SELECT COUNT(*) FROM tbl_galeri WHERE galeri_album_id = tbl_album.album_id) AS album_count
			FROM tbl_album ORDER BY tbl_album.album_id DESC";
		return $this->db->query($sql);
	}

	function simpan_album($album, $user_id, $user_nama, $gambar){
		$data = array(
			'album_nama'        => $album,
			'album_pengguna_id' => (int)$user_id,
			'album_author'      => $user_nama,
			'album_cover'       => $gambar
		);
		return $this->db->insert('tbl_album', $data);
	}

	function get_tulisan_by_kode($kode){
		return $this->db->query("SELECT tbl_tulisan.*, DATE_FORMAT(tulisan_tanggal,'%d/%m/%Y') AS tanggal FROM tbl_tulisan WHERE tulisan_id = ?", array((int)$kode));
	}

	function update_album($album_id, $album_nama, $user_id, $user_nama, $gambar){
		$data = array(
			'album_nama'        => $album_nama,
			'album_pengguna_id' => (int)$user_id,
			'album_author'      => $user_nama,
			'album_cover'       => $gambar
		);
		$this->db->where('album_id', (int)$album_id);
		return $this->db->update('tbl_album', $data);
	}

	function update_album_tanpa_img($album_id, $album_nama, $user_id, $user_nama){
		$data = array(
			'album_nama'        => $album_nama,
			'album_pengguna_id' => (int)$user_id,
			'album_author'      => $user_nama
		);
		$this->db->where('album_id', (int)$album_id);
		return $this->db->update('tbl_album', $data);
	}

	function hapus_album($kode){
		$this->db->where('album_id', (int)$kode);
		return $this->db->delete('tbl_album');
	}
}
