<?php
/**
 * @version        1.14.0
 * @package        Joomla
 * @subpackage     Edocman
 * @author         Tuan Pham Ngoc
 * @copyright      Copyright (C) 2011 - 2025 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */

defined('_JEXEC') or die;

use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Session\Session;

class EdocmanControllerImportdb extends EDocmanController 
{
    public function display($cachable = false, $urlparams = array())
    {
        $view = $this->getView('import', 'html');
        $model = $this->getModel('import');
        $view->setModel($model, true);
        
        parent::display($cachable, $urlparams);
    }
    
    public function upload()
    {
        // Check for request forgeries
        Session::checkToken() or jexit(Text::_('JINVALID_TOKEN'));
        
        $app = Factory::getApplication();
        $input = $app->getInput();
        
        try {
            $model = $this->getModel('importdb');
            
            $options = array(
                'clear_existing' => $input->get('clear_existing', 0, 'int'),
                'update_existing' => $input->get('update_existing', 0, 'int'),
                'import_files' => $input->get('import_files', 0, 'int')
            );
            
            $result = $model->importData($options);
            
            if ($result['success']) {
                $app->enqueueMessage($result['message'], 'success');
            } else {
                $app->enqueueMessage($result['message'], 'error');
            }
            
        } catch (Exception $e) {
            $app->enqueueMessage('Import failed: ' . $e->getMessage(), 'error');
        }
        
        $this->setRedirect(Route::_('index.php?option=com_edocman&view=importdb', false));
    }
}
