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

class Testimonial extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('common_model');
		if(get_cookie('admin_id'))
		{
			$this->session->set_userdata('admin_id',get_cookie('admin_id'));
		}
		if(!$this->session->userdata('admin_id'))
		{
			redirect('admin/login');
		}
	}

	public function index(){
		//echo "TEST";die;
		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$content_data['adverts'] = $this->common_model->getAllMultipleJoin('testimonials','','','display_order ASC');
		//echo '<pre>';print_r($content_data);die;
		$data['content'] = $this->load->view('admin/testimonials/list', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function edit($edit_id=''){
		if($edit_id!=''){
			$advertId = base64_decode($edit_id);
			if($this->input->post()){
				$Details_on = array(
	            	'name' => $this->input->post('name_1'),
					'text' => $this->input->post('text_1')
				);//print_r($Details_on); die;
	            $this->common_model->UpdateData('testimonials',$Details_on,array('id'=>$advertId));
	            $languages = $this->common_model->getAll('sys_languages');
				$arr= array();
				if(!empty($languages))
				{
					foreach ($languages as $value) {
						$arr['text'] = $this->input->post('text_'.$value['id']);
						$arr['name'] = $this->input->post('name_'.$value['id']);
						$exist = $this->common_model->getAllRow('testimonial_values',array('testimonial_id'=>$advertId,'lang_id'=>$value['id']));
						if(empty($exist))
						{
							$arr1['testimonial_id'] = $advertId;
							$arr1['text'] = $this->input->post('text_'.$value['id']);
							$arr1['name'] = $this->input->post('name_'.$value['id']);
							$arr1['lang_id'] = $value['id'];
							$this->common_model->insertData('testimonial_values',$arr1);
						}
						else
						{
							$this->common_model->UpdateData('testimonial_values',$arr,array('testimonial_id'=>$advertId,'lang_id'=>$value['id']));
						}
					}
				}
	            //print_r($_FILES['file']); die;
				if($_FILES['file']['name']!='')
				{
					$imagefolder = 'img/section-image/' ;
					$uploadpath = 'assets/userfiles/'. $imagefolder;
					/*if (!file_exists(FCPATH . $uploadpath)) {
						mkdir(FCPATH . $uploadpath, 0777, true);
					} */ 
					$filename = microtime().$_FILES['file']['name'];
					$config = array(
						'upload_path' 		=> FCPATH . $uploadpath . '/',
						'allowed_types' 	=> 'jpg|JPG|png|PNG|jpeg|JPEG',
						'encrypt_name' 		=> true,
						'file_name'			=> $filename,
						'file_ext_tolower' 	=> true,
						//'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
					);

					$this->load->library('upload', $config);
				   
					if(!$this->upload->do_upload('file')) {

						echo $this->upload->display_errors();
					}

					$uploaddata = $this->upload->data();
					if($uploaddata)
					{
						$file = array(
						'user_id' 	=> $this->session->userdata('user_id'),
						'path' 		=> $imagefolder,
						'filename' 	=> $uploaddata['raw_name'],
						'extension' => substr($uploaddata['file_ext'], 1),
						'mimetype' 	=> $uploaddata['file_type'],
						'is_image' 	=> $uploaddata['is_image'],
						'attr_alt' 	=> 'Bild von ' ,
						'created'	=> time(),
						//'temp_name'	=> $uploaddata['raw_name']
						);
						
						$imagesize = getimagesize($uploaddata['full_path']);
						$file['width'] 	= $imagesize[0];
						$file['height'] = $imagesize[1];


						$fileId = $this->common_model->insertData('sys_files',$file);
						$this->common_model->UpdateData('testimonials',array('image_id'=>$fileId),array('id'=>$advertId));
					}
				}	

	            $this->utility->setMsg('Testimonials added','SUCCESS');	
				redirect(base_url().'admin/testimonial');
			}
			$content_data = array();
			$content_data['language'] = $this->common_model->getAll('sys_languages');
			$content_data['advertDetails'] = $this->common_model->getAllRow('testimonials',array('id'=>$advertId)) ;
			$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
			$data['content'] = $this->load->view('admin/testimonials/edit', $content_data, TRUE);
			$this->load->view('admin/layout', $data);
		}else{
			//echo "Edit Failed";die;
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/testimonial');
		}
	}
	public function visibility($advert_id,$updated_status){
		$advertId = base64_decode($advert_id);
		$update_status = array(
			'status' => $updated_status
		);
		$this->common_model->UpdateData('testimonials',$update_status,array('id'=>$advertId));
		$this->utility->setMsg('Testimonials status changed','SUCCESS');	
		redirect(base_url().'admin/testimonial');
	}
	public function delete($advert_id='')
	{
		//echo $cat_id;die;
		if($advert_id!='')
		{
			$advertId=base64_decode($advert_id);
			$this->common_model->deleteData('testimonials',array('id'=>$advertId));
			$this->common_model->deleteData('testimonial_values',array('testimonial_id'=>$advertId));
			$this->utility->setMsg('Testimonial deleted','SUCCESS');	
			redirect(base_url().'admin/testimonial');
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/testimonial');
		}
	}
	public function display_order($display_order,$type,$blog_id){
		if($type == 1)
		{
			$new = $display_order-1;
		}
		else
		{
			$new = $display_order+1;
		}
		$update_status = array(
				'display_order' => $new,
			);
		$get = $this->common_model->getAllRow('testimonials',array('display_order'=>$new));
		$this->common_model->UpdateData('testimonials',array('display_order'=>$display_order),array('id'=>$get['id']));
		$this->common_model->UpdateData('testimonials',$update_status,array('id'=>$blog_id));
		$this->utility->setMsg('Display order updated','SUCCESS');	
		redirect(base_url().'admin/testimonial');
	}
	
}