using System; using System.Web.UI; using CMS.DataEngine; using CMS.GlobalHelper; using CMS.PortalEngine; using CMS.SettingsProvider; using CMS.UIControls; [EditedObject(PortalObjectType.DEVICEPROFILE, "profileid")] public partial class CMSModules_DeviceProfile_Pages_Development_PageLayouts : CMSDeviceProfilesPage, IPostBackEventHandler { #region "Variables" /// /// The current device profile to edit. /// private DeviceProfileInfo mDeviceProfile = null; #endregion #region "Properties" /// /// Gets the current device profile to edit. /// protected DeviceProfileInfo DeviceProfile { get { if (mDeviceProfile == null) { mDeviceProfile = EditedObject as DeviceProfileInfo; } return mDeviceProfile; } } /// /// Gets or sets a value indicating whether the request processing should stop. /// /// /// If there is an exception, the request processing is stopped to prevent another errors. /// protected bool StopProcessing { get; set; } #endregion #region "Life-cycle methods" protected override void OnInit(EventArgs e) { base.OnInit(e); ScriptHelper.RegisterJQuery(this); ScriptHelper.RegisterDialogScript(this); BindingGrid.OnExternalDataBound += new OnExternalDataBoundEventHandler(BindingGrid_OnExternalDataBound); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); BindingGrid.GridView.ShowHeader = false; BindingGrid.GridView.BorderWidth = 0; } protected object BindingGrid_OnExternalDataBound(object sender, string sourceName, object parameter) { int sourceLayoutId = ValidationHelper.GetInteger(parameter, 0); return ExecuteFunction(() => CreateLayoutBindingControl(sourceLayoutId)); } /// /// Processes an event raised when a form is posted to the server. /// /// An event argument. public void RaisePostBackEvent(string eventArgument) { ExecuteClientRequest(eventArgument); } #endregion #region "Private methods" /// /// Creates a new instance of control that displays layout mapping for the specified layout, and returns it. /// /// An identifier of the source layout. /// A new instance of control that displays layout mapping for the specified layout. private CMSModules_DeviceProfile_Controls_LayoutBinding CreateLayoutBindingControl(int sourceLayoutId) { CMSModules_DeviceProfile_Controls_LayoutBinding control = LoadControl("~/CMSModules/DeviceProfile/Controls/LayoutBinding.ascx") as CMSModules_DeviceProfile_Controls_LayoutBinding; control.SourceLayout = LayoutInfoProvider.GetLayoutInfo(sourceLayoutId); control.DeviceProfile = DeviceProfile; return control; } /// /// Processes the specified action from the client. /// /// /// The page contains a simple client API. /// If an API function is called, the arguments are stored in hidden fields and postback is executed. /// /// An action name. private void ExecuteClientRequest(string action) { if (!StopProcessing) { switch (action) { case "SetTargetLayout": ExecuteClientRequest(SetTargetLayout, "device_profile.layoutmapping.confirmations.set", "device_profile.layoutmapping.errors.set"); break; case "UnsetTargetLayout": ExecuteClientRequest(UnsetTargetLayout, "device_profile.layoutmapping.confirmations.unset", "device_profile.layoutmapping.errors.unset"); break; } } } /// /// Processes the specified action from the client. /// /// /// The specified delegate is invoked and an information message is displayed. /// If there is an exception, the request processing is stopped and an error message is displayed. /// /// A delegate to execute. /// A resource name of the message that will be displayed in case of success. /// A resource name of the message that will be displayed in case of error. private void ExecuteClientRequest(Action request, string confirmationResourceName, string errorResourceName) { try { request.Invoke(); ShowConfirmation(GetString(confirmationResourceName)); } catch (ApplicationException exception) { ShowError(exception.Message); StopProcessing = true; } catch (Exception exception) { string text = GetString(errorResourceName); if (SettingsKeyProvider.DevelopmentMode) { ShowError(text, null, exception.Message ?? exception.ToString()); } else { ShowError(text); } StopProcessing = true; } } /// /// Executes the SetTragetLayout client action using parameters passed in hidden fields. /// private void SetTargetLayout() { int sourceLayoutId = ValidationHelper.GetInteger(SourceLayoutIdentifierHiddenField.Value, 0); int targetLayoutId = ValidationHelper.GetInteger(TargetLayoutIdentifierHiddenField.Value, 0); using (CMSTransactionScope scope = new CMSTransactionScope()) { LayoutInfo sourceLayout = LayoutInfoProvider.GetLayoutInfo(sourceLayoutId); if (sourceLayout == null) { throw new ApplicationException(GetString("device_profile.layoutmapping.errors.nosourcelayout")); } LayoutInfo targetLayout = LayoutInfoProvider.GetLayoutInfo(targetLayoutId); if (targetLayout == null) { throw new ApplicationException(GetString("device_profile.layoutmapping.errors.notargetlayout")); } string condition = String.Format("DeviceProfileID = {0:D} AND SourceLayoutID = {1:D}", DeviceProfile.ProfileID, sourceLayout.LayoutId); InfoObjectCollection bindings = DeviceProfileLayoutInfoProvider.GetDeviceProfileLayouts(condition, null).Items; DeviceProfileLayoutInfo binding = null; if (bindings.Count > 0) { binding = bindings[0]; } else { binding = new DeviceProfileLayoutInfo { DeviceProfileID = DeviceProfile.ProfileID, SourceLayoutID = sourceLayout.LayoutId }; } binding.TargetLayoutID = targetLayout.LayoutId; DeviceProfileLayoutInfoProvider.SetDeviceProfileLayoutInfo(binding); scope.Commit(); } } /// /// Executes the UnsetTragetLayout client action using parameters passed in hidden fields. /// private void UnsetTargetLayout() { int sourceLayoutId = ValidationHelper.GetInteger(SourceLayoutIdentifierHiddenField.Value, 0); using (CMSTransactionScope scope = new CMSTransactionScope()) { LayoutInfo sourceLayout = LayoutInfoProvider.GetLayoutInfo(sourceLayoutId); if (sourceLayout == null) { throw new ApplicationException(GetString("device_profile.layoutmapping.errors.nosourcelayout")); } string condition = String.Format("DeviceProfileID = {0:D} AND SourceLayoutID = {1:D}", DeviceProfile.ProfileID, sourceLayout.LayoutId); InfoObjectCollection bindings = DeviceProfileLayoutInfoProvider.GetDeviceProfileLayouts(condition, null).Items; if (bindings.Count > 0) { DeviceProfileLayoutInfoProvider.DeleteDeviceProfileLayoutInfo(bindings[0]); } scope.Commit(); } } /// /// Invokes the specified delegate safely, and returns the result. /// /// /// The specified delegate is invoked only if the request processing is not stopped. /// If there is an exception during invocation, the request processing is stopped and an error message is displayed. /// /// Type of delegate result. /// A delegate to invoke. /// The result of delegate invocation. private T ExecuteFunction(Func function) { if (!StopProcessing) { try { return function.Invoke(); } catch (Exception exception) { string text = GetString("general.exception"); if (SettingsKeyProvider.DevelopmentMode) { ShowError(text, null, exception.Message ?? exception.ToString()); } else { ShowError(text); } StopProcessing = true; } } return default(T); } #endregion }