Parcourir la source

Cosmetics on the bulk load (followed new test plan)

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@159 a333f486-631f-4898-b8df-5754b55c2be0
romainq il y a 15 ans
Parent
commit
b523c16f80
3 fichiers modifiés avec 69 ajouts et 37 suppressions
  1. 2 2
      core/bulkchange.class.inc.php
  2. 14 6
      core/metamodel.class.php
  3. 53 29
      pages/csvimport.php

+ 2 - 2
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

+ 14 - 6
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();
 	}
 

+ 53 - 29
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("<form method=\"post\" action=\"\">");
-    $oPage->MakeClassesSelect("class", $sClass, 50);
-    //$oPage->Add("<input type=\"text\" size=\"40\" name=\"initialsituation\" value=\"\">");
-    $oPage->add("</br>");
-    $oPage->add("<textarea rows=\"25\" cols=\"80\" name=\"csvdata\" wrap=\"soft\">$sCSVData</textarea>");
-    $oPage->add("</br>");
-    $oPage->add("<input type=\"hidden\" name=\"fromwiztep\" value=\"$sWiztep\">");
-    $oPage->add("<input type=\"submit\" name=\"todo\" value=\"Next\"><br/>\n");
-    $oPage->add("</form>");
-
-    // 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.
+	$oPage->add("<form method=\"post\" action=\"\">");
+	$oPage->MakeClassesSelect("class", $sClass, 50);
+	$oPage->add("<br/>");
+	$oPage->add("<textarea rows=\"25\" cols=\"100\" name=\"csvdata\" wrap=\"soft\">$sCSVData</textarea>");
+	$oPage->add("<br/>");
+	$oPage->add("<input type=\"hidden\" name=\"fromwiztep\" value=\"$sWiztep\">");
+	$oPage->add("<input type=\"submit\" name=\"todo\" value=\"Next\"><br/>\n");
+	$oPage->add("</form>");
+
+	// 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)
 	</script>\n");
 	
 	$oPage->add_ready_script("$('#select_class').change( function() {DisplayFields(this.value);} );");
-	
-    $oPage->add("Fields for this object: <textarea readonly id=fields rows=\"3\" cols=\"60\" wrap=\"soft\">$sCurrentList</textarea>");
+	$oPage->add("<br/>");
+	$oPage->add("Fields for this object<br/><textarea readonly id=fields rows=\"3\" cols=\"100\" wrap=\"soft\">$sCurrentList</textarea>");
 
 }
 
@@ -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("<button onClick=\"window.history.back();\">Back</button>\n");
+		$oPage->add("<button disabled>Next</button>\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("<h2>Check...</h2>");
 	}
@@ -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("<h1>Bulk load from CSV data / step 3</h1>");
 	$sWiztep = "3_verify";
 
-	DoProcessOrVerify($oPage, $sClass, null);
-	// FORM started by DoProcessOrVerify...
-
-	$oPage->add("<input type=\"hidden\" name=\"fromwiztep\" value=\"$sWiztep\">");
-	$oPage->add("<input type=\"submit\" name=\"todo\" value=\"Back\">");
-	$oPage->add("<input type=\"submit\" name=\"todo\" value=\"Next\">");
-	$oPage->add("</form>");
+	if (DoProcessOrVerify($oPage, $sClass, null))
+	{
+		// FORM started by DoProcessOrVerify...
+		$oPage->add("<input type=\"hidden\" name=\"fromwiztep\" value=\"$sWiztep\">");
+		$oPage->add("<input type=\"submit\" name=\"todo\" value=\"Back\">");
+		$oPage->add("<input type=\"submit\" name=\"todo\" value=\"Next\">");
+		$oPage->add("</form>");
+	}
 }
 
 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("<input type=\"hidden\" name=\"fromwiztep\" value=\"$sWiztep\">");
-    $oPage->add("<input type=\"submit\" name=\"todo\" value=\"Back\">");
-	$oPage->add("</form>");
+	if (DoProcessOrVerify($oPage, $sClass, $oMyChange))
+	{
+		// FORM started by DoProcessOrVerify...
+		$oPage->add("<input type=\"hidden\" name=\"fromwiztep\" value=\"$sWiztep\">");
+		$oPage->add("<input type=\"submit\" name=\"todo\" value=\"Back\">");
+		$oPage->add("</form>");
+	}
 }