function check_chart_exists($id, $callback) { $chart = ChartQuery::create()->findPK($id); if ($chart) { call_user_func($callback, $chart); } else { // no such chart error_chart_not_found($id); } }
<?php /** * API: get data to a chart * * @param chart_id chart id */ $app->get('/chart/:id/data(\\.csv)?', function ($chart_id) use($app) { disable_cache($app); $chart = ChartQuery::create()->findPK($chart_id); $res = $app->response(); $res['Cache-Control'] = 'max-age=0'; $res['Content-Type'] = 'text/csv'; $res['Content-Disposition'] = 'attachment; filename="datawrapper-' . $chart_id . '.csv"'; if (!empty($chart)) { print $chart->loadData(); } else { error_chart_not_found($chart_id); } });
/** * checks if a chart is reable by the current user (or guest) * * @param chart_id * @param callback the function to be executed if chart is writable */ function if_chart_is_readable($chart_id, $callback) { $chart = ChartQuery::create()->findPK($chart_id); if ($chart) { $user = DatawrapperSession::getUser(); if ($chart->isReadable($user) === true) { call_user_func($callback, $user, $chart); } else { // no such chart error_chart_not_writable(); } } else { // no such chart error_chart_not_found($id); } }