diff --git a/core/bulkchange.class.inc.php b/core/bulkchange.class.inc.php index 6b4332d52..fae8db1eb 100644 --- a/core/bulkchange.class.inc.php +++ b/core/bulkchange.class.inc.php @@ -172,7 +172,7 @@ class RowStatus_NewObj extends RowStatus } else { - if (empty($this->m_sClass)) + if (!empty($this->m_sClass)) { $oObj = MetaModel::GetObject($this->m_sClass, $this->m_iObjKey); return 'Created '.$oObj->GetHyperLink(); @@ -281,7 +281,7 @@ class BulkChange { case 0: $aErrors[$sAttCode] = "Object not found"; - $aResults[$sAttCode]= new CellChangeSpec_Issue(null, $oTargetObj->Get($sAttCode), 'Object not found'); + $aResults[$sAttCode]= new CellChangeSpec_Issue(null, $oTargetObj->Get($sAttCode), 'Object not found - check the spelling (no space before/after)'); break; case 1: // Do change the external key attribute diff --git a/core/metamodel.class.php b/core/metamodel.class.php index 39d116f49..f93f8a897 100644 --- a/core/metamodel.class.php +++ b/core/metamodel.class.php @@ -2564,7 +2564,7 @@ abstract class MetaModel return $iTotalHits.' ('.implode(', ', $aRes).')'; } - public static function MakeSingleRow($sClass, $iKey) + public static function MakeSingleRow($sClass, $iKey, $bMustBeFound = true) { if (!array_key_exists($sClass, self::$aQueryCacheGetObject)) { @@ -2591,7 +2591,7 @@ abstract class MetaModel $aRow = CMDBSource::FetchArray($res); CMDBSource::FreeResult($res); - if (empty($aRow)) + if ($bMustBeFound && empty($aRow)) { throw new CoreException("No result for the single row query: '$sSQL'"); } @@ -2625,10 +2625,10 @@ abstract class MetaModel return new $sClass($aRow); } - public static function GetObject($sClass, $iKey) + public static function GetObject($sClass, $iKey, $bMustBeFound = true) { self::_check_subclass($sClass); - $aRow = self::MakeSingleRow($sClass, $iKey); + $aRow = self::MakeSingleRow($sClass, $iKey, $bMustBeFound); if (empty($aRow)) { return null; @@ -2636,9 +2636,17 @@ abstract class MetaModel return self::GetObjectByRow($sClass, $aRow); } - public static function GetHyperLink($sTargetClass, $sOldValue) + public static function GetHyperLink($sTargetClass, $iKey) { - $oObj = self::GetObject($sTargetClass, $sOldValue); + if ($iKey < 0) + { + return "$sTargetClass: $iKey (invalid value)"; + } + $oObj = self::GetObject($sTargetClass, $iKey, false); + if (is_null($oObj)) + { + return "$sTargetClass: $iKey (not found)"; + } return $oObj->GetHyperLink(); } diff --git a/pages/csvimport.php b/pages/csvimport.php index 24bf03798..256aa2957 100644 --- a/pages/csvimport.php +++ b/pages/csvimport.php @@ -75,6 +75,8 @@ function ShowTableForm($oPage, $oCSVParser, $sClass) $aFields = array(); foreach($oCSVParser->ListFields() as $iFieldIndex=>$sFieldName) { + $sFieldName = trim($sFieldName); + $aOptions = array(); $aOptions['id'] = array( 'LabelHtml' => "Private key", @@ -336,18 +338,17 @@ function Do_Welcome($oPage, $sClass) $sCSVData = utils::ReadPostedParam('csvdata'); - $oPage->add("
"); - $oPage->MakeClassesSelect("class", $sClass, 50); - //$oPage->Add(""); - $oPage->add("
"); - $oPage->add(""); - $oPage->add("
"); - $oPage->add(""); - $oPage->add("
\n"); - $oPage->add("
"); + $oPage->add("
"); + $oPage->MakeClassesSelect("class", $sClass, 50); + $oPage->add("
"); + $oPage->add(""); + $oPage->add("
"); + $oPage->add(""); + $oPage->add("
\n"); + $oPage->add("
"); - // As a help to the end-user, let's display the list of possible fields - // for a class, that can be copied/pasted into the CSV area. + // As a help to the end-user, let's display the list of possible fields + // for a class, that can be copied/pasted into the CSV area. $sCurrentList = ""; $aHeadersList = array(); foreach(MetaModel::GetClasses('bizmodel') as $sClassName) @@ -385,8 +386,8 @@ function Do_Welcome($oPage, $sClass) \n"); $oPage->add_ready_script("$('#select_class').change( function() {DisplayFields(this.value);} );"); - - $oPage->add("Fields for this object: "); + $oPage->add("
"); + $oPage->add("Fields for this object
"); } @@ -437,6 +438,15 @@ function DoProcessOrVerify($oPage, $sClass, CMDBChange $oChange = null) $aFieldMap = utils::ReadPostedParam('fmap'); $aIsReconcKey = utils::ReadPostedParam('iskey'); + if (empty($aIsReconcKey)) + { + $oPage->p("Error: no reconciliation key has been specified. Please specify which field(s) will be used to identify the object"); + + $oPage->add("\n"); + $oPage->add("\n"); + return; + } + $oCSVParser = new CSVParser($sCSVData); $oCSVParser->SetSeparator($sSep); $oCSVParser->SetSkipLines($iSkip); @@ -521,6 +531,7 @@ function DoProcessOrVerify($oPage, $sClass, CMDBChange $oChange = null) { $oPage->p("ok - required external keys (if any) have been found in the field list"); } + $oPage->p("Note: the procedure will fail if any line has not the same number of columns as the first line"); $oPage->p("

Check...

"); } @@ -534,7 +545,7 @@ function DoProcessOrVerify($oPage, $sClass, CMDBChange $oChange = null) $oPage->add_input_hidden("fmap", $aFieldMap); $oPage->add_input_hidden("iskey", $aIsReconcKey); - return; + return true; } function Do_Verify($oPage, $sClass) @@ -542,13 +553,14 @@ function Do_Verify($oPage, $sClass) $oPage->p("

Bulk load from CSV data / step 3

"); $sWiztep = "3_verify"; - DoProcessOrVerify($oPage, $sClass, null); - // FORM started by DoProcessOrVerify... - - $oPage->add(""); - $oPage->add(""); - $oPage->add(""); - $oPage->add(""); + if (DoProcessOrVerify($oPage, $sClass, null)) + { + // FORM started by DoProcessOrVerify... + $oPage->add(""); + $oPage->add(""); + $oPage->add(""); + $oPage->add(""); + } } function Do_Execute($oPage, $sClass) @@ -558,15 +570,27 @@ function Do_Execute($oPage, $sClass) $oMyChange = MetaModel::NewObject("CMDBChange"); $oMyChange->Set("date", time()); - $oMyChange->Set("userinfo", "CSV Import"); + $iUser = UserRights::GetContactId(); + if ($iUser != null) + { + // Ok, that's dirty, I admit :-) + $oUser = MetaModel::GetObject('bizContact', $iUser); + $sUser = $oUser->GetName(); + $oMyChange->Set("userinfo", "CSV Import, by ".$sUser); + } + else + { + $oMyChange->Set("userinfo", "CSV Import"); + } $iChangeId = $oMyChange->DBInsert(); - DoProcessOrVerify($oPage, $sClass, $oMyChange); - - // FORM started by DoProcessOrVerify... - $oPage->add(""); - $oPage->add(""); - $oPage->add(""); + if (DoProcessOrVerify($oPage, $sClass, $oMyChange)) + { + // FORM started by DoProcessOrVerify... + $oPage->add(""); + $oPage->add(""); + $oPage->add(""); + } }