mirror of
https://github.com/Combodo/iTop.git
synced 2026-05-19 15:22:17 +02:00
Add operation on replica
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
*/
|
||||
|
||||
use Combodo\iTop\Application\WebPage\iTopWebPage;
|
||||
use Combodo\iTop\Core\CMDBChange\CMDBChangeOrigin;
|
||||
|
||||
require_once('../approot.inc.php');
|
||||
require_once(APPROOT.'/application/application.inc.php');
|
||||
@@ -34,6 +35,90 @@ $oP = new iTopWebPage("iTop - Synchro Replicas");
|
||||
|
||||
// Main program
|
||||
$sOperation = utils::ReadParam('operation', 'details');
|
||||
|
||||
/**
|
||||
* @param \DBObject|null $oReplica
|
||||
* @param $this
|
||||
*
|
||||
* @return \SynchroLog
|
||||
* @throws \ArchivedObjectException
|
||||
* @throws \CoreCannotSaveObjectException
|
||||
* @throws \CoreException
|
||||
* @throws \CoreUnexpectedValue
|
||||
* @throws \CoreWarning
|
||||
* @throws \MySQLException
|
||||
* @throws \OQLException
|
||||
* @throws \SynchroExceptionNotStarted
|
||||
*/
|
||||
function Synchro($oReplica): SynchroLog
|
||||
{
|
||||
$oDataSource = MetaModel::GetObject('SynchroDataSource', $oReplica->Get('sync_source_id'));
|
||||
|
||||
$oStatLog = new SynchroLog();
|
||||
$oStatLog->Set('sync_source_id', $oDataSource->GetKey());
|
||||
$oStatLog->Set('start_date', time());
|
||||
$oStatLog->Set('status', 'running');
|
||||
$oStatLog->AddTrace('Manual synchro');
|
||||
|
||||
// Get the list of SQL columns
|
||||
$aAttCodesExpected = array();
|
||||
$aAttCodesToReconcile = array();
|
||||
$aAttCodesToUpdate = array();
|
||||
$sSelectAtt = 'SELECT SynchroAttribute WHERE sync_source_id = :source_id AND (update = 1 OR reconcile = 1)';
|
||||
$oSetAtt = new DBObjectSet(DBObjectSearch::FromOQL($sSelectAtt), array() /* order by*/, array('source_id' => $oDataSource->GetKey()) /* aArgs */);
|
||||
while ($oSyncAtt = $oSetAtt->Fetch()) {
|
||||
if ($oSyncAtt->Get('update')) {
|
||||
$aAttCodesToUpdate[$oSyncAtt->Get('attcode')] = $oSyncAtt;
|
||||
}
|
||||
if ($oSyncAtt->Get('reconcile')) {
|
||||
$aAttCodesToReconcile[$oSyncAtt->Get('attcode')] = $oSyncAtt;
|
||||
}
|
||||
$aAttCodesExpected[$oSyncAtt->Get('attcode')] = $oSyncAtt;
|
||||
}
|
||||
|
||||
// Get the list of attributes, determine reconciliation keys and update targets
|
||||
//
|
||||
if ($oDataSource->Get('reconciliation_policy') == 'use_attributes') {
|
||||
$aReconciliationKeys = $aAttCodesToReconcile;
|
||||
} elseif ($oDataSource->Get('reconciliation_policy') == 'use_primary_key') {
|
||||
// Override the settings made at the attribute level !
|
||||
$aReconciliationKeys = array('primary_key' => null);
|
||||
}
|
||||
|
||||
if (count($aAttCodesToUpdate) == 0) {
|
||||
$oStatLog->AddTrace('No attribute to update');
|
||||
throw new SynchroExceptionNotStarted('There is no attribute to update');
|
||||
}
|
||||
if (count($aReconciliationKeys) == 0) {
|
||||
$oStatLog->AddTrace('No attribute for reconciliation');
|
||||
throw new SynchroExceptionNotStarted('No attribute for reconciliation');
|
||||
}
|
||||
|
||||
|
||||
$aAttributesToUpdate = array();
|
||||
foreach ($aAttCodesToUpdate as $sAttCode => $oSyncAtt) {
|
||||
$oAttDef = MetaModel::GetAttributeDef($oDataSource->GetTargetClass(), $sAttCode);
|
||||
if ($oAttDef->IsWritable()) {
|
||||
$aAttributesToUpdate[$sAttCode] = $oSyncAtt;
|
||||
}
|
||||
}
|
||||
// Create a change used for logging all the modifications/creations happening during the synchro
|
||||
$oChange = MetaModel::NewObject('CMDBChange');
|
||||
$oChange->Set('date', time());
|
||||
$sUserString = CMDBChange::GetCurrentUserName();
|
||||
$oChange->Set('userinfo', $sUserString.' '.Dict::S('Core:SyncDataExchangeComment'));
|
||||
$oChange->Set('origin', CMDBChangeOrigin::SYNCHRO_DATA_SOURCE);
|
||||
$oChange->DBInsert();
|
||||
CMDBObject::SetCurrentChange($oChange);
|
||||
|
||||
$oReplica->InitExtendedData($oDataSource);
|
||||
|
||||
$oReplica->Synchro($oDataSource, $aReconciliationKeys, $aAttributesToUpdate, $oChange, $oStatLog);
|
||||
$oReplica->DBUpdate();
|
||||
|
||||
return $oStatLog;
|
||||
}
|
||||
|
||||
try {
|
||||
switch ($sOperation) {
|
||||
case 'details':
|
||||
@@ -69,6 +154,44 @@ try {
|
||||
$sDelete = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?'.$_SERVER['QUERY_STRING'];
|
||||
header("Location: $sDelete");
|
||||
break;
|
||||
|
||||
case 'unlinksynchro':
|
||||
$iId = utils::ReadParam('id', null);
|
||||
if ($iId == null) {
|
||||
throw new ApplicationException(Dict::Format('UI:Error:1ParametersMissing', 'id'));
|
||||
}
|
||||
$oReplica = MetaModel::GetObject('SynchroReplica', $iId);
|
||||
$oReplica->Set('dest_id', '');
|
||||
$oReplica->Set('status', 'new');
|
||||
$oReplica->DBWrite();
|
||||
|
||||
$oStatLog = Synchro($oReplica);
|
||||
$oP->add(implode('<br>', $oStatLog->GetTraces()));
|
||||
|
||||
$oReplica->DisplayDetails($oP);
|
||||
break;
|
||||
|
||||
case 'unlink':
|
||||
$iId = utils::ReadParam('id', null);
|
||||
if ($iId == null) {
|
||||
throw new ApplicationException(Dict::Format('UI:Error:1ParametersMissing', 'id'));
|
||||
}
|
||||
$oReplica = MetaModel::GetObject('SynchroReplica', $iId);
|
||||
$oReplica->Set('dest_id', '');
|
||||
$oReplica->Set('status', 'new');
|
||||
$oReplica->DBWrite();
|
||||
|
||||
$oReplica->DisplayDetails($oP);
|
||||
break;
|
||||
|
||||
case 'synchro':
|
||||
$iId = utils::ReadParam('id', null);
|
||||
if ($iId == null) {
|
||||
throw new ApplicationException(Dict::Format('UI:Error:1ParametersMissing', 'id'));
|
||||
}
|
||||
$oReplica = MetaModel::GetObject('SynchroReplica', $iId);
|
||||
$oStatLog = Synchro($oReplica);
|
||||
break;
|
||||
}
|
||||
} catch (CoreException $e) {
|
||||
$oP->p('<b>An error occured while running the query:</b>');
|
||||
|
||||
Reference in New Issue
Block a user