public function executeAutoAddMissionsOnCamp(sfWebRequest $request) { // see if there is airport exist with this name $airport_name = $request->getParameter('airportname'); $airport = AirportPeer::getBySpecificAirportName($airport_name); // If there is no airport then insert it into airport table if (!$airport) { // @TODO Set default airport fields values $airport = new Airport(); $airport->setName($airport_name); $airport->save(); } $pass_id = $request->getParameter('passenger'); $camp_id = $request->getParameter('camp_id'); $camp_passenger = new CampPassenger(); $camp_passenger->setCampId($camp_id); $camp_passenger->setPassengerId($pass_id); if (CampPassengerPeer::doCount($camp_passenger->buildCriteria())) { $passenger = PassengerPeer::retrieveByPK($pass_id); $this->getUser()->setFlash('warning', 'Passenger "' . $passenger->getPerson()->getName() . '" has already been added!'); return $this->redirect('camp/view?id=' . $request->getParameter('camp_id')); } $camp = CampPeer::retrieveByPK($camp_id); $this->forward404Unless($camp); $note = $request->getParameter('note'); //$camp_passenger->setAirportId($camp->getAirportId ()); // Camp Location Id $camp_passenger->setNote($note); //if ($camp_passenger->save()){ //return $this->renderText($airport->getId()); //} $mission_type = MissionTypePeer::getByName('normal'); if (!$mission_type instanceof MissionType) { $mission_type = MissionTypePeer::doSelectOne(new Criteria()); $this->forward404Unless($mission_type); } $airport = AirportPeer::getBySpecificAirportName($airport_name); $camp_location = $camp->getAirport(); $itinerary = new Itinerary(); $itinerary->setCampId($camp->getId()); $itinerary->setPassengerId($pass_id); $itinerary->setDateRequested(time()); $itinerary->setApointTime('unspecified'); $itinerary->setMissionTypeId($mission_type->getId()); $itinerary->setAgencyId($camp->getAgencyId()); $itinerary->setCancelItinerary(1); $itinerary->save(); // Mission 1 $mission1 = new Mission(); $mission1->setItineraryId($itinerary->getId()); $mission1->setCampId($camp->getId()); $mission1->setCancelMission(1); $mission1->setPassengerId($pass_id); $mission1->setMissionCount(1); $mission1->setDateRequested(time()); $mission1->setExternalId($this->getLatestExternalId()); $mission1->setMissionTypeId($mission_type->getId()); $mission1->save(); $camp_passenger->setReturnAirportId(NULL); // this indicates that return airport id would be camp airport id $camp_passenger->setAirportId($airport->getId()); $camp_passenger->setMissionId($mission1->getId()); $camp_passenger->save(); $mission1_leg1 = new MissionLeg(); $mission1_leg1->setFromAirportId($airport->getId()); $mission1_leg1->setLegNumber(1); $mission1_leg1->setMissionId($mission1->getId()); if ($camp_location) { $mission1_leg1->setToAirportId($camp_location->getId()); } else { $mission1_leg1->setToAirportId(NULL); } $mission1_leg1->setCancelMissionLeg(1); $mission1_leg1->save(); // Mission 2 $mission2 = new Mission(); $mission2->setItineraryId($itinerary->getId()); $mission2->setCampId($camp->getId()); $mission2->setCancelMission(1); $mission2->setPassengerId($pass_id); $mission2->setMissionCount(2); // From treatment to hme $mission2->setDateRequested(time()); $mission2->setExternalId($this->getLatestExternalId()); $mission2->setMissionTypeId($mission_type->getId()); $mission2->save(); $camp_passenger2 = new CampPassenger(); $camp_passenger2->setCampId($camp_id); $camp_passenger2->setPassengerId($pass_id); $camp_passenger2->setMissionId($mission2->getId()); $camp_passenger2->setAirportId(NULL); // this indicates that return airport id would be camp airport id $camp_passenger2->setReturnAirportId($airport->getId()); $camp_passenger2->setNote($note); $camp_passenger2->save(); $mission2_leg2 = new MissionLeg(); if ($camp_location) { $mission2_leg2->setFromAirportId($camp_location->getId()); } else { $mission2_leg2->setFromAirportId(NULL); } $mission2_leg2->setLegNumber(1); $mission2_leg2->setMissionId($mission2->getId()); $mission2_leg2->setToAirportId($airport->getId()); $mission2_leg2->setCancelMissionLeg(1); $mission2_leg2->save(); return $this->redirect('camp/view?id=' . $camp->getId()); }
/** * 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()); }