using System;
using System.Web.UI.HtmlControls;
using CMS.DataCom;
using CMS.EventLog;
using CMS.FormEngine;
using CMS.GlobalHelper;
using CMS.SettingsProvider;
using CMS.UIControls;
///
/// A control that displays Data.com contact mapping details.
///
public partial class CMSModules_ContactManagement_Controls_UI_DataCom_ContactMapping : CMSAdminControl
{
#region "Variables"
///
/// The Data.com contact mapping to display.
///
private EntityMapping mEntityMapping;
///
/// Indicates whether the control is enabled.
///
private bool mEnabled = true;
#endregion
#region "Properties"
///
/// Gets or sets the Data.com contact mapping to display.
///
public EntityMapping Mapping
{
get
{
return mEntityMapping;
}
set
{
mEntityMapping = value;
}
}
///
/// Gets or sets the value indicating whether the control is enabled.
///
public bool Enabled
{
get
{
return mEnabled;
}
set
{
mEnabled = value;
}
}
#endregion
#region "Life cycle methods"
protected override void OnPreRender(EventArgs e)
{
if (Mapping != null)
{
try
{
FormInfo formInfo = DataComHelper.GetContactFormInfo();
EntityInfo entityInfo = DataComHelper.GetContactEntityInfo();
HtmlTable table = CreateTable(formInfo, entityInfo, Mapping);
Controls.Add(table);
}
catch (Exception exception)
{
HandleException(exception);
}
}
base.OnPreRender(e);
}
#endregion
#region "Private methods"
///
/// Creates and initializes a new instance of the HtmlTable class with the specified Data.com contact mapping, and returns it.
///
/// The CMS contact form info.
/// The Data.com contact entity info.
/// The Data.com contact mapping.
/// A new instance of the HtmlTable class initialized with the specified Data.com contact mapping.
private HtmlTable CreateTable(FormInfo formInfo, EntityInfo entityInfo, EntityMapping mapping)
{
HtmlTable table = new HtmlTable();
table.Rows.Add(CreateHeaderRow());
foreach (IFormItem formItem in formInfo.ItemsList)
{
FormFieldInfo formField = formItem as FormFieldInfo;
if (formField != null)
{
EntityMappingItem mappingItem = mapping.GetItem(formField.Name);
if (mappingItem != null)
{
EntityAttributeInfo entityAttribute = entityInfo.GetAttributeInfo(mappingItem.EntityAttributeName);
if (entityAttribute != null)
{
HtmlTableRow row = new HtmlTableRow();
table.Rows.Add(row);
HtmlTableCell formFieldCell = new HtmlTableCell();
row.Cells.Add(formFieldCell);
formFieldCell.InnerText = ResHelper.LocalizeString(formField.Caption);
HtmlTableCell entityAttributeCell = new HtmlTableCell();
row.Cells.Add(entityAttributeCell);
entityAttributeCell.InnerText = ResHelper.LocalizeString(entityAttribute.DisplayName);
if (!Enabled)
{
formFieldCell.Style.Add("color", "#888888");
entityAttributeCell.Style.Add("color", "#888888");
}
}
}
}
}
return table;
}
///
/// Creates a header for the table with Data.com contact mapping details, and returns it.
///
/// A header for the table with Data.com contact mapping details.
private HtmlTableRow CreateHeaderRow()
{
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell sourceCell = new HtmlTableCell()
{
InnerText = GetString("datacom.kenticocms")
};
sourceCell.Style.Add("font-weight", "bold");
row.Cells.Add(sourceCell);
HtmlTableCell destinationCell = new HtmlTableCell()
{
InnerText = GetString("datacom.datacom")
};
destinationCell.Style.Add("font-weight", "bold");
row.Cells.Add(destinationCell);
if (!Enabled)
{
sourceCell.Style.Add("color", "#888888");
destinationCell.Style.Add("color", "#888888");
}
return row;
}
///
/// Displays an error message and logs the specified exception to the event log.
///
/// The exception to handle.
private void HandleException(Exception exception)
{
ErrorSummary.Report(exception);
EventLogProvider.LogException("Data.com Connector", "ContactMappingControl", exception);
}
#endregion
}