<?php
namespace App\Controller;
use App\Repository\CalendarRepository;
use App\Repository\DataRepository;
use App\Repository\IndemaUsersRepository;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\Request\ParamFetcher;
use FOS\RestBundle\View\View;
use FOS\RestBundle\Controller\Annotations as Rest;
use Symfony\Component\HttpFoundation\Response;
class CalendarController extends AbstractFOSRestController
{
/**
* @var DataRepository
*/
private $dataRepository;
/**
* @var CalendarRepository
*/
private $calendarRepository;
/**
* @var IndemaUsersRepository
*/
private $usersRepository;
/**
* DataController constructor.
* @param DataRepository $dataRepository
* @param CalendarRepository $calendarRepository
* @param IndemaUsersRepository $usersRepository
*/
function __construct(DataRepository $dataRepository, CalendarRepository $calendarRepository, IndemaUsersRepository $usersRepository) {
$this->dataRepository = $dataRepository;
$this->calendarRepository = $calendarRepository;
$this->usersRepository = $usersRepository;
}
/**
* @Rest\Get("/calendar", name="calendar_index")
*/
public function index(): View
{
return $this->view(['results' => 'controller loaded'],Response::HTTP_OK);
}
/**
* @Rest\Get("/calendar/cron", name="calendar_cron")
*/
public function cronAction(): View
{
$this->calendarRepository->cronActions();
return $this->view(['results' => 'cron loaded'],Response::HTTP_OK);
}
/**
* @Rest\Get("/calendar/dates/{uid}/{args}", name="calendar_dates", defaults={"uid"=0,"args"=""})
*/
public function calendarDatesAction(int $uid, string $args): View
{
$defaultCalenderArgs = ['calendar' => 'userCalendar', 'uid' => 0];
$args = ['start' => date('Y-m-d 00:00:00', $_GET['start']),
'end' => date('Y-m-d 23:59:59', $_GET['end']),
'filter' => $_GET['filter'] ?? [],
'viewgroup' => $_GET['viewgroup'] ?? 'single',
'userType' => $_GET['ut'] ?? 'none',
'uid' => $_GET['u'] ?? 0,
];
if(isset($_GET['filter']['calenderEventData'] ) && !empty($_GET['filter']['calenderEventData'])) {
$tmp = explode('-', $_GET['filter']['calenderEventData']);
if(in_array($tmp[0], ['project']))
$defaultCalenderArgs = ['calendar' => $tmp[0], 'uid' => $tmp[1]];
}
switch($defaultCalenderArgs['calendar']) {
case 'project':
$events = $this->calendarRepository->loadProjectCalendarData($defaultCalenderArgs['uid'], $args);
$businessHours = [];
break;
default:
$events = $this->calendarRepository->loadCalendarData($uid, $args);
$businessHours = $this->calendarRepository->getUserBusinessHours($uid, $args);
break;
}
return $this->view(['events' => $events, 'businessHours' => $businessHours],Response::HTTP_OK);
}
/**
* @Rest\Post("/calender/date/update", name="calendar_date_update")
* @Rest\RequestParam(name="data", description="form data", nullable=false)
* @param ParamFetcher $paramFetcher
* @return View
*/
public function calendarUpdateAction(ParamFetcher $paramFetcher): View
{
$data = $paramFetcher->get('data');
if(is_string($data))
parse_str($data, $data);
if(isset($data['object_data']) && $data['object_data']['mainTable'] == 'calendar_dates' && isset($data['acceptInvitation'])) {
$this->calendarRepository->updateInvitationStatus($data);
// echo 'SPECIAL';die();
} elseif(isset($data['object_data']) && $data['object_data']['mainTable'] == 'calendar_dates') {
$this->calendarRepository->updateData($data);
} else {
$tmp = $data['object_data'];
$tmp['start'] = $data['main_data']['start'];
$tmp['end'] = $data['main_data']['end'] ?? $data['main_data']['start'];
$tmp['gmDate'] = false;
$this->calendarRepository->updateDateRangeData($tmp);
}
return $this->view(['data' => $data],Response::HTTP_OK);
}
/**
* @Rest\Post("/calender/date/updatedaterange", name="calendar_date_update_range")
* @Rest\RequestParam(name="data", description="form data", nullable=false)
* @param ParamFetcher $paramFetcher
* @return View
*/
public function calendarUpdateDateRangeAction(ParamFetcher $paramFetcher): View
{
$data = $paramFetcher->get('data');
if(is_string($data))
parse_str($data, $data);
$data['gmDate'] = true;
$this->calendarRepository->updateDateRangeData($data);
return $this->view(['data' => $data],Response::HTTP_OK);
}
/**
* @Rest\Post("/calender/addicaldata", name="calendar_addicaldata")
* @Rest\RequestParam(name="data", description="form data", nullable=false)
* @param ParamFetcher $paramFetcher
* @return View
*/
public function ticketAddImageAction(ParamFetcher $paramFetcher): View
{
$data = $paramFetcher->get('data');
if(isset($_POST['ref_uid']) && $_POST['ref_uid'] > 0)
$this->calendarRepository->iCalImport(['ref_table' => isset($_POST['ref_table']) && !empty($_POST['ref_table']) ? $_POST['ref_table'] : 'users', 'ref_uid' => $_POST['ref_uid']]);
return $this->view(['status' => 'success'],Response::HTTP_CREATED);
}
}