Blame view

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

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

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

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

9f82279e   Nathanael Jourdane   Store actual func...
20
	private function dateToJD($gregorian_date) {
4e39b17b   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);
9f82279e   Nathanael Jourdane   Store actual func...
23
	}
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
24

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

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

f740e66b   Nathanael Jourdane   Improve the VOTab...
44
	public function request($access_url, $query) {
a79b0198   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);
9f82279e   Nathanael Jourdane   Store actual func...
48

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

c6f0618e   Nathanael Jourdane   Fix the paginatio...
55
	/* filter order: product type, target name, time min, time max */
4e39b17b   Nathanael Jourdane   Add a function to...
56
	public function getGranules($table_name, $access_url, $filter, $select, $limit, $offset) {
c4eda008   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}";
4e39b17b   Nathanael Jourdane   Add a function to...
58
59
		// return $query;
		$result = $this->request($access_url, $query);
b034ce89   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']);
			}
9f82279e   Nathanael Jourdane   Store actual func...
66
		}
f740e66b   Nathanael Jourdane   Improve the VOTab...
67
		return $result;
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
68
69
	}

c6f0618e   Nathanael Jourdane   Fix the paginatio...
70
	/* filter order: product type, target name, time min, time max */
4e39b17b   Nathanael Jourdane   Add a function to...
71
	public function getNbRows($table_name, $access_url, $filter) {
c4eda008   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]);
9f82279e   Nathanael Jourdane   Store actual func...
73
		// return $query;
4e39b17b   Nathanael Jourdane   Add a function to...
74
		return $this->request($access_url, $query)[0]['nb_rows'];
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
75
	}
4e39b17b   Nathanael Jourdane   Add a function to...
76

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