<?php
    $urls = $this->driver->getOnlineLinks($expand=true);
    $aLinks = $this->driver->getAdditionalLinks();
    $displayDownloadButton = false;
    if (isset($urls))  {
        foreach($urls as $current)  {
                if (strcmp($current['desc'], "Inhaltsverzeichnis") == 0 || strncmp($current['desc'],"Volltext über anderen Anbieter", strlen("Volltext über anderen Anbieter")) == 0)    continue;
                $displayDownloadButton = true;
                break;
        }
    }

  //   $url = is_array($urls) ? $urls[0] : $urls;

  $coverDetails = $this->record($this->driver)->getCoverDetails('result-list', 'medium', $this->recordLinker($this->results)->getUrl($this->driver));
  $cover = $coverDetails['html'];
  $thumbnail = false;
  $data = $this->driver->getRawData(); 
  $id = $data['id'];
  $voevzlink = '';
  if (isset($data['voevzlink'])) {
         if (is_array($data['voevzlink'])) { 
		$voevzlink = $data['voevzlink'][0]; 
	   } else { 
		$voevzlink = $data['voevzlink'];
  	}
  }
  //  $voevzlink = is_array($data['voevzlink']) ? $data['voevzlink'][0] : $data['voevzlink']; // isset($data['voevzlink'][0]) ? $data['voevzlink'][0] : '' ;
  $license = []; 
  if (isset($data['rightsuri']))    {
	if (is_array($data['rightsuri']))  {
		$license = $data['rightsuri'][0];
	    } else {
		$license = $data['rightsuri']; 
	}
     } else {
	$license = []; 

  }
//  $license =  isset($data['rightsuri']) ? $data['rightsuri'] : '' ;
//  $license   = isset($data['rightsuri'][0])    ? $data['rightsuri'][0]    : '' ;
  $parts = explode("/", $license);
  $rightsImage = $rightsLink = '';
  $orderText = ''; 
  $filetypes = $this->driver->getFileType();
  $filetype  = count($filetypes) > 0 ? $filetypes[0] : '';
  if (strlen(trim($filetype)) == 0)  $filetype = "PDF";
  if (strcmp(trim(strtoupper($filetype)), "ADOBE PDF") == 0) $filetype = "PDF";
  if (isset($parts) && count($parts) > 1)   {
	  $rightsImage = "/vufind/themes/bibb/images/".$parts[4].".png";
	  $rightsLink = $license; 
  }
  if (strlen($rightsLink) == 0)  {
        $right = isset($data['license'][0]) ? $data['license'][0] : '' ;
        if (strcmp($right,"Urheberrecht") == 0 || strcmp($right,"Deutsches Urheberrecht") == 0)  {
                $rightsLink = "http://www.gesetze-im-internet.de/urhg";
                $rightsImage = "/vufind/themes/bibb_new/images/urheberrechtDe.png";
        }
  }

  $thumbnailAlignment = $this->record($this->driver)->getThumbnailAlignment('result');

  // Collect data for the article requestform
  $formatArray = $this->driver->getFormats(); 

  (! in_array("Electronic resource", $formatArray) && (in_array("Journal article", $formatArray) || in_array("Paper in edited volumes", $formatArray))) ?  $displayRequestForm = 1 : $displayRequestForm = 0  ;
  if ($displayRequestForm == 1)  {
	  if (in_array("Journal article", $formatArray)) $orderText = 'Artikel';
	  if (in_array("Paper in edited volumes", $formatArray)) $orderText = 'Beitrag';
	  $articleTitle 	= $this->record($this->driver)->getTitleHtml(); 
	  $journalTitle 	= $this->driver->getContainerTitle(); 
	  $journalVolume 	= $this->driver->getContainerVolume(); 
	  $journalIssue 	= $this->driver->getContainerIssue(); 
	  $journalStartPage	= $this->driver->getContainerStartPage();
	  $outText = ''; 
	  if (strlen($journalTitle)) 		$outText .= "erschienen in : " .$journalTitle;  
	  if (strlen($journalVolume)) 		$outText .= " / " .$journalVolume; 
	  if (strlen($journalIssue))  		$outText .= ", " .$journalIssue ; 
	  if (strlen($journalStartPage)) 	$outText .= " (" .$journalStartPage .")"; 
  }

  if ($cover):
    ob_start(); ?>
    <div class="media-<?=$thumbnailAlignment ?> <?=$this->escapeHtmlAttr($coverDetails['size'])?>">
      	<?=$cover ?>
    </div> 
    <?php $thumbnail = ob_get_contents(); ?>
    <?php ob_end_clean(); ?>
   <?php endif; ?>


  <input type="hidden" value="<?=$this->escapeHtmlAttr($this->driver->getUniqueID())?>" class="hiddenId" />
  <input type="hidden" value="<?=$this->escapeHtmlAttr($this->driver->getSourceIdentifier())?>" class="hiddenSource" />
  <div class="media">
  <?php if ($thumbnail && $thumbnailAlignment == 'left'): ?>
    <?=$thumbnail ?>
  <?php endif ?>

  <div class="media-body">
    <div class="result-body">

        <div class="title-container">
            <a href="<?=$this->recordLinker($this->results)->getUrl($this->driver)?>" class="title getFull" data-view="<?=$this->params->getOptions()->getListViewOption() ?>">
            <?=$this->record($this->driver)->getTitleHtml()?>
	    <?php $subTitle = $this->driver->getTitleSection(); ?>
            <?php if (!empty($subTitle)): ?>
		<br> <?=$subTitle?>
            <?php endif; ?>
	    </a>
        </div>

        <div class="author">
            <?php $summAuthors = $this->driver->getPrimaryAuthorsWithHighlighting(); if (!empty($summAuthors)): ?>
                <?=$this->transEsc('by')?>
                <?php $authorCount = count($summAuthors); foreach ($summAuthors as $i => $summAuthor): ?>
                    <a href="<?=$this->record($this->driver)->getLink('author', $this->highlight($summAuthor, null, true, false))?>"><?=$this->highlight($summAuthor)?></a><?=$i + 1 < $authorCount ? ';' : ''?>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>

        <table width="99%">
	    <tr>
	        <th width="35%"></th>
        	<td width="20%"></td>
	        <td width="15%"></td>
	        <td width="*"></td>
	    </tr>
	    <tbody class="result-table">
            <tr>
                <th><?=$this->transEsc("Format")?></th>
                <th><?=$this->transEsc("Stock")?></th>
                <th>&nbsp;</th>
                <th><?=$this->transEsc("Availability")?></th>
            </tr>
            <tr>
	    <td><?=$this->record($this->driver)->getFormatList() ?></td>
	    <td>  
		<?php $Collections = $this->driver->getCollection() ; if (!empty ($Collections)) :  ?>
			<?php foreach($Collections as $Collection)  : ?>
				<?php if (strcmp($Collection,"VET Repository") == 0) : ?> <?php  $img = "VET_Repository_small.png"; ?> <?php endif; ?>
				<?php if (!strcmp($Collection,"VET Repository") == 0) : ?> <?php  $img = "BIBB_library_small.png"; ?> <?php endif; ?>
				<img src="/vufind/themes/bibb/images/<?=$img?>">
			<?php endforeach ; ?>
		<?php endif; ?>

	    </td>
                <td>
                   <?php $summCallNo = $this->driver->getCallNumber(); if (!empty($summCallNo)): ?>
                        <?=$this->escapeHtml($summCallNo)?>
                    <?php endif; ?>
                </td>

                <td>
        <?php $openUrl = $this->openUrl($this->driver, 'results'); 
	   $openUrlActive = $openUrl->isActive(); 
           $urls = $this->record($this->driver)->getLinkDetails($openUrlActive); 

	   $urls = $this->driver->getOnlineLinks($expand=true); // extend links with PIDs in case links are empty
	?>

        <?php if (!is_array($urls)) $urls = array();
          if(!$this->driver->isCollection()):
            foreach ($urls as $current): ?>
		<?php if (strlen($rightsImage))  : ?>
                 <a href="<?= $rightsLink ?>"><img src="<?= $rightsImage ?>" alt="License" style="zoom: 0.2" name="<?php echo $this->escapeHtmlAttr($this->driver->getUniqueID()); ?>" onmouseover="this.style.zoom='0.8'" onmouseout="this.style.zoom='0.2'" /></a>
                 <?php  $rightsImage = ''; ?>
              <?php endif; ?>

              <a href="<?=$this->escapeHtmlAttr($this->proxyUrl($current['url']))?>" class="fulltext" target="new"><i class="fa fa-external-link" aria-hidden="true"></i> <?=($current['url'] == $current['desc']) ? $this->transEsc('Get full text') : str_replace("DESC:","",$this->escapeHtml($current['desc']))?></a><br/>
          <?php endforeach; ?>
        <?php endif; ?>

        <?php if (!$openUrlActive && empty($urls) && $this->driver->supportsAjaxStatus()): ?>
             <span class="status ajax-availability hidden">
               	<span class="label label-default"><?=$this->transEsc('Loading')?>...</span>
             </span>

        <?php endif; ?>
        </td>
        </tr>
	</tbody>
        </table>

      <div class="published">
        <?php if($this->driver->isCollection()): ?>
          <?=implode('<br>', array_map(array($this, 'escapeHtml'), $this->driver->getSummary())); ?>
        <?php else: ?>

          <?php $journalTitle = $this->driver->getContainerTitle(); $summDate = $this->driver->getPublicationDates(); ?>
          <?php if (!empty($journalTitle)): ?>
            <?=!empty($summAuthor) ? '<br />' : ''?>
            <?=$this->transEsc('Published in')?>
            <?php $containerSource = $this->driver->getSourceIdentifier(); ?>
            <?php $containerID = $this->driver->getContainerRecordID(); ?>
            <?php /* TODO: handle highlighting more elegantly here: */?>
            <a href="<?=($containerID ? $this->recordLinker($this->results)->getUrl("$containerSource|$containerID") : $this->record($this->driver)->getLink('journaltitle', str_replace(array('{{{{START_HILITE}}}}', '{{{{END_HILITE}}}}'), '', $journalTitle)))?>"><?=$this->highlight($journalTitle) ?></a>
            <?=!empty($summDate) ? ' (' . $this->escapeHtml($summDate[0]) . ')' : ''?>
          <?php elseif (!empty($summDate)): ?>
            <?=!empty($summAuthor) ? '<br />' : ''?>
            <?=$this->transEsc('Published') . ' ' . $this->escapeHtml($summDate[0])?>
          <?php endif; ?>
          <?php $summInCollection = $this->driver->getContainingCollections(); if (!empty($summInCollection)): ?>
            <?php foreach ($summInCollection as $collId => $collText): ?>
              <div>
                <b><?=$this->transEsc("in_collection_label")?></b>
                <a class="collectionLinkText" href="<?=$this->url('collection', array('id' => $collId))?>?recordID=<?=urlencode($this->driver->getUniqueID())?>">
                  <?=$this->escapeHtml($collText)?>
                </a>
              </div>
            <?php endforeach; ?>
          <?php endif; ?>
        <?php endif; ?>
      </div>

      <?php if(!$this->driver->isCollection()): ?>
        <?php if ($snippet = $this->driver->getHighlightedSnippet()): ?>
          <?php if (!empty($snippet['caption'])): ?>
            <strong><?=$this->transEsc($snippet['caption']) ?>:</strong> ';
          <?php endif; ?>
          <?php if (!empty($snippet['snippet'])): ?>
            <span class="quotestart">&#8220;</span>...<?=$this->highlight($snippet['snippet']) ?>...<span class="quoteend">&#8221;</span><br/>
          <?php endif; ?>
        <?php endif; ?>
      <?php endif; ?>

      <?
      /* Display information on duplicate records if available */
      if ($dedupData = $this->driver->getDedupData()): ?>
        <div class="dedupInformation">
        <?
          $i = 0;
          foreach ($dedupData as $source => $current) {
            if (++$i == 1) {
              ?><span class="currentSource"><a href="<?=$this->recordLinker($this->results)->getUrl($this->driver)?>"><?=$this->transEsc("source_$source", array(), $source)?></a></span><?
            } else {
              if ($i == 2) {
                ?> <span class="otherSources">(<?=$this->transEsc('Other Sources')?>: <?
              } else {
                ?>, <?
              }
              ?><a href="<?=$this->recordLinker($this->results)->getUrl($current['id'])?>"><?=$this->transEsc("source_$source", array(), $source)?></a><?
            }
          }
          if ($i > 1) {
            ?>)</span><?
          }?>
        </div>
      <?php endif; ?>

      <?php /* We need to find out if we're supposed to display an OpenURL link ($openUrlActive),
            but even if we don't plan to display the link, we still want to get the $openUrl
            value for use in generating a COinS (Z3988) tag -- see bottom of file.
          */ 
        $openUrl = $this->openUrl($this->driver, 'results');
        $openUrlActive = $openUrl->isActive();
        // Account for replace_other_urls setting
        $urls = $this->record($this->driver)->getLinkDetails($openUrlActive);

        if ($openUrlActive || !empty($urls)): ?>
        <?php if ($openUrlActive): ?>
          <?=$openUrl->renderTemplate()?>
        <?php endif; ?>
        <?php endif; ?>

      <div class="result-previews">
        <?=$this->record($this->driver)->getPreviews()?>
      </div>
    </div>

      <div class="add-result">
          <div class="favorites">
              <?php if ($this->userlist()->getMode() !== 'disabled'): ?>
                  <?php /* Add to favorites */ ?>
                  <a href="<?=$this->recordLinker($this->results)->getActionUrl($this->driver, 'Save')?>" data-lightbox class="save-record result-link-label" data-id="<?=$this->escapeHtmlAttr($this->driver->getUniqueId()) ?>"><?=$this->transEsc('Add to favorites')?></a>
                  <i class="result-link-icon fa fa-fw fa-star" aria-hidden="true"></i><br/>
                  <?php /* Saved lists */ ?>
                  <div class="savedLists">
                      <strong><?=$this->transEsc("Saved in")?>:</strong>
                  </div>
              <?php endif; ?>
          </div>

          <div class="result-links hidden-print">
              <?php /* Display qrcode if appropriate: */ ?>
              <?php if ($QRCode = $this->record($this->driver)->getQRCode("results")): ?>
                  <?
                  // Add JS Variables for QrCode
                  $this->jsTranslations()->addStrings(array('qrcode_hide' => 'qrcode_hide', 'qrcode_show' => 'qrcode_show'));
                  ?>
                  <span class="hidden-xs">
          <a href="<?=$this->escapeHtmlAttr($QRCode);?>" class="qrcodeLink"><?=$this->transEsc('qrcode_show')?></a><i class="fa fa-fw fa-qrcode" aria-hidden="true"></i>
          <div class="qrcode hidden">
            <script type="text/template" class="qrCodeImgTag">
              <img alt="<?=$this->transEsc('QR Code')?>" src="<?=$this->escapeHtmlAttr($QRCode);?>"/>
            </script>
          </div><br/>
        </span>
              <?php endif; ?>

              <?php if ($this->cart()->isActiveInSearch() && $this->params->getOptions()->supportsCart() && $this->cart()->isActive()): ?>
                  <?=$this->render('record/cart-buttons.phtml', ['id' => $this->driver->getUniqueId(), 'source' => $this->driver->getSourceIdentifier()]); ?><br/>
              <?php endif; ?>

              <?php /* Hierarchy tree link */ ?>
              <?php $trees = $this->driver->tryMethod('getHierarchyTrees'); if (!empty($trees)): ?>
                  <?php foreach ($trees as $hierarchyID => $hierarchyTitle): ?>
                      <div class="hierarchyTreeLink">
                          <input type="hidden" value="<?=$this->escapeHtmlAttr($hierarchyID)?>" class="hiddenHierarchyId" />

                          <a class="hierarchyTreeLinkText result-link-label" data-lightbox href="<?=$this->recordLinker($this->results)->getTabUrl($this->driver, 'HierarchyTree')?>?hierarchy=<?=urlencode($hierarchyID)?>#tabnav" title="<?=$this->transEsc('hierarchy_tree')?>" data-lightbox-href="<?=$this->recordLinker($this->results)->getTabUrl($this->driver, 'AjaxTab')?>?hierarchy=<?=urlencode($hierarchyID)?>" data-lightbox-post="tab=hierarchytree">
                              <?=$this->transEsc('hierarchy_view_context')?><?php if (count($trees) > 1): ?>: <?=$this->escapeHtml($hierarchyTitle)?><?php endif; ?>
                          </a>
                          <i class="result-link-icon fa fa-fw fa-sitemap" aria-hidden="true"></i>
                      </div>
                  <?php endforeach; ?>
              <?php endif; ?>

	<?php if ($displayRequestForm > 0) : ?>
		<br />
		<?php if ($orderText == 'Artikel')  : ?> 
	    		<a class="feedbackLink" data-lightbox href="<?=$this->url('feedback-home') ."?function=requestArticle&comments=Artikel: " .urlencode($articleTitle) .", " .urlencode($outText) ." (ID: " .$id .")"?>"><?=$this->transEsc('Request article')?></a> <i class="fa fa-newspaper" style="color:#44801e;" aria-hidden="true"></i>
		<?php endif; ?>
                <?php if ($orderText == 'Beitrag')  : ?>
                        <a class="feedbackLink" data-lightbox href="<?=$this->url('feedback-home') ."?function=requestPaper&comments=Beitrag: " .urlencode($articleTitle) .", " .urlencode($outText) ." (ID: " .$id .")"?>"><?=$this->transEsc('Request Paper in edited volumes')?></a> <i class="fa fa-newspaper" style="color:#44801e;" aria-hidden="true"></i>
                <?php endif; ?>

	<?php endif; ?>
        <?php // RKE @ in nächster Zeile ergänzt wegen Meldungen in Apache-Logfile ?>
        <?php if (strlen(trim($current['url'])) > 0) : ?>	
	<br />
	<form action="<?=$this->escapeHtmlAttr(trim($this->proxyUrl($current['url'])))?>" target="_blank">
			<?php if ($displayDownloadButton) : ?>
				<button type="submit" class="btn btn-lg" style="height:30px;width:130px; background-color:#ffce53;color:#5e6c78;"><i class="fa fa-download" aria-hidden="true"></i> - <?=$filetype?></button>
        		<?php endif; ?>
			<?php if(count($aLinks) > 0) : ?>
				<br />&nbsp;<br />
				<span style="font-size:80%; font-weight:bold";><strong><?= "Weitere Links s. Treffer" ?> </span>
			<?php endif ; ?>
			
	</form>
        <?php endif; ?>


              <?=$this->driver->supportsCoinsOpenUrl()?'<span class="Z3988" title="'.$this->escapeHtmlAttr($this->driver->getCoinsOpenUrl()).'"></span>':''?>
          </div>
      </div>
  </div>
  <?php if ($thumbnail && $thumbnailAlignment == 'right'): ?>
    <?=$thumbnail ?>
  <?php endif ?>
</div>
