CampaignPage.php
119 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Campaigns\Models; |
| 4 | |
| 5 | use DateTime; |
| 6 | use Give\Campaigns\Repositories\CampaignPageRepository; |
| 7 | use Give\Campaigns\ValueObjects\CampaignPageStatus; |
| 8 | use Give\Framework\Models\Contracts\ModelCrud; |
| 9 | use Give\Framework\Models\Model; |
| 10 | use Give\Framework\Models\ModelQueryBuilder; |
| 11 | use Give\Framework\Models\ValueObjects\Relationship; |
| 12 | use Give\Framework\Support\Facades\DateTime\Temporal; |
| 13 | |
| 14 | /** |
| 15 | * @since 4.0.0 |
| 16 | * |
| 17 | * @property int $id |
| 18 | * @property int $campaignId |
| 19 | * @property DateTime $createdAt |
| 20 | * @property DateTime $updatedAt |
| 21 | * @property CampaignPageStatus $status |
| 22 | */ |
| 23 | class CampaignPage extends Model implements ModelCrud |
| 24 | { |
| 25 | public $properties = [ |
| 26 | 'id' => 'int', |
| 27 | 'campaignId' => 'int', |
| 28 | 'createdAt' => DateTime::class, |
| 29 | 'updatedAt' => DateTime::class, |
| 30 | 'status' => CampaignPageStatus::class, |
| 31 | ]; |
| 32 | |
| 33 | public $relationships = [ |
| 34 | 'campaign' => Relationship::BELONGS_TO, |
| 35 | ]; |
| 36 | |
| 37 | /** |
| 38 | * @since 4.0.0 |
| 39 | */ |
| 40 | public function getEditLinkUrl(): string |
| 41 | { |
| 42 | // By default, the URL is encoded for display purposes. |
| 43 | // Setting any other value prevents encoding the URL. |
| 44 | return get_edit_post_link($this->id, 'redirect'); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @since 4.0.0 |
| 49 | */ |
| 50 | public function campaign(): ?Campaign |
| 51 | { |
| 52 | return Campaign::find($this->campaignId); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @since 4.0.0 |
| 57 | */ |
| 58 | public static function find($id): ?CampaignPage |
| 59 | { |
| 60 | return give(CampaignPageRepository::class)->getById($id); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @since 4.0.0 |
| 65 | */ |
| 66 | public static function create(array $attributes): CampaignPage |
| 67 | { |
| 68 | $campaignPage = new static($attributes); |
| 69 | |
| 70 | give(CampaignPageRepository::class)->insert($campaignPage); |
| 71 | |
| 72 | return $campaignPage; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @since 4.0.0 |
| 77 | */ |
| 78 | public function save(): void |
| 79 | { |
| 80 | if (!$this->id) { |
| 81 | give(CampaignPageRepository::class)->insert($this); |
| 82 | } else { |
| 83 | give(CampaignPageRepository::class)->update($this); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @since 4.0.0 |
| 89 | */ |
| 90 | public function delete(): bool |
| 91 | { |
| 92 | return give(CampaignPageRepository::class)->delete($this); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @since 4.0.0 |
| 97 | * |
| 98 | * @return ModelQueryBuilder<CampaignPage> |
| 99 | */ |
| 100 | public static function query(): ModelQueryBuilder |
| 101 | { |
| 102 | return give(CampaignPageRepository::class)->prepareQuery(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @since 4.0.0 |
| 107 | */ |
| 108 | public static function fromQueryBuilderObject($object): CampaignPage |
| 109 | { |
| 110 | return new CampaignPage([ |
| 111 | 'id' => (int) $object->id, |
| 112 | 'campaignId' => (int) $object->campaignId, |
| 113 | 'createdAt' => Temporal::toDateTime($object->createdAt), |
| 114 | 'updatedAt' => Temporal::toDateTime($object->updatedAt), |
| 115 | 'status' => new CampaignPageStatus($object->status), |
| 116 | ]); |
| 117 | } |
| 118 | } |
| 119 |