-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathciqual_api.php
More file actions
198 lines (151 loc) · 5.25 KB
/
ciqual_api.php
File metadata and controls
198 lines (151 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* appel PHP par [$data = file_get_contents($url);]
*
* recherche par defaut sur le nom d'un aliment et ses deux sous-categories
* alim_name = concat(a.alim_nom_fr, ' | ', b.alim_ssssgrp_nom_fr, ' | ', b.alim_ssgrp_nom_fr)
* la categorie (alim_grp_nom_gr) n'est pas intégrée car trop générale
*
* API:
* $url = {serveur}/ciqual_api.php?table=?&where=?[values=?][&key=?][mode=_AAC]
* table : {alim, alim_grp}
* where : {_ALL, _KEY, [recherche]}
* _ALL : tous les enregistrements
* _KEY : un enregistrement -> associer l'argument [key]
* _CAT : ingredients d'une catégories -> associer l'argument [key={alim_grp_code}]
* [recherche] : partie du nom (%foo%)
* values : {yes, no} / default:no //opère sur ingredients
* key : {alim_code, alim_grp_code}
* order : champs tri des résultats / default(auto):{alim_name}
* mode : {_AAC, ~} / default:~
* _AAC : AjaxAutoComplete -> positionner [where=xx]
*
* @return Json file avec ligne(s) de la base ou message 'error' ou message 'norecordsfound'
*
* $url = '{serveur}/ciqual_api.php?table=alim_grp&where=vian';
* $url = '{serveur}/ciqual_api.php?table=alim_grp&where=_ALL';
* $url = '{serveur}/ciqual_api.php?table=alim&where=veau';
* $url = '{serveur}/ciqual_api.php?table=alim&where=veau&values=yes';
* $url = '{serveur}/ciqual_api.php?table=alim&where=_ALL';
* $url = '{serveur}/ciqual_api.php?table=alim&where=_KEY&key=21515';
* $url = '{serveur}/ciqual_api.php?table=alim&where=_KEY&key=21515&values=yes';
* $url = '{serveur}/ciqual_api.php?table=alim_grp&where=_ALL&mode=_AAC';
*/
session_start();
header('Content-Type: application/json; charset=UTF-8');
// print_r($_GET);
include('defines.php');
$_MESS = array(
'Arguments incomplets.',
'Argument invalide.',
'Argument manquant.',
'Aucun enregistrement trouvé.',
'Erreur de connexion.'
);
$_TABLE = array(
'alim',
'alim_grp'
);
$_PREFIX = array(
'alim' => 'alim',
'alim_grp' => 'alim_grp'
);
$_WHERE = array(
'_ALL',
'_KEY',
'_CAT'
);
$_VALUES = array(
'yes',
'no'
);
$_MODE = array(
'_AAC'
);
if (! empty($error = valid_url()) ) { error($error); }
try {
$pdo = new PDO(_DBSERVER, _DBUSER, _DBPWD);
$pdo->query('SET NAMES UTF8');
} catch (Exception $e) {
error($_MESS[4]);
}
set_defaults();
$table = $_GET['table'];
$where = $_GET['where'];
$values = $_GET['values'];
$key = $_GET['key'];
$order = $_GET['order'];
$mode = $_GET['mode'];
set_specials();
$sql = 'SELECT *';
$sql .= ' FROM ' .$table;
$sql .= ( $table == $_TABLE[0] && $values == $_VALUES[0] ? '_values' : '' ); //valeurs ingrédient
$sql .= '_details ';
switch ($where) {
case $_WHERE[0]:
$sql .= '';
break;
case $_WHERE[1]:
$sql .= " WHERE prefixX_code = '{$key}'";
break;
case $_WHERE[2]:
$sql .= " WHERE prefix1_code = '{$key}'";
break;
default:
$sql .= " WHERE prefixX_name LIKE '%{$where}%'";
}
$sql .= ' ORDER BY ' .$order;
$sql = str_replace('prefixX', $_PREFIX[ $table ], $sql);
$sql = str_replace('prefix0', $_PREFIX[ $_TABLE[0] ], $sql);
$sql = str_replace('prefix1', $_PREFIX[ $_TABLE[1] ], $sql);
$req = $pdo->prepare($sql);
$req->execute();
$data = $req->fetchAll(PDO::FETCH_ASSOC);
if ( !is_array($data) ) {
$data = array( 'return' => $_MESS[3] .'-'.$sql);
}
else{
if (!empty($mode)) {
$data = call_user_func('format' .$mode, $data);
}
}
// echo $sql .'<br/>';
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit();
//-------------------
function format_AAC($data) {
$result = [];
$prefix = $GLOBALS['_PREFIX'][$GLOBALS['table']];
foreach ($data as $element) {
$result[] = array(
"value" => $element[$prefix .'_name'],
"data" => $element[$prefix .'_code']
);
}
return array( "query" => "Unit",
"suggestions" => $result );
}
function valid_url() {
if (!isset($_GET['table']) | !isset($_GET['where'])) return $GLOBALS['_MESS'][0];
if (!in_array($_GET['table'], $GLOBALS['_TABLE'])) return $GLOBALS['_MESS'][1];
if ($_GET['where'] == $GLOBALS['_WHERE'][1] && (!isset($_GET['key']) | empty($_GET['key']))) return $GLOBALS['_MESS'][2];
if ($_GET['where'] == $GLOBALS['_WHERE'][2] && (!isset($_GET['key']) | empty($_GET['key']))) return $GLOBALS['_MESS'][2];
}
function set_defaults() {
if (!isset($_GET['values']) || !in_array($_GET['values'], $GLOBALS['_VALUES'])) $_GET['values'] = $GLOBALS['_VALUES'][1];
if (!isset($_GET['key'])) $_GET['key'] = '';
if (!isset($_GET['mode']) || !in_array($_GET['mode'], $GLOBALS['_MODE'])) $_GET['mode'] = '';
if (!isset($_GET['order'])) $_GET['order'] = 'prefixX_name';
// array_walk($_GET, 'myTrim');
}
function set_specials() {
//mode AAC
if ($GLOBALS['mode'] == $GLOBALS['_MODE'][0]) $GLOBALS['where'] = $_GET['query'];
}
function myTrim(&$value) {
$value = trim($value);
}
function error($message) {
echo json_encode( array('error' => $message .' - ' .print_r($_GET, true)));
exit();
}