コード例 #1
0
function save_custom_class_to_db($a_values, $i_user_id, $sem, $year)
{
    global $maindb;
    // index the array
    foreach ($a_values as $k => $v) {
        unset($a_values[$k]);
        $a_values[$v->name] = $v->value;
    }
    // standardize the inputs
    $semester_string = number_to_season($sem);
    $semester_string = strtolower(substr($semester_string, 0, 3));
    $realyear = school_to_real_year($year, $sem);
    $a_matches = array();
    $a_values["Days"] = strtoupper($a_values["Days"]);
    $a_values["Days"] = preg_replace("/[^UMTWRFS]+/", "", $a_values["Days"]);
    preg_match('/U?M?T?W?R?F?S?/', $a_values["Days"], $a_matches);
    $a_values["Days"] = count($a_matches) > 0 ? $a_matches[0] : "";
    $a_values["Time"] = preg_replace("/[^\\d-]+/", "", $a_values["Time"]);
    preg_match('/\\d\\d\\d\\d-\\d\\d\\d\\d/', $a_values["Time"], $a_matches);
    $a_values["Time"] = count($a_matches) > 0 ? $a_matches[0] : "";
    foreach (array("Hrs", "Limit") as $cat) {
        $a_values[$cat] = preg_replace("/[^\\d]+/", "", $a_values[$cat]);
    }
    // check that none of the fields are blank
    foreach ($a_values as $k => $v) {
        if ($v == "") {
            return json_encode(array(new command("failure", "Failure: bad value for {$k}")));
        }
    }
    // get the next crn for custom classes
    $i_crn = 1;
    $a_custom_classes = db_query("SELECT `crn` FROM `{$maindb}`.`classes` WHERE `semester`='[sem]' AND `year`='[year]' AND `subject`='CUSTOM' ORDER BY `crn` DESC LIMIT 1", array("sem" => $semester_string, "year" => $realyear));
    if (count($a_custom_classes) > 0) {
        $i_crn = (int) $a_custom_classes[0]["crn"];
        $i_crn++;
        // check that it doesn't conflict with other types of classes
        $query_string = "SELECT `crn` FROM `{$maindb}`.`classes WHERE `semester`='[sem]' AND `year`='[year]' AND `crn`='[crn]'";
        $query_vars = array("sem" => $semester_string, "year" => $realyear, "crn" => $i_crn);
        $a_class = db_query($query_string, $query_vars);
        while ($a_class !== FALSE && count($a_class) > 0) {
            $i_crn++;
            $query_vars["crn"] = $i_crn;
            $a_class = db_query($query_string, $query_vars);
        }
    }
    // find some specific information
    $a_days = str_split($a_values["Days"]);
    $a_days_times_locations = array();
    foreach ($a_days as $s_day) {
        $a_days_times_locations[] = array($s_day, $a_values["Time"], $a_values["Location"]);
    }
    $s_days_times_locations = json_encode($a_days_times_locations);
    $a_dates = getStartEndDays($semester_string, $realyear);
    // build the class
    $a_class = array("crn" => $i_crn, "year" => $realyear, "semester" => $semester_string, "subject" => "CUSTOM", "course" => "CUSTOM {$i_crn}", "campus" => $a_values["*Campus"], "days" => $a_values["Days"], "days_times_locations" => $s_days_times_locations, "start_date" => $a_dates["start"], "end_date" => $a_dates["end"], "time" => $a_values["Time"], "location" => $a_values["Location"], "hours" => $a_values["Hrs"], "title" => $a_values["Title"], "instructor" => $a_values["Instructor"], "seats" => 0, "limit" => $a_values["Limit"], "enroll" => 0, "parent_class" => "", "subclass_identifier" => "", "user_ids_with_access" => "rwx|{$i_user_id},", "last_mod_time" => date("Y-m-d H:i:s"));
    // insert into the database
    $s_insert_clause = array_to_insert_clause($a_class);
    $query = db_query("INSERT INTO `{$maindb}`.`classes` {$s_insert_clause}", $a_class);
    if ($query !== FALSE) {
        return json_encode(array(new command("success", "")));
    }
    return json_encode(array(new command("failure", "failure")));
}
コード例 #2
0
function updateCommon_Data($a_curr_common_data, $a_common_data)
{
    global $maindb;
    global $mysqli;
    echo "<pre>";
    foreach ($a_common_data as $a_table) {
        $s_tablename = $mysqli->real_escape_string($a_table["name"]);
        $s_index = $mysqli->real_escape_string($a_table["index"]);
        echo "importing table {$s_tablename}\n";
        $i_rows_to_import = 0;
        $i_total_rows = 0;
        foreach ($a_table["rows"] as $a_row) {
            $b_found = FALSE;
            foreach ($a_curr_common_data as $a_curr_table) {
                if ($a_curr_table["name"] != $s_tablename) {
                    continue;
                }
                foreach ($a_curr_table["rows"] as $a_curr_acc) {
                    if ($a_curr_acc[$s_index] == $a_row[$s_index]) {
                        if (print_r($a_row, TRUE) != print_r($a_curr_acc, TRUE)) {
                            db_query("UPDATE `{$maindb}`.`{$s_tablename}` SET " . array_to_update_clause($a_row) . " WHERE `{$s_index}`='[{$s_index}]'", $a_row, 1);
                            echo "\n";
                        }
                        $b_found = TRUE;
                        break;
                    }
                }
                break;
            }
            $i_total_rows++;
            if (!$b_found) {
                $i_rows_to_import++;
                db_query("INSERT INTO `{$maindb}`.`{$s_tablename}` " . array_to_insert_clause($a_row), $a_row, 1);
                echo "\n";
            }
        }
        echo "imported {$i_rows_to_import}/{$i_total_rows} rows\n";
        echo "\n";
    }
    echo "</pre>";
}
コード例 #3
0
ファイル: forum.php プロジェクト: op-erator/banwebplus
 public function handleRespondPostAJAX($post_id)
 {
     global $maindb;
     // check if the user has permission
     if (!$this->user->has_access($this->s_createaccess)) {
         return json_encode(array(new command("alert", "Incorrect permissions")));
     }
     // check that the post exists that we're trying to create a response to
     $a_posts = db_query("SELECT `id` FROM `{$maindb}`.`[table]` WHERE `id`='[id]' LIMIT 1", array("table" => $this->s_tablename, "id" => $post_id));
     if (!is_array($a_posts) || count($a_posts) == 0) {
         return json_encode(array(new command("alert", "Original post not found, possible error in database")));
     }
     // create the response
     $a_insert_response = array("userid" => $this->user->get_id(), "owner_userid" => $this->user->get_id(), "datetime" => date("Y-m-d H:i:s"), "is_response" => 1, "original_post_id" => $post_id);
     $s_insert_response = array_to_insert_clause($a_insert_response);
     $query = db_query("INSERT INTO `{$maindb}`.`[table]` {$s_insert_response}", array_merge($a_insert_response, array("table" => $this->s_tablename)));
     if ($query === FALSE) {
         return json_encode(array(new command("alert", "Failed to insert into database")));
     }
     return json_encode(array(new command("reload page", "")));
 }
コード例 #4
0
ファイル: php_to_mysql.php プロジェクト: op-erator/banwebplus
function saveData($s_semester, $s_year, $a_data_to_save, $a_keys, $s_primary_key, $s_table, $exclude_comparison_columns = NULL, $a_searchby = NULL)
{
    global $maindb;
    global $mysqli;
    // compiles the keys
    $s_keylist = "`" . implode("`,`", $a_keys) . "`";
    $a_exclude_comparison_columns = array();
    if ($exclude_comparison_columns !== NULL && count($exclude_comparison_columns) > 0) {
        foreach ($exclude_comparison_columns as $k => $v) {
            $a_exclude_comparison_columns[$v] = 0;
        }
    }
    // load existing data from the database
    // loads them each as an "primary_key"=>array("key"=>value, ...)
    $a_searchby = $a_searchby === NULL ? array() : $a_searchby;
    $a_searchby = array_merge(array("semester" => $s_semester, "year" => $s_year), $a_searchby);
    if ($s_table == "classes") {
        $a_searchby = array_merge(array("user_ids_with_access" => ""), $a_searchby);
    }
    $s_where_clause = array_to_where_clause($a_searchby);
    $db_data_loaded = db_query("SELECT {$s_keylist} FROM `{$maindb}`.`{$s_table}` WHERE {$s_where_clause} ORDER BY `{$s_primary_key}`", $a_searchby);
    $s_where_clause = $s_where_clause == "" ? "" : "AND {$s_where_clause}";
    $db_data = array();
    foreach ($db_data_loaded as $db_row) {
        $db_data[$db_row[$s_primary_key]] = $db_row;
    }
    // determine what data has not already been saved,
    // and which should be removed
    $data_to_add = array();
    $data_to_remove = array();
    $data_to_change = array();
    foreach ($a_data_to_save as $k => $a_row) {
        $primary_value = $a_row[$s_primary_key];
        // decided if it should be changed or inserted
        $row_exists = FALSE;
        if (isset($db_data[$primary_value])) {
            $row_exists = TRUE;
            // build the comparison for updating
            if (count($a_exclude_comparison_columns) == 0) {
                $s_db_row = implode(",", $db_data[$primary_value]);
                $s_tosave_row = implode(",", $a_row);
            } else {
                $a_row1 = array_diff_key($db_data[$primary_value], $a_exclude_comparison_columns);
                $a_row2 = array_diff_key($a_row, $a_exclude_comparison_columns);
                $s_db_row = implode(",", $a_row1);
                $s_tosave_row = implode(",", $a_row2);
            }
            // compare for updates
            if ($s_db_row != $s_tosave_row) {
                $data_to_change[$primary_value] = $a_row;
            }
        }
        if (!$row_exists) {
            // should be added
            $data_to_add[$primary_value] = $a_row;
        } else {
            unset($db_data[$primary_value]);
        }
        unset($a_data_to_save[$primary_value]);
    }
    foreach ($db_data as $primary_value => $a_db_row) {
        // delete everything else
        $data_to_remove[$primary_value] = $primary_value;
        unset($db_data[$primary_value]);
    }
    echo "update: " . count($data_to_change) . "\ndelete: " . count($data_to_remove) . "\ninsert: " . count($data_to_add) . "\n";
    // change, then remove, then add
    foreach ($data_to_change as $a_row) {
        $s_update_clause = array_to_update_clause($a_row);
        $success = db_query("UPDATE `{$maindb}`.`{$s_table}` SET {$s_update_clause} WHERE `{$s_primary_key}`='[{$s_primary_key}]' {$s_where_clause}", array_merge($a_searchby, $a_row));
        if ($success === FALSE) {
            echo $mysqli->error() . "\n";
        }
    }
    foreach ($data_to_remove as $primary_value) {
        $success = db_query("DELETE FROM `{$maindb}`.`{$s_table}` WHERE `{$s_primary_key}`='[{$s_primary_key}]' {$s_where_clause}", array_merge($a_searchby, array("{$s_primary_key}" => $primary_value)));
        if ($success === FALSE) {
            echo $mysqli->error() . "\n";
        }
    }
    foreach ($data_to_add as $a_row) {
        $a_row = array_merge($a_row, array("year" => $s_year, "semester" => $s_semester));
        $s_insert_clause = array_to_insert_clause($a_row);
        $success = db_query("INSERT INTO `{$maindb}`.`{$s_table}` {$s_insert_clause}", $a_row);
        if ($success === FALSE) {
            echo $mysqli->error() . "\n";
        }
    }
}