<?php
namespace App\Controller;
use Carbon\Carbon;
use FormBuilderBundle\Assembler\FormAssembler;
use FormBuilderBundle\Resolver\FormOptionsResolver;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\DataObject;
use Pimcore\Model\Asset;
use Pimcore\Model\Element\Tag;
use Pimcore\Model\DataObject\Service;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use App\Model\DataObject\News;
use App\Model\DataObject\Event;
use Symfony\Component\String\Slugger\AsciiSlugger;
class DefaultController extends FrontendController
{
/**
* @Template
* @param Request $request
* @return array
*/
public function defaultAction(Request $request)
{
return [];
}
/**
* @Route("/news/reader", name="news_reader")
* @Route("/news/reader/{requestDate}", name="news_reader_date")
* @Template(template="views/newsReader.html.twig")
*/
public function newsReaderAction(Request $request, $requestDate = '') {
$today = Carbon::now();
$dayStart = Carbon::parse(($requestDate ? $requestDate : 'today') . ' at 00:00:00');
$dayEnd = Carbon::parse(($requestDate ? $requestDate : 'today') . ' at 23:59:59');
$news = new DataObject\News\Listing();
$news->setOrderKey('articleDate');
$news->setOrder('desc');
$news->setCondition('o_published = 1 and (articleDate between :dateStart and :dateEnd)', [
'dateStart' => $dayStart->getTimestamp(),
'dateEnd' => $dayEnd->getTimestamp()
]);
$news->load();
$results = $news->getObjects();
$nextDay = $dayEnd->copy()->addMonth();
if ($nextDay->gt($today)) {
$nextDay = $today;
}
$prevDay = $dayStart->copy()->subMonth();
$newsDays = new DataObject\News\Listing();
$newsDays->setOrderKey('articleDate');
$newsDays->setOrder('desc');
$newsDays->setCondition('o_published = 1 and (articleDate between :dateStart and :dateEnd)', [
'dateStart' => $prevDay->getTimestamp(),
'dateEnd' => $nextDay->getTimestamp()
]);
// for loop from nextDayStart day to prevDayEnd day
$days = [];
foreach ($newsDays->load() as $newsDay) {
$day = intval($newsDay->getArticleDate()->format('Ymd'));
if (!isset($days[$day])) {
$days[$day] = [ 'count' => 0, 'date' => $newsDay->getArticleDate() ];
}
$days[$day]['count']++;
}
return [
'currentDate' => $dayStart,
'news' => $results,
'days' => $days
];
}
/**
* @Route("/news/{path}", name="news_detail")
* @Template(template="views/newsDetail.html.twig")
*/
public function newsDetailAction(Request $request, $path)
{
$listing = new DataObject\News\Listing();
$news = $listing->filterByKey($path);
if ($news->getCount() === 0) {
throw $this->createNotFoundException('nichts gefunden :(');
} else {
$dataObject = $news->getObjects()[0];
$tags = self::getTagsForElement('object', $dataObject->getId());
return [
'dataObject' => $news->getObjects()[0],
'tags' => $tags
];
}
}
/**
* @Route("/veranstaltungen/{path}", name="event_detail")
* @Template(template="views/eventDetail.html.twig")
*/
public function eventDetailAction(Request $request, $path)
{
$listing = new DataObject\Event\Listing();
$events = $listing->filterByKey($path);
if ($events->getCount() === 0) {
throw $this->createNotFoundException('nichts gefunden :(');
} else {
/** @var DataObject\Event $event */
$event = $events->getObjects()[0];
$otherDates = [];
$otherDates[] = [
"dateStart" => $event->getDateStart(),
"dateEnd" => $event->getDateEnd()
];
if ($event->getOtherDates()) {
/** @var DataObject\Fieldcollection\Data\EventDates $otherDate */
foreach ($event->getOtherDates() as $otherDate) {
$otherDates[] = [
"dateStart" => $otherDate->getDateStart(),
"dateEnd" => $otherDate->getDateEnd()
];
}
}
usort($otherDates, function ($a, $b): int {
return [$a['dateStart']] <=> [$b['dateStart']];
});
return [
'dataObject' => $event,
'otherDates' => $otherDates
];
}
}
/**
* @Route("/jobportal/anzeige/{customerPath}/{path}", name="job_offer_detail")
* @Template(template="views/jobportal/jobOfferDetail.html.twig")
*/
public function jobOfferDetailAction(Request $request, $customerPath, $path)
{
$listing = new DataObject\JobOffer\Listing();
$listing->filterByKey($path);
$jobCategory = null;
/** @var DataObject\JobOffer $jobOffer */
$jobOffer = null;
foreach ($listing->getObjects() as $offer){
if(!empty($offer->getJobCustomer()) && $offer->getJobCustomer()[0]->getKey() == $customerPath){
$jobOffer = $offer;
}
}
if ($jobOffer == null && !empty($listing->getObjects())) {
$jobOffer = $listing->getObjects()[0];
}
if ($jobOffer == null){
throw $this->createNotFoundException('nichts gefunden :(');
}
$jobOfferAdditionalData = null;
if($jobOffer->getJobCustomer()){
$jobOfferAdditionalData = $this->getAdditionalOffersData($jobOffer->getJobCustomer()[0], $jobOffer->getId());
}
return [
'dataObject' => $jobOffer,
'additionalJobOffers' => $jobOfferAdditionalData
];
}
/**
* @Route("/jobportal/firma/{path}", name="job_customer_detail")
* @Template(template="views/jobportal/jobCustomerDetail.html.twig")
*/
public function jobCustomerAction(Request $request, $path)
{
$listing = new DataObject\JobCustomer\Listing();
$jobCustomers = $listing->filterByKey($path);
/** @var DataObject\JobCustomer $jobCustomer */
$jobCustomer = $jobCustomers->getObjects()[0];
$jobOfferAdditionalData = $this->getAdditionalOffersData($jobCustomer);
return [
'dataObject' => $jobCustomer,
'additionalJobOffers' => $jobOfferAdditionalData,
];
}
/**
* @Route("/aktionen/aktion/{path}", name="action_detail")
* @Template(template="views/actionDetail.html.twig")
*/
public function actionDetailAction(Request $request, $path)
{
$listing = new DataObject\Action\Listing();
$actions = $listing->filterByKey($path);
return [
'dataObject' => $actions->getObjects()[0],
];
}
/**
* @Route("/themen", name="thema")
* @Template(template="views/thema.html.twig")
*/
public function themaAction(Request $request)
{
$bereichListing = new DataObject\ThemaBereich\Listing();
$bereiche = [];
$otherBereiche = [];
$folder = null;
/** @var DataObject\ThemaBereich $bereich */
foreach ($bereichListing->getObjects() as $bereich) {
if ($bereich->getParent()->getType() === 'folder') {
if (!$folder) {
$folder = $bereich->getParent();
}
$condition = [];
$conditionParams = [];
$condition[] = "articleDate < ?";
$conditionParams[] = Carbon::now()->getTimestamp();
$bereichIds = [$bereich->getId()];
/** @var DataObject\ThemaBereich $subBereich */
foreach ($bereich->getChildren() as $subBereich) {
$bereichIds[] = $subBereich->getId();
}
$bereichCondition = [];
foreach ($bereichIds as $bereichId) {
$bereichCondition[] = "bereiche LIKE ?";
$conditionParams[] = "%," . $bereichId . ",%";
}
$condition[] = '(' . join(' OR ', $bereichCondition) . ')';
$bereichListing = new DataObject\ThemaArtikel\Listing();
$bereichListing->setOrderKey('articleDate');
$bereichListing->setOrder('desc');
$bereichListing->setLimit(9);
$bereichListing->setCondition(join(' AND ', $condition), $conditionParams);
$articles = $bereichListing->getObjects();
$data = [
"order" => 0,
"bereich" => $bereich,
"articles" => $articles
];
$bereiche[] = $data;
}
}
// get folder sort order
$i = 0;
foreach ($folder->getChildren() as $child) {
$i++;
foreach ($bereiche as $index => $bereich) {
if ($bereich["bereich"]->getId() === $child->getId()) {
$bereiche[$index]["order"] = $i;
}
}
}
usort($bereiche, function ($a, $b) {
return $a['order'] - $b['order'];
});
// echo "<pre>";
// var_dump($bereiche);
// die();
return [
'error' => count($bereiche) === 0,
'bereiche' => $bereiche
];
}
/**
* @Route("/themen/{path}", name="thema_bereich")
* @Template(template="views/themaOverview.html.twig")
*/
public function themaOverviewAction(Request $request, $path)
{
$bereich = new DataObject\ThemaBereich\Listing();
$condition = [];
$conditionParams = [];
$listing = new DataObject\ThemaArtikel\Listing();
$listing->setOrderKey('articleDate');
$listing->setOrder('desc');
$condition[] = "articleDate < ?";
$conditionParams[] = Carbon::now()->getTimestamp();
$bereich->filterByKey($path);
$found = $bereich->getObjects();
$actions = [];
$otherBereiche = [];
if (count($found) > 0) {
// $condition[] = "bereiche in (:bereiche)";
// $conditionParams["bereiche"] = [$found[0]->getId()];
$listing->setCondition(join(' AND ', $condition), $conditionParams);
$actions = $listing->filterByBereiche($found[0]->getId())->getObjects();
foreach ($found[0]->getChildren() as $child) {
$condition = [];
$conditionParams = [];
$condition[] = "articleDate < ?";
$conditionParams[] = Carbon::now()->getTimestamp();
$bereichListing = new DataObject\ThemaArtikel\Listing();
$bereichListing->setOrderKey('articleDate');
$bereichListing->setOrder('desc');
$bereichListing->setLimit(3);
$bereichListing->setCondition(join(' AND ', $condition), $conditionParams);
$articles = $bereichListing->filterByBereiche($child->getId())->getObjects();
$data = [
"bereich" => $child,
"articles" => $articles
];
$otherBereiche[$child->getId()] = $data;
}
}
// echo "<pre>";
// var_dump($found[0]->getChildren());
// die();
return [
'error' => count($found) === 0,
'bereich' => count($found) > 0 ? $found[0] : '',
'bereiche' => $otherBereiche,
'articles' => $actions,
];
}
public function themaLatest(): Response
{
$listing = new DataObject\ThemaArtikel\Listing();
$listing->setOrderKey('articleDate');
$listing->setOrder('desc');
$listing->setLimit(5);
$condition = [];
$conditionParams = [];
$condition[] = "articleDate < ?";
$conditionParams[] = Carbon::now()->getTimestamp();
$listing->setCondition(join(' AND ', $condition), $conditionParams);
return $this->render('includes/themenLatest.html.twig', [
'articles' => $listing->load()
]);
}
/**
* @Route("/themen/artikel/{path}", name="thema_detail")
* @Template(template="views/themaDetail.html.twig")
*/
public function themaDetailAction(Request $request, $path)
{
$listing = new DataObject\ThemaArtikel\Listing();
$entries = $listing->filterByKey($path)->getObjects();
$entry = null;
$others = [];
$ids = [];
if ($request->get('pimcore_object_preview')) {
$entry = DataObject\ThemaArtikel::getById($request->get('pimcore_object_preview'), true);
} elseif (count($entries) > 0) {
$entry = $entries[0];
$ids[] = $entry->getId();
$otherListing = new DataObject\ThemaArtikel\Listing();
$otherListing->setCondition(join(' AND ', ['articleDate <= ?', 'o_id != ?']), [Carbon::now()->getTimestamp(), $entry->getId()]);
$otherListing->filterByBereiche($entry->getBereiche()[0]->getId());
$otherListing->setLimit(6);
$otherListing->setOrderKey('RAND()', false);
$others = $otherListing->getObjects();
if (count($others) < 6) {
foreach ($others as $other) {
$ids[] = $other->getId();
}
$additionalListing = new DataObject\ThemaArtikel\Listing();
$additionalListing->setCondition(join(' AND ', ['articleDate <= ?', 'o_id not in (?)']), [Carbon::now()->getTimestamp(), $ids]);
$additionalListing->setLimit(6 - count($others));
$additionalListing->setOrderKey('RAND()', false);
$others = array_merge($others, $additionalListing->getObjects());
}
}
return [
'dataObject' => $entry,
'others' => $others
];
}
/**
* @Route("/branchenverzeichnis/firma/{path}", name="branchen_customer_detail")
* @Template(template="views/branchenportal/branchenCustomerDetail.html.twig")
*/
public function branchenCustomerAction(Request $request, $path)
{
$listing = new DataObject\BranchenCustomer\Listing();
$branchenCustomers = $listing->filterByKey($path);
/** @var DataObject\BranchenCustomer $branchenCustomer */
$branchenCustomer = $branchenCustomers->getObjects()[0];
return [
'dataObject' => $branchenCustomer,
];
}
public function photoboothOverviewAction(Request $request) {
$event = $request->get('event', null);
$slugger = new AsciiSlugger();
$tomorrow = Carbon::parse('tomorrow');
$listing = new DataObject\Photobooth\Listing();
$listing->setOrderKey('eventDate');
$listing->setOrder('desc');
$entries = [];
$eventObject = null;
foreach ($listing as $entry) {
// get asset folder /Photobooth/{entry.key}
$images = [];
$folders = [$entry->getKey(), $slugger->slug($entry->getKey(), '.')];
foreach ($folders as $folderName) {
$folder = Asset\Folder::getByPath('/Photobooth/' . $folderName);
if ($folder) {
if ($folder->getChildren()) {
foreach ($folder->getChildren() as $image) {
$images[$image->getCreationDate()] = $image;
}
krsort($images);
}
$entries[] = [
'event' => $entry,
'hide' => $entry->getHide(),
'path' => '/Photobooth/' . $folderName,
'images' => $images
];
}
}
if ($entry->getKey() === $event) {
$eventObject = end($entries);
}
}
return $this->render('views/photobooth/overview.html.twig', [
'entries' => $entries,
'event' => $event,
'eventObject' => $eventObject
]);
}
/**
* @Route("/sport/{sportType}", name="sport_type_overview")
* @Template(template="views/sport/sportType.html.twig")
*/
public function sportTypeAction(Request $request, $sportType)
{
$listing = new DataObject\SportType\Listing();
$type = $listing->filterByKey($sportType);
/** @var DataObject\SportType $sportTypeObject */
$results = $type->getObjects();
$sportTypeObject = count($results) > 0 ? $results[0] : null;
return [
'dataObject' => $sportTypeObject,
];
}
/**
* @Route("/preview/app-banner/{bannerId}", name="app_banner_preview")
* @Template(template="views/preview/appBanner.html.twig")
*/
public function appBannerPreviewAction(Request $request, $bannerId)
{
$context = json_decode($request->get("context"), true);
$banner = Service::getElementFromSession('object', $bannerId);
return ['banner' => $banner];
}
/**
* @Route("/app-link/news/{newsId}", name="app_link_news")
*/
public function appLinkNewsAction(Request $request, $newsId)
{
// If the app is installed, this URL is linked directly into the app by the OS,
// if it is not, we redirect to the respective content on the web.
$article = DataObject\News::getById($newsId);
if ($article) {
return $this->redirectToRoute('news_detail', ['path' => $article->getKey()]);
}
return $this->redirect('https://boa.bayern');
}
/**
* @Route("/app-link/event/{eventId}", name="app_link_event")
*/
public function appLinkEventAction(Request $request, $newsId)
{
// If the app is installed, this URL is linked directly into the app by the OS,
// if it is not, we redirect to the respective content on the web.
$event = DataObject\Event::getById($newsId);
if ($event) {
return $this->redirectToRoute('event_detail', ['path' => $event->getKey()]);
}
return $this->redirect('https://boa.bayern');
}
/**
* @Route("/app-link/{path}", name="app_link_default", requirements={"path"=".+"}, priority=-1)
*/
public function appLinkDefaultAction(Request $request, $path)
{
// Fallback for any deeplinks we do not know
return $this->redirect('https://boa.bayern');
}
protected function getAdditionalOffersData($jobCustomer, $jobId = null){
$jobOfferData = [];
foreach ($jobCustomer->getJobOffers() as $offer){
$jobCategory = null;
$jobKey = 'firma';
if(!empty($offer->getJobCustomer())){
$jobKey = $offer->getJobCustomer()[0]->getKey();
}
$link = $this->generateUrl('job_offer_detail', ['customerPath' => $jobKey, 'path' => $offer->getKey()]);
$dateDiffer = abs($offer->getDatePublished()->getTimestamp() - time());
$days = round(($dateDiffer/ (60 * 60 * 24)));
if ($days == 1) {
$days = 'vor 1 Tag';
} elseif ($days == 0) {
$days = 'heute';
} else {
$days = 'vor '.$days.' Tagen';
}
$catData = [];
foreach ($offer->getJobCategories() as $cat){
$catData[] = [
"id" => $cat->getId(),
"title" => $cat->getTitle()
];
if($cat->getTitle() == "Ausbildung"){
$jobCategory = ["id" => $cat->getId(), "title" => $cat->getTitle()];
}
}
if($offer->getId() != $jobId){
$jobOfferData[] = [
"jobCustomer" => $offer->getJobCustomer(),
"jobCategory" => $jobCategory,
"jobLocation" => $offer->getJobLocation(),
"jobLocationTitle" => $offer->getJobLocation() ? $offer->getJobLocation()->getTitle() : '',
"id" => $offer->getId(),
"title" => $offer->getTitle(),
"description" => $offer->getDescription(),
//"image" => ($offer->getImage() ? $offer->getImage()->getThumbnail('website')->getHtml() : null),
"datePublished" => $offer->getDatePublished(),
"published" => $offer->getPublished(),
"big" => $offer->getBig(),
"jobCustomerLogo" => ($offer->getJobCustomer() ? $offer->getJobCustomer()[0]->getLogo()->getThumbnail()->getHtml() : null),
"jobCustomerKey" => ($offer->getJobCustomer() ? $offer->getJobCustomer()[0]->getKey() : null),
//"highlight" => $article->getHighlight(),
"creationDate" => date("c", $offer->getCreationDate()),
"modificationDate" => date("c", $offer->getModificationDate()),
"class" => 'md:w-6/12 lg:w-3/12',
"contentClass" => 'w-full',
/*"class" => ($offer->getImage() ? 'lg:w-6/12' : 'lg:w-3/12'),
"contentClass" => ($offer->getImage() ? 'w-6/12' : 'w-full'),*/
"key" => $offer->getKey(),
"link" => $link,
"days" => $days,
];
}
}
return $jobOfferData;
}
/**
* returns all assigned tags for element
*
* @param string $cType
* @param int $cId
* @return Tag[]
*/
public static function getTagsForElement($cType, $cId)
{
$tag = new Tag();
return $tag->getDao()->getTagsForElement($cType, $cId);
}
/**
* @Template(template="includes/tags.html.twig")
*/
public function renderTagsForElement(Request $request, $cType, $cId)
{
$tags = self::getTagsForElement($cType, $cId);
return [
'tags' => $tags
];
}
}