<?  // PHPmyGallery - backend core - main setup/gallery setup

/* File description:

This file processes an individual gallery's setup or the overall
setup and saves changes to DB.

### What this script does:
###
### Included from: adminmenu.index.php
### |
### | (Uses templates from adminmenu/gallery_settings)
### +- Setup          (Load Gallery Settings, Modify Settings, Save to DB, display Result)
###
*/ 

// Init stuff
$main_conf = $conf;
$main_lang = $lang;

$dbEntryName = 'db/gallerysettings/all';

// Load settings for all galleries
if(allocateData('gallerysettings','all'))
    $galleryConf = getData('gallerysettings','all');

// Load settings for this gallery (if $group and $picdir are set)
if($group != '' && $picdir != '' && allocateData('gallerysettings',$group.'--.--'.$picdir))
    $galleryConf = getData('gallerysettings',$group.'--.--'.$picdir);

// Get available language packs
// This generates a comma seperated list to put
// it into the $galleryconfig_items array
$dirObj  = dir('./lang/');
$l_packs = '';
while ($pack = $dirObj->read()) {
    if ($pack != '.' && $pack != '..') {
        $tmp = explode('.',$pack);
        if($tmp[0] == 'langpack')
            $l_packs .= $tmp[1].',';
    } // if
} // while
$l_packs = substr($l_packs,0,-1);

// Lookup if we're from do_mainsetup.php
if(isset($configure_main))
    $lang_help = $l['change_language_main'];
else
    $lang_help = $l['change_language_gallery'];

// Out of this array, setup items are generated in HTML
$galleryconfig_items = array(
    'lang'=>array(            'select',$l_packs,             $lang_help),
    'use_htmlTable'=>array(   'select','0,1,2,3,4,5,6,7,8,9',$l['use_fixedColumnNumber']),
    'usethumbs'=>array(       'select','true,false',         $l['use_thumbs']),
    'useprevnext'=>array(     'select','false,true',         $l['use_prevnext']),
    'prefix_or_suffix'=>array('select','prefix,suffix',      $l['prefix_or_suffix_select']),
    'thumb_prefix'=>array(    'text',  'th_',                $l['prefix_of_thumbnail_files']),
    'thumb_suffix'=>array(    'text',  '_th',                $l['suffix_of_thumbnail_files']),
);

// Make header templates
$pktpl_vars['template_admin_gallerysetupTableHeaders']  = pktpl('adminmenu/gallery_settings/TableHeaders.html');
$pktpl_vars['text_headline']                            = $l['gallery_setup'];
$pktpl_vars['template_admin_gallerysetupTableHeadline'] = pktpl('adminmenu/gallery_settings/TableHeadline.html');

// Genrate HTML form out of $galleryconfig_items
$tmp = '';
foreach($galleryconfig_items as $k=>$v) {
    
    if (isset($_POST[$k])) {
        $k_value = $_POST[$k];
    } // if
    else {
        if($galleryConf[$k] != '')
            $k_value = $galleryConf[$k];
        else
            $k_value = $conf[$k];
        
        if ($k_value===true) {
            $k_value = 'true';
        } // if
        elseif ($k_value===false) {
            $k_value = 'false';
        } // else
    } // else
    
    $galleryConf[$k] = $k_value;
    
    $pktpl_vars['text_help'] = $v[2];
    
    $pktpl_vars['text_defaultValue'] = stripslashes($main_conf[$k]);
    if ($v[0]=='text') {
        $pktpl_vars['text_formelementName'] = $k;
        $pktpl_vars['text_formelementValue'] = stripslashes($k_value);
        $pktpl_vars['template_all_forminputText'] = pktpl('all_forminputText.html');
    } // if
    elseif ($v[0]=='select') {
        $options = explode(',',$v[1]);
        $tmp2 = '';
        foreach($options as $vv) {
            $pktpl_vars['text_optionValue'] = $vv;
            $pktpl_vars['text_selected'] = ($k_value==$vv) ? 'selected': '';
            $pktpl_vars['text_optionCaption'] = $vv;
            $tmp2 .= pktpl('all_forminputSelectOptions.html');
        } // foreach();
        $pktpl_vars['text_selectName'] = $k;
        $pktpl_vars['template_forminputSelectOptions'] = $tmp2;
        $pktpl_vars['template_all_forminputText'] = pktpl('all_forminputSelect.html');
    } // elseif
    $tmp .= pktpl('adminmenu/gallery_settings/TableRow.html');
    
} // foreach();
$pktpl_vars['template_admin_gallerysetupTableRow'] = $tmp;

// Write Config
if ($saveit=='save_it_baby') {
    $erg = -4;
    
    if($action=='mainsetup')
        $erg = putData($galleryConf,'gallerysettings','all');
    if($group!=''&&$picdir!='')
        $erg = putData($galleryConf,'gallerysettings',$group.'--.--'.$picdir);
    
    // Assign success vars (may change in later version to be more exact)
    switch($erg) {
        case -1: // is_writable returns false
            $pktpl_vars['text_headline'] = $dbEntryName.$l['error.writefile_not_writable'];
        break;
        case -2: // fopen() failed
            $pktpl_vars['text_headline'] = $dbEntryName.$l['error.writefile_fopen_failed'];
        break;
        case -3: // fwrite() failed
            $pktpl_vars['text_headline'] = $dbEntryName.$l['error.writefile_fwrite_failed'];
        break;
        case -4: // internal error
            $pktpl_vars['text_headline'] = $dbEntryName.$l['error.writefile_internal_error'];
        break;
        case 0:
        default: // success!
            $pktpl_vars['text_headline'] = $l['success.writefile_saved'];
        break;
    } // switch
    
    // Success Box template + vars
    if($group!=''&&$picdir!='')
        $pktpl_vars['template_admin_successBoxLinks'] = pktpl('adminmenu/gallery_settings/successBoxGallerysetup.html');
    $pktpl_vars['template_admin_successBox'] = pktpl('adminmenu/admin_successBox.html');
} // if

// Final templates
$pktpl_vars['template_admin_gallerysetupTableFormsubmit'] = pktpl('adminmenu/gallery_settings/TableFormsubmit.html');
$pktpl_vars['template_admin_table'] = pktpl('adminmenu/gallery_settings/Table.html');

?>