diff --git a/application/ui.linksdirectwidget.class.inc.php b/application/ui.linksdirectwidget.class.inc.php
index 499f6695f..d41916813 100644
--- a/application/ui.linksdirectwidget.class.inc.php
+++ b/application/ui.linksdirectwidget.class.inc.php
@@ -278,14 +278,19 @@ class UILinksWidgetDirect
/**
* @param WebPage $oPage
* @param DBObject $oCurrentObj
- * @throws Exception
+ * @param $aAlreadyLinked
+ *
+ * @throws \CoreException
+ * @throws \DictExceptionMissingString
+ * @throws \Exception
+ * @throws \OQLException
*/
- public function GetObjectsSelectionDlg($oPage, $oCurrentObj)
+ public function GetObjectsSelectionDlg($oPage, $oCurrentObj, $aAlreadyLinked)
{
$sHtml = "
\n";
-
- $oLinksetDef = MetaModel::GetAttributeDef($this->sClass, $this->sAttCode);
- $valuesDef = $oLinksetDef->GetValuesDef();
+
+ $oLinkSetDef = MetaModel::GetAttributeDef($this->sClass, $this->sAttCode);
+ $valuesDef = $oLinkSetDef->GetValuesDef();
if ($valuesDef === null)
{
$oFilter = new DBObjectSearch($this->sLinkedClass);
@@ -302,12 +307,35 @@ class UILinksWidgetDirect
{
$this->SetSearchDefaultFromContext($oCurrentObj, $oFilter);
}
+ if (($oCurrentObj != null) && MetaModel::IsSameFamilyBranch($this->sLinkedClass, $this->sClass))
+ {
+ // Prevent linking to self if the linked object is of the same family
+ // and already present in the database
+ if (!$oCurrentObj->IsNew())
+ {
+ $oFilter->AddCondition('id', $oCurrentObj->GetKey(), '!=');
+ }
+ }
+ if (count($aAlreadyLinked) > 0)
+ {
+ $oFilter->AddCondition('id', $aAlreadyLinked, 'NOTIN');
+ }
+ if ($oCurrentObj != null)
+ {
+ $aArgs = array_merge($oCurrentObj->ToArgs('this'), $oFilter->GetInternalParams());
+ $oFilter->SetInternalParams($aArgs);
+ }
$bOpen = MetaModel::GetConfig()->Get('legacy_search_drawer_open');
$oBlock = new DisplayBlock($oFilter, 'search', false);
$sHtml .= $oBlock->GetDisplay($oPage, "SearchFormToAdd_{$this->sInputid}",
array(
'open' => $bOpen,
- 'table_id' => "SearchResultsToAdd_{$this->sInputid}")
+ 'table_id' => "SearchResultsToAdd_{$this->sInputid}",
+ 'table_id2' => "add_{$this->sInputid}",
+ 'table_inner_id' => "ResultsToAdd_{$this->sInputid}",
+ 'cssCount' => "#count_{$this->sInputid}",
+ 'query_params' => $oFilter->GetInternalParams(),
+ )
);
$sHtml .= "