/** * createRectypes - Function that inserts a new rectype into defRecTypes table.and use the rty_ID to insert any * fields into the defRecStructure table * @author Stephen White * @param $commonNames an array valid column names in the defRecTypes table which match the order of data in the $rt param * @param $dtFieldNames an array valid column names in the defRecStructure table * @param $rt astructured array of which can contain the column names and data for one or more rectypes with fields * @param $icon_filename - filename from icon library - for new record type ONLY * @return $ret an array of return values for the various data elements created or errors if they occurred **/ function createRectypes($commonNames, $rt, $isAddDefaultSetOfFields, $convertTitleMask = true, $icon_filename = null) { global $mysqli, $rtyColumnNames; $ret = null; if (count($commonNames)) { $colNames = join(",", $commonNames); $parameters = array(""); $titleMask = null; $query = ""; $querycols = ""; foreach ($commonNames as $colName) { $val = array_shift($rt[0]['common']); if (@$rtyColumnNames[$colName]) { //keep value of text title mask to create canonical one if ($convertTitleMask && $colName == "rty_TitleMask") { $titleMask = $val; } if ($query != "") { $query = $query . ","; $querycols = $querycols . ","; } $querycols = $querycols . $colName; $query = $query . "?"; $parameters = addParam($parameters, $rtyColumnNames[$colName], $val); } } $query = "insert into defRecTypes ({$querycols}) values ({$query})"; $rows = execSQL($mysqli, $query, $parameters, true); if ($rows == "1062") { $ret = "Record type with specified name already exists in the database, please use the existing record type\nThis type may be hidden - turn it on through Database > Manage structure"; } else { if ($rows == 0 || is_string($rows)) { $ret = "SQL error inserting data into table defRecTypes: " . $rows; } else { $rtyID = $mysqli->insert_id; $ret = -$rtyID; if ($isAddDefaultSetOfFields) { //add default set of detail types addDefaultFieldForNewRecordType($rtyID); } //create canonical title mask if ($titleMask) { updateTitleMask($rtyID, $titleMask); } $need_create_icon = true; if ($icon_filename) { $need_create_icon = copy_IconAndThumb_FromLibrary($rtyID, $icon_filename); } //create icon and thumbnail if ($need_create_icon) { getRectypeIconURL($rtyID); getRectypeThumbURL($rtyID); } } } } if ($ret == null) { $ret = "no data supplied for inserting record type"; } return $ret; }
} } } } else { error_exit("Can't update record type structure rectype#" . $rectypes_correspondence[$rtyID] . ". " . $ret); } } } // ------------------------------------------------------------------------------------------------ // VII. Update titlemasks with new ids $mysqli->commit(); foreach ($imp_recordtypes as $rtyID) { if (@$rectypes_correspondence[$rtyID]) { $mask = $def_rts[$rtyID]['commonFields'][$idx_titlemask_canonical]; // note we use special global array $fields_correspondence - for proper conversion of remote id to concept code $res = updateTitleMask($rectypes_correspondence[$rtyID], $mask); if (!is_numeric($res)) { error_exit($res); } } } $mysqli->commit(); $mysqli->close(); // ------------------------------------------------------------------------------------------------ // Confirmation of import results if ($outputFormat == "json") { header("Content-type: text/javascript"); } else { $trg_rectypes = getAllRectypeStructures(); $trg_fieldtypes = getAllDetailTypeStructures(); $trg_terms = getTerms();
function import() { global $error, $importLog, $tempDBName, $sourceDBName, $targetDBName, $sourceDBID, $importRtyID, $importedRecTypes; $importedRecTypes = array(); $error = false; $importLog = array(); if (!$tempDBName || $tempDBName === "" || !$targetDBName || $targetDBName === "" || !$sourceDBID || !is_numeric($sourceDBID) || !$importRtyID || !is_numeric($importRtyID)) { makeLogEntry("importParameters", -1, "One or more required import parameters not supplied or incorrect form ( " . "importingDBName={name of target DB} sourceDBID={reg number of source DB or 0} " . "importRtyID={numeric ID of record type} tempDBName={temp db name where source DB type data are held}"); $error = true; } $startedTransaction = false; if (!$error) { mysql_query("start transaction"); $startedTransaction = true; // Get recordtype data that has to be imported $res = mysql_query("select * from " . $tempDBName . ".defRecTypes where rty_ID = " . $importRtyID); if (mysql_num_rows($res) == 0) { $error = true; makeLogEntry("Record type", $importRtyID, " was not found in local, temporary copy of, source database ({$sourceDBName})"); } else { $importRty = mysql_fetch_assoc($res); } // check if rectype already imported, if so return the local id. if (!$error && $importRty) { $origRtyName = $importRty["rty_Name"]; $replacementName = @$_GET["replaceRecTypeName"]; if ($replacementName && $replacementName != "") { $importRty["rty_Name"] = $replacementName; $importRty["rty_Plural"] = ""; //TODO need better way of determining the plural } if ($importRty["rty_OriginatingDBID"] == 0 || $importRty["rty_OriginatingDBID"] == "") { $importRty["rty_OriginatingDBID"] = $sourceDBID; $importRty["rty_IDInOriginatingDB"] = $importRtyID; $importRty["rty_NameInOriginatingDB"] = $origRtyName; } //lookup rty in target DB $resRtyExist = mysql_query("select rty_ID from " . $targetDBName . ".defRecTypes " . "where rty_OriginatingDBID = " . $importRty["rty_OriginatingDBID"] . " AND rty_IDInOriginatingDB = " . $importRty["rty_IDInOriginatingDB"]); // Rectype is not in target DB so import it $localRtyID = null; if (mysql_num_rows($resRtyExist) > 0) { $localRtyID = mysql_fetch_array($resRtyExist, MYSQL_NUM); $localRtyID = $localRtyID[0]; makeLogEntry("Record type", $importRtyID, " ALREADY EXISTS in {$targetDBName} as ID = {$localRtyID}"); } $localRtyID = importRectype($importRty, $localRtyID); if ($localRtyID) { array_push($importedRecTypes, $importRty["rty_ID"]); } } } // successful import if (!$error) { if ($startedTransaction) { mysql_query("commit"); } $mask = $importRty["rty_TitleMask"]; // note we use special global array $fields_correspondence - for proper conversion of remote id to concept code $res = updateTitleMask($localRtyID, $mask); if (!is_numeric($res)) { makeLogEntry("Error convertion title mask", $localRtyID, $res); } $statusMsg = ""; if (sizeof($importLog) > 0) { foreach ($importLog as $logLine) { echo $logLine[0] . (intval($logLine[1]) < 0 ? "" : " #" . $logLine[1] . " ") . $logLine[2] . "<br />"; } } echo "Successfully imported record type '" . $importRty["rty_Name"] . "' from " . $sourceDBName . "<br />"; echo "<br />"; echo "IMPORTED:" . implode(",", $importedRecTypes); sendReportEmail($importRty, $localRtyID); return $localRtyID; // duplicate record found } else { if (substr(mysql_error(), 0, 9) == "Duplicate") { if ($startedTransaction) { mysql_query("rollback"); } echo "prompt"; //general error condition } else { if (isset($startedTransaction) && $startedTransaction) { mysql_query("rollback"); } if (mysql_error()) { $statusMsg = "MySQL error: " . mysql_error() . "<br />"; } else { $statusMsg = "Error:<br />"; } if (sizeof($importLog) > 0) { foreach ($importLog as $logLine) { $statusMsg .= $logLine[0] . (intval($logLine[1]) < 0 ? "" : " #" . $logLine[1] . " ") . $logLine[2] . "<br />"; } $statusMsg .= "Changes rolled back, nothing was imported"; } // TODO: Delete all information that has already been imported (retrieve from $importLog) echo $statusMsg; } } }