From 1dcf8301410d07634ec57814b79fcdbb4afea0de Mon Sep 17 00:00:00 2001 From: Guillaume Lajarige Date: Wed, 22 Mar 2017 15:08:34 +0000 Subject: [PATCH] =?UTF-8?q?N=C2=B0783=20Attachments=20can=20now=20be=20rea?= =?UTF-8?q?donly=20in=20some=20states.=20Use=20the=20new=20module=20parame?= =?UTF-8?q?ter=20"readonly=5Fstates".?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SVN:trunk[4610] --- .../2.x/itop-attachments/main.attachments.php | 67 ++++++++++++++++++- .../src/forms/objectformmanager.class.inc.php | 10 ++- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/datamodels/2.x/itop-attachments/main.attachments.php b/datamodels/2.x/itop-attachments/main.attachments.php index 394febd329..b9e9a336b9 100755 --- a/datamodels/2.x/itop-attachments/main.attachments.php +++ b/datamodels/2.x/itop-attachments/main.attachments.php @@ -20,6 +20,10 @@ define('ATTACHMENT_DOWNLOAD_URL', 'pages/ajax.document.php?operation=download_do class AttachmentPlugIn implements iApplicationUIExtension, iApplicationObjectExtension { + const ENUM_GUI_ALL = 'all'; + const ENUM_GUI_BACKOFFICE = 'backoffice'; + const ENUM_GUI_PORTALS = 'portals'; + protected static $m_bIsModified = false; public function OnDisplayProperties($oObject, WebPage $oPage, $bEditMode = false) @@ -243,7 +247,7 @@ EOF $oPage->add('
'); $oPage->add(''.Dict::S('Attachments:FieldsetTitle').''); - if ($bEditMode) + if ($bEditMode && !static::IsReadonlyState($oObject, $oObject->GetState(), static::ENUM_GUI_BACKOFFICE) ) { $sIsDeleteEnabled = $this->m_bDeleteEnabled ? 'true' : 'false'; $iTransactionId = $oPage->GetTransactionId(); @@ -642,7 +646,66 @@ EOF $oChangeOp->Set('filename', $sFileName); } return $oChangeOp; - } + } + + ///////////////////////////////////////////////////////////////////////// + /** + * Returns if Attachments should be readonly for $oObject in the $sState state for the $sGUI GUI + * + * @param DBObject $oObject + * @param string $sState + * @param string $sGUI + * @return bool + */ + public static function IsReadonlyState(DBObject $oObject, $sState, $sGUI = self::ENUM_GUI_ALL) + { + $aParamDefaultValue = array( + static::ENUM_GUI_ALL => array( + 'Ticket' => array('closed') + ) + ); + + $bReadonly = false; + $sClass = get_class($oObject); + $aReadonlyStatus = MetaModel::GetModuleSetting('itop-attachments', 'readonly_states', $aParamDefaultValue); + if(!empty($aReadonlyStatus)) + { + // Merging GUIs entries + $aEntries = array(); + // - All + if( array_key_exists(static::ENUM_GUI_ALL, $aReadonlyStatus) ) + { + $aEntries = array_merge_recursive($aEntries, $aReadonlyStatus[static::ENUM_GUI_ALL]); + } + // - Backoffice & Portals + foreach( array(static::ENUM_GUI_BACKOFFICE, static::ENUM_GUI_PORTALS) as $sEnumGUI) + { + if( in_array($sGUI, array(static::ENUM_GUI_ALL, $sEnumGUI)) ) + { + if( array_key_exists($sEnumGUI, $aReadonlyStatus) ) + { + $aEntries = array_merge_recursive($aEntries, $aReadonlyStatus[$sEnumGUI]); + } + } + } + + $aParentClasses = array_reverse( MetaModel::EnumParentClasses($sClass, ENUM_PARENT_CLASSES_ALL) ); + foreach($aParentClasses as $sParentClass) + { + if( array_key_exists($sParentClass, $aEntries) ) + { + // If we found an ancestor of the object's class, we stop looking event if the current state is not specified + if( in_array($oObject->GetState(), $aEntries[$sParentClass]) ) + { + $bReadonly = true; + } + break; + } + } + } + + return $bReadonly; + } } /** diff --git a/datamodels/2.x/itop-portal-base/portal/src/forms/objectformmanager.class.inc.php b/datamodels/2.x/itop-portal-base/portal/src/forms/objectformmanager.class.inc.php index 5daf17c473..c2c56110fe 100644 --- a/datamodels/2.x/itop-portal-base/portal/src/forms/objectformmanager.class.inc.php +++ b/datamodels/2.x/itop-portal-base/portal/src/forms/objectformmanager.class.inc.php @@ -34,6 +34,7 @@ use \DBObjectSearch; use \DBObjectSetComparator; use \InlineImage; use \AttributeDateTime; +use \AttachmentPlugIn; use \Combodo\iTop\Form\FormManager; use \Combodo\iTop\Form\Form; use \Combodo\iTop\Form\Field\FileUploadField; @@ -52,7 +53,9 @@ class ObjectFormManager extends FormManager const ENUM_MODE_CREATE = 'create'; const ENUM_MODE_APPLY_STIMULUS = 'apply_stimulus'; + /** @var \Silex\Application $oApp */ protected $oApp; + /** @var \DBObject $oObject */ protected $oObject; protected $sMode; protected $sActionRulesToken; @@ -658,9 +661,9 @@ class ObjectFormManager extends FormManager } // Checking if the instance has attachments - if (class_exists('Attachment')) + if (class_exists('Attachment') && class_exists('AttachmentPlugIn')) { - // Checking if the object is allowed for attchments + // Checking if the object is allowed for attachments $bClassAllowed = false; $aAllowedClasses = MetaModel::GetModuleSetting('itop-attachments', 'allowed_classes', array('Ticket')); foreach ($aAllowedClasses as $sAllowedClass) @@ -683,7 +686,8 @@ class ObjectFormManager extends FormManager ->SetAllowDelete($this->oApp['combodo.portal.instance.conf']['properties']['attachments']['allow_delete']) ->SetObject($this->oObject); - if (($this->sMode === static::ENUM_MODE_VIEW) || ($oForm->GetEditableFieldCount() === 0)) + // Checking if we can edit attachments in the current state + if (($this->sMode === static::ENUM_MODE_VIEW) || AttachmentPlugIn::IsReadonlyState($this->oObject, $this->oObject->GetState(), AttachmentPlugIn::ENUM_GUI_PORTALS) === true) { $oField->SetReadOnly(true); }