Created
December 23, 2016 13:49
-
-
Save gabcarvalhogama/9c6ea6e93e81f507d64eb2d3a5a0c44b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require_once("DB.class.php"); | |
| class Commerce extends DB{ | |
| public $ip; | |
| public function getCommerceList($lat, $lng, $ct = null){ | |
| if(!empty($ct)): | |
| $ct = strip_tags(addslashes($ct)); | |
| $ct = "categoriesCommerce Like '%{$ct}%' AND"; | |
| endif; | |
| $sql = DB::open()->prepare("SELECT idCommerce, nameCommerce, categoriesCommerce, status, | |
| (6371 * acos( | |
| cos(radians(:lat)) | |
| * cos(radians(latitudeCommerce)) | |
| * cos(radians(longitudeCommerce) - radians(:lng)) | |
| + sin(radians(:lat)) | |
| * sin(radians(latitudeCommerce)) | |
| ) | |
| ) AS distance | |
| FROM zt_commerce WHERE {$ct} status = 1 | |
| HAVING distance < 25 | |
| ORDER BY distance ASC"); | |
| $sql->bindValue("lat", $lat); | |
| $sql->bindValue("lng", $lng); | |
| $sql->execute(); | |
| return $sql; | |
| } | |
| public function getCommerceRanking($lat, $lng){ | |
| $sql = DB::open()->prepare("SELECT idCommerce, nameCommerce, categoriesCommerce, status, | |
| (6371 * acos( | |
| cos(radians(:lat)) | |
| * cos(radians(latitudeCommerce)) | |
| * cos(radians(longitudeCommerce) - radians(:lng)) | |
| + sin(radians(:lat)) | |
| * sin(radians(latitudeCommerce)) | |
| ) | |
| ) AS distance | |
| FROM zt_commerce WHERE status = 1 | |
| HAVING distance < 25 | |
| ORDER BY distance ASC"); | |
| $sql->bindValue("lat", $lat); | |
| $sql->bindValue("lng", $lng); | |
| $sql->execute(); | |
| $CommerceList = []; | |
| while($ftc = $sql->fetchObject()){ | |
| $photo = $this->getPhotos($ftc->idCommerce, "LIMIT 1")->fetchObject(); | |
| $photo = ($photo == null) ? "" : $photo->url; | |
| $evaluationMedia = $this->getEvaluationMedia($ftc->idCommerce); | |
| $evaluationMedia = ($evaluationMedia == null) ? 0 : $evaluationMedia; | |
| #Format float $ftc->distance | |
| $ftc->distance = (float) number_format($ftc->distance, 3); | |
| $arr = array( | |
| "idCommerce" => $ftc->idCommerce, | |
| "nameCommerce" => $ftc->nameCommerce, | |
| "photo" => $photo, | |
| "distance" => $ftc->distance, | |
| "evaluationMedia" => $evaluationMedia | |
| ); | |
| array_push($CommerceList, $arr); | |
| } | |
| return $CommerceList; | |
| } | |
| public function getPhotos($idCommerce, $limit = null){ | |
| $sql = DB::open()->prepare("SELECT url, idCommercePhoto FROM zt_photos WHERE idCommercePhoto = :idCommercePhoto {$limit}"); | |
| $sql->bindValue("idCommercePhoto", $idCommerce); | |
| $sql->execute(); | |
| return $sql; | |
| } | |
| public function getPhones($idCommerce){ | |
| $sql = DB::open()->prepare("SELECT telephone, idCommerceTelephone FROM zt_telephone WHERE idCommerceTelephone = ?"); | |
| $sql->execute(array($idCommerce)); | |
| return $sql; | |
| } | |
| public function getPayment($idCommerce){ | |
| $sql = DB::open()->prepare("SELECT typePayment, idCommercePayment FROM zt_payment WHERE idCommercePayment = ?"); | |
| $sql->execute(array($idCommerce)); | |
| return $sql; | |
| } | |
| public function getIfVoted($idCommerce){ | |
| $sql = DB::open()->prepare("SELECT idCommerce, ip FROM zt_stars WHERE idCommerce = ? AND ip = ?"); | |
| $sql->execute(array($idCommerce, $this->ip)); | |
| return $sql; | |
| } | |
| #Evaluation | |
| public function getEvaluationMedia($idCommerce){ | |
| $sql = DB::open()->prepare("SELECT AVG(stars) FROM zt_stars WHERE idCommerce = ? LIMIT 1"); | |
| $sql->execute(array($idCommerce)); | |
| $mediaStars = $sql->fetchAll(); | |
| return (int) $mediaStars[0]['AVG(stars)']; | |
| } | |
| public function addEvaluation($idCommerce, $stars){ | |
| if($this->getIfVoted($idCommerce)->rowCount() == 0){ | |
| $sql = DB::open()->prepare("INSERT INTO zt_stars VALUES (default, ?, ?, ?)"); | |
| if($sql->execute(array($idCommerce, $stars, $this->ip))){ | |
| return 1; | |
| } | |
| }else{ | |
| die("Você já avaliou este comércio."); | |
| } | |
| } | |
| public function viewCommerce($idCommerce){ | |
| $sql = DB::open()->prepare("SELECT * FROM zt_commerce WHERE idCommerce = :idCommerce"); | |
| $sql->bindValue("idCommerce", $idCommerce); | |
| $sql->execute(); | |
| return $sql; | |
| } | |
| public function addVisit($idCommerce){ | |
| $sql = DB::open()->prepare("SELECT commerceVisited, ipVisit FROM zt_visits WHERE commerceVisited = ? AND ipVisit = ?"); | |
| $sql->execute(array($idCommerce, $this->ip)); | |
| if($sql->rowCount() == 0){ | |
| unset($sql); | |
| $sql = DB::open()->prepare("INSERT INTO zt_visits VALUES (default, ?, ?)"); | |
| $sql->execute(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment