public function updateCMSFields(FieldList $fields)
 {
     Requirements::javascript('silverstripe-video-embed/assests/javascript/PhotoItemExtension.js');
     $typeField = new DropdownField('Type', 'Type', singleton('PhotoItem')->dbObject('Type')->enumValues());
     $fields->insertBefore($typeField, 'Photo');
     $videoField = new DropdownField("VideoItemID", "Video", VideoEmbed::get()->map("ID", "Title"));
     $fields->insertBefore($videoField, 'Caption');
 }
 public static function handle_shortcode($arguments, $url, $parser, $shortcode)
 {
     $result = false;
     if (Director::is_site_url($url) && VideoEmbed::GetByURL($url)) {
         $result = VideoEmbed::GetByURL($url)->forTemplate();
     } else {
         $result = parent::handle_shortcode($arguments, $url, $parser, $shortcode);
     }
     return $result;
 }
 /**
  * @example in template: $FlexSlider(2, 960, 450)
  */
 public function VideoEmbed($ID = 1, $width = null, $height = null, $autoplay = null)
 {
     /* @var $VideoEmbed VideoEmbed */
     $VideoEmbed = is_numeric($ID) ? VideoEmbed::get()->byID($ID) : VideoEmbed::get()->where("Title LIKE '" . $ID . "'")->First();
     if ($width) {
         $VideoEmbed->setWidth($width);
     }
     if ($height) {
         $VideoEmbed->setHeight($height);
     }
     if (!is_null($autoplay)) {
         $VideoEmbed->setAutoPlay($autoplay);
     }
     return $VideoEmbed;
 }
 public function viewfile($request)
 {
     $result = false;
     if ($origUrl = $request->getVar('FileURL')) {
         if (Director::is_site_url($origUrl) && VideoEmbed::GetByURL($origUrl)) {
             $video = VideoEmbed::GetByURL($origUrl);
             $result = $this->GetResultForVideo($video);
         }
     } else {
         if ($fileId = $request->getVar('ID')) {
             $video = VideoEmbed::get()->filter(array("HTML5VideoID" => $fileId))->first();
             $result = $this->GetResultForVideo($video);
         }
     }
     return $result ? $result : parent::viewfile($request);
 }
 public function GetOembedData(SS_HTTPRequest $request)
 {
     $response = "{}";
     $this->getResponse()->addHeader("Content-Type", "application/json; charset=utf-8");
     $url = $request->postVar('url') ? $request->postVar('url') : $request->getVar("mediaurl");
     if (Director::is_site_url($url) && VideoEmbed::GetByURL($url)) {
         $video = VideoEmbed::GetByURL($url);
         $response = $video->GetOembedJson();
     } else {
         $oembed = Oembed::get_oembed_from_url($url);
         if ($oembed && $oembed->exists()) {
             $response = $oembed->toJson();
         }
     }
     echo $response;
 }
 /**
  * Function to save news, photos and videos
  *
  * @param mixed $location_model
  * @param mixed $post
  *
  */
 public static function save_media($post, $incident)
 {
     $upload_dir = Kohana::config('upload.directory', TRUE);
     // Delete Previous Entries
     ORM::factory('media')->where('incident_id', $incident->id)->where('media_type <> 1')->delete_all();
     // a. News
     if (isset($post->incident_news)) {
         foreach ($post->incident_news as $item) {
             if (!empty($item)) {
                 $news = new Media_Model();
                 $news->location_id = $incident->location_id;
                 $news->incident_id = $incident->id;
                 $news->media_type = 4;
                 // News
                 $news->media_link = $item;
                 $news->media_date = date("Y-m-d H:i:s", time());
                 $news->save();
             }
         }
     }
     // b. Video
     if (isset($post->incident_video)) {
         $videoembed = new VideoEmbed();
         foreach ($post->incident_video as $k => $video_link) {
             if (!empty($video_link)) {
                 $video_thumb = $videoembed->thumbnail($video_link);
                 $new_filename = $incident->id . '_v' . $k . '_' . time();
                 $file_type = substr($video_thumb, strrpos($video_thumb, '.'));
                 $media_thumb = NULL;
                 $media_medium = NULL;
                 // Make sure file has an image extension
                 if ($video_thumb and in_array($file_type, array('.gif', '.jpg', '.png', '.jpeg'))) {
                     // Name the files for the DB
                     $media_link = $new_filename . $file_type;
                     $media_medium = $new_filename . '_m' . $file_type;
                     $media_thumb = $new_filename . '_t' . $file_type;
                     try {
                         if ($data = file_get_contents($video_thumb)) {
                             file_put_contents($upload_dir . $media_link, $data);
                         }
                     } catch (Exception $e) {
                     }
                     // IMAGE SIZES: 800X600, 400X300, 89X59
                     // Catch any errors from corrupt image files
                     try {
                         $image = Image::factory($upload_dir . $media_link);
                         // Medium size
                         if ($image->height > 300) {
                             Image::factory($upload_dir . $media_link)->resize(400, 300, Image::HEIGHT)->save($upload_dir . $media_medium);
                         } else {
                             // Cannot reuse the original image as it is deleted a bit further down
                             $image->save($upload_dir . $media_medium);
                         }
                         // Thumbnail
                         if ($image->height > 59) {
                             Image::factory($upload_dir . $media_link)->resize(89, 59, Image::HEIGHT)->save($upload_dir . $media_thumb);
                         } else {
                             // Reuse the medium image when it is small enough
                             $media_thumb = $media_medium;
                         }
                     } catch (Exception $e) {
                         // Do nothing. Too late to throw errors
                         // Set links to NULL
                         $media_medium = NULL;
                         $media_thumb = NULL;
                     }
                     // Okay, now we have these three different files on the server, now check to see
                     //   if we should be dropping them on the CDN
                     $local_directory = rtrim($upload_dir, '/') . '/';
                     if ($media_medium and $media_thumb and Kohana::config("cdn.cdn_store_dynamic_content")) {
                         $cdn_media_medium = cdn::upload($media_medium);
                         $cdn_media_thumb = cdn::upload($media_thumb);
                         // We no longer need the files we created on the server. Remove them.
                         if (file_exists($local_directory . $media_medium)) {
                             unlink($local_directory . $media_medium);
                         }
                         if (file_exists($local_directory . $media_thumb)) {
                             unlink($local_directory . $media_thumb);
                         }
                         $media_medium = $cdn_media_medium;
                         $media_thumb = $cdn_media_thumb;
                     }
                     if (file_exists($local_directory . $media_link)) {
                         // Remove original image
                         unlink($upload_dir . $media_link);
                     }
                 }
                 $video = new Media_Model();
                 $video->location_id = $incident->location_id;
                 $video->incident_id = $incident->id;
                 $video->media_type = 2;
                 // Video
                 $video->media_link = $video_link;
                 $video->media_thumb = $media_thumb;
                 $video->media_medium = $media_medium;
                 $video->media_date = date("Y-m-d H:i:s", time());
                 $video->save();
             }
         }
     }
     // c. Photos
     if (!empty($post->incident_photo)) {
         $filenames = upload::save('incident_photo');
         $i = 1;
         foreach ($filenames as $filename) {
             $new_filename = $incident->id . '_' . $i . '_' . time();
             //$file_type = substr($filename,-4);
             $file_type = "." . substr(strrchr($filename, '.'), 1);
             // replaces the commented line above to take care of images with .jpeg extension.
             // Name the files for the DB
             $media_link = $new_filename . $file_type;
             $media_medium = $new_filename . '_m' . $file_type;
             $media_thumb = $new_filename . '_t' . $file_type;
             // IMAGE SIZES: 800X600, 400X300, 89X59
             // Catch any errors from corrupt image files
             try {
                 $image = Image::factory($filename);
                 // Large size
                 if ($image->width > 800 || $image->height > 600) {
                     Image::factory($filename)->resize(800, 600, Image::AUTO)->save($upload_dir . $media_link);
                 } else {
                     $image->save($upload_dir . $media_link);
                 }
                 // Medium size
                 if ($image->height > 300) {
                     Image::factory($filename)->resize(400, 300, Image::HEIGHT)->save($upload_dir . $media_medium);
                 } else {
                     // Reuse the large image when it is small enough
                     $media_medium = $media_link;
                 }
                 // Thumbnail
                 if ($image->height > 59) {
                     Image::factory($filename)->resize(89, 59, Image::HEIGHT)->save($upload_dir . $media_thumb);
                 } else {
                     // Reuse the medium image when it is small enough
                     $media_thumb = $media_medium;
                 }
             } catch (Kohana_Exception $e) {
                 // Do nothing. Too late to throw errors
                 $media_link = NULL;
                 $media_medium = NULL;
                 $media_thumb = NULL;
             }
             // Okay, now we have these three different files on the server, now check to see
             //   if we should be dropping them on the CDN
             if (Kohana::config("cdn.cdn_store_dynamic_content")) {
                 $cdn_media_link = cdn::upload($media_link);
                 $cdn_media_medium = cdn::upload($media_medium);
                 $cdn_media_thumb = cdn::upload($media_thumb);
                 // We no longer need the files we created on the server. Remove them.
                 $local_directory = rtrim($upload_dir, '/') . '/';
                 if (file_exists($local_directory . $media_link)) {
                     unlink($local_directory . $media_link);
                 }
                 if (file_exists($local_directory . $media_medium)) {
                     unlink($local_directory . $media_medium);
                 }
                 if (file_exists($local_directory . $media_thumb)) {
                     unlink($local_directory . $media_thumb);
                 }
                 $media_link = $cdn_media_link;
                 $media_medium = $cdn_media_medium;
                 $media_thumb = $cdn_media_thumb;
             }
             // Remove the temporary file
             unlink($filename);
             // Save to DB
             $photo = new Media_Model();
             $photo->location_id = $incident->location_id;
             $photo->incident_id = $incident->id;
             $photo->media_type = 1;
             // Images
             $photo->media_link = $media_link;
             $photo->media_medium = $media_medium;
             $photo->media_thumb = $media_thumb;
             $photo->media_date = date("Y-m-d H:i:s", time());
             $photo->save();
             $i++;
         }
     }
 }
 /**
  *
  * @param string $url
  * @return VideoEmbed Description
  */
 public static function GetByURL($url)
 {
     $result = false;
     $url = preg_replace('/_resampled\\/[^-]+-/', '', Director::makeRelative($url));
     $file = File::get()->filter('Filename', $url)->first();
     if ($file && $file->exists()) {
         $result = VideoEmbed::get()->filter('HTML5VideoID', $file->ID)->first();
     }
     return $result && $result->exists() ? $result : false;
 }