| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\API\REST\V3\Routes\Campaigns\ViewModels; |
| 4 |
|
| 5 |
use Give\Campaigns\Models\Campaign; |
| 6 |
use Give\Campaigns\Repositories\CampaignsDataRepository; |
| 7 |
use Give\Campaigns\ValueObjects\CampaignPageMetaKeys; |
| 8 |
use Give\Framework\Database\DB; |
| 9 |
use Give\Framework\Support\Facades\DateTime\Temporal; |
| 10 |
|
| 11 |
/** |
| 12 |
* @since 4.0.0 |
| 13 |
*/ |
| 14 |
class CampaignViewModel |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @var Campaign |
| 18 |
*/ |
| 19 |
private $campaign; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var CampaignsDataRepository|null |
| 23 |
*/ |
| 24 |
private $data; |
| 25 |
|
| 26 |
/** |
| 27 |
* @since 4.0.0 |
| 28 |
*/ |
| 29 |
public function __construct(Campaign $campaign) |
| 30 |
{ |
| 31 |
$this->campaign = $campaign; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Set data source |
| 36 |
* |
| 37 |
* @param CampaignsDataRepository $data |
| 38 |
* |
| 39 |
* @return CampaignViewModel |
| 40 |
*/ |
| 41 |
public function setData(CampaignsDataRepository $data): CampaignViewModel |
| 42 |
{ |
| 43 |
$this->data = $data; |
| 44 |
|
| 45 |
return $this; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @since 4.0.0 |
| 50 |
*/ |
| 51 |
public function exports(): array |
| 52 |
{ |
| 53 |
$pagePermalink = $this->getPagePermalink(); |
| 54 |
|
| 55 |
return [ |
| 56 |
'id' => $this->campaign->id, |
| 57 |
'pageId' => $pagePermalink ? (int)$this->campaign->pageId : null, |
| 58 |
'pagePermalink' => $pagePermalink, |
| 59 |
'defaultFormId' => $this->campaign->defaultFormId, |
| 60 |
'defaultFormTitle' => $this->campaign->defaultForm()->title, |
| 61 |
'type' => $this->campaign->type->getValue(), |
| 62 |
'title' => $this->campaign->title, |
| 63 |
'shortDescription' => $this->campaign->shortDescription, |
| 64 |
'longDescription' => $this->campaign->longDescription, |
| 65 |
'logo' => $this->campaign->logo, |
| 66 |
'image' => $this->campaign->image, |
| 67 |
'primaryColor' => $this->campaign->primaryColor, |
| 68 |
'secondaryColor' => $this->campaign->secondaryColor, |
| 69 |
'goal' => $this->campaign->goal, |
| 70 |
'goalType' => $this->campaign->goalType->getValue(), |
| 71 |
'goalStats' => is_null($this->data) |
| 72 |
? $this->campaign->getGoalStats() |
| 73 |
: $this->data->getGoalData($this->campaign), |
| 74 |
'status' => $this->campaign->status->getValue(), |
| 75 |
'startDate' => Temporal::getFormattedDateTimeUsingTimeZoneAndFormatSettings($this->campaign->startDate), |
| 76 |
'endDate' => $this->campaign->endDate |
| 77 |
? Temporal::getFormattedDateTimeUsingTimeZoneAndFormatSettings($this->campaign->endDate) |
| 78 |
: null, |
| 79 |
'createdAt' => Temporal::getFormattedDateTimeUsingTimeZoneAndFormatSettings($this->campaign->createdAt), |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @since 4.0.0 |
| 85 |
*/ |
| 86 |
protected function getPagePermalink(): ?string |
| 87 |
{ |
| 88 |
$page = get_post($this->campaign->pageId); |
| 89 |
if (!$page){ |
| 90 |
return null; |
| 91 |
} |
| 92 |
|
| 93 |
if ($page->post_status === 'trash') { |
| 94 |
return null; |
| 95 |
} |
| 96 |
|
| 97 |
$permalink = get_permalink($this->campaign->pageId ?? 0); |
| 98 |
|
| 99 |
if ($permalink){ |
| 100 |
return $permalink; |
| 101 |
} |
| 102 |
|
| 103 |
$query = DB::table('postmeta') |
| 104 |
->select('post_id') |
| 105 |
->where('meta_key', CampaignPageMetaKeys::CAMPAIGN_ID) |
| 106 |
->where('meta_value', $this->campaign->id) |
| 107 |
->get(); |
| 108 |
|
| 109 |
if (!$query) { |
| 110 |
return null; |
| 111 |
} |
| 112 |
|
| 113 |
return get_permalink($query->post_id); |
| 114 |
} |
| 115 |
} |
| 116 |
|