Skip to content

feat: sponsorship model update to support multiple tiers for sponsor #313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: feature/doctrine-mapping-upgrade
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct
$summit = $promo_code->getSummit();
$payload = [];
$sponsor = $promo_code->getSponsor();
$payload[IMailTemplatesConstants::sponsor_tier_name] = $sponsor->getSponsorship()->getType()->getName();
$payload[IMailTemplatesConstants::sponsor_tier_name] = implode(',', $sponsor->getSponsorshipTierNames());
$payload[IMailTemplatesConstants::promo_code] = $promo_code->getCode();
$payload[IMailTemplatesConstants::company_name] = '';
$company = $sponsor->getCompany();
Expand Down
4 changes: 4 additions & 0 deletions app/ModelSerializers/SerializerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@
use App\ModelSerializers\Summit\SummitScheduleConfigSerializer;
use App\ModelSerializers\Summit\SummitSchedulePreFilterElementConfigSerializer;
use App\ModelSerializers\Summit\SummitSignSerializer;
use App\ModelSerializers\Summit\SummitSponsorshipAddOnSerializer;
use App\ModelSerializers\Summit\SummitSponsorshipSerializer;
use App\ModelSerializers\Summit\SummitSponsorshipTypeSerializer;
use App\ModelSerializers\Summit\TrackTagGroups\TrackTagGroupAllowedTagSerializer;
use App\ModelSerializers\Summit\TrackTagGroups\TrackTagGroupSerializer;
Expand Down Expand Up @@ -566,6 +568,8 @@ private function __construct()
// summit sponsors

$this->registry['SponsorshipType'] = SponsorshipTypeSerializer::class;
$this->registry['SummitSponsorship'] = SummitSponsorshipSerializer::class;
$this->registry['SummitSponsorshipAddOn'] = SummitSponsorshipAddOnSerializer::class;
$this->registry['SummitSponsorshipType'] = SummitSponsorshipTypeSerializer::class;
$this->registry['Sponsor'] = SponsorSerializer::class;
$this->registry['SponsorAd'] = SponsorAdSerializer::class;
Expand Down
34 changes: 24 additions & 10 deletions app/ModelSerializers/Summit/SponsorSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ final class SponsorSerializer extends SilverStripeSerializer
protected static $allowed_relations = [
'extra_questions',
'members',
'sponsorships',
];

/**
Expand Down Expand Up @@ -80,6 +81,14 @@ public function serialize($expand = null, array $fields = [], array $relations =
$values['members'] = $members;
}

if (in_array('sponsorships', $relations)) {
$sponsorships = [];
foreach ($sponsor->getSponsorships() as $sponsorship) {
$sponsorships[] = $sponsorship->getId();
}
$values['sponsorships'] = $sponsorships;
}

if (!empty($expand)) {
$exp_expand = explode(',', $expand);
foreach ($exp_expand as $relation) {
Expand Down Expand Up @@ -163,17 +172,22 @@ public function serialize($expand = null, array $fields = [], array $relations =
}
}
break;
case 'sponsorship':
case 'sponsorships':
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we refactor this serializer to use
expand mappings ?
like here

{
if ($sponsor->hasSponsorship()) {
unset($values['sponsorship_id']);
$values['sponsorship'] = SerializerRegistry::getInstance()->getSerializer($sponsor->getSponsorship())->serialize
(
AbstractSerializer::filterExpandByPrefix($expand, $relation),
AbstractSerializer::filterFieldsByPrefix($fields, $relation),
AbstractSerializer::filterFieldsByPrefix($relations, $relation),
$params
);
$sponsorships = $sponsor->getSponsorships();
if (count($sponsorships) > 0) {
unset($values['sponsorships']);
$summit_sponsorships = [];
foreach ($sponsorships as $sponsorship) {
$summit_sponsorships[] = SerializerRegistry::getInstance()->getSerializer($sponsorship)->serialize
(
AbstractSerializer::filterExpandByPrefix($expand, $relation),
AbstractSerializer::filterFieldsByPrefix($fields, $relation),
AbstractSerializer::filterFieldsByPrefix($relations, $relation),
$params
);
}
$values['sponsorships'] = $summit_sponsorships;
}
}
break;
Expand Down
28 changes: 28 additions & 0 deletions app/ModelSerializers/Summit/SummitSponsorshipAddOnSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php namespace App\ModelSerializers\Summit;

use ModelSerializers\SilverStripeSerializer;

/**
* Copyright 2025 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

/**
* Class SummitSponsorshipAddOnSerializer
* @package ModelSerializers
*/
final class SummitSponsorshipAddOnSerializer extends SilverStripeSerializer
{
protected static $array_mappings = [
'Name' => 'name:json_string',
'Type' => 'type:json_string'
];
}
72 changes: 72 additions & 0 deletions app/ModelSerializers/Summit/SummitSponsorshipSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php namespace App\ModelSerializers\Summit;

use Libs\ModelSerializers\AbstractSerializer;
use Libs\ModelSerializers\Many2OneExpandSerializer;
use Libs\ModelSerializers\One2ManyExpandSerializer;
use models\summit\SummitSponsorship;
use ModelSerializers\SerializerRegistry;
use ModelSerializers\SilverStripeSerializer;

/**
* Copyright 2025 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

/**
* Class SummitSponsorshipSerializer
* @package ModelSerializers
*/
final class SummitSponsorshipSerializer extends SilverStripeSerializer
{
protected static $array_mappings = [
'TypeId' => 'type_id:json_int'
];

protected static $allowed_relations = [
'add_ons',
];

/**
* @param null $expand
* @param array $fields
* @param array $relations
* @param array $params
* @return array
*/
public function serialize($expand = null, array $fields = [], array $relations = [], array $params = [])
{
$sponsorship = $this->object;
if (!$sponsorship instanceof SummitSponsorship) return [];
$values = parent::serialize($expand, $fields, $relations, $params);

if (in_array('add_ons', $relations) && !isset($values['add_ons'])) {
$add_ons = [];
foreach ($sponsorship->getAddOns() as $add_on) {
$add_ons[] = $add_on->getId();
}
$values['add_ons'] = $add_ons;
}
return $values;
}

protected static $expand_mappings = [
'add_ons' => [
'type' => Many2OneExpandSerializer::class,
'getter' => 'getAddOns',
],
'type' => [
'type' => One2ManyExpandSerializer::class,
'original_attribute' => 'type_id',
'getter' => 'getType',
'has' => 'hasType'
],
];
}
5 changes: 2 additions & 3 deletions app/Models/Foundation/Summit/Factories/SponsorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/

use models\exceptions\ValidationException;
use models\summit\Sponsor;
/**
* Class SponsorFactory
Expand All @@ -36,9 +38,6 @@ public static function populate(Sponsor $sponsor, array $data):Sponsor{
if(isset($data['company']))
$sponsor->setCompany($data['company']);

if(isset($data['sponsorship']))
$sponsor->setSponsorship($data['sponsorship']);

if(isset($data['featured_event']))
$sponsor->setFeaturedEvent($data['featured_event']);

Expand Down
79 changes: 53 additions & 26 deletions app/Models/Foundation/Summit/Sponsor.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class Sponsor extends SilverstripeBaseModel implements IOrderable
'getCarouselAdvertiseImageId' => 'carousel_advertise_image',
'getFeaturedEventId' => 'featured_event',
'getCompanyId' => 'company',
'getSponsorshipId' => 'sponsorship',
];

protected $hasPropertyMappings = [
Expand All @@ -61,7 +60,6 @@ class Sponsor extends SilverstripeBaseModel implements IOrderable
'hasCarouselAdvertiseImage' => 'carousel_advertise_image',
'hasFeaturedEvent' => 'featured_event',
'hasCompany' => 'company',
'hasSponsorship' => 'sponsorship',
];

/**
Expand Down Expand Up @@ -90,13 +88,6 @@ class Sponsor extends SilverstripeBaseModel implements IOrderable
#[ORM\Column(name: 'IsPublished', type: 'boolean')]
protected $is_published;

/**
* @var SummitSponsorshipType
*/
#[ORM\JoinColumn(name: 'SummitSponsorshipTypeID', referencedColumnName: 'ID', onDelete: 'SET NULL')]
#[ORM\ManyToOne(targetEntity: \SummitSponsorshipType::class)]
protected $sponsorship;

/**
* @var SponsorUserInfoGrant[]
*/
Expand Down Expand Up @@ -230,6 +221,12 @@ class Sponsor extends SilverstripeBaseModel implements IOrderable
#[ORM\OneToOne(targetEntity: \models\summit\SummitLeadReportSetting::class, mappedBy: 'sponsor', cascade: ['persist', 'remove'], orphanRemoval: true, fetch: 'EXTRA_LAZY')]
private $lead_report_setting;

/**
* @var SummitSponsorship[]
*/
#[ORM\OneToMany(targetEntity: \models\summit\SummitSponsorship::class, mappedBy: 'sponsor', cascade: ['persist', 'remove'], orphanRemoval: true, fetch: 'EXTRA_LAZY')]
private $sponsorships;

/**
* Sponsor constructor.
*/
Expand All @@ -243,7 +240,8 @@ public function __construct()
$this->ads = new ArrayCollection();
$this->is_published = true;
$this->show_logo_in_event_page = true;
$this->extra_questions = new ArrayCollection;
$this->extra_questions = new ArrayCollection();
$this->sponsorships = new ArrayCollection();
}

public static function getAllowedQuestionTypes(): array
Expand Down Expand Up @@ -289,22 +287,6 @@ public function setCompany(Company $company): void
$this->company = $company;
}

/**
* @return SummitSponsorshipType
*/
public function getSponsorship(): ?SummitSponsorshipType
{
return $this->sponsorship;
}

/**
* @param SummitSponsorshipType $sponsorship
*/
public function setSponsorship(SummitSponsorshipType $sponsorship): void
{
$this->sponsorship = $sponsorship;
}

/**
* @return Member[]
*/
Expand Down Expand Up @@ -957,4 +939,49 @@ public function clearLeadReportSetting()
$this->lead_report_setting->clearSponsor();
$this->lead_report_setting = null;
}


/**
* @return SummitSponsorship[]
*/
public function getSponsorships()
{
return $this->sponsorships;
}

/**
* @param SummitSponsorship $sponsorship
* @throws ValidationException
*/
public function addSponsorship(SummitSponsorship $sponsorship): void
{
$sponsorship_type = $sponsorship->getType();
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->eq('type', $sponsorship_type));
if ($this->sponsorships->matching($criteria)->count() > 0) {
throw new ValidationException(
sprintf('Sponsor %s already has a sponsorship of the same type (%s).',
$this->id, $sponsorship_type->getId()));
}
$sponsorship->setSponsor($this);
$this->sponsorships->add($sponsorship);
}

/**
* @return void
*/
public function clearSponsorships(): void
{
if (is_null($this->sponsorships)) return;
$this->sponsorships->clear();
$this->sponsorships = null;
}

/**
* @return array
*/
public function getSponsorshipTierNames(): array
{
return array_map(fn($sponsorship) => $sponsorship->getType()->getType()->getName(), $this->sponsorships);
}
}
Loading