12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568 |
- <?php
- // Copyright (C) 2010 Combodo SARL
- //
- // This program is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation; version 3 of the License.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program; if not, write to the Free Software
- // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- /**
- * Main page of iTop
- *
- * @author Erwan Taloc <erwan.taloc@combodo.com>
- * @author Romain Quetiez <romain.quetiez@combodo.com>
- * @author Denis Flaven <denis.flaven@combodo.com>
- * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
- */
- /**
- * Perform all the needed checks to delete one (or more) objects
- */
- function DeleteObjects(WebPage $oP, $sClass, $aObjects, $bDeleteConfirmed)
- {
- $bFoundManual = false;
- $bFoundStopper = false;
- $iTotalDelete = 0; // count of object that must be deleted
- $iTotalReset = 0; // count of object for which an ext key will be reset (to 0)
- $aTotalDeletedObjs = array();
- $aTotalResetedObjs = array();
- foreach($aObjects as $oObj)
- {
- // Evaluate the impact on the DB integrity
- //
- $aDeletedObjs = array();
- $aResetedObjs = array();
- $oObj->GetDeletionScheme($aDeletedObjs, $aResetedObjs);
- // Evaluate feasibility (user access control)
- //
- foreach ($aDeletedObjs as $sRemoteClass => $aDeletes)
- {
- $iTotalDelete += count($aDeletes);
- foreach ($aDeletes as $iId => $aData)
- {
- $oToDelete = $aData['to_delete'];
- $bDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_DELETE, DBObjectSet::FromObject($oToDelete));
- $aTotalDeletedObjs[$sRemoteClass][$iId]['auto_delete'] = $aData['auto_delete'];
- if (!$bDeleteAllowed)
- {
- $aTotalDeletedObjs[$sRemoteClass][$iId]['issue'] = Dict::S('UI:Delete:NotAllowedToDelete');
- $bFoundStopper = true;
- }
- else
- {
- $aTotalDeletedObjs[$sRemoteClass][$iId]['to_delete'] = $oToDelete;
- }
-
- $bAutoDel = $aData['auto_delete'];
- if (!$bAutoDel)
- {
- $bFoundManual = true;
- }
- }
- }
-
- foreach ($aResetedObjs as $sRemoteClass => $aToReset)
- {
- $iTotalReset += count($aToReset);
- foreach ($aToReset as $iId => $aData)
- {
- $oToReset = $aData['to_reset'];
- $aExtKeyLabels = array();
- $aForbiddenKeys = array(); // keys on which the current user is not allowed to write
- foreach ($aData['attributes'] as $sRemoteExtKey => $aRemoteAttDef)
- {
- $bUpdateAllowed = UserRights::IsActionAllowedOnAttribute($sClass, $sRemoteExtKey, UR_ACTION_MODIFY, DBObjectSet::FromObject($oToReset));
- if (!$bUpdateAllowed)
- {
- $bFoundStopper = true;
- $aForbiddenKeys[] = $aRemoteAttDef->GetLabel();
- }
- $aExtKeyLabels[] = $aRemoteAttDef->GetLabel();
- }
- $aResetedObjs[$sRemoteClass][$iId]['attributes_list'] = implode(', ', $aExtKeyLabels);
- $aTotalResetedObjs[$sRemoteClass][$iId]['attributes_list'] = $aResetedObjs[$sRemoteClass][$iId]['attributes_list'];
- $aTotalResetedObjs[$sRemoteClass][$iId]['attributes'] = $aResetedObjs[$sRemoteClass][$iId]['attributes'];
- if (count($aForbiddenKeys) > 0)
- {
- $aTotalResetedObjs[$sRemoteClass][$iId]['issue'] = Dict::Format('UI:Delete:NotAllowedToUpdate_Fields',implode(', ', $aForbiddenKeys));
- }
- else
- {
- $aTotalResetedObjs[$sRemoteClass][$iId]['to_reset'] = $oToReset;
- }
- }
- }
- // Count of dependent objects (+ the current one)
- $iTotalTargets = $iTotalDelete + $iTotalReset;
- }
-
- if ($bDeleteConfirmed)
- {
- if (count($aObjects) == 1)
- {
- $oObj = $aObjects[0];
- $oP->add("<h1>".Dict::Format('UI:Title:DeletionOf_Object', $oObj->GetName())."</h1>\n");
- }
- else
- {
- $oP->add("<h1>".Dict::Format('UI:Title:BulkDeletionOf_Count_ObjectsOf_Class', count($aObjects), MetaModel::GetName($sClass))."</h1>\n");
- }
- // Security - do not allow the user to force a forbidden delete by the mean of page arguments...
- if ($bFoundStopper)
- {
- throw new CoreException(Dict::S('UI:Error:NotEnoughRightsToDelete'));
- }
- if ($bFoundManual)
- {
- throw new CoreException(Dict::S('UI:Error:CannotDeleteBecauseOfDepencies'));
- }
- // Prepare the change reporting
- //
- $oMyChange = MetaModel::NewObject("CMDBChange");
- $oMyChange->Set("date", time());
- if (UserRights::IsImpersonated())
- {
- $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUser(), UserRights::GetUser());
- }
- else
- {
- $sUserString = UserRights::GetUser();
- }
- $oMyChange->Set("userinfo", $sUserString);
- $oMyChange->DBInsert();
- // Delete dependencies
- //
- $aDisplayData = array();
- foreach ($aTotalDeletedObjs as $sRemoteClass => $aDeletes)
- {
- foreach ($aDeletes as $iId => $aData)
- {
- $oToDelete = $aData['to_delete'];
- $aDisplayData[] = array(
- 'class' => MetaModel::GetName(get_class($oToDelete)),
- 'object' => $oToDelete->GetHyperLink(),
- 'consequence' => Dict::S('UI:Delete:AutomaticallyDeleted'),
- );
- $oToDelete->DBDeleteTracked($oMyChange);
- }
- }
-
- // Update dependencies
- //
- foreach ($aTotalResetedObjs as $sRemoteClass => $aToReset)
- {
- foreach ($aToReset as $iId => $aData)
- {
- $oToReset = $aData['to_reset'];
- $aDisplayData[] = array(
- 'class' => MetaModel::GetName(get_class($oToReset)),
- 'object' => $oToReset->GetHyperLink(),
- 'consequence' => Dict::Format('UI:Delete:AutomaticResetOf_Fields', $aData['attributes_list']),
- );
- foreach ($aData['attributes'] as $sRemoteExtKey => $aRemoteAttDef)
- {
- $oToReset->Set($sRemoteExtKey, 0);
- $oToReset->DBUpdateTracked($oMyChange);
- }
- }
- }
- // Report automatic jobs
- //
- if ($iTotalTargets > 0)
- {
- if (count($aObjects) == 1)
- {
- $oObj = $aObjects[0];
- $oP->p(Dict::Format('UI:Delete:CleaningUpRefencesTo_Object', $oObj->GetName()));
- }
- else
- {
- $oP->p(Dict::Format('UI:Delete:CleaningUpRefencesTo_Several_ObjectsOf_Class', count($aObjects), MetaModel::GetName($sClass)));
- }
- $aDisplayConfig = array();
- $aDisplayConfig['class'] = array('label' => 'Class', 'description' => '');
- $aDisplayConfig['object'] = array('label' => 'Object', 'description' => '');
- $aDisplayConfig['consequence'] = array('label' => 'Done', 'description' => Dict::S('UI:Delete:Done+'));
- $oP->table($aDisplayConfig, $aDisplayData);
- }
- foreach($aObjects as $oObj)
- {
- $sName = $oObj->GetName();
- $sClassLabel = MetaModel::GetName(get_class($oObj));
- $oObj->DBDeleteTracked($oMyChange);
- $oP->add("<h1>".Dict::Format('UI:Delete:_Name_Class_Deleted', $sName, $sClassLabel)."</h1>\n");
- }
- }
- else
- {
- if (count($aObjects) == 1)
- {
- $oObj = $aObjects[0];
- $oP->add("<h1>".Dict::Format('UI:Delete:ConfirmDeletionOf_Name', $oObj->GetName())."</h1>\n");
- }
- else
- {
- $oP->add("<h1>".Dict::Format('UI:Delete:ConfirmDeletionOf_Count_ObjectsOf_Class', count($aObjects), MetaModel::GetName($sClass))."</h1>\n");
- }
- // Explain what should be done
- //
- $aDisplayData = array();
- foreach ($aTotalDeletedObjs as $sRemoteClass => $aDeletes)
- {
- foreach ($aDeletes as $iId => $aData)
- {
- $oToDelete = $aData['to_delete'];
- $bAutoDel = $aData['auto_delete'];
- if (array_key_exists('issue', $aData))
- {
- if ($bAutoDel)
- {
- $sConsequence = Dict::S('UI:Delete:ShouldBeDeletedAtomaticallyButNotAllowed');
- }
- else
- {
- $sConsequence = Dict::S('UI:Delete:MustBeDeletedManuallyButNotAllowed');
- }
- }
- else
- {
- if ($bAutoDel)
- {
- $sConsequence = Dict::S('UI:Delete:WillBeDeletedAutomatically');
- }
- else
- {
- $sConsequence = Dict::S('UI:Delete:MustBeDeletedManually');
- }
- }
- $aDisplayData[] = array(
- 'class' => MetaModel::GetName(get_class($oToDelete)),
- 'object' => $oToDelete->GetHyperLink(),
- 'consequence' => $sConsequence,
- );
- }
- }
- foreach ($aTotalResetedObjs as $sRemoteClass => $aToReset)
- {
- foreach ($aToReset as $iId => $aData)
- {
- $oToReset = $aData['to_reset'];
- if (array_key_exists('issue', $aData))
- {
- $sConsequence = Dict::Format('UI:Delete:CannotUpdateBecause_Issue', $aData['issue']);
- }
- else
- {
- $sConsequence = Dict::Format('UI:Delete:WillAutomaticallyUpdate_Fields', $aData['attributes_list']);
- }
- $aDisplayData[] = array(
- 'class' => MetaModel::GetName(get_class($oToReset)),
- 'object' => $oToReset->GetHyperLink(),
- 'consequence' => $sConsequence,
- );
- }
- }
- if ($iTotalTargets > 0)
- {
- if (count($aObjects) == 1)
- {
- $oObj = $aObjects[0];
- $oP->p(Dict::Format('UI:Delete:Count_Objects/LinksReferencing_Object', $iTotalTargets, $oObj->GetName()));
- }
- else
- {
- $oP->p(Dict::Format('UI:Delete:Count_Objects/LinksReferencingTheObjects', $iTotalTargets));
- }
- $oP->p(Dict::S('UI:Delete:ReferencesMustBeDeletedToEnsureIntegrity'));
- $aDisplayConfig = array();
- $aDisplayConfig['class'] = array('label' => 'Class', 'description' => '');
- $aDisplayConfig['object'] = array('label' => 'Object', 'description' => '');
- $aDisplayConfig['consequence'] = array('label' => 'Consequence', 'description' => Dict::S('UI:Delete:Consequence+'));
- $oP->table($aDisplayConfig, $aDisplayData);
- }
- if ($iTotalTargets > 0 && ($bFoundManual || $bFoundStopper))
- {
- if ($bFoundStopper)
- {
- $oP->p(Dict::S('UI:Delete:SorryDeletionNotAllowed'));
- }
- elseif ($bFoundManual)
- {
- $oP->p(Dict::S('UI:Delete:PleaseDoTheManualOperations'));
- }
- $oP->add("<form method=\"post\">\n");
- $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::ReadParam('transaction_id')."\">\n");
- $oP->add("<input type=\"button\" onclick=\"window.history.back();\" value=\"".Dict::S('UI:Button:Back')."\">\n");
- $oP->add("<input DISABLED type=\"submit\" name=\"\" value=\"".Dict::S('UI:Button:Delete')."\">\n");
- $oP->add("</form>\n");
- }
- else
- {
- if (count($aObjects) == 1)
- {
- $oObj = $aObjects[0];
- $id = $oObj->GetKey();
- $oP->p('<h1>'.Dict::Format('UI:Delect:Confirm_Object', $oObj->GetHyperLink()).'</h1>');
- $oP->add("<form method=\"post\">\n");
- $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::ReadParam('transaction_id')."\">\n");
- $oP->add("<input type=\"hidden\" name=\"operation\" value=\"delete_confirmed\">\n");
- $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
- $oP->add("<input type=\"hidden\" name=\"id\" value=\"$id\">\n");
- $oP->add("<input type=\"button\" onclick=\"window.history.back();\" value=\"".Dict::S('UI:Button:Back')."\">\n");
- $oP->add("<input type=\"submit\" name=\"\" value=\"".Dict::S('UI:Button:Delete')."\">\n");
- $oP->add("</form>\n");
- }
- else
- {
- $oP->p('<h1>'.Dict::Format('UI:Delect:Confirm_Count_ObjectsOf_Class', count($aObjects), MetaModel::GetName($sClass)).'</h1>');
- $oSet = CMDBobjectSet::FromArray($sClass, $aObjects);
- CMDBAbstractObject::DisplaySet($oP, $oSet, array('display_limit' => false, 'menu' => false));
- $oP->add("<form method=\"post\">\n");
- $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::ReadParam('transaction_id')."\">\n");
- $oP->add("<input type=\"hidden\" name=\"operation\" value=\"bulk_delete_confirmed\">\n");
- $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
- foreach($aObjects as $oObj)
- {
- $oP->add("<input type=\"hidden\" name=\"selectObject[]\" value=\"".$oObj->GetKey()."\">\n");
- }
- $oP->add("<input type=\"button\" onclick=\"window.history.back();\" value=\"".Dict::S('UI:Button:Back')."\">\n");
- $oP->add("<input type=\"submit\" name=\"\" value=\"".Dict::S('UI:Button:Delete')."\">\n");
- $oP->add("</form>\n");
- }
- }
- }
- }
- /**
- * Updates an object from the POSTed parameters
- */
- function UpdateObject(&$oObj)
- {
- foreach(MetaModel::ListAttributeDefs(get_class($oObj)) as $sAttCode=>$oAttDef)
- {
- if ($oAttDef->IsLinkSet() && $oAttDef->IsIndirect())
- {
- $aLinks = utils::ReadPostedParam("attr_$sAttCode", '');
- $sLinkedClass = $oAttDef->GetLinkedClass();
- $sExtKeyToRemote = $oAttDef->GetExtKeyToRemote();
- $sExtKeyToMe = $oAttDef->GetExtKeyToMe();
- $oLinkedSet = DBObjectSet::FromScratch($sLinkedClass);
- if (is_array($aLinks))
- {
- foreach($aLinks as $id => $aData)
- {
- if (is_numeric($id))
- {
- if ($id < 0)
- {
- // New link to be created, the opposite of the id (-$id) is the ID of the remote object
- $oLink = MetaModel::NewObject($sLinkedClass);
- $oLink->Set($sExtKeyToRemote, -$id);
- $oLink->Set($sExtKeyToMe, $oObj->GetKey());
- }
- else
- {
- // Existing link, potentially to be updated...
- $oLink = MetaModel::GetObject($sLinkedClass, $id);
- }
- // Now populate the attributes
- foreach($aData as $sName => $value)
- {
- if (MetaModel::IsValidAttCode($sLinkedClass, $sName))
- {
- $oLinkAttDef = MetaModel::GetAttributeDef($sLinkedClass, $sName);
- if ($oLinkAttDef->IsWritable())
- {
- $oLink->Set($sName, $value);
- }
- }
- }
- $oLinkedSet->AddObject($oLink);
- }
- }
- }
- $oObj->Set($sAttCode, $oLinkedSet);
- }
- else if ($oAttDef->IsWritable())
- {
- $iFlags = $oObj->GetAttributeFlags($sAttCode);
- if ($iFlags & (OPT_ATT_HIDDEN | OPT_ATT_READONLY))
- {
- // Non-visible, or read-only attribute, do nothing
- }
- elseif ($oAttDef->GetEditClass() == 'Document')
- {
- // There should be an uploaded file with the named attr_<attCode>
- $oDocument = utils::ReadPostedDocument('file_'.$sAttCode);
- if (!$oDocument->IsEmpty())
- {
- // A new file has been uploaded
- $oObj->Set($sAttCode, $oDocument);
- }
- }
- elseif ($oAttDef->GetEditClass() == 'One Way Password')
- {
- // Check if the password was typed/changed
- $bChanged = utils::ReadPostedParam("attr_{$sAttCode}_changed", false);
- if ($bChanged)
- {
- // The password has been changed or set
- $rawValue = utils::ReadPostedParam("attr_$sAttCode", null);
- $oObj->Set($sAttCode, $rawValue);
- }
- }
- 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]);
- }
- }
- }
- }
- }
- }
- /**
- * Displays a popup welcome message, once per session at maximum
- * until the user unchecks the "Display welcome at startup"
- * @param WebPage $oP The current web page for the display
- * @return void
- */
- function DisplayWelcomePopup(WebPage $oP)
- {
- if (!isset($_SESSION['welcome']))
- {
- // Check, only once per session, if the popup should be displayed...
- // If the user did not already ask for hiding it forever
- $bPopup = appUserPreferences::GetPref('welcome_popup', true);
- if ($bPopup)
- {
- $sTemplate = @file_get_contents('../application/templates/welcome_popup.html');
- if ($sTemplate !== false)
- {
- $oTemplate = new DisplayTemplate($sTemplate);
- $oP->add("<div id=\"welcome_popup\">");
- $oTemplate->Render($oP, array());
- $oP->add("<p style=\"float:left\"><input type=\"checkbox\" checked id=\"display_welcome_popup\"/><label for=\"display_welcome_popup\"> ".Dict::S('UI:DisplayThisMessageAtStartup')."</label></p>\n");
- $oP->add("<p style=\"float:right\"><input type=\"button\" value=\"".Dict::S('UI:Button:Ok')."\" onClick=\"$('#welcome_popup').dialog('close');\"/>\n");
- $oP->add("</div>\n");
- $sTitle = addslashes(Dict::S('UI:WelcomeMenu:Title'));
- $oP->add_ready_script(
- <<<EOF
- $('#welcome_popup').dialog( { width:'80%', title: '$sTitle', autoOpen: true, modal:true,
- close: function() {
- var bDisplay = $('#display_welcome_popup:checked').length;
- SetUserPreference('welcome_popup', bDisplay, true);
- }
- });
- EOF
- );
- $_SESSION['welcome'] = 'ok';
- }
- }
- }
- }
- /***********************************************************************************
- *
- * Main user interface page, starts here
- *
- * ***********************************************************************************/
- try
- {
- require_once('../approot.inc.php');
- require_once(APPROOT.'/application/application.inc.php');
- require_once(APPROOT.'/application/itopwebpage.class.inc.php');
- require_once(APPROOT.'/application/wizardhelper.class.inc.php');
- require_once(APPROOT.'/application/startup.inc.php');
- $operation = utils::ReadParam('operation', '');
- $oKPI = new ExecutionKPI();
- require_once(APPROOT.'/application/loginwebpage.class.inc.php');
- LoginWebPage::DoLogin(); // Check user rights and prompt if needed
- $oAppContext = new ApplicationContext();
- $oKPI->ComputeAndReport('User login');
- $oP = new iTopWebPage(Dict::S('UI:WelcomeToITop'));
- switch($operation)
- {
- case 'details':
-
- $sClass = utils::ReadParam('class', '');
- $sClassLabel = MetaModel::GetName($sClass);
- $id = utils::ReadParam('id', '');
- $oSearch = new DBObjectSearch($sClass);
- $oBlock = new DisplayBlock($oSearch, 'search', false);
- $oBlock->Display($oP, 0);
- if ( empty($sClass) || empty($id)) // TO DO: check that the class name is valid !
- {
- throw new ApplicationException(Dict::Format('UI:Error:2ParametersMissing', 'class', 'id'));
- }
- $oObj = MetaModel::GetObject($sClass, $id, false /* MustBeFound */);
- if (is_null($oObj))
- {
- $oP->set_title(Dict::S('UI:ErrorPageTitle'));
- $oP->P(Dict::S('UI:ObjectDoesNotExist'));
- }
- else
- {
- // The object could be listed, check if it is actually allowed to view it
- $oSet = CMDBObjectSet::FromObject($oObj);
- if (UserRights::IsActionAllowed($sClass, UR_ACTION_READ, $oSet) == UR_ALLOWED_NO)
- {
- throw new SecurityException('User not allowed to view this object', array('class' => $sClass, 'id' => $id));
- }
- $oP->set_title(Dict::Format('UI:DetailsPageTitle', $oObj->GetName(), $sClassLabel));
- $oObj->DisplayDetails($oP);
- }
- break;
-
- case 'search_oql':
- $sOQLClass = utils::ReadParam('oql_class', '');
- $sOQLClause = utils::ReadParam('oql_clause', '');
- $sFormat = utils::ReadParam('format', '');
- $bSearchForm = utils::ReadParam('search_form', true);
- if (empty($sOQLClass))
- {
- throw new ApplicationException(Dict::Format('UI:Error:1ParametersMissing', 'oql_class'));
- }
- $oP->set_title(Dict::S('UI:SearchResultsPageTitle'));
- $sOQL = "SELECT $sOQLClass $sOQLClause";
- try
- {
- $oFilter = DBObjectSearch::FromOQL($sOQL); // To Do: Make sure we don't bypass security
- $oSet = new DBObjectSet($oFilter);
- if ($bSearchForm)
- {
- $oBlock = new DisplayBlock($oFilter, 'search', false);
- $oBlock->Display($oP, 0);
- }
- if (strtolower($sFormat) == 'csv')
- {
- $oBlock = new DisplayBlock($oFilter, 'csv', false);
- $oBlock->Display($oP, 'csv');
- $oPage->add_ready_script(" $('#csv').css('height', '95%');"); // adjust the size of the block
- }
- else
- {
- $oBlock = new DisplayBlock($oFilter, 'list', false);
- $oBlock->Display($oP, 1);
- }
- }
- catch(CoreException $e)
- {
- $oFilter = new DBObjectSearch($sOQLClass); // To Do: Make sure we don't bypass security
- $oSet = new DBObjectSet($oFilter);
- if ($bSearchForm)
- {
- $oBlock = new DisplayBlock($oFilter, 'search', false);
- $oBlock->Display($oP, 0);
- }
- $oP->P('<b>'.Dict::Format('UI:Error:IncorrectOQLQuery_Message', $e->getHtmlDesc()).'</b>');
- }
- catch(Exception $e)
- {
- $oP->P('<b>'.Dict::Format('UI:Error:AnErrorOccuredWhileRunningTheQuery_Message', $e->getMessage()).'</b>');
- }
- break;
-
- case 'search_form':
- $sClass = utils::ReadParam('class', '');
- $sFormat = utils::ReadParam('format', 'html');
- $bSearchForm = utils::ReadParam('search_form', true);
- if (empty($sClass))
- {
- throw new ApplicationException(Dict::Format('UI:Error:1ParametersMissing', 'class'));
- }
- $oP->set_title(Dict::S('UI:SearchResultsPageTitle'));
- $oFilter = new DBObjectSearch($sClass);
- $oSet = new DBObjectSet($oFilter);
- if ($bSearchForm)
- {
- $aParams = array('open' => true);
- $sBaseClass = utils::ReadParam('baseClass', '');
- if (!empty($sBaseClass))
- {
- $aParams['baseClass'] = $sBaseClass;
- }
- $oBlock = new DisplayBlock($oFilter, 'search', false /* Asynchronous */, $aParams);
- $oBlock->Display($oP, 0);
- }
- if (strtolower($sFormat) == 'csv')
- {
- $oBlock = new DisplayBlock($oFilter, 'csv', false);
- $oBlock->Display($oP, 1);
- $oP->add_ready_script(" $('#csv').css('height', '95%');"); // adjust the size of the block
- }
- else
- {
- $oBlock = new DisplayBlock($oFilter, 'list', false);
- $oBlock->Display($oP, 1);
- }
- break;
-
- case 'search':
- $sFilter = utils::ReadParam('filter', '');
- $sFormat = utils::ReadParam('format', '');
- $bSearchForm = utils::ReadParam('search_form', true);
- if (empty($sFilter))
- {
- throw new ApplicationException(Dict::Format('UI:Error:1ParametersMissing', 'filter'));
- }
- $oP->set_title(Dict::S('UI:SearchResultsPageTitle'));
- // TO DO: limit the search filter by the user context
- $oFilter = CMDBSearchFilter::unserialize($sFilter); // TO DO : check that the filter is valid
- $oSet = new DBObjectSet($oFilter);
- if ($bSearchForm)
- {
- $oBlock = new DisplayBlock($oFilter, 'search', false);
- $oBlock->Display($oP, 0);
- }
- if (strtolower($sFormat) == 'csv')
- {
- $oBlock = new DisplayBlock($oFilter, 'csv', false);
- $oBlock->Display($oP, 'csv');
- $oP->add_ready_script(" $('#csv').css('height', '95%');"); // adjust the size of the block
- }
- else
- {
- $oBlock = new DisplayBlock($oFilter, 'list', false);
- $oBlock->Display($oP, 1);
- }
- break;
-
- case 'full_text':
- $sFullText = trim(utils::ReadParam('text', ''));
- if (empty($sFullText))
- {
- $oP->p(Dict::S('UI:Search:NoSearch'));
- }
- else
- {
- $oP->set_title(Dict::S('UI:SearchResultsPageTitle'));
- $oP->p("<h1>".Dict::Format('UI:FullTextSearchTitle_Text', $sFullText)."</h1>");
- $iCount = 0;
- $iBlock = 0;
- // Search in full text mode in all the classes
- $aMatches = array();
- if (preg_match('/^"(.*)"$/', $sFullText, $aMatches))
- {
- // The text is surrounded by double-quotes, remove the quotes and treat it as one single expression
- $aFullTextNeedles = array($aMatches[1]);
- }
- else
- {
- // Split the text on the blanks and treat this as a search for <word1> AND <word2> AND <word3>
- $aFullTextNeedles = explode(' ', $sFullText);
- }
- foreach(MetaModel::GetClasses('searchable') as $sClassName)
- {
- $oFilter = new DBObjectSearch($sClassName);
- foreach($aFullTextNeedles as $sSearchText)
- {
- $oFilter->AddCondition_FullText($sSearchText);
- }
- $oSet = new DBObjectSet($oFilter);
- if ($oSet->Count() > 0)
- {
- $aLeafs = array();
- while($oObj = $oSet->Fetch())
- {
- if (get_class($oObj) == $sClassName)
- {
- $aLeafs[] = $oObj->GetKey();
- }
- }
- $oLeafsFilter = new DBObjectSearch($sClassName);
- if (count($aLeafs) > 0)
- {
- $iCount += count($aLeafs);
- $oP->add("<div class=\"page_header\">\n");
- $oP->add("<h2>".MetaModel::GetClassIcon($sClassName)." <span class=\"hilite\">".Dict::Format('UI:Search:Count_ObjectsOf_Class_Found', count($aLeafs), Metamodel::GetName($sClassName))."</h2>\n");
- $oP->add("</div>\n");
- $oLeafsFilter->AddCondition('id', $aLeafs, 'IN');
- $oBlock = new DisplayBlock($oLeafsFilter, 'list', false);
- $oBlock->Display($oP, $iBlock++);
- $oP->P(' '); // Some space ?
- }
- }
- }
- if ($iCount == 0)
- {
- $oP->p(Dict::S('UI:Search:NoObjectFound'));
- }
- }
- break;
-
- case 'modify':
- $oP->add_linked_script("../js/json.js");
- $oP->add_linked_script("../js/forms-json-utils.js");
- $oP->add_linked_script("../js/wizardhelper.js");
- $oP->add_linked_script("../js/wizard.utils.js");
- $oP->add_linked_script("../js/linkswidget.js");
- $oP->add_linked_script("../js/autocompletewidget.js");
- $oP->add_linked_script("../js/jquery.blockUI.js");
- $sClass = utils::ReadParam('class', '');
- $sClassLabel = MetaModel::GetName($sClass);
- $id = utils::ReadParam('id', '');
- if ( empty($sClass) || empty($id)) // TO DO: check that the class name is valid !
- {
- throw new ApplicationException(Dict::Format('UI:Error:2ParametersMissing', 'class', 'id'));
- }
- // Check if the user can modify this object
- $oObj = MetaModel::GetObject($sClass, $id, false /* MustBeFound */);
- if (is_null($oObj))
- {
- $oP->set_title(Dict::S('UI:ErrorPageTitle'));
- $oP->P(Dict::S('UI:ObjectDoesNotExist'));
- }
- else
- {
- // The object could be read - check if it is allowed to modify it
- $oSet = CMDBObjectSet::FromObject($oObj);
- if (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet) == UR_ALLOWED_NO)
- {
- throw new SecurityException('User not allowed to modify this object', array('class' => $sClass, 'id' => $id));
- }
- // Note: code duplicated to the case 'apply_modify' when a data integrity issue has been found
- $oP->set_title(Dict::Format('UI:ModificationPageTitle_Object_Class', $oObj->GetName(), $sClassLabel));
- $oP->add("<div class=\"page_header\">\n");
- $oP->add("<h1>".$oObj->GetIcon()." ".Dict::Format('UI:ModificationTitle_Class_Object', $sClassLabel, $oObj->GetName())."</h1>\n");
- $oP->add("</div>\n");
- $oP->add("<div class=\"wizContainer\">\n");
- $oObj->DisplayModifyForm($oP);
- $oP->add("</div>\n");
- }
- break;
-
- case 'new':
- $sClass = utils::ReadParam('class', '');
- $sStateCode = utils::ReadParam('state', '');
- $bCheckSubClass = utils::ReadParam('checkSubclass', true);
- if ( empty($sClass) )
- {
- throw new ApplicationException(Dict::Format('UI:Error:1ParametersMissing', 'class'));
- }
- // Note: code duplicated to the case 'apply_modify' when a data integrity issue has been found
- $oP->add_linked_script("../js/json.js");
- $oP->add_linked_script("../js/forms-json-utils.js");
- $oP->add_linked_script("../js/wizardhelper.js");
- $oP->add_linked_script("../js/wizard.utils.js");
- $oP->add_linked_script("../js/linkswidget.js");
- $oP->add_linked_script("../js/autocompletewidget.js");
- $oP->add_linked_script("../js/jquery.blockUI.js");
- $aArgs = utils::ReadParam('default', array());
- $aContext = $oAppContext->GetAsHash();
- foreach( $oAppContext->GetNames() as $key)
- {
- $aArgs[$key] = $oAppContext->GetCurrentValue($key);
- }
- // If the specified class has subclasses, ask the user an instance of which class to create
- $aSubClasses = MetaModel::EnumChildClasses($sClass, ENUM_CHILD_CLASSES_ALL); // Including the specified class itself
- $aPossibleClasses = array();
- $sRealClass = '';
- if ($bCheckSubClass)
- {
- foreach($aSubClasses as $sCandidateClass)
- {
- if (!MetaModel::IsAbstract($sCandidateClass) && (UserRights::IsActionAllowed($sCandidateClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES))
- {
- $aPossibleClasses[$sCandidateClass] = MetaModel::GetName($sCandidateClass);
- }
- }
- // Only one of the subclasses can be instantiated...
- if (count($aPossibleClasses) == 1)
- {
- $aKeys = array_keys($aPossibleClasses);
- $sRealClass = $aKeys[0];
- }
- }
- else
- {
- $sRealClass = $sClass;
- }
-
- if (!empty($sRealClass))
- {
- // Display the creation form
- $sClassLabel = MetaModel::GetName($sRealClass);
- // Note: some code has been duplicated to the case 'apply_new' when a data integrity issue has been found
- $oP->set_title(Dict::Format('UI:CreationPageTitle_Class', $sClassLabel));
- $oP->add("<h1>".MetaModel::GetClassIcon($sRealClass)." ".Dict::Format('UI:CreationTitle_Class', $sClassLabel)."</h1>\n");
- $oP->add("<div class=\"wizContainer\">\n");
- $aDefaults = utils::ReadParam('default', array());
- $aContext = $oAppContext->GetAsHash();
- foreach( $oAppContext->GetNames() as $key)
- {
- $aDefaults[$key] = $oAppContext->GetCurrentValue($key);
- }
- // Set all the default values in an object and clone this "default" object
- $oObjToClone = MetaModel::NewObject($sRealClass);
- foreach($aDefaults as $sName => $value)
- {
- if (MetaModel::IsValidAttCode($sRealClass, $sName))
- {
- $oAttDef = MetaModel::GetAttributeDef($sRealClass, $sName);
- if ($oAttDef->IsWritable())
- {
- $oObjToClone->Set($sName, $value);
- }
- }
- }
- cmdbAbstractObject::DisplayCreationForm($oP, $sRealClass, $oObjToClone, array('default' => $aDefaults));
- $oP->add("</div>\n");
- }
- else
- {
- // Select the derived class to create
- $sClassLabel = MetaModel::GetName($sClass);
- $oP->add("<h1>".MetaModel::GetClassIcon($sClass)." ".Dict::Format('UI:CreationTitle_Class', $sClassLabel)."</h1>\n");
- $oP->add("<div class=\"wizContainer\">\n");
- $oP->add('<form>');
- $oP->add('<p>'.Dict::Format('UI:SelectTheTypeOf_Class_ToCreate', $sClassLabel));
- $aDefaults = utils::ReadParam('default', array());
- $oP->add($oAppContext->GetForForm());
- $oP->add("<input type=\"hidden\" name=\"checkSubclass\" value=\"0\">\n");
- $oP->add("<input type=\"hidden\" name=\"state\" value=\"$sStateCode\">\n");
- $oP->add("<input type=\"hidden\" name=\"operation\" value=\"new\">\n");
- foreach($aDefaults as $key => $value)
- {
- $oP->add("<input type=\"hidden\" name=\"default[$key]\" value=\"$value\">\n");
- $aDefaults[$key] = $value;
- }
- $oP->add('<select name="class">');
- asort($aPossibleClasses);
- foreach($aPossibleClasses as $sClassName => $sClassLabel)
- {
- $sSelected = ($sClassName == $sClass) ? 'selected' : '';
- $oP->add("<option $sSelected value=\"$sClassName\">$sClassLabel</option>");
- }
- $oP->add('</select>');
- $oP->add(" <input type=\"submit\" value=\"".Dict::S('UI:Button:Apply')."\"></p>");
- $oP->add('</form>');
- $oP->add("</div>\n");
- }
- break;
-
- case 'apply_modify':
- $sClass = utils::ReadPostedParam('class', '');
- $sClassLabel = MetaModel::GetName($sClass);
- $id = utils::ReadPostedParam('id', '');
- $sTransactionId = utils::ReadPostedParam('transaction_id', '');
- if ( empty($sClass) || empty($id)) // TO DO: check that the class name is valid !
- {
- throw new ApplicationException(Dict::Format('UI:Error:2ParametersMissing', 'class', 'id'));
- }
- $bDisplayDetails = true;
- $oObj = MetaModel::GetObject($sClass, $id, false);
- if ($oObj == null)
- {
- $bDisplayDetails = false;
- $oP->set_title(Dict::S('UI:ErrorPageTitle'));
- $oP->P(Dict::S('UI:ObjectDoesNotExist'));
- }
- elseif (!utils::IsTransactionValid($sTransactionId, false))
- {
- $oP->set_title(Dict::Format('UI:ModificationPageTitle_Object_Class', $oObj->GetName(), $sClassLabel));
- $oP->p("<strong>".Dict::S('UI:Error:ObjectAlreadyUpdated')."</strong>\n");
- }
- else
- {
- UpdateObject($oObj);
- if (!$oObj->IsModified())
- {
- $oP->set_title(Dict::Format('UI:ModificationPageTitle_Object_Class', $oObj->GetName(), $sClassLabel));
- $oP->p(Dict::Format('UI:Class_Object_NotUpdated', MetaModel::GetName(get_class($oObj)), $oObj->GetName()));
- }
- else
- {
- list($bRes, $aIssues) = $oObj->CheckToWrite();
- if ($bRes)
- {
- $oP->set_title(Dict::Format('UI:ModificationPageTitle_Object_Class', $oObj->GetName(), $sClassLabel));
- $oP->add("<h1>".Dict::Format('UI:ModificationTitle_Class_Object', $sClassLabel, $oObj->GetName())."</h1>\n");
- $oMyChange = MetaModel::NewObject("CMDBChange");
- $oMyChange->Set("date", time());
- if (UserRights::IsImpersonated())
- {
- $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUser(), UserRights::GetUser());
- }
- else
- {
- $sUserString = UserRights::GetUser();
- }
- $oMyChange->Set("userinfo", $sUserString);
- $iChangeId = $oMyChange->DBInsert();
- $oObj->DBUpdateTracked($oMyChange);
- utils::RemoveTransaction($sTransactionId);
-
- $oP->p(Dict::Format('UI:Class_Object_Updated', MetaModel::GetName(get_class($oObj)), $oObj->GetName()));
- }
- else
- {
- $bDisplayDetails = false;
- // Found issues, explain and give the user a second chance
- //
- // Note: code duplicated from the case 'modify'
- $oP->add_linked_script("../js/json.js");
- $oP->add_linked_script("../js/forms-json-utils.js");
- $oP->add_linked_script("../js/wizardhelper.js");
- $oP->add_linked_script("../js/wizard.utils.js");
- $oP->add_linked_script("../js/linkswidget.js");
- $oP->add_linked_script("../js/autocompletewidget.js");
- $oP->add_linked_script("../js/jquery.blockUI.js");
- $oP->set_title(Dict::Format('UI:ModificationPageTitle_Object_Class', $oObj->GetName(), $sClassLabel));
- $oP->add("<div class=\"page_header\">\n");
- $oP->add("<h1>".$oObj->GetIcon()." ".Dict::Format('UI:ModificationTitle_Class_Object', $sClassLabel, $oObj->GetName())."</h1>\n");
- $oP->add("</div>\n");
- $oP->add("<div class=\"wizContainer\">\n");
- $oObj->DisplayModifyForm($oP);
- $oP->add("</div>\n");
- $sIssueDesc = Dict::Format('UI:ObjectCouldNotBeWritten', implode(', ', $aIssues));
- $oP->add_ready_script("alert('".addslashes($sIssueDesc)."');");
- }
- }
- }
- if ($bDisplayDetails)
- {
- $oObj = MetaModel::GetObject(get_class($oObj), $oObj->GetKey()); //Workaround: reload the object so that the linkedset are displayed properly
- $oObj->DisplayDetails($oP);
- }
- break;
- case 'select_for_deletion':
- $sFilter = utils::ReadParam('filter', '');
- $sFormat = utils::ReadParam('format', '');
- if (empty($sFilter))
- {
- throw new ApplicationException(Dict::Format('UI:Error:1ParametersMissing', 'filter'));
- }
- $oP->set_title(Dict::S('UI:BulkDeletePageTitle'));
- $oP->add("<h1>".Dict::S('UI:BulkDeleteTitle')."</h1>\n");
- // TO DO: limit the search filter by the user context
- $oFilter = CMDBSearchFilter::unserialize($sFilter); // TO DO : check that the filter is valid
- $oSet = new DBObjectSet($oFilter);
- $oBlock = new DisplayBlock($oFilter, 'list', false);
- $oP->add("<form method=\"post\">\n");
- $oP->add("<input type=\"hidden\" name=\"operation\" value=\"bulk_delete\">\n");
- $oP->add("<input type=\"hidden\" name=\"class\" value=\"".$oFilter->GetClass()."\">\n");
- $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
- $oBlock->Display($oP, 1, array('selection_type' => 'multiple', 'selection_mode' => true, 'display_limit' => false, 'menu' => false));
- $oP->add("<input type=\"button\" value=\"".Dict::S('UI:Button:Cancel')."\" onClick=\"window.history.back()\"> <input type=\"submit\" value=\"".Dict::S('UI:Button:Next')."\">\n");
- $oP->add("</form>\n");
- break;
-
- case 'bulk_delete_confirmed':
- $sTransactionId = utils::ReadPostedParam('transaction_id', '');
- if (!utils::IsTransactionValid($sTransactionId))
- {
- throw new ApplicationException(Dict::S('UI:Error:ObjectsAlreadyDeleted'));
- }
- case 'bulk_delete':
- $sClass = utils::ReadPostedParam('class', '');
- $sClassLabel = MetaModel::GetName($sClass);
- $aSelectObject = utils::ReadPostedParam('selectObject', '');
- $aObjects = array();
- if ( empty($sClass) || empty($aSelectObject)) // TO DO: check that the class name is valid !
- {
- throw new ApplicationException(Dict::Format('UI:Error:2ParametersMissing', 'class', 'selectObject[]'));
- }
- foreach($aSelectObject as $iId)
- {
- $aObjects[] = MetaModel::GetObject($sClass, $iId);
- }
- if (!UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_DELETE, DBObjectSet::FromArray($sClass, $aObjects)))
- {
- throw new SecurityException(Dict::Format('UI:Error:BulkDeleteNotAllowedOn_Class', $sClass));
- }
- $oP->set_title(Dict::S('UI:BulkDeletePageTitle'));
- DeleteObjects($oP, $sClass, $aObjects, ($operation == 'bulk_delete_confirmed'));
- break;
-
- case 'delete':
- case 'delete_confirmed':
- $sClass = utils::ReadParam('class', '');
- $sClassLabel = MetaModel::GetName($sClass);
- $id = utils::ReadParam('id', '');
- $oObj = MetaModel::GetObject($sClass, $id);
-
- if (!UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, DBObjectSet::FromObject($oObj)))
- {
- throw new SecurityException(Dict::Format('UI:Error:DeleteNotAllowedOn_Class', $sClass));
- }
- DeleteObjects($oP, $sClass, array($oObj), ($operation == 'delete_confirmed'));
- break;
-
- case 'apply_new':
- $sClass = utils::ReadPostedParam('class', '');
- $sClassLabel = MetaModel::GetName($sClass);
- $sTransactionId = utils::ReadPostedParam('transaction_id', '');
- if ( empty($sClass) ) // TO DO: check that the class name is valid !
- {
- throw new ApplicationException(Dict::Format('UI:Error:1ParametersMissing', 'class'));
- }
- if (!utils::IsTransactionValid($sTransactionId, false))
- {
- $oP->p("<strong>".Dict::S('UI:Error:ObjectAlreadyCreated')."</strong>\n");
- }
- else
- {
- $oObj = MetaModel::NewObject($sClass);
- UpdateObject($oObj);
- }
- if (isset($oObj) && is_object($oObj))
- {
- $sClass = get_class($oObj);
- $sClassLabel = MetaModel::GetName($sClass);
- list($bRes, $aIssues) = $oObj->CheckToWrite();
- if ($bRes)
- {
- $oMyChange = MetaModel::NewObject("CMDBChange");
- $oMyChange->Set("date", time());
- if (UserRights::IsImpersonated())
- {
- $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUser(), UserRights::GetUser());
- }
- else
- {
- $sUserString = UserRights::GetUser();
- }
- $oMyChange->Set("userinfo", $sUserString);
- $iChangeId = $oMyChange->DBInsert();
- $oObj->DBInsertTracked($oMyChange);
- utils::RemoveTransaction($sTransactionId);
- $oP->set_title(Dict::S('UI:PageTitle:ObjectCreated'));
- $oP->add("<h1>".Dict::Format('UI:Title:Object_Of_Class_Created', $oObj->GetName(), $sClassLabel)."</h1>\n");
- $oObj->DisplayDetails($oP);
- }
- else
- {
- // Found issues, explain and give the user a second chance
- //
- // Note: code similar to the case 'modify'
- $oP->add_linked_script("../js/json.js");
- $oP->add_linked_script("../js/forms-json-utils.js");
- $oP->add_linked_script("../js/wizardhelper.js");
- $oP->add_linked_script("../js/wizard.utils.js");
- $oP->add_linked_script("../js/linkswidget.js");
- $oP->add_linked_script("../js/autocompletewidget.js");
- $oP->add_linked_script("../js/jquery.blockUI.js");
- $oP->set_title(Dict::Format('UI:CreationPageTitle_Class', $sClassLabel));
- $oP->add("<h1>".MetaModel::GetClassIcon($sClass)." ".Dict::Format('UI:CreationTitle_Class', $sClassLabel)."</h1>\n");
- $oP->add("<div class=\"wizContainer\">\n");
- cmdbAbstractObject::DisplayCreationForm($oP, $sClass, $oObj);
- $oP->add("</div>\n");
- $sIssueDesc = Dict::Format('UI:ObjectCouldNotBeWritten', implode(', ', $aIssues));
- $oP->add_ready_script("alert('".addslashes($sIssueDesc)."');");
- }
- }
- break;
-
- case 'wizard_apply_new':
- $sJson = utils::ReadPostedParam('json_obj', '');
- $oWizardHelper = WizardHelper::FromJSON($sJson);
- $sTransactionId = utils::ReadPostedParam('transaction_id', '');
- if (!utils::IsTransactionValid($sTransactionId))
- {
- $oP->p(Dict::S('UI:Error:ObjectAlreadyCreated'));
- }
- else
- {
- $oObj = $oWizardHelper->GetTargetObject(true /* read uploaded files */);
- if (is_object($oObj))
- {
- $sClass = get_class($oObj);
- $sClassLabel = MetaModel::GetName($sClass);
- $oMyChange = MetaModel::NewObject("CMDBChange");
- $oMyChange->Set("date", time());
- if (UserRights::IsImpersonated())
- {
- $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUser(), UserRights::GetUser());
- }
- else
- {
- $sUserString = UserRights::GetUser();
- }
- $oMyChange->Set("userinfo", $sUserString);
- $iChangeId = $oMyChange->DBInsert();
- $oObj->DBInsertTracked($oMyChange);
- $oP->set_title(Dict::S('UI:PageTitle:ObjectCreated'));
- $oP->add("<h1>".Dict::Format('UI:Title:Object_Of_Class_Created', $oObj->GetName(), $sClassLabel)."</h1>\n");
- $oObj->DisplayDetails($oP);
- }
- }
- break;
-
- case 'stimulus':
- $sClass = utils::ReadParam('class', '');
- $id = utils::ReadParam('id', '');
- $sStimulus = utils::ReadParam('stimulus', '');
- if ( empty($sClass) || empty($id) || empty($sStimulus) ) // TO DO: check that the class name is valid !
- {
- throw new ApplicationException(Dict::Format('UI:Error:3ParametersMissing', 'class', 'id', 'stimulus'));
- }
- $oObj = MetaModel::GetObject($sClass, $id, false);
- if ($oObj != null)
- {
- $aTransitions = $oObj->EnumTransitions();
- $aStimuli = MetaModel::EnumStimuli($sClass);
- if (!isset($aTransitions[$sStimulus]))
- {
- // Invalid stimulus
- throw new ApplicationException(Dict::Format('UI:Error:Invalid_Stimulus_On_Object_In_State', $sStimulus, $oObj->GetName(), $oObj->GetStateLabel()));
- }
- $sActionLabel = $aStimuli[$sStimulus]->GetLabel();
- $sActionDetails = $aStimuli[$sStimulus]->GetDescription();
- $aTransition = $aTransitions[$sStimulus];
- $sTargetState = $aTransition['target_state'];
- $aTargetStates = MetaModel::EnumStates($sClass);
- $oP->add_linked_script("../js/json.js");
- $oP->add_linked_script("../js/forms-json-utils.js");
- $oP->add_linked_script("../js/wizardhelper.js");
- $oP->add_linked_script("../js/wizard.utils.js");
- $oP->add_linked_script("../js/linkswidget.js");
- $oP->add_linked_script("../js/autocompletewidget.js");
- $oP->add_linked_script("../js/jquery.blockUI.js");
- $oP->add("<div class=\"page_header\">\n");
- $oP->add("<h1>$sActionLabel - <span class=\"hilite\">{$oObj->GetName()}</span></h1>\n");
- $oP->set_title($sActionLabel);
- $oP->add("</div>\n");
- $oObj->DisplayBareProperties($oP);
- $aTargetState = $aTargetStates[$sTargetState];
- $aExpectedAttributes = $aTargetState['attribute_list'];
- $oP->add("<h1>$sActionDetails</h1>\n");
- $oP->add("<div class=\"wizContainer\">\n");
- $oP->add("<form id=\"apply_stimulus\" method=\"post\" onSubmit=\"return CheckFields('apply_stimulus', true);\">\n");
- $aDetails = array();
- $iFieldIndex = 0;
- $aFieldsMap = array();
- foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
- {
- // Prompt for an attribute if
- // - the attribute must be changed or must be displayed to the user for confirmation
- // - or the field is mandatory and currently empty
- if ( ($iExpectCode & (OPT_ATT_MUSTCHANGE | OPT_ATT_MUSTPROMPT)) ||
- (($iExpectCode & OPT_ATT_MANDATORY) && ($oObj->Get($sAttCode) == '')) )
- {
- $aAttributesDef = MetaModel::ListAttributeDefs($sClass);
- $oAttDef = $aAttributesDef[$sAttCode];
- $aArgs = array('this' => $oObj);
- $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $oObj->Get($sAttCode), $oObj->GetEditValue($sAttCode), 'att_'.$iFieldIndex, '', $iExpectCode, $aArgs);
- $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => "<span id=\"field_att_$iFieldIndex\">$sHTMLValue</span>");
- $aFieldsMap[$sAttCode] = 'att_'.$iFieldIndex;
- $iFieldIndex++;
- }
- }
- $oP->details($aDetails);
- $oP->add("<input type=\"hidden\" name=\"id\" value=\"$id\" id=\"id\">\n");
- $aFieldsMap['id'] = 'id';
- $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
- $oP->add("<input type=\"hidden\" name=\"operation\" value=\"apply_stimulus\">\n");
- $oP->add("<input type=\"hidden\" name=\"stimulus\" value=\"$sStimulus\">\n");
- $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
- $oP->add($oAppContext->GetForForm());
- $oP->add("<button type=\"button\" class=\"action\" onClick=\"BackToDetails('$sClass', $id)\"><span>".Dict::S('UI:Button:Cancel')."</span></button> \n");
- $oP->add("<button type=\"submit\" class=\"action\"><span>$sActionLabel</span></button>\n");
- $oP->add("</form>\n");
- $oP->add("</div>\n");
- $iFieldsCount = count($aFieldsMap);
- $sJsonFieldsMap = json_encode($aFieldsMap);
-
- $oP->add_script(
- <<<EOF
- // Initializes the object once at the beginning of the page...
- var oWizardHelper = new WizardHelper('$sClass');
- oWizardHelper.SetFieldsMap($sJsonFieldsMap);
- oWizardHelper.SetFieldsCount($iFieldsCount);
- EOF
- );
- $oP->add_ready_script(
- <<<EOF
- // Starts the validation when the page is ready
- CheckFields('apply_stimulus', false);
- EOF
- );
- }
- else
- {
- $oP->set_title(Dict::S('UI:ErrorPageTitle'));
- $oP->P(Dict::S('UI:ObjectDoesNotExist'));
- }
- break;
- case 'apply_stimulus':
- $sClass = utils::ReadPostedParam('class', '');
- $id = utils::ReadPostedParam('id', '');
- $sTransactionId = utils::ReadPostedParam('transaction_id', '');
- $sStimulus = utils::ReadPostedParam('stimulus', '');
- if ( empty($sClass) || empty($id) || empty($sStimulus) ) // TO DO: check that the class name is valid !
- {
- throw new ApplicationException(Dict::Format('UI:Error:3ParametersMissing', 'class', 'id', 'stimulus'));
- }
- $oObj = MetaModel::GetObject($sClass, $id, false);
- if ($oObj != null)
- {
- $aTransitions = $oObj->EnumTransitions();
- $aStimuli = MetaModel::EnumStimuli($sClass);
- if (!isset($aTransitions[$sStimulus]))
- {
- throw new ApplicationException(Dict::Format('UI:Error:Invalid_Stimulus_On_Object_In_State', $sStimulus, $oObj->GetName(), $oObj->GetStateLabel()));
- }
- if (!utils::IsTransactionValid($sTransactionId))
- {
- $oP->p(Dict::S('UI:Error:ObjectAlreadyUpdated'));
- }
- else
- {
- $sActionLabel = $aStimuli[$sStimulus]->GetLabel();
- $sActionDetails = $aStimuli[$sStimulus]->GetDescription();
- $aTransition = $aTransitions[$sStimulus];
- $sTargetState = $aTransition['target_state'];
- $aTargetStates = MetaModel::EnumStates($sClass);
- //$oP->add("<div class=\"page_header\">\n");
- //$oP->add("<h1>$sActionLabel - <span class=\"hilite\">{$oObj->GetName()}</span></h1>\n");
- //$oP->add("<p>$sActionDetails</p>\n");
- //$oP->p(Dict::Format('UI:Apply_Stimulus_On_Object_In_State_ToTarget_State', $sActionLabel, $oObj->GetName(), $oObj->GetStateLabel(), $sTargetState));
- //$oP->add("</div>\n");
- $aTargetState = $aTargetStates[$sTargetState];
- $aExpectedAttributes = $aTargetState['attribute_list'];
- $aDetails = array();
- foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
- {
- if (($iExpectCode & (OPT_ATT_MUSTCHANGE|OPT_ATT_MUSTPROMPT)) || ($oObj->Get($sAttCode) == '') )
- {
- $paramValue = utils::ReadPostedParam("attr_$sAttCode", '');
- $oObj->Set($sAttCode, $paramValue);
- }
- }
- if ($oObj->ApplyStimulus($sStimulus))
- {
- $oMyChange = MetaModel::NewObject("CMDBChange");
- $oMyChange->Set("date", time());
- if (UserRights::IsImpersonated())
- {
- $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUser(), UserRights::GetUser());
- }
- else
- {
- $sUserString = UserRights::GetUser();
- }
- $oMyChange->Set("userinfo", $sUserString);
- $iChangeId = $oMyChange->DBInsert();
- $oObj->DBUpdateTracked($oMyChange);
- $oP->p(Dict::Format('UI:Class_Object_Updated', MetaModel::GetName(get_class($oObj)), $oObj->GetName()));
- }
- }
- $oObj->DisplayDetails($oP);
- }
- else
- {
- $oP->set_title(Dict::S('UI:ErrorPageTitle'));
- $oP->P(Dict::S('UI:ObjectDoesNotExist'));
- }
- break;
- case 'modify_links':
- $sClass = utils::ReadParam('class', '');
- $sLinkAttr = utils::ReadParam('link_attr', '');
- $sTargetClass = utils::ReadParam('target_class', '');
- $id = utils::ReadParam('id', '');
- $bAddObjects = utils::ReadParam('addObjects', false);
- if ( empty($sClass) || empty($id) || empty($sLinkAttr) || empty($sTargetClass)) // TO DO: check that the class name is valid !
- {
- throw new ApplicationException(Dict::Format('UI:Error:4ParametersMissing', 'class', 'id', 'target_class', 'link_attr'));
- }
- require_once(APPROOT.'/application/uilinkswizard.class.inc.php');
- $oWizard = new UILinksWizard($sClass, $sLinkAttr, $id, $sTargetClass);
- $oWizard->Display($oP, array('StartWithAdd' => $bAddObjects));
- break;
-
- case 'do_modify_links':
- $aLinks = utils::ReadPostedParam('linkId', array());
- $sLinksToRemove = trim(utils::ReadPostedParam('linksToRemove', ''));
- $aLinksToRemove = array();
- if (!empty($sLinksToRemove))
- {
- $aLinksToRemove = explode(' ', trim($sLinksToRemove));
- }
- $sClass = utils::ReadPostedParam('class', '');
- $sLinkageAtt = utils::ReadPostedParam('linkage', '');
- $iObjectId = utils::ReadPostedParam('object_id', '');
- $sLinkingAttCode = utils::ReadPostedParam('linking_attcode', '');
- $oMyChange = MetaModel::NewObject("CMDBChange");
- $oMyChange->Set("date", time());
- if (UserRights::IsImpersonated())
- {
- $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUser(), UserRights::GetUser());
- }
- else
- {
- $sUserString = UserRights::GetUser();
- }
- $oMyChange->Set("userinfo", $sUserString);
- $iChangeId = $oMyChange->DBInsert();
-
- // Delete links that are to be deleted
- foreach($aLinksToRemove as $iLinkId)
- {
- if ($iLinkId > 0) // Negative IDs are objects that were not even created
- {
- $oLink = MetaModel::GetObject($sClass, $iLinkId);
- $oLink->DBDeleteTracked($oMyChange);
- }
- }
-
- $aEditableFields = array();
- $aData = array();
- foreach(MetaModel::GetAttributesList($sClass) as $sAttCode)
- {
- $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
- if ( (!$oAttDef->IsExternalKey()) && (!$oAttDef->IsExternalField()))
- {
- $aEditableFields[] = $sAttCode;
- $aData[$sAttCode] = utils::ReadParam('attr_'.$sAttCode, array(), 'post');
- }
- }
-
- // Update existing links or create new links
- foreach($aLinks as $iLinkId)
- {
- if ($iLinkId > 0)
- {
- // This is an existing link to be modified
- $oLink = MetaModel::GetObject($sClass, $iLinkId);
-
- // Update all the attributes of the link
- foreach($aEditableFields as $sAttCode)
- {
- $value = $aData[$sAttCode][$iLinkId];
- $oLink->Set($sAttCode, $value);
- }
- if ($oLink->IsModified())
- {
- $oLink->DBUpdateTracked($oMyChange);
- }
- //echo "Updated link:<br/>\n";
- //var_dump($oLink);
- }
- else
- {
- // A new link must be created
- $oLink = MetaModel::NewObject($sClass);
- $oLinkedObjectId = -$iLinkId;
- // Set all the attributes of the link
- foreach($aEditableFields as $sAttCode)
- {
- $value = $aData[$sAttCode][$iLinkId];
- $oLink->Set($sAttCode, $value);
- }
- // And the two external keys
- $oLink->Set($sLinkageAtt, $iObjectId);
- $oLink->Set($sLinkingAttCode, $oLinkedObjectId);
- // then save it
- //echo "Created link:<br/>\n";
- //var_dump($oLink);
- $oLink->DBInsertTracked($oMyChange);
- }
- }
- // Display again the details of the linked object
- $oAttDef = MetaModel::GetAttributeDef($sClass, $sLinkageAtt);
- $sTargetClass = $oAttDef->GetTargetClass();
- $oObj = MetaModel::GetObject($sTargetClass, $iObjectId);
-
- $oSearch = new DBObjectSearch(get_class($oObj));
- $oBlock = new DisplayBlock($oSearch, 'search', false);
- $oBlock->Display($oP, 0);
- $oObj->DisplayDetails($oP);
- break;
-
- case 'swf_navigator':
- $sClass = utils::ReadParam('class', '');
- $id = utils::ReadParam('id', 0);
- $sRelation = utils::ReadParam('relation', 'impact');
-
- $oP->AddTabContainer('Navigator');
- $oP->SetCurrentTabContainer('Navigator');
- $oP->SetCurrentTab(Dict::S('UI:RelationshipGraph'));
- $width = 1000;
- $height = 700;
- $sParams = "pWidth=$width&pHeight=$height&drillUrl=".urlencode('../pages/UI.php?operation=details')."&displayController=false&xmlUrl=".urlencode("./xml.navigator.php")."&obj_class=$sClass&obj_id=$id&relation=$sRelation";
-
- $oP->add("<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"$width\" height=\"$height\" id=\"navigator\" align=\"middle\">
- <param name=\"allowScriptAccess\" value=\"sameDomain\" />
- <param name=\"allowFullScreen\" value=\"false\" />
- <param name=\"FlashVars\" value=\"$sParams\" />
- <param name=\"movie\" value=\"../navigator/navigator.swf\" /><param name=\"quality\" value=\"high\" /><param name=\"bgcolor\" value=\"#ffffff\" />
- <embed src=\"../navigator/navigator.swf\" flashVars=\"$sParams\" quality=\"high\" bgcolor=\"#ffffff\" width=\"$width\" height=\"$height\" name=\"navigator\" align=\"middle\" allowScriptAccess=\"sameDomain\" allowFullScreen=\"false\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.adobe.com/go/getflashplayer\" />
- </object>\n");
- $oP->SetCurrentTab(Dict::S('UI:RelationshipList'));
- $oP->add("<div id=\"impacted_objects\" style=\"width:100%;background-color:#fff;padding:10px;\"><p style=\"height:150px;\"> </p></div>");
- $oP->add_ready_script(
- <<<EOF
- var ajax_request = null;
-
- function UpdateImpactedObjects(sClass, iId, sRelation)
- {
- var class_name = sClass; //$('select[name=class_name]').val();
- if (class_name != '')
- {
- $('#impacted_objects').block();
-
- // Make sure that we cancel any pending request before issuing another
- // since responses may arrive in arbitrary order
- if (ajax_request != null)
- {
- ajax_request.abort();
- ajax_request = null;
- }
-
- ajax_request = $.get('xml.navigator.php', { 'class': sClass, id: iId, relation: sRelation, format: 'html' },
- function(data)
- {
- $('#impacted_objects').empty();
- $('#impacted_objects').append(data);
- $('#impacted_objects').unblock();
- $('#impacted_objects .listResults').tablesorter( { widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
- $('#impacted_objects table.listResults').tableHover(); // hover tables
- }
- );
- }
- }
-
- UpdateImpactedObjects('$sClass', $id, '$sRelation');
- EOF
- );
- $oP->SetCurrentTab('');
- break;
-
- default:
- $oMenuNode = ApplicationMenu::GetMenuNode(ApplicationMenu::GetActiveNodeId());
- if (is_object($oMenuNode))
- {
-
- $oMenuNode->RenderContent($oP, $oAppContext->GetAsHash());
- $oP->set_title($oMenuNode->GetLabel());
- }
- }
- $oKPI->ComputeAndReport('GUI creation before output');
- ExecutionKPI::ReportStats();
- MetaModel::ShowQueryTrace();
- DisplayWelcomePopup($oP);
- $oP->output();
- }
- catch(CoreException $e)
- {
- require_once(APPROOT.'/setup/setuppage.class.inc.php');
- $oP = new SetupWebPage(Dict::S('UI:PageTitle:FatalError'));
- if ($e instanceof SecurityException)
- {
- $oP->add("<h1>".Dict::S('UI:SystemIntrusion')."</h1>\n");
- }
- else
- {
- $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
- }
- $oP->error(Dict::Format('UI:Error_Details', $e->getHtmlDesc()));
- $oP->output();
- if (MetaModel::IsLogEnabledIssue())
- {
- if (MetaModel::IsValidClass('EventIssue'))
- {
- try
- {
- $oLog = new EventIssue();
-
- $oLog->Set('message', $e->getMessage());
- $oLog->Set('userinfo', '');
- $oLog->Set('issue', $e->GetIssue());
- $oLog->Set('impact', 'Page could not be displayed');
- $oLog->Set('callstack', $e->getTrace());
- $oLog->Set('data', $e->getContextData());
- $oLog->DBInsertNoReload();
- }
- catch(Exception $e)
- {
- IssueLog::Error("Failed to log issue into the DB");
- }
- }
- IssueLog::Error($e->getMessage());
- }
- // For debugging only
- //throw $e;
- }
- catch(Exception $e)
- {
- require_once(APPROOT.'/setup/setuppage.class.inc.php');
- $oP = new SetupWebPage(Dict::S('UI:PageTitle:FatalError'));
- $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
- $oP->error(Dict::Format('UI:Error_Details', $e->getMessage()));
- $oP->output();
- if (MetaModel::IsLogEnabledIssue())
- {
- if (MetaModel::IsValidClass('EventIssue'))
- {
- try
- {
- $oLog = new EventIssue();
-
- $oLog->Set('message', $e->getMessage());
- $oLog->Set('userinfo', '');
- $oLog->Set('issue', 'PHP Exception');
- $oLog->Set('impact', 'Page could not be displayed');
- $oLog->Set('callstack', $e->getTrace());
- $oLog->Set('data', array());
- $oLog->DBInsertNoReload();
- }
- catch(Exception $e)
- {
- IssueLog::Error("Failed to log issue into the DB");
- }
- }
- IssueLog::Error($e->getMessage());
- }
- }
- ?>
|