<?php

class Vvvwebupload extends CI_Controller {

	var $module = 'blog';
	public function __construct() {
		// $this->load does not exist until after you call this
		parent::__construct(); // Construct CI's core so that you can use it

		$this->load->database();
		$this->load->helper('language');
		$this->load->library('session');
		$this->load->model('Security', 'security_model');
		$this->load->model('Blog_Model', 'blog_model');
		$this->load->model('Language', 'language_model');
		$this->load->model('Vvweb_Model', 'vvweb_model');
		//require_once(APPPATH.'controllers/admin/Security.php'); //include controller
		//$security = new Security();  //create object
		//$security->permission(); //call function

		//check user have login or not
		$this->security_model->admin($this->session->userdata('session_id'), $this->session->userdata('user_id'), $this->session->userdata('username'));
		//end check user have login or not
	}
	function index() {

		$session_id = $this->security_model->getcode();
		//$sess = $this->security_model->admin($session_id);
		//$property = $this->Property->get();

		//check user have login or not
		// $this->security_model->admin($this->session->userdata('session_id'),$this->session->userdata('user_id'),$this->session->userdata('username'));
		//end check user have login or not

		//$data['sess'] = $sess;
		//$data['property'] = $property;
		$data['module'] = $this->module;
		$data['blog_content'] = $this->blog_model->get();
		$data['selectedmenu'] = "question_submit";

		$this->load->view('admin/blog_view.php', $data);

	}
	function upload() {
		// echo "http://amigo.at-appmaker.com/assets/images/uploads/202012171533032367.png";
		define("MAX_SIZE", "3000");
		$result = array();
		// $result['message']="";
		// $result['url']="";
		// $result['status']="";

		//upload code
		//handle upload file
		$status = "success";

		$image = $_FILES['file']["name"];
		$uploadedfile = $_FILES['file']['tmp_name'];

		if ($image) {

			$filename = stripslashes($_FILES['file']['name']);

			$extension = $this->getExtension($filename);
			$extension = strtolower($extension);

			if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
				// echo "Unknown Extension..!";
				$status = "fail";
				$message = "Unknown Extension..!";
			} else {
				$size = filesize($uploadedfile);
				if ($size > MAX_SIZE * 1024) {
					// echo "File Size Excedeed..!!";
					$status = "fail";
					$message = "File Size Excedeed..!!";
				}

				if ($extension == "jpg" || $extension == "jpeg") {
					//$uploadedfile = $_FILES[$filename]['tmp_name'];
					$src = imagecreatefromjpeg($uploadedfile);

				} else if ($extension == "png") {
					//$uploadedfile = $_FILES[$filename]['tmp_name'];
					$src = imagecreatefrompng($uploadedfile);

				} else {
					$src = imagecreatefromgif($uploadedfile);
					// echo $scr;
				}

				list($width, $height) = getimagesize($uploadedfile);

				if ($width > 1000) {
					//if width >1000 then resize it
					$newwidth = 1000;
					$newheight = ($height / $width) * $newwidth;
				} else {
					$newwidth = $width;
					$newheight = $height;
				}

				$tmp = imagecreatetruecolor($newwidth, $newheight);

				//Keep transparent of image

				imageAlphaBlending($tmp, false);
				imageSaveAlpha($tmp, true);
				//end Keep transparent of image

				// $newwidth1=1000;
				// $newheight1=($height/$width)*$newwidth1;
				// $tmp1=imagecreatetruecolor($newwidth1,$newheight1);

				imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

				// $filename = "../images/product-image/Cars/". $_FILES['image-1']['name'];
				$newName = date('YmdHis') . rand(1000, 9999);
				$filename = $_SERVER['DOCUMENT_ROOT'] . "/assets/images/uploads/" . $newName . "." . $extension;
				// imagejpeg($tmp, $filename, 100);

				//change to png for transparent image
				imagepng($tmp, $filename, 0);
				//end change to png for transparent image

				imagedestroy($src);
				imagedestroy($tmp);
				$url = 'assets/images/uploads/' . $newName . "." . $extension;
				$message = "successful upload";
			}
		}
		// imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);

		// $result['message']=$message;
		// $result['url']=$url;
		// $result['status']=$status;

		// return $result;

		echo base_url() . $url;

	}
	function save() {
		// echo $this->input->post('html');
		$lastsegment = $this->input->post('lastsegment');

		$arr = array();
		$arr['partccontent'] = $this->input->post('html');
		// $this->test_vweb_model->insert($arr);
		$result = $this->vvweb_model->update($lastsegment, $arr);

		if ($result == true) {
			echo '成功儲存';
		} else {
			echo '不能儲存';
		}

		// $data['blog_content'] = $this->blog_model->get_id($id);
	}

	function getcontent() {
		$result = $this->vvweb_model->get_id(10);
		echo $result[0]->partccontent;
	}
	function getcontentbyid() {
		$uri = $this->uri->segment(4);
		$result = $this->vvweb_model->get_id($uri);
		echo $result[0]->partccontent;
	}
	function loadeditwebpage() {
		// $id = $this->input->get('id');
		$id = $this->uri->segment(4);

		$data['id'] = $id;
		$this->load->view('admin/vvvwebedit.php', $data);

	}

	function edit() {

		// echo "1";
		//$session_id = $this->security_model->getcode();
		//$this->security_model->admin($this->session->userdata('session_id'),$this->session->userdata('user_id'),$this->session->userdata('username'));
		//$property = $this->Property->get();

		//echo $this->input->post('save');

		if ($this->input->post('save')) {
			//echo "post save";
			$id = $this->input->post('id');

			$passtest = true;
			if (is_uploaded_file($_FILES['blog_image']['tmp_name'])) {
				$result = $this->uploadfile('blog_image');
				//     $result['message']="";
				// $result['url']="";
				// $result['status']="";
				// $arr['partcimage'] =$url;
				if ($result['status'] == 'fail') {
					echo $result['message'];
					$passtest = false;
				} else {
					$arr['blog_image'] = $result['url'];
					if (!empty($this->input->post('blog_imageolddata'))) {
						$replacestring = str_replace(base_url(), "", $this->input->post('blog_imageolddata'));
						unlink($replacestring);
					}
				}
			} else {
				$arr['blog_image'] = $this->input->post('blog_imageolddata');
			}

			$arr['blog_video'] = $this->input->post('blog_video');
			// $arr['blog_image'] = $this->input->post('blog_image');
			$arr['blog_title'] = $this->input->post('blog_title');
			$arr['blog_short_text'] = $this->input->post('blog_short_text');
			$arr['blog_long_text'] = $this->input->post('blog_long_text');
			$arr['blog_date'] = $this->input->post('blog_date');
			$arr['facebooklink'] = $this->input->post('facebooklink');

			// $arr['keywords'] = $this->input->post('keywords');
			// $arr['lang'] = $this->input->post('lang');

			if ($id == false) {
				$arr['entered'] = date('Y-m-d H:i:s');
				$id = $this->blog_model->insertForm($arr);
				header('location:' . base_url() . 'admin/blog/edit?save=1&id=' . $id);
				exit;
			} else {
				$this->blog_model->update($id, $arr);
				header('location:' . base_url() . 'admin/blog/edit?save=1&id=' . $id);
				exit;
			}
		}

		$id = $this->input->get('id');

		$data['languages'] = $this->language_model->get_all();
		//$data['sess'] = $sess;
		//$data['sess'] = $session_id;
		//$data['property'] = $property;
		//$data['module'] = $this->module;

		$data['blog_content'] = $this->blog_model->get_id($id);

		$data['save'] = $this->input->get('save');
		$data['selectedmenu'] = "blog";
		$this->load->view('admin/blog_edit_view.php', $data);

	}
	function delete($id) {
		//$this->db->delete('items', array('id' => $id));
		$this->blog_model->delete($id);
		echo 'Deleted successfully.';
	}
	function uploadfile($filename) {
		define("MAX_SIZE", "3000");
		$result = array();
		$result['message'] = "";
		$result['url'] = "";
		$result['status'] = "";

		//upload code
		//handle upload file
		$status = "success";

		$image = $_FILES[$filename]["name"];
		$uploadedfile = $_FILES[$filename]['tmp_name'];

		if ($image) {

			$filename = stripslashes($_FILES[$filename]['name']);

			$extension = $this->getExtension($filename);
			$extension = strtolower($extension);

			if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
				// echo "Unknown Extension..!";
				$status = "fail";
				$message = "Unknown Extension..!";
			} else {
				$size = filesize($uploadedfile);
				if ($size > MAX_SIZE * 1024) {
					// echo "File Size Excedeed..!!";
					$status = "fail";
					$message = "File Size Excedeed..!!";
				}

				if ($extension == "jpg" || $extension == "jpeg") {
					//$uploadedfile = $_FILES[$filename]['tmp_name'];
					$src = imagecreatefromjpeg($uploadedfile);

				} else if ($extension == "png") {
					//$uploadedfile = $_FILES[$filename]['tmp_name'];
					$src = imagecreatefrompng($uploadedfile);

				} else {
					$src = imagecreatefromgif($uploadedfile);
					// echo $scr;
				}

				list($width, $height) = getimagesize($uploadedfile);

				if ($width > 1000) {
					//if width >1000 then resize it
					$newwidth = 1000;
					$newheight = ($height / $width) * $newwidth;
				} else {
					$newwidth = $width;
					$newheight = $height;
				}

				$tmp = imagecreatetruecolor($newwidth, $newheight);

				// $newwidth1=1000;
				// $newheight1=($height/$width)*$newwidth1;
				// $tmp1=imagecreatetruecolor($newwidth1,$newheight1);

				imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

				// $filename = "../images/product-image/Cars/". $_FILES['image-1']['name'];
				$newName = date('YmdHis') . rand(1000, 9999);
				$filename = $_SERVER['DOCUMENT_ROOT'] . "/assets/images/uploads/" . $newName . "." . $extension;
				imagejpeg($tmp, $filename, 100);

				imagedestroy($src);
				imagedestroy($tmp);
				$url = 'assets/images/uploads/' . $newName . "." . $extension;
				$message = "successful upload";
			}
		}
		// imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);

		$result['message'] = $message;
		$result['url'] = $url;
		$result['status'] = $status;

		return $result;
	}

	private function getExtension($str) {
		$i = strrpos($str, ".");
		if (!$i) {return "";}
		$l = strlen($str) - $i;
		$ext = substr($str, $i + 1, $l);
		return $ext;
	}

}

?>