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

class GalleryPlugin extends CI_Controller {

	public function __construct(){
		parent::__construct();
	}
	
	public function fileUpload(){
		
		if(!empty($_FILES['file']['name'])){
				
			// Set preference
			$config['upload_path'] = 'uploads/';	
			$config['allowed_types'] = 'jpg|jpeg|png|gif';
			$config['max_size']    = '1024'; // max_size in kb
			$config['file_name'] = $_FILES['file']['name'];
					
			//Load upload library
			$this->load->library('upload',$config);			
				
			// File upload
			if($this->upload->do_upload('file')){
				// Get data about the file
				$uploadData = $this->upload->data();
			}
		}
		
	}
	
	public function uploadgalleryimages()
    {
		
		if ($this->input->is_ajax_request()) {
		   $upath = $_POST['folder'];
            if (!file_exists($upath)) {
                mkdir($upath, 0777);
            }

            $files = $_FILES;
            $cpt = count($_FILES['others']['name']);
            for ($i = 0; $i < $cpt; $i++) {
                unset($_FILES);
                $_FILES['others']['name'] = $files['others']['name'][$i];
                $_FILES['others']['type'] = $files['others']['type'][$i];
                $_FILES['others']['tmp_name'] = $files['others']['tmp_name'][$i];
                $_FILES['others']['error'] = $files['others']['error'][$i];
                $_FILES['others']['size'] = $files['others']['size'][$i];
				//print_r($_FILES);
                $this->upload->initialize(array(
                    'upload_path' => $upath,
                    'allowed_types' => 'gif|jpg|png|jpeg|JPG|PNG|JPEG'
                ));
                $this->upload->do_upload('others');
			}  
        }
    }
	
	
public function loadgalleryimages()
    {
       //print_r($_POST);
		$output='';
         $dir = $_POST['folder'];
            
			if (is_dir($dir)) {
                if ($dh = opendir($dir)) {
                    $i = 0; 
				   while (($file = readdir($dh)) !== false) {
						 if ($file != "." && $file != ".."){
                           $output .= '
                                <div class="other-img" id="image-container-' . $i . '">
                                    <img src="' . base_url($dir.'/'.$file) . '" style="width:100px; height: 100px;">
                                    <a href="javascript:void(0);" onclick="removeSecondaryProductImage(\'' . $file . '\', \'' . $dir . '\', ' . $i . ')">
                                        <span class="fa fa-trash"></span>
                                    </a>
									<input type="hidden" name="images[]" value="'.$dir.'/'.$file.'" >
                                </div>
                               ';
                        }
                        $i++;
                    }
                    closedir($dh);
                }
            }
        if ($this->input->is_ajax_request()) {
            echo $output;
        } else {
            return $output;
        }
    }

    /*
     * called from ajax
     */

    public function removegallery()
    {
		if ($this->input->is_ajax_request()) {
            $img = $_POST['folder']."/".$_POST['image'];
			unlink($img);
        }
    }
}
