<?php
defined('BASEPATH') OR exit('No direct script access allowed');
defined('DIR_SEPARATOR') || define('DIR_SEPARATOR', '/');
defined('XUXO_VERSION') || define('XUXO_VERSION', '1.0.0');

class Testimonyview extends CI_Model {
	
	public function __construct() {
		parent::__construct();
		
	}
	
	/**
	* TESTIMONY
	*/
	public function getAllTestimonies($start = NULL, $length = 9, $feature = FALSE) {
		$sql = "SELECT
							 testimony_main.ID,
							 testimony_main.name,
							 testimony_main.origin,
							 testimony_main.content,
							 testimony_main.image,
							 testimony_main.postDT,
							 testimony_main.createdDT,
							 testimony_main.status
						FROM  testimony_main
						WHERE testimony_main.status = 'PUBLISHED' ";
		$sql .= ($feature) ? " AND feature = 1 " : " AND (feature IS NULL OR feature = 0) ";
		$sql .= " ORDER BY createdDT DESC ";
		$sql .= ($start !== NULL && $length) ? " LIMIT ".$start.", ".$length : NULL;
		
		$q = $this->db->query($sql);
		return ($q && ($r = $q->result())) ? $this->ObjToArray($r) : NULL;
	}
	
	public function getTestimony($id) {
		$sql = "SELECT
							ID,
							name,
							origin,
							content,
							DATE_FORMAT(postDT, '%d/%m/%Y') postDT,
							image,
							status,
							DATE_FORMAT(createdDT, '%Y-%m-%d %H:%i:%s') createdDT,
							DATE_FORMAT(updatedDT, '%Y-%m-%d %H:%i:%s') updatedDT,
							IFNULL((SELECT penName FROM people, user WHERE user.peopleID = people.ID AND user.ID =  testimony_main.createdBy),'admin') createdBy,
							IFNULL((SELECT penName FROM people, user WHERE user.peopleID = people.ID AND user.ID =  testimony_main.updatedBy),'admin') updatedBy
						FROM  testimony_main
						WHERE  testimony_main.ID = ".$id."
						ORDER BY name ASC";
		$q = $this->db->query($sql);
		return ($q && ($r = $q->result()) && $r[0]) ? $this->ObjToArray($r[0]) : NULL;
	}
	
	public function ObjToArray($item) { return json_decode(json_encode($item),true); }
	
}































