/** * Add new airport from pop up by ajax * CODE:airport_create */ public function executeUpdateAjax(sfWebRequest $request) { # security if (!$this->getUser()->hasCredential(array('Administrator'), false)) { $this->getUser()->setFlash("warning", 'You don\'t have permission to access this url ' . $request->getReferer()); $this->redirect('dashboard/index'); } // $this->setLayout(false); $airport = new Airport(); $this->airport = $airport; $this->form = new AirportForm($airport); $this->back = $request->getReferer(); if ($request->isMethod('post')) { $this->referer = $request->getReferer(); $this->form->bind($request->getParameter('airp')); if ($this->form->isValid() && $this->form->getValue('ident')) { if ($this->form->getValue('ident') && $request->getParameter('id')) { $is_used = AirportPeer::getByIdent($this->form->getValue('ident')); $airport->setIdent($is_used->getIdent()); } elseif ($this->form->getValue('ident') && !$request->getParameter('id')) { //new $is_used = AirportPeer::getByIdent($this->form->getValue('ident')); if (isset($is_used)) { if ($is_used->getIdent() == $this->form->getValue('ident')) { if ($request->getParameter('back') == null) { $last = $request->getReferer(); } else { $last = $request->getParameter('back'); } if (strstr($last, 'camp/create')) { $back_url = $last; } elseif (strstr($last, 'fbo/create')) { $back_url = $last; } else { $back_url = 'airport'; } $this->getUser()->setFlash('success', 'This Airport Ident has already used. Please confirm else!'); $this->redirect($back_url); } } elseif ($this->form->getValue('ident') == 'null') { $airport->setIdent(null); } else { $airport->setIdent($this->form->getValue('ident')); } } $airport->setName($this->form->getValue('name')); $airport->setCity($this->form->getValue('city')); $airport->setState($this->form->getValue('state')); $airport->setLatitude($this->form->getValue('latitude')); $airport->setLongitude($this->form->getValue('longitude')); $airport->setRunwayLength($this->form->getValue('runway_length')); if ($this->form->getValue('wing_id') == 0) { $airport->setWingId(null); } else { $airport->setWingId($this->form->getValue('wing_id')); } $airport->setGmtOffset($this->form->getValue('gmt_offset')); $airport->setDstOffset($this->form->getValue('dst_offset')); $airport->setZipcode($this->form->getValue('zipcode')); if ($this->form->getValue('closed') == null) { $airport->setClosed(0); } else { $airport->setClosed($this->form->getValue('closed')); } if ($airport->isNew()) { $content = $this->getUser()->getName() . ' added new Airport: ' . $airport->getName() . ' (' . $airport->getIdent() . ')'; ActivityPeer::log($content); } $airport->save(); $str = '<script type="text/javascript">' . 'var a = $id;' . "window.\$('#back_airport_id').val('');" . "window.\$('#back_airport_id').val(a);" . "window.\$('#back1').val(\$('#camp_agency_id').val());" . "window.location.reload();" . '</script>'; $this->getUser()->setFlash('success', 'New Airport created successfully!'); $this->formValid = true; $this->airportName = $airport->getName(); //return $this->renderText($str); } else { $str = '<script type="text/javascript">' . "//window.setTimeout(\\'window.location.reload()\\', 1500);" . 'window.location.reload();' . '</script>'; $this->getUser()->setFlash('success', 'Can not create new Airport without datas!'); $this->formNotValid = true; //return $this->renderText($str); } } else { # Set referer URL $this->referer = $request->getReferer() ? $request->getReferer() : '@airport'; } $this->airport = $airport; }
public function executeCalculateDistance(sfWebRequest $request) { $origin_ = $request->getParameter('origin'); $destination_ = $request->getParameter('destination'); $origin = array(); $destination = array(); if (empty($origin_)) { $origin[0] = '*'; } else { if (strstr($origin_, ',')) { $origin = explode(',', $origin_); } else { $origin[0] = $origin_; } } if (empty($destination_)) { $destination[0] = '*'; } else { if (strstr($destination_, ',')) { $destination = explode(',', $destination_); } else { $destination[0] = $destination_; } } // find airports # origin // $c = new Criteria(); // if (is_array($origin)) { // if (count($origin) != 3) return $this->renderText('please format: city, state, zipcode'); // $c->add(AirportPeer::CITY, trim($origin[0])); // $c->add(AirportPeer::STATE, trim($origin[1])); // $c->add(AirportPeer::ZIPCODE, trim($origin[2])); // }else{ // if (empty($origin)) return $this->renderText('please enter origin'); // $c->add(AirportPeer::IDENT, $origin); // } // $origin_airport = AirportPeer::doSelectOne($c); // if (!($origin_airport instanceof Airport)) return $this->renderText('origin airport not found'); // // # destination // $c = new Criteria(); // if (is_array($destination)) { // if (count($destination) != 3) return $this->renderText('please format: city, state, zipcode'); // $c->add(AirportPeer::CITY, trim($destination[0])); // $c->add(AirportPeer::STATE, trim($destination[1])); // $c->add(AirportPeer::ZIPCODE, trim($destination[2])); // }else{ // if (empty($destination)) return $this->renderText('please enter destination'); // $c->add(AirportPeer::IDENT, $destination); // } // $destination_airport = AirportPeer::doSelectOne($c); // if (!($destination_airport instanceof Airport)) return $this->renderText('origin airport not found'); /* Edited By Masum , LAST EDITED BY ZIYED*/ $flag = 0; $c = new Criteria(); if (is_array($origin)) { switch (count($origin)) { case 3: $city_name = trim($origin[0]); $state_name = trim($origin[1]); $zip_code = trim($origin[2]); $sql = "SELECT * FROM afids.zipcode WHERE zipcode.city ='" . $city_name . "' AND zipcode.state ='" . $state_name . "' AND zipcode.zipcode ='" . $zip_code . "' limit 1"; $ident = 0; break; case 2: $city_name = trim($origin[0]); $state_name = trim($origin[1]); $sql = "SELECT * FROM afids.zipcode WHERE zipcode.city ='" . $city_name . "' AND zipcode.state ='" . $state_name . "' limit 1"; $ident = 0; break; case 1: //trace city $cityname = trim($origin[0]); $query = "select * from afids.zipcode where zipcode.city ='" . $cityname . "'"; $conn = Propel::getConnection(); $statement = $conn->prepare($query); $statement->execute(); $row = $statement->fetch(); if (!empty($row)) { $city = 1; } else { $city = 0; } //trace state $statename = trim($origin[0]); $query = "select * from afids.zipcode where zipcode.state ='" . $statename . "'"; $conn = Propel::getConnection(); $statement = $conn->prepare($query); $statement->execute(); $row = $statement->fetch(); if (!empty($row)) { $state = 1; } else { $state = 0; } //trace zip $zipcode = trim($origin[0]); $query = "select * from afids.zipcode where zipcode.zipcode ='" . $zipcode . "'"; $conn = Propel::getConnection(); $statement = $conn->prepare($query); $statement->execute(); $row = $statement->fetch(); if (!empty($row)) { $zip = 1; } else { $zip = 0; } //trace airport $airportident = trim($origin[0]); $query = "select * from afids.airport where airport.ident ='" . $airportident . "'"; $conn = Propel::getConnection(); $statement = $conn->prepare($query); $statement->execute(); $row = $statement->fetch(); if (!empty($row)) { $ident = 1; } else { $ident = 0; } if ($city == 1) { $sql = "SELECT * FROM afids.zipcode WHERE zipcode.city ='" . $cityname . "' limit 1 "; } else { if ($state == 1) { $sql = "SELECT * FROM afids.zipcode WHERE zipcode.state ='" . $statename . "' limit 1"; } else { if ($zip == 1) { $sql = "SELECT * FROM afids.zipcode WHERE zipcode.zipcode ='" . $zipcode . "' limit 1"; } else { if ($ident == 1) { $sql = "SELECT * FROM afids.airport WHERE airport.ident ='" . $airportident . "'"; } else { $flag = 1; } } } } break; default: return $this->renderText('please format: city, state, zipcode'); break; } //$origin_airport = AirportPeer::doSelectOne($c); } else { if (empty($origin)) { return $this->renderText('please enter origin'); } $flag = 1; } if ($ident == 1) { $conn = Propel::getConnection(); $statement = $conn->prepare($sql); $statement->execute(); $row = $statement->fetch(); if (empty($row)) { return $this->renderText('origin airport not found'); } $origin_airport = new Airport(); $origin_airport->setCity($row['city']); $origin_airport->setIdent($row['ident']); $origin_airport->setState($row['state']); $origin_airport->setLatitude($row['latitude']); $origin_airport->setLongitude($row['longitude']); } else { if ($flag != 1) { $conn = Propel::getConnection(); $statement = $conn->prepare($sql); $statement->execute(); $row = $statement->fetch(); if (empty($row)) { return $this->renderText('origin airport not found'); } $origin_airport = new Zipcode(); $origin_airport->setCity($row['city']); $origin_airport->setState($row['state']); $origin_airport->setLatitude($row['latitude']); $origin_airport->setLongitude($row['longitude']); } else { return $this->renderText('origin airport not found'); } } //if (!($origin_airport instanceof Airport)) return $this->renderText('origin airport not found'); $flag = 0; # destination $c = new Criteria(); if (is_array($destination)) { switch (count($destination)) { case 3: $city_name_ = trim($destination[0]); $state_name_ = trim($destination[1]); $zip_code_ = trim($destination[2]); $sql = "SELECT * FROM afids.zipcode WHERE zipcode.city ='" . $city_name_ . "' AND zipcode.state ='" . $state_name_ . "' AND zipcode.zipcode ='" . $zip_code_ . "' limit 1"; $identt = 0; break; case 2: $city_name_ = trim($destination[0]); $state_name_ = trim($destination[1]); $sql = "SELECT * FROM afids.zipcode WHERE zipcode.city ='" . $city_name_ . "' AND zipcode.state ='" . $state_name_ . "' limit 1"; $identt = 0; break; case 1: //trace city $dest_city = trim($destination[0]); $query = "select * from afids.zipcode where zipcode.city ='" . $dest_city . "'"; $conn = Propel::getConnection(); $statement = $conn->prepare($query); $statement->execute(); $row = $statement->fetch(); if (!empty($row)) { $city = 1; } else { $city = 0; } //trace state $dest_state = trim($destination[0]); $query = "select * from afids.zipcode where zipcode.state ='" . $dest_state . "'"; $conn = Propel::getConnection(); $statement = $conn->prepare($query); $statement->execute(); $row = $statement->fetch(); if (!empty($row)) { $state = 1; } else { $state = 0; } //trace zip $dest_zip = trim($destination[0]); $query = "select * from afids.zipcode where zipcode.zipcode ='" . $dest_zip . "'"; $conn = Propel::getConnection(); $statement = $conn->prepare($query); $statement->execute(); $row = $statement->fetch(); if (!empty($row)) { $zip = 1; } else { $zip = 0; } //trace airport $dest_airport = trim($destination[0]); $query = "select * from afids.airport where airport.ident ='" . $dest_airport . "'"; $conn = Propel::getConnection(); $statement = $conn->prepare($query); $statement->execute(); $row = $statement->fetch(); if (!empty($row)) { $identt = 1; } else { $identt = 0; } if ($city == 1) { $sql = "SELECT * FROM afids.zipcode WHERE zipcode.city ='" . $dest_city . "' limit 1"; } else { if ($state == 1) { $sql = "SELECT * FROM afids.zipcode WHERE zipcode.state ='" . $dest_state . "' limit 1"; } else { if ($zip == 1) { $sql = "SELECT * FROM afids.zipcode WHERE zipcode.zipcode ='" . $dest_zip . "' limit 1"; } else { if ($identt == 1) { $sql = "SELECT * FROM afids.airport WHERE airport.ident ='" . $dest_airport . "'"; } else { $flag = 1; } } } } break; default: return $this->renderText('please format: city, state, zipcode'); break; } } else { if (empty($destination)) { return $this->renderText('please enter destination'); } $flag = 1; } if ($identt == 1) { $conn = Propel::getConnection(); $statement = $conn->prepare($sql); $statement->execute(); $row = $statement->fetch(); if (empty($row)) { return $this->renderText('destination airport not found'); } $destination_airport = new Airport(); $destination_airport->setCity($row['city']); $destination_airport->setIdent($row['ident']); $destination_airport->setState($row['state']); $destination_airport->setLatitude($row['latitude']); $destination_airport->setLongitude($row['longitude']); } else { if ($flag != 1) { $conn = Propel::getConnection(); $statement = $conn->prepare($sql); $statement->execute(); $row = $statement->fetch(); if (empty($row)) { return $this->renderText('destination airport not found'); } $destination_airport = new Zipcode(); $destination_airport->setCity($row['city']); $destination_airport->setState($row['state']); $destination_airport->setLatitude($row['latitude']); $destination_airport->setLongitude($row['longitude']); } else { return $this->renderText('destination airport not found'); } } //if (!($destination_airport instanceof Airport)) return $this->renderText('destination airport not found'); /* End Edited By Masum , LAST EDITED BY ZIYED*/ $distance = round(acos(sin(deg2rad($origin_airport->getLatitude())) * sin(deg2rad($destination_airport->getLatitude())) + cos(deg2rad($origin_airport->getLatitude())) * cos(deg2rad($destination_airport->getLatitude())) * cos(deg2rad($origin_airport->getLongitude()) - deg2rad($destination_airport->getLongitude()))) * (180 * 60) / pi()); if ($ident == 1) { $html = 'Origin: ' . $origin_airport->getIdent() . ' (' . $origin_airport->getCity() . ', ' . $origin_airport->getState() . ')'; } else { $html = 'Origin: (' . $origin_airport->getCity() . ', ' . $origin_airport->getState() . ')'; } $html .= '<br/>'; if ($identt == 1) { $html .= 'Destination: ' . $destination_airport->getIdent() . ' (' . $destination_airport->getCity() . ', ' . $destination_airport->getState() . ')'; } else { $html .= 'Destination: (' . $destination_airport->getCity() . ', ' . $destination_airport->getState() . ')'; } $html .= '<br/>'; $html .= 'Distance: ' . $distance . ' nautical miles'; return $this->renderText($html); }