예제 #1
0
 /**
  * Mission
  * CODE: mission_create, mission_leg_create
  */
 public function executeUpdate(sfWebRequest $request)
 {
     #security
     if (!$this->getUser()->hasCredential(array('Administrator', 'Staff', 'Coordinator'), false)) {
         $this->getUser()->setFlash("warning", 'You don\'t have permission to access this url ' . $request->getReferer());
         $this->redirect('dashboard/index');
     }
     if ($request->getParameter('add_passengers')) {
         $this->group_camp_id = $request->getParameter('add_passengers');
     }
     if ($request->getParameter('id')) {
     } else {
         if ($request->isMethod('post')) {
             $main_id = $request->getParameter('main_id');
             $row_id = $request->getParameter('row_id');
             $camp_id = $request->getParameter('camp_id');
             if (isset($row_id)) {
                 $fname = $request->getParameter('pass_fname' . $row_id);
                 $lname = $request->getParameter('pass_lname' . $row_id);
                 $location = $request->getParameter('pass_loc' . $row_id);
                 $note = $request->getParameter('pass_note' . $row_id);
                 $link = $request->getParameter('pass_link' . $row_id);
             } else {
                 $main_id = $request->getParameter('person_id');
                 $fname = $request->getParameter('pass_fname');
                 $lname = $request->getParameter('pass_lname');
                 $location = $request->getParameter('pass_loc');
                 $note = $request->getParameter('pass_note');
                 $link = $request->getParameter('link');
             }
             //echo var_dump($request->getParameter('link'));die();
             #create default Itinerary to passenger
             if (isset($main_id)) {
                 $is_passenger = PassengerPeer::getByPersonId($main_id);
                 if (!$is_passenger->getRequesterId() && $camp_id) {
                     $this->getUser()->setFlash('success', 'Passenger has no Requester. Requester must be selected before create a Mission!');
                     $this->redirect('@add_passengers?id=' . $camp_id);
                 }
                 $itinerary = new Itinerary();
                 if (isset($is_passenger) && $is_passenger instanceof Passenger) {
                     $itinerary->setDateRequested(date('y/m/d'));
                     $itinerary->setApointTime('Morning');
                     $itinerary->setPassengerId($is_passenger->getId());
                     $miss_type = MissionTypePeer::getName('Camp');
                     if (isset($miss_type) && $miss_type instanceof MissionType) {
                         $itinerary->setMissionTypeId($miss_type->getId());
                     }
                     if ($is_passenger->getRequesterId()) {
                         $pass_req = $is_passenger->getRequester();
                         if (isset($pass_req) && $pass_req) {
                             if (isset($pass_req) && $pass_req instanceof Requester) {
                                 $itinerary->setRequesterId($pass_req->getId());
                             }
                         }
                     }
                     if (isset($camp_id)) {
                         $camp = CampPeer::retrieveByPK($camp_id);
                         if (isset($camp) && $camp instanceof Camp) {
                             if ($camp->getAgencyId()) {
                                 $agency = $camp->getAgency();
                                 if (isset($agency) && $agency instanceof Agency) {
                                     $itinerary->setAgencyId($agency->getId());
                                 }
                             }
                         }
                     }
                     $itinerary->setCampId($camp_id);
                     $itinerary->save();
                     if (isset($itinerary) && $itinerary instanceof Itinerary) {
                         $this->redirect('@itinerary_detail?id=' . $itinerary->getId() . '&add_passengers=' . $camp_id);
                     }
                 }
             }
         }
     }
     $this->itinerary = ItineraryPeer::retrieveByPK($request->getParameter('id'));
     $this->forward404Unless($this->itinerary);
     $itinerary =& $this->itinerary;
     $this->errors = array();
     if ($request->isMethod('post')) {
         # validation
         $mission_date = $request->getParameter('mission_date');
         if (empty($mission_date)) {
             $this->errors[] = 'Mission date is required';
         }
         $companions = (array) $request->getParameter('companions');
         if (count($companions)) {
             $c = new Criteria();
             $c->add(CompanionPeer::ID, $companions, Criteria::IN);
             $c->add(CompanionPeer::PASSENGER_ID, $this->itinerary->getPassengerId());
             if (CompanionPeer::doCount($c) != count($companions)) {
                 $this->errors[] = 'Some companions not found';
             }
         }
         #use it when add group mission
         $group_camp_id = $request->getParameter('add_passengers');
         switch ($request->getParameter('transportation')) {
             case 'air_mission':
                 $origin_airports = (array) $request->getParameter('origin_idents');
                 $dest_airports = (array) $request->getParameter('destination_idents');
                 $idents = $dest_airports;
                 $tmp_arr = array();
                 foreach ($origin_airports as $i => $ident) {
                     $idents[] = $ident;
                     $v = $ident . ' to ' . $dest_airports[$i];
                     if (in_array($v, $tmp_arr)) {
                         $this->errors[] = 'Leg ' . $v . ' appeared more than one';
                     } else {
                         $tmp_arr[] = $v;
                     }
                     if ($dest_airports[$i] == $ident) {
                         $this->errors[] = 'Leg ' . $ident . ' to ' . $dest_airports[$i] . ' is invalid';
                     }
                 }
                 $idents = array_unique($idents);
                 $c = new Criteria();
                 $c->add(AirportPeer::IDENT, $idents, Criteria::IN);
                 if (count($idents) != AirportPeer::doCount($c)) {
                     $this->errors[] = 'Some airport idents are invalid';
                 }
                 break;
             case 'ground_mission':
                 $origin = $request->getParameter('ground_origin');
                 $destination = $request->getParameter('ground_destination');
                 if (empty($destination)) {
                     $this->errors[] = 'Please specify destination address';
                 }
                 if (empty($origin)) {
                     $this->errors[] = 'Please specify origin address';
                 } elseif ($destination == $origin) {
                     $this->errors[] = 'Origin and Destination addresses conflict';
                 }
                 break;
             case 'commercial_mission':
                 if ($v = $request->getParameter('airline_id')) {
                     $custom = $request->getParameter('airline_custom');
                     if ($v == 'other') {
                         if (empty($custom)) {
                             $this->errors[] = 'Please type a new airline name!';
                         }
                     } else {
                         $airline = AirlinePeer::retrieveByPK($v = $request->getParameter('airline_id'));
                         if (!$airline instanceof Airline) {
                             $this->errors[] = 'Please select airline!';
                         }
                     }
                 } else {
                     $this->errors[] = 'Please select airline!';
                 }
                 break;
             default:
                 $this->errors[] = 'Please select Transportation Type';
         }
         if (count($this->errors)) {
             # error in form
             switch ($request->getParameter('transportation')) {
                 case 'air_mission':
                     $this->origin_idents = $origin_airports;
                     $this->dest_idents = $dest_airports;
                     break;
                 case 'ground_mission':
                     break;
                 case 'commercial_mission':
                     break;
             }
             $this->selected_companions = $companions;
         } else {
             # Create Mission
             $mission = new Mission();
             $mission->setItineraryId($itinerary->getId());
             $mission->setMissionTypeId($itinerary->getMissionTypeId());
             $mission->setDateRequested($itinerary->getDateRequested());
             $mission->setPassengerId($itinerary->getPassengerId());
             $mission->setRequesterId($itinerary->getRequesterId());
             $mission->setCampId($itinerary->getCampId());
             $mission->setMissionDate(strtotime($request->getParameter('mission_date')));
             $mission->setMissionSpecificComments($request->getParameter('comment'));
             $mission->save();
             # Create Companions
             foreach ($companions as $id) {
                 $mission_companion = new MissionCompanion();
                 $mission_companion->setMissionId($mission->getId());
                 $mission_companion->setCompanionId($id);
                 $mission_companion->save();
             }
             # Create Legs
             switch ($request->getParameter('transportation')) {
                 case 'air_mission':
                     for ($i = 0; $i < sizeof($origin_airports); $i++) {
                         $airport_o = AirportPeer::getByIdent($origin_airports[$i]);
                         $airport_d = AirportPeer::getByIdent($dest_airports[$i]);
                         $mission_leg = new MissionLeg();
                         $mission_leg->setMissionId($mission->getId());
                         $mission_leg->setLegNumber($i + 1);
                         $mission_leg->setFromAirportId($airport_o->getId());
                         $mission_leg->setToAirportId($airport_d->getId());
                         $mission_leg->setBaggageWeight($request->getParameter('baggage_weight'));
                         $mission_leg->setBaggageDesc($request->getParameter('baggage_desc'));
                         $mission_leg->setPassOnBoard(0);
                         $mission_leg->setTransportation('air_mission');
                         $mission_leg->save();
                     }
                     $this->getUser()->setFlash('success', 'Mission and Leg(s) has successfully created!');
                     break;
                 case 'ground_mission':
                     $mission_leg = new MissionLeg();
                     $mission_leg->setMissionId($mission->getId());
                     $mission_leg->setLegNumber(1);
                     $mission_leg->setPassOnBoard(0);
                     $mission_leg->setTransportation('ground_mission');
                     $mission_leg->setGroundOrigin($request->getParameter('ground_origin'));
                     $mission_leg->setGroundDestination($request->getParameter('ground_destination'));
                     $mission_leg->save();
                     break;
                 case 'commercial_mission':
                     $flight_time = $request->getParameter('flight_time');
                     if (empty($flight_time['hour']) || empty($flight_time['minute'])) {
                         $flight_time = null;
                     }
                     $airline_id = $request->getParameter('airline_id');
                     if ($airline_id == 'other') {
                         $airline = new Airline();
                         $airline->setName($request->getParameter('airline_custom'));
                         $airline->save();
                     } else {
                         $airline = AirlinePeer::retrieveByPK($airline_id);
                         $this->forward404Unless($airline);
                     }
                     $origins = $request->getParameter('origin');
                     $destinations = $request->getParameter('destination');
                     $flight_numbers = $request->getParameter('flight_number');
                     $departures = $request->getParameter('departure');
                     $arrivals = $request->getParameter('arrival');
                     $mission->setFlightTime($flight_time['hour'] . ':' . $flight_time['minute'] . ' ' . $flight_time['period']);
                     $n_leg = 0;
                     foreach ($origins as $i => $origin) {
                         if (empty($origin) || empty($destinations[$i])) {
                             continue;
                         }
                         $mission_leg = new MissionLeg();
                         $mission_leg->setMissionId($mission->getId());
                         $mission_leg->setLegNumber(++$n_leg);
                         $mission_leg->setFlightTime($flight_time ? strtotime($flight_time['hour'] . ':' . $flight_time['minute'] . ' ' . $flight_time['period']) : null);
                         $mission_leg->setBaggageDesc($request->getParameter('baggage_desc'));
                         $mission_leg->setBaggageWeight($request->getParameter('baggage_weight'));
                         $mission_leg->setAirlineId($airline->getId());
                         $mission_leg->setFundId($request->getParameter('fund_id'));
                         $mission_leg->setConfirmCode($request->getParameter('confirm_code'));
                         $mission_leg->setFlightCost($request->getParameter('flight_cost'));
                         $mission_leg->setCommOrigin($origin);
                         $mission_leg->setCommDest($destinations[$i]);
                         $mission_leg->setFlightNumber($flight_numbers[$i]);
                         $v = $departures[$i];
                         if (empty($v['hour']) || empty($v['minute'])) {
                             $v = null;
                         }
                         $mission_leg->setDeparture($v ? strtotime($v['hour'] . ':' . $v['minute'] . ' ' . $v['period']) : null);
                         $v = $arrivals[$i];
                         if (empty($v['hour']) || empty($v['minute'])) {
                             $v = null;
                         }
                         $mission_leg->setDeparture($v ? strtotime($v['hour'] . ':' . $v['minute'] . ' ' . $v['period']) : null);
                         $mission_leg->setTransportation('commercial_mission');
                         $mission_leg->save();
                     }
                     break;
             }
             //        $this->redirect('@create_companion?id='.$mission_leg->getId());
             if (isset($group_camp_id)) {
                 $this->redirect('@mission_view?id=' . $mission->getId() . '&add_passengers=' . $group_camp_id);
             } else {
                 $this->redirect('@mission_view?id=' . $mission->getId());
             }
         }
     }
     $this->date_widget = new widgetFormDate(array('format_date' => array('js' => 'mm/dd/yy', 'php' => 'm/d/Y')), array('class' => 'text'));
     $this->time_widget = new widgetFormTime();
     $this->mission = MissionPeer::getByItineraryId($this->itinerary->getId());
     $this->airport_list = AirportPeer::getMappable();
     $this->ground_addresses = $this->getGroundAddresses();
     $this->airlines = AirlinePeer::doSelect(new Criteria());
     $this->funds = FundPeer::doSelect(new Criteria());
 }