/*
* Box Social™
* http://boxsocial.net/
* Copyright © 2007, David Lachlan Smith
*
* $Id:$
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Web;
using BoxSocial;
using BoxSocial.Forms;
using BoxSocial.Internals;
using BoxSocial.IO;
namespace BoxSocial.Internals
{
public abstract class ControlPanelSubModule : MarshalByRefObject, IComparable
{
protected Core core;
protected Mysql db;
protected Template template;
protected SessionState session;
protected UnixTime tz;
//protected User loggedInMember;
///
/// Account panel owner
///
protected Primitive Owner;
///
/// Account panel viewer
///
protected User LoggedInMember;
//protected HttpRequest Request;
//protected HttpResponse Response;
//protected HttpServerUtility Server;
private Dictionary modes = new Dictionary();
private Dictionary saveHandlers = new Dictionary();
public Primitive SetOwner
{
set
{
Owner = value;
}
}
///
/// We do this so we don't have to keep re-declaring the same
/// constructor
///
/// Core token
public void ModuleVector(Core core)
{
ModuleVector(core, core.Session.LoggedInMember);
}
///
/// We do this so we don't have to keep re-declaring the same
/// constructor
///
/// Core token
/// Owner
public void ModuleVector(Core core, Primitive owner)
{
this.core = core;
this.db = core.Db;
this.session = core.Session;
this.tz = core.Tz;
this.Owner = owner;
this.LoggedInMember = session.LoggedInMember;
CreateTemplate();
string mode = core.Http["mode"];
if (Load != null)
{
Load(this, new EventArgs());
}
if (string.IsNullOrEmpty(mode) || !HasModeHandler(mode))
{
if (Show != null)
{
Show(this, new EventArgs());
}
}
else if (!string.IsNullOrEmpty(mode))
{
ShowMode(mode);
}
RenderTemplate();
}
///
/// The title of the sub-module, null for an invisible sub-module
///
public abstract string Title
{
get;
}
///
/// The order the module is to appear along the tab display.
///
public abstract int Order
{
get;
}
///
/// The unique key used to identify the module in requests.
///
public string Key
{
get
{
Type type = this.GetType();
foreach (Attribute attr in type.GetCustomAttributes(typeof(AccountSubModuleAttribute), false))
{
if (attr != null)
{
AccountSubModuleAttribute asmattr = (AccountSubModuleAttribute)attr;
if (asmattr.Name != null)
{
return asmattr.Name;
}
}
}
// null key, should not happen!!!
return null;
}
}
public string ModuleKey
{
get
{
Type type = this.GetType();
foreach (Attribute attr in type.GetCustomAttributes(typeof(AccountSubModuleAttribute), false))
{
if (attr != null)
{
AccountSubModuleAttribute asmattr = (AccountSubModuleAttribute)attr;
if (asmattr.Name != null)
{
return asmattr.ModuleName;
}
}
}
// null key, should not happen!!!
return null;
}
}
public bool IsDefault
{
get
{
Type type = this.GetType();
foreach (Attribute attr in type.GetCustomAttributes(typeof(AccountSubModuleAttribute), false))
{
if (attr != null)
{
AccountSubModuleAttribute asmattr = (AccountSubModuleAttribute)attr;
if (asmattr.Name != null)
{
return asmattr.IsDefault;
}
}
}
// null key, should not happen!!!
return false;
}
}
public AppPrimitives Primitives
{
get
{
Type type = this.GetType();
foreach (Attribute attr in type.GetCustomAttributes(typeof(AccountSubModuleAttribute), false))
{
if (attr != null)
{
AccountSubModuleAttribute asmattr = (AccountSubModuleAttribute)attr;
if (asmattr.Name != null)
{
return asmattr.Primitives;
}
}
}
// null key, should not happen!!!
return AppPrimitives.None;
}
}
public delegate void ModuleModeHandler(object sender, ModuleModeEventArgs e);
public delegate void ItemModuleModeHandler(object sender, ItemModuleModeEventArgs e);
private bool HasModeHandler(string mode)
{
if (modes.ContainsKey(mode) || saveHandlers.ContainsKey(mode))
{
return true;
}
else
{
return false;
}
}
private void ShowMode(string mode)
{
if (core.Http.Form["save"] != null)
{
if (saveHandlers.ContainsKey(mode))
{
Save(saveHandlers[mode]);
return;
}
}
modes[mode](this, new ModuleModeEventArgs(mode));
}
protected void AddModeHandler(string mode, ModuleModeHandler modeHandler)
{
modes.Add(mode, modeHandler);
}
protected void AddSaveHandler(string mode, EventHandler saveHandler)
{
saveHandlers.Add(mode, saveHandler);
}
protected void Save(EventHandler saveHandler)
{
if (core.Http.Form["save"] != null)
{
saveHandler(this, new EventArgs());
}
}
protected void SaveMode(ModuleModeHandler saveHandler)
{
if (core.Http.Form["save"] != null)
{
if (core.Http.Form["mode"] != null)
{
saveHandler(this, new ModuleModeEventArgs(core.Http.Form["mode"]));
}
}
}
protected void SaveItemMode(ItemModuleModeHandler saveHandler, NumberedItem item)
{
if (core.Http.Form["save"] != null)
{
if (core.Http.Form["mode"] != null)
{
saveHandler(this, new ItemModuleModeEventArgs(core.Http.Form["mode"], item));
}
}
}
protected event EventHandler Load;
protected event EventHandler Show;
///
/// Creates an isolated template class for the module to render
/// inside.
///
private void CreateTemplate()
{
template = new Template(core.Http.TemplatePath, "1301.html");
if (Owner != null)
{
template.Parse("U_ACCOUNT", core.Uri.AppendSid(Owner.AccountUriStub, true));
template.Parse("S_ACCOUNT", core.Uri.AppendSid(Owner.AccountUriStub, true));
}
template.AddPageAssembly(Assembly.GetCallingAssembly());
template.SetProse(core.Prose);
}
///
/// Renders the template to the account panel.
///
private void RenderTemplate()
{
core.Template.ParseRaw("MODULE_CONTENT", ((Template)template).ToString());
}
///
/// Renders an error to the account panel.
///
///
protected void DisplayError(string errorMessage)
{
template = new Template(core.Http.TemplatePath, "1302.html");
template.Parse("ERROR_MESSAGE", errorMessage);
RenderTemplate();
}
///
/// Renders an error to the account panel.
///
///
protected void DisplayGenericError()
{
template = new Template(core.Http.TemplatePath, "1302.html");
template.ParseRaw("ERROR_MESSAGE", "An error has occured accessing this account module, maybe you are accessing it incorrectly. Go Back");
RenderTemplate();
}
protected void SetTemplate(string templateName)
{
template.AddPageAssembly(Assembly.GetCallingAssembly());
template.SetTemplate(Assembly.GetCallingAssembly().GetName().Name, templateName);
core.Prose.AddApplication(Assembly.GetCallingAssembly().GetName().Name);
}
///
/// Builds a URI to the current sub module
///
/// URI built
public string BuildUri()
{
return BuildUri(Key);
}
public string BuildUri(Core core)
{
return BuildUri(core, Key);
}
///
/// Builds a URI to a different sub module in this module
///
/// URI built
protected string BuildUri(string sub)
{
return core.Uri.AppendSid(string.Format("{0}{1}/{2}",
Owner.AccountUriStub, ModuleKey, sub));
}
protected string BuildUri(Core core, string sub)
{
return core.Uri.AppendSid(string.Format("{0}{1}/{2}",
Owner.AccountUriStub, ModuleKey, sub));
}
public string BuildUri(string sub, long id)
{
return core.Uri.AppendSid(string.Format("{0}{1}/{2}?id={3}",
Owner.AccountUriStub, ModuleKey, sub, id));
}
public string BuildUri(string sub, string mode)
{
return core.Uri.AppendSid(string.Format("{0}{1}/{2}?mode={3}",
Owner.AccountUriStub, ModuleKey, sub, mode));
}
public string BuildUri(string sub, string mode, long id)
{
return core.Uri.AppendSid(string.Format("{0}{1}/{2}?mode={3}&id={4}",
Owner.AccountUriStub, ModuleKey, sub, mode, id), true);
}
///
/// Builds a URI to the sub module key given of the current module,
/// appending additional query string arguments given.
///
/// Sub module key
/// Additional query string arguments
/// URI built
public string BuildUri(Dictionary arguments)
{
return BuildUri(Key, arguments);
}
///
/// Builds a URI to the sub module key given of the current module,
/// appending additional query string arguments given.
///
/// Sub module key
/// Additional query string arguments
/// URI built
public string BuildUri(string sub, Dictionary arguments)
{
string argumentList = string.Empty;
foreach (string key in arguments.Keys)
{
if (argumentList == string.Empty)
{
argumentList = string.Format("?{0}={1}",
key, arguments[key]);
}
else
{
argumentList = string.Format("{0}&{1}={2}",
argumentList, key, arguments[key]);
}
}
return core.Uri.AppendSid(string.Format("{0}{1}/{2}{3}",
Owner.AccountUriStub, ModuleKey, sub, argumentList));
}
///
/// Sets the redirect URI.
///
///
/// Useful for redirecting from a message box after posting a form.
///
/// URI to redirect to
protected void SetRedirectUri(string uri)
{
core.Template.Parse("REDIRECT_URI", uri);
}
///
/// Sets an error in posting.
///
/// Error string to be displayed
protected void SetError(string errorString)
{
core.Template.Parse("ERROR", errorString);
}
///
/// Set an information display.
///
///
/// Can be used for displaying a success in saving information on a page.
///
/// Information string to be displayed
protected void SetInformation(string informationString)
{
core.Template.Parse("INFO", informationString);
}
///
/// Authorises a request ensuring the SID is present in the URL to
/// prevent undesired operation of the account panel for users.
///
public static void AuthoriseRequestSid(Core core)
{
if (core.Http.Query["sid"] != core.Session.SessionId)
{
if (string.IsNullOrEmpty(core.Http.Query["sid"]))
{
core.Display.ShowMessage("Unauthorised", "You are unauthorised to do this action.");
return;
}
SelectQuery query = new SelectQuery("user_sessions");
query.AddFields("user_id");
query.AddCondition("session_string", core.Http.Query["sid"]);
query.AddCondition("user_id", core.Session.LoggedInMember.Id);
query.AddCondition("session_signed_in", true);
query.AddCondition("session_ip", core.Session.IPAddress.ToString());
if (core.Db.Query(query).Rows.Count == 0)
{
core.Display.ShowMessage("Unauthorised", "You are unauthorised to do this action.");
return;
}
}
}
protected void AuthoriseRequestSid()
{
AuthoriseRequestSid(core);
}
///
/// Implements CompareTo
///
///
/// Comparison based on Order. Can be used to sort a list of
/// modules in the desired display order.
///
/// Object to compare with
/// Comparisson value
public int CompareTo(object obj)
{
if (!(obj is ControlPanelSubModule)) return -1;
return Order.CompareTo(((ControlPanelSubModule)obj).Order);
}
protected void ParseBbcode(string templateVar, string input)
{
ParseBbcode(templateVar, input, null);
}
protected void ParseBbcode(string templateVar, string input, User owner)
{
if (owner != null)
{
template.ParseRaw(templateVar, core.Bbcode.Parse(HttpUtility.HtmlEncode(input), core.Session.LoggedInMember, owner));
}
else
{
template.ParseRaw(templateVar, core.Bbcode.Parse(HttpUtility.HtmlEncode(input), core.Session.LoggedInMember));
}
}
/*protected void ParsePermissionsBox(string templateVar, ushort permission, List permissions)
{
template.ParseRaw(templateVar, Functions.BuildPermissionsBox(permission, permissions));
}*/
protected void ParseRadioArray(string templateVar, string name, int columns, List items, string selectedItem)
{
template.ParseRaw(templateVar, Functions.BuildRadioArray(name, columns, items, selectedItem));
}
protected void ParseRadioArray(string templateVar, string name, int columns, List items, string selectedItem, List disabledItems)
{
template.ParseRaw(templateVar, Functions.BuildRadioArray(name, columns, items, selectedItem, disabledItems));
}
protected void ParseSelectBox(string templateVar, string name, List items, string selectedItem)
{
template.ParseRaw(templateVar, Functions.BuildSelectBox(name, items, selectedItem));
}
protected void ParseSelectBox(string templateVar, string name, List items, string selectedItem, List disabledItems)
{
template.ParseRaw(templateVar, Functions.BuildSelectBox(name, items, selectedItem, disabledItems));
}
protected void ParseSelectBox(string templateVar, string name, Dictionary items, string selectedItem)
{
template.ParseRaw(templateVar, Functions.BuildSelectBox(name, items, selectedItem));
}
protected void ParseSelectBox(string templateVar, string name, Dictionary items, string selectedItem, List disabledItems)
{
template.ParseRaw(templateVar, Functions.BuildSelectBox(name, items, selectedItem, disabledItems));
}
protected void ParseLicensingBox(string templateVar, byte selectedLicense)
{
template.ParseRaw(templateVar, ContentLicense.BuildLicenseSelectBox(core.Db, selectedLicense));
}
protected void ParseClassification(Core core, string templateVar, Classifications classification)
{
template.ParseRaw(templateVar, Classification.BuildClassificationBox(core, classification));
}
/*protected void ParseTimeZoneBox(string templateVar, string timeZone)
{
template.ParseRaw(templateVar, UnixTime.BuildTimeZoneSelectBox(timeZone));
}*/
protected Dictionary GetModeHiddenFieldList()
{
Dictionary hiddenFieldList = new Dictionary();
hiddenFieldList.Add("module", this.ModuleKey);
hiddenFieldList.Add("sub", this.Key);
hiddenFieldList.Add("mode", core.Http["mode"]);
return hiddenFieldList;
}
}
public class ModuleModeEventArgs : EventArgs
{
private string mode;
public string Mode
{
get
{
return mode;
}
}
public ModuleModeEventArgs(string mode)
{
this.mode = mode;
}
}
public class ItemModuleModeEventArgs : EventArgs
{
private string mode;
private NumberedItem item;
public string Mode
{
get
{
return mode;
}
}
public NumberedItem Item
{
get
{
return item;
}
}
public ItemModuleModeEventArgs(string mode, NumberedItem item)
{
this.mode = mode;
this.item = item;
}
}
}