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

/**
 * Description of filemanager_admin
 *
 * @author Igor Ratsun
 */
class Filemanager_admin extends \Ciext\Controller\AbstractAdminController
{
    public function manage()
    {
        $this->load->model('filemanager/filemanager_crud');

        return ['content' => $this->filemanager_crud->render(), 'title' => $this->lang->line('Uploaded media files')];
    }

    public function upload()
    {
        $type = $this->input->get('type');
        $funcNum = $this->input->get('CKEditorFuncNum');
        $url = '';
        $message = '';

        $config['upload_path'] = './assets/uploads/multimedia/';
        if ($type == 'images') {
            $config['upload_path'] .= 'images/';
            $config['allowed_types'] = 'png|jpe|jpg|jpeg|gif|webp|bmp';
        } else {
            $config['upload_path'] .= 'files/';
            $config['allowed_types'] = '*';
        }

        $this->load->library('upload')->initialize($config);

        if (!$this->upload->do_upload('upload')) {
            $message = $this->upload->display_errors('', '');
        } else {
            $this->load->model('filemanager/media_tmodel');
            $data = $this->upload->data();
            $url = base_url(trim($config['upload_path'], '.') . $data['file_name']);
            $this->media_tmodel->add([
                'name'       => $data['file_name'],
                'type'       => $data['file_type'],
                'size'       => $data['file_size'],
                'is_image'   => $data['is_image'],
                'image_type' => $data['image_type'],
                'created'    => date('Y-m-d H:i:s'),
            ]);
        }

        echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($funcNum,'$url','$message')</script>";
        exit;
    }
}

