function copyRectypeIcon($sourceDBName, $importRtyID, $importedRecTypeID, $thumb = "") { // BUG TODO: this is not always true, and what about cross server $filename = HEURIST_UPLOAD_ROOT . $sourceDBName . "/rectype-icons/" . $thumb . $importRtyID . ".png"; if (file_exists($filename)) { $target_filename = HEURIST_ICON_DIR . $thumb . $importedRecTypeID . ".png"; if (file_exists($target_filename)) { unlink($target_filename); } if (!copy($filename, $target_filename)) { makeLogEntry("<b>Warning</b> Importing Record-type", $importRtyID, " Can't copy " . ($thumb == "" ? "icon" : "thumbnail") . " " . $filename . " to " . $target_filename); } } else { makeLogEntry("<b>Warning</b> Importing Record-type", $importRtyID, " " . ($thumb == "" ? "icon" : "thumbnail") . " does not exist"); } if ($thumb == "") { copyRectypeIcon($sourceDBName, $importRtyID, $importedRecTypeID, "thumb/th_"); } }
function copyRectypeIcon($source_RtyID, $target_RtyID, $thumb = "") { global $sourceIconURL; $sourceURL = $sourceIconURL . $thumb . $source_RtyID . ".png"; $targetPath = HEURIST_ICON_DIR . $thumb . $target_RtyID . ".png"; //print "<br>sourcce=".$sourceURL; //print "<br>path=".$targetPath; saveURLasFile($sourceURL, $targetPath); if ($thumb == "") { copyRectypeIcon($source_RtyID, $target_RtyID, "thumb/th_"); } }
function importRectype($importRty) { global $error, $importLog, $tempDBName, $sourceDBName, $targetDBName, $sourceDBID; //was static $importRtyGroupID; $importRtyID = $importRty['rty_ID']; /*****DEBUG****/ //error_log("import rtyID $importRtyID to $targetDBName DB"); // Get Imported rectypeGroupID if (!$error) { // && !$importRtyGroupID) { //find group in source $query = "select * from " . $tempDBName . ".defRecTypeGroups where rtg_ID = " . $importRty['rty_RecTypeGroupID']; $res = mysql_query($query); if (mysql_num_rows($res) == 0) { makeLogEntry("<b>Error</b> Creating Record-type Group", -1, " Can not find group #" . $importRty['rty_RecTypeGroupID']); $error = true; } else { $rtyGroup_src = mysql_fetch_assoc($res); //find group by name in target $rtyGroup = mysql_query("select rtg_ID from " . $targetDBName . ".defRecTypeGroups where rtg_Name = '" . $rtyGroup_src['rtg_Name'] . "'"); if (mysql_num_rows($rtyGroup) == 0) { //not found //add new one mysql_query("INSERT INTO " . $targetDBName . ".defRecTypeGroups " . "(rtg_Name,rtg_Domain,rtg_Order, rtg_Description) " . "VALUES ('" . $rtyGroup_src['rtg_Name'] . "','" . $rtyGroup_src['rtg_Domain'] . "' , '" . $rtyGroup_src['rtg_Order'] . "'," . " '" . $rtyGroup_src['rtg_Description'] . "')"); if (mysql_error()) { $error = true; makeLogEntry("<b>Error</b> Creating Record-type Group", -1, ". Could not add record type group '" . $rtyGroup_src['rtg_Name'] . "' - " . mysql_error()); } else { $importRtyGroupID = mysql_insert_id(); makeLogEntry("Creating Record-type Group", -1, " '" . $rtyGroup_src['rtg_Name'] . "' as #{$importRtyGroupID}"); } } else { $row = mysql_fetch_row($rtyGroup); $importRtyGroupID = $row[0]; makeLogEntry("Using Record-type Group", -1, " '" . $rtyGroup_src['rtg_Name'] . "' as #{$importRtyGroupID}"); } } /* ARTEM: old way with Imported group // Finded 'Imported' rectype group or create it if it doesn't exist $rtyGroup = mysql_query("select rtg_ID from ".$targetDBName.".defRecTypeGroups where rtg_Name = 'Imported'"); if(mysql_num_rows($rtyGroup) == 0) { //not exist mysql_query("INSERT INTO ".$targetDBName.".defRecTypeGroups ". "(rtg_Name,rtg_Domain,rtg_Order, rtg_Description) ". "VALUES ('Imported','functionalgroup' , '999',". " 'This group contains all record types that were imported from external databases')"); // Write the insert action to $logEntry, and set $error to true if one occurred if(mysql_error()) { $error = true; makeLogEntry("<b>Error</b> Creating Record-type Group", -1, ". Could not find record type group 'Imported' - ".mysql_error()); } else { $importRtyGroupID = mysql_insert_id(); makeLogEntry("Creating Record-type Group", -1, " 'Imported' as #$importRtyGroupID"); } } else { $row = mysql_fetch_row($rtyGroup); $importRtyGroupID = $row[0]; makeLogEntry("Using Record-type Group", -1, " 'Imported' as #$importRtyGroupID"); } */ } /*****DEBUG****/ //error_log("import rty 3a"); if (!$error) { /*****DEBUG****/ //error_log("import rty 3aa"); // get rectype Fields and check they are not already imported $recStructuresByDtyID = array(); // get the rectypes structure $resRecStruct = mysql_query("select * from " . $tempDBName . ".defRecStructure where rst_RecTypeID = " . $importRtyID); while ($rtsFieldDef = mysql_fetch_assoc($resRecStruct)) { $importFieldDtyID = $rtsFieldDef['rst_DetailTypeID']; $recStructuresByDtyID[$importFieldDtyID] = $rtsFieldDef; // If this recstructure field has originating DBID 0 it's an original concept // need to set the origin DBID to the DB it is being imported from if ($rtsFieldDef["rst_OriginatingDBID"] == 0 || $rtsFieldDef["rst_OriginatingDBID"] == "") { $rtsFieldDef["rst_OriginatingDBID"] = $sourceDBID; $rtsFieldDef["rst_IDInOriginatingDB"] = $rtsFieldDef["rst_ID"]; } // check that field doesn't already exist $resRstExist = mysql_query("select rst_ID from " . $targetDBName . ".defRecStructure " . "where rst_OriginatingDBID = " . $rtsFieldDef["rst_OriginatingDBID"] . " AND rst_IDInOriginatingDB = " . $rtsFieldDef["rst_IDInOriginatingDB"]); if (mysql_num_rows($resRstExist)) { makeLogEntry("<b>Error</b> Importing Field", $rtsFieldDef["rst_ID"], " '" . $rtsFieldDef["rst_DisplayName"] . "' already exists detail type #{$importFieldDtyID} rectype #{$importRtyID}"); makeLogEntry("", -1, ". Originating DBID = " . $rtsFieldDef["rst_OriginatingDBID"] . " Originating Field #" . $rtsFieldDef["rst_IDInOriginatingDB"]); // $error = true; } } /*****DEBUG****/ //error_log("import rty 3b"); if (!$error) { //import rectype /*****DEBUG****/ //error_log("import rty 3bb"); $recTypeSuffix = 2; while (mysql_num_rows(mysql_query("select * from " . $targetDBName . ".defRecTypes where rty_Name = '" . $importRty["rty_Name"] . "'")) != 0) { $importRty["rty_Name"] = $importRty["rty_Name"] . $recTypeSuffix; makeLogEntry("Record type", $importRtyID, "Record type name used in the source DB already exist in the target DB({$targetDBName}) but with different concept code. Added suffix: " . $recTypeSuffix); $recTypeSuffix++; } // Change some recordtype fields to make it suitable for the new DB unset($importRty["rty_ID"]); $importRty["rty_RecTypeGroupID"] = $importRtyGroupID; $importRty["rty_Name"] = mysql_escape_string($importRty["rty_Name"]); $importRty["rty_Description"] = mysql_escape_string($importRty["rty_Description"]); $importRty["rty_Plural"] = mysql_escape_string($importRty["rty_Plural"]); $importRty["rty_NameInOriginatingDB"] = mysql_escape_string($importRty["rty_NameInOriginatingDB"]); $importRty["rty_ReferenceURL"] = mysql_escape_string($importRty["rty_ReferenceURL"]); $importRty["rty_AlternativeRecEditor"] = mysql_escape_string($importRty["rty_AlternativeRecEditor"]); // Insert recordtype mysql_query("INSERT INTO " . $targetDBName . ".defRecTypes " . "(" . implode(", ", array_keys($importRty)) . ") VALUES " . "('" . implode("', '", array_values($importRty)) . "')"); // Write the insert action to $logEntry, and set $error to true if one occurred if (mysql_error()) { $error = true; /*****DEBUG****/ //error_log("import rty $importRtyID 3bbb ". mysql_error()); makeLogEntry("Importing Record-type", $importRtyID, "MySQL error importing record type - " . mysql_error()); } else { $importedRecTypeID = mysql_insert_id(); makeLogEntry("Importing Record-type", $importRtyID, " '" . $importRty["rty_Name"] . "' as #{$importedRecTypeID}"); copyRectypeIcon($sourceDBName, $importRtyID, $importedRecTypeID); } } if (!$error) { // Import the structure for the recordtype imported foreach ($recStructuresByDtyID as $dtyID => $rtsFieldDef) { // get import detailType for this field $resDTY = mysql_query("select * from " . $tempDBName . ".defDetailTypes where dty_ID = {$dtyID}"); if (mysql_num_rows($resDTY) == 0) { $error = true; /*****DEBUG****/ //error_log("import rty $importRtyID 3cc dtyID = $dtyID not in source db "); makeLogEntry("<b>Error</b> Importing Field-type", $dtyID, " '" . $rtsFieldDef['rst_DisplayName'] . "' for record type #" . $rtsFieldDef['rst_RecTypeID'] . " not found in the source db. Please contact owner of {$sourceDBName}"); return null; // missing detatiltype in importing DB } else { $importDty = mysql_fetch_assoc($resDTY); /*****DEBUG****/ //error_log("Import dty is ".print_r($importDty,true)); } // If detailtype has originating DBID 0, set it to the DBID from the DB it is being imported from if (!$importDty["dty_OriginatingDBID"] || $importDty["dty_OriginatingDBID"] == 0 || $importDty["dty_OriginatingDBID"] == "") { $importDty["dty_OriginatingDBID"] = $sourceDBID; $importDty["dty_IDInOriginatingDB"] = $importDty['dty_ID']; $importDty["dty_NameInOriginatingDB"] = $importDty['dty_Name']; } // Check to see if the detailType for this field exist in the target DB $resExistingDty = mysql_query("select dty_ID from " . $targetDBName . ".defDetailTypes " . "where dty_OriginatingDBID = " . $importDty["dty_OriginatingDBID"] . " AND dty_IDInOriginatingDB = " . $importDty["dty_IDInOriginatingDB"]); // Detailtype is not in target DB so import it if (mysql_num_rows($resExistingDty) == 0) { /*****DEBUG****/ //error_log("import rty $importRtyID 4a dtyID = ".$importDty['dty_ID']); $rtsFieldDef["rst_DetailTypeID"] = importDetailType($importDty); /*****DEBUG****/ //error_log("import rty $importRtyID 4b dtyID = ".$importDty['dty_ID']."->".$rtsFieldDef["rst_DetailTypeID"]); } else { $existingDtyID = mysql_fetch_array($resExistingDty); $rtsFieldDef["rst_DetailTypeID"] = $existingDtyID[0]; /*****DEBUG****/ //error_log("import rty $importRtyID 5 dtyID = ".$importDty['dty_ID']."=".$rtsFieldDef["rst_DetailTypeID"]); } if (!$error && @$rtsFieldDef['rst_FilteredJsonTermIDTree'] && $rtsFieldDef['rst_FilteredJsonTermIDTree'] != '') { /*****DEBUG****/ //error_log("import rty $importRtyID 6 dtyID = ".$importDty['dty_ID']."->".$rtsFieldDef["rst_DetailTypeID"]." (".$rtsFieldDef['rst_FilteredJsonTermIDTree'].")"); // term tree exist so need to translate to new ids $rtsFieldDef['rst_FilteredJsonTermIDTree'] = translateTermIDs($rtsFieldDef['rst_FilteredJsonTermIDTree'], "filtered term tree", " field '" . $rtsFieldDef['rst_DisplayName'] . "' detailTypeID = {$dtyID} in rectype '" . $importRty["rty_Name"] . "'"); } if (!$error && @$rtsFieldDef['rst_TermIDTreeNonSelectableIDs'] && $rtsFieldDef['rst_TermIDTreeNonSelectableIDs'] != '') { /*****DEBUG****/ //error_log("import rty $importRtyID 7 dtyID = ".$importDty['dty_ID']."->".$rtsFieldDef["rst_DetailTypeID"]); // term non selectable list exist so need to translate to new ids /*****DEBUG****/ //error_log("non selectable = ". print_r($rtsFieldDef['rst_TermIDTreeNonSelectableIDs'],true)); $rtsFieldDef['rst_TermIDTreeNonSelectableIDs'] = translateTermIDs($rtsFieldDef['rst_TermIDTreeNonSelectableIDs'], "non-selectable", " field '" . $rtsFieldDef['rst_DisplayName'] . "' detailTypeID = {$dtyID} in rectype '" . $importRty["rty_Name"] . "'"); } if (!$error && @$rtsFieldDef['rst_PtrFilteredIDs'] && $rtsFieldDef['rst_PtrFilteredIDs'] != '') { /*****DEBUG****/ //error_log("import rty $importRtyID 8 dtyID = ".$importDty['dty_ID']."->".$rtsFieldDef["rst_PtrFilteredIDs"]); // Target Rectype list exist so need to translate to new ids $rtsFieldDef['rst_PtrFilteredIDs'] = translateRtyIDs($rtsFieldDef['rst_PtrFilteredIDs'], 'filtered pointers', $importDty["dty_ID"]); } /*****DEBUG****/ //error_log("import rty $importRtyID 9 dtyID = ".$importDty['dty_ID']."->".$rtsFieldDef["rst_DetailTypeID"]); if (!$error) { /*****DEBUG****/ //error_log("import rty $importRtyID 10 dtyID = ".$importDty['dty_ID']."->".$rtsFieldDef["rst_DetailTypeID"]); // Adjust values of the field structure for the imported recordtype $importRstID = $rtsFieldDef["rst_ID"]; unset($rtsFieldDef["rst_ID"]); $rtsFieldDef["rst_RecTypeID"] = $importedRecTypeID; $rtsFieldDef["rst_DisplayName"] = mysql_escape_string($rtsFieldDef["rst_DisplayName"]); $rtsFieldDef["rst_DisplayHelpText"] = mysql_escape_string($rtsFieldDef["rst_DisplayHelpText"]); $rtsFieldDef["rst_DisplayExtendedDescription"] = mysql_escape_string($rtsFieldDef["rst_DisplayExtendedDescription"]); $rtsFieldDef["rst_DefaultValue"] = mysql_escape_string($rtsFieldDef["rst_DefaultValue"]); $rtsFieldDef["rst_DisplayHelpText"] = mysql_escape_string($rtsFieldDef["rst_DisplayHelpText"]); // Import the field structure for the imported recordtype mysql_query("INSERT INTO " . $targetDBName . ".defRecStructure (" . implode(", ", array_keys($rtsFieldDef)) . ") VALUES ('" . implode("', '", array_values($rtsFieldDef)) . "')"); // Write the insert action to $logEntry, and set $error to true if one occurred if (mysql_error()) { $error = true; makeLogEntry("<b>Error</b> Importing Field", $importRstID, " '" . $rtsFieldDef["rst_DisplayName"] . "' for record type '" . $importRty["rty_Name"] . "' - " . mysql_error()); break; } else { makeLogEntry("Importing Field", mysql_insert_id(), " '" . $rtsFieldDef["rst_DisplayName"] . "' for record type '" . $importRty["rty_Name"] . "'"); } } if ($error) { break; } } if (!$error) { return $importedRecTypeID; } } return null; } }