Blame view

php/classes/EpnTapMgr.php 2.78 KB
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
1
2
<?php
/**  @class EpnTapMgr
210a3164   Nathanael Jourdane   Store actual func...
3
*	@brief Manager to communicates with EPN-TAP services.
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
4
5
6
*/

class EpnTapMgr {
f534c4a8   Nathanael Jourdane   Make the VOTable ...
7
8
9
	private $logEpnTap;

	function __construct()
210a3164   Nathanael Jourdane   Store actual func...
10
11
	{
		$this->logEpnTap = fopen(LOG_DIR . "epnTap.log", "a");
f534c4a8   Nathanael Jourdane   Make the VOTable ...
12
		$this->addLog("\n---\n");
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
13
14
	}

210a3164   Nathanael Jourdane   Store actual func...
15
16
17
18
	private function addLog($msg)
	{
		fwrite($this->logEpnTap, date("h:i:s A") . ": " . $msg . "\n");
	}
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
19

210a3164   Nathanael Jourdane   Store actual func...
20
	private function dateToJD($gregorian_date) {
67a4e0da   Nathanael Jourdane   Add a function to...
21
22
		list($day, $month, $year, $hours, $minutes, $seconds) = split('[/ :]', $gregorian_date);
		return GregorianToJD($month, $day, $year) + $hours/24 + $minutes/(24*60) + $seconds/(24*60*60);
210a3164   Nathanael Jourdane   Store actual func...
23
	}
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
24

210a3164   Nathanael Jourdane   Store actual func...
25
26
	public function JDTodate($jd) {
		list($month, $day, $year) = split('/', JDToGregorian($jd));
67a4e0da   Nathanael Jourdane   Add a function to...
27
28
		$date = sprintf('%02d', $day) . '/' . sprintf('%02d', $month) . '/' . sprintf('%04d', $year);
		return ($date == '00/00/0000') ? '' : $date;
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
29
30
	}

47a2829d   Nathanael Jourdane   Fix the paginatio...
31
	public function createFilter($dataproduct_type, $target_name, $time_min, $time_max) {
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
32
		$filter = array();
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
33
34
		if($dataproduct_type)
			array_push($filter, "dataproduct_type = '$dataproduct_type'");
47a2829d   Nathanael Jourdane   Fix the paginatio...
35
36
		if($target_name)
			array_push($filter, "target_name = '$target_name'");
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
37
		if($time_min)
d53e0fe6   Nathanael Jourdane   Move EPN-TAP modu...
38
			array_push($filter, "time_min >= " . $this->dateToJD($time_min));
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
39
		if($time_max)
d53e0fe6   Nathanael Jourdane   Move EPN-TAP modu...
40
			array_push($filter, "time_max <= " . $this->dateToJD($time_max));
210a3164   Nathanael Jourdane   Store actual func...
41
		return (count($filter) > 0 ? ' WHERE ' . join(' AND ', $filter) : '');
5de62950   Nathanael Jourdane   Improve the VOTab...
42
	}
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
43

5de62950   Nathanael Jourdane   Improve the VOTab...
44
	public function request($access_url, $query) {
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
45
46
47
		$votMgr = new VOTableMgr;
		$params = 'FORMAT=votable&LANG=ADQL&REQUEST=doQuery';
		$url = $access_url . '/sync?' . $params . '&QUERY=' . urlencode($query);
210a3164   Nathanael Jourdane   Store actual func...
48

d53e0fe6   Nathanael Jourdane   Move EPN-TAP modu...
49
		$res = $votMgr->load($url);
5de62950   Nathanael Jourdane   Improve the VOTab...
50
		$this->addLog("Query URL: " . $url);
d53e0fe6   Nathanael Jourdane   Move EPN-TAP modu...
51
52
		$result = $votMgr->parseStream();
		return $votMgr->getVotableError() ? array('error' => $votMgr->getVotableError()) : $result;
67a4e0da   Nathanael Jourdane   Add a function to...
53
54
	}

47a2829d   Nathanael Jourdane   Fix the paginatio...
55
	/* filter order: product type, target name, time min, time max */
67a4e0da   Nathanael Jourdane   Add a function to...
56
	public function getGranules($table_name, $access_url, $filter, $select, $limit, $offset) {
b185823c   Nathanael Jourdane   Use IntervalUI mo...
57
		$query = "SELECT TOP {$limit} " . join(', ', $select) . " FROM {$table_name}.epn_core " . $this->createFilter($filter[0], $filter[1], $filter[2], $filter[3]) . " OFFSET {$offset}";
67a4e0da   Nathanael Jourdane   Add a function to...
58
59
		// return $query;
		$result = $this->request($access_url, $query);
d53e0fe6   Nathanael Jourdane   Move EPN-TAP modu...
60
61
62
63
64
65
		if(! array_key_exists("error", $result)) {
			for ($i = 0 ; $i < sizeof($result) ; $i++) {
				$result[$i]['num'] = $i + $offset + 1;
				$result[$i]['time_min'] = $this->JDTodate($result[$i]['time_min']);
				$result[$i]['time_max'] = $this->JDTodate($result[$i]['time_max']);
			}
210a3164   Nathanael Jourdane   Store actual func...
66
		}
5de62950   Nathanael Jourdane   Improve the VOTab...
67
		return $result;
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
68
69
	}

47a2829d   Nathanael Jourdane   Fix the paginatio...
70
	/* filter order: product type, target name, time min, time max */
67a4e0da   Nathanael Jourdane   Add a function to...
71
	public function getNbRows($table_name, $access_url, $filter) {
b185823c   Nathanael Jourdane   Use IntervalUI mo...
72
		$query = "SELECT COUNT(*) AS nb_rows FROM {$table_name}.epn_core " . $this->createFilter($filter[0], $filter[1], $filter[2], $filter[3]);
210a3164   Nathanael Jourdane   Store actual func...
73
		// return $query;
67a4e0da   Nathanael Jourdane   Add a function to...
74
		return $this->request($access_url, $query)[0]['nb_rows'];
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
75
	}
67a4e0da   Nathanael Jourdane   Add a function to...
76

3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
77
78
}
?>