<?php

class logosModel extends baseModel {

	public $tableName = 'logos';

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

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

		return $logo;
	}

	function getList() {

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

		return $logos;
	}

	function getLogos() {
		return $this->db->select('logos');
	}

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

	function updateLogo($id, $data) {
		$this->db->where('id=?', $id);	
		
		if (isset($_FILES['image']) && !$_FILES['image']['error']) {
			$cur_image = $this->db->select_one("logos", ['image']);
			
			if(!empty($cur_image['image']) && isset($_FILES['image']) && !$_FILES['image']['error']) {
				@unlink($cur_image['image']);	
			}
			
				$file = $_FILES['image'];
				
//				$ext = substr($imgname, strrpos($imgname, '.') + 1);
//				//$ext = pathinfo($filename, PATHINFO_EXTENSION);
//				print_r($ext);
//				exit;
					 
				if ($ext == 'jpg' || $ext == 'png' || $ext = 'jpeg' || $ext = 'gif' ) {	
					$imgname = strtolower(basename($_FILES["image"]["name"]));
					$destination = "static/uploads/logos/$imgname";
					move_uploaded_file($file['tmp_name'], $destination);
					$this->db->where('id=?', $id);
					$this->db->update('logos', ['image' => $destination]);					
				}	
		}
		
		$this->db->where('id=?', $id);
		$this->db->update('logos', $data);
	}	

	function insertLogo($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/logos/$imgname";
				move_uploaded_file($file['tmp_name'], $destination);
				$data['image'] = $destination;
			}
		}			
		$this->db->insert('logos', $data);
	}

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

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

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

}