Wiki: add links to objects in text attributes

SVN:trunk[1060]
This commit is contained in:
Romain Quetiez
2011-02-01 13:40:51 +00:00
parent 20ec91e9f3
commit ba822a655e
3 changed files with 87 additions and 2 deletions

View File

@@ -1080,7 +1080,8 @@ EOF
$aEventsList[] ='validate';
$aEventsList[] ='keyup';
$aEventsList[] ='change';
$sHTMLValue = "<table><tr><td><textarea class=\"resizable\" title=\"$sHelpText\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" rows=\"8\" cols=\"40\" id=\"$iId\">$value</textarea></td><td>{$sValidationField}</td></tr></table>";
$sEditValue = $oAttDef->GetEditValue($value);
$sHTMLValue = "<table><tr><td><textarea class=\"resizable\" title=\"$sHelpText\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" rows=\"8\" cols=\"40\" id=\"$iId\">$sEditValue</textarea></td><td>{$sValidationField}</td></tr></table>";
break;
case 'HTML':

View File

@@ -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", "<br>\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], "<span class=\"wiki_broken_link\">$sClassLabel:$sName</span>", $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('/\[\[(.+):(.+)\]\]/', '<a href="./UI.php?operation=new&class='.$sClass.'&default[att1]=xxx&default[att2]=yyy">'.$sName.'</a>', $sValue);
}
}
}
}
return str_replace("\n", "<br>\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)

View File

@@ -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);