diff --git a/teemip-request-mgmt/src/Hook/IPRequestPlugIn.php b/teemip-request-mgmt/src/Hook/IPRequestPlugIn.php index 116c330..d408960 100644 --- a/teemip-request-mgmt/src/Hook/IPRequestPlugIn.php +++ b/teemip-request-mgmt/src/Hook/IPRequestPlugIn.php @@ -15,77 +15,66 @@ use utils; use WebPage; -class IPRequestPlugIn implements iApplicationUIExtension -{ +class IPRequestPlugIn implements iApplicationUIExtension { /** * @inheritdoc */ - public function OnDisplayProperties($oObject, WebPage $oPage, $bEditMode = false) - { + public function OnDisplayProperties($oObject, WebPage $oPage, $bEditMode = false) { } /** * @inheritdoc */ - public function OnDisplayRelations($oObject, WebPage $oPage, $bEditMode = false) - { + public function OnDisplayRelations($oObject, WebPage $oPage, $bEditMode = false) { } /** * @inheritdoc */ - public function OnFormSubmit($oObject, $sFormPrefix = '') - { + public function OnFormSubmit($oObject, $sFormPrefix = '') { } - - public function OnFormCancel($sTempId) - { + + public function OnFormCancel($sTempId) { } /** * @inheritdoc */ - public function EnumUsedAttributes($oObject) - { + public function EnumUsedAttributes($oObject) { return array(); } /** * @inheritdoc */ - public function GetIcon($oObject) - { + public function GetIcon($oObject) { return ''; } /** * @inheritdoc */ - public function GetHilightClass($oObject) - { + public function GetHilightClass($oObject) { return HILIGHT_CLASS_NONE; } /** * @inheritdoc */ - public function EnumAllowedActions(DBObjectSet $oSet) - { + public function EnumAllowedActions(DBObjectSet $oSet) { $oObj = $oSet->Fetch(); - + // Additional actions for IPRequest - if ($oObj instanceof IPRequest) - { - // Add action if in new state only - if ($oObj->Get('status') == 'assigned') - { + if ($oObj instanceof IPRequest) { + // Add action if in assigned state only + if ($oObj->Get('status') == 'assigned') { $oAppContext = new ApplicationContext(); $sContext = $oAppContext->GetForLink(); - + $sClass = get_class($oObj); - if (UserRights::IsStimulusAllowed($sClass, 'ev_resolve')) - { + if (UserRights::IsStimulusAllowed($sClass, 'ev_resolve')) { $id = $oObj->GetKey(); + return array(Dict::S('UI:IPManagement:Action:Implement:IPRequest') => utils::GetAbsoluteUrlModulesRoot()."teemip-request-mgmt/ui.teemip-request-mgmt.php?operation=stimulus&stimulus=ev_resolve&class=$sClass&id=$id&$sContext"); } } diff --git a/teemip-request-mgmt/src/Model/AttributeClassWithIP.php b/teemip-request-mgmt/src/Model/AttributeClassWithIP.php index a5981d8..b6e031b 100644 --- a/teemip-request-mgmt/src/Model/AttributeClassWithIP.php +++ b/teemip-request-mgmt/src/Model/AttributeClassWithIP.php @@ -10,55 +10,44 @@ * - that have an external key to an IPAddress or to an IPvnAddress (n = 4 or 6) */ -class AttributeClassWithIP extends AttributeString -{ - public function GetAllowedValues($aArgs = array(), $sContains = '') - { +class AttributeClassWithIP extends AttributeString { + public function GetAllowedValues($aArgs = array(), $sContains = '') { $oHostObj = null; $aValues = array(); - if (isset($aArgs['this'])) - { + if (isset($aArgs['this'])) { $oHostObj = $aArgs['this']; - } - elseif (isset($aArgs['this->object()'])) - { + } elseif (isset($aArgs['this->object()'])) { $oHostObj = $aArgs['this->object()']; } - if ($oHostObj != null) - { + if ($oHostObj != null) { $iOrgId = $oHostObj->Get('org_id'); $aCIClassesWithIp = IPAddress::GetListOfClassesWIthIP('leaf'); - foreach($aCIClassesWithIp as $sCIClass => $sKey) - { + foreach ($aCIClassesWithIp as $sCIClass => $sKey) { $oCISet = new CMDBObjectSet(DBObjectSearch::FromOQL("SELECT FunctionalCI AS ci WHERE ci.org_id = :org_id AND ci.finalclass = :ciclass"), array(), array('org_id' => $iOrgId, 'ciclass' => $sCIClass)); - if ($oCISet->CountExceeds(0)) - { + if ($oCISet->CountExceeds(0)) { $aValues[$sCIClass] = MetaModel::GetName($sCIClass); } } } + return $aValues; } - public function GetAsHTML($sValue, $oHostObject = null, $bLocalize = true) - { - if (empty($sValue)) - { + public function GetAsHTML($sValue, $oHostObject = null, $bLocalize = true) { + if (empty($sValue)) { return ''; } + return MetaModel::GetName($sValue); } - static public function GetFormFieldClass() - { + static public function GetFormFieldClass() { return '\\Combodo\\iTop\\Form\\Field\\SelectField'; } - public function MakeFormField(DBObject $oObject, $oFormField = null) - { - if ($oFormField === null) - { + public function MakeFormField(DBObject $oObject, $oFormField = null) { + if ($oFormField === null) { // Later : We should check $this->Get('display_style') and create a Radio / Select / ... regarding its value $sFormFieldClass = static::GetFormFieldClass(); $oFormField = new $sFormFieldClass($this->GetCode()); diff --git a/teemip-request-mgmt/src/Model/AttributeIPFieldInClass.php b/teemip-request-mgmt/src/Model/AttributeIPFieldInClass.php index aff676b..3c0b3cd 100644 --- a/teemip-request-mgmt/src/Model/AttributeIPFieldInClass.php +++ b/teemip-request-mgmt/src/Model/AttributeIPFieldInClass.php @@ -10,25 +10,18 @@ * - that point to an IPAddress or to an IPvnAddress (n = 4 or 6) */ -class AttributeIPFieldInClass extends AttributeString -{ - public function GetAllowedValues($aArgs = array(), $sContains = '') - { +class AttributeIPFieldInClass extends AttributeString { + public function GetAllowedValues($aArgs = array(), $sContains = '') { $oHostObj = null; $aValues = array(); - if (isset($aArgs['this'])) - { + if (isset($aArgs['this'])) { $oHostObj = $aArgs['this']; - } - elseif (isset($aArgs['this->object()'])) - { + } elseif (isset($aArgs['this->object()'])) { $oHostObj = $aArgs['this->object()']; } - if ($oHostObj != null) - { + if ($oHostObj != null) { $sThisClass = get_class($oHostObj); - switch ($sThisClass) - { + switch ($sThisClass) { case 'IPRequestAddressCreateV6': $sClass = 'IPv6Address'; break; @@ -39,44 +32,38 @@ public function GetAllowedValues($aArgs = array(), $sContains = '') break; } $sCiClass = $oHostObj->Get('ciclass'); - if ($sCiClass != '') - { + if ($sCiClass != '') { $aCIClassesWithIp = IPAddress::GetListOfClassesWIthIP('leaf'); - foreach($aCIClassesWithIp[$sCiClass]['IPAddress'] as $sKey => $sAttribute) - { + foreach ($aCIClassesWithIp[$sCiClass]['IPAddress'] as $sKey => $sAttribute) { $oAttDef = MetaModel::GetAttributeDef($sCiClass, $sAttribute); $aValues[$oAttDef->GetCode()] = $oAttDef->GetLabel(); } - foreach($aCIClassesWithIp[$sCiClass][$sClass] as $sKey => $sAttribute) - { + foreach ($aCIClassesWithIp[$sCiClass][$sClass] as $sKey => $sAttribute) { $oAttDef = MetaModel::GetAttributeDef($sCiClass, $sAttribute); $aValues[$oAttDef->GetCode()] = $oAttDef->GetLabel(); } } } + return $aValues; } - public function GetAsHTML($sValue, $oHostObject = null, $bLocalize = true) - { - if (empty($sValue) || is_null($oHostObject)) - { + public function GetAsHTML($sValue, $oHostObject = null, $bLocalize = true) { + if (empty($sValue) || is_null($oHostObject)) { return ''; } $sCiClass = $oHostObject->Get('ciclass'); $oAttDef = MetaModel::GetAttributeDef($sCiClass, $sValue); + return $oAttDef->GetLabel(); } - static public function GetFormFieldClass() - { + static public function GetFormFieldClass() { return '\\Combodo\\iTop\\Form\\Field\\SelectField'; } - public function MakeFormField(DBObject $oObject, $oFormField = null) - { - if ($oFormField === null) - { + public function MakeFormField(DBObject $oObject, $oFormField = null) { + if ($oFormField === null) { // Later : We should check $this->Get('display_style') and create a Radio / Select / ... regarding its value $sFormFieldClass = static::GetFormFieldClass(); $oFormField = new $sFormFieldClass($this->GetCode()); diff --git a/teemip-request-mgmt/src/Model/_IPRequest.php b/teemip-request-mgmt/src/Model/_IPRequest.php index ea44056..edc3f59 100644 --- a/teemip-request-mgmt/src/Model/_IPRequest.php +++ b/teemip-request-mgmt/src/Model/_IPRequest.php @@ -6,42 +6,49 @@ namespace TeemIp\TeemIp\Extension\IPRequestManagement\Model; +use cmdbAbstractObject; +use Combodo\iTop\Application\UI\Base\Component\Button\ButtonUIBlockFactory; +use Combodo\iTop\Application\UI\Base\Component\Form\FormUIBlockFactory; +use Combodo\iTop\Application\UI\Base\Component\Input\InputUIBlockFactory; +use Combodo\iTop\Application\UI\Base\Component\Toolbar\ToolbarUIBlockFactory; +use Combodo\iTop\Application\UI\Base\Layout\Object\ObjectFactory; +use Combodo\iTop\Application\UI\Base\Layout\PageContent\PageContentFactory; +use Combodo\iTop\Application\UI\Base\Layout\UIContentBlock; +use Dict; +use iTopWebPage; +use MetaModel; use Ticket; use utils; use WebPage; -class _IPRequest extends Ticket -{ +class _IPRequest extends Ticket { /** * @param $sStimulusCode * * @return bool + * @throws \CoreException + * @throws \CoreUnexpectedValue */ - public function SetClosureDate($sStimulusCode) - { + public function SetClosureDate($sStimulusCode) { $this->Set('close_date', time()); + return true; } /** - * @throws \CoreException - * @throws \CoreUnexpectedValue + * @inheritdoc */ - protected function OnInsert() - { + protected function OnInsert() { $this->Set('start_date', time()); $this->Set('last_update', time()); } /** - * @throws \CoreException - * @throws \CoreUnexpectedValue + * @inheritdoc */ - protected function OnUpdate() - { + protected function OnUpdate() { // Auto assign if possible - if (($this->Get('status') == 'new') && ($this->Get('team_id') > 0) && ($this->Get('agent_id') > 0)) - { + if (($this->Get('status') == 'new') && ($this->Get('team_id') > 0) && ($this->Get('agent_id') > 0)) { $this->ApplyStimulus('ev_auto_assign'); } @@ -49,10 +56,9 @@ protected function OnUpdate() } /** - * @return string + * @inheritdoc */ - public static function GetTicketRefFormat() - { + public static function GetTicketRefFormat() { return 'R-IP-%06d'; } @@ -63,10 +69,8 @@ public static function GetTicketRefFormat() * @throws \CoreException * @throws \Exception */ - public function GetIcon($bImgTag = true) - { - switch($this->GetState()) - { + public function GetIcon($bImgTag = true) { + switch ($this->GetState()) { case 'closed': $sIconFile = 'iprequest-closed.png'; break; @@ -76,89 +80,241 @@ public function GetIcon($bImgTag = true) break; } $sPath = utils::GetAbsoluteUrlModulesRoot().'teemip-request-mgmt/asset/img/'.$sIconFile; - if ($bImgTag) - { + if ($bImgTag) { return "\"\"/"; - } - else - { + } else { return $sPath; } } + /* + * Displays choices related to operation. + * + * @param \iTopWebPage $oP + * @param $oAppContext + * @param $sOperation + * @param $aDefault + * + * @return void + * @throws \ArchivedObjectException + * @throws \CoreException + * @throws \DictExceptionMissingString + */ + public function DisplayOperationForm(iTopWebPage $oP, $oAppContext, $sOperation, $aDefault = array()) { + $id = $this->GetKey(); + $sClass = get_class($this); + $sClassLabel = MetaModel::GetName($sClass); + $sUIPath = $this->MakeUIPath($sOperation); + + $sNextOperation = $this->GetNextOperation($sOperation); + if (version_compare(ITOP_DESIGN_LATEST_VERSION, '3.0', '<')) { + // Set page titles + $this->SetPageTitles($oP, $sUIPath); + + // Set blue modification frame + $oP->add("
\n"); + + // Preparation to allow new values to be posted + $aFieldsMap = array(); + $sPrefix = ''; + $m_iFormId = $this->GetNewFormId($sPrefix); + $iTransactionId = utils::GetNewTransactionId(); + $oP->SetTransactionId($iTransactionId); + $sFormAction = utils::GetAbsoluteUrlModulesRoot()."teemip-request-mgmt/ui.teemip-request-mgmt.php"; + $oP->add("
\n"); + $oP->add_ready_script("$(window).unload(function() { OnUnload('$iTransactionId') } );\n"); + + // Display action fields + $this->DisplayActionFieldsForOperation($oP, $sOperation, $m_iFormId, $aDefault); + + // Load other parameters to post + $oP->add($oAppContext->GetForForm()); + $oP->add("\n"); + $oP->add("\n"); + $oP->add("\n"); + $oP->add("\n"); + + $oP->add('
'); + $oP->add("
\n"); + + $iFieldsCount = count($aFieldsMap); + $sJsonFieldsMap = json_encode($aFieldsMap); + $sState = $this->GetState(); + $oP->add_script( + <<GetName(), $sClassLabel); + $oP->SetBreadCrumbEntry($sUITitle, $sUITitle, '', '', 'fas fa-wrench', iTopWebPage::ENUM_BREADCRUMB_ENTRY_ICON_TYPE_CSS_CLASSES); + $oP->set_title($sUITitle); + + $iTransactionId = utils::GetNewTransactionId(); + $oP->SetTransactionId($iTransactionId); + $this->GetNewFormId(''); + + $oP->SetContentLayout(PageContentFactory::MakeForObjectDetails($this, cmdbAbstractObject::ENUM_DISPLAY_MODE_VIEW)); + $oContentBlock = new UIContentBlock(); + $oP->AddUiBlock($oContentBlock); + + $oForm = FormUIBlockFactory::MakeStandard(); + $oContentBlock->AddSubBlock($oForm); + + $oForm->AddHtml($oAppContext->GetForForm()) + ->AddSubBlock(InputUIBlockFactory::MakeForHidden('operation', $sNextOperation)) + ->AddSubBlock(InputUIBlockFactory::MakeForHidden('class', $sClass)) + ->AddSubBlock(InputUIBlockFactory::MakeForHidden('id', $id)) + ->AddSubBlock(InputUIBlockFactory::MakeForHidden('transaction_id', $iTransactionId)); + + $oToolbarButtons = ToolbarUIBlockFactory::MakeStandard(null); + $oCancelButton = ButtonUIBlockFactory::MakeForCancel(Dict::S('UI:Button:Cancel'), 'cancel', 'cancel')->SetOnClickJsCode("BackToDetails('$sClass', '$id', '', '{null}');"); + $oCancelButton->AddCSSClasses(['action', 'cancel']); + $oToolbarButtons->AddSubBlock($oCancelButton); + $oApplyButton = ButtonUIBlockFactory::MakeForPrimaryAction(Dict::S('UI:Button:Apply'), null, null, true); + $oApplyButton->AddCSSClass('action'); + $oToolbarButtons->AddSubBlock($oApplyButton); + + $oObjectDetails = ObjectFactory::MakeDetails($this); + $oToolbarButtons->AddCSSClass('ibo-toolbar-top'); + $oObjectDetails->AddToolbarBlock($oToolbarButtons); + + $oForm->AddSubBlock($oObjectDetails); + + // Note: DisplayBareHeader is called before adding $oObjectDetails to the page, so it can inject HTML before it through $oPage. + $oP->AddTabContainer(OBJECT_PROPERTIES_TAB, '', $oObjectDetails); + $oP->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB); + $oP->SetCurrentTab(Dict::S($sUIPath)); + + // Display action fields and action buttons + $this->DisplayActionFieldsForOperationV3($oP, $oObjectDetails, $sOperation, $aDefault); + + $oP->add_ready_script( + <<set_title(Dict::Format($sUIPath.':PageTitle_Object_Class', $this->GetName(), $sClassLabel)); + $oP->add("
\n"); + $sIcon = ''; + if ($bIcon) { + $sIcon = $this->GetIcon()." "; + } + $oP->add("

".$sIcon.Dict::Format($sUIPath.':Title_Class_Object', $sClassLabel, + ''.$this->GetName().'')."

\n"); + $oP->add("
\n"); + } + /** * @param $sPrefix * * @return string */ - public function GetNewFormId($sPrefix) - { + public function GetNewFormId($sPrefix) { self::$iGlobalFormId++; $this->m_iFormId = $sPrefix.self::$iGlobalFormId; + return ($this->m_iFormId); } /** * Create common string for UI displays + * + * @param $sOperation + * + * @return string */ - public function MakeUIPath($sOperation) - { - switch ($sOperation) - { - case 'stimulus': + public function MakeUIPath($sOperation) { + switch ($sOperation) { + case 'stimulus': case 'apply_stimulus': - return ('UI:IPManagement:Action:Implement:IPRequest:'); + return ('UI:IPManagement:Action:Implement:IPRequest'); default: return ''; } } - + /** - * Return next operation after current one + * @param $sOperation + * + * @return string */ - function GetNextOperation($sOperation) - { - switch ($sOperation) - { - case 'stimulus': return 'apply_stimulus'; - case 'apply_stimulus': return 'stimulus'; - - default: return ''; + function GetNextOperation($sOperation) { + switch ($sOperation) { + case 'stimulus': + return 'apply_stimulus'; + case 'apply_stimulus': + return 'stimulus'; + + default: + return ''; } } - + /** * Check validity of stimulus before allowing it to be applied */ - public function CheckStimulus($sStimulusCode) - { + public function CheckStimulus($sStimulusCode) { return ''; } + /** + * @inheritdoc + */ + public function DisplayBareRelations(WebPage $oP, $bEditMode = false) { + parent::DisplayBareRelations($oP, $bEditMode); - /* - * Display additional tabs to Zone object + if (!$bEditMode) { + $oP->RemoveTab('Ticket:ImpactAnalysis'); + } + } + + /** + * Display attributes associated to an operation * - * @param \WebPage $oP - * @param bool $bEditMode + * @param \iTopWebPage $oP + * @param $sOperation + * @param $iFormId + * @param $aDefault * + * @throws \ArchivedObjectException * @throws \CoreException - * @throws \CoreUnexpectedValue * @throws \DictExceptionMissingString - * @throws \MissingQueryArgument - * @throws \MySQLException - * @throws \MySQLHasGoneAwayException - * @throws \OQLException */ - public function DisplayBareRelations(WebPage $oP, $bEditMode = false) - { - parent::DisplayBareRelations($oP, $bEditMode); + protected function DisplayActionFieldsForOperation(iTopWebPage $oP, $sOperation, $iFormId, $aDefault) { + } - if (!$bEditMode) - { - $oP->RemoveTab('Ticket:ImpactAnalysis'); - } + /** + * Display attributes associated to operation - V >= 3.0 + * + * @param \iTopWebPage $oP + * @param $oClassForm + * @param $sOperation + * @param $aDefault + * + * @return void + */ + protected function DisplayActionFieldsForOperationV3(iTopWebPage $oP, $oClassForm, $sOperation, $aDefault) { } } diff --git a/teemip-request-mgmt/src/Model/_IPRequestAddressCreateV4.php b/teemip-request-mgmt/src/Model/_IPRequestAddressCreateV4.php index 0263a47..6da667f 100644 --- a/teemip-request-mgmt/src/Model/_IPRequestAddressCreateV4.php +++ b/teemip-request-mgmt/src/Model/_IPRequestAddressCreateV4.php @@ -7,55 +7,47 @@ namespace TeemIp\TeemIp\Extension\IPRequestManagement\Model; use CMDBObjectSet; +use Combodo\iTop\Application\UI\Base\Component\Html\HtmlFactory; +use Combodo\iTop\Application\UI\Base\Component\Input\InputUIBlockFactory; +use Combodo\iTop\Application\UI\Base\Component\Input\Select\SelectOptionUIBlockFactory; +use Combodo\iTop\Application\UI\Base\Component\Input\SelectUIBlockFactory; +use Combodo\iTop\Application\UI\Base\Layout\MultiColumn\Column\Column; +use Combodo\iTop\Application\UI\Base\Layout\MultiColumn\MultiColumn; use DBObjectSearch; use Dict; use IPConfig; use IPRequestAddressCreate; use IPv4Address; +use iTopWebPage; use MetaModel; use TeemIp\TeemIp\Extension\Framework\Helper\IPUtils; use UserRights; use utils; -use WebPage; -class _IPRequestAddressCreateV4 extends IPRequestAddressCreate -{ +class _IPRequestAddressCreateV4 extends IPRequestAddressCreate { /** - * @throws \ArchivedObjectException - * @throws \CoreCannotSaveObjectException - * @throws \CoreException - * @throws \CoreUnexpectedValue - * @throws \CoreWarning - * @throws \MySQLException - * @throws \OQLException + * @inheritdoc */ - public function AfterInsert() - { + public function AfterInsert() { parent::AfterInsert(); // Has the user the right profile for auto registration ? $aProfiles = UserRights::ListProfiles(); - if (in_array('IP Portal Automation user', $aProfiles)) - { + if (in_array('IP Portal Automation user', $aProfiles)) { // Can the stimulus be applied ? $sResCheck = $this->CheckStimulus('ev_resolve'); - if ($sResCheck == '') - { + if ($sResCheck == '') { // If the subnet exists... $oIPSubnet = MetaModel::GetObject('IPv4Subnet', $this->Get('subnet_id'), false /* MustBeFound */); - if (!is_null($oIPSubnet)) - { + if (!is_null($oIPSubnet)) { // ... and allows auto registration - if ($oIPSubnet->Get('allow_automatic_ip_creation') == "yes") - { + if ($oIPSubnet->Get('allow_automatic_ip_creation') == "yes") { // If there is as least one Ip available $aFreeIPs = $this->GetFreeIPs(); - if (count($aFreeIPs) > 0) - { - if (parent::ApplyStimulus('ev_resolve', true /* $bDoNotWrite */)) - { + if (count($aFreeIPs) > 0) { + if (parent::ApplyStimulus('ev_resolve', true /* $bDoNotWrite */)) { // Register IP and update public log - $this->RegisterIp(true,$aFreeIPs[0]); + $this->RegisterIp(true, $aFreeIPs[0]); $oLog = $this->Get('public_log'); $sLogEntry = Dict::S('UI:IPManagement:Action:Implement:IPRequestAutomaticallyProcessed'); @@ -72,113 +64,73 @@ public function AfterInsert() } /** - * Check validity of stimulus before allowing it to be applied - * - * @param $sStimulusCode - * - * @return string - * @throws \ArchivedObjectException - * @throws \CoreException - * @throws \CoreUnexpectedValue + * @inheritdoc */ - public function CheckStimulus($sStimulusCode) - { - if ($sStimulusCode == 'ev_resolve') - { + public function CheckStimulus($sStimulusCode) { + if ($sStimulusCode == 'ev_resolve') { // Run the check only if no IP has been manually assigned yet ! - if ($this->Get('ip_id') <= 0) - { + if ($this->Get('ip_id') <= 0) { // Check that range or subnet is not full already - $oIpContainer = MetaModel::GetObject('IPv4Range', $this->Get('range_id'), false /* MustBeFound */); - if (is_null($oIpContainer)) - { + $oIpContainer = MetaModel::GetObject('IPv4Range', $this->Get('range_id'), false /* MustBeFound */); + if (is_null($oIpContainer)) { $oIpContainer = MetaModel::GetObject('IPv4Subnet', $this->Get('subnet_id'), false /* MustBeFound */); - if (is_null($oIpContainer)) - { + if (is_null($oIpContainer)) { return (Dict::Format('UI:IPManagement:Action:Implement:IPRequestAddressCreate:NoSuchSubnet', $this->Get('block_id'))); - } - elseif ($oIpContainer->GetOccupancy('IPv4Address_out_IPv4Range') == 100) - { + } elseif ($oIpContainer->GetOccupancy('IPv4Address_out_IPv4Range') == 100) { return (Dict::Format('UI:IPManagement:Action:Implement:IPRequestAddressCreate:FullSubnet')); } + } elseif ($oIpContainer->GetOccupancy('IPv4Address') == 100) { + return (Dict::Format('UI:IPManagement:Action:Implement:IPRequestAddressCreate:FullRange')); } - else - { - if ($oIpContainer->GetOccupancy('IPv4Address') == 100) - { - return (Dict::Format('UI:IPManagement:Action:Implement:IPRequestAddressCreate:FullRange')); - } - } - + // Check that an IP address can be created with given parameters $oIp = MetaModel::NewObject('IPv4Address'); $oIp->Set('org_id', $this->Get('org_id')); $oIp->Set('short_name', $this->Get('short_name')); $oIp->Set('domain_id', $this->Get('domain_id')); $oIp->ComputeValues(); - if (! $oIp->IsFqdnUnique()) - { + if (!$oIp->IsFqdnUnique()) { return (Dict::Format('UI:IPManagement:Action:Implement:IPRequestAddressCreate:IPNameCollision')); } } } return ''; } - + /** - * Display attributes associated operation - * - * @param \WebPage $oP - * @param $sOperation - * @param $m_iFormId - * @param array $aDefault - * - * @return string - * @throws \ArchivedObjectException - * @throws \CoreException - * @throws \CoreUnexpectedValue - * @throws \DictExceptionMissingString - * @throws \MySQLException - * @throws \OQLException + * @inheritdoc */ - function DisplayActionFieldsForOperation(WebPage $oP, $sOperation, $m_iFormId, $aDefault = array()) - { + protected function DisplayActionFieldsForOperation(iTopWebPage $oP, $sOperation, $iFormId, $aDefault) { $sStimulus = $aDefault['stimulus']; - if ($sStimulus != 'ev_resolve') - { - return ''; + if ($sStimulus != 'ev_resolve') { + return; } - + $oP->add(""); $oP->add("\n"); $oP->add(''); - + // Cancel button $iObjId = $this->GetKey(); $oP->add(""); - + $oP->add("
'); // Check if IP has already been manually allocated - if ($this->Get('ip_id') <= 0) - { + if ($this->Get('ip_id') <= 0) { // No IP has already been manually allocated, offer some // Get array of free IPs $sLabelOfAction1 = Dict::S('UI:IPManagement:Action:Implement:IPRequestAddressCreate:PickAnIp'); $aFreeIPs = $this->GetFreeIPs(); $iNumberOfFreeIps = count($aFreeIPs); - + // There is at least an IP free. Check has been done before... // ... unless pings has been required and all IPs ping - if ($iNumberOfFreeIps != 0) - { + if ($iNumberOfFreeIps != 0) { // Translate it into select box - $sInputId = $m_iFormId.'_'.'ip'; + $sInputId = $iFormId.'_'.'ip'; $sHTMLValue = ""; - } - else - { + } else { $sHTMLValue = ""; } } @@ -192,67 +144,95 @@ function DisplayActionFieldsForOperation(WebPage $oP, $sOperation, $m_iFormId, $ $aDetails[] = array('label' => ''.$sLabelOfAction1.'', 'value' => $sHTMLValue); $oP->Details($aDetails); $oP->add('
  "); - + // Implement button $oP->add("  
"); } - + /** - * Apply stimulus to object - * - * @param string $sStimulusCode - * @param false $bDoNotWrite - * - * @return bool - * @throws \ArchivedObjectException - * @throws \CoreCannotSaveObjectException - * @throws \CoreException - * @throws \CoreUnexpectedValue - * @throws \CoreWarning - * @throws \MySQLException - * @throws \OQLException + * @inheritdoc */ - public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) - { - if ($sStimulusCode != 'ev_resolve') - { + protected function DisplayActionFieldsForOperationV3(iTopWebPage $oP, $oForm, $sOperation, $aDefault) { + $sStimulus = $aDefault['stimulus']; + if ($sStimulus != 'ev_resolve') { + return; + } + + $oForm->AddSubBlock(InputUIBlockFactory::MakeForHidden('stimulus', 'ev_resolve')); + + $oMultiColumn = new MultiColumn(); + $oP->AddUIBlock($oMultiColumn); + $oColumn1 = new Column(); // First column = labels or fields + $oMultiColumn->AddColumn($oColumn1); + $oColumn2 = new Column(); // Second column = data + $oMultiColumn->AddColumn($oColumn2); + + // Check if IP has already been manually allocated + if ($this->Get('ip_id') <= 0) { + // No IP has already been manually allocated, offer some + // Get array of free IPs + $sLabelOfAction1 = Dict::S('UI:IPManagement:Action:Implement:IPRequestAddressCreate:PickAnIp'); + + $aFreeIPs = $this->GetFreeIPs(); + $iNumberOfFreeIps = count($aFreeIPs); + + // There is at least an IP free. Check has been done before... + // ... unless pings has been required and all IPs ping + $oSelect = SelectUIBlockFactory::MakeForSelect('ip'); + $oColumn2->AddSubBlock($oSelect); + if ($iNumberOfFreeIps != 0) { + // There is at least an IP free. Check has been done before. + for ($i = 0; $i < $iNumberOfFreeIps; $i++) { + $bSelected = ($i == 0) ? true : false; + $oSelect->AddOption(SelectOptionUIBlockFactory::MakeForSelectOption($aFreeIPs[$i], $aFreeIPs[$i], $bSelected)); + } + } + } else { + // AnIP has already been manually allocated + $sLabelOfAction1 = Dict::Format('UI:IPManagement:Action:Implement:IPRequestAddressCreate:ConfirmSelectedIP', $this->GetAsHTML('ip_id')); + } + $oColumn1->AddSubBlock(HtmlFactory::MakeParagraph($sLabelOfAction1)); + $oColumn1->AddSubBlock(HtmlFactory::MakeRaw('
')); + } + + /** + * @inheritdoc + */ + public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) { + if ($sStimulusCode != 'ev_resolve') { return parent::ApplyStimulus($sStimulusCode); } - + $bProceedWithChange = false; - if ($this->Get('ip_id') != 0) - { + if ($this->Get('ip_id') != 0) { // An IP has already been manually allocated $bProceedWithChange = true; - $bRegisterNewIp = false; - } - else - { + $bRegisterNewIp = false; + } else { // No IP has been allocated yet $sIp = filter_var(utils::ReadPostedParam('ip', '', 'raw_data'), FILTER_VALIDATE_IP); - if ($sIp != '') - { + if ($sIp != '') { $bProceedWithChange = true; $bRegisterNewIp = true; } } - if ($bProceedWithChange) - { - if (parent::ApplyStimulus($sStimulusCode, true /* $bDoNotWrite */)) - { + if ($bProceedWithChange) { + if (parent::ApplyStimulus($sStimulusCode, true /* $bDoNotWrite */)) { $this->RegisterIp($bRegisterNewIp, $sIp); // Update ticket $this->DBUpdate(); + return true; } } + return false; } @@ -266,22 +246,19 @@ public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) * @throws \MySQLException * @throws \OQLException */ - private function GetFreeIPs() - { + private function GetFreeIPs() { $aFreeIPs = array(); // Make sure specified subnet exists $iSubnetId = $this->Get('subnet_id'); $oSubnet = MetaModel::GetObject('IPv4Subnet', $iSubnetId, true /* MustBeFound */); - if (is_null($oSubnet)) - { + if (is_null($oSubnet)) { return $aFreeIPs; } // Define search interval $oIpContainer = MetaModel::GetObject('IPv4Range', $this->Get('range_id'), false /* MustBeFound */); - if (is_null($oIpContainer)) - { + if (is_null($oIpContainer)) { $bWithRanges = true; $oIpContainer = $oSubnet; $sFirstIp = $oIpContainer->Get('ip'); @@ -289,9 +266,7 @@ private function GetFreeIPs() $oIpRangeSet = new CMDBObjectSet(DBObjectSearch::FromOQL("SELECT IPv4Range AS r WHERE r.subnet_id = $iSubnetId")); $aRangeIPs = $oIpRangeSet->GetColumnAsArray('firstip', false); $oIpRangeSet->Rewind(); - } - else - { + } else { $bWithRanges = false; $sFirstIp = $oIpContainer->Get('firstip'); $sLastIp = $oIpContainer->Get('lastip'); @@ -303,8 +278,7 @@ private function GetFreeIPs() $iOrgId = $this->Get('org_id'); $sPingBeforeAssign = IPConfig::GetFromGlobalIPConfig('ping_before_assign', $iOrgId); $iCreationOffset = IPConfig::GetFromGlobalIPConfig('request_creation_ipv4_offset', $iOrgId); - if ($iFirstIp + $iCreationOffset <= $iLastIp) - { + if ($iFirstIp + $iCreationOffset <= $iLastIp) { $iFirstIp += $iCreationOffset; } @@ -312,37 +286,28 @@ private function GetFreeIPs() $iAnIp = $bWithRanges ? ($iFirstIp + 1) : $iFirstIp; $i = 0; $iMaxFreeOffers = ($sPingBeforeAssign == 'ping_yes') ? DEFAULT_MAX_FREE_IP_OFFERS_WITH_PING_REQ : DEFAULT_MAX_FREE_IP_OFFERS_REQ; - while (($iAnIp <= $iLastIp) && ($i < $iMaxFreeOffers)) - { + while (($iAnIp <= $iLastIp) && ($i < $iMaxFreeOffers)) { $sAnIp = IPUtils::mylong2ip($iAnIp); - if ($bWithRanges) - { + if ($bWithRanges) { // If IP belongs to a range, skip range - if (in_array($sAnIp, $aRangeIPs)) - { + if (in_array($sAnIp, $aRangeIPs)) { $oIpRangeSet->Rewind(); $oIpRange = $oIpRangeSet->Fetch(); - while ($oIpRange->Get('firstip') != $sAnIp) - { + while ($oIpRange->Get('firstip') != $sAnIp) { $oIpRange = $oIpRangeSet->Fetch(); } $iAnIp = IPUtils::myip2long($oIpRange->Get('lastip')); } } - if (!in_array($sAnIp, $aRegisteredIPs)) - { + if (!in_array($sAnIp, $aRegisteredIPs)) { // Found free IP. If required, make sure it doesn't ping (well... locally) - if ($sPingBeforeAssign == 'ping_yes') - { + if ($sPingBeforeAssign == 'ping_yes') { $aOutput = IPv4Address::DoCheckIpPings($sAnIp, TIME_TO_WAIT_FOR_PING_SHORT); - if (empty($aOutput)) - { + if (empty($aOutput)) { // IP doesn't ping $aFreeIPs [$i++] = $sAnIp; } - } - else - { + } else { $aFreeIPs [$i++] = $sAnIp; } } @@ -367,10 +332,8 @@ private function GetFreeIPs() * @throws \MySQLException * @throws \OQLException */ - private function RegisterIp($bNewIp, $sIp) - { - if ($bNewIp) - { + private function RegisterIp($bNewIp, $sIp) { + if ($bNewIp) { // Create IP $oIp = MetaModel::NewObject('IPv4Address'); $oIp->Set('org_id', $this->Get('org_id')); @@ -387,9 +350,7 @@ private function RegisterIp($bNewIp, $sIp) // Update ticket with IP $this->Set('ip_id', $oIp->GetKey()); - } - else - { + } else { $iIpId = $this->Get('ip_id'); $oIp = MetaModel::GetObject('IPv4Address', $iIpId, true /* MustBeFound */); $oIp->Set('status', $this->Get('status_ip')); @@ -398,19 +359,15 @@ private function RegisterIp($bNewIp, $sIp) // Update FunctionalCI, if any $sCIClass = $this->Get('ciclass'); - if ($sCIClass != '') - { + if ($sCIClass != '') { $iFunctionalCIid = $this->Get('connectableci_id'); $oFunctionalCI = MetaModel::GetObject($sCIClass, $iFunctionalCIid, false /* MustBeFound */); - if (!is_null($oFunctionalCI)) - { + if (!is_null($oFunctionalCI)) { $sIPAttribute = $this->Get('ci_ip_attribute'); - if (MetaModel::IsValidAttCode($sCIClass, $sIPAttribute)) - { + if (MetaModel::IsValidAttCode($sCIClass, $sIPAttribute)) { // Check if attribute can be written $iFlags = $oFunctionalCI->GetFormAttributeFlags($sIPAttribute); - if (!($iFlags & (OPT_ATT_READONLY | OPT_ATT_SLAVE))) - { + if (!($iFlags & (OPT_ATT_READONLY | OPT_ATT_SLAVE))) { $oFunctionalCI->Set($sIPAttribute, $oIp->GetKey()); $oFunctionalCI->DBUpdate(); } diff --git a/teemip-request-mgmt/src/Model/_IPRequestAddressCreateV6.php b/teemip-request-mgmt/src/Model/_IPRequestAddressCreateV6.php index 02c698d..5ff12b6 100644 --- a/teemip-request-mgmt/src/Model/_IPRequestAddressCreateV6.php +++ b/teemip-request-mgmt/src/Model/_IPRequestAddressCreateV6.php @@ -7,55 +7,47 @@ namespace TeemIp\TeemIp\Extension\IPRequestManagement\Model; use CMDBObjectSet; +use Combodo\iTop\Application\UI\Base\Component\Html\HtmlFactory; +use Combodo\iTop\Application\UI\Base\Component\Input\InputUIBlockFactory; +use Combodo\iTop\Application\UI\Base\Component\Input\Select\SelectOptionUIBlockFactory; +use Combodo\iTop\Application\UI\Base\Component\Input\SelectUIBlockFactory; +use Combodo\iTop\Application\UI\Base\Layout\MultiColumn\Column\Column; +use Combodo\iTop\Application\UI\Base\Layout\MultiColumn\MultiColumn; use DBObjectSearch; use Dict; use IPConfig; use IPRequestAddressCreate; use IPv6Address; +use iTopWebPage; use MetaModel; use TeemIp\TeemIp\Extension\IPv6Management\Model\ormIPv6; use UserRights; use utils; -use WebPage; -class _IPRequestAddressCreateV6 extends IPRequestAddressCreate -{ +class _IPRequestAddressCreateV6 extends IPRequestAddressCreate { /** - * @throws \ArchivedObjectException - * @throws \CoreCannotSaveObjectException - * @throws \CoreException - * @throws \CoreUnexpectedValue - * @throws \CoreWarning - * @throws \MySQLException - * @throws \OQLException + * @inheritdoc */ - public function AfterInsert() - { + public function AfterInsert() { parent::AfterInsert(); // Has the user the right profile for auto registration ? $aProfiles = UserRights::ListProfiles(); - if (in_array('IP Portal Automation user', $aProfiles)) - { + if (in_array('IP Portal Automation user', $aProfiles)) { // Can the stimulus be applied ? $sResCheck = $this->CheckStimulus('ev_resolve'); - if ($sResCheck == '') - { + if ($sResCheck == '') { // If the subnet exists... $oIPSubnet = MetaModel::GetObject('IPv6Subnet', $this->Get('subnet_id'), false /* MustBeFound */); - if (!is_null($oIPSubnet)) - { + if (!is_null($oIPSubnet)) { // ... and allows auto registration - if ($oIPSubnet->Get('allow_automatic_ip_creation') == "yes") - { + if ($oIPSubnet->Get('allow_automatic_ip_creation') == "yes") { // If there is as least one Ip available $aFreeIPs = $this->GetFreeIPs(); - if (count($aFreeIPs) > 0) - { - if (parent::ApplyStimulus('ev_resolve',true /* $bDoNotWrite */)) - { + if (count($aFreeIPs) > 0) { + if (parent::ApplyStimulus('ev_resolve', true /* $bDoNotWrite */)) { // Register IP and update public log - $this->RegisterIp(true,$aFreeIPs[0]); + $this->RegisterIp(true, $aFreeIPs[0]); $oLog = $this->Get('public_log'); $sLogEntry = Dict::S('UI:IPManagement:Action:Implement:IPRequestAutomaticallyProcessed'); @@ -81,29 +73,19 @@ public function AfterInsert() * @throws \CoreException * @throws \CoreUnexpectedValue */ - public function CheckStimulus($sStimulusCode) - { - if ($sStimulusCode == 'ev_resolve') - { + public function CheckStimulus($sStimulusCode) { + if ($sStimulusCode == 'ev_resolve') { // Run the check only if no IP has been manually assigned yet ! - if ($this->Get('ip_id') <= 0) - { + if ($this->Get('ip_id') <= 0) { // Check that range or subnet is not full already $oIpContainer = MetaModel::GetObject('IPv6Range', $this->Get('range_id'), false /* MustBeFound */); - if (is_null($oIpContainer)) - { + if (is_null($oIpContainer)) { $oIpContainer = MetaModel::GetObject('IPv6Subnet', $this->Get('subnet_id'), false /* MustBeFound */); - if (is_null($oIpContainer)) - { + if (is_null($oIpContainer)) { return (Dict::Format('UI:IPManagement:Action:Implement:IPRequestAddressCreate:NoSuchSubnet', $this->Get('block_id'))); } - } - else - { - if ($oIpContainer->GetOccupancy() == 100) - { - return (Dict::Format('UI:IPManagement:Action:Implement:IPRequestAddressCreate:FullRange')); - } + } elseif ($oIpContainer->GetOccupancy() == 100) { + return (Dict::Format('UI:IPManagement:Action:Implement:IPRequestAddressCreate:FullRange')); } // Check that an IP address can be created with given parameters @@ -112,8 +94,7 @@ public function CheckStimulus($sStimulusCode) $oIp->Set('short_name', $this->Get('short_name')); $oIp->Set('domain_id', $this->Get('domain_id')); $oIp->ComputeValues(); - if (!$oIp->IsFqdnUnique()) - { + if (!$oIp->IsFqdnUnique()) { return (Dict::Format('UI:IPManagement:Action:Implement:IPRequestAddressCreate:IPNameCollision')); } } @@ -123,27 +104,12 @@ public function CheckStimulus($sStimulusCode) } /** - * Display attributes associated operation - * - * @param \WebPage $oP - * @param $sOperation - * @param $m_iFormId - * @param array $aDefault - * - * @return string - * @throws \ArchivedObjectException - * @throws \CoreException - * @throws \CoreUnexpectedValue - * @throws \DictExceptionMissingString - * @throws \MySQLException - * @throws \OQLException + * @inheritdoc */ - function DisplayActionFieldsForOperation(WebPage $oP, $sOperation, $m_iFormId, $aDefault = array()) - { + protected function DisplayActionFieldsForOperation(iTopWebPage $oP, $sOperation, $m_iFormId, $aDefault = array()) { $sStimulus = $aDefault['stimulus']; - if ($sStimulus != 'ev_resolve') - { - return ''; + if ($sStimulus != 'ev_resolve') { + return; } $oP->add(""); @@ -151,8 +117,7 @@ function DisplayActionFieldsForOperation(WebPage $oP, $sOperation, $m_iFormId, $ $oP->add('
'); // Check if IP has already been manually allocated - if ($this->Get('ip_id') <= 0) - { + if ($this->Get('ip_id') <= 0) { // No IP has already been manually allocated, offer some // Create array of free IPs $sLabelOfAction1 = Dict::S('UI:IPManagement:Action:Implement:IPRequestAddressCreate:PickAnIp'); @@ -161,26 +126,20 @@ function DisplayActionFieldsForOperation(WebPage $oP, $sOperation, $m_iFormId, $ // There is at least an IP free. Check has been done before... // ... unless pings has been required and all IPs ping - if ($iNumberOfFreeIps != 0) - { + if ($iNumberOfFreeIps != 0) { // Translate it into select box $sInputId = $m_iFormId.'_'.'ip'; $sHTMLValue = ""; - } - else - { + } else { $sHTMLValue = ""; } - } - else - { + } else { // AnIP has already been manually allocated $sLabelOfAction1 = Dict::Format('UI:IPManagement:Action:Implement:IPRequestAddressCreate:ConfirmSelectedIP', $this->GetAsHTML('ip_id')); $sHTMLValue = ""; @@ -201,55 +160,83 @@ function DisplayActionFieldsForOperation(WebPage $oP, $sOperation, $m_iFormId, $ } /** - * Apply stimulus to object - * - * @param string $sStimulusCode - * @param false $bDoNotWrite - * - * @return bool - * @throws \ArchivedObjectException - * @throws \CoreCannotSaveObjectException - * @throws \CoreException - * @throws \CoreUnexpectedValue - * @throws \CoreWarning - * @throws \MySQLException - * @throws \OQLException + * @inheritdoc + */ + protected function DisplayActionFieldsForOperationV3(iTopWebPage $oP, $oForm, $sOperation, $aDefault) { + $sStimulus = $aDefault['stimulus']; + if ($sStimulus != 'ev_resolve') { + return; + } + + $oForm->AddSubBlock(InputUIBlockFactory::MakeForHidden('stimulus', 'ev_resolve')); + + $oMultiColumn = new MultiColumn(); + $oP->AddUIBlock($oMultiColumn); + $oColumn1 = new Column(); // First column = labels or fields + $oMultiColumn->AddColumn($oColumn1); + $oColumn2 = new Column(); // Second column = data + $oMultiColumn->AddColumn($oColumn2); + + // Check if IP has already been manually allocated + if ($this->Get('ip_id') <= 0) { + // No IP has already been manually allocated, offer some + // Get array of free IPs + $sLabelOfAction1 = Dict::S('UI:IPManagement:Action:Implement:IPRequestAddressCreate:PickAnIp'); + + $aFreeIPs = $this->GetFreeIPs(); + $iNumberOfFreeIps = count($aFreeIPs); + + // There is at least an IP free. Check has been done before... + // ... unless pings has been required and all IPs ping + $oSelect = SelectUIBlockFactory::MakeForSelect('ip'); + $oColumn2->AddSubBlock($oSelect); + if ($iNumberOfFreeIps != 0) { + // There is at least an IP free. Check has been done before. + for ($i = 0; $i < $iNumberOfFreeIps; $i++) { + $bSelected = ($i == 0) ? true : false; + $oSelect->AddOption(SelectOptionUIBlockFactory::MakeForSelectOption($aFreeIPs[$i], $aFreeIPs[$i], $bSelected)); + } + } + } else { + // AnIP has already been manually allocated + $sLabelOfAction1 = Dict::Format('UI:IPManagement:Action:Implement:IPRequestAddressCreate:ConfirmSelectedIP', $this->GetAsHTML('ip_id')); + } + $oColumn1->AddSubBlock(HtmlFactory::MakeParagraph($sLabelOfAction1)); + $oColumn1->AddSubBlock(HtmlFactory::MakeRaw('
')); + } + + /** + * @inheritdoc */ - public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) - { - if ($sStimulusCode != 'ev_resolve') - { + public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) { + if ($sStimulusCode != 'ev_resolve') { return parent::ApplyStimulus($sStimulusCode); } $bProceedWithChange = false; - if ($this->Get('ip_id') != 0) - { + if ($this->Get('ip_id') != 0) { // An IP has already been manually allocated $bProceedWithChange = true; $bRegisterNewIp = false; - } - else - { + } else { // No IP has been allocated yet $sIp = filter_var(utils::ReadPostedParam('ip', '', 'raw_data'), FILTER_VALIDATE_IP); - if ($sIp != '') - { + if ($sIp != '') { $bProceedWithChange = true; $bRegisterNewIp = true; } } - if ($bProceedWithChange) - { - if (parent::ApplyStimulus($sStimulusCode, true /* $bDoNotWrite */)) - { + if ($bProceedWithChange) { + if (parent::ApplyStimulus($sStimulusCode, true /* $bDoNotWrite */)) { $this->RegisterIp($bRegisterNewIp, $sIp); // Update ticket $this->DBUpdate(); + return true; } } + return false; } @@ -263,22 +250,19 @@ public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) * @throws \MySQLException * @throws \OQLException */ - private function GetFreeIPs() - { + private function GetFreeIPs() { $aFreeIPs = array(); // Make sure specified subnet exists $iSubnetId = $this->Get('subnet_id'); $oSubnet = MetaModel::GetObject('IPv6Subnet', $iSubnetId, true /* MustBeFound */); - if (is_null($oSubnet)) - { + if (is_null($oSubnet)) { return $aFreeIPs; } // Define search interval $oIpContainer = MetaModel::GetObject('IPv6Range', $this->Get('range_id'), false /* MustBeFound */); - if (is_null($oIpContainer)) - { + if (is_null($oIpContainer)) { $bWithRanges = true; $oIpContainer = $oSubnet; $oFirstIp = $oIpContainer->Get('ip'); @@ -286,9 +270,7 @@ private function GetFreeIPs() $oIpRangeSet = new CMDBObjectSet(DBObjectSearch::FromOQL("SELECT IPv6Range AS r WHERE r.subnet_id = $iSubnetId")); $aRangeIPs = $oIpRangeSet->GetColumnAsArray('firstip', false); $oIpRangeSet->Rewind(); - } - else - { + } else { $bWithRanges = false; $oFirstIp = $oIpContainer->Get('firstip'); $oLastIp = $oIpContainer->Get('lastip'); @@ -299,8 +281,7 @@ private function GetFreeIPs() $sPingBeforeAssign = IPConfig::GetFromGlobalIPConfig('ping_before_assign', $iOrgId); $iCreationOffset = IPConfig::GetFromGlobalIPConfig('request_creation_ipv6_offset',$iOrgId); // Note: because of the size of an Ipv6 subnet, subnet IP + offset (which is less than 10K) < last IP - for($i = 0; $i < $iCreationOffset; $i++) - { + for ($i = 0; $i < $iCreationOffset; $i++) { $oFirstIp = $oFirstIp->GetNextIp(); } @@ -308,37 +289,28 @@ private function GetFreeIPs() $oAnIp = $bWithRanges ? ($oFirstIp->GetNextIp()) : $oFirstIp; $i = 0; $iMaxFreeOffers = ($sPingBeforeAssign == 'ping_yes') ? DEFAULT_MAX_FREE_IP_OFFERS_WITH_PING_REQ : DEFAULT_MAX_FREE_IP_OFFERS_REQ; - while ($oAnIp->IsSmallerOrEqual($oLastIp) && ($i < $iMaxFreeOffers)) - { + while ($oAnIp->IsSmallerOrEqual($oLastIp) && ($i < $iMaxFreeOffers)) { $sAnIp = $oAnIp->ToString(); - if ($bWithRanges) - { + if ($bWithRanges) { // If IP belongs to a range, skip range - if (in_array($oAnIp, $aRangeIPs)) - { + if (in_array($oAnIp, $aRangeIPs)) { $oIpRangeSet->Rewind(); $oIpRange = $oIpRangeSet->Fetch(); - while (!$oIpRange->Get('firstip')->IsEqual($oAnIp)) - { + while (!$oIpRange->Get('firstip')->IsEqual($oAnIp)) { $oIpRange = $oIpRangeSet->Fetch(); } $oAnIp = $oIpRange->Get('lastip'); } } - if (!in_array($oAnIp, $aRegisteredIPs)) - { + if (!in_array($oAnIp, $aRegisteredIPs)) { // Found free IP. If required, make sure it doesn't ping (well... locally) - if ($sPingBeforeAssign == 'ping_yes') - { + if ($sPingBeforeAssign == 'ping_yes') { $aOutput = IPv6Address::DoCheckIpPings($sAnIp, TIME_TO_WAIT_FOR_PING_SHORT); - if (empty($aOutput)) - { + if (empty($aOutput)) { // IP doesn't ping $aFreeIPs [$i++] = $sAnIp; } - } - else - { + } else { $aFreeIPs [$i++] = $sAnIp; } } @@ -363,10 +335,8 @@ private function GetFreeIPs() * @throws \MySQLException * @throws \OQLException */ - private function RegisterIp($bNewIp, $sIp) - { - if ($bNewIp) - { + private function RegisterIp($bNewIp, $sIp) { + if ($bNewIp) { // Create IP $oIpv6 = MetaModel::NewObject('IPv6Address'); $oIpv6->Set('org_id', $this->Get('org_id')); @@ -384,9 +354,7 @@ private function RegisterIp($bNewIp, $sIp) // Update ticket with IP $this->Set('ip_id', $oIpv6->GetKey()); - } - else - { + } else { $iIpId = $this->Get('ip_id'); $oIpv6 = MetaModel::GetObject('IPv6Address', $iIpId, true /* MustBeFound */); $oIpv6->Set('status', $this->Get('status_ip')); @@ -395,19 +363,15 @@ private function RegisterIp($bNewIp, $sIp) // Update FunctionalCI, if any $sCIClass = $this->Get('ciclass'); - if ($sCIClass != '') - { + if ($sCIClass != '') { $iFunctionalCIid = $this->Get('connectableci_id'); $oFunctionalCI = MetaModel::GetObject($sCIClass, $iFunctionalCIid, false /* MustBeFound */); - if (!is_null($oFunctionalCI)) - { + if (!is_null($oFunctionalCI)) { $sIPAttribute = $this->Get('ci_ip_attribute'); - if (MetaModel::IsValidAttCode($sCIClass, $sIPAttribute)) - { + if (MetaModel::IsValidAttCode($sCIClass, $sIPAttribute)) { // Check if attribute can be written $iFlags = $oFunctionalCI->GetFormAttributeFlags($sIPAttribute); - if (!($iFlags & (OPT_ATT_READONLY | OPT_ATT_SLAVE))) - { + if (!($iFlags & (OPT_ATT_READONLY | OPT_ATT_SLAVE))) { $oFunctionalCI->Set($sIPAttribute, $oIpv6->GetKey()); $oFunctionalCI->DBUpdate(); } diff --git a/teemip-request-mgmt/src/Model/_IPRequestAddressDelete.php b/teemip-request-mgmt/src/Model/_IPRequestAddressDelete.php index aee8e93..a66b199 100644 --- a/teemip-request-mgmt/src/Model/_IPRequestAddressDelete.php +++ b/teemip-request-mgmt/src/Model/_IPRequestAddressDelete.php @@ -11,24 +11,17 @@ use MetaModel; use UserRights; -class _IPRequestAddressDelete extends IPRequestAddress -{ +class _IPRequestAddressDelete extends IPRequestAddress { /** - * @throws \ArchivedObjectException - * @throws \CoreCannotSaveObjectException - * @throws \CoreException - * @throws \CoreUnexpectedValue + * @inheritdoc */ - public function AfterInsert() - { + public function AfterInsert() { parent::AfterInsert(); // If user profile allows it and if parameter allows automatic processing, try to release IP straight away $aProfiles = UserRights::ListProfiles(); - if (in_array('IP Portal Automation user', $aProfiles)) - { - if (parent::ApplyStimulus('ev_resolve', true /* $bDoNotWrite */)) - { + if (in_array('IP Portal Automation user', $aProfiles)) { + if (parent::ApplyStimulus('ev_resolve', true /* $bDoNotWrite */)) { // Release IP and update public log $oIp = MetaModel::GetObject('IPAddress', $this->Get('ip_id'), false /* MustBeFound */); $sIp = (is_null($oIp)) ? '' : $oIp->Get('ip'); @@ -45,30 +38,18 @@ public function AfterInsert() } /** - * Apply stimulus to object - * - * @param string $sStimulusCode - * @param false $bDoNotWrite - * - * @return bool - * @throws \CoreException - * @throws \CoreUnexpectedValue + * @inheritdoc */ - public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) - { - if ($sStimulusCode != 'ev_resolve') - { + public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) { + if ($sStimulusCode != 'ev_resolve') { return parent::ApplyStimulus($sStimulusCode); + } elseif (parent::ApplyStimulus($sStimulusCode, false /* $bDoNotWrite */)) { + $oIp = MetaModel::GetObject('IPAddress', $this->Get('ip_id'), false /* MustBeFound */); + + return $this->ReleaseIP($oIp); } - else - { - if (parent::ApplyStimulus($sStimulusCode, false /* $bDoNotWrite */)) - { - $oIp = MetaModel::GetObject('IPAddress', $this->Get('ip_id'), false /* MustBeFound */); - return $this->ReleaseIP($oIp); - } - return false; - } + + return false; } /** @@ -78,17 +59,15 @@ public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) * @throws \CoreException * @throws \CoreUnexpectedValue */ - private function ReleaseIP($oIp) - { - if (!is_null($oIp)) - { + private function ReleaseIP($oIp) { + if (!is_null($oIp)) { $oIp->Set('status', 'released'); // release_date is managed at IPObject level $iCallerId = $this->Get('caller_id'); - if (!is_null($iCallerId )) - { + if (!is_null($iCallerId)) { $oIp->Set('requestor_id', $iCallerId); } $oIp->DBUpdate(); + return true; } return false; diff --git a/teemip-request-mgmt/src/Model/_IPRequestAddressUpdate.php b/teemip-request-mgmt/src/Model/_IPRequestAddressUpdate.php index f65c25f..06ea68b 100644 --- a/teemip-request-mgmt/src/Model/_IPRequestAddressUpdate.php +++ b/teemip-request-mgmt/src/Model/_IPRequestAddressUpdate.php @@ -11,28 +11,20 @@ use MetaModel; use UserRights; -class _IPRequestAddressUpdate extends IPRequestAddress -{ +class _IPRequestAddressUpdate extends IPRequestAddress { /** - * @throws \ArchivedObjectException - * @throws \CoreCannotSaveObjectException - * @throws \CoreException - * @throws \CoreUnexpectedValue + * @inheritdoc */ - public function AfterInsert() - { + public function AfterInsert() { parent::AfterInsert(); // Has the user the right profile for automatic update ? $aProfiles = UserRights::ListProfiles(); - if (in_array('IP Portal Automation user', $aProfiles)) - { + if (in_array('IP Portal Automation user', $aProfiles)) { // Can the stimulus be applied ? $sResCheck = $this->CheckStimulus('ev_resolve'); - if ($sResCheck == '') - { - if (parent::ApplyStimulus('ev_resolve', false /* $bDoNotWrite */)) - { + if ($sResCheck == '') { + if (parent::ApplyStimulus('ev_resolve', false /* $bDoNotWrite */)) { // Update IP and update public log $oIp = MetaModel::GetObject('IPAddress', $this->Get('ip_id'), false /* MustBeFound */); $sIp = (is_null($oIp)) ? '' : $oIp->Get('ip'); @@ -59,61 +51,45 @@ public function AfterInsert() * @throws \CoreException * @throws \CoreUnexpectedValue */ - public function CheckStimulus($sStimulusCode) - { - if ($sStimulusCode == 'ev_resolve') - { + public function CheckStimulus($sStimulusCode) { + if ($sStimulusCode == 'ev_resolve') { // Check that an IP address can be created with given parameters $sNewShortName = $this->Get('new_short_name'); - if ($sNewShortName != '') - { + if ($sNewShortName != '') { // Check uniqueness of FQDN $oIp = MetaModel::GetObject('IPAddress', $this->Get('ip_id'), false); - if (!is_null($oIp)) - { + if (!is_null($oIp)) { $oIp->Set('org_id', $this->Get('org_id')); $oIp->Set('short_name', $this->Get('new_short_name')); $oIp->Set('domain_id', $this->Get('new_domain_id')); $oIp->ComputeValues(); - if (!$oIp->IsFqdnUnique()) - { + if (!$oIp->IsFqdnUnique()) { return (Dict::Format('UI:IPManagement:Action:Implement:IPRequestAddressUpdate:IPNameCollision')); } - } - else - { + } else { return (Dict::Format('UI:IPManagement:Action:Implement:IPRequestAddressUpdate:NoSelectedIP')); } } } + return ''; } - + /** - * @param string $sStimulusCode - * @param false $bDoNotWrite - * - * @return bool - * @throws \ArchivedObjectException - * @throws \CoreCannotSaveObjectException - * @throws \CoreException - * @throws \CoreUnexpectedValue + * @inheritdoc */ - public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) - { - if ($sStimulusCode != 'ev_resolve') - { + public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) { + if ($sStimulusCode != 'ev_resolve') { return parent::ApplyStimulus($sStimulusCode); - } - else - { - if (parent::ApplyStimulus($sStimulusCode, false /* $bDoNotWrite */)) - { + } else { + if (parent::ApplyStimulus($sStimulusCode, false /* $bDoNotWrite */)) { $oIp = MetaModel::GetObject('IPAddress', $this->Get('ip_id'), false /* MustBeFound */); + return $this->UpdateIP($oIp); } - return false; } + + return false; } /** @@ -123,33 +99,26 @@ public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) * @throws \CoreException * @throws \CoreUnexpectedValue */ - private function UpdateIP($oIp) - { - if (!is_null($oIp)) - { + private function UpdateIP($oIp) { + if (!is_null($oIp)) { $sNewStatusIp = $this->Get('new_status_ip'); - if (($sNewStatusIp != '') && ($sNewStatusIp != $oIp->Get('status'))) - { + if (($sNewStatusIp != '') && ($sNewStatusIp != $oIp->Get('status'))) { $oIp->Set('status', $sNewStatusIp); } $sNewShortName = $this->Get('new_short_name'); - if (($sNewShortName != '') && ($sNewShortName != $oIp->Get('short_name'))) - { + if (($sNewShortName != '') && ($sNewShortName != $oIp->Get('short_name'))) { $oIp->Set('short_name', $sNewShortName); } $iNewDomainId = $this->Get('new_domain_id'); - if (($iNewDomainId != 0) && ($iNewDomainId != $oIp->Get('domain_id'))) - { + if (($iNewDomainId != 0) && ($iNewDomainId != $oIp->Get('domain_id'))) { $oIp->Set('domain_id', $iNewDomainId); } $iNewUsageId = $this->Get('new_usage_id'); - if (($iNewUsageId != '') && ($iNewUsageId != $oIp->Get('usage_id'))) - { + if (($iNewUsageId != '') && ($iNewUsageId != $oIp->Get('usage_id'))) { $oIp->Set('usage_id', $iNewUsageId); } $iCallerId = $this->Get('caller_id'); - if (! is_null($iCallerId )) - { + if (!is_null($iCallerId)) { $oIp->Set('requestor_id', $iCallerId); } $oIp->DBUpdate(); diff --git a/teemip-request-mgmt/src/Model/_IPRequestSubnetCreateV4.php b/teemip-request-mgmt/src/Model/_IPRequestSubnetCreateV4.php index 3eca5a7..26ed8a2 100644 --- a/teemip-request-mgmt/src/Model/_IPRequestSubnetCreateV4.php +++ b/teemip-request-mgmt/src/Model/_IPRequestSubnetCreateV4.php @@ -6,44 +6,42 @@ namespace TeemIp\TeemIp\Extension\IPRequestManagement\Model; +use Combodo\iTop\Application\UI\Base\Component\Html\HtmlFactory; +use Combodo\iTop\Application\UI\Base\Component\Input\InputUIBlockFactory; +use Combodo\iTop\Application\UI\Base\Component\Input\Select\SelectOptionUIBlockFactory; +use Combodo\iTop\Application\UI\Base\Component\Input\SelectUIBlockFactory; +use Combodo\iTop\Application\UI\Base\Layout\MultiColumn\Column\Column; +use Combodo\iTop\Application\UI\Base\Layout\MultiColumn\MultiColumn; use Dict; use IPRequestSubnetCreate; +use iTopWebPage; use MetaModel; use TeemIp\TeemIp\Extension\Framework\Helper\IPUtils; use UserRights; use utils; -use WebPage; -class _IPRequestSubnetCreateV4 extends IPRequestSubnetCreate -{ +class _IPRequestSubnetCreateV4 extends IPRequestSubnetCreate { /** - * @throws \ArchivedObjectException - * @throws \CoreException + * @inheritdoc */ - public function AfterInsert() - { + public function AfterInsert() { parent::AfterInsert(); // Has the user the right profile for auto registration ? $aProfiles = UserRights::ListProfiles(); - if (in_array('IP Portal Automation user', $aProfiles)) - { + if (in_array('IP Portal Automation user', $aProfiles)) { // CheckStimulus is not called as all check operations need to be replayed here // If the block exists... $oBlock = MetaModel::GetObject('IPv4Block', $this->Get('block_id'), false /* MustBeFound */); - if (!is_null($oBlock)) - { + if (!is_null($oBlock)) { // ... and allows auto registration - if ($oBlock->Get('allow_automatic_subnet_creation') == "yes") - { + if ($oBlock->Get('allow_automatic_subnet_creation') == "yes") { // Check if there is some space available $iSize = IPUtils::MaskToSize($this->Get('mask')); $aFreeSpace = $oBlock->GetFreeSpace($iSize, DEFAULT_MAX_FREE_SPACE_OFFERS_REQ); - if (count($aFreeSpace) > 0) - { + if (count($aFreeSpace) > 0) { // Register subnet and update public log - if (parent::ApplyStimulus('ev_resolve',true /* $bDoNotWrite */)) - { + if (parent::ApplyStimulus('ev_resolve', true /* $bDoNotWrite */)) { $this->RegisterSubnet(true, $aFreeSpace[0]['firstip']); $oLog = $this->Get('public_log'); @@ -68,86 +66,64 @@ public function AfterInsert() * @throws \ArchivedObjectException * @throws \CoreException */ - public function CheckStimulus($sStimulusCode) - { - if ($sStimulusCode == 'ev_resolve') - { + public function CheckStimulus($sStimulusCode) { + if ($sStimulusCode == 'ev_resolve') { // Run the check only if no subnet has been manually assigned yet ! - if ($this->Get('subnet_id') <= 0) - { + if ($this->Get('subnet_id') <= 0) { // Check that block is not full already for required size $oBlock = MetaModel::GetObject('IPv4Block', $this->Get('block_id'), false /* MustBeFound */); - if (is_null($oBlock)) - { + if (is_null($oBlock)) { return (Dict::Format('UI:IPManagement:Action:Implement:IPRequestSubnetCreate:NoSuchBlock', $this->Get('block_id'))); } $iSize = IPUtils::MaskToSize($this->Get('mask')); $aFreeSpace = $oBlock->GetFreeSpace($iSize, DEFAULT_MAX_FREE_SPACE_OFFERS_REQ); - $iSizeFreeArray = sizeof ($aFreeSpace); - if ($iSizeFreeArray == 0) - { + $iSizeFreeArray = sizeof($aFreeSpace); + if ($iSizeFreeArray == 0) { return (Dict::Format('UI:IPManagement:Action:Implement:IPRequestSubnetCreate:NoSpaceInBlock', $this->Get('mask'))); } } } + return ''; } - + /** - * Display attributes associated operation - * - * @param \WebPage $oP - * @param $sOperation - * @param $m_iFormId - * @param array $aDefault - * - * @return string - * @throws \ArchivedObjectException - * @throws \CoreException + * @inheritdoc */ - function DisplayActionFieldsForOperation(WebPage $oP, $sOperation, $m_iFormId, $aDefault = array()) - { + protected function DisplayActionFieldsForOperation(iTopWebPage $oP, $sOperation, $m_iFormId, $aDefault = array()) { $sStimulus = $aDefault['stimulus']; - if ($sStimulus != 'ev_resolve') - { - return ''; + if ($sStimulus != 'ev_resolve') { + return; } - + $oP->add(""); $oP->add("\n"); $oP->add(''); - + // Cancel button $iObjId = $this->GetKey(); $oP->add(""); $oP->add("
'); // Check if Subnet has already been manually allocated - if ($this->Get('subnet_id') <= 0) - { + if ($this->Get('subnet_id') <= 0) { // No subnet has already been manually allocated, offer some $sLabelOfAction1 = Dict::S('UI:IPManagement:Action:Implement:IPRequestSubnetCreate:PickASubnet'); $oBlock = MetaModel::GetObject('IPv4Block', $this->Get('block_id'), true /* MustBeFound */); $iSize = IPUtils::MaskToSize($this->Get('mask')); $aFreeSpace = $oBlock->GetFreeSpace($iSize, DEFAULT_MAX_FREE_SPACE_OFFERS_REQ); - $iSizeFreeArray = sizeof ($aFreeSpace); - if ($iSizeFreeArray != 0) - { + $iSizeFreeArray = sizeof($aFreeSpace); + if ($iSizeFreeArray != 0) { // Translate list of spaces into select box $sInputId = $m_iFormId.'_'.'ip'; $sHTMLValue = ""; - } - else - { + $sHTMLValue .= ""; + } else { $sHTMLValue = ""; } - } - else - { + } else { // A subnet has already been manually allocated $sLabelOfAction1 = Dict::Format('UI:IPManagement:Action:Implement:IPRequestSubnetCreate:ConfirmSelectedSubnet', $this->GetAsHTML('subnet_id')); $sHTMLValue = ""; @@ -156,62 +132,93 @@ function DisplayActionFieldsForOperation(WebPage $oP, $sOperation, $m_iFormId, $ $aDetails[] = array('label' => ''.$sLabelOfAction1.'', 'value' => $sHTMLValue); $oP->Details($aDetails); $oP->add('
  "); - - + + // Implement button $oP->add("  
"); } - + /** - * Apply stimulus to object - * - * @param $sStimulusCode - * @param bool $bDoNotWrite - * - * @return bool - * @throws \CoreException - * @throws \CoreUnexpectedValue + * @inheritdoc */ - public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) - { - if ($sStimulusCode != 'ev_resolve') - { - return parent::ApplyStimulus($sStimulusCode); + protected function DisplayActionFieldsForOperationV3(iTopWebPage $oP, $oForm, $sOperation, $aDefault) { + $sStimulus = $aDefault['stimulus']; + if ($sStimulus != 'ev_resolve') { + return; + } + + $oForm->AddSubBlock(InputUIBlockFactory::MakeForHidden('stimulus', 'ev_resolve')); + + $oMultiColumn = new MultiColumn(); + $oP->AddUIBlock($oMultiColumn); + $oColumn1 = new Column(); // First column = labels or fields + $oMultiColumn->AddColumn($oColumn1); + $oColumn2 = new Column(); // Second column = data + $oMultiColumn->AddColumn($oColumn2); + + // Check if IP has already been manually allocated + if ($this->Get('subnet_id') <= 0) { + // No subnet has already been manually allocated, offer some + $sLabelOfAction1 = Dict::S('UI:IPManagement:Action:Implement:IPRequestSubnetCreate:PickASubnet'); + + $oBlock = MetaModel::GetObject('IPv4Block', $this->Get('block_id'), true /* MustBeFound */); + $iSize = IPUtils::MaskToSize($this->Get('mask')); + $aFreeSpace = $oBlock->GetFreeSpace($iSize, DEFAULT_MAX_FREE_SPACE_OFFERS_REQ); + $iSizeFreeArray = sizeof($aFreeSpace); + + $oSelect = SelectUIBlockFactory::MakeForSelect('ip'); + $oColumn2->AddSubBlock($oSelect); + if ($iSizeFreeArray != 0) { + // Translate list of spaces into select box + for ($i = 0; $i < sizeof($aFreeSpace); $i++) { + $bSelected = ($i == 0) ? true : false; + $sFirstIp = $aFreeSpace[$i]['firstip']; + $oSelect->AddOption(SelectOptionUIBlockFactory::MakeForSelectOption($sFirstIp, $sFirstIp, $bSelected)); + } + } + } else { + // A subnet has already been manually allocated + $sLabelOfAction1 = Dict::Format('UI:IPManagement:Action:Implement:IPRequestSubnetCreate:ConfirmSelectedSubnet', $this->GetAsHTML('subnet_id')); } - else - { + $oColumn1->AddSubBlock(HtmlFactory::MakeParagraph($sLabelOfAction1)); + $oColumn1->AddSubBlock(HtmlFactory::MakeRaw('
')); + } + + /** + * @inheritdoc + */ + public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) { + if ($sStimulusCode != 'ev_resolve') { + return parent::ApplyStimulus($sStimulusCode); + } else { $bProceedWithChange = false; - if ($this->Get('subnet_id') > 0) - { + if ($this->Get('subnet_id') > 0) { // A subnet has already been manually allocated $bProceedWithChange = true; - $bRegisterNewSubnet = false; - } - else - { + $bRegisterNewSubnet = false; + } else { // No subnet has been allocated yet $sIp = filter_var(utils::ReadPostedParam('ip', '', 'raw_data'), FILTER_VALIDATE_IP); - if ($sIp != '') - { + if ($sIp != '') { $bProceedWithChange = true; $bRegisterNewSubnet = true; } } - if ($bProceedWithChange) - { - if (parent::ApplyStimulus($sStimulusCode, true /* $bDoNotWrite */)) - { + if ($bProceedWithChange) { + if (parent::ApplyStimulus($sStimulusCode, true /* $bDoNotWrite */)) { $this->RegisterSubnet($bRegisterNewSubnet, $sIp); $this->DBUpdate(); + return true; } } + return false; } } @@ -230,11 +237,9 @@ public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) * @throws \MySQLException * @throws \OQLException */ - private function RegisterSubnet($bNewSubnet, $sSubnetIp) - { + private function RegisterSubnet($bNewSubnet, $sSubnetIp) { // Prepare and register subnet - if ($bNewSubnet) - { + if ($bNewSubnet) { $oSubnet = MetaModel::NewObject('IPv4Subnet'); $oSubnet->Set('org_id', $this->Get('org_id')); $oSubnet->Set('block_id', $this->Get('block_id')); @@ -246,8 +251,7 @@ private function RegisterSubnet($bNewSubnet, $sSubnetIp) $oSubnet->Set('requestor_id', $this->Get('caller_id')); $oSubnet->DBInsert(); - if (!$this->Get('location_id') <= 0) - { + if (!$this->Get('location_id') <= 0) { // A geography has been selected. $oNewLocationLink = MetaModel::NewObject('lnkIPSubnetToLocation'); $oNewLocationLink->Set('ipsubnet_id', $oSubnet->GetKey()); diff --git a/teemip-request-mgmt/src/Model/_IPRequestSubnetCreateV6.php b/teemip-request-mgmt/src/Model/_IPRequestSubnetCreateV6.php index af710a0..76bc8e6 100644 --- a/teemip-request-mgmt/src/Model/_IPRequestSubnetCreateV6.php +++ b/teemip-request-mgmt/src/Model/_IPRequestSubnetCreateV6.php @@ -6,43 +6,41 @@ namespace TeemIp\TeemIp\Extension\IPRequestManagement\Model; +use Combodo\iTop\Application\UI\Base\Component\Html\HtmlFactory; +use Combodo\iTop\Application\UI\Base\Component\Input\InputUIBlockFactory; +use Combodo\iTop\Application\UI\Base\Component\Input\Select\SelectOptionUIBlockFactory; +use Combodo\iTop\Application\UI\Base\Component\Input\SelectUIBlockFactory; +use Combodo\iTop\Application\UI\Base\Layout\MultiColumn\Column\Column; +use Combodo\iTop\Application\UI\Base\Layout\MultiColumn\MultiColumn; use Dict; use IPRequestSubnetCreate; +use iTopWebPage; use MetaModel; use TeemIp\TeemIp\Extension\IPv6Management\Model\ormIPv6; use UserRights; use utils; -use WebPage; -class _IPRequestSubnetCreateV6 extends IPRequestSubnetCreate -{ +class _IPRequestSubnetCreateV6 extends IPRequestSubnetCreate { /** - * @throws \ArchivedObjectException - * @throws \CoreException + * @inheritdoc */ - public function AfterInsert() - { + public function AfterInsert() { parent::AfterInsert(); // Has the user the right profile for auto registration ? $aProfiles = UserRights::ListProfiles(); - if (in_array('IP Portal Automation user', $aProfiles)) - { + if (in_array('IP Portal Automation user', $aProfiles)) { // CheckStimulus is not called as all check operations need to be replayed here // If the block exists... $oBlock = MetaModel::GetObject('IPv6Block', $this->Get('block_id'), false /* MustBeFound */); - if (!is_null($oBlock)) - { + if (!is_null($oBlock)) { // ... and allows auto registration - if ($oBlock->Get('allow_automatic_subnet_creation') == "yes") - { + if ($oBlock->Get('allow_automatic_subnet_creation') == "yes") { // Check if there is some space available $iPrefix = $this->Get('mask'); $aFreeSpace = $oBlock->GetFreeSpace($iPrefix, DEFAULT_MAX_FREE_SPACE_OFFERS_REQ); - if (count($aFreeSpace) > 0) - { - if (parent::ApplyStimulus('ev_resolve',true /* $bDoNotWrite */)) - { + if (count($aFreeSpace) > 0) { + if (parent::ApplyStimulus('ev_resolve', true /* $bDoNotWrite */)) { // Register subnet and update public log $this->RegisterSubnet(true, $aFreeSpace[0]['firstip']->ToString()); @@ -62,77 +60,64 @@ public function AfterInsert() /** * Check validity of stimulus before allowing it to be applied */ - public function CheckStimulus($sStimulusCode) - { - if ($sStimulusCode == 'ev_resolve') - { + public function CheckStimulus($sStimulusCode) { + if ($sStimulusCode == 'ev_resolve') { // Run the check only if no subnet has been manually assigned yet ! - if ($this->Get('subnet_id') <= 0) - { + if ($this->Get('subnet_id') <= 0) { // Check that block is not full already for required size $oBlock = MetaModel::GetObject('IPv6Block', $this->Get('block_id'), false /* MustBeFound */); - if (is_null($oBlock)) - { + if (is_null($oBlock)) { return (Dict::Format('UI:IPManagement:Action:Implement:IPRequestSubnetCreate:NoSuchBlock', $this->Get('block_id'))); } $iPrefix = $this->Get('mask'); $aFreeSpace = $oBlock->GetFreeSpace($iPrefix, DEFAULT_MAX_FREE_SPACE_OFFERS_REQ); - $iSizeFreeArray = sizeof ($aFreeSpace); - if ($iSizeFreeArray == 0) - { + $iSizeFreeArray = sizeof($aFreeSpace); + if ($iSizeFreeArray == 0) { return (Dict::Format('UI:IPManagement:Action:Implement:IPRequestSubnetCreate:NoSpaceInBlock', $this->Get('mask'))); } } } + return ''; } - + /** - * Display attributes associated operation - */ - function DisplayActionFieldsForOperation(WebPage $oP, $sOperation, $m_iFormId, $aDefault = array()) - { + * @inheritdoc + */ + protected function DisplayActionFieldsForOperation(iTopWebPage $oP, $sOperation, $m_iFormId, $aDefault = array()) { $sStimulus = $aDefault['stimulus']; - if ($sStimulus != 'ev_resolve') - { - return ''; + if ($sStimulus != 'ev_resolve') { + return; } - + $oP->add(""); $oP->add("\n"); $oP->add(''); - + // Cancel button $iObjId = $this->GetKey(); $oP->add(""); $oP->add("
'); // Check if Subnet has already been manually allocated - if ($this->Get('subnet_id') <= 0) - { + if ($this->Get('subnet_id') <= 0) { // No subnet has already been manually allocated, offer some $sLabelOfAction1 = Dict::S('UI:IPManagement:Action:Implement:IPRequestSubnetCreate:PickASubnet'); $oBlock = MetaModel::GetObject('IPv6Block', $this->Get('block_id'), true /* MustBeFound */); $iPrefix = $this->Get('mask'); $aFreeSpace = $oBlock->GetFreeSpace($iPrefix, DEFAULT_MAX_FREE_SPACE_OFFERS_REQ); - $iSizeFreeArray = sizeof ($aFreeSpace); - if ($iSizeFreeArray != 0) - { + $iSizeFreeArray = sizeof($aFreeSpace); + if ($iSizeFreeArray != 0) { // Translate list of spaces into select box $sInputId = $m_iFormId.'_'.'ip'; $sHTMLValue = ""; - } - else - { + $sHTMLValue .= ""; + } else { $sHTMLValue = ""; } - } - else - { + } else { // A subnet has already been manually allocated $sLabelOfAction1 = Dict::Format('UI:IPManagement:Action:Implement:IPRequestSubnetCreate:ConfirmSelectedSubnet', $this->GetAsHTML('subnet_id')); $sHTMLValue = ""; @@ -141,55 +126,92 @@ function DisplayActionFieldsForOperation(WebPage $oP, $sOperation, $m_iFormId, $ $aDetails[] = array('label' => ''.$sLabelOfAction1.'', 'value' => $sHTMLValue); $oP->Details($aDetails); $oP->add('
  "); - - + + // Implement button $oP->add("  
"); } - + /** - * Apply stimulus to object + * @inheritdoc */ - public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) - { - if ($sStimulusCode != 'ev_resolve') - { - return parent::ApplyStimulus($sStimulusCode); + protected function DisplayActionFieldsForOperationV3(iTopWebPage $oP, $oForm, $sOperation, $aDefault) { + $sStimulus = $aDefault['stimulus']; + if ($sStimulus != 'ev_resolve') { + return; } - else - { + $oForm->AddSubBlock(InputUIBlockFactory::MakeForHidden('stimulus', 'ev_resolve')); + + $oMultiColumn = new MultiColumn(); + $oP->AddUIBlock($oMultiColumn); + $oColumn1 = new Column(); // First column = labels or fields + $oMultiColumn->AddColumn($oColumn1); + $oColumn2 = new Column(); // Second column = data + $oMultiColumn->AddColumn($oColumn2); + + // Check if Subnet has already been manually allocated + if ($this->Get('subnet_id') <= 0) { + // No subnet has already been manually allocated, offer some + $sLabelOfAction1 = Dict::S('UI:IPManagement:Action:Implement:IPRequestSubnetCreate:PickASubnet'); + + $oBlock = MetaModel::GetObject('IPv6Block', $this->Get('block_id'), true /* MustBeFound */); + $iPrefix = $this->Get('mask'); + $aFreeSpace = $oBlock->GetFreeSpace($iPrefix, DEFAULT_MAX_FREE_SPACE_OFFERS_REQ); + $iSizeFreeArray = sizeof($aFreeSpace); + + $oSelect = SelectUIBlockFactory::MakeForSelect('ip'); + $oColumn2->AddSubBlock($oSelect); + if ($iSizeFreeArray != 0) { + // Translate list of spaces into select box + for ($i = 0; $i < sizeof($aFreeSpace); $i++) { + $bSelected = ($i == 0) ? true : false; + $sFirstIp = $aFreeSpace[$i]['firstip']->ToString(); + $oSelect->AddOption(SelectOptionUIBlockFactory::MakeForSelectOption($sFirstIp, $sFirstIp, $bSelected)); + } + } + } else { + // A subnet has already been manually allocated + $sLabelOfAction1 = Dict::Format('UI:IPManagement:Action:Implement:IPRequestSubnetCreate:ConfirmSelectedSubnet', $this->GetAsHTML('subnet_id')); + } + $oColumn1->AddSubBlock(HtmlFactory::MakeParagraph($sLabelOfAction1)); + $oColumn1->AddSubBlock(HtmlFactory::MakeRaw('
')); + } + + /** + * @inheritdoc + */ + public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) { + if ($sStimulusCode != 'ev_resolve') { + return parent::ApplyStimulus($sStimulusCode); + } else { $bProceedWithChange = false; - if ($this->Get('subnet_id') > 0) - { + if ($this->Get('subnet_id') > 0) { // A subnet has already been manually allocated $bProceedWithChange = true; - $bRegisterNewSubnet = false; - } - else - { + $bRegisterNewSubnet = false; + } else { // No subnet has been allocated yet $sIp = filter_var(utils::ReadPostedParam('ip', '', 'raw_data'), FILTER_VALIDATE_IP); - if ($sIp != '') - { + if ($sIp != '') { $bProceedWithChange = true; $bRegisterNewSubnet = true; } } - if ($bProceedWithChange) - { - if (parent::ApplyStimulus($sStimulusCode, true /* $bDoNotWrite */)) - { + if ($bProceedWithChange) { + if (parent::ApplyStimulus($sStimulusCode, true /* $bDoNotWrite */)) { $this->RegisterSubnet($bRegisterNewSubnet, $sIp); $this->DBUpdate(); + return true; } } + return false; } } @@ -208,11 +230,9 @@ public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) * @throws \MySQLException * @throws \OQLException */ - private function RegisterSubnet($bNewSubnet, $sSubnetIp) - { + private function RegisterSubnet($bNewSubnet, $sSubnetIp) { // Prepare and register subnet - if ($bNewSubnet) - { + if ($bNewSubnet) { $oSubnet = MetaModel::NewObject('IPv6Subnet'); $oSubnet->Set('org_id', $this->Get('org_id')); $oSubnet->Set('block_id', $this->Get('block_id')); @@ -225,8 +245,7 @@ private function RegisterSubnet($bNewSubnet, $sSubnetIp) $oSubnet->Set('requestor_id', $this->Get('caller_id')); $oSubnet->DBInsert(); - if (!$this->Get('location_id') <= 0) - { + if (!$this->Get('location_id') <= 0) { // A geography has been selected. $oNewLocationLink = MetaModel::NewObject('lnkIPSubnetToLocation'); $oNewLocationLink->Set('ipsubnet_id', $oSubnet->GetKey()); diff --git a/teemip-request-mgmt/src/Model/_IPRequestSubnetDelete.php b/teemip-request-mgmt/src/Model/_IPRequestSubnetDelete.php index e08cb10..4e993c2 100644 --- a/teemip-request-mgmt/src/Model/_IPRequestSubnetDelete.php +++ b/teemip-request-mgmt/src/Model/_IPRequestSubnetDelete.php @@ -11,24 +11,17 @@ use MetaModel; use UserRights; -class _IPRequestSubnetDelete extends IPRequestSubnet -{ +class _IPRequestSubnetDelete extends IPRequestSubnet { /** - * @throws \ArchivedObjectException - * @throws \CoreCannotSaveObjectException - * @throws \CoreException - * @throws \CoreUnexpectedValue + * @inheritdoc */ - public function AfterInsert() - { + public function AfterInsert() { parent::AfterInsert(); // If user profile allows it and if parameter allows automatic processing, try to release subnet straight away $aProfiles = UserRights::ListProfiles(); - if (in_array('IP Portal Automation user', $aProfiles)) - { - if (parent::ApplyStimulus('ev_resolve', true /* $bDoNotWrite */)) - { + if (in_array('IP Portal Automation user', $aProfiles)) { + if (parent::ApplyStimulus('ev_resolve', true /* $bDoNotWrite */)) { // Release subnet and update public log $oSubnet = MetaModel::GetObject('IPSubnet', $this->Get('subnet_id'), false /* MustBeFound */); $sSubnet = (is_null($oSubnet)) ? '' : $oSubnet->Get('ip').' /'.$oSubnet->Get('mask'); @@ -45,32 +38,19 @@ public function AfterInsert() } /** - * Apply stimulus to object - * - * @param $sStimulusCode - * @param bool $bDoNotWrite - * - * @return bool - * @throws \ArchivedObjectException - * @throws \CoreCannotSaveObjectException - * @throws \CoreException - * @throws \CoreUnexpectedValue + * @inheritdoc */ - public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) - { - if ($sStimulusCode != 'ev_resolve') - { + public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) { + if ($sStimulusCode != 'ev_resolve') { return parent::ApplyStimulus($sStimulusCode); + } elseif (parent::ApplyStimulus($sStimulusCode, false /* $bDoNotWrite */)) { + $oSubnet = MetaModel::GetObject('IPSubnet', $this->Get('subnet_id'), false /* MustBeFound */); + + return $this->ReleaseSubnet($oSubnet); } - else - { - if (parent::ApplyStimulus($sStimulusCode, false /* $bDoNotWrite */)) - { - $oSubnet = MetaModel::GetObject('IPSubnet', $this->Get('subnet_id'), false /* MustBeFound */); - return $this->ReleaseSubnet($oSubnet); - } - return false; - } + + return false; + } /** @@ -80,23 +60,19 @@ public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) * @throws \CoreException * @throws \CoreUnexpectedValue */ - private function ReleaseSubnet($oSubnet) - { - if (!is_null($oSubnet)) - { + private function ReleaseSubnet($oSubnet) { + if (!is_null($oSubnet)) { $oSubnet->Set('status', 'released'); // release_date is managed at IPObject level $iCallerId = $this->Get('caller_id'); - if (!is_null($iCallerId )) - { + if (!is_null($iCallerId)) { $oSubnet->Set('requestor_id', $iCallerId); } $oSubnet->DBUpdate(); + return true; } - else - { - return false; - } + + return false; } } diff --git a/teemip-request-mgmt/src/Model/_IPRequestSubnetUpdate.php b/teemip-request-mgmt/src/Model/_IPRequestSubnetUpdate.php index a7fd7b7..bf02a18 100644 --- a/teemip-request-mgmt/src/Model/_IPRequestSubnetUpdate.php +++ b/teemip-request-mgmt/src/Model/_IPRequestSubnetUpdate.php @@ -13,28 +13,20 @@ use MetaModel; use UserRights; -class _IPRequestSubnetUpdate extends IPRequestSubnet -{ +class _IPRequestSubnetUpdate extends IPRequestSubnet { /** - * @throws \ArchivedObjectException - * @throws \CoreCannotSaveObjectException - * @throws \CoreException - * @throws \CoreUnexpectedValue + * @inheritdoc */ - public function AfterInsert() - { + public function AfterInsert() { parent::AfterInsert(); // Has the user the right profile for automatic update ? $aProfiles = UserRights::ListProfiles(); - if (in_array('IP Portal Automation user', $aProfiles)) - { + if (in_array('IP Portal Automation user', $aProfiles)) { // Can the stimulus be applied ? $sResCheck = $this->CheckStimulus('ev_resolve'); - if ($sResCheck == '') - { - if (parent::ApplyStimulus('ev_resolve', false /* $bDoNotWrite */)) - { + if ($sResCheck == '') { + if (parent::ApplyStimulus('ev_resolve', false /* $bDoNotWrite */)) { // Update subnet and update public log $oSubnet = MetaModel::GetObject('IPSubnet', $this->Get('subnet_id'), false /* MustBeFound */); $sSubnet = (is_null($oSubnet)) ? '' : $oSubnet->Get('ip').' /'.$oSubnet->Get('mask'); @@ -61,52 +53,31 @@ public function AfterInsert() * @throws \ArchivedObjectException * @throws \CoreException */ - public function CheckStimulus($sStimulusCode) - { - if ($sStimulusCode == 'ev_resolve') - { + public function CheckStimulus($sStimulusCode) { + if ($sStimulusCode == 'ev_resolve') { // If subnet mask has changed, request agent to change it through manual tools $oSubnet = MetaModel::GetObject('IPSubnet', $this->Get('subnet_id'), false /* MustBeFound */); - if (is_null($oSubnet)) - { + if (is_null($oSubnet)) { return (Dict::Format('UI:IPManagement:Action:Implement:IPRequestSubnetUpdate:NoSuchSubnet', $this->Get('subnet_id'))); } } + return ''; } - + /** - * Apply stimulus to object - * - * @param $sStimulusCode - * @param bool $bDoNotWrite - * - * @return bool - * @throws \ArchivedObjectException - * @throws \CoreCannotSaveObjectException - * @throws \CoreException - * @throws \CoreUnexpectedValue - * @throws \DeleteException - * @throws \MissingQueryArgument - * @throws \MySQLException - * @throws \MySQLHasGoneAwayException - * @throws \OQLException + * @inheritdoc */ - public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) - { - if ($sStimulusCode != 'ev_resolve') - { + public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) { + if ($sStimulusCode != 'ev_resolve') { return parent::ApplyStimulus($sStimulusCode); + } elseif (parent::ApplyStimulus($sStimulusCode, false /* $bDoNotWrite */)) { + $oSubnet = MetaModel::GetObject('IPSubnet', $this->Get('subnet_id'), false /* MustBeFound */); + + return $this->UpdateSubnet($oSubnet); } - else - { - if (parent::ApplyStimulus($sStimulusCode, false /* $bDoNotWrite */)) - { - $oSubnet = MetaModel::GetObject('IPSubnet', $this->Get('subnet_id'), false /* MustBeFound */); - return $this->UpdateSubnet($oSubnet); - } - return false; - } + + return false; } /** @@ -116,23 +87,18 @@ public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false) * @throws \CoreException * @throws \CoreUnexpectedValue */ - private function UpdateSubnet($oSubnet) - { - if (!is_null($oSubnet)) - { + private function UpdateSubnet($oSubnet) { + if (!is_null($oSubnet)) { $sNewName = $this->Get('new_name'); - if ($sNewName != '') - { + if ($sNewName != '') { $oSubnet->Set('name', $sNewName); } $sNewStatusSubnet = $this->Get('new_status_subnet'); - if (($sNewStatusSubnet != '') && ($sNewStatusSubnet != $oSubnet->Get('status'))) - { + if (($sNewStatusSubnet != '') && ($sNewStatusSubnet != $oSubnet->Get('status'))) { $oSubnet->Set('status', $sNewStatusSubnet); } $sNewType = $this->Get('new_type'); - if ($sNewType != '') - { + if ($sNewType != '') { $oSubnet->Set('type', $sNewType); } $oSubnet->Set('requestor_id', $this->Get('caller_id')); @@ -141,25 +107,20 @@ private function UpdateSubnet($oSubnet) $iKey = $oSubnet->GetKey(); $iOldLocationId = $this->Get('old_location_id'); $iNewLocationId = $this->Get('new_location_id'); - if ($iNewLocationId != $iOldLocationId) - { - if (!$iOldLocationId <= 0) - { + if ($iNewLocationId != $iOldLocationId) { + if (!$iOldLocationId <= 0) { // A geography needs to be removed $oLocationLinkSet = new CMDBObjectSet(DBObjectSearch::FromOQL("SELECT lnkIPSubnetToLocation AS l WHERE l.location_id = $iOldLocationId AND l.ipsubnet_id = $iKey")); - while ($oLocationLink = $oLocationLinkSet->Fetch()) - { + while ($oLocationLink = $oLocationLinkSet->Fetch()) { $oLocationLink->DBDelete(); } } - if (!$iNewLocationId <= 0) - { + if (!$iNewLocationId <= 0) { // A new geography has been selected. // Create link if it doesn't already exist $oLocationLinkSet = new CMDBObjectSet(DBObjectSearch::FromOQL("SELECT lnkIPSubnetToLocation AS l WHERE l.location_id = $iNewLocationId AND l.ipsubnet_id = $iKey")); - if (!$oLocationLinkSet->CountExceeds(0)) - { + if (!$oLocationLinkSet->CountExceeds(0)) { $oNewLocationLink = MetaModel::NewObject('lnkIPSubnetToLocation'); $oNewLocationLink->Set('ipsubnet_id', $iKey); $oNewLocationLink->Set('location_id', $iNewLocationId);