<?php

class MPosts extends Model{

	function MPosts(){
		parent::Model();
	}


function getPost($id){
    $data = array();
    $this->db->where('id',$id);
    $this->db->limit(1);
    $Q = $this->db->get('posts');
    if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
	

 function getAllPosts(){
     $data = array();
     $Q = $this->db->get('posts');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
       }
    }
    $Q->free_result();  
    return $data; 
 }

 function getAllPostsByCategory($catid){
     $data = array();
     $this->db->where("category_id",$catid);
     $this->db->where('status', 'published');
	$this->db->orderby('pubdate', 'desc');
     $Q = $this->db->get('posts');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
       }
    }
    $Q->free_result();  
    return $data; 
 }
 
  function getLivePosts($limit){
     $data = array();
     $this->db->limit($limit);
     $this->db->where('status', 'published');
	$this->db->where('category_id',1);
     $this->db->order_by('pubdate','desc');
     $Q = $this->db->get('posts');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
       }
    }
    $Q->free_result();  
    return $data; 
 }
 
	function getAllLivePosts(){
     $data = array();
     $this->db->where('status', 'published');
	 $this->db->where('category_id',1);
     $this->db->order_by('pubdate','desc');
     $Q = $this->db->get('posts');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
       }
    }
    $Q->free_result();  
    return $data; 
 }
 	function getAllMonthPosts(){
	 $date = date('Y-m');
     $data = array();
     $this->db->where('status', 'published');
	 $this->db->where('category_id',1);
	 $this->db->like('pubdate', $date, 'after');
     $this->db->order_by('pubdate','desc');
     $Q = $this->db->get('posts');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
       }
    }
    $Q->free_result();  
    return $data; 
 }

 function addPost(){
 	$now = date("Y-m-d H:i:s");
	$data = array( 
		'title' => $this->input->post('title'),
		'tags' => $this->input->post('tags'),
		'status' => $this->input->post('status'),
		'body' => $this->input->post('body'),
		'category_id' => $this->input->post('category_id'),
		'user_id' => $_SESSION['userid'],
		'pubdate' => $now
	
	);

	$this->db->insert('posts', $data);	 
 }
 
 function updatePost(){
	$data = array( 
		'title' => $this->input->post('title'),
		'tags' => $this->input->post('tags'),
		'status' => $this->input->post('status'),
		'body' => $this->input->post('body'),
		'category_id' => $this->input->post('category_id'),
		'user_id' => $_SESSION['userid']

	);

 	$this->db->where('id', $this->input->post('id'));
	$this->db->update('posts', $data);	
 
 }
 
 function deletePost($id){
 	$this->db->where('id', $id);
	$this->db->delete('posts');	
 }
 function NewsletterMonths(){
 	//var $months 
 	$Q = $this->db->query('SELECT ((PERIOD_DIFF((SELECT EXTRACT(YEAR_MONTH FROM CURDATE()+0)), 200810))+1)as months'); 
	//$months += 1;
	//return $months;
    if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }
    $Q->free_result();  
    return $data; 

 	}
 	function getAllCurrentMonthPosts($year_month){
	 //$date = date('Y-m');
     $data = array();
     $this->db->where('status', 'published');
	 $this->db->where('category_id',1);
	 $this->db->like('pubdate', $year_month, 'after');
     $this->db->order_by('pubdate','desc');
     $Q = $this->db->get('posts');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
       }
    }
    $Q->free_result();  
    return $data; 
 }

 function ReturnMonthYear()
 {
 $monthyear='';
// $date = date('m-Y');
 switch (date('m'))
		{
			case '01'	:	$monthyear = 'Ιανουάριος';
				break;
			case '02'	:	$monthyear = 'Φεβρουάριος';
				break;
			case '03'	:	$monthyear = 'Μάρτιος';
				break;
			case '04'	:	$monthyear = 'Απρίλιος';
				break;
			case '05'	:	$monthyear = 'Μάιος';
				break;
			case '06'	:	$monthyear = 'Ιούνιος';
				break;
			case '07'	:	$monthyear = 'Ιούλιος';
				break;
			case '08'	:	$monthyear = 'Αύγουστος';
				break;
			case '09'	:	$monthyear = 'Σεπτέμβριος';
				break;
			case '10'	:	$monthyear = 'Οκτώβριος';
				break;
			case '11'	:	$monthyear = 'Νοέμβριος';
				break;
			case '12'	:	$monthyear = 'Δεκέμβριος';
				break;
			default		:	$monthyear = '';
				break;
		}
		return $monthyear;
 }
	 function ReturnMonth($month)
 {
 $monthyear='';
// $date = date('m-Y');
 switch ($month)
		{
			case '01'	:	$monthyear = 'Ιανουάριος';
				break;
			case '02'	:	$monthyear = 'Φεβρουάριος';
				break;
			case '03'	:	$monthyear = 'Μάρτιος';
				break;
			case '04'	:	$monthyear = 'Απρίλιος';
				break;
			case '05'	:	$monthyear = 'Μάιος';
				break;
			case '06'	:	$monthyear = 'Ιούνιος';
				break;
			case '07'	:	$monthyear = 'Ιούλιος';
				break;
			case '08'	:	$monthyear = 'Αύγουστος';
				break;
			case '09'	:	$monthyear = 'Σεπτέμβριος';
				break;
			case '10'	:	$monthyear = 'Οκτώβριος';
				break;
			case '11'	:	$monthyear = 'Νοέμβριος';
				break;
			case '12'	:	$monthyear = 'Δεκέμβριος';
				break;
			default		:	$monthyear = '';
				break;
		}
		return $monthyear;
 }

}

?>