<?php

require_once "adHocConst.php";
require_once "adHocInclude.php";
//adHocAdminEditMenu.php
//===================================================================================
//This section retrieves the menu record
$connAdHoc = pdoConnect(cAdHocServer, cAdHocDatabase, cAdHocUsername, cAdHocPassword);
traceHide("Passed keys / value");
foreach ($_REQUEST as $key => $val) {
    traceHide("key:" . $key . " / val:" . $val);
}
$curMenu = $_REQUEST["menu_num"];
$curLine = $_REQUEST["line_num"];
if (isset($_REQUEST['upd'])) {
    $sql = "call sp_updatemenu(" . $_REQUEST["parent_menu"] . ',' . $curMenu . ',' . $curLine . ',' . sqlLiteral($_REQUEST['title']) . ")";
    traceHide($sql);
    pdoQuery($sql, $connAdHoc);
    header("Location: adHocAdmin.php?nextmenu=" . $curMenu);
    exit;
}
$sql = " SELECT title" . " FROM menus" . " WHERE menu_num = " . $curMenu . " AND line_num = " . $curLine;
traceHide("main query=" . $sql);
//===================================================================================
//Get the menu record pertaining to this query
$adhocStmt = pdoQuery($sql, $connAdHoc);
$row = pdoFirstRow($adhocStmt);
$title = pdoData($row, 'title');
traceHide('Title: ' . $title);
//===================================================================================
//Get today's date
require_once "adHocConst.php";
require_once "adHocInclude.php";
//adHocAdminEditQuery.php
//===================================================================================
//This section retrieves the menu record
$connAdHoc = pdoConnect(cAdHocServer, cAdHocDatabase, cAdHocUsername, cAdHocPassword);
traceHide("Passed keys / value");
foreach ($_REQUEST as $key => $val) {
    traceHide("key:" . $key . " / val:" . $val);
}
$curMenu = $_REQUEST["menu_num"];
$curLine = $_REQUEST["line_num"];
if (isset($_REQUEST['upd'])) {
    $query = unformatSql($_REQUEST['query']);
    $sql = "call sp_updatequery(" . $curMenu . ',' . $curLine . ',' . $_REQUEST['sub_menu_num'] . ',' . sqlLiteral($_REQUEST['title']) . ',' . $_REQUEST['query_num'] . ',' . sqlLiteral($query) . ',' . sqlLiteral($_REQUEST['heading']) . ',' . sqlLiteral($_REQUEST['pre_select']) . ',' . sqlLiteral($_REQUEST['total_fields']) . ',' . sqlLiteral($_REQUEST['link_field']) . ',' . $_REQUEST['udb_num'] . ")";
    traceHide($sql);
    pdoQuery($sql, $connAdHoc);
    header("Location: adHocAdmin.php?nextmenu=" . $_REQUEST['sub_menu_num']);
    exit;
}
$sql = " SELECT q.query_num, title, q.heading_text AS heading, q.total_fields," . " q.link_field, q.select_stmt AS detail_select, q.pre_select, q.udb_num" . " FROM menus m" . " JOIN queries q ON m.main_query_num = q.query_num" . " WHERE m.menu_num = " . $curMenu . " AND m.line_num = " . $curLine;
traceHide("main query=" . $sql);
//===================================================================================
//Get the menu record pertaining to this query
$stmt = pdoQuery($sql, $connAdHoc);
$row = pdoFirstRow($stmt);
$title = pdoData($row, 'title');
$heading = pdoData($row, 'heading');
$preselect = pdoData($row, 'pre_select');
$linkfield = pdoData($row, 'link_field');