Subversion Repositories eFlore/Applications.cel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1768 raphael 1
<?php
2
 
3
global $champs;
4
$champs = Array(
5
	'nom_sel' => 'Espèce',
6
	'nom_sel_nn' => 'Numéro nomenclatural',
7
	'nom_ret' => 'Nom retenu',
8
	'nom_ret_nn' => 'Numéro nomenclatural nom retenu',
9
	'nt' => 'Numéro taxonomique',
10
	'famille' => 'Famille',
11
	'nom_referentiel' => 'Referentiel taxonomique',
12
	'zone_geo' => 'Commune',
13
	'ce_zone_geo' => 'Identifiant Commune',
14
	'date_observation' => 'Date',
15
	'lieudit' => 'Lieu-dit',
16
	'station' => 'Station',
17
	'milieu' => 'Milieu',
18
	'commentaire' => 'Notes',
19
	'latitude' => 'Latitude',
20
	'longitude' => 'Longitude',
21
	'altitude' => 'Altitude',
22
	'geodatum' => 'Référentiel Géographique',
23
 
24
	'ordre' => 'Ordre',
25
	'id_observation' => 'Identifiant',
26
 
27
	'mots_cles_texte' => 'Mots Clés',
28
 
29
	'date_creation' => 'Date Création',
30
	'date_modification' => 'Date Modification',
31
 
32
	'transmission' => 'Transmis',
33
	'date_transmission' => 'Date Transmission',
34
	'abondance' => 'Abondance',
35
	'certitude' => 'Certitude',
36
	'phenologie' => 'Phénologie',
37
	'images' => 'Image(s)',
38
	'nom_commun' => 'Nom Commun',
39
);
40
 
41
 
42
function cel_get_obs() {
43
	$URL = DOMAIN . '/jrest/InventoryObservationList/' . USER;
44
	return json_decode(file_get_contents($URL . '/?limite=100000&numero_page=0'));
45
}
46
 
47
function cel_get_images() {
48
	$URL = DOMAIN . '/jrest/InventoryImageList/' . USER;
49
	return json_decode(file_get_contents($URL . '/?limite=100000&numero_page=0'));
50
}
51
 
52
function cel_delete_obs($obs) {
53
	$URL = DOMAIN . '/jrest/InventoryObservationList/' . USER;
54
	$ch = curl_init();
55
 
56
	//set the url, number of POST vars, POST data
57
	curl_setopt($ch,CURLOPT_URL, $URL . '/' . implode(',', $obs));
58
	curl_setopt($ch,CURLOPT_POSTFIELDS, array('action' => 'DELETE'));
59
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
60
	//execute post
61
	$result = curl_exec($ch);
62
	curl_close($ch);
63
	return $result;
64
}
65
 
66
function cel_delete_image($img) {
67
	$URL = DOMAIN . '/jrest/InventoryImageList/' . USER;
68
	$ch = curl_init();
69
 
70
	//set the url, number of POST vars, POST data
71
	curl_setopt($ch,CURLOPT_URL, $URL . '/' . implode(',', $img));
72
	curl_setopt($ch,CURLOPT_POSTFIELDS, array('action' => 'DELETE'));
73
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
74
	//execute post
75
	$result = curl_exec($ch);
76
	curl_close($ch);
77
	return $result;
78
}
79
 
80
 
81
function cel_delete_all_obs() {
82
	$obs = cel_get_obs();
83
	$ordres = array_map(function($item) { return $item->ordre; }, $obs);
84
	cel_delete_obs($ordres);
85
}
86
 
87
 
88
function cel_delete_all_images() {
89
	$img = cel_get_images();
90
	$ids = array_map(function($item) { return $item->id_image; }, $img);
91
	cel_delete_image($ids);
92
}
93
 
94
function cel_upload_image($file) {
95
	if(! file_exists($file)) return NULL;
96
	$URL = DOMAIN . '/jrest/InventoryImage';
97
 
98
	$ch = curl_init();
99
 
100
	//set the url, number of POST vars, POST data
101
	curl_setopt($ch,CURLOPT_URL, $URL);
102
	curl_setopt($ch,CURLOPT_POSTFIELDS, array(
103
		'file' => '@' . $file,
104
		'ce_utilisateur' => USER
105
	));
106
 
107
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
108
	//execute post
109
	$result = curl_exec($ch);
110
	curl_close($ch);
111
	return $result;
112
}
113
 
114
function cel_link_images($user, $id_image, $id_obs) {
115
	$URL = DOMAIN . '/jrest/inventoryImageLink/';
116
	$ch = curl_init();
117
 
118
	//set the url, number of POST vars, POST data
119
	curl_setopt($ch,CURLOPT_URL, $URL);
120
	curl_setopt($ch,CURLOPT_POSTFIELDS, array(
121
		'id_image' => $id_obs,
122
		'id_observation' => $id_obs,
123
		'ce_utilisateur' => $user
124
	));
125
 
126
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
127
	//execute post
128
	$result = curl_exec($ch);
129
	curl_close($ch);
130
	return $result;
131
}
132
 
133
 
134
function auth() {
135
	// TODO: CURLOPT_NETRC + Cel API support
136
	if(!defined('EMAIL') || !defined('PASS') || ! EMAIL || ! PASS) return FALSE;
137
	$URL = DOMAIN . '/jrest/User/' . EMAIL . '/' . PASS;
138
 
139
	$ch = curl_init();
140
 
141
	//set the url, number of POST vars, POST data
142
	curl_setopt($ch,CURLOPT_URL, $URL);
143
	curl_setopt($ch,CURLOPT_HEADER, true);
144
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
145
 
146
	$result = curl_exec($ch);
147
	curl_close($ch);
148
	$sess = array();
149
	preg_match('/.*PHPSESSID=(\w+).*/', $result, $sess);
150
	if(isset($sess[1])) {
151
		define('COOKIE', $sess[1]);
152
		return true;
153
	}
154
	return false;
155
}
156
 
157
function genCSV($d = Array()) {
158
	$out = fopen("php://temp", 'r+');
159
	// don't filter-out unknown field (for testing)
160
	// $d = array_intersect_key($d, $GLOBALS['champs']);
161
	$head = array();
162
	foreach($d as $k => $v) {
163
		if(isset($GLOBALS['champs'][$k])) $head[$GLOBALS['champs'][$k]] = $v;
164
		else $head[$k] = $v;
165
	}
166
	fputcsv($out, array_keys($head) , ',', '"');
167
	fputcsv($out, array_values($head) , ',', '"');
168
	rewind($out);
169
	$csv = stream_get_contents($out);
170
	fclose($out);
171
	return $csv;
172
}
173
 
174
function import($d) {
175
	$URL = DOMAIN . '/jrest/ImportXLS';
176
	if(!is_file($d)) return FALSE;
177
	$ch = curl_init();
178
 
179
	// curl does not support setting filename="X.csv";type=application/octet-stream'
180
	// with plain POST variables
181
 
182
	curl_setopt($ch,CURLOPT_URL, $URL);
183
	curl_setopt($ch,CURLOPT_POSTFIELDS, array(
184
		'upload' => '@' . $d . ';filename=test.csv',
185
		'utilisateur' => USER
186
	));
187
 
188
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
189
	//execute post
190
	$result = curl_exec($ch);
191
	curl_close($ch);
192
	return $result;
193
}
194
 
1773 raphael 195
function export($cols = "standard,avance") {
1768 raphael 196
	if(!defined('COOKIE')) return FALSE;
197
 
198
	$URL = DOMAIN . '/jrest/CelWidgetExport/export?';
1773 raphael 199
	$URL .= http_build_query(array('colonnes' => $cols,
1768 raphael 200
								   'id_utilisateur' => USER,
201
								   'format' => 'csv'));
202
	$ch = curl_init();
203
 
204
	curl_setopt($ch,CURLOPT_URL, $URL);
205
	curl_setopt($ch,CURLOPT_COOKIE, 'PHPSESSID=' . COOKIE);
206
 
1773 raphael 207
	if(DEBUG) fwrite(STDERR, "curl -b PHPSESSID=" . COOKIE . ' "' . $URL ."\"\n");
208
 
1768 raphael 209
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
210
	//execute post
211
	$result = curl_exec($ch);
212
	curl_close($ch);
213
	return $result;
214
}
215
 
216
function getCSV_line($csv, $line) {
217
	$line = max(0, intval($line - 1)); // -1 à cause du header
218
 
219
	$out = fopen("php://temp", 'rw');
220
	fwrite($out, $csv);
221
	rewind($out);
222
 
223
	$head = champsLongToShort(fgetcsv($out));
224
	while ($line--) fgetcsv($out);
225
	$l = fgetcsv($out);
226
	fclose($out);
227
	return array_combine($head, $l);
228
}
229
 
230
 
231
function champsLongToShort($a) {
232
	$r = array();
233
	$x = array_flip($GLOBALS['champs']);
234
	foreach($a as $k => $v) {
235
		if(isset($x[$v])) $r[$k] = $x[$v];
236
		else $r[$k] = -1;
237
	}
238
	return $r;
239
}
240
 
1769 raphael 241
function champsLongToShort2($a) {
242
	$r = array();
243
	$x = array_flip($GLOBALS['champs']);
244
	foreach($a as $k => $v) {
245
		if(isset($x[$k])) $r[$x[$k]] = $v;
246
		else $r[$k] = $v;
247
	}
248
	return $r;
249
}