<?php
require_once DIR_CORE.'files-'.DB_TYPE.'.php';
require_once DIR_CORE.'files-admin.php';
require_once DIR_CORE.'files.php';

require_once DIR_CORE.'products-'.DB_TYPE.'.php';
require_once DIR_CORE.'products-admin.php';
require_once DIR_CORE.'products.php';

require_once DIR_CORE.'categories-'.DB_TYPE.'.php';
require_once DIR_CORE.'categories.php';

if( $a == 'List' ){
  if( isset( $sOption ) ){
    if( $sOption == 'del' )
      $content .= $tpl->tbHtml( 'messages.tpl', 'DELETED_SHORT' );
    elseif( $sOption == 'save' )
      $content .= $tpl->tbHtml( 'messages.tpl', 'SAVED_SHORT' );    
  }
  
  if( isset( $iCategory ) && is_numeric( $iCategory ) ){
    $aCategory[] = $iCategory;
  }
  else{
    $iCategory = null;
    $aCategory = Array( );
  }
  
  $sCategorySelect = listCategories( 'categories_select.tpl', null, null, $aCategory, null, null, 1 );
  if( !isset( $sCategorySelect ) )
    $sCategorySelect = $tpl->tbHtml( 'categories_select.tpl', 'NOT_FOUND' );

  $content .= $tpl->tbHtml( 'products_list.tpl', 'HEAD' );
  $sProductsList = listProducts( 'products_list.tpl' );
  $content .= !empty( $sProductsList ) ? $sProductsList : $tpl->tbHtml( 'products_list.tpl', 'NOT_FOUND' );
}
elseif( $a == 'Form' ){
  if( isset( $_POST['sOption'] ) && $_POST['sOption'] == 'save' ){
    saveProduct( $_POST );
    header( 'Location: '.$_SERVER['PHP_SELF'].'?p='.$aActions['g'].'List&sOption=save' );  
    exit;
  }
  else{
    if( isset( $iProduct ) && is_numeric( $iProduct ) )
      $aData = throwProduct( $iProduct );
    if( !isset( $iCategory ) )
      $iCategory = null;
      
    if( isset( $aData ) && is_array( $aData ) ){
      $aFiles   = listFiles( $iProduct, 'products_form.tpl', 1 );

      $aData['sDescriptionShort'] = changeTxt( $aData['sDescriptionShort'], 'nlNds' );
      $aData['sDescriptionFull']  = changeTxt( $aData['sDescriptionFull'], 'nlNds' );
    }
    else{
      $aData['aCategories']       = Array( );
      $aData['iStatus']           = 1;
      $aData['iPosition']         = 0;
      $aData['sDescriptionFull']  = null;
    }
    $sCategoriesSelect  = listCategories( 'categories_select.tpl', null, null, $aData['aCategories'], null, null, 1 );
    if( !isset( $sCategoriesSelect ) )
      $sCategoriesSelect = $tpl->tbHtml( 'categories_select.tpl', 'NOT_FOUND' );
    $sStatusSelect      = throwYesNoSelect( $aData['iStatus'] );

    $sHtmlEditor = htmlEditor( 'sDescriptionFull', '300', '600', $aData['sDescriptionFull'] );
    
    $content .= $tpl->tbHtml( 'products_form.tpl', 'FORM' );
  }
}
elseif( $a == 'Delete' && isset( $iProduct ) && is_numeric( $iProduct ) ){
  delProduct( $iProduct );
  header( 'Location: '.$_SERVER['PHP_SELF'].'?p='.$aActions['g'].'List&sOption=del' );
  exit;
}
?>