Skip to content
Snippets Groups Projects
Commit 5d69b2c3 authored by David Siegfried's avatar David Siegfried Committed by Jan-Hendrik Willms
Browse files

move rest-api routes to trails controller, closes #3791

Closes #3791

Merge request studip/studip!2665
parent f20f8f19
No related branches found
No related tags found
No related merge requests found
<?php
/**
* ajax.php - contains Resources_AjaxController
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author David Siegfried <ds.siegfried@gmail.com>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
*/
class Resources_AjaxController extends AuthenticatedController
{
public function toggle_marked_action($request_id)
{
$request = \ResourceRequest::find($request_id);
if (!$request) {
throw new Exception('Resource request object not found!');
}
$current_user = \User::findCurrent();
if ($request->isReadOnlyForUser($current_user)) {
throw new \AccessDeniedException();
}
//Switch to the next marking state or return to the unmarked state
//if the next marking state would be after the last defined
//marking state.
$request->marked = ($request->marked + 1) % \ResourceRequest::MARKING_STATES;
$request->store();
$this->render_json($request->toArray());
}
public function get_resource_booking_intervals_action($booking_id)
{
$booking = \ResourceBooking::find($booking_id);
if (!$booking) {
throw new Exception('Resource booking object not found!');
}
$resource = $booking->resource->getDerivedClassInstance();
if (!$resource->bookingPlanVisibleForUser(\User::findCurrent())) {
throw new \AccessDeniedException();
}
//Get begin and end:
$begin_str = \Request::get('begin');
$end_str = \Request::get('end');
$begin = null;
$end = null;
if ($begin_str && $end_str) {
//Try the ISO format first: YYYY-MM-DDTHH:MM:SS±ZZ:ZZ
$begin = \DateTime::createFromFormat(\DateTime::RFC3339, $begin_str);
$end = \DateTime::createFromFormat(\DateTime::RFC3339, $end_str);
if (!($begin instanceof \DateTime) || !($end instanceof \DateTime)) {
$tz = new \DateTime();
$tz = $tz->getTimezone();
//Try the ISO format without timezone:
$begin = \DateTime::createFromFormat('Y-m-d\TH:i:s', $begin_str, $tz);
$end = \DateTime::createFromFormat('Y-m-d\TH:i:s', $end_str, $tz);
}
}
$sql = "booking_id = :booking_id ";
$sql_data = ['booking_id' => $booking->id];
if ($begin instanceof \DateTime && $end instanceof \DateTime) {
$sql .= "AND begin >= :begin AND end <= :end ";
$sql_data['begin'] = $begin->getTimestamp();
$sql_data['end'] = $end->getTimestamp();
}
if (\Request::submitted('exclude_cancelled_intervals')) {
$sql .= "AND takes_place = '1' ";
}
$sql .= "ORDER BY begin ASC, end ASC";
$intervals = \ResourceBookingInterval::findBySql($sql, $sql_data);
$result = [];
foreach ($intervals as $interval) {
$result[] = $interval->toRawArray();
}
$this->render_json($result);
}
public function toggle_takes_place_field_action($interval_id)
{
$interval = \ResourceBookingInterval::find($interval_id);
if (!$interval) {
throw new Exception('ResourceBookingInterval object not found!');
}
//Get the resource and check the permissions of the user:
$resource = $interval->resource;
if (!$resource) {
throw new Exception('ResourceBookingInterval not linked with a resource!');
}
$resource = $resource->getDerivedClassInstance();
if (!$resource->userHasPermission(\User::findCurrent(), 'autor', [$interval->begin, $interval->end])) {
throw new Exception('You do not have sufficient permissions to modify the interval!');
}
if (
!$interval->takes_place
&& $resource->isAssigned(new \DateTime('@' . $interval->begin), new \DateTime('@' . $interval->end))
) {
throw new Exception('Already booked');
}
//Switch the takes_place field:
$interval->takes_place = $interval->takes_place ? '0' : '1';
if ($interval->store()) {
$this->render_json([
'takes_place' => $interval->takes_place
]);
} else {
throw new Exception('Error while storing the interval!');
}
}
public function get_semester_booking_plan_action($resource_id)
{
$resource = \Resource::find($resource_id);
if (!$resource) {
throw new Exception('Resource object not found!');
}
$resource = $resource->getDerivedClassInstance();
$current_user = User::findCurrent();
if (!$resource->bookingPlanVisibleForUser($current_user)) {
throw new AccessDeniedException();
}
$display_requests = Request::get('display_requests');
$display_all_requests = Request::get('display_all_requests');
$begin = new \DateTime();
$end = new \DateTime();
$semester_id = Request::get('semester_id');
$semester = $semester_id ? Semester::find($semester_id) : Semester::findCurrent();
if (!$semester) {
throw new Exception('No semester found!');
}
if (Request::get('semester_timerange') !== 'fullsem') {
$begin->setTimestamp($semester->vorles_beginn);
$end->setTimestamp($semester->vorles_ende);
} else {
$begin->setTimestamp($semester->beginn);
$end->setTimestamp($semester->ende);
}
//Get parameters:
$booking_types = Request::getArray('booking_types');
$begin_timestamp = $begin->getTimestamp();
$end_timestamp = $end->getTimestamp();
//Get the event data sources:
$bookings = ResourceBooking::findByResourceAndTimeRanges(
$resource,
[
[
'begin' => $begin_timestamp,
'end' => $end_timestamp
]
],
$booking_types
);
$requests = [];
if ($display_all_requests || $display_requests) {
$requests_sql = "JOIN seminar_cycle_dates AS scd USING (metadate_id)
WHERE resource_id = :resource_id
AND closed = 0";
$requests_sql_params = [
'begin' => $begin_timestamp,
'end' => $end_timestamp,
'resource_id' => $resource->id
];
if (!$display_all_requests) {
$requests_sql .= "AND user_id = :user_id ";
$requests_sql_params['user_id'] = $current_user->id;
}
$requests = \ResourceRequest::findBySql(
$requests_sql,
$requests_sql_params
);
}
$merged_objects = [];
$meta_dates = [];
foreach ($bookings as $booking) {
$booking->resource = $resource;
$irrelevant_booking = $booking->getRepetitionType() !== 'weekly'
&& (
!\Request::get('display_single_bookings')
|| $booking->end < strtotime('today')
);
if ($booking->getAssignedUserType() === 'course' && in_array($booking->assigned_course_date->metadate_id, $meta_dates)) {
$irrelevant_booking = true;
};
if (!$irrelevant_booking) {
//It is an booking with repetitions that has to be included
//in the semester plan.
if (in_array($booking->getRepetitionType(), ['single', 'weekly'])) {
$event_list = $booking->convertToEventData(
[
ResourceBookingInterval::build(
[
'interval_id' => md5(uniqid()),
'begin' => $booking->begin - $booking->preparation_time,
'end' => $booking->end
]
)
],
$current_user
);
} else {
$event_list = $booking->getFilteredEventData(null, null, null, strtotime('today'), $end_timestamp);
}
foreach ($event_list as $event_data) {
if ($booking->getAssignedUserType() === 'course' && $booking->assigned_course_date->metadate_id) {
$index = sprintf(
'%s_%s_%s',
$booking->assigned_course_date->metadate_id,
$event_data->begin->format('NHis'),
$event_data->end->format('NHis')
);
$meta_dates[] = $booking->assigned_course_date->metadate_id;
} else {
$index = sprintf(
'%s_%s_%s',
$booking->id,
$event_data->begin->format('NHis'),
$event_data->end->format('NHis')
);
}
//Strip some data that cannot be used effectively in here:
$event_data->api_urls = [];
$event_data->editable = false;
$merged_objects[$index] = $event_data;
}
}
}
$relevant_request = false;
foreach ($requests as $request) {
if ($request->cycle instanceof \SeminarCycleDate) {
$cycle_dates = $request->cycle->getAllDates();
foreach ($cycle_dates as $cycle_date) {
$relevant_request = $semester->beginn <= $cycle_date->date
&& $semester->ende >= $cycle_date->date;
if ($relevant_request) {
//We have found a date for the current semester
//that makes the request relevant.
break;
}
}
if (!$relevant_request) {
continue;
}
$event_data_list = $request->getFilteredEventData(
$current_user->id
);
foreach ($event_data_list as $event_data) {
$index = sprintf(
'%s_%s_%s',
$request->metadate_id,
$event_data->begin->format('NHis'),
$event_data->end->format('NHis')
);
//Strip some data that cannot be used effectively in here:
$event_data->view_urls = [];
$event_data->api_urls = [];
$merged_objects[$index] = $event_data;
}
}
}
//Convert the merged events to Fullcalendar events:
$data = [];
foreach ($merged_objects as $obj) {
$data[] = $obj->toFullCalendarEvent();
}
$this->render_json($data);
}
public function get_booking_plan_action($resource_id)
{
$resource = Resource::find($resource_id);
if (!$resource) {
throw new Exception('Resource object not found!');
}
$resource = $resource->getDerivedClassInstance();
$current_user = User::findCurrent();
$nobody_access = true;
if ($current_user instanceof User) {
$nobody_access = false;
if (!$resource->bookingPlanVisibleForUser($current_user)) {
throw new AccessDeniedException();
}
} else if ($resource instanceof Room) {
if (!$resource->bookingPlanVisibleForUser($current_user)) {
throw new AccessDeniedException();
}
}
$user_is_resource_user = $current_user && $resource->userHasPermission($current_user);
$display_requests = $current_user && Request::bool('display_requests');
$display_all_requests = Request::bool('display_all_requests');
if ($display_all_requests && !$user_is_resource_user) {
//The user is not allowed to see all requests.
throw new AccessDeniedException();
}
$begin_date = Request::get('start');
$end_date = Request::get('end');
if (!$begin_date || !$end_date) {
//No time range specified.
throw new Exception('The parameters "start" and "end" are missing!');
}
$begin = DateTime::createFromFormat(DateTime::RFC3339, $begin_date);
$end = DateTime::createFromFormat(DateTime::RFC3339, $end_date);
if (!($begin instanceof DateTime) || !($end instanceof DateTime)) {
$begin = new DateTime();
$end = new DateTime();
//Assume the local timezone and use the Y-m-d format:
$date_regex = '/[0-9]{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])/';
if (preg_match($date_regex, $begin_date)) {
//$begin is specified in the date format YYYY-MM-DD:
$begin_str = explode('-', $begin_date);
$begin->setDate(
intval($begin_str[0]),
intval($begin_str[1]),
intval($begin_str[2])
);
$begin->setTime(0, 0, 0);
} else {
$begin->setTimestamp($begin_date);
}
//Now we do the same for $end_timestamp:
if (preg_match($date_regex, $end_date)) {
//$begin is specified in the date formay YYYY-MM-DD:
$end_str = explode('-', $end_date);
$end->setDate(
intval($end_str[0]),
intval($end_str[1]),
intval($end_str[2])
);
$end->setTime(23, 59, 59);
} else {
$end->setTimestamp($end_date);
}
}
//Get parameters:
$booking_types = [];
if (!$nobody_access) {
$booking_types = explode(',', Request::get('booking_types'));
}
$begin_timestamp = $begin->getTimestamp();
$end_timestamp = $end->getTimestamp();
//Get the event data sources:
$bookings = ResourceBooking::findByResourceAndTimeRanges(
$resource,
[
[
'begin' => $begin_timestamp,
'end' => $end_timestamp
]
],
$booking_types
);
$requests = [];
if ($display_all_requests) {
$requests = ResourceRequest::findByResourceAndTimeRanges(
$resource,
[
[
'begin' => $begin_timestamp,
'end' => $end_timestamp
]
],
0
);
} else if ($display_requests) {
//Get the users own request only:
$requests = ResourceRequest::findByResourceAndTimeRanges(
$resource,
[
[
'begin' => $begin_timestamp,
'end' => $end_timestamp
]
],
0,
[],
'user_id = :user_id',
['user_id' => $current_user->id]
);
}
$objects = array_merge($bookings, $requests);
$event_data = Studip\Fullcalendar::createData($objects, $begin_timestamp, $end_timestamp);
if ($nobody_access) {
//For nobody users, the code stops here since
//nobody users are not allowed to include additional objects.
$this->render_json($event_data);
return;
}
//Check if there are additional objects to be displayed:
$additional_objects = Request::getArray('additional_objects');
$additional_object_colours = Request::getArray('additional_object_colours');
if ($additional_objects) {
foreach ($additional_objects as $object_class => $object_ids) {
if (
!is_a($object_class, SimpleORMap::class, true)
|| !is_a($object_class, Studip\Calendar\EventSource::class, true)
) {
continue;
}
$special_colours = [];
if ($additional_object_colours[$object_class]) {
$special_colours = $additional_object_colours[$object_class];
}
$additional_objects = $object_class::findMany($object_ids);
foreach ($additional_objects as $additional_object) {
$event_data = $additional_object->getFilteredEventData(
$current_user->id,
null,
null,
$begin,
$end
);
if ($special_colours) {
foreach ($event_data as $data) {
$data->text_colour = $special_colours['fg'];
$data->background_colour = $special_colours['bg'];
$data->editable = false;
$event_data[] = $data->toFullcalendarEvent();
}
}
}
}
}
$this->render_json($event_data);
}
public function get_clipboard_semester_plan_action($clipboard_id = null)
{
if (!$clipboard_id) {
throw new Exception('ID of clipboard has not been provided!');
}
$clipboard = Clipboard::find($clipboard_id);
if (!empty($_SESSION['selected_clipboard_id'])) {
$clipboard = \Clipboard::find($_SESSION['selected_clipboard_id']);
}
if (!$clipboard) {
throw new Exception('Clipboard object not found!');
}
$current_user = User::findCurrent();
//Permission check:
if ($clipboard->user_id !== $current_user->id) {
throw new \AccessDeniedException();
}
$display_requests = Request::bool('display_requests');
$display_all_requests = Request::bool('display_all_requests');
$begin = new DateTime();
$end = new DateTime();
$semester_id = Request::get('semester_id');
$semester = $semester_id ? Semester::find($semester_id) : Semester::findCurrent();
if (!$semester) {
throw new Exception('No semester found!');
}
if (Request::get('semester_timerange') === 'vorles') {
$begin->setTimestamp($semester->vorles_beginn);
$end->setTimestamp($semester->vorles_ende);
} else {
$begin->setTimestamp($semester->beginn);
$end->setTimestamp($semester->ende);
}
$rooms = Room::findMany($clipboard->getAllRangeIds('Room'));
//Get parameters:
$booking_types = Request::getArray('booking_types');
//Get the event data sources:
$plan_objects = [];
foreach ($rooms as $room) {
if ($room->bookingPlanVisibleForuser($current_user)) {
$plan_objects = array_merge(
$plan_objects,
ResourceManager::getBookingPlanObjects(
$room,
[
[
'begin' => $begin->getTimestamp(),
'end' => $end->getTimestamp()
]
],
$booking_types,
$display_all_requests ? 'all' : $display_requests
)
);
}
}
$merged_objects = [];
$meta_dates = [];
$relevant_request = false;
foreach ($plan_objects as $plan_object) {
if ($plan_object instanceof ResourceBooking) {
$irrelevant_booking = $plan_object->getRepetitionType() !== 'weekly'
|| (
$plan_object->getAssignedUserType() === 'course'
&& in_array($plan_object->assigned_course_date->metadate_id, $meta_dates)
);
if ($irrelevant_booking) {
continue;
}
//It is a booking with repetitions that has to be included
//in the semester plan.
$real_begin = $plan_object->begin;
if ($plan_object->preparation_time > 0) {
$real_begin -= $plan_object->preparation_time;
}
$event_data = $plan_object->convertToEventData(
[
ResourceBookingInterval::build(
[
'interval_id' => md5(uniqid()),
'begin' => $real_begin,
'end' => $plan_object->end
]
)
],
$current_user
);
//Merge event data from the same booking that have the
//same weekday and begin and end time into one event.
//If no repetition interval is set and the booking belongs
//to a course date, use the corresponding metadate ID or the
//course date ID in the index. Otherwise use the booking's
//ID (specified by event_data->object_id).
foreach ($event_data as $event) {
if ($plan_object->getAssignedUserType() === 'course') {
$index = sprintf(
'%s_%s_%s',
$plan_object->assigned_course_date->metadate_id,
$event->begin->format('NHis'),
$event->end->format('NHis')
);
$meta_dates[] = $plan_object->assigned_course_date->metadate_id;
} else {
$index = sprintf(
'%s_%s_%s',
$plan_object->id,
$event->begin->format('NHis'),
$event->end->format('NHis')
);
}
//Strip some data that cannot be used effectively in here:
$event->api_urls = [];
$merged_objects[$index] = $event;
}
} else if ($plan_object instanceof ResourceRequest) {
if ($plan_object->cycle instanceof SeminarCycleDate) {
$cycle_dates = $plan_object->cycle->getAllDates();
foreach ($cycle_dates as $cycle_date) {
$relevant_request = $semester->beginn <= $cycle_date->date
&& $semester->ende >= $cycle_date->date;
if ($relevant_request) {
//We have found a date for the current semester
//that makes the request relevant.
break;
}
}
if (!$relevant_request) {
continue;
}
$event_data_list = $plan_object->getFilteredEventData(
$current_user->id
);
foreach ($event_data_list as $event_data) {
$index = sprintf(
'%s_%s_%s',
$plan_object->metadate_id,
$event_data->begin->format('NHis'),
$event_data->end->format('NHis')
);
//Strip some data that cannot be used effectively in here:
$event_data->view_urls = [];
$event_data->api_urls = [];
$merged_objects[$index] = $event_data;
}
}
}
}
//Convert the merged events to Fullcalendar events:
$data = [];
foreach ($merged_objects as $obj) {
$data[] = $obj->toFullCalendarEvent();
}
$this->render_json($data);
}
}
...@@ -171,8 +171,7 @@ ...@@ -171,8 +171,7 @@
'eventSources' => [ 'eventSources' => [
[ [
'url' => URLHelper::getURL( 'url' => URLHelper::getURL(
'api.php/resources/resource/' 'dispatch.php/resources/ajax/get_semester_booking_plan/' . $room->id
. $room->id . '/booking_plan'
), ),
'method' => 'GET', 'method' => 'GET',
'extraParams' => [ 'extraParams' => [
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
'eventSources' => [ 'eventSources' => [
[ [
'url' => URLHelper::getURL( 'url' => URLHelper::getURL(
'api.php/resources/resource/' . $resource->id . '/booking_plan' 'dispatch.php/resources/ajax/get_semester_booking_plan/' . $resource->id
), ),
'method' => 'GET', 'method' => 'GET',
'extraParams' => [ 'extraParams' => [
......
...@@ -19,8 +19,7 @@ ...@@ -19,8 +19,7 @@
'eventSources' => [ 'eventSources' => [
[ [
'url' => URLHelper::getLink( 'url' => URLHelper::getLink(
'api.php/resources/resource/' 'dispatch.php/resources/ajax/get_semester_booking_plan/' . $resource->id
. htmlReady($resource->id) . '/booking_plan'
), ),
'method' => 'GET', 'method' => 'GET',
'extraParams' => [ 'extraParams' => [
......
...@@ -76,7 +76,7 @@ ...@@ -76,7 +76,7 @@
'eventSources' => [ 'eventSources' => [
[ [
'url' => URLHelper::getURL( 'url' => URLHelper::getURL(
'api.php/resources/resource/' . $resource->id . '/booking_plan' 'dispatch.php/resources/ajax/get_booking_plan/' . $resource->id
), ),
'method' => 'GET', 'method' => 'GET',
'extraParams' => [ 'extraParams' => [
......
...@@ -103,10 +103,7 @@ ...@@ -103,10 +103,7 @@
'eventSources' => [ 'eventSources' => [
[ [
'url' => URLHelper::getURL( 'url' => URLHelper::getURL(
sprintf( 'dispatch.php/resources/ajax/get_semester_booking_plan/' . $resource->id
'api.php/resources/resource/%s/semester_plan',
htmlReady($resource->id)
)
), ),
'method' => 'GET', 'method' => 'GET',
'extraParams' => [ 'extraParams' => [
......
...@@ -82,10 +82,7 @@ ...@@ -82,10 +82,7 @@
'eventSources' => [ 'eventSources' => [
[ [
'url' => URLHelper::getURL( 'url' => URLHelper::getURL(
sprintf( 'dispatch.php/resources/ajax/get_booking_plan/' . $resource->id
'api.php/resources/resource/%s/semester_plan',
htmlReady($resource->id)
)
), ),
'method' => 'GET', 'method' => 'GET',
'extraParams' => [ 'extraParams' => [
......
...@@ -56,8 +56,7 @@ ...@@ -56,8 +56,7 @@
'eventSources' => [ 'eventSources' => [
[ [
'url' => URLHelper::getLink( 'url' => URLHelper::getLink(
'api.php/room_clipboard/' 'dispatch.php/resources/ajax/get_clipboard_semester_plan/' . $clipboard->id
. htmlReady($clipboard->id) . '/booking_plan'
), ),
'method' => 'GET', 'method' => 'GET',
'extraParams' => [ 'extraParams' => [
......
...@@ -61,8 +61,7 @@ ...@@ -61,8 +61,7 @@
'eventSources' => [ 'eventSources' => [
[ [
'url' => URLHelper::getLink( 'url' => URLHelper::getLink(
'api.php/room_clipboard/' 'dispatch.php/resources/ajax/get_clipboard_semester_plan/' . $clipboard->id
. htmlReady($clipboard->id) . '/semester_plan'
), ),
'method' => 'GET', 'method' => 'GET',
'extraParams' => [ 'extraParams' => [
......
...@@ -3,8 +3,7 @@ import { $gettext } from '../lib/gettext'; ...@@ -3,8 +3,7 @@ import { $gettext } from '../lib/gettext';
class Resources class Resources
{ {
static addUserToPermissionList(user_id, table_element) static addUserToPermissionList(user_id, table_element) {
{
if (!user_id || !table_element) { if (!user_id || !table_element) {
return; return;
} }
...@@ -156,8 +155,7 @@ class Resources ...@@ -156,8 +155,7 @@ class Resources
} }
static addCourseUsersToPermissionList(course_id, table_element) static addCourseUsersToPermissionList(course_id, table_element) {
{
if (!course_id || !table_element) { if (!course_id || !table_element) {
return; return;
} }
...@@ -183,8 +181,7 @@ class Resources ...@@ -183,8 +181,7 @@ class Resources
} }
static removeUserFromPermissionList(html_node) static removeUserFromPermissionList(html_node) {
{
if (!html_node) { if (!html_node) {
return; return;
} }
...@@ -208,8 +205,7 @@ class Resources ...@@ -208,8 +205,7 @@ class Resources
//Room search related methods: //Room search related methods:
static addSearchCriteriaToRoomSearchWidget(select_node) static addSearchCriteriaToRoomSearchWidget(select_node) {
{
if (!select_node) { if (!select_node) {
return; return;
} }
...@@ -381,8 +377,7 @@ class Resources ...@@ -381,8 +377,7 @@ class Resources
} }
static removeSearchCriteriaFromRoomSearchWidget(icon_node) static removeSearchCriteriaFromRoomSearchWidget(icon_node) {
{
if (!icon_node) { if (!icon_node) {
return; return;
} }
...@@ -412,8 +407,7 @@ class Resources ...@@ -412,8 +407,7 @@ class Resources
} }
static submitRoomSearchWidgetForm(input_node) static submitRoomSearchWidgetForm(input_node) {
{
if (!input_node) { if (!input_node) {
return; return;
} }
...@@ -431,8 +425,7 @@ class Resources ...@@ -431,8 +425,7 @@ class Resources
//Resource request related methods: //Resource request related methods:
static addPropertyToRequest(event) static addPropertyToRequest(event) {
{
var select = jQuery(event.target).siblings('select.requestable-properties-select')[0]; var select = jQuery(event.target).siblings('select.requestable-properties-select')[0];
if (!select) { if (!select) {
return; return;
...@@ -486,48 +479,32 @@ class Resources ...@@ -486,48 +479,32 @@ class Resources
//ResourceBookingInterval methods: //ResourceBookingInterval methods:
static toggleBookingIntervalStatus(event) static toggleBookingIntervalStatus(event) {
{
event.preventDefault(); event.preventDefault();
var li = jQuery(event.target).parents('tr')[0]; let button = event.target.closest('button');
if (!li) { let intervalId = button.dataset.interval_id;
//Something is wrong with the HTML.
return; if (!intervalId) {
}
var interval_id = jQuery(li).data('interval_id');
if (!interval_id) {
return; return;
} }
STUDIP.api.POST( const url = STUDIP.URLHelper.getURL(`dispatch.php/resources/ajax/toggle_takes_place_field/${intervalId}`);
`resources/booking_interval/${interval_id}/toggle_takes_place` fetch(url)
).done(function(data) { .then(response => response.json())
if (data['takes_place'] === undefined) { .then(response => {
if (response['takes_place'] === undefined) {
//Something went wrong: do nothing. //Something went wrong: do nothing.
return; return;
} }
if (data['takes_place'] === '1') { const cell = button.closest('td');
//Switch on the icons and text for the "takes place" cell.previousElementSibling.classList.toggle('not-taking-place');
//status and switch off the other ones: cell.querySelectorAll('button').forEach(node => node.classList.toggle('invisible'));
jQuery(li).find('.takes-place-revive').addClass('invisible');
jQuery(li).find('.takes-place-delete').removeClass('invisible');
jQuery(li).find('.booking-list-interval-date').removeClass('not-taking-place');
} else {
//Do the opposite of the if-block above:
jQuery(li).find('.takes-place-delete').addClass('invisible');
jQuery(li).find('.takes-place-revive').removeClass('invisible');
jQuery(li).find('.booking-list-interval-date').addClass('not-taking-place');
}
}); });
} }
//Methods for the resource category form: static addResourcePropertyToTable(event) {
static addResourcePropertyToTable(event)
{
var select = jQuery(event.target).siblings('select')[0]; var select = jQuery(event.target).siblings('select')[0];
if (!select) { if (!select) {
//Something is wrong with the HTML //Something is wrong with the HTML
...@@ -607,8 +584,7 @@ class Resources ...@@ -607,8 +584,7 @@ class Resources
//Methods for opening or closing of ressource tree elements: //Methods for opening or closing of ressource tree elements:
static toggleTreeNode(treenode) static toggleTreeNode(treenode) {
{
var arr = treenode.children("img"); var arr = treenode.children("img");
if (arr.hasClass('rotated')) { if (arr.hasClass('rotated')) {
arr.attr('style', 'transform: rotate(0deg)'); arr.attr('style', 'transform: rotate(0deg)');
...@@ -620,8 +596,7 @@ class Resources ...@@ -620,8 +596,7 @@ class Resources
} }
static moveTimeOptions(bookingtype_val) static moveTimeOptions(bookingtype_val) {
{
if (bookingtype_val === 'single') { if (bookingtype_val === 'single') {
$(".time-option-container").hide(); $(".time-option-container").hide();
$(".block-booking-item").hide(); $(".block-booking-item").hide();
...@@ -657,83 +632,81 @@ class Resources ...@@ -657,83 +632,81 @@ class Resources
//Fullcalendar specialisations: //Fullcalendar specialisations:
static updateEventUrlsInCalendar(calendar_event) static updateEventUrlsInCalendar(calendarEvent) {
{ if (!calendarEvent) {
if (!calendar_event) {
return; return;
} }
STUDIP.api.GET( let parentObjectId = calendarEvent.extendedProps.studip_parent_object_id;
`resources/booking/${calendar_event.extendedProps.studip_parent_object_id}/intervals`, let begin = STUDIP.Fullcalendar.toRFC3339String(calendarEvent.start);
{ let end = STUDIP.Fullcalendar.toRFC3339String(calendarEvent.end);
data: {
begin: STUDIP.Fullcalendar.toRFC3339String(calendar_event.start), const url = STUDIP.URLHelper.getURL(
end: STUDIP.Fullcalendar.toRFC3339String(calendar_event.end) `dispatch.php/resources/ajax/get_resource_booking_intervals/${parentObjectId}`,
} {begin, end}
} );
).done(function (data) { fetch(url)
if (!data || data.length === 0) { .then(response => response.json())
return; .then(response => {
} if (!response || response.length === 0) {
var new_interval_id = data[0].interval_id; return;
calendar_event.setExtendedProp('studip_object_id', new_interval_id); }
if (new_interval_id) { let newIntervalId = response[0].intervalId;
var move_url = calendar_event.extendedProps.studip_api_urls['move']; calendarEvent.setExtendedProp('studip_object_id', newIntervalId);
var resize_url = calendar_event.extendedProps.studip_api_urls['resize']; if (newIntervalId) {
move_url = move_url.replace( let moveUrl = calendarEvent.extendedProps.studip_api_urls['move'];
let resizeUrl = calendarEvent.extendedProps.studip_api_urls['resize'];
moveUrl = moveUrl.replace(
/&interval_id=([0-9a-f]{32})/, /&interval_id=([0-9a-f]{32})/,
'&interval_id=' + new_interval_id '&interval_id=' + newIntervalId
); );
resize_url = resize_url.replace( resizeUrl = resizeUrl.replace(
/&interval_id=([0-9a-f]{32})/, /&interval_id=([0-9a-f]{32})/,
'&interval_id=' + new_interval_id '&interval_id=' + newIntervalId
); );
var studip_api_urls = calendar_event.extendedProps.studip_api_urls; let studipApiUrls = calendarEvent.extendedProps.studip_api_urls;
studip_api_urls['move'] = move_url; studipApiUrls['move'] = moveUrl;
studip_api_urls['resize'] = resize_url; studipApiUrls['resize'] = resizeUrl;
calendar_event.setExtendedProp('studip_api_urls', studip_api_urls); calendarEvent.setExtendedProp('studip_api_urls', studipApiUrls);
} }
}); })
} }
static resizeEventInRoomGroupBookingPlan(info) static resizeEventInRoomGroupBookingPlan(info) {
{
STUDIP.Fullcalendar.defaultResizeEventHandler(info); STUDIP.Fullcalendar.defaultResizeEventHandler(info);
STUDIP.Resources.updateEventUrlsInCalendar(info.event); STUDIP.Resources.updateEventUrlsInCalendar(info.event);
} }
static dropEventInRoomGroupBookingPlan(info) static dropEventInRoomGroupBookingPlan(info) {
{
STUDIP.Fullcalendar.defaultDropEventHandler(info); STUDIP.Fullcalendar.defaultDropEventHandler(info);
STUDIP.Resources.updateEventUrlsInCalendar(info.event); STUDIP.Resources.updateEventUrlsInCalendar(info.event);
} }
static toggleRequestMarked(source_node) static toggleRequestMarked(sourceNode) {
{ if (!sourceNode) {
if (!source_node) {
return; return;
} }
var request_id = jQuery(source_node).data('request_id'); let requestId = sourceNode.dataset.request_id
if (!request_id) {
if (!requestId) {
return; return;
} }
STUDIP.api.POST( fetch(STUDIP.URLHelper.getURL(`dispatch.php/resources/ajax/toggle_marked/${requestId}`))
`resources/request/${request_id}/toggle_marked` .then(response => response.json())
).done(function(data) { .then(response => {
jQuery(source_node).attr('data-marked', data.marked); sourceNode.dataset.marked = response.marked;
jQuery(source_node).parent().attr('data-sort-value', data.marked); sourceNode.parentNode.dataset.sortValue = response.marked;
jQuery(source_node).parents('table.request-list').trigger('update'); sourceNode.closest('table.request-list').dispatchEvent(new Event('update'));
}); })
} }
static bookAllCalendarRequests() static bookAllCalendarRequests() {
{ let calendarSection = $('*[data-resources-fullcalendar="1"]')[0];
var calendarSektion = $('*[data-resources-fullcalendar="1"]')[0]; if (calendarSection) {
if (calendarSektion) { let calendar = calendarSection.calendar;
var calendar = calendarSektion.calendar;
if (calendar) { if (calendar) {
if (!$('#loading-spinner').length) { if (!$('#loading-spinner').length) {
jQuery('#content').append( jQuery('#content').append(
...@@ -747,10 +720,10 @@ class Resources ...@@ -747,10 +720,10 @@ class Resources
); );
} }
$('.fc-request-event').each(function () { $('.fc-request-event').each(function () {
var objectData = $(this).data(); let objectData = $(this).data();
var existingRequestEvent = calendar.getEventById(objectData.eventId); let existingRequestEvent = calendar.getEventById(objectData.eventId);
if (existingRequestEvent) { if (existingRequestEvent) {
var bookingURL = 'dispatch.php/resources/room_request/quickbook/' let bookingURL = 'dispatch.php/resources/room_request/quickbook/'
+ objectData.eventRequest + '/' + objectData.eventRequest + '/'
+ objectData.eventResource + '/' + objectData.eventResource + '/'
+ objectData.eventMetadate; + objectData.eventMetadate;
...@@ -780,10 +753,8 @@ Resources.definedResourceClasses = [ ...@@ -780,10 +753,8 @@ Resources.definedResourceClasses = [
]; ];
class Messages class Messages {
{ static selectRoom(room_id, room_name) {
static selectRoom(room_id, room_name)
{
if (!room_id) { if (!room_id) {
return; return;
} }
...@@ -806,15 +777,14 @@ class Messages ...@@ -806,15 +777,14 @@ class Messages
jQuery(selection_area).append(new_room); jQuery(selection_area).append(new_room);
} }
} }
Resources.Messages = Messages; Resources.Messages = Messages;
class BookingPlan class BookingPlan {
{ static insertEntry(new_entry, date, begin_hour, end_hour) {
static insertEntry(new_entry, date, begin_hour, end_hour)
{
//Get the resource-ID from the current URL: //Get the resource-ID from the current URL:
var results = window.location.href.match( let results = window.location.href.match(
/dispatch.php\/resources\/resource\/booking_plan\/([a-z0-9]{1,32})/ /dispatch.php\/resources\/resource\/booking_plan\/([a-z0-9]{1,32})/
); );
if (results.length === 0) { if (results.length === 0) {
...@@ -822,14 +792,14 @@ class BookingPlan ...@@ -822,14 +792,14 @@ class BookingPlan
jQuery(new_entry).remove(); jQuery(new_entry).remove();
return; return;
} }
var resource_id = results[1]; let resource_id = results[1];
//Now we re-format the time from begin_hour and end_hour. //Now we re-format the time from begin_hour and end_hour.
//In case the data-dragged attribute is set for the //In case the data-dragged attribute is set for the
//calendar entry we just add two hours to the start time //calendar entry we just add two hours to the start time
//to get the end time. //to get the end time.
var dragged = jQuery(new_entry).data('dragged'); let dragged = jQuery(new_entry).data('dragged');
if (dragged) { if (dragged) {
end_hour = begin_hour + 2; end_hour = begin_hour + 2;
} }
...@@ -840,7 +810,7 @@ class BookingPlan ...@@ -840,7 +810,7 @@ class BookingPlan
end_hour += ':00'; end_hour += ':00';
} }
var result = STUDIP.Dialog.fromURL( let result = STUDIP.Dialog.fromURL(
STUDIP.URLHelper.getURL( STUDIP.URLHelper.getURL(
'dispatch.php/resources/booking/add/' + resource_id, 'dispatch.php/resources/booking/add/' + resource_id,
{ {
...@@ -853,6 +823,7 @@ class BookingPlan ...@@ -853,6 +823,7 @@ class BookingPlan
); );
} }
} }
Resources.BookingPlan = BookingPlan; Resources.BookingPlan = BookingPlan;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment