<?php
/* Copyright (C) 2004-2017 Laurent Destailleur  <eldy@users.sourceforge.net>
 * Copyright (C) 2020      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");

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

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

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

$arrayofparameters = array(
	'SIGNATURE_CONFIRMATION_MAIL'=>array('css'=>'minwidth500', 'enabled'=>1),
    'DOUBLE_SIGNATURE'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_DISPLAY_NAME'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_SOCIAL_LINKS'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_DOCUMENT_PREVIEW'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_URL'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_DOC_PATH'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_ENABLE_PROPAL'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_ENABLE_ORDER'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_ENABLE_ORDER_SUPPLIER'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_ENABLE_EXPEDITION'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_ENABLE_DELIVERY'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_ENABLE_INTERVENTION'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_ENABLE_CONTRACT'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_ENABLE_SEPA_MANDATE'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_ENABLE_HOLIDAY'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_ENABLE_EXPENSE_REPORT'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_USE_CERTIFICATE'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_CRT'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_CRT_PRIVATE_KEY'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_SEND_HOLIDAY_TO'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_SEND_EXPENSE_REPORT_TO'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_DISPLAY_LINE_AND_RIBBON'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_PROPAL_CAN_BE_REFUSED'=>array('css'=>'minwidth500', 'enabled'=>1),
    'SIGNATURE_COMPANY_STAMP'=>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);
$context = stream_context_create(array('http' => array('header'=>'Connection: close\r\n')));
$last_version = file_get_contents($mbisignature->url_last_version, false, $context);
$last_version = preg_replace("/\s+/", "", $last_version);

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

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

if (version_compare($mbisignature->version, $last_version, '<')) {
    echo "<div style='font-size: 18px; color: red;'>";
    echo "<hr><i class='fas fa-download'></i> " . $langs->trans('MBI_UPDATE_AVAILABLE') . " " . $mbisignature->version . " <i class='fas fa-long-arrow-alt-right'></i> " . $last_version . "<br><a href='https://www.dolistore.com/fr/modules/1270-MBI-Signature.html' target='_blank'>" . $langs->trans('AchatTelechargement') . "</a><br><hr><br>";
    echo "</div>";
} else if (version_compare($mbisignature->version, $last_version, '=')) {
    echo "<div style='font-size: 18px;'>";
    echo "<hr>" . $langs->trans('MBI_UPTODATE') . " <i class='fas fa-check-circle' style='color: green;'></i><br><hr><br>";
    echo "</div>";
}

$conf->global->SIGNATURE_ENABLE_PROPAL == "true" ? $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 1 WHERE name = 'signature_link' AND elementtype = 'propal'") : $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 0 WHERE name = 'signature_link' AND elementtype = 'propal'");
$conf->global->SIGNATURE_ENABLE_ORDER == "true" ? $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 1 WHERE name = 'signature_link' AND elementtype = 'commande'") : $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 0 WHERE name = 'signature_link' AND elementtype = 'commande'");
$conf->global->SIGNATURE_ENABLE_ORDER_SUPPLIER == "true" ? $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 1 WHERE name = 'signature_link' AND elementtype = 'commande_fournisseur'") : $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 0 WHERE name = 'signature_link' AND elementtype = 'commande_fournisseur'");
$conf->global->SIGNATURE_ENABLE_EXPEDITION == "true" ? $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 1 WHERE name = 'signature_link' AND elementtype = 'expedition'") : $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 0 WHERE name = 'signature_link' AND elementtype = 'expedition'");
$conf->global->SIGNATURE_ENABLE_INTERVENTION == "true" ? $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 1 WHERE name = 'signature_link' AND elementtype = 'fichinter'") : $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 0 WHERE name = 'signature_link' AND elementtype = 'fichinter'");
$conf->global->SIGNATURE_ENABLE_CONTRACT == "true" ? $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 1 WHERE name = 'signature_link' AND elementtype = 'contrat'") : $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 0 WHERE name = 'signature_link' AND elementtype = 'contrat'");
$conf->global->SIGNATURE_ENABLE_SEPA_MANDATE == "true" ? $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 1 WHERE name = 'signature_link' AND elementtype = 'societe'") : $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 0 WHERE name = 'signature_link' AND elementtype = 'societe'");
$conf->global->SIGNATURE_ENABLE_HOLIDAY == "true" ? $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 1 WHERE name = 'signature_link' AND elementtype = 'holiday'") : $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 0 WHERE name = 'signature_link' AND elementtype = 'holiday'");
$conf->global->SIGNATURE_ENABLE_DELIVERY == "true" ? $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 1 WHERE name = 'signature_link' AND elementtype = 'delivery'") : $db->query("UPDATE " . MAIN_DB_PREFIX . "extrafields SET list = 0 WHERE name = 'signature_link' AND elementtype = 'delivery'");

print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
if (version_compare(DOL_VERSION, '11.0.0', '>')) {
    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">'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td></tr>';

foreach ($arrayofparameters as $key => $val)
{
    print '<tr class="oddeven"><td>';
    $tooltiphelp = (($langs->trans($key.'Tooltip') != $key.'Tooltip') ? $langs->trans($key.'Tooltip') : '');
    if ($key == "SIGNATURE_URL") {
        print $form->textwithpicto($langs->trans("URLRoot"), $tooltiphelp);
    } else if ($key == "SIGNATURE_DOC_PATH") {
        print $form->textwithpicto($langs->trans("DataRootServer"), $tooltiphelp);
    } else {
        print $form->textwithpicto($langs->trans($key), $tooltiphelp);
    }
    $optionThatIsSet = "<option value='" . $conf->global->$key . "' selected>" . $conf->global->$key . "</option>";
    if ($conf->global->$key == $langs->trans('OPTION1')) {
        $option2 = "<option value='" . $langs->trans('OPTION2') . "'>" . $langs->trans('OPTION2') . "</option>";
        $option3 = "<option value='" . $langs->trans('OPTION3') . "'>" . $langs->trans('OPTION3') . "</option>";
    }
    else if ($conf->global->$key == $langs->trans('OPTION2')) {
        $option2 = "<option value='" . $langs->trans('OPTION1') . "'>" . $langs->trans('OPTION1') . "</option>";
        $option3 = "<option value='" . $langs->trans('OPTION3') . "'>" . $langs->trans('OPTION3') . "</option>";
    }
    else if ($conf->global->$key == $langs->trans('OPTION3')) {
        $option2 = "<option value='" . $langs->trans('OPTION1') . "'>" . $langs->trans('OPTION1') . "</option>";
        $option3 = "<option value='" . $langs->trans('OPTION2') . "'>" . $langs->trans('OPTION2') . "</option>";
    }
    if ($key == "SIGNATURE_CONFIRMATION_MAIL") {
        print '</td><td><select name="' . $key . '"  class="flat ' . (empty($val['css']) ? 'minwidth200' : $val['css']) . '">' . $optionThatIsSet . $option2 . $option3 . '</select></td></tr>';
    } else if ($key == "DOUBLE_SIGNATURE" || $key == "SIGNATURE_ENABLE_PROPAL" || $key == "SIGNATURE_ENABLE_ORDER" || $key == "SIGNATURE_ENABLE_ORDER_SUPPLIER" || $key == "SIGNATURE_ENABLE_EXPEDITION" || $key == "SIGNATURE_ENABLE_DELIVERY" || $key == "SIGNATURE_ENABLE_INTERVENTION" || $key == "SIGNATURE_ENABLE_CONTRACT" || $key == "SIGNATURE_ENABLE_SEPA_MANDATE" || $key == "SIGNATURE_ENABLE_HOLIDAY" || $key == "SIGNATURE_ENABLE_EXPENSE_REPORT" || $key == "SIGNATURE_DISPLAY_NAME" || $key == "SIGNATURE_SOCIAL_LINKS" || $key == "SIGNATURE_DOCUMENT_PREVIEW" || $key == "SIGNATURE_USE_CERTIFICATE" || $key == "SIGNATURE_DISPLAY_LINE_AND_RIBBON" || $key == "SIGNATURE_PROPAL_CAN_BE_REFUSED") {
        if ($conf->global->$key == "true") {
            print '</td><td><select name="' . $key . '"  class="flat ' . (empty($val['css']) ? 'minwidth200' : $val['css']) . '"><option value="true" selected>' . $langs->trans("Yes") . '</option><option value="false">' . $langs->trans("No") . '</option></select></td></tr>';
        } else if ($conf->global->$key == "false") {
            print '</td><td><select name="' . $key . '"  class="flat ' . (empty($val['css']) ? 'minwidth200' : $val['css']) . '"><option value="false" selected>' . $langs->trans("No") . '</option><option value="true">' . $langs->trans("Yes") . '</option></select></td></tr>';
        }
        // print '</td><td>' . ajax_constantonoff($conf->global->$key) . '</td></tr>'; // Add on/off buttons but doesn't work with true/false, need 1/0
    } else if ($key == "SIGNATURE_URL" || $key == "SIGNATURE_DOC_PATH") {
        print '</td><td><input type="text" name="' . $key . '" value="' . $conf->global->$key . '" class="flat ' . (empty($val['css']) ? 'minwidth200' : $val['css']) . '"></td></tr>';
    } else if ($key == "SIGNATURE_SEND_HOLIDAY_TO" || $key == "SIGNATURE_SEND_EXPENSE_REPORT_TO") {
        print '</td><td><input type="email" name="' . $key . '" value="' . $conf->global->$key . '" class="flat ' . (empty($val['css']) ? 'minwidth200' : $val['css']) . '"></td></tr>';
    } else if ($key == "SIGNATURE_CRT" || $key == "SIGNATURE_CRT_PRIVATE_KEY") {
        if ($key == "SIGNATURE_CRT" && $conf->global->SIGNATURE_USE_CERTIFICATE == "true" && file_exists($conf->global->SIGNATURE_CRT)) {
            $okko = "<i class='fas fa-check-circle' style='color: green; position: relative; left: 4px;'></i>";
        } else if ($key == "SIGNATURE_CRT_PRIVATE_KEY" && $conf->global->SIGNATURE_USE_CERTIFICATE == "true" && file_exists($conf->global->SIGNATURE_CRT_PRIVATE_KEY)) {
            $okko = "<i class='fas fa-check-circle' style='color: green; position: relative; left: 4px;'></i>";
        }  else if ($conf->global->SIGNATURE_USE_CERTIFICATE !== "true") {
            $okko = "";
        } else {
            $okko = "<i class='fas fa-times' style='color: red; position: relative; left: 4px;'></i>";
        }
            print '</td><td><input type="text" name="' . $key . '" value="' . $conf->global->$key . '" class="flat ' . (empty($val['css']) ? 'minwidth200' : $val['css']) . '">' . $okko . '</td></tr>';
    } else if ($key == "SIGNATURE_COMPANY_STAMP") {
        if (file_exists($conf->global->SIGNATURE_COMPANY_STAMP)) {
            $okko = "<i class='fas fa-check-circle' style='color: green; position: relative; left: 4px;'></i>";
        } else {
            $okko = "";
        }
        print '</td><td><input type="text" name="' . $key . '" value="' . $conf->global->$key . '" class="flat ' . (empty($val['css']) ? 'minwidth200' : $val['css']) . '">' . $okko . '</td></tr>';
    }
}
print '</table>';

print '<br><div class="center">';
print '<input class="button" type="submit" value="'.$langs->trans("Save").'">';
print '</div>';

print '</form><br>';

// Page end
dol_fiche_end();

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