Example #1
0
function _db_make_idcond($table, $row)
{
    global $ERROR;
    global $debug;
    $primary = _db_primary($table);
    if ($debug) {
        echo "_db_make_idcond table='{$table}' row=";
        print_r($row);
        echo "<br>\n";
    }
    $ok = true;
    foreach (split(",", $primary) as $pr) {
        if (!array_key_exists($pr, @$row)) {
            $ok = false;
            break;
        }
    }
    $cond = array();
    if ($row && $ok) {
        foreach (split(",", $primary) as $pr) {
            $cond[$pr] = @$row[$pr];
        }
    } else {
        $keys = split(",", _db_unique($table));
        if (!$keys) {
            $ERROR = "primary key '{$primary}' is missing in your data, and other keys are not available";
            return null;
        }
        foreach ($keys as $key) {
            if (!$row || !array_key_exists($key, $row)) {
                $ERROR = "id-key '{$key}' is missing in your data, and primary key '{$primary}' is also missing";
                return null;
            }
            $cond[$key] = $row[$key];
        }
    }
    return $cond;
}
Example #2
0
function _app_prepare_data($table, $querydata, $mode)
{
    global $SCHEMA;
    global $PERM;
    global $TOOL, $TOOL_NAMES;
    $singular = _db_singular($table);
    $data = $TOOL;
    $data["DATA"] = db_force_data($table, $querydata);
    $data["TABLE"] = $table;
    $data["PRIMARY"] = _db_primary($table);
    $data["PRIMARIES"] = split(",", $data["PRIMARY"]);
    $data["UNIQUE"] = _db_unique($table);
    $data["UNIQUES"] = split(",", $data["UNIQUE"]);
    $data["PREFIX"] = "";
    $data["SUFFIX"] = "";
    $data["PERM"] = $PERM;
    $data["SCHEMA"] = $SCHEMA;
    $data["ACTION_SELF"] = $_SERVER["PHP_SELF"];
    $action = dirname($_SERVER["PHP_SELF"]) . "/index.php?table={$table}";
    $data["ACTION_BASE"] = $action;
    // generate link parameters reflecting current tools
    foreach ($TOOL_NAMES as $tool) {
        if (@$TOOL[$tool]) {
            $action .= "&{$tool}=" . htmlspecialchars($TOOL[$tool]);
        }
    }
    $data["ACTION"] = $action;
    if ($mode == "insert") {
        // check if immutable values are missing
        $immutable = array();
        $oldrec = $data["DATA"][0];
        $done = 0;
        foreach ($SCHEMA[$table]["FIELDS"] as $field => $info) {
            if (@$info["IMMUTABLE"]) {
                if (@$oldrec[$field]) {
                    $done++;
                }
                //echo "immutable: $field<br>\n";
                $immutable[$field] = true;
                $oldrec[$field] = @$oldrec[$field];
                // ensure the key exists
            }
        }
        //echo "done: $done<br>\n";
        if ($immutable && $done < count($immutable)) {
            // bail out any other fields
            $mode = "prepare";
            $data["FIELDS"] = array();
            $data["DATA"] = array();
            foreach ($oldrec as $field => $value) {
                if (@$immutable[$field]) {
                    //echo "transferring: $field<br>\n";
                    $data["FIELDS"][$field] = $field;
                    $data["DATA"][0][$field] = $value;
                }
            }
        } else {
            $immutable = array();
        }
    }
    $data["MODE"] = $mode;
    $data["IMMUTABLE"] = @$immutable;
    return $data;
}