get('/categories', function () use ($app) { $callbackFn = $app->request()->get('callback'); $mysqlconnetion = new MysqlClass; //$mysqlconneti on->connetti(); $retObj = $mysqlconnetion->queryToObject("select ID as category_id, NAME as name from categorie order by name"); $mysqlconnetion->disconnetti(); returnJsonWithDecode($app, $callbackFn, $retObj); }); $app->get('/typeingredients', function () use ($app) { $callbackFn = $app->request()->get('callback'); $mysqlconnetion = new MysqlClass; //$mysqlconnetion->connetti(); $retObj = $mysqlconnetion->queryToObject("select ID as tipo_ingredienti_id, name from tipo_ingredienti order by name"); $mysqlconnetion->disconnetti(); returnJsonWithDecode($app, $callbackFn, $retObj); }); $app->get('/typeqtys', function () use ($app) { $callbackFn = $app->request()->get('callback'); $mysqlconnetion = new MysqlClass; //$mysqlconnetion->connetti(); $retObj = $mysqlconnetion->queryToObject("select ID as tipo_quantita_id, name from tipo_quantita"); $mysqlconnetion->disconnetti(); returnJsonWithDecode($app, $callbackFn, $retObj); }); $app->get('/categoryitems/:catID', function ($categoryID) use ($app) { $callbackFn = $app->request()->get('callback'); $mysqlconnetion = new MysqlClass; //$mysqlconnetion->connetti(); $query = "select ID as ricetta_id, titolo, autore, valutazione, difficolta from ricette where ID_CATEGORIA = " . $categoryID . " order by titolo, autore"; $retObj = $mysqlconnetion->queryToObject($query); $mysqlconnetion->disconnetti(); returnJson($app, $callbackFn, $retObj); }); $app->get('/ricetta/body/:itemID', function ($itemID) use ($app) { $callbackFn = $app->request()->get('callback'); // istanza della classe $mysqlconnetion = new MysqlClass; $query = "SELECT id_categoria, categorie.name AS categoria_name, ricette.ID AS ricetta_id, titolo, procedimento, autore, link_fonte, link_youtube, valutazione, difficolta FROM ricette INNER JOIN categorie ON categorie.ID = ricette.id_categoria WHERE ricette.ID = " . $itemID; $retObj = $mysqlconnetion->queryToObject($query); $query2 = "select ingredienti.id_tipo_ingredienti, tipo_ingredienti.name as nome_ingrediente, quantita, ingredienti.id_tipo_quantita, tipo_quantita.Name as unita, note, posizione from ingredienti " . "inner join tipo_ingredienti on ingredienti.ID_TIPO_INGREDIENTI = tipo_ingredienti.ID " . "left outer join tipo_quantita on ingredienti.ID_TIPO_QUANTITA = tipo_quantita.ID " . "where ingredienti.ID_RICETTE = " . $itemID . " order by ingredienti.posizione"; $retObj2 = $mysqlconnetion->queryToObject($query2); foreach ($retObj2 as $ele) { $ele["note"] = html_entity_decode($ele["note"], ENT_COMPAT | ENT_HTML401, 'ISO-8859-1'); } $retObj[0]["titolo"] = html_entity_decode($retObj[0]["titolo"],ENT_COMPAT | ENT_HTML401,'ISO-8859-1'); $retObj[0]["procedimento"] = html_entity_decode($retObj[0]["procedimento"],ENT_COMPAT | ENT_HTML401,'ISO-8859-1'); $retObj[0]["ingredienti"] = $retObj2; $mysqlconnetion->disconnetti(); returnJson($app, $callbackFn, $retObj); }); $app->post('/typeingredients', function () use ($app) { $callbackFn = $app->request()->get('callback'); $json_data_body = json_decode($app->request()->post('bodydata')); $mysqlconnetion = new MysqlClass; //$mysqlconnetion->connetti(); $query = "insert into tipo_ingredienti(Name) values ('" . $json_data_body->name . "')"; $retNewID = $mysqlconnetion->insertRecord($query); $mysqlconnetion->disconnetti(); returnJson($app, $callbackFn, $retNewID); }); $app->post('/ricetta/body', function () use ($app) { $callbackFn = $app->request()->get('callback'); $json_data_body = json_decode($app->request()->post('bodydata')); $retValue["result"] = true; $retValue["message"] = ""; $mysqlconnetion = new MysqlClass; // istanza della classe try { $retNewID = 0; if ($json_data_body->ricettaID != "") { $query = "update ricette SET ID_CATEGORIA = " . $json_data_body->categoria_id . ", titolo = '" . htmlentities($json_data_body->titolo, null, "UTF-8") . "', procedimento = '" . str_replace("'", "''", htmlentities($json_data_body->preparazione, null, "UTF-8")) . "', autore = '" . str_replace("'", "''", $json_data_body->autore) . "', link_fonte = '" . $json_data_body->linkFonte . "', Link_youtube = '" . $json_data_body->linkVideo . "', Difficolta = '" . $json_data_body->difficolta . "' where ID = " . $json_data_body->ricettaID; $mysqlconnetion->executeQuery($query); $queryDelete = "DELETE FROM ingredienti where ID_RICETTE = " . $json_data_body->ricettaID; $mysqlconnetion->executeQuery($queryDelete); $retNewID = $json_data_body->ricettaID; } else { $query = "insert into ricette(ID_CATEGORIA, titolo, procedimento, autore, link_fonte, Link_youtube,Difficolta) values (" . $json_data_body->categoria_id . ",'" . htmlentities($json_data_body->titolo, null, "UTF-8") . "','" . str_replace("'", "''", htmlentities($json_data_body->preparazione, null, "UTF-8")) . "','" . str_replace("'", "''", $json_data_body->autore) . "','" . $json_data_body->linkFonte . "','" . $json_data_body->linkVideo . "', " . $json_data_body->difficolta . ")"; $retNewID = $mysqlconnetion->insertRecord($query); } $pos = 0; foreach ($json_data_body->ingredienti as $arr) { $note = ""; if ($arr->note != "") { $note = str_replace("'", "''", htmlentities($arr->note, null, "UTF-8")); } $query = "insert into ingredienti(ID_TIPO_INGREDIENTI, ID_RICETTE, Quantita, ID_TIPO_QUANTITA, Note, Posizione) values (" . $arr->ingrediente_id . "," . $retNewID . "," . ($arr->quantita == "" ? "0" : $arr->quantita) . "," . ($arr->tipo_quantita_id == "" ? "NULL" : $arr->tipo_quantita_id) . ",'" . $note . "'," . $pos . ")"; $mysqlconnetion->insertRecord($query); $pos = $pos + 1; } $retValue["message"] = "Ricetta inserita con successo"; } catch (Exception $e) { $retValue["message"] = $e->getMessage(); } $mysqlconnetion->disconnetti(); returnJson($app, $callbackFn, $retValue); }); $app->get('/photos/', function () use ($app) { $callbackFn = $app->request()->get('callback'); $query = "SELECT * from ( SELECT id as ricetta_id, `titolo`,`autore`, ". " (select COUNT(*) from immagini where immagini.id_ricette = `ricette`.id) as num_img,". " (select COUNT(*) from immagini where immagini.id_ricette = `ricette`.id and immagini.published = 1) as num_img_pub". " FROM `ricette`". " ) as tmp". " WHERE tmp.num_img> 0"; $mysqlconnetion = new MysqlClass; //$mysqlconnetion->connetti(); $retObj = $mysqlconnetion->queryToObject($query); $mysqlconnetion->disconnetti(); returnJson($app, $callbackFn, $retObj); }); $app->get('/ricetta/:itemID/photos', function ($itemID) use ($app) { $callbackFn = $app->request()->get('callback'); $query = "SELECT id as id_photo, type_format, from_profile_id, uploaded_date, profilo.Name as profile_name, published from immagini" . " INNER JOIN profilo ON profilo.ProfiloID = immagini.from_profile_id". " WHERE id_ricette = " . $itemID; $mysqlconnetion = new MysqlClass; //$mysqlconnetion->connetti(); $retObj = $mysqlconnetion->queryToObject($query); $mysqlconnetion->disconnetti(); returnJson($app, $callbackFn, $retObj); }); $app->get('/statistics/gender', function () use ($app) { $callbackFn = $app->request()->get('callback'); $query = "SELECT 'Gender' as Type, IF(Gender='', 'Sconosciuto', Gender) as Serie, COUNT(gender) as CountSerie from profilo GROUP BY gender"; $mysqlconnetion = new MysqlClass; //$mysqlconnetion->connetti(); $retObj = $mysqlconnetion->queryToObject($query); $mysqlconnetion->disconnetti(); returnJson($app, $callbackFn, $retObj); }); $app->get('/statistics/typeAccess', function () use ($app) { $callbackFn = $app->request()->get('callback'); $query = "SELECT 'TipoAccesso' as Type, TipoAccesso as Serie, COUNT(TipoAccesso) as CountSerie from profilo GROUP BY TipoAccesso"; $mysqlconnetion = new MysqlClass; //$mysqlconnetion->connetti(); $retObj = $mysqlconnetion->queryToObject($query); $mysqlconnetion->disconnetti(); returnJson($app, $callbackFn, $retObj); }); $app->get('/statistics/themesUsage', function () use ($app) { $callbackFn = $app->request()->get('callback'); $query = "SELECT 'TemaUI' as Type, TemaUI as Serie, COUNT(TemaUI) as CountSerie from profilo GROUP BY TemaUI"; $mysqlconnetion = new MysqlClass; //$mysqlconnetion->connetti(); $retObj = $mysqlconnetion->queryToObject($query); $mysqlconnetion->disconnetti(); returnJson($app, $callbackFn, $retObj); });