PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
give / src / Campaigns / Models / Campaign.php

Campaign.php in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/Campaigns/Models/Campaign.php

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