File: D:/HostingSpaces/TDijk1/erp-apps.eu/wwwroot/ERPApps/ERPWebParts/Documents/ReviewForm.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using CMS.GlobalHelper;
using CMS.PortalControls;
using CMS.CMSHelper;
using CMS.FormEngine;
using CMS.TreeEngine;
using CMS.EventLog;
using CMS.FormControls;
using CMS.DocumentEngine;
using CMS.ExtendedControls;
using CMS.Helpers;
/// <summary>
/// Form for creating new user review.
/// </summary>
public partial class ERPApps_ERPWebParts_Documents_ReviewForm : CMSAbstractWebPart
{
#region "Properties"
/// <summary>
/// Document type of reviews.
/// Default is ERPConfig.DOCTYPE_REVIEW
/// </summary>
public string DocumentType
{
get
{
return ValidationHelper.GetString(this.GetValue("DocumentType"), ERPConfig.DOCTYPE_REVIEW);
}
set
{
this.SetValue("DocumentType", value);
}
}
/// <summary>
/// Alternative form full name.
/// </summary>
public string AlternativeFormFullName
{
get
{
return ValidationHelper.GetString(this.GetValue("AlternativeFormFullName"), "");
}
set
{
this.SetValue("AlternativeFormFullName", value);
}
}
/// <summary>
/// ID of application for which the new review should be created.
/// </summary>
public int ApplicationNodeID
{
get
{
return ValidationHelper.GetInteger(this.GetValue("ApplicationNodeID"), 0);
}
set
{
this.SetValue("ApplicationNodeID", value);
}
}
/// <summary>
/// Content displayed after saving the review.
/// </summary>
public string ConfirmationText
{
get
{
return ValidationHelper.GetString(this.GetValue("ConfirmationText"), "");
}
set
{
this.SetValue("ConfirmationText", value);
}
}
/// <summary>
/// Redirect URL to redirect to after saving the review.
/// </summary>
public string RedirectURL
{
get
{
return ValidationHelper.GetString(this.GetValue("RedirectURL"), "");
}
set
{
this.SetValue("RedirectURL", value);
}
}
#endregion
#region "Events"
/// <summary>
/// Creates the review
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void btnSave_Click(object sender, EventArgs e)
{
EditingFormControl documentNameControl = (formElem.FieldEditingControls["DocumentName"] as EditingFormControl);
if ( formElem.ValidateData() )
{
EventLogProvider elp = new EventLogProvider();
TreeNode applicationNode = TreeHelper.SelectSingleNode(ApplicationNodeID);
if ( applicationNode == null || ( !applicationNode.NodeClassName.ToLower().Equals(ERPConfig.DOCTYPE_APPLICATION.ToLower()) ) )
{
// error, invalid parent node
EventLogProvider.LogEvent(EventType.ERROR, "Reviews", "CreateNew", null, "Application node for ID " + ApplicationNodeID + " can not be found or is not of " + ERPConfig.DOCTYPE_APPLICATION + " type. Review not stored.");
return;
}
// does the target folder exists?
TreeNode targetFolder = TreeHelper.SelectSingleNode(applicationNode.NodeAliasPath + "/" + ERPConfig.APP_REVIEWS_DIR_NAME);
if ( targetFolder == null )
{
targetFolder = ERPDataHelper.CreateReviewsDirectory(applicationNode.NodeID);
}
if ( targetFolder == null )
{
// error, target folder was not created
EventLogProvider.LogEvent(EventType.ERROR, "Reviews", "CreateNew", null, "Target folder for reviews of application ID " + ApplicationNodeID + " can not be found or created. Review not stored.");
return;
}
// create document name from date
DateTime now = DateTime.Now;
string documentName = now.ToShortDateString() + " " + now.ToShortTimeString();
if ( documentNameControl != null )
{
documentNameControl.Value = documentName;
}
else
{
formElem.Data["DocumentName"] = documentName;
}
// set values
formElem.NodeID = targetFolder.NodeID;
formElem.Data["ReviewDate"] = DateTime.Now;
formElem.Data["ReviewAuthorIP"] = RequestContext.UserHostAddress;
formElem.Data["ReviewApplicationID"] = ValidationHelper.GetInteger(applicationNode.GetValue("AppID"), 0);
formElem.OnAfterSave += new EventHandler(formElem_OnAfterSave);
// save review
formElem.Save();
}
}
void formElem_OnAfterSave(object sender, EventArgs e)
{
// send notification to admin
ERPNotifications.SendNotificationToAdmin("ERP.AdminNotification.Review", formElem.EditedObject as TreeNode);
if ( !DataHelper.IsEmpty(RedirectURL) )
{
// redirect to goal URL
URLHelper.Redirect(URLHelper.ResolveUrl(RedirectURL));
}
// show confirmation message
lblConfirmation.Text = ConfirmationText;
lblConfirmation.Visible = true;
// hide form
panelForm.Visible = false;
}
/// <summary>
/// Displays the form after clicking the "Add review" button.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void btnShow_Click(object sender, EventArgs e)
{
panelForm.Visible = true;
}
#endregion
#region "Initialization"
private void SetupControl()
{
if ( this.StopProcessing || !this.Visible )
{
return;
}
CMS.DocumentEngine.TreeProvider provider = new CMS.DocumentEngine.TreeProvider();
// setup form
formElem.Visible = true;
formElem.TreeProvider = provider;
formElem.NodeID = 0;
formElem.FormName = DocumentType + ".default";
formElem.CultureCode = DocumentContext.CurrentPageInfo.DocumentCulture;
formElem.AlternativeFormFullName = AlternativeFormFullName;
formElem.FormMode = FormModeEnum.Insert;
}
#endregion
#region "On events"
public override void OnContentLoaded()
{
base.OnContentLoaded();
SetupControl();
}
#endregion
}