From 90069f6826803b3accc26247f3542d7602ae02d3 Mon Sep 17 00:00:00 2001 From: Denis Flaven Date: Tue, 29 Jun 2010 09:22:22 +0000 Subject: [PATCH] New data model integration... Enhanced the display of blob attributes SVN:trunk[502] --- application/cmdbabstract.class.inc.php | 62 ++++++++++ business/itop.business.class.inc.php | 13 +++ core/cmdbchangeop.class.inc.php | 5 +- core/ormdocument.class.inc.php | 39 +------ dictionaries/dictionary.itop.model.php | 1 + dictionaries/dictionary.itop.ui.php | 4 + pages/UI.php | 154 +++++++++---------------- 7 files changed, 140 insertions(+), 138 deletions(-) diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index 75f1fb227..74b63e252 100644 --- a/application/cmdbabstract.class.inc.php +++ b/application/cmdbabstract.class.inc.php @@ -930,6 +930,7 @@ abstract class cmdbAbstractObject extends CMDBObject case 'List': // Not editable for now... + $sHTMLValue = ''; break; case 'String': @@ -963,7 +964,15 @@ abstract class cmdbAbstractObject extends CMDBObject $sHTMLValue .= "\n"; foreach($aAllowedValues as $key => $display_value) { + if (count($aAllowedValues) == 1) + { + // When there is only once choice, select it by default + $sSelected = ' selected'; + } + else + { $sSelected = ($value == $key) ? ' selected' : ''; + } $sHTMLValue .= "\n"; } $sHTMLValue .= " {$sValidationField}\n"; @@ -1380,6 +1389,13 @@ EOF // Special display for the 'state' attribute itself $sDisplayValue = $this->GetStateLabel(); } + else if ($oAttDef->GetEditClass() == 'Document') + { + $oDocument = $this->Get($sAttCode); + $sDisplayValue = $this->GetAsHTML($sAttCode); + $sDisplayValue .= "
".Dict::Format('UI:OpenDocumentInNewWindow_', $oDocument->GetDisplayLink(get_class($this), $this->GetKey(), $sAttCode)).", \n"; + $sDisplayValue .= "
".Dict::Format('UI:DownloadDocument_', $oDocument->GetDisplayLink(get_class($this), $this->GetKey(), $sAttCode)).", \n"; + } else { $sDisplayValue = $this->GetAsHTML($sAttCode); @@ -1389,5 +1405,51 @@ EOF return $retVal; } + /** + * Displays a blob document *inline* (if possible, depending on the type of the document) + * @return string + */ + public function DisplayDocumentInline(WebPage $oPage, $sAttCode) + { + $oDoc = $this->Get($sAttCode); + $sClass = get_class($this); + $Id = $this->GetKey(); + switch ($oDoc->GetMainMimeType()) + { + case 'text': + case 'html': + $data = $oDoc->GetData(); + switch($oDoc->GetMimeType()) + { + case 'text/html': + case 'text/xml': + $oPage->add("\n"); + + default: + $oPage->add("
".htmlentities(MyHelpers::beautifulstr($data, 1000, true))."
\n"); + } + break; + + case 'application': + switch($oDoc->GetMimeType()) + { + case 'application/pdf': + $oPage->add("\n"); + break; + + default: + $oPage->add(Dict::S('UI:Document:NoPreview')); + } + break; + + case 'image': + $oPage->add("\n"); + break; + + default: + $oPage->add(Dict::S('UI:Document:NoPreview')); + } + } + } ?> diff --git a/business/itop.business.class.inc.php b/business/itop.business.class.inc.php index 3059da87f..43405d19a 100644 --- a/business/itop.business.class.inc.php +++ b/business/itop.business.class.inc.php @@ -304,6 +304,19 @@ class FileDoc extends Document MetaModel::Init_SetZListItems('standard_search', array('name', 'description', 'type', 'status')); MetaModel::Init_SetZListItems('list', array('type', 'status', 'contents')); } + + /** + * Overload the display of the properties to add a tab (the first one) + * with the preview of the document + */ + public function DisplayBareProperties(WebPage $oPage) + { + $oPage->SetCurrentTab(Dict::S('Class:Document:PreviewTab')); + $oPage->add($this->DisplayDocumentInline($oPage, 'contents')); + $oPage->SetCurrentTab(Dict::S('UI:PropertiesTab')); + parent::DisplayBareProperties($oPage); + + } } class Licence extends cmdbAbstractObject { diff --git a/core/cmdbchangeop.class.inc.php b/core/cmdbchangeop.class.inc.php index 3e6f17be1..3a0d53216 100644 --- a/core/cmdbchangeop.class.inc.php +++ b/core/cmdbchangeop.class.inc.php @@ -310,9 +310,8 @@ class CMDBChangeOpSetAttributeBlob extends CMDBChangeOpSetAttribute $sAttName = $oAttDef->GetLabel(); $oPrevDoc = $this->Get('prevdata'); $sDocView = $oPrevDoc->GetAsHtml(); - $sDocView .= "
Open in New Window: ".$oPrevDoc->GetDisplayLink(get_class($this), $this->GetKey(), 'prevdata').", \n"; - $sDocView .= "Download: ".$oPrevDoc->GetDownloadLink(get_class($this), $this->GetKey(), 'prevdata')."\n"; - + $sDocView .= "
".Dict::Format('UI:OpenDocumentInNewWindow_',$oPrevDoc->GetDisplayLink(get_class($this), $this->GetKey(), 'prevdata')).", \n"; + $sDocView .= Dict::Format('UI:DownloadDocument_', $oPrevDoc->GetDownloadLink(get_class($this), $this->GetKey(), 'prevdata'))."\n"; //$sDocView = $oPrevDoc->GetDisplayInline(get_class($this), $this->GetKey(), 'prevdata'); $sResult = "$sAttName changed, previous value: $sDocView"; } diff --git a/core/ormdocument.class.inc.php b/core/ormdocument.class.inc.php index e5c9ef204..ad23e6c05 100644 --- a/core/ormdocument.class.inc.php +++ b/core/ormdocument.class.inc.php @@ -94,46 +94,11 @@ class ormDocument else { $data = $this->GetData(); - $sResult = $this->GetFileName().' [ '.$this->GetMimeType().', size: '.strlen($data).' byte(s) ]'; + $sResult = $this->GetFileName().' [ '.$this->GetMimeType().', size: '.strlen($data).' byte(s) ]
'; } return $sResult; } - - /** - * Returns an HTML fragment that will display the document *inline* (if possible) - * @return string - */ - public function GetDisplayInline($sClass, $Id, $sAttCode) - { - switch ($this->GetMainMimeType()) - { - case 'text': - case 'html': - $data = $this->GetData(); - switch($this->GetMimeType()) - { - case 'text/html': - case 'text/xml': - return "\n"; - - default: - return "
".htmlentities(MyHelpers::beautifulstr($data, 1000, true))."
\n"; - } - break; // Not really needed, but... - - case 'application': - switch($this->GetMimeType()) - { - case 'application/pdf': - return "\n"; - } - break; - - case 'image': - return "\n"; - } - } - + /** * Returns an hyperlink to display the document *inline* * @return string diff --git a/dictionaries/dictionary.itop.model.php b/dictionaries/dictionary.itop.model.php index d7fa0ba7c..ad67bad5a 100644 --- a/dictionaries/dictionary.itop.model.php +++ b/dictionaries/dictionary.itop.model.php @@ -217,6 +217,7 @@ Dict::Add('EN US', 'English', 'English', array( 'Class:Document/Attribute:contract_list+' => 'Contracts refering to this document', 'Class:Document/Attribute:ticket_list' => 'Tickets', 'Class:Document/Attribute:ticket_list+' => 'Tickets refering to this document', + 'Class:Document:PreviewTab' => 'Preview', )); // diff --git a/dictionaries/dictionary.itop.ui.php b/dictionaries/dictionary.itop.ui.php index e14752133..cf01fa18d 100644 --- a/dictionaries/dictionary.itop.ui.php +++ b/dictionaries/dictionary.itop.ui.php @@ -881,6 +881,10 @@ When associated with a trigger, each action is given an "order" number, specifyi 'UI:iTopVersion:Short' => 'iTop version %1$s', 'UI:iTopVersion:Long' => 'iTop version %1$s-%2$s built on %3$s', 'UI:PropertiesTab' => 'Properties', + + 'UI:OpenDocumentInNewWindow_' => 'Open this document in a new window: %1$s', + 'UI:DownloadDocument_' => 'Download this document: %1$s', + 'UI:Document:NoPreview' => 'No preview is available for this type of document', )); diff --git a/pages/UI.php b/pages/UI.php index 5fa4061cd..3d725f540 100644 --- a/pages/UI.php +++ b/pages/UI.php @@ -350,6 +350,57 @@ function DeleteObjects(WebPage $oP, $sClass, $aObjects, $bDeleteConfirmed) } +/** + * Updates an object from the POSTed parameters + */ +function UpdateObject(&$oObj) +{ + foreach(MetaModel::ListAttributeDefs(get_class($oObj)) as $sAttCode=>$oAttDef) + { + if ($oAttDef->IsLinkSet()) + { + // Link set, the data is a set of link objects, encoded in JSON + $aAttributes[$sAttCode] = trim(utils::ReadPostedParam("attr_$sAttCode", '')); + if (!empty($aAttributes[$sAttCode])) + { + $oLinkSet = WizardHelper::ParseJsonSet($oObj, $oAttDef->GetLinkedClass(), $oAttDef->GetExtKeyToMe(), $aAttributes[$sAttCode]); + $oObj->Set($sAttCode, $oLinkSet); + // TO DO: detect a real modification, for now always update !! + } + } + else if ($oAttDef->IsWritable()) + { + $iFlags = $oObj->GetAttributeFlags($sAttCode); + if ($iFlags & (OPT_ATT_HIDDEN | OPT_ATT_READONLY)) + { + // Non-visible, or read-only attribute, do nothing + } + else if ($oAttDef->GetEditClass() == 'Document') + { + // There should be an uploaded file with the named attr_ + $oDocument = utils::ReadPostedDocument('file_'.$sAttCode); + if (!$oDocument->IsEmpty()) + { + // A new file has been uploaded + $oObj->Set($sAttCode, $oDocument); + } + } + else + { + $rawValue = utils::ReadPostedParam("attr_$sAttCode", null); + if (!is_null($rawValue)) + { + $aAttributes[$sAttCode] = trim($rawValue); + $previousValue = $oObj->Get($sAttCode); + if ($previousValue !== $aAttributes[$sAttCode]) + { + $oObj->Set($sAttCode, $aAttributes[$sAttCode]); + } + } + } + } + } +} /*********************************************************************************** * * Main user interface page, starts here @@ -715,51 +766,9 @@ try { $oP->set_title(Dict::Format('UI:ModificationPageTitle_Object_Class', $oObj->GetName(), $sClassLabel)); $oP->add("

".Dict::Format('UI:ModificationTitle_Class_Object', $sClassLabel, $oObj->GetName())."

\n"); - foreach(MetaModel::ListAttributeDefs(get_class($oObj)) as $sAttCode=>$oAttDef) - { - if ($oAttDef->IsLinkSet()) - { - // Link set, the data is a set of link objects, encoded in JSON - $aAttributes[$sAttCode] = trim(utils::ReadPostedParam("attr_$sAttCode", '')); - if (!empty($aAttributes[$sAttCode])) - { - $oLinkSet = WizardHelper::ParseJsonSet($oObj, $oAttDef->GetLinkedClass(), $oAttDef->GetExtKeyToMe(), $aAttributes[$sAttCode]); - $oObj->Set($sAttCode, $oLinkSet); - // TO DO: detect a real modification, for now always update !! - } - } - else if (!$oAttDef->IsExternalField()) - { - $rawValue = utils::ReadPostedParam("attr_$sAttCode", null); - if (!is_null($rawValue)) - { - $aAttributes[$sAttCode] = trim($rawValue); - $previousValue = $oObj->Get($sAttCode); - if ($previousValue !== $aAttributes[$sAttCode]) - { - $oObj->Set($sAttCode, $aAttributes[$sAttCode]); - } - } - } - else if ($oAttDef->IsWritable()) - { - $iFlags = $oObj->GetAttributeFlags($sAttCode); - if ($iFlags & (OPT_ATT_HIDDEN | OPT_ATT_READONLY)) - { - // Non-visible, or read-only attribute, do nothing - } - else if ($oAttDef->GetEditClass() == 'Document') - { - // There should be an uploaded file with the named attr_ - $oDocument = utils::ReadPostedDocument('file_'.$sAttCode); - if (!$oDocument->IsEmpty()) - { - // A new file has been uploaded - $oObj->Set($sAttCode, $oDocument); - } - } - } - } + + UpdateObject($oObj); + if (!$oObj->IsModified()) { $oP->p(Dict::Format('UI:Class_Object_NotUpdated', MetaModel::GetName(get_class($oObj)), $oObj->GetName())); @@ -884,14 +893,7 @@ try $oMyChange->Set("userinfo", $sUserString); $iChangeId = $oMyChange->DBInsert(); $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($oObj)); - foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef) - { - if ( ($oAttDef->IsWritable()) ) - { - $value = utils::ReadPostedParam('attr_'.$sAttCode, ''); - $oObj->Set($sAttCode, $value); - } - } + UpdateObject($oObj); $oObj->DBCloneTracked($oMyChange); $oP->set_title(Dict::S('UI:PageTitle:ObjectCreated')); $oP->add("

".Dict::Format('UI:Title:Object_Of_Class_Created', $oObj->GetName(), $sClassLabel)."

\n"); @@ -915,51 +917,7 @@ try else { $oObj = MetaModel::NewObject($sClass); - foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef) - { - if ($oAttDef->IsLinkSet()) - { - // Link set, the data is a set of link objects, encoded in JSON - $aAttributes[$sAttCode] = trim(utils::ReadPostedParam("attr_$sAttCode", '')); - if (!empty($aAttributes[$sAttCode])) - { - $oLinkSet = WizardHelper::ParseJsonSet($oObj, $oAttDef->GetLinkedClass(), $oAttDef->GetExtKeyToMe(), $aAttributes[$sAttCode]); - $oObj->Set($sAttCode, $oLinkSet); - // TO DO: detect a real modification, for now always update !! - } - } - else if (!$oAttDef->IsExternalField()) - { - $rawValue = utils::ReadPostedParam("attr_$sAttCode", null); - if (!is_null($rawValue)) - { - $aAttributes[$sAttCode] = trim($rawValue); - $previousValue = $oObj->Get($sAttCode); - if ($previousValue !== $aAttributes[$sAttCode]) - { - $oObj->Set($sAttCode, $aAttributes[$sAttCode]); - } - } - } - else if ($oAttDef->IsWritable()) - { - $iFlags = $oObj->GetAttributeFlags($sAttCode); - if ($iFlags & (OPT_ATT_HIDDEN | OPT_ATT_READONLY)) - { - // Non-visible, or read-only attribute, do nothing - } - else if ($oAttDef->GetEditClass() == 'Document') - { - // There should be an uploaded file with the named attr_ - $oDocument = utils::ReadPostedDocument('file_'.$sAttCode); - if (!$oDocument->IsEmpty()) - { - // A new file has been uploaded - $oObj->Set($sAttCode, $oDocument); - } - } - } - } + UpdateObject($oObj); } if (isset($oObj) && is_object($oObj)) {