<?php

/*  Alternate WebKit
(C) 2013 by Alternate Tools
-----------------------------------------------------

File:         select_theme.php
Description:  Administrative module to select theme

*/

    include "../modules/class_theme.php";
    include "../modules/class_themelist.php";
    include "../modules/mod_admin.php";

    $b_ErrorOccured = false;
    $usrId = "";
    $errorString = "";

    // Check privileges
    check_login();
    if (user_is_reporter() == true || user_is_customer() == true)
    {
        log_out();
        return;
    }

    $thmTheme = new cTheme;
    $thmTheme->SetFolder($THEME_FOLDER);
    $thmList = new cThemeList;
    $thmList->SetFolder($THEME_FOLDER);

    // Theme select cancelled?
    if (isset ($_POST['thm_Cancel']))
    {
        header('location: ' . $ADMIN_MAIN_SITE );
        return;
    }

    // Check for errors and if none, store the user values
    if (isset ($_POST['thm_Save']))
    {
        if (isset($_POST['thm_ThemeId']) == false || $_POST['thm_ThemeId'] == "")
            $errorString = get_translation('admin_thmsel_errorconfig') . '!';
        else if ($thmTheme->LoadThemeFromId($_POST['thm_ThemeId']) == false)
            $errorString = get_translation('admin_thmsel_errorconfig') . '!';

        if ($errorString == "")
        {
            if ($thmTheme->InstallTheme($THEME_MAIN_FOLDER, $THEME_IMAGES_FOLDER, $THEME_FILES_FOLDER, $FAVICO_IMAGE) == false)
                $errorString = get_translation('admin_thmsel_errorinstall') . '!';
            else
            {
                $cConfig->SetParameter("site_theme", $thmTheme->GetId());
                if ($cConfig->SaveConfigFile() == false)
                {
                    $errorString = get_translation('admin_thmsel_errorconfig') . '!';
                }
                else // All done
                {
                    // Update the index file
                    update_index_file($thmTheme->GetCompleteFolder() . $thmTheme->GetIndexFile(), $SITE_INDEX_FILE);
                    log_message ('User ' . get_user_name() . ' changed theme ' . $thmTheme->GetId());
                    header ('Location: ' . $ADMIN_MAIN_SITE);
                    return;
                }
            }
        }
    }

    // Display the theme selection page
    print_admin_header(get_translation('admin_thmsel_head'), true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-12', true, get_translation('admin_backtomain'), "", "", false);
    echo_r('<script type="text/javascript" language="JavaScript">');
    echo_r('function selectTheme() {');
    echo_r('  document.getElementById(\'thmPreview\').src=document.getElementById(document.getElementById(\'thmId\').value).value;');
    echo_r('  document.getElementById(\'thmDate\').innerHTML=document.getElementById(document.getElementById(\'thmId\').value + \'_date\').value;');
    echo_r('  document.getElementById(\'thmImages\').innerHTML=document.getElementById(document.getElementById(\'thmId\').value + \'_logo\').value + \'<br>\' + document.getElementById(document.getElementById(\'thmId\').value + \'_index\').value + \'<br>\' + document.getElementById(document.getElementById(\'thmId\').value + \'_ico\').value;');
    echo_r('}');
    echo_r('</script>');
    finish_admin_header(get_translation('admin_thmsel_head'), true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-12', true, get_translation('admin_backtomain'), "", "");
    if ($errorString !="")
        print_error_message($errorString);

    echo_r('<form action="' . $_SERVER['PHP_SELF'] . '" method="post" enctype="multipart/form-data">');

    echo_r('<table width="100%">');
    echo_r('  <tr>');
    echo_r('    <td width="30%">' . get_translation('admin_thmsel_theme') . ':</td>');
    echo_r('    <td><select name="thm_ThemeId" id="thmId" onChange="selectTheme();">');

    $thmList->LoadThemeList();
    $thmList->SortThemeList(3);  // name asc
    $list = $thmList->GetThemeList();
    $mainFiles = array();
    $mainFile = "";
    $mainLogoFiles = array();
    $mainLogo = "";
    $mainIndexLogoFiles = array();
    $mainIndexLogo = "";
    $mainFavIcons = array();
    $mainFavIcon = "";
    $mainDates = array();
    $mainDate = "";
    $mainIds = array();
    for ($i = 0; $i < count($list); $i ++)
    {
        $thmTheme->LoadThemeFromId($list[$i]);
        echo('      <option value="' . $thmTheme->GetId() . '"');
        if ($SITE_THEME == $thmTheme->GetId())
        {
            $mainFile = $thmTheme->GetFolder() . $thmTheme->GetThemeFolder() . $thmTheme->GetMainFile();
            $mainDate = $thmTheme->GetDate();
            $mainLogoFile = $thmTheme->GetCompleteFolder() . $thmTheme->GetImagesFolder() . $thmTheme->GetLogo();
            if (file_exists($mainLogoFile))
                $mainLogoFile = "<font color='#00AA00'>" . basename($mainLogoFile) . " [" . $thmTheme->GetLogoWidth() . " x " . $thmTheme->GetLogoHeight() . " Pixel]</font>";
            else
                $mainLogoFile = "<font color='#AA0000'>" . basename($mainLogoFile) . " [" . $thmTheme->GetLogoWidth() . " x " . $thmTheme->GetLogoHeight() . " Pixel]</font>";
            $mainIndexLogo =  $thmTheme->GetCompleteFolder() . $thmTheme->GetImagesFolder() . $thmTheme->GetIndexLogo();
            if (file_exists($mainIndexLogo))
                $mainIndexLogo = "<font color='#00AA00'>" . basename($mainIndexLogo) . " [" . $thmTheme->GetIndexLogoWidth() . " x " . $thmTheme->GetIndexLogoHeight() . " Pixel]</font>";
            else
                $mainIndexLogo = "<font color='#AA0000'>" . basename($mainIndexLogo) . " [" . $thmTheme->GetIndexLogoWidth() . " x " . $thmTheme->GetIndexLogoHeight() . " Pixel]</font>";
            $mainFavIcon = $thmTheme->GetCompleteFolder() . $thmTheme->GetImagesFolder() . $thmTheme->GetFavIco();
            if (file_exists($mainFavIcon))
                $mainFavIcon = "<font color='#00AA00'>" . basename($mainFavIcon) . "</font>";
            else
                $mainFavIcon = "<font color='#AA0000'>" . basename($mainFavIcon) . "</font>";
            echo (' selected');
        }
        echo_r('>' . $thmTheme->GetName() . '</option>');
        $mainIds[] =  $thmTheme->GetId();
        $mainFiles[] = $thmTheme->GetFolder() . $thmTheme->GetThemeFolder() . $thmTheme->GetMainFile();
        $mainDates[] = $thmTheme->GetDate();
        $mainLogoFiles[] = $thmTheme->GetCompleteFolder() . $thmTheme->GetImagesFolder() . $thmTheme->GetLogo();
        if (file_exists($mainLogoFiles[$i]))
            $mainLogoFiles[$i] = "<font color='#00AA00'>" . basename($mainLogoFiles[$i]) . " [" . $thmTheme->GetLogoWidth() . " x " . $thmTheme->GetLogoHeight() . " Pixel]</font>";
        else
            $mainLogoFiles[$i] = "<font color='#AA0000'>" . basename($mainLogoFiles[$i]) . " [" . $thmTheme->GetLogoWidth() . " x " . $thmTheme->GetLogoHeight() . " Pixel]</font>";
        $mainIndexLogoFiles[] = $thmTheme->GetCompleteFolder() . $thmTheme->GetImagesFolder() . $thmTheme->GetIndexLogo();
        if (file_exists($mainIndexLogoFiles[$i]))
            $mainIndexLogoFiles[$i] = "<font color='#00AA00'>" . basename($mainIndexLogoFiles[$i]) . " [" . $thmTheme->GetIndexLogoWidth() . " x " . $thmTheme->GetIndexLogoHeight() . " Pixel]</font>";
        else
            $mainIndexLogoFiles[$i] = "<font color='#AA0000'>" . basename($mainIndexLogoFiles[$i]) . " [" . $thmTheme->GetIndexLogoWidth() . " x " . $thmTheme->GetIndexLogoHeight() . " Pixel]</font>";
        $mainFavIcons[] = $thmTheme->GetCompleteFolder() . $thmTheme->GetImagesFolder() . $thmTheme->GetFavIco();
        if (file_exists($mainFavIcons[$i]))
            $mainFavIcons[$i] = "<font color='#00AA00'>" . basename($mainFavIcons[$i]) . "</font>";
        else
            $mainFavIcons[$i] = "<font color='#AA0000'>" . basename($mainFavIcons[$i]) . "</font>";
    }
    
    echo_r('    </select></td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td width="30%">' . get_translation('admin_thmsel_date') . ':</td>');
    echo_r('    <td id="thmDate">' . $mainDate . '</td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td width="30%">' . get_translation('admin_thmsel_images') . ':</td>');
    echo_r('    <td id="thmImages">' . $mainLogoFile . '<br>' . $mainIndexLogo . '<br>' . $mainFavIcon . '</td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td width="30%">' . get_translation('admin_thmsel_preview') . ':</td>');
    echo_r('    <td><iframe id="thmPreview" width="100%" height="300" src="' . $mainFile . '"></iframe></td>');
    echo_r('  </tr>');
    echo_r('</table>');
    for ($i = 0; $i < count($mainFiles); $i ++)
    {
        echo_r('<input type="hidden" id="' . $mainIds[$i] . '" value="' . $mainFiles[$i]  . '">');
        echo_r('<input type="hidden" id="' . $mainIds[$i] . '_date" value="' . $mainDates[$i]  . '">');
        echo_r('<input type="hidden" id="' . $mainIds[$i] . '_logo" value="' . $mainLogoFiles[$i]  . '">');
        echo_r('<input type="hidden" id="' . $mainIds[$i] . '_index" value="' . $mainIndexLogoFiles[$i]  . '">');
        echo_r('<input type="hidden" id="' . $mainIds[$i] . '_ico" value="' . $mainFavIcons[$i]  . '">');
    }

    print_bottom_links("thm_Save", get_translation('admin_save'),
                       "thm_Cancel", get_translation('admin_cancel'), false);
    echo_r('</form>');

    print_admin_footer();
?>