Ejemplo n.º 1
0
 /**
  * Mission Leg Edit
  * CODE: 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');
     }
     $this->errors = array();
     // for validation
     $this->orig_set = '';
     $this->dest_set = '';
     if ($request->getParameter('id')) {
         $this->title = 'Edit Mission Leg';
         $this->leg = MissionLegPeer::retrieveByPK($request->getParameter('id'));
         if ($this->leg->getFromAirportId()) {
             $this->fromAirport = $this->leg->getAirportRelatedByFromAirportId();
         }
         if ($this->leg->getToAirportId()) {
             $this->toAirport = $this->leg->getAirportRelatedBytoAirportId();
         }
         if ($this->leg->getCoordinatorId()) {
             $this->coordinator = CoordinatorPeer::retrieveByPK($this->leg->getCoordinatorId());
         }
         //print_r($this->coordinator);
         if ($this->leg->getFboId()) {
             $this->fbo = FboPeer::retrieveByPK($this->leg->getFboId());
             $this->fbo_address = $this->leg->getFboId();
         }
         if ($this->leg->getBackupPilotId()) {
             $this->backup_pilot = PilotPeer::retrieveByPK($this->leg->getBackupPilotId());
             if (isset($this->backup_pilot) && $this->backup_pilot instanceof Pilot) {
                 $this->bp_person = $this->backup_pilot->getMember()->getPerson();
             }
         }
         if ($this->leg->getBackupCopilotId()) {
             $this->backup_co_pilot = PilotPeer::retrieveByPK($this->leg->getBackupCopilotId());
             if (isset($this->backup_co_pilot) && $this->backup_co_pilot instanceof Pilot) {
                 $this->bp_co_person = $this->backup_co_pilot->getMember()->getPerson();
             }
         }
         if ($this->leg->getWaiverReceived()) {
             $this->waiver_rec = $this->leg->getWaiverReceived();
         } else {
             $this->waiver_rec = '';
         }
         if ($this->leg->getFboAddressNew()) {
             $this->new_fbo_address = $this->leg->getFboAddressNew();
         } else {
             $this->new_fbo_address = '';
         }
         $this->date_widget = new widgetFormDate(array('format_date' => array('js' => 'mm/dd/yy', 'php' => 'm/d/Y')), array('class' => 'text'));
         $this->forward404Unless($this->leg);
         $this->mission = $this->leg->getMission();
         $this->passenger = $this->mission->getPassenger();
         $this->person = $this->passenger->getPerson();
         $this->member = MemberPeer::getByPersonId($this->person->getId());
         /*echo "<pre>";
           print_r($this->member );
           */
         /*
          if(isset($this->member) && $this->member instanceof Member){
          $this->pilot = PilotPeer::getByMemberId($this->member->getId());
          }
          /*
          if(isset($this->leg) && $this->leg instanceof MissionLeg){
          $this->pilot = PilotPeer::retrieveByPK($this->leg->getPilotId());
          }
         *
         */
         if (isset($this->leg) && $this->leg instanceof MissionLeg) {
             if ($this->leg->getPilotId()) {
                 $this->pilot = PilotPeer::retrieveByPK($this->leg->getPilotId());
                 $this->pilot_member = MemberPeer::retrieveByPK($this->pilot->getMemberId());
                 $this->copilot = PilotPeer::retrieveByPK($this->leg->getCoPilotId());
                 //echo "<pre>";
                 //print_r($this->mission_assistant);
                 $this->back_up_mission_assistant = PilotPeer::retrieveByPK($this->leg->getBackupMissAssisId());
             }
             if ($this->leg->getMissAssisId()) {
                 $this->mission_assistant = PilotPeer::getByMemberId($this->leg->getMissAssisId());
             }
         }
         //echo "<pre>";
         //print_r($this->pilot_member);
         $is_pilot_requested = PilotRequestPeer::getByLegId($this->leg->getId());
         if (isset($is_pilot_requested) && $is_pilot_requested instanceof PilotRequest) {
             $this->p_req = $is_pilot_requested;
         }
         /*
          echo "<pre>";
          print_r($this->person->getId());
          die();
         */
         $this->forward404Unless($this->mission);
         $mission_id = $this->mission->getId();
     } else {
         $this->title = 'Add Mission Leg';
         $this->leg = new MissionLeg();
         $mission_id = $request->hasParameter('mis') ? $request->getParameter('mis') : $request->getParameter('mission_id');
         $this->mission = MissionPeer::retrieveByPK($mission_id);
         $this->forward404Unless($this->mission);
     }
     if ($request->isMethod('post')) {
         if ($request->getParameter('transportation') == 'air_mission') {
             # AIR MISSION
             if ($request->getParameter('orgin_airport')) {
                 $o_airport = AirportPeer::getByIdent($request->getParameter('orgin_airport'));
                 if (!$o_airport instanceof Airport) {
                     $this->errors[] = 'Origin airport not found in database';
                 }
             } else {
                 $this->errors[] = 'Please specify origin airport';
             }
             if ($request->getParameter('dest_airport')) {
                 $d_airport = AirportPeer::getByIdent($request->getParameter('dest_airport'));
                 if (!$d_airport instanceof Airport) {
                     $this->errors[] = 'Destination airport not found in database';
                 }
             } else {
                 $this->errors[] = 'Please specify destination airport';
             }
             if (count($this->errors) == 0) {
                 $mission_leg = $this->leg;
                 $mission_leg->setMissionId($mission_id);
                 $mission_leg->setCancelled($request->getParameter('cancelled'));
                 if ($mission_leg->isNew()) {
                     $mission_leg->setLegNumber(MissionLegPeer::getMaxLegNumber($mission_id) + 1);
                 }
                 $mission_leg->setFromAirportId($o_airport->getId());
                 $mission_leg->setToAirportId($d_airport->getId());
                 $o_air = AirportPeer::retrieveByPK($o_airport->getId());
                 $d_air = AirportPeer::retrieveByPK($d_airport->getId());
                 $distances = MissionLegPeer::getDistance($o_air->getIdent(), $d_air->getIdent());
                 //$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();
                 $id = $mission_leg->getId();
                 $leg = MissionLegPeer::retrieveByPK($id);
                 $leg->setReverseFrom($id);
                 $leg->save();
             }
         } elseif ($request->getParameter('transportation') == 'ground_mission') {
             # GROUND MISSION
             $origin = $request->getParameter('ground_origin');
             $destination = $request->getParameter('ground_destination');
             $orgintset = $request->getParameter('orig_set');
             $desttset = $request->getParameter('dest_set');
             $fbo_address = $request->getParameter('fbo_address');
             $this->orig_set = $orgintset;
             $this->dest_set = $desttset;
             $this->fbo_address = $fbo_address;
             if (empty($destination) && empty($desttset)) {
                 $this->errors[] = 'Please specify destination address';
             }
             if (empty($origin) && empty($orgintset)) {
                 $this->errors[] = 'Please specify origin address';
             } elseif ($destination == $origin && $destination != '') {
                 $this->errors[] = 'Origin and Destination addresses conflict';
             }
             if (empty($fbo_address)) {
                 $this->errors[] = 'Please specify FBO name';
             }
             if (count($this->errors) == 0) {
                 $mission_leg = $this->leg;
                 $mission_leg->setMissionId($mission_id);
                 if ($mission_leg->isNew()) {
                     $mission_leg->setLegNumber(MissionLegPeer::getMaxLegNumber($mission_id) + 1);
                 }
                 $mission_leg->setPassOnBoard(0);
                 $mission_leg->setTransportation('ground_mission');
                 //get addresses by type
                 $p = $this->passenger = $this->mission->getPassenger();
                 if ($p instanceof Passenger) {
                     $this->person = $p->getPerson();
                 }
                 $this->ground_addresses = array('patient' => '', 'facility' => '', 'lodging' => '');
                 $orgintsetsave = $request->getParameter('ground_origin');
                 if (empty($orgintsetsave)) {
                     $orgintsetsave = $request->getParameter('orig_set');
                 }
                 $desttsetsave = $request->getParameter('ground_destination');
                 if (empty($desttsetsave)) {
                     $desttsetsave = $request->getParameter('dest_set');
                 }
                 $mission_leg->setGroundOrigin($orgintsetsave);
                 $mission_leg->setGroundDestination($desttsetsave);
                 $mission_leg->setFboId($fbo_address);
                 //ziyed
                 $fbo_address_new = $request->getParameter('fbo_address_new');
                 $mission_leg->setFboAddressNew($fbo_address_new);
                 //end ziyed
                 $mission_leg->save();
                 $id = $mission_leg->getId();
                 $leg = MissionLegPeer::retrieveByPK($id);
                 $leg->setReverseFrom($id);
                 $leg->save();
             }
         } elseif ($request->getParameter('transportation') == 'commercial_mission') {
             # COMMERCIAL MISSION
             $airline_id = $request->getParameter('airline_id');
             if ($airline_id) {
                 $custom = $request->getParameter('airline_custom');
                 if ($airline_id == 'other') {
                     if (empty($custom)) {
                         $this->errors[] = 'Please type a new airline name!';
                     }
                 } else {
                     $airline = AirlinePeer::retrieveByPK($airline_id = $request->getParameter('airline_id'));
                     if (!$airline instanceof Airline) {
                         $this->errors[] = 'Please select airline!';
                     }
                 }
             } else {
                 $this->errors[] = 'Please select airline!';
             }
             $origin = $request->getParameter('origin');
             $destination = $request->getParameter('destination');
             if (empty($origin)) {
                 $this->errors[] = 'Please specify origin';
             }
             if (empty($destination)) {
                 $this->errors[] = 'Please specify destination';
             }
             if (count($this->errors) == 0) {
                 $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);
                 }
                 $flight_number = $request->getParameter('flight_number');
                 $departure = $request->getParameter('departure');
                 $arrival = $request->getParameter('arrival');
                 $mission_leg = $this->leg;
                 $mission_leg->setMissionId($this->mission->getId());
                 if ($mission_leg->isNew()) {
                     $mission_leg->setLegNumber(MissionLegPeer::getMaxLegNumber($mission_id) + 1);
                 }
                 $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($destination);
                 $mission_leg->setFlightNumber($flight_number);
                 $v = $departure;
                 if (empty($v[0]['hour']) || empty($v[0]['minute'])) {
                     $v = null;
                 }
                 $mission_leg->setDeparture($v ? strtotime($v[0]['hour'] . ':' . $v[0]['minute'] . ' ' . $v[0]['period']) : null);
                 $v = $arrival;
                 if (empty($v[0]['hour']) || empty($v[0]['minute'])) {
                     $v = null;
                 }
                 $mission_leg->setArrival($v ? strtotime($v[0]['hour'] . ':' . $v[0]['minute'] . ' ' . $v[0]['period']) : null);
                 $mission_leg->setTransportation('commercial_mission');
                 $mission_leg->save();
             }
         } else {
             $this->forward404();
         }
         if (count($this->errors) == 0) {
             $this->getUser()->setFlash('success', 'New Mission leg has successfully created!');
             if ($request->getParameter('add_another')) {
                 $this->redirect('@leg_create?mis=' . $this->mission->getId());
             } else {
                 $this->redirect('@mission_view?id=' . $this->mission->getId());
             }
         }
     }
     $this->date_widget = new widgetFormDate(array('format_date' => array('js' => 'mm/dd/yy', 'php' => 'm/d/Y')), array('class' => 'text'));
     //echo '<pre>';print_r($this->date_widget);
     $this->time_widget = new widgetFormTime();
     $this->airport_list = AirportPeer::getMappable();
     $this->airlines = AirlinePeer::doSelect(new Criteria());
     $this->funds = FundPeer::doSelect(new Criteria());
     $p = $this->passenger = $this->mission->getPassenger();
     if ($p instanceof Passenger) {
         $this->person = $p->getPerson();
     } else {
         unset($this->passenger);
     }
     $this->itinerary = $this->mission->getItinerary();
     //echo '<pre>';print_r($this->itinerary);
     // Pre-define addresses for ground missions
     $this->ground_addresses = array('patient' => '', 'facility' => '', 'lodging' => '', 'airport' => '');
     $this->ground_addr_sel = sfConfig::get('app_ground_address_type', array());
     if ($this->itinerary) {
         $this->ground_addresses['lodging'] = $this->ground_addresses['facility'] = $this->itinerary->getDestCity() . ', ' . $this->itinerary->getDestState();
     }
     if ($this->passenger) {
         $this->ground_addresses['lodging'] = $this->passenger->getLodgingName() . ' ' . $this->ground_addresses['lodging'];
         $this->ground_addresses['facility'] = $this->passenger->getFacilityName() . ' ' . $this->ground_addresses['facility'];
         $this->ground_addresses['patient'] = $this->person->getAddress1() . ' ' . $this->person->getAddress2() . ' ' . $this->person->getCity() . ', ' . $this->person->getState() . ' ' . $this->person->getZipcode();
     }
 }