| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Models; |
| 4 |
|
| 5 |
use FluentCart\App\Models\Concerns\CanSearch; |
| 6 |
use FluentCart\App\Services\FileSystem\DownloadService; |
| 7 |
|
| 8 |
/** |
| 9 |
* Product Download Model - DB Model for Product Downloads |
| 10 |
* |
| 11 |
* Database Model |
| 12 |
* |
| 13 |
* |
| 14 |
* @package FluentCart\App\Models |
| 15 |
* |
| 16 |
* @version 1.0.0 |
| 17 |
*/ |
| 18 |
class ProductDownload extends Model |
| 19 |
{ |
| 20 |
use CanSearch; |
| 21 |
|
| 22 |
protected $table = 'fct_product_downloads'; |
| 23 |
|
| 24 |
protected $fillable = [ |
| 25 |
'post_id', |
| 26 |
'product_variation_id', |
| 27 |
'download_identifier', |
| 28 |
'title', |
| 29 |
'type', |
| 30 |
'driver', |
| 31 |
'file_name', |
| 32 |
'file_path', |
| 33 |
'file_url', |
| 34 |
'file_size', // size in bytes |
| 35 |
'settings', |
| 36 |
'serial', |
| 37 |
]; |
| 38 |
|
| 39 |
|
| 40 |
public function setSettingsAttribute($settings) |
| 41 |
{ |
| 42 |
if (is_array($settings) || is_object($settings)) { |
| 43 |
$settings = json_encode($settings, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
| 44 |
} |
| 45 |
$this->attributes['settings'] = $settings; |
| 46 |
} |
| 47 |
|
| 48 |
public function getSettingsAttribute($settings) |
| 49 |
{ |
| 50 |
if (is_string($settings)) { |
| 51 |
$decoded = json_decode($settings, true); |
| 52 |
return $decoded ?: $settings; |
| 53 |
} |
| 54 |
return $settings; |
| 55 |
} |
| 56 |
|
| 57 |
public function setProductVariationIdAttribute($variations) |
| 58 |
{ |
| 59 |
if (is_array($variations)) { |
| 60 |
$value = json_encode($variations, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
| 61 |
} elseif (is_numeric($variations)) { |
| 62 |
$value = json_encode([(int)$variations], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
| 63 |
} else { |
| 64 |
$value = json_encode([], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
| 65 |
} |
| 66 |
|
| 67 |
$this->attributes['product_variation_id'] = $value; |
| 68 |
} |
| 69 |
|
| 70 |
public function getProductVariationIdAttribute($value) |
| 71 |
{ |
| 72 |
if (is_string($value)) { |
| 73 |
$decoded = json_decode($value, true); |
| 74 |
return $decoded ?: []; |
| 75 |
} |
| 76 |
return []; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* One2One: Dwonloadable Files belongs to one product |
| 81 |
* |
| 82 |
* @return \FluentCart\Framework\Database\Orm\Relations\BelongsTo |
| 83 |
*/ |
| 84 |
public function product() |
| 85 |
{ |
| 86 |
return $this->belongsTo(Product::class, 'post_id', 'ID'); |
| 87 |
} |
| 88 |
|
| 89 |
public function download_permissions() |
| 90 |
{ |
| 91 |
return $this->hasMany(OrderDownloadPermission::class, 'download_id', 'id'); |
| 92 |
} |
| 93 |
|
| 94 |
public function getSignedDownloadUrl(): string |
| 95 |
{ |
| 96 |
return DownloadService::getDownloadableUrlFromDownload($this->toArray()); |
| 97 |
} |
| 98 |
|
| 99 |
} |
| 100 |
|