Subversion Repositories eFlore/Applications.cel

Rev

Rev 1803 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1802 raphael 1
<?php
2
 
3
global $champs;
4
$champs = Array(
5
	'nom_sel' => 'Espece',
6
	'nom_sel_nn' => 'Numero nomenclatural',
7
	'nom_ret' => 'Nom retenu',
8
	'nom_ret_nn' => 'Numero nomenclatural nom retenu',
9
	'nt' => 'Numero 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' => 'Referentiel Geographique',
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
	if(defined('COOKIE') && COOKIE) return TRUE;
138
	$URL = DOMAIN . '/jrest/User/' . EMAIL . '/' . PASS;
139
 
140
	$ch = curl_init();
141
 
142
	//set the url, number of POST vars, POST data
143
	curl_setopt($ch,CURLOPT_URL, $URL);
144
	curl_setopt($ch,CURLOPT_HEADER, true);
145
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
146
 
147
	$result = curl_exec($ch);
148
	curl_close($ch);
149
	$sess = $id = array();
150
	preg_match('/.*PHPSESSID=(\w+)/', $result, $sess);
151
	preg_match('/"id_utilisateur":"(\d+)"/', $result, $id);
152
	if(DEBUG) fwrite(STDERR, "curl \"".$URL ."\" : {$sess[1]} / {$id[1]}\n");
153
	if(isset($sess[1])) {
154
		define('COOKIE', $sess[1]);
155
		if(!defined('USER')) {
156
			if(!isset($id[1])) return FALSE;
157
			define('USER', $id[1]);
158
		}
159
		return TRUE;
160
	}
161
	return FALSE;
162
}
163
 
164
function genCSV($d = Array()) {
165
	$out = fopen("php://temp", 'r+');
166
	// don't filter-out unknown field (for testing)
167
	// $d = array_intersect_key($d, $GLOBALS['champs']);
168
	$head = array();
169
	foreach($d as $k => $v) {
170
		if(isset($GLOBALS['champs'][$k])) $head[$GLOBALS['champs'][$k]] = $v;
171
		else $head[$k] = $v;
172
	}
173
	fputcsv($out, array_keys($head) , ',', '"');
174
	fputcsv($out, array_values($head) , ',', '"');
175
	rewind($out);
176
	$csv = stream_get_contents($out);
177
	fclose($out);
178
	return $csv;
179
}
180
 
181
function import($d) {
182
	$URL = DOMAIN . '/jrest/InventoryImportExcel';
183
	if(!is_file($d)) return FALSE;
184
	exec("csvtool -o /tmp/a.csv col 1- $d");
1803 raphael 185
	exec(__DIR__ . "/bin/csv2xls -v 0 -f -o /tmp/a /tmp/a.csv");
186
	//if(DEBUG) fwrite(STDERR, __DIR__ . "/bin/csv2xls -v 0 -f -o /tmp/a $d\n");
187
	if(DEBUG) fwrite(STDERR, __DIR__ . "/bin/csv2xls -v 0 -f -o /tmp/a /tmp/a.csv\n");
1802 raphael 188
	$d = "/tmp/a.xls";
189
	$ch = curl_init();
190
 
191
	// curl does not support setting filename="X.csv";type=application/octet-stream'
192
	// with plain POST variables
193
 
194
	curl_setopt($ch,CURLOPT_URL, $URL);
195
	curl_setopt($ch,CURLOPT_POSTFIELDS, array(
196
		'upload' => '@' . $d . ';filename=test.csv',
197
		'utilisateur' => USER
198
	));
199
 
200
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
201
 
202
	if(DEBUG) fwrite(STDERR, "curl -F \"upload=@$d;filename=test.csv\" -F utilisateur=" . USER . " \"$URL\"\n");
203
 
204
	//execute post
205
	$result = curl_exec($ch);
206
	curl_close($ch);
207
	return $result;
208
}
209
 
210
function export($cols = NULL) {
211
	if(!defined('COOKIE')) return FALSE;
212
 
213
	/*$URL = DOMAIN . '/jrest/CelWidgetExport/csv?format=csv&utilisateur=' . EMAIL;
214
	  $cookie = 'PHPSESSID=' . COOKIE . '; ' . 'utilisateur=' . EMAIL . '; ' . 'cel_id=' . USER;*/
215
	$URL = DOMAIN . '/jrest/InventoryExport/' . USER . '/';
216
	$cookie = 'PHPSESSID=' . COOKIE;
217
 
218
	$ch = curl_init();
219
 
220
	curl_setopt($ch,CURLOPT_URL, $URL);
221
	curl_setopt($ch,CURLOPT_COOKIE, $cookie);
222
	$fh = fopen('/tmp/b.xls', 'w');
223
	curl_setopt($ch,CURLOPT_FILE, $fh);
224
 
225
	if(DEBUG) fwrite(STDERR, "curl -b \"$cookie\" \"$URL\"\n");
226
 
227
	//execute post
228
	$result = curl_exec($ch);
229
	fflush($fh);
230
	fclose($fh);
231
	curl_close($ch);
232
	exec("xls2csv /tmp/b.xls", $result);
233
	if(DEBUG) fwrite(STDERR, "xls2csv /tmp/b.xls\n");
234
	$result = implode("\n", $result);
235
	return $result;
236
}
237
 
238
function getCSV_line($csv, $line) {
239
	$line = max(0, intval($line - 1)); // -1 à cause du header
240
 
241
	$out = fopen("php://temp", 'rw');
242
	fwrite($out, $csv);
243
	rewind($out);
244
 
245
	$head = champsLongToShort(fgetcsv($out));
246
	while ($line--) fgetcsv($out);
247
	$l = fgetcsv($out);
248
	fclose($out);
249
	if(!$l) return FALSE;
250
	return array_combine($head, $l);
251
}
252
 
253
 
254
function champsLongToShort($a) {
255
	$r = array();
256
	$x = array_flip($GLOBALS['champs']);
257
	foreach($a as $k => $v) {
258
		if(isset($x[$v])) $r[$k] = $x[$v];
259
		else $r[$k] = -1;
260
	}
261
	return $r;
262
}
263
 
264
function champsLongToShort2($a) {
265
	$r = array();
266
	$x = array_flip($GLOBALS['champs']);
267
	foreach($a as $k => $v) {
268
		if(isset($x[$k])) $r[$x[$k]] = $v;
269
		else $r[$k] = $v;
270
	}
271
	return $r;
272
}