<?php
/**
 * DokuWiki Plugin do (Action Component)
 *
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
 * @author  Andreas Gohr <gohr@cosmocode.de>
 * @author  Adrian Lang <lang@cosmocode.de>
 * @author  Dominik Eckelmann <eckelmann@cosmocode.de>
 */

class action_plugin_do extends DokuWiki_Action_Plugin
{

    /**
     * Register handlers for some event hooks
     *
     * @param Doku_Event_Handler $controller
     */
    public function register(Doku_Event_Handler $controller)
    {
        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call');
        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_act_preprocess');
        $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'handle_delete');
        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, '_adduser');
    }

    /**
     * @param Doku_Event $event event object by reference
     * @param null       $param the parameters passed to register_hook when this handler was registered
     */
    public function _adduser(&$event, $param)
    {
        if (!isset($_SERVER['REMOTE_USER'])) {
            return;
        }
        global $JSINFO;
        /** @var helper_plugin_do $hlp */
        $hlp = plugin_load('helper', 'do');
        $JSINFO['plugin_do_user'] = $_SERVER['REMOTE_USER'];
        $JSINFO['plugin_do_user_name'] = $hlp->getPrettyUser($_SERVER['REMOTE_USER']);
        $JSINFO['plugin_do_user_clean'] = html_entity_decode(strip_tags($JSINFO['plugin_do_user_name']));
    }

    /**
     * @param Doku_Event $event event object by reference
     * @param null       $param the parameters passed to register_hook when this handler was registered
     *
     * @return bool
     */
    public function handle_ajax_call(&$event, $param)
    {
        if ($event->data == 'plugin_do') { // FIXME: refactor this into early return and switch
            // toggle status of a single task
            global $INPUT;

            $event->preventDefault();
            $event->stopPropagation();

            $id = cleanID($_REQUEST['do_page']);

            if (auth_quickaclcheck($id) < AUTH_EDIT) {
                if ($INPUT->server->has('REMOTE_USER')) {
                    echo -2; //not allowed
                } else {
                    echo -1; //not logged in
                }
                return;
            }

            /** @var helper_plugin_do $hlp */
            global $INPUT, $conf;
            $hlp = plugin_load('helper', 'do');
            $status = $hlp->toggleTaskStatus($id, $_REQUEST['do_md5'], $_REQUEST['do_commit']);

            // Si el estado es falso eliminamos el registro R**G
            if(!$status) {
                $namespace = getNS($id);
                $hlp->eliminarArchivoTaskStatus($id, $_REQUEST['do_md5']);
            }

            // rerender the page
            p_get_metadata($id, '', true);

            //Guardamos la cantidad de requerimientos del submodulo. R**G
            $hlp->actualizarProgresoReq($id);

            // Actualizar progreso de admin thread si la página pertenece a uno R**G
            $hlp->actualizarProgresoAdminThread($id);

            header('Content-Type: application/json; charset=utf-8');
            echo json_encode($status);
            

        } elseif ($event->data == 'plugin_do_status') {
            // read status for a bunch of tasks

            $event->preventDefault();
            $event->stopPropagation();

            $page = cleanID($_REQUEST['do_page']);

            if (auth_quickaclcheck($page) < AUTH_READ) {
                $status = array();
            } else {
                /** @var helper_plugin_do $hlp */
                $hlp = plugin_load('helper', 'do');
                $status = $hlp->getAllPageStatuses($page);
            }

            header('Content-Type: application/json; charset=utf-8');
            echo json_encode($status);
        } elseif ($event->data === 'plugin_do_userTasksOverlay') {
            $event->preventDefault();
            $event->stopPropagation();

            global $INPUT;

            if (!$INPUT->server->has('REMOTE_USER')) {
                http_status(401, 'login required');
                return false;
            }

            $user = $INPUT->server->str('REMOTE_USER');

            /** @var helper_plugin_do $hlp */
            $hlp = plugin_load('helper', 'do');
            $tasks = $hlp->loadTasks(array('status' => array('undone'), 'user' => $user));
            /** @var syntax_plugin_do_dolist $syntax */
            $syntax = plugin_load('syntax', 'do_dolist');
            $html = $syntax->buildTasklistHTML($tasks, true, false);
            header('Content-Type: text/html; charset=utf-8');
            echo $html;
        } elseif ($event->data == 'cargar_archivo_do') {
            // Manejo de subida de archivos completo por checkbox R**G
            $event->preventDefault();
            $event->stopPropagation();
        
            global $INPUT, $conf;
        
            if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
                $pageId = cleanID($INPUT->str('do_page'));
                $file = $_FILES['file'];
                $md5 = $INPUT->str('md5');
                $nsRuta = cleanID($INPUT->str('nsRuta'));
        
                // Validar permisos
                if (auth_quickaclcheck($nsRuta) < AUTH_UPLOAD) {
                    $response = [
                        'status' => 'error',
                        'message' => 'No tienes permisos para subir archivos.',
                    ];
                    header('Content-Type: application/json; charset=utf-8');
                    echo json_encode($response);
                    return;
                }

                $folderPath = $conf['mediadir'] . '/' . str_replace(':', '/', $nsRuta) . '/' . date('d-m-Y');
                $filename = cleanID($file['name']);
                $destination = $folderPath . '/' . $filename;
        
                io_mkdir_p($folderPath);
        
                if (move_uploaded_file($file['tmp_name'], $destination)) {
                    // Generar el enlace público
                    $publicPath = ml("$nsRuta:" . date('d-m-Y') . ":$filename", '', true, '&');

                    // Guardamos la informacion en la tabla de archivos task filename.
                    /** @var helper_plugin_do $hlp */
                    $hlp = plugin_load('helper', 'do');
                    $hlp->actualizarArchivoTaskStatus($md5, $filename, $pageId, $publicPath);

                    $response = [
                        'status' => 'success',
                        'message' => 'Archivo Procesado correctamente.',
                        'file' => $filename,
                        'path' => $publicPath,
                        'namespace' => $nsRuta,
                        'directorioRaiz' => $conf['mediadir'],
                        'rutaArchivo' => $folderPath,
                        'page' => $pageId
                    ];
                } else {
                    $response = [
                        'status' => 'error',
                        'message' => 'Error al mover el archivo al destino final.',
                    ];
                }
            } else {
                $response = [
                    'status' => 'error',
                    'message' => 'No se recibió ningún archivo.',
                ];
            }
        
            header('Content-Type: application/json; charset=utf-8');
            echo json_encode($response);
        }
    }

    /**
     * @param Doku_Event $event event object by reference
     * @param null       $param the parameters passed to register_hook when this handler was registered
     *
     * @return bool
     */
    public function handle_act_preprocess(&$event, $param)
    {

        if ($event->data != 'plugin_do') {
            return true;
        }
        global $INPUT;

        $pageid = cleanID($_REQUEST['do_page']);
        $status = '';
        if (auth_quickaclcheck($pageid) < AUTH_EDIT) {
            $lvl = -1;
            $key = 'notloggedin';
            if ($INPUT->server->has('REMOTE_USER')) {
                $key = 'notallowed';
            }
        } else {
            /** @var helper_plugin_do $hlp */
            $hlp = plugin_load('helper', 'do');
            $status = $hlp->toggleTaskStatus($pageid, $_REQUEST['do_md5']);
            if ($status == -2) {
                $lvl = -1;
                $key = 'notallowed';
            } else {
                $lvl = 1;
                if ($status) {
                    $key = 'done';
                } else {
                    $key = 'open';
                }
            }

        }

        $jslang = $this->getLang('js');
        msg(sprintf($jslang[$key], $status), $lvl);

        global $ACT;
        $ACT = 'show';
        return true;
    }

    /**
     * Delete all tasks associated with a page from database, if all have been removed from the page
     *
     * @param Doku_Event $event event object by reference
     * @param null       $param the parameters passed to register_hook when this handler was registered
     */
    public function handle_delete(&$event, $param)
    {
        if (preg_match('/<do[^>]*>.*<\/do>/i', $event->data[0][1])) {
            // Only run if all tasks where removed from the page, partial removes are handled in \syntax_plugin_do_do::_save
            return;
        }
        $namespace = $event->data[1] ? $event->data[1] . ':' : '';
        $id = $namespace . $event->data[2];
        if (isset($this->run[$id])) {
            // Only execute on the first run
            return;
        }

        /** @var helper_plugin_do $hlp */
        $hlp = plugin_load('helper', 'do');
        $hlp->cleanPageTasks($id);
        $this->run[$id] = true;
    }

}

