<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

session_start();
if (!defined('BASEDIR')) {
  include_once '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'constants.php';
}




class DownloadProtocolFile
{
  
  public function start()
  {
    header('Content-type: application/octet-stream');
    header('Content-Disposition: attachment; filename=web_protocol.sql');
    echo $this->generateProtocolScript();
  }

  
  public function getFieldNames4WebProtocolDM()
  {
    $return = '';
    $return .= 'user_id, sqltime, tablename, fieldname, identifier, oldvalue, newvalue, sqltype, status, bearbeiter, bearbdatum';
    $return .= ', actionid';

    return $return;
  }

  
  public function getFieldValues4WebProtocolDM($pWebProtocol)
  {
    $return = '';
    $return .= $pWebProtocol->getVarValue('user_id') . ', ';
    $return .= "'" . $pWebProtocol->getVarValue('sqltime') . "', ";
    $return .= "'" . $pWebProtocol->getVarValue('tablename') . "', ";
    $return .= "'" . $pWebProtocol->getVarValue('fieldname') . "', ";
    $return .= "'" . $pWebProtocol->getVarValue('identifier') . "', ";
    $return .= "'" . $pWebProtocol->getVarValue('oldvalue') . "', ";
    $return .= "'" . $pWebProtocol->getVarValue('newvalue') . "', ";
    $return .= "'" . $pWebProtocol->getVarValue('sqltype') . "', ";
    $return .= $pWebProtocol->getVarValue('status') . ', ';
    $return .= "'" . $pWebProtocol->getVarValue('bearbeiter') . "', ";
    $return .= "'" . $pWebProtocol->getVarValue('bearbdatum') . "'";

    if ($pWebProtocol->getVarValue('actionid') != null) {
      $return .= ', ' . $pWebProtocol->getVarValue('actionid');
    } else {
      $return .= ', NULL';
    }

    return $return;
  }

  
  public function generateProtocolScript()
  {
    $dbwprotocol = new DBWorkWebProtocol();
    $ary = $dbwprotocol->getProtocolEntries4Book();
    $sql = '';
    foreach ($ary as $entry) {
      $sql .= 'INSERT INTO web_protocol (' . $this->getFieldNames4WebProtocolDM() . ') VALUES ';
      $sql .= '(' . $this->getFieldValues4WebProtocolDM($entry) . ");\r\n";
    }

    $dbwprotocol->setProtocolEntriesStatus();

    return $sql;
  }
}

if (($_GET['start'] == '1') && SessionOperations::getVarValueIntern('wizard_authorized') != '') { 

  $downloadprotocol = new DownloadProtocolFile();
  $downloadprotocol->start();
}