<?php

class commentModel extends baseModel{
	var $fields = ['feature_id','project_id','kode_id', 'user_id', 'content', 'parent_id'];
//	function insert($data) {
//		$newComment = [];
//		
//		foreach ($data as $key => $value) {
//			if (in_array($key, $this->fields)) {
//				$newComment[$key] = $value;
//			}
//		}
//		
//		$this->db->insert('comments', $newComment);
//	}
	
	function get($id = 0){
		global $website_options;

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

		if ($project)
			$this->fetchProject($project);

		return $project;
	}	
	
	function fetchProject(&$project){
		$this->db->where('id = ?', $project['user_id']);
		$project['user'] = $this->db->select('square_users')[0];
	}	
	
	//var $fields = ['feature_id','project_id', 'user_id', 'content', 'parent_id'];
	
	function insertnow($data){
		
		$newComment = [];

		foreach ($data as $key => $value) {
			if (in_array($key, $this->fields)) {
				$newComment[$key] = $value;
			}
		}

		$this->db->insert('comments', $newComment);
	}	
	
	// THIS INSERTS THE MAIN PARENT COMMENT
	function insert($data)
	{
		$newComment = [];

		foreach ($data as $key => $value) {
			if (in_array($key, $this->fields)) {
				$newComment[$key] = $value;
			}
		}
		
		if (isset($_FILES['file']) && !$_FILES['file']['error']) {

			$file = $_FILES['file'];

			$imgname = strtolower(basename($_FILES["file"]["name"]));
			if ($ext == 'jpg' || $ext == 'png' || $ext = 'jpeg' || $ext = 'gif') {
				$destination = "static/uploads/chat/$imgname";
				move_uploaded_file($file['tmp_name'], $destination);
				$newComment['img'] = $destination;
			}
		}		

		$this->db->insert('comments', $newComment);
		
		$id = $this->db->insert_id();

		return $id;		
		
	}
	
	function insertComment($data){

		if (isset($_FILES['file']) && !$_FILES['file']['error']) {

			$file = $_FILES['file'];

			$imgname = strtolower(basename($_FILES["file"]["name"]));
			if ($ext == 'jpg' || $ext == 'png' || $ext = 'jpeg' || $ext = 'gif') {
				$destination = "static/uploads/chat/$imgname";
				move_uploaded_file($file['tmp_name'], $destination);
				$data['image'] = $destination;
			}
		}
		$this->db->insert('comments', $data);
		
		$id = $this->db->insert_id();
		
		return $id;
	}	
	
	function update($cid){
		$this->db->where('id=?', $cid); 
		//$this->db->update('comments', $data);
		$this->db->update('comments', ['project_id'=> $cid]);
	}

	function getList($cond = [])
	{
		if (isset($cond['project_id'])) {
			$this->db->where('project_id=?', $cond['project_id']);
		}
		if (isset($cond['num'])) {
			$this->db->limit = $cond['num'];
		}
		if (isset($cond['parent_id'])) {
			$this->db->where('parent_id=?', $cond['parent_id']);
		}	
		if (isset($cond['replies'])) {
			$this->db->where('parent_id > 0');
		}	
		
		if (isset($cond['ordersub'])) {
			$this->db->order = 'comments.id ASC';
		} else {
			$this->db->order = 'comments.id DESC';
		}		
					
		//$this->db->where('parent_id=?', 0);
		//$this->db->order = 'comments.id DESC';

		$this->db->join('users', 'users.id=comments.user_id');

		$comments = $this->db->select('comments', 'project_id, content, img, user_id, users.name, parent_id, users.image, comments.date_created, comments.id'); // []

//		foreach ($comments as &$comment) {
//			if ($comment['image'] != '' && !strstr($comment['image'], '//')) {
//				$comment['image'] = userModel::image_folder . $comment['image'];
//				//$comment['image'] = $comment['image'];
//			}
//			
//		}
		//var_dump($comments);
		return $comments;
	}
	
	function getSubcomments(){
		$this->db->where('parent_id > 0');
		$this->db->order = 'comments.id ASC';
		$this->db->join('users', 'users.id=comments.user_id');

		$subcomments = $this->db->select('comments', 'project_id, content, img, user_id, users.name, parent_id, user_slug, users.image, comments.date_created, comments.id');
		
		return $subcomments;
	}

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