File: /home/imensosw/www/mpl.imenso.co/app/Models/Video.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image as ImageLibrary;
class Video extends Model
{
public function images()
{
return $this->morphMany(\App\Models\Image::class, 'imageable');
}
public function videoable()
{
return $this->morphTo();
}
public function thumbnail()
{
return $this->image(5);
}
public function image($image_type_id = 0)
{
if ($this->images()->where('type_id', $image_type_id)->count() > 0) {
return $this->images()->where('type_id', $image_type_id)->first();
} else {
$image = new Image;
$image->src = ImageType::findOrFail($image_type_id)->default_image;
return $image;
}
}
public static function addNew($data)
{
$video = new self;
$video->name = $data->name;
$video->embed_code = $data->embed_code;
$video->status_id = $data->status_id;
$video->save();
if ($data->hasFile('thumbnail')) {
$video->addThumbnail($data->thumbnail);
}
return $video;
}
public function edit($data)
{
$this->name = $data->name;
$this->embed_code = $data->embed_code;
$this->status_id = $data->status_id;
$this->save();
if ($data->hasFile('thumbnail')) {
$this->addThumbnail($data->thumbnail);
}
}
public function addThumbnail($image)
{
$filename = $this->id.'-'.time().'-hero.'.$image->getClientOriginalExtension();
$web_location = 'video-thumbnails/'.$filename;
$processed_image = ImageLibrary::make($image->getRealPath());
$processed_image->resize(300, 160, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$save = $processed_image->save();
Storage::disk('s3')->put($web_location, $save->__toString());
$this->deleteImage(5);
$this->setImage(5, $web_location);
}
public function deleteImage($type_id)
{
if ($this->images()->where('type_id', $type_id)->count() > 0) {
$this->images()->where('type_id', $type_id)->first()->remove();
}
}
public function setImage($type_id, $location)
{
$image = new Image;
$image->src = $location;
$image->imageable_id = $this->id;
$image->imageable_type = self::class;
$image->type_id = $type_id;
$image->save();
}
public function assignToUser(User $user)
{
$this->videoable_id = $user->id;
$this->videoable_type = \App\Models\User::class;
$this->save();
}
public function assignToNews(News $news)
{
$this->videoable_id = $news->id;
$this->videoable_type = \App\Models\News::class;
$this->save();
}
public function getStatus()
{
if ($this->status_id == 1) {
return 'Draft';
} elseif ($this->status_id == 2) {
return 'Published';
}
}
public function remove()
{
$this->delete();
}
}