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

class Downloads extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('downloadsModel', '', TRUE);
        $this->load->model('FileModel', '', TRUE);
    }

    public function index()
    {
        if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 9) redirect(base_url('login'));
        $downloads = $this->downloadsModel->getDownloads();
        $this->load->view('admin/sidebar', ['title' => 'Alle Downloads']);
        $this->load->view('admin/downloads', ['downloads' => $downloads]);
        $this->load->view('admin/footer');
    }

    public function edit($id = NULL)
    {
        if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 9) redirect(base_url('login'));
        $edit = $id === NULL ? false : true;
        $p = $this->input->post(['title', 'description', 'descriptionEnglish', 'image', 'url', 'datetime']);

        if ($edit) {
            if ($this->downloadsModel->checkIfExists($id)) {
                $downloadContent = $this->downloadsModel->getDownload($id);
            } else {
                redirect(base_url('admin/downloads/edit'));
            }

            if ($p['title'] != NULL && $p['description'] != NULL && $p['datetime'] != NULL && $p['url'] !== NULL) {
                $imgurl = '/assets/images/placeholder.jpg';
                if (isset($_FILES['downloadImage']) && $_FILES['downloadImage']['size'] > 0) {
                    $fileName = $_FILES['downloadImage']['name'];
                    $tmpName = $_FILES['downloadImage']['tmp_name'];
                    $fileSize = $_FILES['downloadImage']['size'];
                    $fileType = $_FILES['downloadImage']['type'];
                    unset($_FILES['downloadImage']);

                    $imgurl = $this->FileModel->uploadFile($fileName, $tmpName, $fileSize, $fileType);
                }
                $this->downloadsModel->addNewDownload($p['datetime'], $p['title'], $p['description'], $p['descriptionEnglish'], $imgurl, $p['url']);
                redirect(base_url('admin/downloads/edit/' . $id));
            }

            $this->load->view('admin/sidebar', ['title' => 'Download-Eintrag bearbeiten']);
            $this->load->view('admin/download_edit', ['edit' => $edit, 'content' => $downloadContent]);
            $this->load->view('admin/footer');
        } else {
            if ($p['title'] != NULL && $p['description'] != NULL && $p['datetime'] != NULL && isset($_FILES['downloadImage']) && $_FILES['downloadImage']['size'] > 0 && $p['url'] !== NULL) {
                $fileName = $_FILES['downloadImage']['name'];
                $tmpName = $_FILES['downloadImage']['tmp_name'];
                $fileSize = $_FILES['downloadImage']['size'];
                $fileType = $_FILES['downloadImage']['type'];

                $imgurl = $this->FileModel->uploadFile($fileName, $tmpName, $fileSize, $fileType);
                unset($_FILES['downloadImage']);

                $this->downloadsModel->addNewDownload($p['datetime'], $p['title'], $p['description'], $p['descriptionEnglish'], $imgurl, $p['url']);
                redirect(base_url('admin/downloads/edit'));
            }

            $this->load->view('admin/sidebar', ['title' => 'Download-Eintrag erstellen']);
            $this->load->view('admin/download_edit', ['edit' => $edit]);
            $this->load->view('admin/footer');
        }
    }

}