Campaign.php
205 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Campaigns\Models; |
| 4 | |
| 5 | use DateTime; |
| 6 | use Exception; |
| 7 | use Give\Campaigns\Actions\ConvertQueryDataToCampaign; |
| 8 | use Give\Campaigns\DataTransferObjects\CampaignGoalData; |
| 9 | use Give\Campaigns\Factories\CampaignFactory; |
| 10 | use Give\Campaigns\Repositories\CampaignPageRepository; |
| 11 | use Give\Campaigns\Repositories\CampaignRepository; |
| 12 | use Give\Campaigns\ValueObjects\CampaignGoalType; |
| 13 | use Give\Campaigns\ValueObjects\CampaignStatus; |
| 14 | use Give\Campaigns\ValueObjects\CampaignType; |
| 15 | use Give\DonationForms\V2\Models\DonationForm; |
| 16 | use Give\DonationForms\V2\Repositories\DonationFormsRepository; |
| 17 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 18 | use Give\Framework\Models\Contracts\ModelCrud; |
| 19 | use Give\Framework\Models\Contracts\ModelHasFactory; |
| 20 | use Give\Framework\Models\Model; |
| 21 | use Give\Framework\Models\ModelQueryBuilder; |
| 22 | use Give\Framework\QueryBuilder\JoinQueryBuilder; |
| 23 | |
| 24 | /** |
| 25 | * @since 4.0.0 |
| 26 | * |
| 27 | * @property int $id |
| 28 | * @property int $pageId |
| 29 | * @property int $defaultFormId |
| 30 | * @property CampaignType $type |
| 31 | * @property string $title |
| 32 | * @property string $url |
| 33 | * @property string $shortDescription |
| 34 | * @property string $longDescription |
| 35 | * @property string $logo |
| 36 | * @property string $image |
| 37 | * @property string $primaryColor |
| 38 | * @property string $secondaryColor |
| 39 | * @property int $goal |
| 40 | * @property CampaignGoalType $goalType |
| 41 | * @property CampaignStatus $status |
| 42 | * @property DateTime $startDate |
| 43 | * @property DateTime $endDate |
| 44 | * @property DateTime $createdAt |
| 45 | */ |
| 46 | class Campaign extends Model implements ModelCrud, ModelHasFactory |
| 47 | { |
| 48 | /** |
| 49 | * @inheritdoc |
| 50 | */ |
| 51 | protected $properties = [ |
| 52 | 'id' => 'int', |
| 53 | 'pageId' => 'int', |
| 54 | 'defaultFormId' => 'int', |
| 55 | 'type' => CampaignType::class, |
| 56 | 'title' => 'string', |
| 57 | 'shortDescription' => 'string', |
| 58 | 'longDescription' => 'string', |
| 59 | 'logo' => 'string', |
| 60 | 'image' => 'string', |
| 61 | 'primaryColor' => 'string', |
| 62 | 'secondaryColor' => 'string', |
| 63 | 'goal' => 'int', |
| 64 | 'goalType' => CampaignGoalType::class, |
| 65 | 'status' => CampaignStatus::class, |
| 66 | 'startDate' => DateTime::class, |
| 67 | 'endDate' => DateTime::class, |
| 68 | 'createdAt' => DateTime::class, |
| 69 | ]; |
| 70 | |
| 71 | /** |
| 72 | * @since 4.0.0 |
| 73 | */ |
| 74 | public function defaultForm(): ?DonationForm |
| 75 | { |
| 76 | return give(DonationFormsRepository::class)->getById($this->defaultFormId); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @since 4.0.0 |
| 81 | */ |
| 82 | public function forms(): ModelQueryBuilder |
| 83 | { |
| 84 | return DonationForm::query() |
| 85 | ->join(function (JoinQueryBuilder $builder) { |
| 86 | $builder |
| 87 | ->leftJoin('give_campaign_forms', 'campaign_forms') |
| 88 | ->on('campaign_forms.form_id', 'id'); |
| 89 | }) |
| 90 | ->where('campaign_forms.campaign_id', $this->id); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @since 4.0.0 |
| 95 | */ |
| 96 | public function page(): ?CampaignPage |
| 97 | { |
| 98 | return give(CampaignPageRepository::class)->findByCampaignId($this->id); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @since 4.0.0 |
| 103 | */ |
| 104 | public static function factory(): CampaignFactory |
| 105 | { |
| 106 | return new CampaignFactory(static::class); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Find campaign by ID |
| 111 | * |
| 112 | * @since 4.0.0 |
| 113 | */ |
| 114 | public static function find($id): ?Campaign |
| 115 | { |
| 116 | return give(CampaignRepository::class)->getById($id); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Find campaign by Form ID |
| 121 | * |
| 122 | * @since 4.0.0 |
| 123 | */ |
| 124 | public static function findByFormId(int $formId): ?Campaign |
| 125 | { |
| 126 | return give(CampaignRepository::class)->getByFormId($formId); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @since 4.0.0 |
| 131 | * |
| 132 | * @throws Exception |
| 133 | */ |
| 134 | public static function create(array $attributes): Campaign |
| 135 | { |
| 136 | $campaign = new static($attributes); |
| 137 | |
| 138 | give(CampaignRepository::class)->insert($campaign); |
| 139 | |
| 140 | return $campaign; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @since 4.0.0 |
| 145 | * |
| 146 | * @throws Exception|InvalidArgumentException |
| 147 | */ |
| 148 | public function save(): void |
| 149 | { |
| 150 | if ( ! $this->id) { |
| 151 | give(CampaignRepository::class)->insert($this); |
| 152 | } else { |
| 153 | give(CampaignRepository::class)->update($this); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @since 4.0.0 |
| 159 | * |
| 160 | * @throws Exception |
| 161 | */ |
| 162 | public function delete(): bool |
| 163 | { |
| 164 | return give(CampaignRepository::class)->delete($this); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @since 4.0.0 |
| 169 | * |
| 170 | * @throws Exception |
| 171 | */ |
| 172 | public function merge(Campaign ...$campaignsToMerge): bool |
| 173 | { |
| 174 | return give(CampaignRepository::class)->mergeCampaigns($this, ...$campaignsToMerge); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * @since 4.0.0 |
| 179 | */ |
| 180 | public function getGoalStats(): array |
| 181 | { |
| 182 | return (new CampaignGoalData($this))->toArray(); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * @since 4.0.0 |
| 187 | * |
| 188 | * @return ModelQueryBuilder<Campaign> |
| 189 | */ |
| 190 | public static function query(): ModelQueryBuilder |
| 191 | { |
| 192 | return give(CampaignRepository::class)->prepareQuery(); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * @since 4.0.0 |
| 197 | * |
| 198 | * @param object $object |
| 199 | */ |
| 200 | public static function fromQueryBuilderObject($object): Campaign |
| 201 | { |
| 202 | return (new ConvertQueryDataToCampaign())($object); |
| 203 | } |
| 204 | } |
| 205 |