123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443 |
- <?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());
- $sUserString = CMDBChange::GetCurrentUserName();
- $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");
- }
- }
- }
- }
- /**
- * 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'));
- }
- if (is_numeric($id))
- {
- $oObj = MetaModel::GetObject($sClass, $id, false /* MustBeFound */);
- }
- else
- {
- $oObj = MetaModel::GetObjectByName($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/extkeywidget.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/extkeywidget.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
- {
- $oObj->UpdateObject();
- 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());
- $sUserString = CMDBChange::GetCurrentUserName();
- $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/extkeywidget.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);
- $oObj->UpdateObject();
- }
- 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());
- $sUserString = CMDBChange::GetCurrentUserName();
- $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/extkeywidget.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());
- $sUserString = CMDBChange::GetCurrentUserName();
- $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/extkeywidget.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());
- $sUserString = CMDBChange::GetCurrentUserName();
- $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());
- $sUserString = CMDBChange::GetCurrentUserName();
- $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;
-
- case 'cancel':
- $oP->set_title(Dict::S('UI:OperationCancelled'));
- $oP->add('<h1>'.Dict::S('UI:OperationCancelled').'</h1>');
- 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());
- }
- }
- ?>
|