|
|
<?php
namespace App;
use App\File; use App\SoftDeletes; use App\HiLib\Models\Model; use Illuminate\Validation\Rule; use Illuminate\Http\UploadedFile; use Spatie\MediaLibrary\HasMedia; use App\HiLib\Models\ReportableRelation; use Spatie\MediaLibrary\InteractsWithMedia; use Spatie\MediaLibrary\MediaCollections\Models\Media;
class Project extends Model implements HasMedia { use SoftDeletes, InteractsWithMedia;
const CONVERSION_NAME = 'avatar';
const COLLECTION_NAME = 'avatars';
public static $permissions = ['level'];
protected $table = 'projects';
protected $fillable = [ 'business_id', 'name', 'slug', 'private', 'budget', 'color', 'active', 'description', 'has_avatar', 'start', 'finish','members' ];
protected $reportable = [ 'business_id', 'name', 'slug', ['members' => 'project_user'] ];
protected $fillable_relations = ['members'];
public $detach_relation = false;
public function rules() { return [ 'name' => 'required|string|min:2|max:225', 'slug' => ['required', 'string', 'min:2', 'max:225', Rule::unique($this->table, 'slug') ->where('business_id', request('business_id')) ->whereNull('deleted_at') ->ignore($this->id)], 'order' => 'nullable|numeric|min:0', 'private' => 'nullable|boolean', 'color' => 'nullable|string|min:2|max:255', 'active' => 'nullable|boolean', 'description' => 'nullable|string|min:2|max:1000', // 'members' => empty($this->id) ? '' : 'required|array'
]; }
protected $casts = [ 'private' => 'boolean', 'start' => 'date', 'finish' => 'date', 'has_avatar' => 'boolean', ];
public function getValueOf(?string $key) { $values = [ 'business_id' => $this->business_id, 'project_id' => $this->id, 'sprint_id' => null, 'workflow_id' => null, 'status_id' => null, 'system_id' => null, 'actor_id' => auth()->id(), 'user_id' => null, 'task_id' => null, 'subject_id' => $this->id, ];
if ($key && isset($values, $key)) { return $values[$key]; }
return $values; } public function members() { $permissions = self::$permissions; return $this->belongsToMany( User::class, 'project_user', 'project_id', 'user_id', 'id', 'id', __FUNCTION__ )->using(ReportableRelation::class) ->withPivot($permissions); }
public function tasks() { return $this->hasMany(Task::class, 'project_id', 'id'); }
public function business() { return $this->belongsTo(Business::class, 'business_id', 'id'); }
public function systems() { return $this->hasMany(System::class, 'project_id', 'id'); }
public function sprints() { return $this->hasMany(Sprint::class, 'project_id', 'id'); }
public function files() { return $this->hasMany(File::class, 'user_id', 'id'); }
public function updateRelations() { // members
// if (!empty($this->filled_relations['members']) || $this->detach_relation) {
// $this->dirties['members'] = $this->members()->sync($this->filled_relations['members'], $this->detach_relation);
// }
}
public function reportActivity() { // foreach ($this->dirties as $name => $value) {
// return \post('task', 'task/v1/log', [
// 'user_id' => Auth::id(),
// 'business_id' => $this->business_id,
// 'loggable_id' => $this->id,
// 'loggable_type' => $this->getTypeId(),
// 'action' => $this->getAction(), // id of the action
// 'data' => [$name => [
// 'original' => $value['original'],
// 'diff' => $value['diff'],
// ]],
// ]);
// }
}
public function getPermissions($user_id) { return $this->members->where('id',$user_id)->first(); }
public function registerMediaCollections(): void { $this->addMediaCollection(static::COLLECTION_NAME) ->acceptsMimeTypes([ 'image/jpeg', 'image/png', 'image/tiff', 'image/gif', ]) ->useDisk('public') ->singleFile(); }
public function registerMediaConversions(Media $media = null): void { $this->addMediaConversion(static::CONVERSION_NAME) ->width(200) ->height(200) ->queued() ->nonOptimized() ->performOnCollections(static::COLLECTION_NAME); }
public function saveAsAvatar(UploadedFile $avatar): void { $this->addMedia($avatar)->toMediaCollection(static::COLLECTION_NAME); $this->update([ 'has_avatar' => true, ]); @unlink($this->getFirstMedia(static::COLLECTION_NAME)->getPath()); }
public function deleteAvatar(): void { $path = $this->getFirstMedia(static::COLLECTION_NAME)->getPath(); $this->getFirstMedia(static::COLLECTION_NAME)->delete(); $this->update([ 'has_avatar' => false, ]); @unlink($path); }
public function getAvatarUrl(): ?string { if ($url = $this->getFirstMediaUrl(static::COLLECTION_NAME, static::CONVERSION_NAME)) { return $url; }
return null; } }
|