public function bookings_add() { Util\Auth::isAdminAuthorized($this->signedUser, null, false, '/'); $today = new Util\DateTime(); $this->date = Util\Converter::toDate('date'); if (!isset($this->date)) { $this->date = new Util\DateTime(); } /* header('Content-Type: application/json'); echo(json_encode($this->getBookings()));exit(); */ $message = []; $message[] = $this->clearBookings(); $message[] = $this->addBookings($this->getBookings()); // main $tpl = new Util\Template(Util\Template::SITE, ['admin']); $tpl->set('today', $today->format('Y-m-d')); $tpl->set('message', implode($message, '<br>')); return $this->response('tpl-default', ['title' => Util\Lang::lang('own'), 'jsLauncher' => 'integration'], 'layout-1-col', ['column1' => $tpl->render('integration')]); }
public function adminBooking() { $isAllowed = Util\Auth::isAdminAuthorized($this->signedUser); $json = []; $json['result'] = \Rebond\Core\ResultType::ERROR; if (!$isAllowed) { $json['message'] = Util\Lang::lang('accessNonAuthorized'); return json_encode($json); } $title = Util\Converter::toString('title', 'post'); $color = Util\Converter::toString('color', 'post'); $courtIds = Util\Converter::toArray('courtIds', 'post'); $startDate = Util\Converter::toDate('startDate', 'post', new \DateTime()); $endDate = Util\Converter::toDate('endDate', 'post', new \DateTime()); $days = Util\Converter::toArray('days', 'post'); $startTime = Util\Converter::toInt('startTime', 'post'); $endTime = Util\Converter::toInt('endTime', 'post'); $count = 0; // @todo validate param $json['startDate'] = $startDate->format('datetime'); $json['endDate'] = $endDate->format('datetime'); $options = []; $options['where'][] = ['id IN (?)', $courtIds]; $courts = \Own\Bus\Court\Data::loadAll($options); $book = new Book\Model(); $book->setType(\Own\Bus\BookingType::ADMIN); $book->setTitle($title); $book->setSequence(uniqid()); $book->setColor($color); $dateLoop = clone $startDate; $today = new \DateTime(); $now = (int) $today->format('H') * 60 + (int) $today->format('i'); $today->setTime(0, 0, 0); $rule = \Own\Bus\Rule\Data::loadById(1, true); $timeLength = $rule->getTimeLength(); foreach ($courts as $court) { $book->setCourtId($court->getId()); while ($dateLoop <= $endDate) { if ($dateLoop < $today) { $dateLoop->add(new \DateInterval('P1D')); continue; } if (!in_array($dateLoop->format('w'), $days)) { $dateLoop->add(new \DateInterval('P1D')); continue; } $possibleTime = (int) $court->getStartTime()->format('H') * 60 + (int) $court->getStartTime()->format('i'); $limitTime = (int) $court->getEndTime()->format('H') * 60 + (int) $court->getEndTime()->format('i'); while ($possibleTime < min($limitTime, $endTime)) { if ($possibleTime >= $startTime && ($dateLoop != $today || $possibleTime >= $now)) { $dateLoop->setTime(floor($possibleTime / 60), $possibleTime % 60, 0); // check for court already booked $options = []; $options['where'][] = ['court_id = ?', $court->getId()]; $options['where'][] = ['booking_date = ?', $dateLoop->format('Y-m-d H:i:00')]; $booking = Book\Data::load($options); if (!isset($booking)) { $book->setBookingDate($dateLoop); $book->save(); $book->setId(0); $count++; } } $dateLoop->setTime(0, 0, 0); $possibleTime += $timeLength; } $dateLoop->add(new \DateInterval('P1D')); } $dateLoop = clone $startDate; } $json['result'] = \Rebond\Core\ResultType::SUCCESS; $json['count'] = $count; return json_encode($json); }