コード例 #1
0
ファイル: Booking.php プロジェクト: rizkioa/etak6
 /**
  * Insert ticket (helper)
  * @param int $seatCountYouWant
  * @param array $seats
  * @param int $orderID
  * @param array $tempSeats
  * @param int $tripID
  * @return array
  */
 private function _insertTickets($seatCountYouWant, $seats, $orderID, $tempSeats, $tripID)
 {
     // $seats has form: Array([1] => 'no', [2] => 'yesOnNormal', [3] => 'yesOnTemp', ...)
     // $tempSeats has form: Array([0] => [1], ...) with 0 is index, 1 is seat number.
     // check if the seat count is enough to book
     $arrayCountValues = array_count_values($seats);
     $freeSeatCount = !array_key_exists('no', $arrayCountValues) ? 0 : $arrayCountValues['no'];
     $selectedNormalSeatCount = !array_key_exists('yesOnNormal', $arrayCountValues) ? 0 : $arrayCountValues['yesOnNormal'];
     $selectedTempSeatCount = !array_key_exists('yesOnTemp', $arrayCountValues) ? 0 : $arrayCountValues['yesOnTemp'];
     $remainingSeatCount = $seatCountYouWant - $selectedNormalSeatCount - $selectedTempSeatCount;
     if ($remainingSeatCount > $freeSeatCount || $freeSeatCount == 0) {
         throw new Exception('Not enough available seats');
     }
     // store the seat numbers you selected as fixed seats, unset them from $seats,
     //// we will actually insert them later
     $tickets = array();
     $fixedSelectedSeats = array();
     $ticketModel = new Customer_Model_Ticket();
     foreach ($seats as $seatNumber => $isBooked) {
         if ($isBooked != 'no') {
             // store this for later insert
             $fixedSelectedSeats[] = $seatNumber;
             // remove this seat from seats array
             unset($seats[$seatNumber]);
         }
     }
     // translate temp seats of others backward
     $i = 0;
     $tempSeatCount = count($tempSeats);
     foreach ($seats as $seatNumber => $isBooked) {
         if ($i == $tempSeatCount) {
             break;
         }
         if ($isBooked == 'no') {
             $ticketID = $tempSeats[$i];
             $this->_updateTempSeat($ticketID, $seatNumber);
             // remove this seat from seats array
             unset($seats[$seatNumber]);
             ++$i;
         }
     }
     // create tickets you selected as fixed seats
     foreach ($fixedSelectedSeats as $seatNumber) {
         $ticketID = $ticketModel->createTicket($orderID, $seatNumber, 1, $tripID);
         $tickets[] = $ticketID;
     }
     // create tickets you has not selected as temp seats
     $seatCountYouNotSelected = $remainingSeatCount;
     $i = 0;
     foreach ($seats as $seatNumber => $isBooked) {
         if ($i == $seatCountYouNotSelected) {
             break;
         }
         if ($isBooked == 'no') {
             $tickets[] = $ticketModel->createTicket($orderID, $seatNumber, 0, $tripID);
             // remove this seat from seats array
             unset($seats[$seatNumber]);
             ++$i;
         }
     }
     return $tickets;
 }