/** * Get booking info from the booking form, verify validity and call a function who records the book * @author Albert Pérez Monfort (aperezm@xtec.cat) * @author Josep Ferràndiz Farré (jferran6@xtec.cat) * @return */ public function reserva($args) { if (!SecurityUtil::checkPermission('IWbookings::', '::', ACCESS_ADD)) { LogUtil::registerError($this->__('You are not allowed to administrate the bookings')); System::redirect(ModUtil::url('IWbookings', 'user', 'assigna', array('sid' => $sid))); return false; } // Confirm authorisation code $this->checkCsrfToken(); $bookingDate = FormUtil::getPassedValue('bookingDate', isset($args['bookingDate']) ? $args['bookingDate'] : null, 'POST'); $date = FormUtil::getPassedValue('date', isset($args['date']) ? $args['date'] : null, 'POST'); $dow = FormUtil::getPassedValue('dow', isset($args['dow']) ? $args['dow'] : 0, 'POST'); $fh = FormUtil::getPassedValue('fh', isset($args['fh']) ? $args['fh'] : 0, 'POST'); $sid = FormUtil::getPassedValue('sid', isset($args['sid']) ? $args['sid'] : null, 'POST'); $nsessions = FormUtil::getPassedValue('nsessions', isset($args['nsessions']) ? $args['nsessions'] : null, 'POST'); $group = FormUtil::getPassedValue('group', isset($args['group']) ? $args['group'] : null, 'POST'); $reason = FormUtil::getPassedValue('reason', isset($args['reason']) ? $args['reason'] : null, 'POST'); $inici_h = FormUtil::getPassedValue('inici_h', isset($args['inici_h']) ? $args['inici_h'] : null, 'POST'); $inici_m = FormUtil::getPassedValue('inici_m', isset($args['inici_m']) ? $args['inici_m'] : null, 'POST'); $final_h = FormUtil::getPassedValue('final_h', isset($args['final_h']) ? $args['final_h'] : null, 'POST'); $final_m = FormUtil::getPassedValue('final_m', isset($args['final_m']) ? $args['final_m'] : null, 'POST'); $user = FormUtil::getPassedValue('user', isset($args['user']) ? $args['user'] : null, 'POST'); $book_end = FormUtil::getPassedValue('book_end', isset($args['book_end']) ? $args['book_end'] : null, 'POST'); $finish_date = FormUtil::getPassedValue('date_input', isset($args['date_input']) ? $args['date_input'] : null, 'POST'); $hora = FormUtil::getPassedValue('hora', isset($args['hora']) ? $args['hora'] : null, 'POST'); if (is_null($user)) $user = UserUtil::getVar('uid'); // Arrange dates and times $horaris = array(); $d = explode('-', $bookingDate); $cdate = DateUtil::buildDatetime($d[2], $d[1], $d[0], 0, 0, 0, '%Y-%m-%d'); if ($hora) { // Space has got timeFrame $mdid = ModUtil::apiFunc('IWbookings', 'user', 'get', array('sid' => $sid)); $mdid = $mdid['mdid']; $frames = ModUtil::apiFunc('IWtimeframes', 'user', 'getall_horari', array('mdid' => $mdid)); $t = explode(' - ', $hora); $t_inici = explode(':', $t[0]); $t_final = explode(':', $t[1]); foreach ($frames as $frame) { $f = explode(' - ', $frame['hora']); $f_inici = explode(':', $f[0]); $f_final = explode(':', $f[1]); if (($t_inici[0] . $t_inici[1] <= $f_inici[0] . $f_inici[1]) && ($t_final[0] . $t_final[1] >= $f_final[0] . $f_final[1])) { $horaris[] = array('t_inici' => $f_inici, 't_final' => $f_final); } } $horari = $hora; } else { // Space has no timeframe $t_inici[0] = $inici_h; $t_inici[1] = $inici_m; $t_final[0] = $final_h; $t_final[1] = $final_m; $horaris[] = array('t_inici' => $t_inici, 't_final' => $t_final); $horari = $t_inici[0] . ":" . $t_inici[1] . " - " . $t_final[0] . ":" . $t_final[1]; } $startBooking = DateUtil::buildDatetime($d[2], $d[1], $d[0], $t_inici[0], $t_inici[1], 0, '%Y-%m-%d %H:%M:%S'); $endBooking = DateUtil::buildDatetime($d[2], $d[1], $d[0], $t_final[0], $t_final[1], 0, '%Y-%m-%d %H:%M:%S'); $sb = DateUtil::makeTimeStamp($startBooking); $eb = DateUtil::makeTimeStamp($endBooking); if (empty($group)) { LogUtil::registerError($this->__('You must specify a group')); System::redirect(ModUtil::url('IWbookings', 'user', 'assigna', array('sid' => $sid, 'fh' => $fh, 'dow' => $dow, 'd' => $bookingDate))); return true; } //Check if start date < end date if ($sb > $eb) { LogUtil::registerError($this->__('Finish time is minnor than start time')); System::redirect(ModUtil::url('IWbookings', 'user', 'assigna', array('sid' => $sid, 'fh' => $fh, 'dow' => $dow, 'd' => $bookingDate))); return true; } if ($book_end == 'date') { if (empty($finish_date)) { LogUtil::registerError($this->__('You must specify a finish date')); System::redirect(ModUtil::url('IWbookings', 'user', 'assigna', array('sid' => $sid))); return true; } else { $finish_date = strtotime($finish_date); if ($finish_date < strtotime('today')) { LogUtil::registerError($this->__('Finish date is minnor than actual date')); System::redirect(ModUtil::url('IWbookings', 'user', 'assigna', array('sid' => $sid))); return true; } } } // get nsessions from finish_date if ($book_end == 'date') { $nsessions = 0; $tmp_date = strtotime(date('Y-m-d', $eb)); while ($tmp_date <= $finish_date) { $nsessions++; $tmp_date = strtotime(date('Y-m-d', $tmp_date) . ' + 1 week'); } } $admin = true; if (!SecurityUtil::checkPermission('IWbookings::', '::', ACCESS_ADMIN)) { $admin = false; // Check if num sessions exceeds num weeks limit from actual date $maxWeeks = ModUtil::getVar('IWbookings', 'weeks'); if ($maxWeeks != 0) { $factor = $nsessions - 1; $last = strtotime(date('Y-m-d', $eb) . ' + ' . $factor . ' weeks'); $limit = strtotime('today + ' . $maxWeeks . ' weeks'); $alert = false; while ($last > $limit) { $last = strtotime(date('Y-m-d', $last) . ' -1 week'); $nsessions--; $alert = true; } if ($alert) $msg = $this->__('Bookings can only be made for the following ') . $maxWeeks . $this->__(' week(s). Haven\'t made all the requested bookings.'); } } $bookingDates = array(); // Generate all booking dates for ($i = 0; $i < $nsessions; ++$i) { foreach ($horaris as $h) { // Calculate next week $next = DateUtil::getDatetime_NextWeek($i, "%Y-%m-%d", $d[2], $d[1], $d[0], 12, 0, 0); // Per evitar canvis d'hora produits per l'horari d'estiu $bookingDates[] = array('s' => $next . " " . $h[t_inici][0] . ":" . $h[t_inici][1] . ":00", 'e' => $next . " " . $h[t_final][0] . ":" . $h[t_final][1] . ":00"); } } // Check if the requested bookings is possible $reserved = array(); foreach ($bookingDates as $bd) { $bookings = ModUtil::apiFunc('IWbookings', 'user', 'reservat', array('sid' => $sid, 'start' => $bd[s], 'end' => $bd[e])); if (count($bookings) != 0) $reserved[] = $bookings; } if (!count($reserved)) { // If all bookings are possible do it foreach ($bookingDates as $bd) { $success = ModUtil::apiFunc('IWbookings', 'user', 'reserva', array('sid' => $sid, 'start' => $bd[s], 'end' => $bd[e], 'user' => $user, 'usrgroup' => $group, 'reason' => $reason, 'nsessions' => $nsessions, 'nbooking' => $success, 'admin' => $admin)); if (!$success) { LogUtil::registerError($this->__('An error has occurred when loading the table or the form')); return System::redirect(ModUtil::url('IWbookings', 'user', 'assigna', array('sid' => $sid, 'date' => $cdate))); } } // Book receipt LogUtil::registerStatus("<b>[ " . $this->__('Booking receipt') . " ]</b><br />" . $this->__('Booking day: ') . ": <b> " . $bookingDate . "</b><br/>" . $this->__('Time: ') . ": <b>" . $horari . "</b><br />" . $this->__('Group: ') . ": <b>" . $group . "</b><br />" . $this->__('Reason for booking') . ": <b>" . $reason . "</b><br />" . $this->__('Repeat next weeks') . ": <b>" . $nsessions . "<br /><br /><b>" . $msg); } else { // Launch error message with reserved frames $message = $this->__('Booking failed! The following times are reserved:') . '<ul>'; foreach ($reserved as $r) { $message .= '<li><b>' . date('d-m-Y', strtotime($r['0']['start'])) . $this->__('</b> from ') . date('H:i', strtotime($r['0']['start'])) . $this->__(' to ') . date('H:i', strtotime($r['0']['end'])) . '</li>'; } $message .= '</ul>'; LogUtil::registerError($message); } return System::redirect(ModUtil::url('IWbookings', 'user', 'assigna', array('sid' => $sid, 'date' => $cdate))); }
/** * Calculate dates for date navigation purposes * @author Josep Ferr�ndiz Farr� (jferran6@xtec.cat) * @args The date (a MySQL timestamp or a string) * @return array. Fields: nextweek, preweek, nextmonth, prevmonth */ public function getJumpDates($args) { $TheDate = FormUtil::getPassedValue('date', isset($args['date']) ? $args['date'] : null, 'GET'); $result = array(); if (!SecurityUtil::checkPermission('IWbookings::', '::', ACCESS_READ)) { return false; } $fields = explode('-', $TheDate); $result['nextweek'] = DateUtil::getDatetime_NextWeek(1, '%d-%m-%y', $fields[0], $fields[1], $fields[2]); $result['prevweek'] = DateUtil::getDatetime_NextWeek(-1, '%d-%m-%y', $fields[0], $fields[1], $fields[2]); $result['nextmonth'] = DateUtil::getDatetime_NextMonth(1, '%d-%m-%y', $fields[0], $fields[1], $fields[2]); $result['prevmonth'] = DateUtil::getDatetime_NextMonth(-1, '%d-%m-%y', $fields[0], $fields[1], $fields[2]); return $result; }