File: /home/xedaptot/be.naniguide.com/app/Model/FunnelProduct.php
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use App\Library\Traits\HasUid;
use App\Library\Contracts\TagResolverInterface;
use Validator;
class FunnelProduct extends Model implements TagResolverInterface
{
use HasUid;
public const STATUS_ACTIVE = 'active';
public const STATUS_INACTIVE = 'inactive';
protected $fillable = [
'name', 'description', 'price', 'compare_at_price',
'currency', 'image', 'sku', 'is_digital', 'settings', 'status',
];
protected $casts = [
'is_digital' => 'boolean',
'settings' => 'array',
'price' => 'decimal:2',
'compare_at_price' => 'decimal:2',
];
// -------------------------------------------------------------------------
// Relationships
// -------------------------------------------------------------------------
public function customer()
{
return $this->belongsTo(Customer::class);
}
public function funnels()
{
return $this->belongsToMany(Funnel::class, 'funnel_product_links')
->withPivot('sort_order', 'custom_price')
->withTimestamps();
}
// -------------------------------------------------------------------------
// Scopes
// -------------------------------------------------------------------------
public function scopeOfCustomer($query, $customer)
{
return $query->where('customer_id', $customer->id);
}
public function scopeActive($query)
{
return $query->where('status', self::STATUS_ACTIVE);
}
public function scopeSearch($query, $keyword)
{
if ($keyword) {
$query->where('name', 'like', '%' . trim($keyword) . '%');
}
}
// -------------------------------------------------------------------------
// TagResolverInterface
// -------------------------------------------------------------------------
public function getResolvedTags(): array
{
return [
'PRODUCT_NAME' => $this->name,
'PRODUCT_PRICE' => $this->price,
'PRODUCT_SKU' => $this->sku ?? '',
'PRODUCT_DESCRIPTION' => $this->description ?? '',
];
}
// -------------------------------------------------------------------------
// CRUD helpers
// -------------------------------------------------------------------------
public function fillParams($params)
{
$this->name = $params['name'] ?? $this->name;
$this->description = $params['description'] ?? $this->description;
$this->price = $params['price'] ?? $this->price;
$this->compare_at_price = $params['compare_at_price'] ?? $this->compare_at_price;
$this->currency = $params['currency'] ?? $this->currency;
$this->sku = $params['sku'] ?? $this->sku;
$this->is_digital = isset($params['is_digital']) ? (bool) $params['is_digital'] : $this->is_digital;
$this->status = $params['status'] ?? $this->status;
}
public function saveFromParams($params)
{
$this->fillParams($params);
$validator = Validator::make($params, [
'name' => 'required|max:255',
'price' => 'required|numeric|min:0',
]);
if ($validator->fails()) {
return $validator;
}
if (isset($params['image_file'])) {
$this->uploadImage($params['image_file']);
}
$this->save();
return $validator;
}
// -------------------------------------------------------------------------
// Image
// -------------------------------------------------------------------------
public function getImageUrl()
{
if ($this->image) {
return asset('storage/funnel_products/' . $this->image);
}
return url('images/placeholder.jpg');
}
public function uploadImage($file)
{
$path = storage_path('app/public/funnel_products/');
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
$filename = 'product-' . $this->uid . '.' . $file->getClientOriginalExtension();
$file->move($path, $filename);
$this->image = $filename;
}
// -------------------------------------------------------------------------
// Duplicate
// -------------------------------------------------------------------------
public function duplicate()
{
$copy = $this->replicate(['uid']);
$copy->name = $this->name . ' (Copy)';
$copy->save();
return $copy;
}
}