コード例 #1
0
ファイル: DAVE.php プロジェクト: simonfoxe/PHP-DAVE-API
function _EDIT($Table, $VARS = null)
{
    global $TABLES, $DBOBJ, $Connection, $PARAMS;
    if ($VARS == null) {
        $VARS = $PARAMS;
    }
    if (_tableCheck($Table)) {
        $UniqueVars = _getUniqueTableVars($Table);
        $RequiredVars = _getRequiredTableVars($Table);
        $AllTableVars = _getAllTableCols($Table);
        $SQLKeys = array();
        $SQLValues = array();
        $Status = $DBOBJ->GetStatus();
        if ($Status !== true) {
            return array(false, $Status);
        }
        // get the META KEY if it wasn't provided explicitly
        if ($VARS[$TABLES[$Table]['META']['KEY']] == "") {
            $SQL = 'SELECT ' . $TABLES[$Table]['META']['KEY'] . ' FROM `' . $Table . '` WHERE ( ';
            $NeedAnd = false;
            foreach ($VARS as $var => $val) {
                if (in_array($var, $UniqueVars) && $val != "") {
                    if ($NeedAnd) {
                        $SQL .= " AND ";
                    }
                    $SQL .= ' `' . $var . '` = "' . $val . '" ';
                    $NeedAnd = true;
                }
            }
            $SQL .= ' ) ;';
            $DBOBJ->Query($SQL);
            $Status = $DBOBJ->GetStatus();
            if ($Status === true) {
                $results = $DBOBJ->GetResults();
                if (count($results) == 1) {
                    $VARS[$TABLES[$Table]['META']['KEY']] = $results[0][$TABLES[$Table]['META']['KEY']];
                } else {
                    return array(false, "You need to supply the META KEY for this table, " . $TABLES[$Table]['META']['KEY']);
                }
            } else {
                return array(false, "You need to supply the META KEY for this table, " . $TABLES[$Table]['META']['KEY'] . ", or one of the unique keys.");
            }
        }
        //loop
        if (is_array($VARS)) {
            foreach ($VARS as $var => $val) {
                if ($var != $TABLES[$Table]['META']['KEY']) {
                    // if (in_array($var, $RequiredVars) && _isSpecialString($val)) // required
                    // {
                    // 		return array(false,$var." is a required value and you must provide a value");
                    // }
                    if (in_array($var, $AllTableVars)) {
                        if (in_array($var, $UniqueVars) && strlen($val) > 0) {
                            $SQL = 'SELECT COUNT(1) FROM `' . $Table . '` WHERE (`' . $var . '` = "' . $val . '" AND `' . $TABLES[$Table]['META']['KEY'] . '` != "' . $VARS[$TABLES[$Table]['META']['KEY']] . '") ;';
                            $DBOBJ->Query($SQL);
                            $Status = $DBOBJ->GetStatus();
                            if ($Status === true) {
                                $results = $DBOBJ->GetResults();
                                if ($results[0]['COUNT(1)'] > 0) {
                                    return array(false, "There is already an entry of '" . $val . "' for " . $var);
                                } else {
                                    $SQLKeys[] = $var;
                                    $SQLValues[] = $val;
                                }
                            }
                        } elseif (strlen($val) > 0) {
                            $SQLKeys[] = $var;
                            $SQLValues[] = $val;
                        }
                    }
                }
            }
        }
        //
        if (strlen($VARS[$TABLES[$Table]['META']['KEY']]) > 0) {
            if (count($SQLKeys) > 0) {
                $SQL = "UPDATE `" . $Table . "` SET ";
                $i = 0;
                $needComma = false;
                while ($i < count($SQLKeys)) {
                    if ($needComma) {
                        $SQL .= ", ";
                    }
                    $SQL .= ' `' . $SQLKeys[$i] . '` = "' . mysql_real_escape_string($SQLValues[$i], $Connection) . '" ';
                    $needComma = true;
                    $i++;
                }
                $SQL .= ' WHERE ( `' . $TABLES[$Table]['META']['KEY'] . '` = "' . $VARS[$TABLES[$Table]['META']['KEY']] . '" ); ';
                $DBOBJ->Query($SQL);
                $Status = $DBOBJ->GetStatus();
                if ($Status === true) {
                    $NewKey = $DBOBJ->GetLastInsert();
                    return _VIEW($Table, $VARS);
                    // do a view again to return fresh data
                } else {
                    return array(false, $Status);
                }
            } else {
                return array(false, "There is nothing to change");
            }
        } else {
            return array(false, "You need to provide a parameter for the KEY of this table, " . $VARS[$TABLES[$Table]['META']['KEY']]);
        }
    } else {
        return array(false, "This table cannot be found");
    }
}
コード例 #2
0
ファイル: DAVE.php プロジェクト: simonfoxe/PHP-DAVE-API
function _EDIT($Table, $VARS = null)
{
    global $TABLES, $DBOBJ, $Connection, $PARAMS;
    if ($VARS == null) {
        $VARS = $PARAMS;
    }
    $UniqueVars = _getUniqueTableVars($Table);
    $RequiredVars = _getRequiredTableVars($Table);
    $attrs = array();
    $Status = $DBOBJ->GetStatus();
    if ($Status !== true) {
        return array(false, $Status);
    }
    $MongoDB = $DBOBJ->GetMongoDB();
    $Collection = $MongoDB->{$Table};
    $resp = _VIEW($Table, $VARS);
    if ($resp[0] == false) {
        return array(false, $resp[1]);
    }
    if (count($resp[1]) > 1) {
        return array(false, "You need to supply the META KEY for this table, " . $TABLES[$Table]['META']['KEY']);
    }
    if (count($resp[1]) == 0) {
        $msg = "You have supplied none of the required parameters to make this edit.  At least one of the following is required: ";
        foreach ($UniqueVars as $var) {
            $msg .= $var . " ";
        }
        return array(false, $msg);
    }
    if ($VARS[$TABLES[$Table]['META']['KEY']] == "") {
        $VARS[$TABLES[$Table]['META']['KEY']] = $resp[1][0][$TABLES[$Table]['META']['KEY']];
    }
    $current_values = $resp[1][0];
    $new_data = false;
    foreach ($VARS as $var => $val) {
        if ($var != $TABLES[$Table]['META']['KEY']) {
            if (in_array($var, $UniqueVars) && strlen($val) > 0 && $val != $current_values[$var]) {
                $count = $Collection->count(array($var => $val));
                if ($count > 0) {
                    return array(false, "There is already an entry of '" . $val . "' for " . $var);
                } else {
                    $attrs[$var] = $val;
                }
            } elseif (strlen($val) > 0) {
                $attrs[$var] = $val;
            }
            if ($attrs[$var] != $current_values[$var] && $var != $TABLES[$Table]['META']['KEY']) {
                $new_data = true;
            }
        }
    }
    // fill in old values
    foreach ($current_values as $var => $val) {
        if (empty($attrs[$var])) {
            if (is_object($val) == false) {
                $attrs[$var] = $val;
            }
        }
    }
    if (count($attrs) > 0 && $new_data) {
        $MongoId = new MongoID($VARS[$TABLES[$Table]['META']['KEY']]);
        $resp = $Collection->update(array("_id" => $MongoId), $attrs);
        if ($resp === true) {
            return _VIEW($Table, $VARS);
            // do a view again to return fresh data
        } else {
            return array(false, $Status);
        }
    } else {
        return array(false, "There is nothing to change");
    }
}