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


class PortraitUploadUserAPI
{
  
  public static function performPortraitUpload()
  {
    
    
    $allgSettings = AllgSettingsAPI::getAllgSettings();
    if (SessionOperations::isReplikat() && !$allgSettings->dmzEnabled) {
      $uploadMessageBox = HTMLOperations::getAlertBox( '<div class="alertbox_text">' .
              'Bei der Übertragung ist ein Fehler aufgetreten!</div>', 'Fehler', 'in_background' );
      return array( 'process' => NO_UPLOAD, 'message' => $uploadMessageBox );
    }
    
    $user = SessionOperations::getCurrentWebToolsUser();
    if ( !isset( $user ) ) {
      $errorMessage = HTMLOperations::getAlertBox('<div class="alertbox_text">Sie sind nicht mehr eingeloggt. '.
              'Bitte loggen Sie sich erneut ein!</div>', 'Fehler', 'in_background' );
      return array( 'process' => NO_UPLOAD, 'message' => $errorMessage );
    }
    
    $tool0Settings = ToolSettingsAPI::getToolSettings( 0 );
    
    
    $uploadPath = UploadUserAPI::getUploadPath4User( PORTRAIT_UPLOAD_PATH, $user, false, true );
    
    $userTypeAndNr = self::_getCurrentWebToolsUserTypeAndNr();
    
    
    
    
    $id = self::_getPortraitId4CurrentWebToolsUser();
    
    
    $imageName = $userTypeAndNr['type'] . '_' . $id . '_portrait_inArbeit.jpg';
    
    
    
    $images4Upload = UploadOperations::doUpload( 'uploadImage', $uploadPath, 1,
            $tool0Settings->bildUploadMaxSize, new UserBasedUploadFileStrategy(), '', true, $imageName );
    
    
    
    $uploadResult = NO_UPLOAD;
    $uploadMessage = '';
    $uploadErrorCount = 0;
    foreach( $images4Upload as $image4Upload ) {
      if ( $image4Upload->errorType !== UPLOAD_ERR_OK ) {
        $uploadResult = UPLOAD_NOT_OK;
        $uploadMessage .= $image4Upload->errorMsg;
        $uploadErrorCount++;
      }
    }
    
    
    if ( $uploadResult == UPLOAD_NOT_OK ) {
      $uploadMessage .= 'Bitte laden Sie ' . ( $uploadErrorCount == 1 ? 'Ihr Bild' : 'Ihre Bilder' ) .
              ' nochmals hoch!';
      $uploadMessageBox = HTMLOperations::getAlertBox( '<div class="alertbox_text">' . CleanOperations::cleanStringForHtml($uploadMessage) .
              '</div>', 'Fehler', 'in_background' );
      $processResult = array( 'process' => UPLOAD_NOT_OK, 'message' => $uploadMessageBox );
    }
      
    
    else {
      Logger::logg( "PortraitUpload: Das Portrait wurde erfolgreich bearbeitet", 200 );
      
      
      $PortraitTupel = self::_getUpdatedPortrait( $userTypeAndNr['type'], $userTypeAndNr['nr'], $image4Upload->name );
      
      
      $processResult = PortraitAPI::processPortrait( $PortraitTupel );
      
      $uploadMessage .= $processResult['message'];
      $processResult['message'] = $uploadMessage;
    }
    return $processResult;
  }
  
  
  private static function _getUpdatedPortrait($pTyp, $pNr, $pImagePath)
  {
    $dbwPortrait = new DBWorkPortrait();
    $changedFields = array();
    $portrait = $dbwPortrait->getPortrait( $pTyp, $pNr );
    
    if ( !isset( $portrait->id ) ) {
      $portrait->typ = $pTyp;
      $portrait->nr = $pNr;
      
      $portraitOrig = null;
    }
    else {
      $portraitOrig = clone $portrait;
    }
    
    $portrait_inArbeit = PostOperations::getVarValue( 'foto_portrait_inArbeit', -1 );
    if ( $portrait_inArbeit === -1 ) {
      $portrait_inArbeit = PostOperations::getVarValue( 'foto_portrait', -1 );
    }
    if ( $portrait_inArbeit !== -1 && $portrait_inArbeit !== $portrait->portrait_inArbeit ) {
      $portrait->portrait_inArbeit = $portrait_inArbeit;
      $changedFields[] = 'portrait_inArbeit';
    }
    
    
    $dataString = file_get_contents( $pImagePath );
    if ( $dataString !== false ) {
      $data = unpack("H*hex", $dataString);
      $portrait->pfoto_inArbeit = '0x' . $data['hex'];
      $changedFields[] = 'pfoto_inArbeit';
    }
    
    if ( ( $portrait->portrait_flag & PORTRAIT_FLAG_KUFERTOOLS ) != PORTRAIT_FLAG_KUFERTOOLS ) {
      $portrait->portrait_flag |= PORTRAIT_FLAG_KUFERTOOLS;
      $changedFields[] = 'portrait_flag';
    }
    
    $portrait4Replikat = clone $portrait;
    $portrait4Replikat->pfoto_inArbeit = '';
    
    return new Tuple( $portrait, $changedFields, $portraitOrig, $portrait4Replikat );
  }
  
  
  private static function _getPortraitId4CurrentWebToolsUser()
  {
    $dbwPortrait = new DBWorkPortrait();
    $userId = SessionOperations::getCurrentWebToolsUser()->dnr;
    
    return $dbwPortrait->getID('D', $userId);
  }
  
  
  private static function _getCurrentWebToolsUserTypeAndNr()
  {
    $user = SessionOperations::getCurrentWebToolsUser();
    
    if ( $user->dnr > 0 ) {
      $userType = 'D';
      $userNr = $user->dnr;
    }
    else if ( $user->tnr > 0 ) {
      $userType = 'T';
      $userNr = $user->tnr;
    }
    else if ( $user->anr > 0 ) {
      $userType = 'A';
      $userNr = $user->anr;
    }
    else if ( $user->ben > 0 ) {
      $userType = 'M';
      $userNr = $user->ben;
    }
    else {
      $userType = '';
      $userNr = -1;
    }
    
    return array( 'type' => $userType, 'nr' => $userNr );
  }
}
?>