<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Account extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		if($this->session->userdata('is_login') && $this->session->userdata('user_id')){
			$this->load->model('user_model');
			$this->load->model('categories_model');
                        $parent_categories=$this->categories_model->getParentCategories();
			$data['parent_categories']=$parent_categories;
			$data['user']=$this->user_model->getUser($this->session->userdata('user_id'));
			$data['main_content']='profile';
			$data['title']=$this->session->userdata('user_name').'- Profile';
			$data['bodyid']='profile';
			$this->load->view('template',$data);
		}else{
			redirect(base_url());
		}		
	
		
	}
        public function listings(){
            if($this->session->userdata('is_login') && $this->session->userdata('user_id')){
			$this->load->model('user_model');
			$this->load->model('postings_model');
			$this->load->model('categories_model');
                        $this->load->model('inquiries_model');
			$parent_categories=$this->categories_model->getParentCategories();
			$data['parent_categories']=$parent_categories;
			$listings=$this->user_model->getUserPostings($this->session->userdata('user_id'));
			$data['listings']=$listings;		
			$data['main_content']='listings';
			$data['title']=$this->session->userdata('user_name');
			$data['bodyid']='userlitsings';
			$this->load->view('template',$data);
		}else{
			redirect(base_url());
		}	
        }

        public function verify($id,$vcode){
		$this->load->model('user_model');
		$user=$this->user_model->getUser($id);
		$vcodedb=$user->vcode;
		if($vcode==$vcodedb){
			$data=array('verified'=>1);
			$this->db->where('id', $user->id);
			$this->db->update('users', $data); 
                        $data1=array('published'=>1);
                        $this->db->where('user_id',$user->id);
                        $this->db->update('postings', $data1); 
		}
		redirect(base_url());
	}
	public function sendpassword(){
            $email=  $_POST['email'];
		$this->load->model('user_model');
		$userid=$this->user_model->checkUser($email);
		if($userid){
			$user=$this->user_model->getUser($userid);
			$newpassword=generatePassword();
			$data=array('password'=>md5($newpassword));
			$this->db->where('id', $userid);
			$this->db->update('users', $data); 
			$fromname="Dewas.co.in Support";
			$fromemail="support@dewas.co.in";
			$toname=$user->name;
			$toemail=$user->email;
			$subject='New Password';
			$content="Dear ".$toname.",<br> New password for your account: ".$newpassword."<p>You can login using this password .</p><p>Thanks,<br> Dewas.co.in</p>";
			$this->load->library('email');
			$config['protocol'] = 'sendmail';
			$config['mailtype'] = 'html';
			$this->email->initialize($config);
			$this->email->from($fromemail, $fromname);
			$this->email->to($toemail,$toname);
			$this->email->subject($subject);
			$this->email->message($content);
			$this->email->send();
			$data['success'] = true;
            		
		}else{
			$data['success'] = false;
            		$data['message'] = 'Email address does not match.';
		}
		 echo json_encode($data);
	}
	public function editad($id){
		$this->load->model('postings_model');
		$details=$this->postings_model->getDetails($id);
		//print_r($details);
		if($this->session->userdata('is_login') && $this->session->userdata('user_id') && $this->session->userdata('user_id') == $details->user_id){
			
			$this->load->model('categories_model');
			$parent_categories=$this->categories_model->getParentCategories();
			$data['parent_categories']=$parent_categories;
			$data['details']=$details;
			$data['main_content']='editad';
			$data['title']='Edit Ad';
			$data['bodyid']='edit-list';
			$this->load->view('template',$data);
		}else{
			redirect(base_url());
		}
	}

	public function deletead($id){
		$this->load->model('postings_model');
		$details=$this->postings_model->getDetails($id);
		if($this->session->userdata('is_login') && $this->session->userdata('user_id') && $this->session->userdata('user_id') == $details->user_id){
			$images=$this->postings_model->getPostingImages($details->id);
			if(count($images) > 0){
				$item_image=$images->image_url;
				$path=dirname($_SERVER['SCRIPT_FILENAME']).'/assets/adimages/';
				if(file_exists($path.$item_image)){
					//print_r(glob($path."*".$item_image));
					// Code to delete all thumbanils of the image created dynamically along with main image
					array_map('unlink', glob($path."*".$item_image));
				}
			}
			$this->db->delete('postings', array('id' => $id));
			redirect(base_url().'account');
		}else{
			redirect(base_url());
		}
	}
        public function deleteenq($enqid){
            $this->load->model('inquiries_model');
            $enq=$this->inquiries_model->getDetails($enqid);
            $this->load->model('postings_model');
            $listing=$this->postings_model->getDetails($enq->listing_id);
            if($this->session->userdata('is_login') && $this->session->userdata('user_id') && $this->session->userdata('user_id') == $listing->user_id){
                $this->db->delete('enquiries', array('id' => $enqid));
                redirect(base_url().'account');
            }else{
			redirect(base_url());
            }
        }
        public function deletereply($id){
            $this->load->model('replies_model');
            $rep=$this->replies_model->getDetails($id);
            $this->load->model('inquiries_model');
            $enq=$this->inquiries_model->getDetails($rep->inquiry_id);
            
            if($this->session->userdata('is_login') && $this->session->userdata('user_id') && $this->session->userdata('user_id') == $enq->user_id){
                $this->db->delete('replies', array('id' => $id));
                redirect(base_url().'account/inbox');
            }else{
			redirect(base_url());
            }
        }

        public function changepassword(){
            $data=null;
            if(isset($_POST) && !empty($_POST)){
                $newpassword=$_POST['newpassword'];
                $currentpassword=$_POST['currentpassword'];
                $user_id=$this->session->userdata('user_id');
                $this->load->model('user_model');
		$checkpass=$this->user_model->checkPassword($user_id,$currentpassword);
                if($checkpass){
                    $data = array(
			   'password' => md5($newpassword)
                        );
                    $result=$this->db->update('users', $data, array('id' => $user_id));
                    if($result){
                        $data['success']=true;
                    }else{
                        $data['success']=false;
                        $data['message']='There was a problem updating your password.Please try again later.';
                    }
                }else{
                    $data['success']=false;
                    $data['message']='Current password did not match.';
                }
                 echo json_encode($data);
            }else{
                $this->load->view('changepassword',$data);
            }
        }
        public function enquiries($listing_id=null){
            if($this->session->userdata('is_login') && $this->session->userdata('user_id')){
            $data=null;
            $this->load->model('inquiries_model');
            $this->load->model('postings_model');
            $this->load->model('categories_model');
            $parent_categories=$this->categories_model->getParentCategories();
            $data['parent_categories']=$parent_categories;
            $enquiries=$this->inquiries_model->getInquiries($listing_id);
            if($listing_id==null){
                $data['allenq']=true;
               
            }
             $data['listid']=$listing_id;
            $data['enquiries']=$enquiries;
            $data['main_content']='enquiries';
            $data['title']=$this->session->userdata('user_name');
            $data['bodyid']='user-enquiries';
            $this->load->view('template',$data);
            }else{
			redirect(base_url());
		}
        }
         public function replyEnquiry($enq_id){
             $this->load->model('inquiries_model');
             //$this->load->model('inquiries_model');
             $enquiry=$this->inquiries_model->getDetails($enq_id);
             $data['enquiry']=$enquiry;
             $update=array('view_status'=>1);
             $updateresult=$this->db->update('inquiries', $update, array('id' => $enq_id));
             if(isset($_POST) && !empty($_POST)){
                $comment=$_POST['response'];
                
                $user_id=$this->session->userdata('user_id');
                
                
                    $data = array(
                        'inquiry_id'=>$enq_id,
                        'user_id' => $user_id,
                        'comment' => $comment,
                        'view_status' => 0,
			'dt_created' => date('Y:m:d h:i:s'),
			'dt_modified'=> date('Y:m:d h:i:s')
                        );
                    $result=$this->db->insert('replies', $data);
                    if($result){
                        $data['success']=true;
                    }else{
                        $data['success']=false;
                        $data['message']='There was a problem submitting response.Please try again later.';
                    }
                
                 echo json_encode($data);
            }else{
            $this->load->view('enqdetails',$data);
            }
         }
         public function inbox($enqid=null){
            if($this->session->userdata('is_login') && $this->session->userdata('user_id')){
            $data=null;
            $this->load->model('inquiries_model');
            $this->load->model('replies_model');
            $this->load->model('postings_model');
            $this->load->model('categories_model');
            $parent_categories=$this->categories_model->getParentCategories();
            $data['parent_categories']=$parent_categories;
            $replies=$this->replies_model->getReplies($enqid);
            if($enqid==null){
                $data['allrep']=true;
               
            }
            $data['enqid']=$enqid;
            $data['replies']=$replies;
            $data['main_content']='replies';
            $data['title']=$this->session->userdata('user_name').' - Inbox';
            $data['bodyid']='replies';
            $this->load->view('template',$data);
            }else{
			redirect(base_url());
		}
        }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
