<?  // PHPmyGallery - backend core - picture descriptions

/* File description:

This file is used to edit the picture descriptions for a gallery.
This is the main feature of PHPmyGallery which was supported since the first version.

### What this script does:
###
### Included from: adminmenu.index.php
### |
### | (Uses templates from adminmenu/picture_descriptions)
### +- Picture Descriptions   (Load Descriptions if any, Modify Descriptions, Save to DB, display Result)
###
*/ 

// Init template variables
$pktpl_vars['text_formelementSize']      = 50;
$pktpl_vars['text_formelementMaxlength'] = 200;
$pktpl_vars['textarea_cols']             = 50;
$pktpl_vars['textarea_rows']             = 3;

// Include picture descriptions file (if exists)
if($saveit!='save_it_baby' &&
    $group!='' && $picdir!='' &&
    allocateData('picsdescriptions',$group.'--.--'.$picdir)) {
    
    $picset = getData('picsdescriptions',$group.'--.--'.$picdir);
}
else {
    $picset = array();
}

// Gallery Title, data from POST vars or picture descriptions file
if($saveit=='save_it_baby') 
    $picset[METADATA]['pagetitle'] = $_POST['pagetitle'];

$pagetitle = $picset[METADATA]['pagetitle'];

$pktpl_vars['text_group'] = $group;

$pktpl_vars['text_formelementName']  = 'pagetitle';
$pktpl_vars['text_formelementValue'] = htmlentities(stripslashes($pagetitle));
$pktpl_vars['template_all_forminputTextTitle'] = pktpl('all_forminputText.html');

// Make template
$pktpl_vars['template_admin_picsdescriptionsTableGallerytitle'] = pktpl('adminmenu/picture_descriptions/TableGallerytitle.html');

// Retreive gallery settings and overwrite their entries in the conf array
// This is needed for thumbnail suffix/prefix settings
if(allocateData('gallerysettings',$group.'--.--'.$picdir)) {
    $galleryconf = getData('gallerysettings',$group.'--.--'.$picdir);
    
    foreach($galleryconf as $k=>$confItem)
        $conf[$k] = $confItem;
} // if

// Picture Descriptions Form
$tmp = '';
$arr = filenames2arr($conf['pathToGroups'].$group.'/'.$picdir, IMAGES);
foreach($arr as $file) {
    // Create unique form element name for each picture
    $var  = 'F' . str_replace('.','_',$file).'__fullname';
    $var2 = 'F' . str_replace('.','_',$file).'__fulltext';
    
    // Data from POST vars or picture descriptions file
    if($saveit=='save_it_baby') {
        $picset[PICSDATA][$file]['fullname'] = $_POST[$var];
        $picset[PICSDATA][$file]['fulltext'] = $_POST[$var2];
    } // if
    $fullname = $picset[PICSDATA][$file]['fullname'];
    $fulltext = $picset[PICSDATA][$file]['fulltext'];
    
    // Make Template variables for picname and picdescription
    $pktpl_vars['text_linktext']              = $file;
    $pktpl_vars['text_pic']                   = urlencode($file);
    $pktpl_vars['text_formelementName']       = $var;
    $pktpl_vars['text_formelementValue']      = htmlentities(stripslashes($fullname));
    $pktpl_vars['template_all_forminputText'] = pktpl('all_forminputText.html');
    $tmp .= pktpl('adminmenu/picture_descriptions/TableRow1.html');
    
    $pktpl_vars['text_formelementName']       = $var2;
    $pktpl_vars['text_formelementValue']      = htmlentities(stripslashes($fulltext));
    
    if($conf['picsdescriptions_multiline_comment'])
        $pktpl_vars['template_all_forminputText'] = pktpl('all_forminputTextarea.html');
    else
        $pktpl_vars['template_all_forminputText'] = pktpl('all_forminputText.html');
    
    $tmp .= pktpl('adminmenu/picture_descriptions/TableRow2.html');
} // foreach
$pktpl_vars['template_admin_picsdescriptionsTableRow'] = $tmp;

// Write Config
if ($saveit=='save_it_baby') {
    $erg = putData($picset,'picsdescriptions',$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'] = $filename.$l['error.writefile_not_writable'];
        break;
        case -2: // fopen() failed
            $pktpl_vars['text_headline'] = $filename.$l['error.writefile_fopen_failed'];
        break;
        case -3: // fwrite() failed
            $pktpl_vars['text_headline'] = $filename.$l['error.writefile_fwrite_failed'];
        break;
        case 0:
        default: // success!
            $pktpl_vars['text_headline'] = $l['success.writefile_saved'];
        break;
    } // switch
    
    // Success Box template + vars
    $pktpl_vars['template_admin_successBoxLinks'] = pktpl('adminmenu/picture_descriptions/successBoxPicturedescriptions.html');
    $pktpl_vars['template_admin_successBox'] = pktpl('adminmenu/admin_successBox.html');
} // if

// Make final templates for do_picssetup
$pktpl_vars['template_admin_picsdescriptionsTableHeaders'] = pktpl('adminmenu/picture_descriptions/TableHeaders.html');

$pktpl_vars['text_headline']                                  = $l['galleries_name'];
$pktpl_vars['template_admin_picsdescriptionsTableHeadline_1'] = pktpl('adminmenu/picture_descriptions/TableHeadline.html');

$pktpl_vars['text_headline']                                  = $l['picture_descriptions'];
$pktpl_vars['template_admin_picsdescriptionsTableHeadline_2'] = pktpl('adminmenu/picture_descriptions/TableHeadline.html');

$pktpl_vars['template_admin_picsdescriptionsTableFormsubmit'] = pktpl('adminmenu/picture_descriptions/TableFormsubmit.html');
$pktpl_vars['template_admin_table']                           = pktpl('adminmenu/picture_descriptions/Table.html');
?>