<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Post_model extends CI_Model
{

    var $post_id;
    var $title = '';
    var $posttext = '';
	var $upload_date = '';
    var $uploaded_by = '';
    var $updated_by = '';
    var $archived = false;
	
	
    public function __construct()
    {
        parent::__construct();
    }

    public function count_all()
    {
        return $this->db->count_all('posts');
    }

    public function count_row_for_query($query)
    {
        return $query->num_rows();
    }

    public function get_all_posts()
    {
        $query = $this->db->get('posts');
        return $query->result();
    }
    
    public function get_recent_posts()
    {
        $this->db->order_by('upload_date', 'desc');
        $this->db->limit(10);
        $query = $this->db->get_where('posts', array('archived' => false));
        return $query->result();
    }
    
        public function get_last_post()
    {
        $this->db->order_by('upload_date', 'desc');
        $this->db->limit(1);
        $query = $this->db->get_where('posts', array('archived' => false));
        $messages = $query->result();
        $message = [];
        if (count($messages) > 0) {
            foreach ($messages as $row) {
                $message = $row;
            }
        }
        $message->post_id = 0;
        return $message;
    }

    public function get_posts_by_id($id)
    {
        $query = $this->db->get_where('posts', array('id' => $id));
        $messages = $query->result();
        $message = [];
        if (count($messages) > 0) {
            foreach ($messages as $row) {
                $message = $row;
            }
        }
        return $message;
    }
    
        public function get_all_active_posts()
    {
        $query = $this->db->get_where('posts', array('archived' => false));
        $messages = $query->result();
        return $messages;
    }

    public function save_post($title, $posttext, $upload_date, $uploaded_by)
    {
		$this->title = $title;
		$this->posttext = $posttext;
		$this->upload_date = $upload_date;
		$this->uploaded_by = $uploaded_by;
        $this->updated_by ='';
        $this->archived = false;
        
        $query = $this->db->get('post_id_seq');
        $ms = $query->result();
        $me = [];
        if (count($ms) > 0) {
            foreach ($ms as $row) {
                $me = $row;
            }
        }
        $mid = $me->next_id;
        $this->post_id = $mid+1;
	    $this->db->set('next_id', $mid+1)->where('current_id', 1)->update('post_id_seq');
        if ($this->db->insert('posts', $this)) {
            $productArray = ['code' => '00', 'message' => 'Post Uploaded Successfully!', 'data' => true];
            return $productArray;
        } else {
            $productArray = ['code' => '99', 'message' => 'Error!', 'data' => false];
            return $productArray;
        }
    }
	

    
        public function archive_post($id)
    {
        $posts = $this->get_posts_by_id($id);
        
        if ($this->db
        ->set('archived', true)
        ->where('id', $id)->update('posts')) {
            return true;
        } else {
           return false;
        }
    }

        public function restore_post($id)
    {
        $posts = $this->get_posts_by_id($id);
        
        if ($this->db
        ->set('archived', false)
        ->where('id', $id)->update('posts')) {
            return true;
        } else {
           return false;
        }
    }    
	
	    public function update_post($id, $title, $posttext, $updated_by)
    {
        $posts = $this->get_posts_by_id($id);
        
        if ($this->db
        ->set('title', $title)
        ->set('posttext', $posttext)
        ->set('updated_by', $updated_by)
        ->where('id', $id)->update('posts')) {
            $productArray = ['code' => '00', 'message' => 'Post Updated Successfully!', 'data' => true];
            return $productArray;
        } else {
            $productArray = ['code' => '99', 'message' => 'Error in Updating Post!', 'data' => false];
            return $productArray;
        }
    }  
}
