<?php  
class Edytuj_model extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
	}
	
	public function PobierzWpisDoEdycji($IdWpisu)
	{
		$RezultatZapytania = $this->db->query('SELECT * FROM blog_wpisy WHERE wpis_id = "'.$this->db->escape_str($IdWpisu).'"');
		
		return $RezultatZapytania;
	}
	
	public function DodajKategorie()
	{
		$this->db->query('INSERT INTO 
		blog_kategorie
		(blog_kategoria)
		VALUES
		("'.$this->db->escape_str($this->input->post('blog_kategoria')).'")
		');
	}
	
	public function UsunKategorie($KategoriaId)
	{
		// Kategoria
		$this->db->query('DELETE FROM 
		blog_kategorie
		WHERE
		blog_id = "'.$this->db->escape_str($KategoriaId).'"
		');
		
		// Wpisy - wybieranie
		$Rezultat = $this->db->query('SELECT * FROM blog_wpisy WHERE wpis_kategoria = "'.$this->db->escape_str($KategoriaId).'"');
		
		// Komentarze
		foreach($Rezultat->result() as $Wiersz)
		{		
			$this->db->query('DELETE FROM 
			blog_komentarz
			WHERE
			komentarz_wpis_id = "'.$this->db->escape_str($Wiersz->wpis_id).'"
			');
		}
				
		// Wpisy - usuwanie
		$this->db->query('DELETE FROM 
		blog_wpisy
		WHERE
		wpis_kategoria = "'.$this->db->escape_str($KategoriaId).'"
		');
	}
	
	public function EdytujKategorie($BlogId)
	{
		$RezultatZapytania = $this->db->query('SELECT * FROM blog_kategorie WHERE blog_id = "'.$this->db->escape_str($BlogId).'"');
		
		return $RezultatZapytania;
	}
	
	public function AktualizujKategorie()
	{
		$this->db->query('UPDATE blog_kategorie 
		SET
		blog_kategoria = "'.$this->db->escape_str($this->input->post('blog_kategoria')).'"
		WHERE 
		blog_id = "'.$this->db->escape_str($this->input->post('kategoria_id')).'"');
	}
	
	public function DodajNowyWpis()
	{
		$this->db->query('INSERT INTO blog_wpisy 
		(wpis_tytul,wpis_tresc,wpis_data,wpis_kategoria)
		VALUES
		(
		"'.$this->db->escape_str($this->input->post('wpis_tytul')).'",
		"'.$this->db->escape_str($this->input->post('wpis_tresc')).'",
		"'.$this->db->escape_str(date('d-m-Y H:i')).'",
		"'.$this->db->escape_str($this->input->post('wpis_kategoria')).'"
		)
		');
	}
	
	public function EdytujBiezacyWpis()
	{
		$this->db->query('UPDATE blog_wpisy 
		SET
		wpis_tytul = "'.$this->db->escape_str($this->input->post('wpis_tytul')).'",
		wpis_tresc = "'.$this->db->escape_str($this->input->post('wpis_tresc')).'",
		wpis_kategoria = "'.$this->db->escape_str($this->input->post('wpis_kategoria')).'"
		WHERE
		wpis_id = "'.$this->db->escape_str($this->input->post('edytuj_wpis_id')).'"
		');
	}
	
	public function SprawdzStareHaslo($StareHaslo)
	{
		$RezultatZapytania = $this->db->query('SELECT count(admin_id) AS IloscUzytkownikow FROM 
		blog_admin 
		WHERE 
		admin_id = "1"
		AND
		admin_haslo = "'.$this->db->escape_str(md5($StareHaslo)).'"
		');
		
		return $RezultatZapytania;
	}
	
	public function ZmienHasloRoota()
	{
		$this->db->query('UPDATE blog_admin 
		SET
		admin_haslo  = "'.$this->db->escape_str(md5($this->input->post('nowe_haslo'))).'"
		WHERE
		admin_id = "1"
		');
	}
}
?>