<?php

/**
 * i-doit
 *
 * Floorplan selection popup.
 *
 * @package     Modules
 * @subpackage  Floorplan
 * @author      Leonard Fischer <lfischer@i-doit.com>
 * @version     1.0.0
 * @copyright   synetics GmbH
 * @license     http://www.i-doit.com/license
 * @since       i-doit 1.5.0
 */
class isys_popup_qcw_profiles extends isys_popup_qcw_adjust
{
    /**
     * Handle specific ajax requests.
     *
     * @param   isys_module_request $p_modreq
     *
     * @return  mixed
     * @author  Leonard Fischer <lfischer@i-doit.org>
     * @throws  isys_exception_objectbrowser
     */
    public function &handle_ajax_request(isys_module_request $p_modreq)
    {
        header('Content-Type: application/json');

        $response = [
            'success' => true,
            'data'    => null,
            'message' => null
        ];

        try {
            isys_module_system::getAuth()->check(isys_auth::EXECUTE, 'GLOBALSETTINGS/QCW');

            switch ($_POST['action']) {
                case 'saveProfile':
                    $response['data'] = basename(isys_module_quick_configuration_wizard::factory()
                        ->generate_export(trim($_POST['profileTitle'] ?? '')));
                    break;

                case 'deleteProfile':
                    $response['data'] = isys_module_quick_configuration_wizard::delete_config_file(basename(trim($_POST['file'] ?? '')));
                    break;
            }
        } catch (Exception $e) {
            $response['success'] = false;
            $response['message'] = $e->getMessage();
        }

        echo \isys_format_json::encode($response);
        die;
    }

    /**
     * Method for handling the module request.
     *
     * @param   isys_module_request $p_modreq
     *
     * @return  void
     */
    public function &handle_module_request(isys_module_request $p_modreq)
    {
        parent::handle_module_request($p_modreq);

        isys_module_system::getAuth()->check(isys_auth::EXECUTE, 'GLOBALSETTINGS/QCW');

        $rules = [
            'C__MODULE__QCW__CONFIG_TITLE' => [
                'p_strClass'        => 'mt10 mb10 input-block',
                'nowiki'            => true,
                'p_bInfoIconSpacer' => 0,
                'p_strPlaceholder'  => 'LC__MODULE__QCW__NAME'
            ]
        ];

        $this->template->assign('profiles', isys_format_json::encode(isys_module_quick_configuration_wizard::get_config_files()))
            ->smarty_tom_add_rules('tom.popup.qcw', $rules)
            ->display(isys_module_quick_configuration_wizard::getPath() . 'templates/popup/popupProfiles.tpl');

        die;
    }
}
