/**
  * 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++;
         }
     }
 }