示例#1
0
    /**
     * 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)));
    }
示例#2
0
    public function fer_reserva($args) {
        $sid = FormUtil::getPassedValue('sid', isset($args['sid']) ? $args['sid'] : null, 'GET');
        $inici = FormUtil::getPassedValue('inici', isset($args['inici']) ? $args['inici'] : null, 'GET');
        $final = FormUtil::getPassedValue('final', isset($args['final']) ? $args['final'] : null, 'GET');
        $grup = FormUtil::getPassedValue('grup', isset($args['grup']) ? $args['grup'] : null, 'GET');
        $profe = FormUtil::getPassedValue('profe', isset($args['profe']) ? $args['profe'] : null, 'GET');
        $finish_date = FormUtil::getPassedValue('finish_date', isset($args['finish_date']) ? $args['finish_date'] : null, 'GET');

        //Comprova que la identitat de l'espai de reserva efectivament hagi arribat
        if ((!isset($sid))) {
            return LogUtil::registerError($this->__('Error! Could not do what you wanted. Please check your input.'));
        }
        // Security check
        if (!SecurityUtil::checkPermission('IWbookings::', "::", ACCESS_ADMIN)) {
            throw new Zikula_Exception_Forbidden();
        }
        // Get day of week
        $dow = date("w", DateUtil::makeTimeStamp($inici));
        // Do a booking for every week until finish date
        $i = 0;
        while ((strtotime($inici . ' + ' . $i . ' weeks')) <= (strtotime(date('d-m-Y', $finish_date) . ' + 24 hours'))) {
            $item = array('sid' => $sid,
                'user' => $profe,
                'usrgroup' => $grup,
                'start' => date('Y-m-d H:i:s', strtotime($inici . ' + ' . $i . ' weeks')),
                'end' => date('Y-m-d H:i:s', strtotime($final . ' + ' . $i . ' weeks')),
                'dayofweek' => $dow,
                'temp' => 0);
            if (!DBUtil::insertObject($item, 'IWbookings', 'bid')) {
                LogUtil::registerError($this->__('Error! Creation attempt failed.'));
                return false;
            }
            $i++;
        }

        // Return the id of the last created item to the calling process
        return $item['bid'];
    }
示例#3
0
    public function reserva($args) {
        $sid = FormUtil::getPassedValue('sid', isset($args['sid']) ? $args['sid'] : null, 'GET');
        $start = FormUtil::getPassedValue('start', isset($args['start']) ? $args['start'] : null, 'GET');
        $end = FormUtil::getPassedValue('end', isset($args['end']) ? $args['end'] : null, 'GET');
        $group = FormUtil::getPassedValue('usrgroup', isset($args['usrgroup']) ? $args['usrgroup'] : null, 'GET');
        $user = FormUtil::getPassedValue('user', isset($args['user']) ? $args['user'] : null, 'GET');
        $reason = FormUtil::getPassedValue('reason', isset($args['reason']) ? $args['reason'] : null, 'GET');
        $nsessions = FormUtil::getPassedValue('nsessions', isset($args['nsessions']) ? $args['nsessions'] : null, 'GET');
        $nbooking = FormUtil::getPassedValue('nbooking', isset($args['nbooking']) ? $args['nbooking'] : null, 'GET');
        $admin = FormUtil::getPassedValue('admin', isset($args['admin']) ? $args['admin'] : null, 'GET');

        //Comprova que la identitat de l'espai de reserva efectivament hagi arribat
        if ((!isset($sid))) {
            return LogUtil::registerError($this->__('Error! Could not do what you wanted. Please check your input.'));
        }
        // Security check
        if (!SecurityUtil::checkPermission('IWbookings::', "::", ACCESS_ADD)) {
            return LogUtil::registerError($this->__('You are not allowed to administrate the bookings'), 403);
        }

        // Identificates grouped bookings: has the same day of week and time during n consecutive weeks
        $key = DBUtil::selectFieldMax('IWbookings', 'bid') + 1;
        if (empty($nbooking))
            $nbooking = $key;

        // Get day of week
        $dow = date("w", DateUtil::makeTimeStamp($start));
        ($dow == 0) ? $dow = 7 : "";
        ($admin) ? $temp = 1 : 0;
        $item = array('sid' => $sid,
            'user' => $user,
            'usrgroup' => $group,
            'start' => $start,
            'end' => $end,
            'reason' => $reason,
            'dayofweek' => $dow,
            'bkey' => $nbooking,
            'temp' => $temp);
        if (!DBUtil::insertObject($item, 'IWbookings', 'bid')) {
            LogUtil::registerError($this->__('Error! Creation attempt failed.'));
            return false;
        }

        // Return the id of the newly created item to the calling process
        return $nbooking;
    }