<?php

/**
 * aMediaItem
 * 
 * This class has been auto-generated by the Doctrine ORM Framework
 * 
 * @package    asandbox
 * @subpackage model
 * @author     Your name here
 * @version    SVN: $Id: Builder.php 7021 2010-01-12 20:39:49Z lsmith $
 */
class aMediaItem extends PluginaMediaItem
{

	public function getEmbedCode($width, $height, $resizeType, $format = 'jpg', $absolute = false, $wmode = 'opaque')
 {
   if ($height === false)
   {
     // Scale the height. I had this backwards
     $height = floor(($width * $this->height / $this->width) + 0.5); 
   }

   // Accessible alt title
   $title = htmlspecialchars($this->getTitle());
   // It would be nice if partials could be used for this.
   // Think about whether that's possible.
   if ($this->getType() === 'video')
   {
     if ($this->embed)
     {
       // Solution for non-YouTube videos based on a manually
       // provided thumbnail and embed code
       return str_replace(array('_TITLE_', '_WIDTH_', '_HEIGHT_'),
         array($title, $width, $height), $this->embed);
     }
     // TODO: less YouTube-specific
     $serviceUrl = $this->getServiceUrl();
     $embeddedUrl = $this->youtubeUrlToEmbeddedUrl($serviceUrl);
     return <<<EOM
		<object alt="$title" width="$width" height="$height">
			<param name="movie" value="$embeddedUrl"></param>
			<param name="allowFullScreen" value="true"></param>
			<param name="allowscriptaccess" value="always"></param>
			<param name="wmode" value="$wmode"></param>
			<embed alt="$title" src="$embeddedUrl" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="$width" height="$height" wmode="$wmode"></embed>
		</object>
EOM
     ;
   }
   elseif (($this->getType() == 'image') || ($this->getType() == 'pdf'))
   {
		 $src = $this->getImageSrc($width, $height, $resizeType, $format, $absolute);
     // Use named routing rule to ensure the desired result (and for speed)
     return "<img alt=\"$title\" src='" . $src . "' />";
   }
   else
   {
     throw new Exception("Unknown media type in getEmbedCode: " . $this->getType() . " id is " . $this->id . " is new? " . $this->isNew());
   }
 }

	public function getImageSrc($width, $height, $resizeType, $format = 'jpg', $absolute = false)
	{
    $controller = sfContext::getInstance()->getController();
    $slug = $this->getSlug();
   	return $controller->genUrl("@a_media_image?" . http_build_query( array("slug" => $slug, "width" => $width, "height" => $height, "resizeType" => $resizeType, "format" => $format)), $absolute);
	}
}
