| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Models; |
| 4 |
|
| 5 |
use FluentCart\App\Services\PlanUpgradeService; |
| 6 |
use FluentCart\Framework\Database\Orm\Builder; |
| 7 |
|
| 8 |
/** |
| 9 |
* Meta Model - DB Model for Meta table |
| 10 |
* |
| 11 |
* Database Model |
| 12 |
* |
| 13 |
* @package FluentCart\App\Models |
| 14 |
* |
| 15 |
* @version 1.0.0 |
| 16 |
*/ |
| 17 |
class Meta extends Model |
| 18 |
{ |
| 19 |
protected $table = 'fct_meta'; |
| 20 |
|
| 21 |
protected $primaryKey = 'id'; |
| 22 |
|
| 23 |
protected $guarded = ['id']; |
| 24 |
|
| 25 |
protected $fillable = [ |
| 26 |
'object_type', |
| 27 |
'object_id', |
| 28 |
'meta_key', |
| 29 |
'meta_value', |
| 30 |
]; |
| 31 |
|
| 32 |
public function setMetaValueAttribute($meta_value) |
| 33 |
{ |
| 34 |
|
| 35 |
if (is_array($meta_value) || is_object($meta_value)) { |
| 36 |
$meta_value = json_encode($meta_value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
| 37 |
} |
| 38 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 39 |
$this->attributes['meta_value'] = $meta_value; |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
public function getMetaValueAttribute($value) |
| 44 |
{ |
| 45 |
if (is_string($value)) { |
| 46 |
$decoded = json_decode($value, true); |
| 47 |
return $decoded !== null ? $decoded : $value; |
| 48 |
} |
| 49 |
return $value; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function scopeUserTheme(Builder $query) |
| 55 |
{ |
| 56 |
return $query |
| 57 |
->where('object_type', User::class) |
| 58 |
->where('object_id', get_current_user_id()) |
| 59 |
->where('meta_key', 'theme'); |
| 60 |
} |
| 61 |
|
| 62 |
public function scopeUpgradeablePath($query, $productId) |
| 63 |
{ |
| 64 |
return $this->whereHas('upgradeableVariants', function ($query) use ($productId) { |
| 65 |
if (!empty($productId)) { |
| 66 |
return $query->where('post_id', $productId); |
| 67 |
} |
| 68 |
}) |
| 69 |
->where('object_type', PlanUpgradeService::$metaType) |
| 70 |
->where('meta_key', PlanUpgradeService::$metaKey); |
| 71 |
} |
| 72 |
|
| 73 |
public function upgradeableVariants() |
| 74 |
{ |
| 75 |
return $this->hasMany(ProductVariation::class, 'id', 'object_id'); |
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
|