mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
411 lines
16 KiB
PHP
411 lines
16 KiB
PHP
<?php
|
|
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' => "<input class=\"select_all\" type=\"checkbox\" value=\"1\" onChange=\"var value = this.checked; $('.selection').each( function() { this.checked = value; } );OnSelectChange();\">", '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, UserContext $oContext, $aExtraParams = array())
|
|
{
|
|
$oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr);
|
|
$sTargetClass = $oAttDef->GetTargetClass();
|
|
$oTargetObj = $oContext->GetObject($sTargetClass, $this->m_iObjectId);
|
|
|
|
$oP->set_title("iTop - ".MetaModel::GetName($this->m_sLinkedClass)." objects linked with ".MetaModel::GetName(get_class($oTargetObj)).": ".$oTargetObj->GetName());
|
|
$oP->add("<div class=\"wizContainer\">\n");
|
|
$oP->add("<form method=\"post\">\n");
|
|
$oP->add("<div class=\"page_header\">\n");
|
|
$oP->add("<input type=\"hidden\" id=\"linksToRemove\" name=\"linksToRemove\" value=\"\">\n");
|
|
$oP->add("<input type=\"hidden\" name=\"operation\" value=\"do_modify_links\">\n");
|
|
$oP->add("<input type=\"hidden\" name=\"class\" value=\"{$this->m_sClass}\">\n");
|
|
$oP->add("<input type=\"hidden\" name=\"linkage\" value=\"{$this->m_sLinkageAttr}\">\n");
|
|
$oP->add("<input type=\"hidden\" name=\"object_id\" value=\"{$this->m_iObjectId}\">\n");
|
|
$oP->add("<input type=\"hidden\" name=\"linking_attcode\" value=\"{$this->m_sLinkingAttCode}\">\n");
|
|
$oP->add("<h1>".Dict::Format('UI:ManageObjectsOf_Class_LinkedWith_Class_Instance', MetaModel::GetName($this->m_sLinkedClass), MetaModel::GetName(get_class($oTargetObj)), "<span class=\"hilite\">".$oTargetObj->GetHyperlink()."</span>")."</h1>\n");
|
|
$oP->add("</div>\n");
|
|
$oP->add("<script type=\"text/javascript\">\n");
|
|
$oP->add(
|
|
<<<EOF
|
|
function OnSelectChange()
|
|
{
|
|
var nbChecked = $('.selection:checked').length;
|
|
if (nbChecked > 0)
|
|
{
|
|
$('#btnRemove').attr('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
|
|
$.get( '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').jqmShow();
|
|
},
|
|
'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
|
|
$.get( 'ajax.render.php', theMap,
|
|
function(data)
|
|
{
|
|
$('#SearchResultsToAdd').html(data);
|
|
nb_rows = $('#SearchResultsToAdd table.listResults tr').length;
|
|
if(nb_rows > 10)
|
|
{
|
|
yOffset = $('#ModalDlg').height() - $('#SearchResultsToAdd table.listResults tbody').height();
|
|
tbodyHeight = $(document).height() - 100 - yOffset;
|
|
if ($('#ModalDlg').height() > ($(document).height() - 100))
|
|
{
|
|
$('#SearchResultsToAdd table.listResults tbody').attr('height', tbodyHeight);
|
|
$('#SearchResultsToAdd .listResults tbody').css('overflow', 'auto');
|
|
$('#SearchResultsToAdd .listResults').tablesorter( { headers: { 0:{sorter: false }}, widgets: ['zebra']} ); // 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
|
|
$.get( '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:{sorter: false }}, widgets: ['zebra']} ); // sortable and zebra tables
|
|
},
|
|
'html'
|
|
);
|
|
$('#ModalDlg').jqmHide();
|
|
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("</script>\n");
|
|
$oP->add_ready_script("InitForm();");
|
|
$oFilter = $oContext->NewFilter($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 = $oContext->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("<span style=\"float:left;\"> <img src=\"../images/tv-item-last.gif\"> <input id=\"btnRemove\" type=\"button\" value=\"".Dict::S('UI:RemoveLinkedObjectsOf_Class')."\" onClick=\"RemoveSelected();\" >");
|
|
$oP->add(" <input id=\"btnAdd\" type=\"button\" value=\"".Dict::Format('UI:AddLinkedObjectsOf_Class', MetaModel::GetName($this->m_sLinkedClass))."\" onClick=\"AddObjects();\"></span>\n");
|
|
$oP->add("<span style=\"float:right;\"><input id=\"btnCancel\" type=\"button\" value=\"".Dict::S('UI:Button:Cancel')."\" onClick=\"goBack();\">");
|
|
$oP->add(" <input id=\"btnOk\" type=\"submit\" value=\"".Dict::S('UI:Button:Ok')."\"></span>\n");
|
|
$oP->add("<span style=\"clear:both;\"><p> </p></span>\n");
|
|
$oP->add("</div>\n");
|
|
$oP->add("</form>\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'] = "<input class=\"selection\" type=\"checkbox\" onChange=\"OnSelectChange();\" value=\"$key\">";
|
|
$aRow['form::checkbox'] .= "<input type=\"hidden\" name=\"linkId[$key]\" value=\"$key\">";
|
|
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'] = "<input class=\"selection\" type=\"checkbox\" onChange=\"OnSelectChange();\" value=\"$currentLink\">";
|
|
$aRow['form::checkbox'] .= "<input type=\"hidden\" name=\"linkId[$currentLink]\" value=\"$currentLink\">";
|
|
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("<table class=\"listResults\">\n");
|
|
// Header
|
|
$oP->add("<thead>\n");
|
|
$oP->add("<tr>\n");
|
|
foreach($aConfig as $sName=>$aDef)
|
|
{
|
|
$oP->add("<th title=\"".$aDef['description']."\">".$aDef['label']."</th>\n");
|
|
}
|
|
$oP->add("</tr>\n");
|
|
$oP->add("</thead>\n");
|
|
|
|
// Content
|
|
$oP->add("</tbody>\n");
|
|
if (count($aData) == 0)
|
|
{
|
|
$oP->add("<tr id=\"empty_row\"><td colspan=\"".count($aConfig)."\" style=\"text-align:center;\">".Dict::S('UI:Message:EmptyList:UseAdd')."</td></td>");
|
|
}
|
|
else
|
|
{
|
|
foreach($aData as $iRowId => $aRow)
|
|
{
|
|
$this->DisplayFormRow($oP, $aConfig, $aRow, $iRowId);
|
|
}
|
|
}
|
|
$oP->add("</tbody>\n");
|
|
|
|
// Footer
|
|
$oP->add("</table>\n");
|
|
}
|
|
|
|
protected function DisplayFormRow(WebPage $oP, $aConfig, $aRow, $iRowId)
|
|
{
|
|
$oP->add("<tr id=\"row_$iRowId\">\n");
|
|
foreach($aConfig as $sName=>$void)
|
|
{
|
|
$oP->add("<td>".$aRow[$sName]."</td>\n");
|
|
}
|
|
$oP->add("</tr>\n");
|
|
}
|
|
|
|
public function DisplayAddForm(WebPage $oP, UserContext $oContext)
|
|
{
|
|
$oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr);
|
|
$sTargetClass = $oAttDef->GetTargetClass();
|
|
$oTargetObj = $oContext->GetObject($sTargetClass, $this->m_iObjectId);
|
|
$oP->add("<div class=\"wizContainer\">\n");
|
|
$oP->add("<div class=\"page_header\">\n");
|
|
$oP->add("<h1>".Dict::Format('UI:AddObjectsOf_Class_LinkedWith_Class_Instance', MetaModel::GetName($this->m_sLinkedClass), MetaModel::GetName(get_class($oTargetObj)), "<span class=\"hilite\">".$oTargetObj->GetHyperlink()."</span>")."</h1>\n");
|
|
$oP->add("</div>\n");
|
|
|
|
$oFilter = $oContext->NewFilter($this->m_sLinkedClass);
|
|
$oSet = new CMDBObjectSet($oFilter);
|
|
$oBlock = new DisplayBlock($oFilter, 'search', false);
|
|
$oBlock->Display($oP, 'SearchFormToAdd', array('open' => true));
|
|
$oP->Add("<form id=\"ObjectsAddForm\" OnSubmit=\"return DoAddObjects(this.id);\">\n");
|
|
$oP->Add("<div id=\"SearchResultsToAdd\">\n");
|
|
$oP->Add("<div style=\"height: 100px; background: #fff;border-color:#F6F6F1 #E6E6E1 #E6E6E1 #F6F6F1; border-style:solid; border-width:3px; text-align: center; vertical-align: center;\"><p>".Dict::S('UI:Message:EmptyList:UseSearchForm')."</p></div>\n");
|
|
$oP->Add("</div>\n");
|
|
$oP->add("<input type=\"button\" value=\"".Dict::S('UI:Button:Cancel')."\" onClick=\"$('#ModalDlg').jqmHide();\"> <input type=\"submit\" value=\"".Dict::S('UI:Button:Add')."\">");
|
|
$oP->Add("</div>\n");
|
|
$oP->Add("</form>\n");
|
|
$oP->add_ready_script("$('div#SearchFormToAdd form').bind('submit.uilinksWizard', SubmitHook);");
|
|
}
|
|
|
|
public function SearchObjectsToAdd(WebPage $oP, UserContext $oContext)
|
|
{
|
|
//$oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr);
|
|
|
|
$oFilter = $oContext->NewFilter($this->m_sLinkedClass);
|
|
$oSet = new CMDBObjectSet($oFilter);
|
|
$oBlock = new DisplayBlock($oFilter, 'list', false);
|
|
$oBlock->Display($oP, 'ResultsToAdd', array('menu' => false, 'selection_mode' => true, 'display_limit' => false)); // Don't display the 'Actions' menu on the results
|
|
}
|
|
|
|
public function DoAddObjects(WebPage $oP, UserContext $oContext, $aLinkedObjectIds = array())
|
|
{
|
|
//$oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr);
|
|
//$sTargetClass = $oAttDef->GetTargetClass();
|
|
//$oP->Add("<!-- nb of objects to add: ".count($aLinkedObjectIds)." -->\n"); // Just to make sure it's not empty
|
|
$aTable = array();
|
|
foreach($aLinkedObjectIds as $iObjectId)
|
|
{
|
|
$oLinkedObj = $oContext->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("<!-- end of added list -->\n"); // Just to make sure it's not empty
|
|
}
|
|
}
|
|
?>
|