<?php
if( $a == 'list' ){
  if( isset( $_POST['sOption'] ) ){
    $oImage->saveImages( $_POST );
    $sOption = 'save';
  }

  if( isset( $sOption ) ){
    $content .= $oTpl->tbHtml( 'messages.tpl', 'DONE' );
  }
  
  $content .= $oTpl->tbHtml( 'images.tpl', 'LIST_TITLE' );
  $sList = $oImage->listImagesAdmin( 'images.tpl' );
  $content .= !empty( $sList ) ? $sList : $oTpl->tbHtml( 'messages.tpl', 'EMPTY' );
}
elseif( $a == 'form' ){
  if( isset( $_POST['sName'] ) ){
    $iImage = $oImage->saveImage( $_POST );
    if( isset( $_POST['sOptionList'] ) )
      header( 'Location: '.$_SERVER['PHP_SELF'].'?p='.$aActions['f'].'-list&sOption=save' );
    else
      header( 'Location: '.$_SERVER['PHP_SELF'].'?p='.$p.'&sOption=save&iImage='.$iImage );
    exit;
  }

  if( isset( $sOption ) )
    $content .= $oTpl->tbHtml( 'messages.tpl', 'DONE' );

  if( isset( $iImage ) && is_numeric( $iImage ) ){
    $aData = $oImage->throwImage( $iImage );
  }

  if( isset( $aData ) && is_array( $aData ) ){
    $aData['sDescription'] = changeTxt( $aData['sDescription'], 'nlNds' );
  }
  else{
    $iImage = null;
    $aData['iStatus'] = 1;
    $aData['iPage'] = null;
    $aData['iPosition'] = 0;
    $aData['sDescription'] = null;
    $aData['sDirectory'] = $config['default_directory'];
    $aData['iComments'] = null;
    $aData['iVotes'] = null;
    $aData['iSize1'] = 0;
    $aData['iSize2'] = 1;
  }

  $sStatusBox = throwYesNoBox( 'iStatus', $aData['iStatus'] );
  $sCommentsBox = throwYesNoBox( 'iComments', $aData['iComments'] );
  $sVotesBox = throwYesNoBox( 'iVotes', $aData['iVotes'] );
  $sPagesSelect = $oPage->throwPagesSelectAdmin( $aData['iPage'] );
  $sDirectorySelect = listDirectoriesSelect( DIR_IMAGES, $aData['sDirectory'] );
  $sSize1Select = throwSelectFromArray( $config['images_sizes'], $aData['iSize1'] );
  $sSize2Select = throwSelectFromArray( $config['images_sizes'], $aData['iSize2'] );
  $oTpl->unsetVariables( );

  $sDescription = htmlEditor ( 'sDescription', '120', '100%', $aData['sDescription'], Array( 'aOptions' => Array( 'ToolbarStartExpanded' => false ), 'ToolbarSet' => 'Basic' ) ) ;

  if( isset( $iImage ) ){
    $sImageInfo = $oTpl->tbHtml( 'images.tpl', 'INFO_IMAGE' );
    $aVotes = getVotes( $iImage );
    if( isset( $aVotes ) ){
      $sVotesInfo = $oTpl->tbHtml( 'images.tpl', 'INFO_VOTES' );
    }
  }
  else{
    $sImageForm = $oTpl->tbHtml( 'images.tpl', 'FORM_IMAGE' );
  }
  
  $sFormTabs = $oTpl->tbHtml( 'images.tpl', 'FORM_TABS' );
  $content .= $oTpl->tbHtml( 'images.tpl', 'FORM_MAIN' );
}
elseif( $a == 'multiform' ){
  if( isset( $_POST['sOption'] ) ){
    $oImage->saveImagesUpload( $_POST );
    foreach( $_POST['aPages'] as $iKey => $iPage ){
      if( !empty( $iPage ) && is_numeric( $iPage ) ){
        if( !isset( $iPageSave ) )
          $iPageSave = $iPage;

        if( $iPageSave != $iPage ){
          $bNotTheSamePage = true;
          break;
        }
      }
    }

    $sPageRedirect = ( isset( $iPageSave ) && !isset( $bNotTheSamePage ) ) ? '&iPageImages='.$iPageSave : null;
    header( 'Location: '.$_SERVER['PHP_SELF'].'?p='.$aActions['f'].'-list&sOption=save'.$sPageRedirect );
    exit;
  }

  $sPagesSelect = $oPage->throwPagesSelectAdmin( null );
  $sDirectorySelect = listDirectoriesSelect( DIR_IMAGES, $config['default_directory'] );
  $sSize1Select = throwSelectFromArray( $config['images_sizes'], 0 );
  $sSize2Select = throwSelectFromArray( $config['images_sizes'], 1 );

  $content .= $oTpl->tbHtml( 'images.tpl', 'FORM_IMAGES' );
}
elseif( $a == 'delete' && isset( $iImage ) && is_numeric( $iImage ) ){
  $oImage->deleteImage( $iImage );
  header( 'Location: '.$_SERVER['PHP_SELF'].'?p='.$aActions['f'].'-list&sOption=del' );
  exit;
}
?>