$msg), $status); } function api_int($val) { return $val === NULL ? NULL : (int)$val; } /* create_ppl_id/delete_ppl_id hold $_SESSION['username'], the *_eid * columns $_SESSION['orig_username'] (differs after do_su) */ function api_wie($username, $orig_username) { if ($username === NULL) return NULL; return array('username' => $username, 'orig_username' => $orig_username); } function api_gast($row) { if ($row['ppl_id'] === NULL) return NULL; $naam = implode(' ', array_filter(array($row['ppl_forename'], $row['ppl_prefix'], $row['ppl_surname']), function ($s) { return $s !== NULL && $s !== ''; })); return array( 'ppl_id' => api_int($row['ppl_id']), 'naam' => $naam, 'voornaam' => $row['ppl_forename'], 'tussenvoegsel' => $row['ppl_prefix'], 'achternaam' => $row['ppl_surname'], 'klas' => $row['klas'] ); } function api_kaartje($row) { return array( 'kaartverkoop_id' => api_int($row['kaartverkoop_id']), 'barcode' => $row['barcode'], 'gast' => api_gast($row), 'introducee' => $row['heeft_introducee'] == 1, 'bron' => $row['bron'], 'tijdstip' => $row['timestamp_create'], 'door' => api_wie($row['create_ppl_id'], $row['create_ppl_eid']), 'verwijderd' => $row['timestamp_delete'] === NULL ? NULL : array( 'tijdstip' => $row['timestamp_delete'], 'door' => api_wie($row['delete_ppl_id'], $row['delete_ppl_eid']) ), 'binnen' => $row['binnen_id'] === NULL ? NULL : array( 'kaartcontrole_id' => api_int($row['binnen_id']), 'tijdstip' => $row['binnen_tijdstip'], 'door' => api_wie($row['binnen_ppl_id'], $row['binnen_ppl_eid']) ) ); } function api_controle($row) { return array( 'kaartcontrole_id' => api_int($row['kaartcontrole_id']), 'kaartverkoop_id' => api_int($row['kaartverkoop_id']), 'barcode' => $row['barcode'], 'gast' => api_gast($row), 'introducee' => $row['heeft_introducee'] == 1, 'tijdstip' => $row['timestamp_create'], 'door' => api_wie($row['create_ppl_id'], $row['create_ppl_eid']), 'verwijderd' => $row['timestamp_delete'] === NULL ? NULL : array( 'tijdstip' => $row['timestamp_delete'], 'door' => api_wie($row['delete_ppl_id'], $row['delete_ppl_eid']) ) ); } function api_limit() { if (!isset($_GET['limit']) || $_GET['limit'] === '') return ''; if (!preg_match('/^[1-9]\d*$/', $_GET['limit'])) api_error('limit moet een positief geheel getal zijn'); return ' LIMIT '.(int)$_GET['limit']; } /* person and current stamklas of the leerling, like common.php */ $gast_join = << UNIX_TIMESTAMP() ) AS klas ON klas.ppl_id = ppl.ppl_id EOQ; $gast_cols = 'ppl.ppl_id, ppl.ppl_forename, ppl.ppl_prefix, ppl.ppl_surname, klas.grp_name klas'; $kaartverkoop_query = << $l + $i, 'leerlingen' => $l, 'introducees' => $i); } /* sold and inside, each as totaal/leerlingen/introducees */ function api_alle_aantallen($db) { return array( 'verkocht' => api_aantallen($db, 'kaartverkoop', 'kaartverkoop.timestamp_delete IS NULL'), 'binnen' => api_aantallen($db, 'kaartcontrole JOIN kaartverkoop USING (kaartverkoop_id)', 'kaartcontrole.timestamp_delete IS NULL') ); } $action = isset($_GET['action']) ? $_GET['action'] : ''; switch ($action) { case 'whoami': api_out(array( 'username' => $_SESSION['username'], 'orig_username' => $_SESSION['orig_username'], 'type' => $_SESSION['type'], 'ppl_id' => isset($_SESSION['ppl_id']) ? api_int($_SESSION['ppl_id']) : NULL, 'admin' => (bool)$_SESSION['admin'], 'feest' => array( 'feest_id' => (int)$feest_id, 'naam' => mdb2_single_val($db, "SELECT feest_name FROM feesten WHERE feest_id = $feest_id"), 'introducee_toegestaan' => mdb2_single_val($db, "SELECT introducee_toegestaan FROM feesten WHERE feest_id = $feest_id") ) )); case 'aantallen': api_out(api_alle_aantallen($db)); case 'kaartverkoop': $and = isset($_GET['verwijderd']) && $_GET['verwijderd'] == 1 ? '' : ' AND kaartverkoop.timestamp_delete IS NULL'; if (!isset($_GET['order']) || $_GET['order'] == 'chron') $order = 'ORDER BY kaartverkoop.timestamp_create DESC, kaartverkoop.kaartverkoop_id DESC'; else if ($_GET['order'] == 'aankomst') // latest arrival first, not (yet) inside last $order = 'ORDER BY binnen.timestamp_create IS NULL, binnen.timestamp_create DESC, kaartverkoop.timestamp_create DESC'; else if ($_GET['order'] == 'alph') $order = 'ORDER BY ppl.ppl_surname IS NULL, ppl.ppl_surname, ppl.ppl_forename, ppl.ppl_prefix, kaartverkoop.barcode'; else api_error('order moet chron, aankomst of alph zijn'); $rows = mdb2_all_assoc($db, $kaartverkoop_query.$and."\n".$order.api_limit()); api_out(array( 'aantallen' => api_alle_aantallen($db), 'kaartjes' => array_map('api_kaartje', $rows) )); case 'kaartcontrole': $and = isset($_GET['verwijderd']) && $_GET['verwijderd'] == 1 ? '' : ' AND kaartcontrole.timestamp_delete IS NULL'; $rows = mdb2_all_assoc($db, $kaartcontrole_query.$and.<< api_alle_aantallen($db), 'controles' => array_map('api_controle', $rows) )); case 'barcode': if (!isset($_GET['barcode']) || $_GET['barcode'] === '') api_error('barcode ontbreekt'); $barcode = ltrim($_GET['barcode'], '~'); $kaartje = mdb2_single_assoc($db, $kaartverkoop_query.<< $barcode, 'gast' => api_gast($gast), 'kaartje' => $kaartje ? api_kaartje($kaartje) : NULL, 'binnen' => $controle ? api_controle($controle) : NULL )); case 'melding': /* the notice or error a do_*.php script left in the session; returned * once and cleared, like html_start() shows it */ $melding = NULL; if (isset($_SESSION['err'])) { $melding = array('title' => $_SESSION['err'][0], 'text' => $_SESSION['err'][1], 'error' => true); unset($_SESSION['err']); } if (isset($_SESSION['notice'])) { if (!$melding) $melding = array('title' => $_SESSION['notice'][0], 'text' => $_SESSION['notice'][1], 'error' => false); unset($_SESSION['notice']); } api_out(array('melding' => $melding)); default: api_error('onbekende action, gebruik whoami, aantallen, kaartverkoop, kaartcontrole, barcode of melding'); } ?>