From ba822a655e178d76bd7705cd0ad97d2cc6e2e484 Mon Sep 17 00:00:00 2001 From: Romain Quetiez Date: Tue, 1 Feb 2011 13:40:51 +0000 Subject: [PATCH] Wiki: add links to objects in text attributes SVN:trunk[1060] --- application/cmdbabstract.class.inc.php | 3 +- core/attributedef.class.inc.php | 74 +++++++++++++++++++++++++- core/metamodel.class.php | 12 +++++ 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index b46c30bef..f9ed61f5a 100644 --- a/application/cmdbabstract.class.inc.php +++ b/application/cmdbabstract.class.inc.php @@ -1080,7 +1080,8 @@ EOF $aEventsList[] ='validate'; $aEventsList[] ='keyup'; $aEventsList[] ='change'; - $sHTMLValue = "
{$sValidationField}
"; + $sEditValue = $oAttDef->GetEditValue($value); + $sHTMLValue = "
{$sValidationField}
"; break; case 'HTML': diff --git a/core/attributedef.class.inc.php b/core/attributedef.class.inc.php index 969f53ee2..ea61e394e 100644 --- a/core/attributedef.class.inc.php +++ b/core/attributedef.class.inc.php @@ -1046,6 +1046,7 @@ class AttributeEncryptedString extends AttributeString * * @package iTopORM */ +define('WIKI_OBJECT_REGEXP', '/\[\[(.+):(.+)\]\]/U'); class AttributeText extends AttributeString { public function GetEditClass() {return "Text";} @@ -1060,7 +1061,78 @@ class AttributeText extends AttributeString public function GetAsHTML($sValue) { - return str_replace("\n", "
\n", parent::GetAsHTML($sValue)); + $sValue = parent::GetAsHTML($sValue); + + if (preg_match_all(WIKI_OBJECT_REGEXP, $sValue, $aAllMatches, PREG_SET_ORDER)) + { + foreach($aAllMatches as $iPos => $aMatches) + { + $sClass = $aMatches[1]; + $sName = $aMatches[2]; + + if (MetaModel::IsValidClass($sClass)) + { + $oObj = MetaModel::GetObjectByName($sClass, $sName, false /* MustBeFound */); + if (is_object($oObj)) + { + // Propose a std link to the object + $sValue = str_replace($aMatches[0], $oObj->GetHyperlink(), $sValue); + } + else + { + // Propose a std link to the object + $sClassLabel = MetaModel::GetName($sClass); + $sValue = str_replace($aMatches[0], "$sClassLabel:$sName", $sValue); + // Later: propose a link to create a new object + // Anyhow... there is no easy way to suggest default values based on the given FRIENDLY name + //$sValue = preg_replace('/\[\[(.+):(.+)\]\]/', ''.$sName.'', $sValue); + } + } + } + } + return str_replace("\n", "
\n", $sValue); + } + + public function GetEditValue($sValue) + { + if (preg_match_all(WIKI_OBJECT_REGEXP, $sValue, $aAllMatches, PREG_SET_ORDER)) + { + foreach($aAllMatches as $iPos => $aMatches) + { + $sClass = $aMatches[1]; + $sName = $aMatches[2]; + + if (MetaModel::IsValidClass($sClass)) + { + $sClassLabel = MetaModel::GetName($sClass); + $sValue = str_replace($aMatches[0], "[[$sClassLabel:$sName]]", $sValue); + } + } + } + return $sValue; + } + + public function MakeRealValue($proposedValue) + { + $sValue = $proposedValue; + if (preg_match_all(WIKI_OBJECT_REGEXP, $sValue, $aAllMatches, PREG_SET_ORDER)) + { + foreach($aAllMatches as $iPos => $aMatches) + { + $sClassLabel = $aMatches[1]; + $sName = $aMatches[2]; + + if (!MetaModel::IsValidClass($sClassLabel)) + { + $sClass = MetaModel::GetClassFromLabel($sClassLabel); + if ($sClass) + { + $sValue = str_replace($aMatches[0], "[[$sClass:$sName]]", $sValue); + } + } + } + } + return $sValue; } public function GetAsXML($value) diff --git a/core/metamodel.class.php b/core/metamodel.class.php index 1f19864b6..643bda5ac 100644 --- a/core/metamodel.class.php +++ b/core/metamodel.class.php @@ -288,6 +288,18 @@ abstract class MetaModel return self::GetName($sClass); } } + final static public function GetClassFromLabel($sClassLabel) + { + foreach(self::GetClasses() as $sClass) + { + if (self::GetName($sClass) == $sClassLabel) + { + return $sClass; + } + } + return null; + } + final static public function GetCategory($sClass) { self::_check_subclass($sClass);