<?php
include_once getabspath("classes/controls/ViewImageDownloadField.php");
class ViewDatabaseImageField extends ViewImageDownloadField
{

	/**
	 * It returns pdfmake image notation { image: ..., width: ..., height: ... }
	*/
	public function getPdfValue(&$data, $keylink = "")
	{
		if( !$data[ $this->field ] )
			return '""';

		$imageType = SupposeImageType( $data[ $this->field ] );
		if( $imageType == "image/jpeg" || $imageType == "image/png" )
		{
			return '{
				image: "' . jsreplace( 'data:'. $imageType . ';base64,' . base64_bin2str( $data[ $this->field ] ) ) . '",
				width: ' . $this->container->pSet->getImageWidth( $this->field ) . ',
				height: ' .$this->container->pSet->getImageHeight( $this->field ) . '
			}';
		} else {
			$urls = $this->getFileURLs( $data, $keylink );
			if( $urls ) {
				return '{
					image: "' . jsreplace( $urls[0]["image"] ) . '",
					width: ' . $this->container->pSet->getImageWidth( $this->field ) . ',
					height: ' .$this->container->pSet->getImageHeight( $this->field ) . '
				}';
			}
		}

		return '""';
	}

	protected function getFileURLs( &$data, $keylink )
	{
		$fileURLs = array();
		if( !$data[ $this->field ] ) {
			return array();
		}
		$pSet = $this->pSettings();
		
		$fileName = 'file.jpg';
		$fileNameF = $pSet->getFilenameField($this->field);
		if( $fileNameF && $data[$fileNameF] )
			$fileName = $data[$fileNameF];

		$params = array();
		$params["filename"] = $fileName;
		$params["table"] = $pSet->table();
		$params["field"] = $this->field;
		$params["hash"] = fileAttrHash( $keylink, strlen_bin( $data[ $this->field ] ) );

		$url = array(
			"image" => GetTableLink("file", "", prepareUrlQuery( $params ).$keylink ),
			"filename" => $fileName
		);
		$thumbField = $pSet->getThumbnailField( $this->field );
		if( $thumbField && $this->showThumbnails ) {
			$params["thumbnail"] = 1;
			$params["hash"] = fileAttrHash( $keylink, strlen_bin( $data[ $thumbField ] ) );
			$url["thumbnail"] = GetTableLink("file", "", prepareUrlQuery( $params ).$keylink );
		}
		$fileURLs[] = $url;

		return $fileURLs;
	}

	/**
	 * @param &Array data
	 * @return String
	 */
	public function getTextValue(&$data)
	{
		if( !strlen( $data[ $this->field ] ) )
			return "";

		$fileNameField = $this->container->pSet->getFilenameField( $this->field );
		if( $fileNameField && $data[ $fileNameField ] )
			return $data[ $fileNameField ];

		return "<<Image>>";
	}

	/**
	 * Get the field's content that will be exported
	 * @prarm &Array data
	 * @prarm String keylink
	 * @return String
	 */
	public function getExportValue(&$data, $keylink = "", $html = false )
	{
		return mlang_message('LONG_BINARY');
	}

}
?>