zeg1jeux/lib/Gchange.class.php

280 lines
5.3 KiB
PHP

<?php
require_once('DAO.class.php');
class Gchange {
private $dao;
private $cacheDir = __DIR__ . '/../cache/';
private $isActivatedCache = true;
private $cacheLongevity = array(
'placesNearby' => 604800, // 3 jours
'users' => 43200, // 12 heures
'records' => 900 // 15 min
);
public function __construct () {
$this->dao = DAO::getInstance();
}
public function getRecordsByIssuer ($issuer) {
$recordsCacheDir = 'records/user/';
$recordsCacheFile = $issuer . '.json';
$json = $this->getJsonFromCache($recordsCacheDir, $recordsCacheFile, $this->cacheLongevity['records']);
if (empty($json)) {
$n = 20;
$queryParams = [
'size' => $n,
'query' => [
'bool' =>[
'must' => [
'term' => [
'issuer' => $issuer
]
],
'must_not' => [
[ "term" => ["stock" => 0] ]
]
]
],
'sort' => [
['time' => 'desc']
]
];
$json = $this->dao->fetchJson('/market/record/_search', 'gchange', $queryParams);
$this->cacheJson($recordsCacheDir, $recordsCacheFile, $json);
}
$result = json_decode($json);
return $result->hits->hits;
}
private function getJsonFromCache ($dir, $file, $cacheLongevity) {
if (!$this->isActivatedCache) {
return NULL;
}
$json = null;
$jsonFullPath = $this->cacheDir . $dir . $file;
if (file_exists($jsonFullPath) and ((time() - filemtime($jsonFullPath)) < $cacheLongevity)) {
$json = file_get_contents($jsonFullPath);
}
return $json;
}
private function cacheJson ($dir, $file, $json) {
if (!$this->isActivatedCache) {
return NULL;
}
$jsonCacheDir = $this->cacheDir . $dir;
$jsonFullPath = $jsonCacheDir . $file;
if (!file_exists($jsonCacheDir)) {
mkdir($jsonCacheDir, 0777, true);
}
file_put_contents($jsonFullPath, $json);
}
public function getUser ($pubkey) {
$usersCacheDir = 'users/';
$usersCacheFile = $pubkey . '.json';
$json = $this->getJsonFromCache($usersCacheDir, $usersCacheFile, $this->cacheLongevity['users']);
if (empty($json)) {
$json = $this->dao->fetchJson(('/user/profile/'. $pubkey), 'gchange');
$this->cacheJson($usersCacheDir, $usersCacheFile, $json);
}
$result = json_decode($json);
return $result;
}
public function getPlacesNearUser ($user, $radius) {
$placesCacheDir = 'places-nearby/user/';
$placesCacheFile = $user->_id . '.json';
$json = $this->getJsonFromCache($placesCacheDir, $placesCacheFile, $this->cacheLongevity['placesNearby']);
if (empty($json)) {
$json = $this->getNearbyPlacesJson($user->_source->geoPoint->lat, $user->_source->geoPoint->lon, RADIUS);
$this->cacheJson($placesCacheDir, $placesCacheFile, $json);
}
$result = json_decode($json);
return $result->hits->hits;
}
public function getNearbyPlaces ($lat, $lon, $maxDistance, $minDistance = NULL) {
$json = $this->getNearbyPlacesJson($lat, $lon, $maxDistance, $minDistance);
$result = json_decode($json);
return $result->hits->hits;
}
public function getNearbyPlacesJson ($lat, $lon, $maxDistance, $minDistance = NULL) {
$n = 20;
$queryParams = [
'size' => $n,
'query' => [
'bool' => [
'must' => [
[
'geo_distance' => [
"distance" => $maxDistance . 'km',
"geoPoint"=> [
"lat" => $lat,
"lon" => $lon
]
]
]
]
]
]
];
$json = $this->dao->fetchJson('/page/record/_search', 'gchange', $queryParams);
return $json;
}
public function getNearbyRecords ($lat, $lon, $maxDistance, $minDistance = NULL) {
$n = 20;
$queryParams = [
'size' => $n,
'query' => [
'bool' => [
'must' => [
[
'geo_distance' => [
"distance" => $maxDistance . 'km',
"geoPoint"=> [
"lat" => $lat,
"lon" => $lon
]
]
], [
'range' => [
'stock' => [
'gte' => 1
]
]
]
]
]
]
];
$json = $this->dao->fetchJson('/market/record/_search?pretty', 'gchange', $queryParams);
$result = json_decode($json);
return $result->hits->hits;
}
public function getImmaterialRecords () {
$n = 20;
$queryParams = [
'size' => $n,
'query' => [
'nested' => [
'path' => 'category',
'query' => [
'bool' => [
'should' => [
[ 'term' => [ 'category.parent' => 'cat31' ] ],
[ 'term' => [ 'category.id' => 'cat31' ] ],
[ 'term' => [ 'category.parent' => 'cat74' ] ],
[ 'term' => [ 'category.id' => 'cat74' ] ]
],
'must_not' => [
[ "term" => ["category.id" => "cat65"] ]
]
]
]
]
]
];
$json = $this->dao->fetchJson('/market/record/_search?pretty', 'gchange', $queryParams);
$result = json_decode($json);
return $result->hits->hits;
}
public function getShippable () {
$n = 20;
$queryParams = [
'size' => $n,
'query' => [
'match' => [
'description' => 'envoi possible'
]
]
];
$json = $this->dao->fetchJson('/market/record/_search?pretty', 'gchange', $queryParams);
$result = json_decode($json);
return $result->hits->hits;
}
public function getHousingOffers () {
}
public function getShippableOffers () {
}
}