<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Post_model extends CI_Model {

	public function __construct() {
		parent::__construct();
	}


	public function get_all() {

		$this->db->select()->from('posts');
		$this->db->order_by( 'id' );
		$query = $this->db->get();
		return $query->result_array();
	}


	public function get_post_by($get_type = 'id', $get_value = FALSE) {
		$this->db->select()->from('posts');
		$this->db->where($get_type, $get_value);
		$this->db->limit(1);
		//$this->db->order_by( 'id' );
		$query = $this->db->get();
		return $query->row_array();
	}


	public function get ($id = null) {
		/*$this->db->select()->from( 'posts' );

		// where condition if id is present
		if ($id != null) {
			$this->db->where( 'posts_id', $id );
		} else {
			$this->db->order_by( 'posts_id' );
		}*/

		/*foreach ( $query->result() as $row ) {
			$model = new $class;
			$model->populate($row);
			$ret_val[$row->{$this::DB_TABLE_PK}] = $model;
		}
		return $ret_val;*/


		$this->db->select()->from('posts');
		if( $id != null ) {
			$this->db->where( 'id', $id );
		} else {
			$this->db->order_by( 'id' );
		}

		$query = $this->db->get();
		//return $query->result();

		if ($id != null) {
			return $query->row_array(); // single row
			//return $query->result();
		} else {
			//return $query->result_array(); // array of result
			return $query->result();
		}
	}


	public function array_from_post($fields) {
		$data = array();
		foreach ($fields as $field) {
			$data[$field] = $this->input->post($field);
		}
		return $data;
	}


	/*public function insert_post() {
		$this->load->helper('url');

		$slug = url_title($this->input->post('title'), 'dash', TRUE);

		$data = array(
			'title' => $this->input->post('title'),
			'slug' => $slug,
			'text' => $this->input->post('text')
		);

		return $this->db->insert('news', $data);
	}*/


	public function update($data, $id = NULL) {

		/*
		// Set timestamps
		if ($this->_timestamps == TRUE) {
			$now = date('Y-m-d H:i:s');
			$id || $data['created'] = $now;
			$data['modified'] = $now;
		}

		// Insert
		if ($id === NULL) {
			!isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
			$this->db->set($data);
			$this->db->insert($this->_table_name);
			$id = $this->db->insert_id();
		}
		// Update
		else {
			$filter = $this->_primary_filter;
			$id = $filter($id);
			$this->db->set($data);
			$this->db->where($this->_primary_key, $id);
			$this->db->update($this->_table_name);
		}*/

		/*if ($id === NULL) { // insert
			$this->db->set($data);
			$this->db->insert('posts');
			$id = $this->db->insert_id();
		} else { // update
			$this->db->set($data);
			$this->db->where('id', $id);
			$this->db->update('posts');
		}*/

		$data['updated'] = date('Y-m-d H:i:s');
		$this->db->set($data);
		$this->db->where('id', $id);
		$this->db->update('posts');

		return $id;
	}


	public function create($data){
		$data['created'] = $data['updated'] = date('Y-m-d H:i:s');
		$this->db->set($data);
		$this->db->insert('posts');
		$insert_id = $this->db->insert_id();
		return $insert_id;
	}


	public function delete($id = NULL) {
		$this->db->where('id', $id);
		$this->db->delete('posts');
	}

}