<?php

class Populardirect {

    const TIME_DELTA = 180; // min (3 hrs)
    const TIME_LIMIT = 1262322000; // 2010-01-01
    const DEFAULT_COUNTRY = 'SG';
    const DEFAULT_OPTIONS = 3;
    const CITY_CURVATURE = 1.25;
    const CITY_SPEED = 0.667; // km/min (0.0667 = 40 km/h)
    const POPULAR_RADIUS = 100000; // 100km

    public function getPopularDirect($db, $gpsdb, $member_id, $lat, $lng, $time, $time_delta = self::TIME_DELTA, $radius = self::POPULAR_RADIUS, $country = self::DEFAULT_COUNTRY) {
        error_log('Populardirect::getPopular()');
        $result = [];

        $limit_lat = (float) $lat;
        $limit_lng = (float) $lng;
        $limit_rad = $radius;

        //$q = "SELECT a.id,a.attraction,b.address,b.latitude,b.longitude FROM tourist_attraction a, address b WHERE b.id=a.address_id and b.country='US' and b.postal like '30%' ";
        $q = "SELECT a.id,a.attraction,b.address,b.latitude,b.longitude "
                . " FROM tourist_attraction a, address b "
                . " WHERE b.id=a.address_id "
                . " AND ST_DWithin(b.geometry,ST_SetSRID(ST_MakePoint(${limit_lng},${limit_lat}),4326)::geography,${limit_rad})"
                . " ORDER BY ST_DistanceSphere(b.geometry::geometry, ST_SetSRID(ST_MakePoint(${limit_lng},${limit_lat}),4326))";

        $r = pg_query($db, $q);
        if ($r && pg_num_rows($r) && $f = pg_fetch_assoc($r)) {
      			$f["address"] = html_entity_decode ($f["address"],ENT_QUOTES|ENT_HTML5,"UTF-8");
            $result[] = $f;
        }

        while ($f = pg_fetch_assoc($r)) {
						$f["address"] = html_entity_decode ($f["address"],ENT_QUOTES|ENT_HTML5,"UTF-8");
            $result[] = $f;
        }
        if (count($result)) {
            return [$result, NULL];
        }
        return [NULL, "No popular trips nearby found"];
    }

}

// vi:ts=2

