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


class UploadUserAPI
{
  
  public static function getUploadPath4User($pUploadPath, $pUser, $pPathWithUserId = true, $pIsPortraitUpload = false)
  {
    $userKuerzel = self::_getUserPart4Path($pUser, $pPathWithUserId, $pIsPortraitUpload);

    
    if (StringOperations::substr($pUploadPath, -1) == DIRECTORY_SEPARATOR) {
      $uploadPath = $pUploadPath;
    } else {
      
      $uploadPath = $pUploadPath . '/';
    }
    $uploadPath .= $userKuerzel;

    return $uploadPath;
  }

  
  public static function getDownloadPath4User($pDownloadPath, $pUser, $pPathWithUserId = true)
  {
    $userKuerzel = self::_getUserPart4Path($pUser, $pPathWithUserId);
    
    if (StringOperations::substr($pDownloadPath, -1) == DIRECTORY_SEPARATOR) {
      $downloadpath = $pDownloadPath;
    } else {
      
      $downloadpath = $pDownloadPath . '/';
    }
    $downloadpath .= $userKuerzel;

    return $downloadpath;
  }

  
  private static function _getUserPart4Path($pUser, $pPathWithUserId, $pIsPortraitUpload = false)
  {
    $userKuerzel = '';
    $wtUserKennung = SessionOperations::getCurrentWebToolsUserKennung();

    
    
    if ($pIsPortraitUpload || $wtUserKennung === 'WT') { 
      
      if ($pUser->dnr > 0) {
        $userKuerzel = 'doz' . ($pPathWithUserId ? ('_' . $pUser->dnr) : '');
      }
      
      elseif ($pUser->tnr !== -1) {
        $userKuerzel = 'tn' . ($pPathWithUserId ? ('_' . $pUser->tnr) : '');
      } elseif ($pUser->anr !== -1) {
        $userKuerzel = 'adr' . ($pPathWithUserId ? ('_' . $pUser->anr) : '');
      }
    }
    
    
    elseif ($pUser->ben !== -1) {
      $userKuerzel = 'ma' . ($pPathWithUserId ? ('_' . $pUser->ben) : '');
    }

    return $userKuerzel;
  }
}