|
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using FogCreek.FogBugz;
using FogCreek.FogBugz.Plugins;
using FogCreek.FogBugz.Plugins.Api;
using FogCreek.FogBugz.Database.Entity;
using FogCreek.FogBugz.Plugins.Entity;
using FogCreek.FogBugz.Plugins.Interfaces;
using FogCreek.FogBugz.UI.Dialog;
namespace TimeIntervalExample
{
public class TimeIntervalExample : Plugin, IPluginTimeIntervalJoin,
IPluginJS, IPluginDatabase, IPluginRawPageDisplay
{
public TimeIntervalExample(CPluginApi api) : base(api) { }
protected const string sPluginId = "Adam+TimeIntervalExample@fogcreek.com";
private const string CommentsTable = "TimeIntervalComment";
private const string IntervalColumn = "ixInterval";
private const string CommentColumn = "sComment";
public const int CommentColumnMaxLength = 255;
private const int PluginSchemaVer = 1;
#region IPluginTimeIntervalJoin Members
public string[] TimeIntervalJoinTables()
{
return new string[] { "TimeIntervalComment" };
}
#endregion
#region IPluginJS Members
// this js comes down on every page. I put this here instead of in the static js file
// because we need the pluginprefix for this plugin (which is based on when the plugin is
// installed. alternatively, we could just set a variable for it in JS-land and put the
// rest of these functions into the static js file
public CJSInfo JSInfo()
{
string fullAjaxPostUrl = api.Url.PluginRawPageUrl() +
String.Format("&{0}sComment=' + sComment + '&{0}action=postTimeIntervalComment&{0}actionToken={1}&{0}ixInterval=' + ixInterval",
api.PluginPrefix,
api.Security.GetActionToken("postTimeIntervalComment"));
string fullAjaxGetUrl = api.Url.PluginRawPageUrl() +
String.Format("&{0}action=getTimeIntervalComment&{0}actionToken={1}&{0}ixInterval=' + ixInterval",
api.PluginPrefix,
api.Security.GetActionToken("postTimeIntervalComment"));
CJSInfo jsInfo = new CJSInfo();
jsInfo.rgsStaticFiles = new string[] { "js/TimeIntervalExample.js" };
jsInfo.sInlineJS = @"
function postTimeIntervalComment()
{
var sComment = $('form#TimeIntervalExamplePluginForm input[name=comment]').val();
var ixInterval = $('form#TimeIntervalExamplePluginForm input[name=ixInterval]').val();
var url = '" + fullAjaxPostUrl + @";
jQuery.get(url, function(data) { postCallback(data); });
}
function getTimeIntervalComment(ixInterval)
{
var url = '" + fullAjaxGetUrl + @";
jQuery.get(url, function(data) { getCallback(data); });
}";
return jsInfo;
}
#endregion
#region IPluginDatabase Members
public CTable[] DatabaseSchema()
{
var timeIntervalComments = api.Database.NewTable(api.Database.PluginTableName(CommentsTable));
timeIntervalComments.sDesc = "Assigns Comments to TimeIntervals";
timeIntervalComments.AddAutoIncrementPrimaryKey("ixTimeIntervalComment");
timeIntervalComments.AddIntColumn(IntervalColumn, true, 0);
timeIntervalComments.AddVarcharColumn(CommentColumn, CommentColumnMaxLength, false);
return new[] { timeIntervalComments };
}
public int DatabaseSchemaVersion()
{
return PluginSchemaVer;
}
public void DatabaseUpgradeAfter(int ixVersionFrom, int ixVersionTo, CDatabaseUpgradeApi apiUpgrade)
{
if (ixVersionTo == 1)
{
// No action required. Don't create any data in the tables
}
}
public void DatabaseUpgradeBefore(int ixVersionFrom, int ixVersionTo, CDatabaseUpgradeApi apiUpgrade)
{
if (ixVersionTo == 1)
{
// No action required. Don't create any data in the tables
}
}
#endregion
#region IPluginRawPageDisplay Members
public string RawPageDisplay()
{
if (api.Request[api.AddPluginPrefix("action")] != null &&
Convert.ToString(api.Request[api.AddPluginPrefix("action")]) == "postTimeIntervalComment")
{
// make sure the request includes a valid action token
if ((api.Request[api.AddPluginPrefix("actionToken")] == null) ||
!api.Security.ValidateActionToken(api.Request[api.AddPluginPrefix("actionToken")],
"postTimeIntervalComment"))
{
return "failure: action token invalid";
}
else
{
int ixInterval = -1;
string sComment = "";
if (api.Request[api.AddPluginPrefix("ixInterval")] != null &&
Int32.TryParse(Convert.ToString(api.Request[api.AddPluginPrefix("ixInterval")]), out ixInterval))
{
if (api.Request[api.AddPluginPrefix("sComment")] != null)
sComment = api.Request[api.AddPluginPrefix("sComment")].ToString();
return PostComment(ixInterval, sComment);
}
else return "failure: invalid ixInterval";
}
}
else if (api.Request[api.AddPluginPrefix("action")] != null &&
Convert.ToString(api.Request[api.AddPluginPrefix("action")]) == "getTimeIntervalComment")
{
int ixInterval = -1;
if (api.Request[api.AddPluginPrefix("ixInterval")] != null &&
Int32.TryParse(Convert.ToString(api.Request[api.AddPluginPrefix("ixInterval")]), out ixInterval))
return GetComment(ixInterval);
else
return "failure: ixInterval invalid";
}
else return "failure: command not recognized";
}
public PermissionLevel RawPageVisibility()
{
return PermissionLevel.Normal;
}
#endregion
private string PostComment(int ixInterval, string sComment)
{
CTimeIntervalQuery query = api.TimeInterval.NewTimeIntervalQuery();
query.AddWhere("TimeInterval.ixInterval = @ixInterval");
query.SetParamInt("@ixInterval", ixInterval);
CTimeInterval[] rgTimeIntervals = query.List();
if (rgTimeIntervals.Length > 0)
{
CTimeInterval interval = rgTimeIntervals[0];
interval.SetPluginField(sPluginId, "sComment", sComment);
interval.Commit();
}
return "success: '" + sComment + "' recorded for interval #" + ixInterval;
}
private string GetComment(int ixInterval)
{
string sComment = "";
CTimeIntervalQuery query = api.TimeInterval.NewTimeIntervalQuery();
query.AddWhere("TimeInterval.ixInterval = @ixInterval");
query.SetParamInt("@ixInterval", ixInterval);
CTimeInterval[] rgTimeIntervals = query.List();
if (rgTimeIntervals.Length > 0)
{
object oComment = rgTimeIntervals[0].GetPluginField(sPluginId, "sComment");
if (oComment != null)
sComment = oComment.ToString();
}
return sComment;
}
}
}
|
Loading...