/** * Class UILinksWizard * * @copyright Copyright (C) 2010-2012 Combodo SARL * @license http://opensource.org/licenses/AGPL-3.0 */ class UILinksWizard { protected $m_sClass; protected $m_sLinkageAttr; protected $m_iObjectId; protected $m_sLinkedClass; protected $m_sLinkingAttCode; protected $m_aEditableFields; protected $m_aTableConfig; public function __construct($sClass, $sLinkageAttr, $iObjectId, $sLinkedClass = '') { $this->m_sClass = $sClass; $this->m_sLinkageAttr = $sLinkageAttr; $this->m_iObjectId = $iObjectId; $this->m_sLinkedClass = $sLinkedClass; // Will try to guess below, if it's empty $this->m_sLinkingAttCode = ''; // Will be filled once we've found the attribute corresponding to the linked class $this->m_aEditableFields = array(); $this->m_aTableConfig = array(); $this->m_aTableConfig['form::checkbox'] = array( 'label' => "", 'description' => Dict::S('UI:SelectAllToggle+')); foreach(MetaModel::GetAttributesList($this->m_sClass) as $sAttCode) { $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode); if ($oAttDef->IsExternalKey() && ($sAttCode != $this->m_sLinkageAttr)) { if (empty($this->m_sLinkedClass)) { // This is a class of objects we can manage ! // Since nothing was specify, any class will do ! $this->m_sLinkedClass = $oAttDef->GetTargetClass(); $this->m_sLinkingAttCode = $sAttCode; } else if ($this->m_sLinkedClass == $oAttDef->GetTargetClass()) { // This is the class of objects we want to manage ! $this->m_sLinkingAttCode = $sAttCode; } } else if ( (!$oAttDef->IsExternalKey()) && (!$oAttDef->IsExternalField())) { $this->m_aEditableFields[] = $sAttCode; $this->m_aTableConfig[$sAttCode] = array( 'label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription()); } } if (empty($this->m_sLinkedClass)) { throw( new Exception(Dict::Format('UI:Error:IncorrectLinkDefinition_LinkedClass_Class', $sLinkedClass, $sClass))); } foreach(MetaModel::GetZListItems($this->m_sLinkedClass, 'list') as $sFieldCode) { $oAttDef = MetaModel::GetAttributeDef($this->m_sLinkedClass, $sFieldCode); $this->m_aTableConfig['static::'.$sFieldCode] = array( 'label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription()); } } public function Display(WebPage $oP, $aExtraParams = array()) { $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr); $sTargetClass = $oAttDef->GetTargetClass(); $oTargetObj = MetaModel::GetObject($sTargetClass, $this->m_iObjectId); $oP->set_title("iTop - ".MetaModel::GetName($this->m_sLinkedClass)." objects linked with ".MetaModel::GetName(get_class($oTargetObj)).": ".$oTargetObj->GetRawName()); $oP->add("
\n"); $oP->add("
\n"); $oP->add("
\n"); $oP->add("\n"); $oP->add("\n"); $oP->add("m_sClass}\">\n"); $oP->add("m_sLinkageAttr}\">\n"); $oP->add("m_iObjectId}\">\n"); $oP->add("m_sLinkingAttCode}\">\n"); $oP->add("

".Dict::Format('UI:ManageObjectsOf_Class_LinkedWith_Class_Instance', MetaModel::GetName($this->m_sLinkedClass), MetaModel::GetName(get_class($oTargetObj)), "".$oTargetObj->GetHyperlink()."")."

\n"); $oP->add("
\n"); $oP->add_script( << 0) { $('#btnRemove').removeAttr('disabled'); } else { $('#btnRemove').attr('disabled','disabled'); } } function RemoveSelected() { $('.selection:checked').each( function() { $('#linksToRemove').val($('#linksToRemove').val() + ' ' + this.value); $('#row_'+this.value).remove(); } ); // Disable the button since all the selected items have been removed $('#btnRemove').attr('disabled','disabled'); // Re-run the zebra plugin to properly highlight the remaining lines $('.listResults').trigger('update'); } function AddObjects() { // TO DO: compute the list of objects already linked with the current Object $.post( GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', { 'operation': 'addObjects', 'class': '{$this->m_sClass}', 'linkageAttr': '{$this->m_sLinkageAttr}', 'linkedClass': '{$this->m_sLinkedClass}', 'objectId': '{$this->m_iObjectId}' }, function(data) { $('#ModalDlg').html(data); dlgWidth = $(document).width() - 100; $('#ModalDlg').css('width', dlgWidth); $('#ModalDlg').css('left', 50); $('#ModalDlg').css('top', 50); $('#ModalDlg').dialog( 'open' ); }, 'html' ); } function SearchObjectsToAdd(currentFormId) { var theMap = { 'class': '{$this->m_sClass}', 'linkageAttr': '{$this->m_sLinkageAttr}', 'linkedClass': '{$this->m_sLinkedClass}', 'objectId': '{$this->m_iObjectId}' } if ($('#'+currentFormId+' :input[name=class]').val() != undefined) { theMap.linkedClass = $('#'+currentFormId+' :input[name=class]').val(); } // Gather the parameters from the search form $('#'+currentFormId+' :input').each( function(i) { if (this.name != '') { theMap[this.name] = this.value; } } ); theMap['operation'] = 'searchObjectsToAdd'; // Run the query and display the results $.post( GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', theMap, function(data) { $('#SearchResultsToAdd').html(data); $('#SearchResultsToAdd .listResults').tablesorter( { headers: {0: false}}, widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables }, 'html' ); return false; } function DoAddObjects(currentFormId) { var theMap = { 'class': '{$this->m_sClass}', 'linkageAttr': '{$this->m_sLinkageAttr}', 'linkedClass': '{$this->m_sLinkedClass}', 'objectId': '{$this->m_iObjectId}' } // Gather the parameters from the search form $('#'+currentFormId+' :input').each( function(i) { if ( (this.name != '') && ((this.type != 'checkbox') || (this.checked)) ) { //console.log(this.type); arrayExpr = /\[\]$/; if (arrayExpr.test(this.name)) { // Array if (theMap[this.name] == undefined) { theMap[this.name] = new Array(); } theMap[this.name].push(this.value); } else { theMap[this.name] = this.value; } } } ); theMap['operation'] = 'doAddObjects'; // Run the query and display the results $.post( GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', theMap, function(data) { //console.log('Data: ' + data); if (data != '') { $('#empty_row').remove(); } $('.listResults tbody').append(data); $('.listResults').trigger('update'); $('.listResults').tablesorter( { headers: {0: false}}, widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables }, 'html' ); $('#ModalDlg').dialog('close'); return false; } function InitForm() { // make sure that the form is clean $('.selection').each( function() { this.checked = false; }); $('#btnRemove').attr('disabled','disabled'); $('#linksToRemove').val(''); } function SubmitHook() { var the_form = this; SearchObjectsToAdd(the_form.id); return false; } EOF ); $oP->add_ready_script("InitForm();"); $oFilter = new DBObjectSearch($this->m_sClass); $oFilter->AddCondition($this->m_sLinkageAttr, $this->m_iObjectId, '='); $oSet = new DBObjectSet($oFilter); $aForm = array(); while($oCurrentLink = $oSet->Fetch()) { $aRow = array(); $key = $oCurrentLink->GetKey(); $oLinkedObj = MetaModel::GetObject($this->m_sLinkedClass, $oCurrentLink->Get($this->m_sLinkingAttCode)); $aForm[$key] = $this->GetFormRow($oP, $oLinkedObj, $oCurrentLink); } //var_dump($aTableLabels); //var_dump($aForm); $this->DisplayFormTable($oP, $this->m_aTableConfig, $aForm); $oP->add("     "); $oP->add("   m_sLinkedClass))."\" onClick=\"AddObjects();\">\n"); $oP->add("m_iObjectId.");\">"); $oP->add("   \n"); $oP->add("

 

\n"); $oP->add("
\n"); $oP->add("\n"); if (isset($aExtraParams['StartWithAdd']) && ($aExtraParams['StartWithAdd'])) { $oP->add_ready_script("AddObjects();"); } } protected function GetFormRow($oP, $oLinkedObj, $currentLink = null ) { $aRow = array(); if(is_object($currentLink)) { $key = $currentLink->GetKey(); $sNameSuffix = "[$key]"; // To make a tabular form $aRow['form::checkbox'] = ""; $aRow['form::checkbox'] .= ""; foreach($this->m_aEditableFields as $sFieldCode) { $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sFieldCode); $aRow[$sFieldCode] = cmdbAbstractObject::GetFormElementForField($oP, $this->m_sClass, $sFieldCode, $oAttDef, $currentLink->Get($sFieldCode), '' /* DisplayValue */, $key, $sNameSuffix); } } else { // form for creating a new record $sNameSuffix = "[$currentLink]"; // To make a tabular form $aRow['form::checkbox'] = ""; $aRow['form::checkbox'] .= ""; foreach($this->m_aEditableFields as $sFieldCode) { $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sFieldCode); $aRow[$sFieldCode] = cmdbAbstractObject::GetFormElementForField($oP, $this->m_sClass, $sFieldCode, $oAttDef, '' /* TO DO/ call GetDefaultValue($oObject->ToArgs()) */, '' /* DisplayValue */, '' /* id */, $sNameSuffix); } } foreach(MetaModel::GetZListItems($this->m_sLinkedClass, 'list') as $sFieldCode) { $aRow['static::'.$sFieldCode] = $oLinkedObj->GetAsHTML($sFieldCode); } return $aRow; } protected function DisplayFormTable(WebPage $oP, $aConfig, $aData) { $oP->add("\n"); // Header $oP->add("\n"); $oP->add("\n"); foreach($aConfig as $sName=>$aDef) { $oP->add("\n"); } $oP->add("\n"); $oP->add("\n"); // Content $oP->add("\n"); if (count($aData) == 0) { $oP->add(""); } else { foreach($aData as $iRowId => $aRow) { $this->DisplayFormRow($oP, $aConfig, $aRow, $iRowId); } } $oP->add("\n"); // Footer $oP->add("
".$aDef['label']."
".Dict::S('UI:Message:EmptyList:UseAdd')."
\n"); } protected function DisplayFormRow(WebPage $oP, $aConfig, $aRow, $iRowId) { $oP->add("\n"); foreach($aConfig as $sName=>$void) { $oP->add("".$aRow[$sName]."\n"); } $oP->add("\n"); } public function DisplayAddForm(WebPage $oP) { $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr); $sTargetClass = $oAttDef->GetTargetClass(); $oTargetObj = MetaModel::GetObject($sTargetClass, $this->m_iObjectId); $oP->add("
\n"); //$oP->add("
\n"); //$oP->add("

".Dict::Format('UI:AddObjectsOf_Class_LinkedWith_Class_Instance', MetaModel::GetName($this->m_sLinkedClass), MetaModel::GetName(get_class($oTargetObj)), "".$oTargetObj->GetHyperlink()."")."

\n"); //$oP->add("
\n"); $oFilter = new DBObjectSearch($this->m_sLinkedClass); $oSet = new CMDBObjectSet($oFilter); $oBlock = new DisplayBlock($oFilter, 'search', false); $oBlock->Display($oP, 'SearchFormToAdd', array('open' => true)); $oP->Add("
\n"); $oP->Add("
\n"); $oP->Add("

".Dict::S('UI:Message:EmptyList:UseSearchForm')."

\n"); $oP->Add("
\n"); $oP->add("  "); $oP->Add("
\n"); $oP->Add("\n"); $oP->add_ready_script("$('#ModalDlg').dialog('option', {title:'".Dict::Format('UI:AddObjectsOf_Class_LinkedWith_Class_Instance', MetaModel::GetName($this->m_sLinkedClass), MetaModel::GetName(get_class($oTargetObj)), "".$oTargetObj->GetHyperlink()."")."'});"); $oP->add_ready_script("$('div#SearchFormToAdd form').bind('submit.uilinksWizard', SubmitHook);"); } public function SearchObjectsToAdd(WebPage $oP) { //$oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr); $oFilter = new DBObjectSearch($this->m_sLinkedClass); $oSet = new CMDBObjectSet($oFilter); $oBlock = new DisplayBlock($oFilter, 'list', false); $oBlock->Display($oP, 'ResultsToAdd', array('menu' => false, 'selection_mode' => true)); // Don't display the 'Actions' menu on the results } public function DoAddObjects(WebPage $oP, $aLinkedObjectIds = array()) { //$oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr); //$sTargetClass = $oAttDef->GetTargetClass(); //$oP->Add("\n"); // Just to make sure it's not empty $aTable = array(); foreach($aLinkedObjectIds as $iObjectId) { $oLinkedObj = MetaModel::GetObject($this->m_sLinkedClass, $iObjectId); if (is_object($oLinkedObj)) { $aRow = $this->GetFormRow($oP, $oLinkedObj, -$iObjectId ); // Not yet created link get negative Ids $this->DisplayFormRow($oP, $this->m_aTableConfig, $aRow, -$iObjectId); } else { echo Dict::Format('UI:Error:Object_Class_Id_NotFound', $this->m_sLinkedClass, $iObjectId); } } //var_dump($aTable); //$oP->Add("\n"); // Just to make sure it's not empty } } ?>