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