<?php
/* Copyright (C) 2004-2017 Laurent Destailleur  <eldy@users.sourceforge.net>
 * Copyright (C) 2021      MB Informatique      <info@mb-informatique.fr>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

/**
 * \file    mbisignature/admin/setup.php
 * \ingroup mbisignature
 * \brief   MBISignature setup page.
 */

// Load Dolibarr environment
$res = 0;
// Try main.inc.php into web root known defined into CONTEXT_DOCUMENT_ROOT (not always defined)
if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) $res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php";
// Try main.inc.php into web root detected using web root calculated from SCRIPT_FILENAME
$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME']; $tmp2 = realpath(__FILE__); $i = strlen($tmp) - 1; $j = strlen($tmp2) - 1;
while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) { $i--; $j--; }
if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1))."/main.inc.php")) $res = @include substr($tmp, 0, ($i + 1))."/main.inc.php";
if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php")) $res = @include dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php";
// Try main.inc.php using relative path
if (!$res && file_exists("../../main.inc.php")) $res = @include "../../main.inc.php";
if (!$res && file_exists("../../../main.inc.php")) $res = @include "../../../main.inc.php";
if (!$res) die("Include of main fails");

global $langs, $user, $conf, $db;

// Libraries
require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
require_once '../lib/mbisignature.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
dol_include_once("/mbisignature/core/modules/modMBISignature.class.php");

echo "<script type=\"text/javascript\" src='" . DOL_URL_ROOT . "/includes/jquery/js/jquery.min.js'></script>";
echo "<script type=\"text/javascript\" src='" . DOL_URL_ROOT . "/includes/jquery/js/jquery-ui.min.js'></script>";
// echo "<link rel='stylesheet' href='" . DOL_URL_ROOT . "/includes/jquery/plugins/jpicker/jPicker.css'>";

$langs->loadLangs(array("admin", "mbisignature@mbisignature"));

// Access control
if (!$user->admin) accessforbidden();

// Parameters
$action = GETPOST('action', 'alpha');
$backtopage = GETPOST('backtopage', 'alpha');

$arrayofparameters = array(
    'SIGNATURE_CUSTOM_BUTTON_COLOR'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_CUSTOM_CHECKBOX_COLOR'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_CUSTOM_RIBBON_COLOR'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_CUSTOM_LINE_COLOR'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_CUSTOM_CSS'=>array('css'=>'minwidth500', 'enabled'=>1)
);

/*
 * Actions
 */

if ((float) DOL_VERSION >= 6) { include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php'; }

/*
 * View
 */

$page_name = "MBISignatureSetup";
llxHeader('', $langs->trans($page_name));

// Subheader
$linkback = '<a href="'.($backtopage ? $backtopage : DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1').'">'.$langs->trans("BackToModuleList").'</a>';

print load_fiche_titre($langs->trans($page_name), $linkback, 'object_logo@mbisignature');

$mbisignature = new modMBISignature($db);

// Configuration header
$head = mbisignatureAdminPrepareHead();
dol_fiche_head($head, 'customization', '', -1, "mbisignature@mbisignature");

echo "<script type=\"text/javascript\" src='" . DOL_URL_ROOT . "/includes/jquery/plugins/jpicker/jpicker-1.1.6.min.js'></script>";
echo "<script>
  $(document).ready(
    function() {
      $('.multiple').jPicker({images:{clientPath: '" . DOL_URL_ROOT . "/includes/jquery/plugins/jpicker/images/'}});
    });
</script>";

echo "<style>.JPicker {display: none;}</style>";

// Setup page goes here
echo '<span class="opacitymedium">'.$langs->trans("MBISignatureSetupPage").'</span><br><br>';

print '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '">';
print '<input type="hidden" name="token" value="' . newToken() . '">';
print '<input type="hidden" name="action" value="update">';

print '<table class="noborder centpercent">';
print '<tr class="liste_titre"><td class="titlefield" style="font-family: arial,tahoma,verdana,helvetica, serif !important; font-weight: normal !important; vertical-align: middle !important; height: 24px !important;">' . $langs->trans("Parameter") . '</td><td>' . $langs->trans("Value") . '</td></tr>';

foreach ($arrayofparameters as $key => $val) {
    if ($key !== "SIGNATURE_CUSTOM_CSS") {
        print '<tr class="oddeven" style="line-height: 1.4 !important; font-size: 0.90em !important; font-family: arial,tahoma,verdana,helvetica, serif !important;"><td>';
        $tooltiphelp = (($langs->trans($key . 'Tooltip') != $key . 'Tooltip') ? $langs->trans($key . 'Tooltip') : '');
        print $form->textwithpicto($langs->trans($key), $tooltiphelp);
        print '</td><td><input type="text" name="' . $key . '" value="' . $conf->global->$key . '" class="flat ' . (empty($val['css']) ? 'minwidth200' : $val['css']) . ' multiple"></td></tr>';
    } else {
        print '<tr class="oddeven" style="line-height: 1.4 !important; font-size: 0.90em !important; font-family: arial,tahoma,verdana,helvetica, serif !important;"><td>';
        $tooltiphelp = (($langs->trans($key . 'Tooltip') != $key . 'Tooltip') ? $langs->trans($key . 'Tooltip') : '');
        print $form->textwithpicto("CSS", $tooltiphelp);
        echo '</td><td>';
        print '<textarea wrap="off" class="flat" name="' . $key . '" id="' . $key . '" wrap="soft">' . $conf->global->$key . '</textarea>';
        echo '</td></tr>';
        echo '<link rel="stylesheet" href="../scripts/codemirror/codemirror.css">
        <script src="../scripts/codemirror/codemirror.js"></script>
        <script src="../scripts/codemirror/css.js"></script>
        <script>
            var editor = CodeMirror.fromTextArea(document.getElementById("' . $key . '"), {
                lineNumbers: true,
                mode: "css",
                indentWithTabs: true,
                indentUnit: 4
            });
        </script>';
    }
}
print '</table>';
print '<div class="center"><input class="button" type="submit" value="' . $langs->trans("Save") . '"></div>';

print '</form>';

// Page end
dol_fiche_end();

llxFooter();
$db->close();
