function storeEntry($name, $exec, $categories, $menu, $indexes = null, $core = false) { if (is_null($indexes)) { // first call of this function for this desktop file $indexes = array(); $indexes["c"] = 0; $indexes["nc"] = count($categories); $indexes["lc"] = $indexes["nc"] - 1; } for ($c =& $indexes["c"]; $c < $indexes["nc"]; $c++) { // loop through each category $category =& $categories[$c]; if ($category == "application") { $core = true; continue; } $is_last = $c == $indexes["lc"]; if (array_key_exists($category, $menu)) { $core = false; if ($is_last) { // put entry in current (sub)menu $menu[$category][] = array("appname" => $name, "appexec" => $exec); } else { // look for a matching submenu ++$c; $menu[$category] = storeEntry($name, $exec, $categories, $menu[$category], $indexes, $core); } return $menu; } else { // unknown category if (!$is_last) { $core = false; continue; } else { if ($core && $category != "core") { // not a "core" app, not a supported category $menu["other"][] = array("appname" => $name, "appexec" => $exec); return $menu; } else { // last category, put it in current (sub)menu $menu[] = array("appname" => $name, "appexec" => $exec); return $menu; } } } } return $menu; }
/** * Insert entry into dedicated table in DB. * * @param array $entry The array representation of the POST payload. * @return JSON On success or failure, returns a prepared JSON response matching the situation. */ function storeEntry($entry) { $connParams = json_decode(file_get_contents('../../config.json')); $conn = mysqli_connect($connParams->sDbHost, $connParams->sDbUser, $connParams->sDbPassword, $connParams->sDbDatabase); if (mysqli_connect_errno()) { echo sendBack(500, 'MySQL error: ' . mysqli_connect_error(), $entry); } $query = "INSERT INTO mango_results_facilitator" . " (id, survey_name, user_token, matching_table_in_limesurvey, matching_row_in_table)" . " VALUES (NULL,'" . $entry['name'] . "', '" . $entry['token'] . "', '" . $entry['table'] . "', '" . $entry['row'] . "')"; $code = 201; $msg = 'Successfully added to DB.'; if (!mysqli_query($conn, $query)) { $code = 500; $msg = 'MySQL error: ' . mysqli_error($conn); } echo sendBack($code, $msg, $entry); } if (isset($_POST['entry'])) { $entry = cleanEntry($_POST['entry']); if (!$entry) { echo sendBack(400, 'Entry passed to results_facilitator.php is not valid.', $entry); } return storeEntry($entry); } else { echo sendBack(500, 'No data was passed via POST!', null); }