<?php

class testimonialModel extends baseModel {

	public $tableName = 'testimonials';

	function get($id = 0) {
		if (!is_numeric($id)) {
			return -1;
		}
		$this->db->where('id = ?', $id);
		$testimonial = $this->db->select('testimonials')[0];

		if ($testimonial) {
			$this->db->where('id=?', $id);
		}

		return $testimonial;
	}

	function getList() {

		$testimonials = $this->db->select('testimonials');

		return $testimonials;
	}

	function getTestimonials() {
		return $this->db->select('testimonials');
	}

	function getTestimonial($id) {
		$this->db->where('id=?', $id);
		return $this->db->select('testimonials')[0];
	}

	function updateTestimonial($id, $data) {
		$this->db->where('id=?', $id);	
		
		if (isset($_FILES['image']) && !$_FILES['image']['error']) {
			$cur_image = $this->db->select_one("testimonials", ['image']);
			
			if(!empty($cur_image['image']) && isset($_FILES['image']) && !$_FILES['image']['error']) {
				@unlink($cur_image['image']);	
			}
			
				$file = $_FILES['image'];
					 
				if ($ext == 'jpg' || $ext == 'png' || $ext = 'jpeg' || $ext = 'gif' ) {	
					$imgname = strtolower(basename($_FILES["image"]["name"]));
					$destination = "static/uploads/testimonials/$imgname";
					move_uploaded_file($file['tmp_name'], $destination);
					$this->db->where('id=?', $id);
					$this->db->update('testimonials', ['image' => $destination]);					
				}	
		}
		
		$this->db->where('id=?', $id);
		$this->db->update('testimonials', $data);
	}	

	function insertTestimonial($data) {
		if (isset($_FILES['image']) && !$_FILES['image']['error']) {
			
			$file = $_FILES['image'];
			 
			$imgname = strtolower(basename($_FILES["image"]["name"]));
			if ($ext == 'jpg' || $ext == 'png' || $ext = 'jpeg' || $ext = 'gif') {				
				$destination = "static/uploads/testimonials/$imgname";
				move_uploaded_file($file['tmp_name'], $destination);
				$data['image'] = $destination;
			}
		}			
		$this->db->insert('testimonials', $data);
	}

	function deleteTestimonial($id) {
		$this->db->where('id=?', $id);
		$this->db->delete('testimonials');
	}

	function delete($id) {
		$this->db->where('id=?', $id);
		$this->db->delete('testimonials');
	}

	function getItem($id) {
		$this->db->where('id=?', $id);
		return $this->db->select('testimonials')[0];
	}
	
	function getDashUpdates() {
		return $this->db->select('api_dashupdates');
	}

}