<?php
namespace App\Model\DataObject;
use Pimcore\Model\DataObject\DemiDistrict;
use Pimcore\Model\DataObject\DemiFilterObject;
use Pimcore\Model\DataObject\DemiTown;
use Pimcore\Model\DataObject\Region;
use Pimcore\Model\WebsiteSetting;
class Community extends \Pimcore\Model\DataObject\Community
{
public function getRegion(): ?Region {
$parent = $this->getParent();
if($parent instanceof Region) {
return $parent;
}
$mainRegion = WebsiteSetting::getByName('mainRegion');
if($mainRegion && $mainRegion->getData() instanceof Region) {
return $mainRegion->getData();
}
return null;
}
public function getDemiTownOrDistrict(): null|DemiTown|DemiDistrict
{
$filterObject = $this->getDemiFilterObject();
if($filterObject instanceof DemiFilterObject) {
$filterElements = $filterObject->getFilterElements();
if(!empty($filterElements)) {
foreach ($filterElements as $filterElement) {
if ($filterElement instanceof DemiTown) {
return $filterElement;
}
if($filterElement instanceof DemiDistrict) {
return $filterElement;
}
}
}
}
return null;
}
}