/**
* Assign record ids to field in import table
*
* @param mixed $mysqli
* @param mixed $imp_session
* @param mixed $params
*
* Not used anymore!!!! Redirection to  assignMultivalues
*/
function matchingAssign($mysqli, $imp_session, $params)
{
    //Now we always use it
    if (true || @$params['mvm'] > 0) {
        return assignMultivalues($mysqli, $imp_session, $params);
    }
    //everything below is NOT IN USE!!!
    $import_table = $imp_session['import_table'];
    //get rectype to import
    $recordType = @$params['sa_rectype'];
    if (intval($recordType) < 1) {
        return "record type not defined";
    }
    $id_field = @$params['idfield'];
    $field_count = count($imp_session['columns']);
    if (!$id_field) {
        //add new field into import table
        //ID field not defined, create new field
        $id_field_idx = $field_count;
        $id_field = "field_" . $field_count;
        array_push($imp_session['columns'], @$params['new_idfield'] ? $params['new_idfield'] : "ID field for Record type #{$recordType}");
        array_push($imp_session['uniqcnt'], 0);
        //$imp_session["mapping"][$id_field] = $recordType.".id"; //$recTypeName."(id# $recordType) ID";
        $imp_session['indexes'][$id_field] = $recordType;
        $altquery = "alter table " . $import_table . " add column " . $id_field . " int(10) ";
        if (!$mysqli->query($altquery)) {
            return "SQL error: cannot alter import session table, cannot add new index field: " . $mysqli->error;
        }
    } else {
        $id_field_idx = substr($id_dield, 6);
    }
    if (@$imp_session['indexes_keyfields'] && @$imp_session['indexes_keyfields'][$id_field]) {
        unset($imp_session['indexes_keyfields'][$id_field]);
    }
    //remove from multivals
    $k = array_search($id_field_idx, $imp_session['multivals']);
    if ($k !== false) {
        unset($imp_session['multivals'][$k]);
    }
    //get fields that will be used in search
    $sel_query = array();
    //for update
    $select_query_update_from = array($import_table, "Records");
    $select_query_update_where = array("rec_RecTypeID=" . $recordType);
    $detDefs = getAllDetailTypeStructures(true);
    $detDefs = $detDefs['typedefs'];
    $idx_dt_type = $detDefs['fieldNamesToIndex']['dty_Type'];
    foreach ($params as $key => $field_type) {
        if (strpos($key, "sa_keyfield_") === 0 && $field_type) {
            //get index
            $index = substr($key, 12);
            $field_name = "field_" . $index;
            array_push($sel_query, $field_name);
            if ($field_type == "id" || $field_type == "url" || $field_type == "scratchpad") {
                array_push($select_query_update_where, "rec_" . $field_type . "=" . $field_name);
            } else {
                $dt_type = $detDefs[$field_type]['commonFields'][$idx_dt_type];
                $where = "d" . $index . ".dtl_DetailTypeID=" . $field_type . " and ";
                if ($dt_type == "enum" || $dt_type == "relationtype") {
                    //if fieldname is numeric - compare it with dtl_Value directly
                    $where = $where . " d" . $index . ".dtl_Value=t" . $index . ".trm_ID and " . " t" . $index . ".trm_Label={$field_name} ";
                    array_push($select_query_update_from, "defTerms t" . $index);
                } else {
                    $where = $where . " (d" . $index . ".dtl_Value=" . $field_name . ")";
                }
                array_push($select_query_update_where, "rec_ID=d" . $index . ".dtl_RecID and " . $where);
                array_push($select_query_update_from, "recDetails d" . $index);
            }
        }
    }
    if (count($sel_query) < 1) {
        return "no one key field is selected";
    }
    //query to search record ids
    //to update - assign existing rec_ID from heurist
    //SET SQL_SAFE_UPDATES=1;
    //reset all values
    $updquery = "UPDATE " . $import_table . " SET " . $id_field . "=NULL WHERE imp_id>0";
    if (!$mysqli->query($updquery)) {
        return "SQL error: cannot update import table (cannot clear record ID field). " . $updquery;
    }
    //matched records
    $updquery = "UPDATE " . implode(",", $select_query_update_from) . " SET " . $id_field . "=rec_ID WHERE " . implode(" and ", $select_query_update_where) . " and imp_id>0";
    if (!$mysqli->query($updquery)) {
        return "SQL error: cannot update import table (set record id field) " . $updquery;
    }
    //new records   ".implode(",",$sel_query).",
    $mysqli->query("SET SESSION group_concat_max_len = 1000000");
    //FIND RECORDS FOR INSERT
    $select_query = "SELECT group_concat(imp_id), " . implode(",", $sel_query) . " FROM " . $import_table . " WHERE (imp_id NOT IN " . " (SELECT imp_id " . " FROM " . implode(",", $select_query_update_from) . " WHERE " . implode(" and ", $select_query_update_where) . ")) GROUP BY " . implode(",", $sel_query);
    $res = $mysqli->query($select_query);
    if ($res) {
        $ind = -1;
        while ($row = $res->fetch_row()) {
            $ids = $row[0];
            if (substr($ids, -1) == ",") {
                $ids = substr($row[0], 0, -1);
            }
            $ids = explode(",", $ids);
            $k = 0;
            while ($k < count($ids)) {
                $ids_part = array_slice($ids, $k, 100);
                $updquery = "update " . $import_table . " set " . $id_field . "=" . $ind . " where imp_id in (" . implode(",", $ids_part) . ")";
                //end($row)
                if (!$mysqli->query($updquery)) {
                    return "SQL error: cannot update import table: mark records for insert. " . $updquery;
                }
                $k = $k + 100;
            }
            $ind--;
        }
    } else {
        return "SQL error: cannot perform query to find unmatched records " . $select_query;
    }
    //calculate distinct number of ids
    $row = mysql__select_array2($mysqli, "select count(distinct " . $id_field . ") from " . $import_table);
    if (is_array($row)) {
        $index = intval(substr($id_field, 6));
        $imp_session['uniqcnt'][$index] = $row[0];
        //save index field into import_session
        $imp_session = saveSession($mysqli, $imp_session);
        if (!is_array($imp_session)) {
            return $imp_session;
        }
    }
    return $imp_session;
}
Beispiel #2
0
            if ($step == 2) {
                //find  - NOT USED ANYMORE  - we trying to assign IDs at once
                // ARTEM TODO: REMOVE REDUNDANT CODE
                ob_start();
                echo '<script>showProgressMsg("Please wait, matching in progress")</script>';
                ob_flush();
                flush();
                $res = matchingSearch($mysqli, $imp_session, $_REQUEST);
            } else {
                if ($step == 3) {
                    //assign ids
                    ob_start();
                    echo '<script>showProgressMsg("Please wait, assign of records ids")</script>';
                    ob_flush();
                    flush();
                    $res = assignMultivalues($mysqli, $imp_session, $_REQUEST);
                    //NOT USED ANYMORE $res = matchingAssign($mysqli, $imp_session, $_REQUEST);
                    if (is_array($res) && count(@$res['validation']['disambiguation']) > 0) {
                        //There is ambiguity with your matching criteria. Please resolve it before proceeding further
                        ?>

                    <script>
                        form_vals["error_message"] = "One or more rows in your file match multiple records in the database. "+
                        "Please click on \"Rows with ambiguous match\" to view and resolve these ambiguous matches.<br><br> "+
                        "If you have many such ambiguities you may need to select adidtional key fields or edit the incoming "+
                        "data file to add further matching information.";
                    </script>

                    <?php 
                    } else {
                        print '<script>form_vals["auto_switch_to_import"] = "1";</script>';