262 lines
7.6 KiB
PHP
262 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Users\RoleEnum;
|
|
use App\Models\BrandContactList;
|
|
use App\Traits\Models\ActivityLog;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Uwil\LaravelCommon\Helpers\FileHelper;
|
|
|
|
/**
|
|
* @property integer $id Brand id.
|
|
* @property string $name Brand name.
|
|
* @property string $logo Brand logo.
|
|
* @property string $url Brand url link.
|
|
* @property string $url_claim Brand associative url claim.
|
|
* @property string $domain Brand domain.
|
|
* @property string $newsletter_email Brand associative newsletter email.
|
|
* @property-read \Illuminate\Support\Carbon $created_at Brand created date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Brand updated date.
|
|
*
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<App\Models\Service>
|
|
* $services Service HasMany relation.
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<App\Models\Question>
|
|
* $questions Question HasMany relation.
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<App\Models\CustomerInfo>
|
|
* $customerInfos CustomerInfo HasMany relation.
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<App\Models\ServicesStat>
|
|
* $servicesStats ServicesStat HasMany relation.
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<App\Models\SingleOptin>
|
|
* $singleOptins SingleOptin HasMany relation.
|
|
*/
|
|
class Brand extends Model
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'logo',
|
|
'url',
|
|
'url_claim',
|
|
'domain',
|
|
'newsletter_email',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
$model->logo = FileHelper::storeFile($model, $model->logo, true);
|
|
});
|
|
static::created(function () {
|
|
self::resetCache();
|
|
});
|
|
static::updating(function (self $model) {
|
|
$model->logo = FileHelper::storeFile($model, $model->logo, true);
|
|
});
|
|
static::updated(function (self $model) {
|
|
self::resetCache();
|
|
FileHelper::removeOldFile($model, 'logo');
|
|
});
|
|
static::deleted(function (self $model) {
|
|
self::resetCache();
|
|
FileHelper::removeOldFile($model, 'logo');
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Account attribute accessor.
|
|
*
|
|
* @return integer
|
|
*/
|
|
public function getAccountsAttribute(): int
|
|
{
|
|
return $this->customerInfos()->count();
|
|
}
|
|
|
|
/**
|
|
* Total optins attribute accessor.
|
|
*
|
|
* @return integer
|
|
*/
|
|
public function getTotalOptinsAttribute(): int
|
|
{
|
|
return $this->customerInfos()->where('opt_in', 1)->count() + $this->singleOptins()->count();
|
|
}
|
|
|
|
/**
|
|
* Get all brands associated to the auth user.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public static function getUserAssociatedBrands(): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
/** @var \App\Models\User $userModel */
|
|
$userModel = auth('backend')->user();
|
|
$brandModels = self::getBrands();
|
|
|
|
if ($userModel->role->equals(RoleEnum::marketing)) {
|
|
$brandIds = $userModel->brands->modelKeys();
|
|
|
|
$brandModels = $brandModels->filter(function ($brand) use ($brandIds) {
|
|
return in_array($brand->getKey(), $brandIds);
|
|
})->values();
|
|
}
|
|
|
|
return $brandModels;
|
|
}
|
|
|
|
/**
|
|
* Reset all cache elements related to this model.
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function resetCache(): void
|
|
{
|
|
Cache::forget('brands');
|
|
self::getBrands();
|
|
}
|
|
|
|
/**
|
|
* Get all brands.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public static function getBrands(): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
return Cache::remember('brands', 86400, function () {
|
|
return self::query()->orderBy('name', 'ASC')->get();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get brand by newsletter_email.
|
|
*
|
|
* @param string $newsletter_email
|
|
* @return Brand|null
|
|
*/
|
|
public static function getBrandByNewsletterEmail(string $newsletter_email): ?Brand
|
|
{
|
|
return Cache::remember("brand_{$newsletter_email}", 86400, function () use ($newsletter_email) {
|
|
return self::query()->where('newsletter_email', $newsletter_email)->first();
|
|
});
|
|
}
|
|
|
|
// * RELATIONS
|
|
|
|
/**
|
|
* Service has many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function services(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(Service::class);
|
|
}
|
|
|
|
/**
|
|
* Question has many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function questions(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(Question::class);
|
|
}
|
|
|
|
/**
|
|
* CustomerInfo has many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function customerInfos(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(CustomerInfo::class);
|
|
}
|
|
|
|
/**
|
|
* ServiceStat has many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function servicesStats(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(ServiceStat::class);
|
|
}
|
|
|
|
/**
|
|
* SingleOptin has many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function singleOptins(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(SingleOptin::class);
|
|
}
|
|
|
|
/**
|
|
* Users many to many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function users(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Brand::class, 'brand_users', 'brand_id', 'user_id');
|
|
}
|
|
|
|
/**
|
|
* Claim has many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function claims(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(Claim::class);
|
|
}
|
|
|
|
/**
|
|
* ClaimSubject has many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function claimSubjects(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(ClaimSubject::class);
|
|
}
|
|
|
|
/**
|
|
* ClaimMailTemplate has many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function claimMailTemplates(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(ClaimMailTemplate::class);
|
|
}
|
|
|
|
|
|
/**
|
|
* BrandContactList has many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function contactLists(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(BrandContactList::class);
|
|
}
|
|
}
|