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

class Add_image extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library('session');
        $this->load->model('admin/Add_image_model');
        if (!isset($_SESSION['admin'])) {
            redirect('admin/Login');
        }
    }

    public function index()
    {
        $this->load->view('admin/header');
        $this->load->view('admin/Add-Images');
        $this->load->view('admin/footer');
    }

    public function add()
    {
        $data = $this->Add_image_model->insert();
        if($data){
            $this->session->set_flashdata('success', "All Images Inserted Successfully");
            redirect('admin/Add_image');
        }else{
            $this->session->set_flashdata('error', "something went wrong");
            redirect('admin/Add_image');
        }
    }

    public function view()
    {
        $sql = $this->db->order_by('id', "desc");
        $sql = $this->db->get('onlyimages');
        $data['tic'] = $sql->result();
        $this->load->view('admin/header');
        $this->load->view('admin/Add-Images', $data);
        $this->load->view('admin/footer');
    }

        public function delete($id)
    {
           $path  = 'uploads/allimages';
        $path1 = 'uploads/allimages/resized/';
        $path2 = 'uploads/allimages/thumbs/';
        $this->db->where('id', $id);
        $we = $this->db->get('onlyimages');
        $file = $we->row();
        $imgname = $file->name;
        unlink($path.'/'.$imgname);
        unlink($path1.'/'.$imgname);
        unlink($path2.'/'.$imgname);
        $dl = $this->db->where('id', $id);
        $dl = $this->db->delete('onlyimages');
        if($dl){
            $this->session->set_flashdata('success', "Deleted Successfully");
            redirect('admin/Add_image/view');
        }else{
            $this->session->set_flashdata('error', "something went wrong");
            redirect('admin/Add_image/view');
        }
    }

}