<?php
/**
 * Set session language based on:
 * - Cookie 'lang' (if exists)
 * - Browser language (if not present among website languages, use website main language)
 * - Form field 'language' (if present)
 *
 * The function also updates the product labels of the products in the session
 * to match the current language.
 *
 * @return bool
 */
function setSessionLang() {
   global $langs, $website, $weblangs, $db;
    // Get available langs for the website
    $website_langs = getAvailableLangs();
    $available_langs = $website_langs['langs'];
    $langs_code_shortcode = $website_langs['langs_code_shortcode'];

    // If no language is set or if a new language is selected (via form field)
    if (!isset($_COOKIE['lang']) || GETPOST('l', 'alpha')) {
        // Get browser language
        $browser_lang = $weblangs->getDefaultLang(1);

        // Change used lang
        $new_lang = GETPOST('l', 'alpha');
        if ($new_lang && array_key_exists ($new_lang, $available_langs)) {
            setcookie('lang', $new_lang, time() + (86400 * 30), '/');
            $session_lang = $new_lang;
        }

        // If no language is set in cookie and browser language is not present among available languages
        // use website main language as default
        if (!isset($_COOKIE['lang'])) {
            if (array_key_exists ($browser_lang, $available_langs)){
                // The browser language is present among the available languages
                setcookie('lang', $browser_lang, time() + (86400 * 30), '/');
                $session_lang = $browser_lang;
            } else {
                // The browser language is not present among the available languages
                setcookie('lang', $website->lang, time() + (86400 * 30), '/');
                $session_lang = $website->lang;
            }
        }

        // Set session lang
        if (!empty($session_lang)) {
            $_SESSION['lang']['shortcode'] = $session_lang;
            $_SESSION['lang']['code'] = $langs_code_shortcode[$session_lang];
        }

    }

    // If session language is not set, use cookie language
    if (empty($_SESSION['lang']['shortcode']) || empty($_SESSION['lang']['code'])) {
        $_SESSION['lang']['shortcode'] = $_COOKIE['lang'];
        $_SESSION['lang']['code'] = $langs_code_shortcode[$_COOKIE['lang']];
    }

    // Set Dolibarr default language
    $langs->setDefaultLang($_SESSION['lang']['code']);
    $weblangs->setDefaultLang($_SESSION['lang']['code']);
    $langs->loadLangs(array("main", "website", "languages", "members", "admin", "donations", "companies"));
    $weblangs->loadLangs(array("main", "website", "languages", "members", "admin", "donations", "companies"));

    return true;
}

/**
 * The function loops through the languages of the website configuration and
 * checks if they exist in the list of supported languages. If they do, they
 * are added to the $available_langs array, along with their corresponding
 * shortcode in the $langs_code_shortcode array.
 *
 * The function returns an array with two keys: 'langs' containing the list
 * of available languages and 'langs_code_shortcode' containing the list
 * of corresponding shortcodes.
 *
 * @return array
 */
function getAvailableLangs() {
    global $website, $weblangs;

    $available_langs = array();
    $langs_code_shortcode = array();
    $ws_langs_list = explode(',', $website->lang.','.$website->otherlang);
    $dol_available_langs = $weblangs->get_available_languages(DOL_DOCUMENT_ROOT, 1, 2, 1);
    foreach ($ws_langs_list as $lang) {
        foreach ($dol_available_langs as $key => $value) {
            if (strpos($key, $lang) === 0) {
                $available_langs[$lang] = $value;
                $langs_code_shortcode[$lang] = $key;
                break;
            }
        }
    }
    
    return array('langs' => $available_langs, 'langs_code_shortcode' => $langs_code_shortcode);
}

/**
 * Get societe information from Dolibarr configuration.
 *
 * This function retrieves the societe information from the global $conf object
 * and returns it as an associative array.
 *
 * @return array
 */
function getSocInfo() {
    global $conf, $mysoc, $weblangs, $website;
    $weblangs->load("admin");

    $societe_info = array(
        'name' => !empty($mysoc->name) ? html_entity_decode($mysoc->name) : '',
        'address' => !empty($mysoc->getFullAddress(1)) ? html_entity_decode($mysoc->getFullAddress(1)) : '',
        'zip' => !empty($mysoc->zip) ? html_entity_decode($mysoc->zip) : '',
        'town' => !empty($mysoc->town) ? html_entity_decode($mysoc->town) : '',
        'country' => !empty($mysoc->country) ? html_entity_decode($mysoc->country) : '',
        'phone' => !empty($mysoc->phone) ? html_entity_decode($mysoc->phone) : '',
        'fax' => !empty($mysoc->fax) ? html_entity_decode($mysoc->fax) : '',
        'email' => !empty($mysoc->email) ? html_entity_decode($mysoc->email) : '',
        'slogan' => getDolGlobalString('WS_'.strtoupper($website->ref).'_' . 'CUSTOM_SLOGAN') 
                ? html_entity_decode(getDolGlobalString('WS_'.strtoupper($website->ref).'_' . 'CUSTOM_SLOGAN')) 
                : (!empty($mysoc->name) 
                    ? html_entity_decode($mysoc->name) 
                    : html_entity_decode($weblangs->trans("noCompanyInfoSlogan"))),
        'description' => getDolGlobalString('WS_'.strtoupper($website->ref).'_' . 'CUSTOM_DESCRIPTION') 
                ? html_entity_decode(getDolGlobalString('WS_'.strtoupper($website->ref).'_' . 'CUSTOM_DESCRIPTION')) 
                : (!empty($mysoc->socialobject) 
                    ? html_entity_decode($mysoc->socialobject) 
                    : html_entity_decode($weblangs->trans("noCompanyInfoDescription", $weblangs->trans("CompanyObject"))))
    );

    // Add social networks if they are set
    foreach($mysoc->socialnetworks as $key => $value) {
        if (!empty($value)) {
            $societe_info[$key] = preg_match('/^http/', $value) ? $value : 'https://www.'.$key.'.com/'.$value;
        }
    }
    
    // Add link to subscription
    if (isModEnabled('multicompany')) {
      $entity_qr = '?entity=' . ((int) $conf->entity);
    } else {
      $entity_qr = '';
    }
    $urlwithouturlroot = preg_replace('/' . preg_quote(DOL_URL_ROOT, '/') . '$/i', '', trim($conf->file->dol_main_url_root));
    $urlwithroot = $urlwithouturlroot . DOL_URL_ROOT;
    $societe_info['subscription_link'] = $urlwithroot . '/public/members/new.php'.$entity_qr;
    
    // Add participation link
    $link_participation = $urlwithroot.'/public/eventorganization/attendee_new.php?type=global';
	$societe_info['link_participation'] = $link_participation;
	
	// Add donation link
    $link_donation = $urlwithroot.'/public/donations/new.php';
	$societe_info['link_donation'] = $link_donation;

    return $societe_info;
}