<?php // BEGIN PHP

//session_set_cookie_params(['lifetime' => 0, 'path' => '/', 'domain' => '', 'secure' => true, 'httponly' => true, 'samesite' => 'Lax');
session_start(); // start session

require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
require_once DOL_DOCUMENT_ROOT . '/core/lib/categories.lib.php';
require_once DOL_DOCUMENT_ROOT . '/core/class/html.formcompany.class.php';
require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
require_once DOL_DOCUMENT_ROOT . '/societe/class/societeaccount.class.php';

error_reporting(E_ALL);
ini_set('display_errors', 1);

/*print '<pre>';
print_r($conf);
print '</pre>';
exit;*/

$tag = GETPOST('tag', 'alpha');
$cat_id = GETPOST('cat', 'alpha');
$search = GETPOST('search_query', 'alpha');
$product_id = GETPOST('id', 'alpha');

$isAdmin = false;

$form = new Form($db);

// If not login redirect to login.
if (empty($_SESSION['user']['id_user'])) {
    redirectToContainer('login-register');
}

if (!empty($product_id)) {
    $product = new Product($db);
    $result = $product->fetch($product_id);
    if ($result <= 0) {
        print "Product not found from id=".((int) $product_id);
        //redirectToContainer('myproducts');
        exit;
    } else {
        // Get Account ID.
        $user_id = $_SESSION['user']['id_user'];
        $supplier_account = new SocieteAccount($db);
        $result = $supplier_account->fetch($user_id);
        $supplier_id = $supplier_account->fk_soc;
        
        /*
        
        // Get societe ext_ref
        $supplier_account_societe = new Societe($db);
        $result = $supplier_account_societe->fetch($supplier_id);
        $supplier_id_ext = $supplier_account_societe->ref_ext;
        //echo $result; exit;

        // Special check for old products with old syntax c...d...
        if (preg_match('/c(\d+)d/', $product->ref, $matches) && $matches[1] != $supplier_id && $matches[1] != $supplier_id_ext) {
            print "Product not found from c...d... or supplier ref_ext is not matching the id into the product ref c...d...";
            //redirectToContainer('myproducts');
            exit;
        }
        */
        
        // TODO 
        // Check that product $product_id is owned by the logged company $_SESSION['user']['socid']
        //var_dump($_SESSION['user']['socid']);
    }
}

// END PHP 
?>
<div id="page">
    <div class="header-container">
        <?php includeContainer('header'); ?>
    </div>
    <div class="columns-container">
        <div id="columns" class="container">
            <!--  Breadcrumb  -->
            <div class="breadcrumb clearfix">
                <a class="home" href="index.php" title="Return to Home"><i class="icon-home"></i></a>
                <span class="navigation-pipe">&gt;</span> <a href="myaccount.php" title="<?php print $weblangs->trans("myAccount"); ?>" data-gg=""><?php print $weblangs->trans("myAccount"); ?></a>
                <span class="navigation-pipe">&gt;</span> <?php print $weblangs->trans("newModuleProduct"); ?>
            </div>
            <!--  Breadcrumb  -->
            <div id="slider_row" class="row">
                <div id="top_column" class="center_column col-xs-12 col-sm-12"></div>
            </div>
            <div class="row">
                <!-- include left sidebar container -->
                <?php includeContainer('sidebar'); ?>
                <div id="center_column" class="center_column col-xs-12 col-sm-9">
                    <div class="clearfix">
                        <script type="text/javascript">
                            var edit_product_url = "_service_page_ajax.php";
                            console.log(edit_product_url);
                        </script>
                        <h1 class="page-heading"><?php print $weblangs->trans("newModuleProduct"); ?></h1>
                        <div class="row">
                            <div class="col-xs-12">
                                <div class="add-product-page">
                                    <?php
                                    $website_available_langs = getAvailableLangs();
                                    if (!defined('ROWS_3')) {
                                        define('ROWS_3', 3);
                                    }
                                    
                                    $mvcid = getDolGlobalInt("MARKETPLACE_VERSIONS_CATEGORY_ID");
                                    $full_version_tree = getOrganizedTree($mvcid, 'label');

                                    if (!empty($product_id) && $_SERVER['REQUEST_METHOD'] != 'POST') {

                                        foreach ($website_available_langs['langs_code_shortcode'] as $lang_key => $code) {
                                            ${'name_' . $lang_key} = $product->multilangs[$code]['label'] ?? '';
                                            ${'short_description_' . $lang_key} = $product->multilangs[$code]['description'] ?? '';
                                            ${'description_' . $lang_key} = $product->multilangs[$code]['other'] ?? '';
                                        }
                                        $status = $product->status ?? 0;
                                        $sale_price = ($product->price ? price($product->price) : '');

                                        $c = new Categorie($db);
                                        $cats = $c->containing($product->id, Categorie::TYPE_PRODUCT);
                                        $arrayselected = array();
                                        if (is_array($cats)) {
                                            foreach ($cats as $cat) {
                                                $arrayselected[] = $cat->id;
                                            }
                                        }
                                        $categories = array();
                                        foreach ($arrayselected as $key => $value) {
                                            if (!in_array($value, ['PRODUCTS_ON_WEBSITE', 'VERSIONS']) && strpos($value, 'V') !== 0) {
                                                $categories[$key] = $value;
                                            }
                                        }

                                        $version = ($product->array_options['options_marketplace_module_version'] ? $product->array_options['options_marketplace_module_version'] : 0);
                                        $keywords = ($product->array_options['options_marketplace_module_keywords'] ? $product->array_options['options_marketplace_module_keywords'] : 0);
                                        $dolibarr_core_include = ($product->array_options['options_marketplace_allow_source_in_core'] ? $product->array_options['options_marketplace_allow_source_in_core'] : 0);
                                        $version_min = ($product->array_options['options_marketplace_min_version'] ? $product->array_options['options_marketplace_min_version'] : 0);
                                        $version_max = ($product->array_options['options_marketplace_max_version'] ? $product->array_options['options_marketplace_max_version'] : 0);
                                        $contact_support = ($product->array_options['options_marketplace_contact_support'] ? $product->array_options['options_marketplace_contact_support'] : 0);

                                        $files_and_images = getPublicFilesOfObject($product);
                                        foreach ($files_and_images as $key => $file) {
                                            if ($file['position'] == 100) {
                                                $virtual_product_file = $file;
                                            }
                                            if ($file['position'] == 0) {
                                                $image_cover = $file;
                                            }
                                            if (strpos($file['filename'], 'image_') !== false && strpos($file['filename'], 'image_cover') === false) {
                                                $otherimages[$key] = $file;
                                            }
                                        }
                                    }


                                    if (GETPOST('action') == "new-module") {    // submit new or update existing one
                                        foreach ($website_available_langs['langs'] as $lang_key => $lang_label) {
                                            ${'name_' . $lang_key} = GETPOST('name_' . $lang_key) ?? '';
                                            ${'short_description_' . $lang_key} = GETPOST('short_description_' . $lang_key) ?? '';
                                            ${'description_' . $lang_key} = GETPOST('description_' . $lang_key, 'restricthtmlallowiframe') ?? '';
                                        }

                                        $error = 0;
                                        $error_messages = array();

                                        foreach ($website_available_langs['langs'] as $lang_key => $lang_label) {
                                            $product_name = GETPOST('name_' . $lang_key);
                                            $short_description = GETPOST('short_description_' . $lang_key);
                                            $description = GETPOST('description_' . $lang_key, 'restricthtmlallowiframe');

                                            if ($lang_key == "en" && ($product_name == "" || $short_description == "" || $description == "")) {
                                                $error++;
                                                $error_messages[] = $weblangs->trans("englishFieldsRequired");
                                            }

                                            if ($lang_key != "en" && $product_name == "") {
                                                $product_name = (isset($productlang['en']['name']) ? $productlang['en']['name'] : '');
                                            }

                                            if ($lang_key != "en" && $short_description == "") {
                                                $short_description = (isset($productlang['en']['short_description']) ? $productlang['en']['short_description'] : '');
                                            }

                                            if ($lang_key != "en" && $description == "") {
                                                $description = (isset($productlang['en']['description']) ? $productlang['en']['description'] : '');
                                            }

                                            $productlang[$lang_key]['name'] = $product_name;
                                            $productlang[$lang_key]['short_description'] = $short_description;
                                            $productlang[$lang_key]['description'] = $description;

                                            $short_descriptionLength = strlen(strip_tags($short_description));
                                            if ($short_descriptionLength > 400) {
                                                $error++;
                                                $error_messages['short_description_' . $lang_key] = $weblangs->trans("fieldTooLong", $weblangs->trans("shortDescription"), 400, $short_descriptionLength);
                                            }
                                        }

                                        $keywords = GETPOST('keywords') ?? '';

                                        $categories = GETPOST('categories') ?? '';
                                        if (empty($categories)) {
                                            $error++;
                                            $error_messages['categories'] = $weblangs->trans("requiredCategory");
                                        }

                                        if (empty($_FILES['virtual_product_file']['name']) && empty($product_id)) {
                                            $error++;
                                            $error_messages['virtual_product_file'] = $weblangs->trans("zipProductRequired");
                                        } else {
                                            if (!empty($_FILES['virtual_product_file']['name'])) {
                                                $checkZipFile = checkZipFile();
                                                if ($checkZipFile['upload'] < 0) {
                                                    $error++;
                                                    $error_messages['virtual_product_file'] = $checkZipFile['errormsg'];
                                                }
                                            }
                                        }

                                        if (empty($_FILES['image_cover']['name']) && empty($product_id)) {
                                            $error++;
                                            $error_messages['image_cover'] = $weblangs->trans("fieldRequired", $weblangs->trans("coverImage"));
                                        } else {
                                            if (!empty($_FILES['image_cover']['name'])) {
                                                $checkCoverImage = checkImage($_FILES['image_cover']);
                                                if ($checkCoverImage != false) {
                                                    $error++;
                                                    $error_messages['image_cover'] = $weblangs->trans("coverImage") . ' : ' . $checkCoverImage;
                                                }
                                            }
                                        }

                                        $other_images = array();
                                        if (!empty($_FILES['image_1']['name'])) {
                                            $other_images = array();
                                            $other_images_errors = array();
                                            for ($x = 1; $x <= 10; $x++) {
                                                if (!empty($_FILES['image_' . $x]['name'])) {
                                                    $other_images['image_' . $x] = $_FILES['image_' . $x];
                                                    $checkImage = checkImage($_FILES['image_' . $x]);
                                                    if ($checkImage != false) {
                                                        $other_images_errors['image_' . $x] = $checkImage;
                                                    }
                                                } else {
                                                    break;
                                                }
                                            }
                                        }
                                        if (!empty($other_images_errors)) {
                                            $error++;
                                            foreach ($other_images_errors as $key => $value) {
                                                $error_messages[$key] = $key . ' : ' . $value;
                                            }
                                        }

                                        $sale_price = (GETPOST('sale_price') ? GETPOST('sale_price') : 0);
                                        $version = (GETPOST('version') ? GETPOST('version') : '');
                                        $dolibarr_core_include = (GETPOST('dolibarr_core_include') ? GETPOST('dolibarr_core_include') : 0);
                                        $version_min = (GETPOST('version_min') ? GETPOST('version_min') : '');
                                        $version_max = (GETPOST('version_max') ? GETPOST('version_max') : '');
                                        $contact_support = (GETPOST('contact_support') ? GETPOST('contact_support') : '');
                                        $status = (GETPOST('status') ? GETPOST('status') : 0);

                                        $dateNow = date("Y-m-d H:i:s");
                                        $dateRef = date("YmdHis");

                                        if ($error == 0) {
                                            $product = array(
                                                'name' => $productlang['en']['name'],
                                                'short_description' => $productlang['en']['short_description'],
                                                'description' => $productlang['en']['description'],
                                                'multilangs' => $productlang,
                                                'keywords' => $keywords,
                                                'categories' => $categories,
                                                'sale_price' => $sale_price,
                                                'version' => $version,
                                                'version_min' => $version_min,
                                                'version_max' => $version_max,
                                                'contact_support' => $contact_support,
                                                'dolibarr_core_include' => $dolibarr_core_include,
                                                'status' => $status,
                                                'image_cover' => $_FILES['image_cover'],
                                                'other_images' => $other_images,
                                                'virtual_product_file' => $_FILES['virtual_product_file']
                                            );

                                            if (!empty($product_id)) {
                                                $addProduct = addProduct($product, $product_id);
                                            } else {
                                                $addProduct = addProduct($product);
                                            }

                                            if ($addProduct == 1) {
                                                redirectToContainer('manage-products');
                                            } else {
                                                $error++;
                                                $error_messages[] = $weblangs->trans("errorOccurred");
                                            }
                                        }
                                    }
                                    ?>

                                    <?php if (isset($error) && $error != 0) : ?>
                                        <div class='alert alert-danger'>
                                            <?php $add_s = ($error > 1) ? "s" : ""; ?>
                                            <p><?php print $weblangs->trans("errorsOccurred", $error, $add_s); ?></p>
                                            <ol>
                                                <?php foreach ($error_messages as $key => $value) : ?>
                                                    <li><?php print $value; ?></li>
                                                <?php endforeach ?>
                                            </ol>
                                        </div>
                                    <?php endif ?>

                                    <center>
                                        <!-- To remove later -->
                                        <?php if (!empty(getDolGlobalInt("MARKETPLACE_IN_MIGRATION")) && !in_array($_SESSION['user']['id_user'], array_map('trim', explode(',', getDolGlobalString("MARKETPLACE_ADMIN_USER_IDS"))))) : ?>
                                            <div style="color: red; font-weight: bold; font-size: 1.2em;"><br>
                                            [EN] This site is currently under maintenance. Please do not submit, edit or buy modules yet.<br>
                                            [FR] Ce site est toujours en maintenance. Merci de ne pas soumettre, éditer ou acheter des modules.<br>
                                            <br><br>
                                            </div>
                                        <?php endif ?>
                                        <!-- To remove later -->
                                        
                                        
                                        <?php print $weblangs->trans("checkRulesBeforeSubmit", "https://wiki.dolibarr.org/index.php?title=Modules_-_Packaging_rules_and_Dolistore_validation_rules"); ?>
                                    </center>
                                    <br><br>
                                    <?php
                                    if (!empty($product) && is_object($product)) {
                                        print strtoupper($weblangs->trans("Product")).': <b>'.$product->ref.'</b><br><br><br>';
                                    }
                                    ?>

                                    <p class="form-subheading"><?php print $weblangs->trans("multilingualInformation"); ?></p>

                                    <nav>
                                        <div class="nav">
                                            <?php foreach ($website_available_langs['langs'] as $key => $value) : ?>
                                                <button class="nav-link <?php if ($key == 'en') {
                                                                            print 'active';
                                                                        } ?>" id="nav-<?php print $key; ?>-tab" data-target="nav-<?php print $key; ?>"><?php print $value; ?> <?php if ($key == 'en') {
                                                                                                                                                                                                                            print '<sup>*</sup>';
                                                                                                                                                                                                                        } ?></button>
                                            <?php endforeach ?>
                                        </div>
                                    </nav>
                                    
                                    <form action="" enctype="multipart/form-data" method="post" id="product-creation-form">
                                        <div class="tab-content std">
                                            <?php foreach ($website_available_langs['langs'] as $key => $value): ?>
                                                <div class="tab-pane <?php if ($key == 'en') {
                                                    print 'active';
                                                } ?>" id="nav-<?php print $key; ?>">
                                                    <div class="form-group row required">
                                                        <label for="name_<?php print $key; ?>"
                                                            class="col-xs-4 col-form-label"><?php print $weblangs->trans("moduleProductName"); ?> <span
                                                                class="lang_span">[<?php print $key; ?>]</span>
                                                            <?php if ($key == 'en') {
                                                                print '<sup>*</sup>';
                                                            } ?></label>
                                                        <div class="col-xs-8">
                                                            <input type="text" class="is_required validate form-control" id="name_<?php print $key; ?>"
                                                                name="name_<?php print $key; ?>" <?php if ($key == 'en') {
                                                                       print 'required';
                                                                   } ?> value="<?php if (isset(${'name_' . $key})) {
                                                                         print ${'name_' . $key};
                                                                     } ?>">
                                                        </div>
                                                    </div>
                                                    <div class="form-group row required">
                                                        <label for="short_description_<?php print $key; ?>"
                                                            class="col-xs-12 col-form-label"><?php print $weblangs->trans("shortDescription"); ?> <span
                                                                class="lang_span">[<?php print $key; ?>]</span>
                                                            <?php if ($key == 'en') {
                                                                print '<sup>*</sup>';
                                                            } ?></label>
                                                        <div class="col-xs-12">
                                                            <textarea row="5" class="rtenockeditor is_required validate form-control textarea-short-description"
                                                                id="short_description_<?php print $key; ?>" name="short_description_<?php print $key; ?>" <?php if ($key == 'en') {
                                                                          print 'required';
                                                                      } ?>><?php if (isset(${'short_description_' . $key})) {
                                                                           print ${'short_description_' . $key};
                                                                       } ?></textarea>
                                                        </div>
                                                    </div>
                                        
                                                    <div class="form-group row required">
                                                        <label for="description_<?php print $key; ?>"
                                                            class="col-xs-12 col-form-label"><?php print $weblangs->trans("largeDescription"); ?> <span
                                                                class="lang_span">[<?php print $key; ?>]</span>
                                                            <?php if ($key == 'en') {
                                                                print '<sup>*</sup>';
                                                            } ?></label>
                                                        <div class="col-xs-12">
                                                            <?php
                                                            $description_value = ${'description_' . $key} ?? '';
                                                            $doleditor = new DolEditor('description_' . $key, $description_value, '', 320, 'dolibarr_notes', 'Basic', false, false, getDolGlobalString('FCKEDITOR_ENABLE_DETAILS'), '4', '90%');
                                                            $doleditor->Create(0, '', 'a[target];section[contenteditable,id];div{float,display};iframe[src,width,height]');
                                                            ?>
                                                        </div>
                                                    </div>
                                        
                                                </div>
                                            <?php endforeach ?>
                                        </div>


                                        <p class="form-subheading"><?php print $weblangs->trans("moduleInformation"); ?></p>
                                        <div class="col-xs-12 box-form">

                                            <div class="form-group row required">
                                                <label for="keywords" class="col-xs-3 col-form-label"><?php print $weblangs->trans("keywords"); ?></label>
                                                <div class="col-xs-9">
                                                    <input type="text" class="is_required validate form-control module-keywords" id="keywords" name="keywords" value="<?php if (isset($keywords)) {
                                                                                                                                                                            print $keywords;
                                                                                                                                                                        } ?>">
                                                </div>
                                            </div>

                                            <hr>

                                            <div class="form-group row required">
                                                <label for="module-version" class="col-xs-3 col-form-label"><?php print $weblangs->trans("moduleVersion"); ?> <sup>*</sup></label>
                                                <div class="col-xs-9">
                                                    <input type="text" class="is_required validate form-control module-version" id="module-version" name="version" required value="<?php if (isset($version)) {
                                                                                                                                                                                        print $version;
                                                                                                                                                                                    } ?>">
                                                </div>
                                            </div>

                                            <hr>

                                            <div class="form-group row required">
                                                <label class="col-xs-3 col-form-label"><?php print $weblangs->trans("compatibility"); ?> <sup>*</sup></label>
                                                <div class="col-xs-9">
                                                    <div class="row">
                                                        <div class="col-xs-4">
                                                            Min
                                                            <?php
                                                                $version_min_tag = (empty($version_min) ? "V?" : (substr($version_min, 0, 1) !== "V" ? "V" : "").$version_min);
                                                                $version_min_tag = preg_replace('/\..*$/', '', $version_min_tag);
                                                            ?>
                                                            <select name="version_min" id="version-min" class="flat maxwidth200onsmartphone selectcountry form-control" required>
                                                                <option value="" disabled><?php print $weblangs->trans("minVersion"); ?></option>
                                                                <?php foreach (array_reverse($full_version_tree) as $key => $value) { ?>
                                                                    <?php if ($value['rowid'] != $mvcid) { ?>
                                                                        <option value="<?php print $value['id']; ?>" <?php if ($version_min_tag === $value['label']) {
                                                                            print 'selected';
                                                                        } ?>><?php print $value['label']; ?></option>
                                                                    <?php } ?>
                                                                <?php } ?>
                                                            </select>
                                                        </div>

                                                        <div class="col-xs-4">
                                                            Max
                                                            <?php
                                                                $version_max_tag = (empty($version_max) ? "V?" : (substr($version_max, 0, 1) !== "V" ? "V" : "").$version_max);
                                                                $version_max_tag = preg_replace('/\..*$/', '', $version_max_tag);
                                                            ?>
                                                            <select name="version_max" id="version-max" class="flat maxwidth200onsmartphone selectcountry form-control" required>
                                                                <option value="" disabled><?php print $weblangs->trans("maxVersion"); ?></option>
                                                                <?php foreach (array_reverse($full_version_tree) as $key => $value) { ?>
                                                                    <?php if ($value['rowid'] != $mvcid) { ?>
                                                                        <option value="<?php print $value['id']; ?>" <?php if ($version_max_tag === $value['label']) {
                                                                            print 'selected';
                                                                        } ?>><?php print $value['label']; ?></option>
                                                                    <?php } ?>
                                                                <?php } ?>
                                                            </select>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>

                                            <hr>

                                            <div class="form-group row required">
                                                <label for="status" class="col-xs-3 col-form-label"><?php print $weblangs->trans("status"); ?></label>
                                                <div class="col-xs-9">
                                                    <div class="form-check">
                                                        <input class="form-check-input valignmiddle" type="radio" name="status" id="status-enabled" value="1" <?php if (isset($status) && $status == 1) {
                                                                                                                                                        print 'checked';
                                                                                                                                                    } elseif (empty($isAdmin)) { print "disabled"; } ?>>
                                                        <label class="form-check-label valignmiddle" for="status-enabled">
                                                            <?php print $langs->trans("Enabled"); ?>
                                                        </label>
                                                    </div>
                                                    <div class="form-check"> 
                                                        <input class="form-check-input valignmiddle" type="radio" name="status" id="status-disabled" value="0" <?php if (isset($status) && $status == 0) {
                                                                                                                                                        print 'checked';
                                                                                                                                                    } ?>>
                                                        <label class="form-check-label valignmiddle" for="status-disabled">
                                                            <?php print $langs->trans("Disabled"); ?>
                                                        </label>
                                                    </div>
                                                    <?php if (isset($status) && $status == 0) { print ' <span class="opacitymedium valignmiddle">'.$product->array_options['options_marketplace_reason_disabled'].'</span>'; } ?>
                                                </div>
                                            </div>

                                            <hr>

                                            <div class="form-group row required">
                                                <label for="module-sale-price" class="col-xs-3 col-form-label"><?php print $weblangs->trans("salePriceExlTax"); ?> <sup>*</sup></label>
                                                <div class="col-xs-9">
                                                    <input type="text" class="is_required validate form-control col-xs-8 price-field" id="module-sale-price" name="sale_price" value="<?php if (isset($sale_price)) {
                                                                                                                                                                                            print $sale_price;
                                                                                                                                                                                        } ?>" required>
                                                    <span class="price-currency">
                                                        <?php print $weblangs->trans("currencyEuro"); ?>
                                                    </span>
                                                    <span class="price-free">
                                                        <?php print $weblangs->trans("salePriceFree"); ?>
                                                    </span>
                                                    <div class="field-info info-inline">
                                                        <?php print $weblangs->trans("salePriceInfo"); ?>
                                                    </div>
                                                </div>
                                            </div>

                                            <hr>

                                            <div class="form-group row required">
                                                <label for="module-how-to-contact" class="col-xs-3 col-form-label"><?php print $weblangs->trans("contactSupport"); ?></label>
                                                <div class="col-xs-9">
                                                    <input type="text" class="is_required validate form-control how-to-contact" id="module-how-to-contact" name="contact_support" value="<?php if (isset($contact_support)) {
                                                                                                                                                                                                print $contact_support;
                                                                                                                                                                                            } ?>">
                                                    <div class="field-info info-inline">
                                                        <?php print $weblangs->trans("contactSupportInfo"); ?>
                                                    </div>
                                                </div>
                                            </div>

                                            <hr>

                                            <div class="form-group row required">
                                                <label for="customer_firstname" class="col-xs-3 col-form-label"><?php print $weblangs->trans("selectCatForProduct"); ?> <sup>*</sup></label>
                                                <div class="col-xs-9">
                                                    <?php
                                                    $mcid = getDolGlobalInt("MARKETPLACE_ROOT_CATEGORY_ID");
                                                    $organized_tree = getOrganizedTree($mcid);
                                                    ?>

                                                    <table border="0" cellspacing="5" cellpadding="0" class="cat-check-table-submit-product">
                                                        <tbody>
                                                            <?php
                                                            if (!isset($categories) || !is_array($categories)) {
                                                                $categories = [];
                                                            }
                                                            ?>
                                                            <?php foreach ($organized_tree as $key => $value) : ?>
                                                                <?php if ($value['label'] != "Versions" && $value['label'] != "Specials" && $value['label'] != "Goodies") : ?>
                                                                    <tr bgcolor="#FFDBB7">
                                                                        <td class="cat-checkbox-td">
                                                                            <?php $checked = in_array($value['id'], $categories) ? 'checked' : '';  ?>
                                                                            <input type="checkbox" id="cat-<?php print $value['rowid']; ?>" name="categories[]" value="<?php print $value['rowid']; ?>" <?php print $checked; ?>>
                                                                            <label for="cat-<?php print $value['rowid']; ?>" class="checkbox_label_parent"> <?php print $value['label']; ?> </label>
                                                                        </td>
                                                                    </tr>
                                                                    <?php if (isset($value['children']) && !empty($value['children'])) : ?>
                                                                        <?php usort($value['children'], build_sorter('position')); ?>
                                                                        <?php foreach ($value['children'] as $key_children => $value_children) : ?>
                                                                            <tr bgcolor="#FFF4EA">
                                                                                <td class="subcat-checkbox-td">
                                                                                    <?php $checked = in_array($value_children['id'], $categories) ? 'checked' : ''; ?>
                                                                                    <input type="checkbox" id="subcat-<?php print $value_children['rowid']; ?>" name="categories[]" value="<?php print $value_children['rowid']; ?>" <?php print $checked; ?>>
                                                                                    <label for="subcat-<?php print $value_children['rowid']; ?>" class="checkbox_label"> <?php print $value_children['label']; ?> </label>
                                                                                </td>
                                                                            </tr>
                                                                        <?php endforeach; ?>
                                                                    <?php endif; ?>
                                                                <?php endif; ?>
                                                            <?php endforeach; ?>
                                                        </tbody>
                                                    </table>
                                                </div>
                                            </div>

                                        </div>


                                        <p class="form-subheading"><?php print $weblangs->trans("attachments"); ?></p>
                                        <div class="col-xs-12 box-form">

                                            <div class="form-group row required">
                                                <label for="virtual_product_file" class="col-xs-3 col-form-label"><?php print $weblangs->trans("zipFileModuleField"); ?> <sup>*</sup></label>
                                                <div class="col-xs-9">
                                                    <div class="uploader" id="uniform-virtual_product_file">
                                                        <input id="virtual_product_file" name="virtual_product_file" class="" maxlength="10000000" type="file" <?php if (empty($product_id)) {
                                                                                                                                                                    print 'required';
                                                                                                                                                                } ?>>
                                                        <div class="field-info info-inline" style="margin-left: 10px;">
                                                            <?php print $weblangs->trans("zipFileModuleInfo"); ?>
                                                        </div>
                                                    </div>
                                                    <?php if (!empty($product_id) && !empty($virtual_product_file['filename'])) : ?>
                                                        <div class="field-info info-inline" id="field-info-virtual_product_file">
                                                            <a href="<?php print $virtual_product_file['url']; ?>" target="_new"><?php print $virtual_product_file['filename']; ?> <i class="icon-cloud-download"></i></a>
                                                        </div>
                                                    <?php endif; ?>

                                                </div>
                                            </div>

                                            <hr>

                                            <div class="form-group row required">
                                                <label for="image_cover" class="col-xs-3 col-form-label"><?php print $weblangs->trans("coverImage"); ?> <sup>*</sup></label>
                                                <div class="col-xs-9">
                                                    <table>
                                                        <tbody class="tbody-image-cover">
                                                            <tr>
                                                                <td>
                                                                    <input type="file" name="image_cover" id="image_cover" class="input-image-cover" accept="image/*" <?php if (empty($product_id)) {
                                                                                                                                                                            print 'required';
                                                                                                                                                                        } ?>>
                                                                </td>
                                                                <td>
                                                                    <img src="<?php if (!empty($product_id)) {
                                                                                    print $image_cover['url'];
                                                                                } else {
                                                                                    print '#';
                                                                                } ?>" <?php if (empty($product_id)) {
                                                                                                                                                                    print  'style="display: none;"';
                                                                                                                                                                } ?> class="image-cover-preview" id="image_preview" width="100px">
                                                                </td>
                                                            </tr>
                                                        </tbody>
                                                    </table>
                                                    <!--<div class="field-info info-inline" id="field-info-image-cover">
                                                        <?php //echo htmlspecialchars($_FILES['image_cover']['name']); 
                                                        ?>
                                                    </div>-->
                                                    <?php //endif; 
                                                    ?>
                                                </div>
                                            </div>

                                            <hr>

                                            <?php if (empty($product_id)) : ?>
                                                <div class="form-group row required">
                                                    <label class="col-xs-3 col-form-label"><?php print $weblangs->trans("otherImages"); ?></label>
                                                    <div class="col-xs-9">
                                                        <button type="button" id="addImageBtn" class="btn btn-default button"><?php print $weblangs->trans("AddPhoto"); ?> <span class="iconAddImage"></span></button>
                                                        <table class="table">
                                                            <tbody class="table-body" id="table-body-add">
                                                            </tbody>
                                                        </table>
                                                    </div>
                                                </div>
                                            <?php endif; ?>

                                            <?php if (!empty($product_id)) : ?>
                                                <div class="form-group row required">
                                                    <label class="col-xs-3 col-form-label"><?php print $weblangs->trans("otherImages"); ?></label>
                                                    <div class="col-xs-9">
                                                        <button type="button" id="addImageBtn2" class="btn btn-default button"><?php print $weblangs->trans("AddPhoto"); ?> <span class="iconAddImage"></span></button>
                                                        <input type="file" id="fileInput2" style="display:none;" accept="image/*">
                                                        <span class="notification"></span>
                                                        <?php if (!empty($otherimages)) : ?>
                                                            <table class="table">
                                                                <tbody class="table-body" id="table-body-edit">
                                                                    <?php foreach ($otherimages as $key => $image) : ?>
                                                                        <tr data-id="<?php print $key; ?>">
                                                                            <td><span class="icon-move"></span></td>
                                                                            <td class="rowCount"><?php print $image['position']; ?></td>
                                                                            <td>
                                                                                <?php print $image['filename']; ?>
                                                                            </td>
                                                                            <td>
                                                                                <img src="<?php print $image['url']; ?>" class="image-preview">
                                                                            </td>
                                                                            <td class="td-deleteBtn">
                                                                                <button type="button" data-id="<?php print $key; ?>" class="btn deleteBtn"><i class="icon-delete"></i></button>
                                                                            </td>
                                                                        </tr>
                                                                    <?php endforeach ?>
                                                                </tbody>
                                                            </table>
                                                        <?php endif; ?>
                                                    </div>
                                                </div>
                                            <?php endif; ?>

                                        </div>


                                        <p class="form-subheading"><?php print $weblangs->trans("termsAndConditionsOfUse"); ?></p>
                                        <div class="col-xs-12 box-form">

                                            <!-- TODO Add hidden option MARKETPLACE_ADD_DOLIBARR_CORE_MESSAGE to enable this section only if option is on -->
                                            <div class="field-info">
                                                <?php print $weblangs->trans("includeModuleInDolibarrInfo"); ?>
                                            </div>
                                            <?php $checked = (!empty($dolibarr_core_include) && $dolibarr_core_include == '1') ? 'checked' : ''; ?>
                                            <input type="checkbox" value="1" id="dolibarr_core_include" name="dolibarr_core_include" <?php print $checked; ?> class="dolibarr_core_include">
                                            <label for="dolibarr_core_include" id="dolibarr_core_include_label">
                                                I would like to have my module included in the core of the project as soon as possible
                                                <span class="tooltip">
                                                    <span class="fa fa-question-circle"></span>
                                                    <!--<img src="https://www.dolistore.com/img/admin/help.png" style="padding: 0px 5px;">-->
                                                    <span class="tooltiptext">
                                                        Because a Dolibarr module is a derivative work of Dolibarr, a module must be licensed under a license compatible with Dolibarr gpl v3 license.
                                                        This means your source code may be included into Dolibarr official version to enhance Dolibarr itself. However, "may" does not say "when". 
                                                        So if you really want this to happen as soon as possible, just check this box so core team will be informed. 
                                                        This will not guarantee it will be done but increase seriously probability and reduce delay time. If your module is included in core, it means the module will then be maintained by Dolibarr developer community, and module will be disabled from Dolistore as soon as dolibarr version including it is publicly available.
                                                    </span>
                                                </span>
                                            </label>

                                            <hr>
                                            

                                            <div class="form-group required">
                                                <?php
                                                $current_lang = $_SESSION['lang']['shortcode'];
                                                $page_terms_module_providers = $websitepage->fetchAll($website->id, '', '', 0, 1, array('title' => 'Terms and conditions of use - Module providers', 'status' => '1', 'lang' => $website->lang));
                                                $obj = $page_terms_module_providers[key($page_terms_module_providers)];
                                                $link = $obj->pageurl;
                                                if ($website->lang != $current_lang) {
                                                    $trans_page = $websitepage->fetchAll($website->id, '', '', 0, 1, array('fk_page' => $obj->id, 'status' => '1', 'lang' => $current_lang));
                                                    $trans_page = $trans_page[key($trans_page)];
                                                    $link = $trans_page->pageurl;
                                                }
                                                ?>

                                                <?php $checked = ($_SERVER['REQUEST_METHOD'] === 'POST' || !empty($product_id)) ? 'checked' : ''; ?>
                                                <input type="checkbox" value="1" id="dolibarr_terms" <?php print $checked; ?> name="dolibarr_terms">
                                                <label for="dolibarr_terms" id="dolibarr_terms_label" class="wordbreak">
                                                    <?php print $weblangs->trans("agreeTermsWebsite"); ?>
                                                    <a href="<?php print $link.".php"; ?>" target="_blank"><?php print $link; ?></a>
                                                </label>
                                            </div>
                                            <div class="form-group required" style="margin-top: 15px;">
                                                <input type="checkbox" value="1" id="module_wiki" <?php print $checked; ?> name="module_wiki">
                                                <label for="module_wiki" id="module_wiki_label" class="wordbreak">
                                                    <?php print $weblangs->trans("suggestToAddWikiPage"); ?>
                                                </label>
                                            </div>

                                        </div>

                                        <hr>

                                        <input type="hidden" name="action" value="new-module">
                                        <?php if (!empty($product_id)) : ?>
                                            <span id="product_to_edit" style="display:none;"> <?php print $product_id; ?></span>
                                        <?php endif; ?>


                                        <div class="form-group required">
                                            <input class="button_large_disabled" id="sub" name="sub" type="submit" value="<?php print $langs->trans("Submit"); ?>" style="opacity: 0.5;" disabled="disabled">
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- .columns-container -->
    <div class="footer-container">
        <?php includeContainer('footer'); ?>
    </div>
</div>
<?php

$themepath = dol_buildpath($conf->css, 1);
$themesubdir = '';
if (!empty($conf->modules_parts['theme'])) {    // This slow down
    foreach ($conf->modules_parts['theme'] as $reldir) {
        if (file_exists(dol_buildpath($reldir . $conf->css, 0))) {
            $themepath = dol_buildpath($reldir . $conf->css, 1);
            $themesubdir = $reldir;
            break;
        }
    }
}

$ext = 'layout=' . (empty($conf->browser->layout) ? '' : $conf->browser->layout) . '&amp;version=' . urlencode(DOL_VERSION);
print '<!-- Includes JS for CKEditor -->' . "\n";
$pathckeditor = $conf->file->dol_main_url_root . '/includes/ckeditor/ckeditor/';
$jsckeditor = 'ckeditor.js';

print '<script nonce="' . getNonce() . '">';
print '/* enable ckeditor by main.inc.php */';
// TODO Embed the ckeditor or use an online cdn cache for the ckeditor lib. We should not have url using $conf->file->dol_main_url_root that is the url of backoffice.
//print 'var CKEDITOR_BASEPATH = \'' . dol_escape_js($pathckeditor) . '\';' . "\n";
print 'var ckeditorConfig = \'' . $conf->file->dol_main_url_root . dol_escape_js(dol_buildpath($themesubdir . '/theme/' . $conf->theme . '/ckeditor/config.js' . ($ext ? '?' . $ext : ''), 1)) . '\';' . "\n"; // $themesubdir='' in standard usage
//print 'var ckeditorFilebrowserBrowseUrl = \'' . $conf->file->dol_main_url_root . '/core/filemanagerdol/browser/default/browser.php?Connector=' . $conf->file->dol_main_url_root . '/core/filemanagerdol/connectors/php/connector.php\';' . "\n";
//print 'var ckeditorFilebrowserImageBrowseUrl = \'' . $conf->file->dol_main_url_root . '/core/filemanagerdol/browser/default/browser.php?Type=Image&Connector=' . $conf->file->dol_main_url_root . '/core/filemanagerdol/connectors/php/connector.php\';' . "\n";
print '</script>' . "\n";
print '<script src="' . $pathckeditor . $jsckeditor . ($ext ? '?' . $ext : '') . '"></script>' . "\n";

?>
</body>
</html>