コード例 #1
0
ファイル: actions.class.php プロジェクト: yasirgit/afids
 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);
 }