投票的Webpart
时间:2011-03-02 来源:昕友
public class AEPoll : WebPart
{
protected string _EndDate;
protected DateTime _InstallDate;
protected string _LicenseKey = "";
protected bool _PieChart = true;
protected string _PollAnswers = "";
protected string _PollAnswersStyle = "";
protected string _PollQuestion = "";
protected string _PollQuestionStyle = "";
protected int _PollWidth = 200;
protected bool _Published = true;
protected string _SubmitText = "Submit";
private bool alreadyVoted;
private Button ButtonSubmit;
private RadioButtonList choices;
public static string cookieName = "";
private ImageButton imgButSubmit;
private bool imgButton;
private bool justVoted;
public static string licenseInfo = "";
private string lit = "";
public static string pollFilename = "";
private string pollResult = "";
private int selectedIndex = -1;
private bool trace;
private RequiredFieldValidator validator;
private bool wpEditing;
private void ButtonSubmit_Click(object sender, EventArgs e)
{
SPSecurity.CodeToRunElevated secureCode = null;
if (this.trace)
{
this.lit = this.lit + "<br>ButtonSubmit";
}
try
{
if (((int) HttpContext.Current.Session[cookieName]) != 0)
{
if (this.trace)
{
this.lit = this.lit + "<br>already voted!";
}
return;
}
}
catch (Exception exception)
{
if (this.trace)
{
this.lit = this.lit + "<br>Session Cookie error:" + exception.Message;
}
}
try
{
this.selectedIndex = this.choices.SelectedIndex;
if (this.selectedIndex > -1)
{
if (this.trace)
{
this.lit = this.lit + "<br>selected Index=" + this.selectedIndex;
}
char[] separator = new char[] { ';' };
string[] strArray = this._PollAnswers.Split(separator);
string pollResult = this.pollResult;
if (pollResult == "")
{
for (int j = 0; j <= (strArray.Length - 1); j++)
{
if (j != 0)
{
pollResult = pollResult + ";";
}
pollResult = pollResult + "0";
}
if (this.trace)
{
this.lit = this.lit + "<br>Init=" + pollResult;
}
}
string[] strArray2 = pollResult.Split(separator);
if (this.trace)
{
this.lit = this.lit + "<br>previous Result=" + pollResult;
}
strArray2[this.selectedIndex] = (int.Parse(strArray2[this.selectedIndex]) + 1).ToString();
string str2 = "";
for (int i = 0; i <= (strArray2.Length - 1); i++)
{
if (i != 0)
{
str2 = str2 + ";";
}
str2 = str2 + strArray2[i];
}
if (this.trace)
{
this.lit = this.lit + "<br>updated Result=" + str2;
}
this.pollResult = str2;
if (secureCode == null)
{
secureCode = delegate {
StreamWriter writer = new StreamWriter(pollFilename);
writer.WriteLine(this.pollResult);
writer.Flush();
writer.Close();
};
}
SPSecurity.RunWithElevatedPrivileges(secureCode);
try
{
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Values["Voted"] = this.selectedIndex.ToString();
cookie.Values["Created"] = DateTime.Now.AddSeconds(5.0).ToString();
cookie.Expires = DateTime.Now.AddDays(300.0);
HttpContext.Current.Response.Cookies.Add(cookie);
}
catch (Exception exception2)
{
this.lit = this.lit + "<br>Cookie Set Error CreateChildControls:" + exception2.Message;
}
this.justVoted = true;
HttpContext.Current.Session[cookieName] = 1;
}
}
catch (Exception exception3)
{
this.lit = this.lit + "<br>Error submit:" + exception3.Message;
}
}
protected override void CreateChildControls()
{
if (!string.IsNullOrEmpty(this._PollQuestion))
{
if (this._SubmitText == "trace")
{
this.trace = true;
}
if (this.trace)
{
this.lit = this.lit + "<br>CreateChildControls";
}
string str = "";
if (!string.IsNullOrEmpty(this.ID))
{
string str2 = this.ID.ToString();
if (this.trace)
{
this.lit = this.lit + "<br>WebPartID=" + str2;
}
int length = 0x10;
if (str2.Length < 0x12)
{
length = str2.Length - 2;
}
if (length <= 0)
{
cookieName = "AEPollxxxx";
this.lit = this.lit + "<br>Web Part ID not found!";
}
else
{
cookieName = "AEPoll" + str2.Substring(2, length);
str = str2.Substring(2, length);
}
}
if (this.trace)
{
this.lit = this.lit + "<br>Cookie Name=" + cookieName;
}
if (this.trace)
{
this.lit = this.lit + "<br>Poll ID=" + str;
}
string str3 = ConfigurationSettings.AppSettings["AEQP_PollFile"];
if (string.IsNullOrEmpty(str3))
{
str3 = HttpContext.Current.Server.MapPath(@"~\_layouts");
}
pollFilename = str3 + @"\" + cookieName + ".txt";
if (this.trace)
{
this.lit = this.lit + "<br>PollFile=" + pollFilename;
}
DateTime maxValue = DateTime.MaxValue;
DateTime minValue = DateTime.MinValue;
try
{
StreamReader reader = new StreamReader(pollFilename);
string str4 = reader.ReadToEnd().Replace(Environment.NewLine, "");
if (this.trace)
{
this.lit = this.lit + "<br>Poll Result=" + str4;
}
reader.Close();
maxValue = File.GetCreationTime(pollFilename);
this.pollResult = str4;
}
catch (Exception exception)
{
if (this.trace)
{
this.lit = this.lit + "<br>PollFile not found:" + exception.Message;
}
}
if (this.trace)
{
this.lit = this.lit + "<br>Server Time=" + DateTime.Now.ToString();
}
if (this.trace)
{
this.lit = this.lit + "<br>Poll Created on=" + maxValue;
}
try
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
if (cookie != null)
{
minValue = DateTime.Parse(cookie.Values["Created"]);
if (this.trace)
{
this.lit = this.lit + "<br>Cookie Created on=" + minValue;
}
if (this.trace)
{
this.lit = this.lit + "<br>Cookie Value=" + cookie.Values["Voted"];
}
}
}
catch (Exception exception2)
{
this.lit = this.lit + "<br>Cookie Read Error:" + exception2.Message;
minValue = DateTime.MaxValue;
}
this.alreadyVoted = maxValue < minValue;
if (this.trace)
{
this.lit = this.lit + "<br>already Voted=" + this.alreadyVoted;
}
try
{
this.imgButton = (this._SubmitText.EndsWith(".jpg") || this._SubmitText.EndsWith(".gif")) || this._SubmitText.EndsWith(".png");
if (this.imgButton)
{
this.imgButSubmit = new ImageButton();
this.imgButSubmit.ID = "PollSubmit";
this.imgButSubmit.ImageUrl = this._SubmitText;
this.imgButSubmit.Click += new ImageClickEventHandler(this.ButtonSubmit_Click);
this.Controls.Add(this.imgButSubmit);
if (this.trace)
{
this.lit = this.lit + "<br>Image Button=" + this._SubmitText;
}
}
else
{
this.ButtonSubmit = new Button();
this.ButtonSubmit.Text = this._SubmitText;
this.ButtonSubmit.CssClass = "UserButton";
this.ButtonSubmit.Click += new EventHandler(this.ButtonSubmit_Click);
this.Controls.Add(this.ButtonSubmit);
}
this.choices = new RadioButtonList();
this.choices.CssClass = "AEQPradio";
if (!string.IsNullOrEmpty(this._PollAnswers))
{
char[] separator = new char[] { ';' };
foreach (string str5 in this._PollAnswers.Split(separator))
{
if (str5 != string.Empty)
{
string text = str5;
if (!string.IsNullOrEmpty(this._PollAnswersStyle))
{
text = "<span style='" + this._PollAnswersStyle + "'>" + str5 + "</span>";
}
this.choices.Items.Add(new ListItem(text, str5));
}
}
this.choices.ID = "choiceID" + str;
this.validator = new RequiredFieldValidator();
this.validator.ValidationGroup = "pollGroup" + str;
this.validator.ErrorMessage = "please choose an answer!";
this.validator.ControlToValidate = this.choices.ID;
this.validator.Display = ValidatorDisplay.Dynamic;
this.validator.EnableClientScript = true;
this.Controls.Add(this.validator);
}
this.Controls.Add(this.choices);
}
catch (Exception exception3)
{
this.lit = this.lit + "<br>Error CreateChildControls:" + exception3.Message;
}
}
}
public override EditorPartCollection CreateEditorParts()
{
ArrayList editorParts = new ArrayList();
MyEditor editor = new MyEditor();
editor.ID = this.ID + "_editorPart";
editorParts.Add(editor);
return new EditorPartCollection(editorParts);
}
protected override void OnPreRender(EventArgs e)
{
bool flag = false;
try
{
WebPartManager currentWebPartManager = WebPartManager.GetCurrentWebPartManager(this.Page);
flag = currentWebPartManager != null;
if (flag && (currentWebPartManager.DisplayMode == WebPartManager.EditDisplayMode))
{
this.wpEditing = true;
}
if (this.trace)
{
this.lit = this.lit + "<br>wpEditor=" + this.wpEditing;
}
}
catch (Exception exception)
{
this.lit = this.lit + "Web Part Manager:" + exception.Message;
}
if (this.InstallDate == DateTime.MinValue)
{
this.InstallDate = DateTime.Now;
if (flag)
{
base.SetPersonalizationDirty();
}
}
base.OnPreRender(e);
this.Hidden = !this._Published;
}
protected override void RenderContents(HtmlTextWriter writer)
{
string str = "";
int num = 0;
string str2 = this._LicenseKey;
if (string.IsNullOrEmpty(str2))
{
str2 = ConfigurationSettings.AppSettings["AEQP"];
}
if (string.IsNullOrEmpty(str2))
{
str2 = ConfigurationSettings.AppSettings["AEWB"];
}
if (string.IsNullOrEmpty(str2))
{
try
{
string str3 = HttpContext.Current.Server.MapPath(@"~\_layouts");
int length = str3.LastIndexOf(@"\");
StreamReader reader = new StreamReader(str3.Substring(0, length) + @"\Features\AEPollWebpart\License.txt");
str2 = reader.ReadToEnd();
reader.Close();
}
catch (Exception exception)
{
if (this.trace)
{
this.lit = this.lit + "<br>License File Read Error:" + exception.Message;
}
}
}
if (!string.IsNullOrEmpty(str2))
{
try
{
str = "Invalid License Key";
string[] strArray = str2.Split(new char[] { '-' });
if (((strArray[0] == "AEQP") && (strArray[1] == "2")) || (strArray[0] == "AEWB"))
{
int num3 = int.Parse(strArray[2]);
int num4 = int.Parse(strArray[3]);
int num5 = (num3 + num4) % 0x63;
int num6 = int.Parse(strArray[4]);
if (num5 == int.Parse(strArray[5]))
{
DateTime time = new DateTime(0x79f, 10, 0x11);
TimeSpan span = (TimeSpan) (DateTime.Now - time);
int days = span.Days;
if (days <= num4)
{
str = "";
num = 4;
if (num6 == 11)
{
str = "Evaluation copy:" + (num4 - days) + " days left";
}
}
else
{
str = "Trial License has expired!";
}
}
}
}
catch
{
str = "License Key Error";
}
}
if (num == 0)
{
str = str + "<br><b><a href='http://www.amrein.com/apps/page.asp?Q=5755'><font color=#cc0000>Evaluation Version</font></a></b> (© 2010 <a href='http://www.amrein.com'>AMREIN ENGINEERING AG</a>)";
}
this.EnsureChildControls();
bool flag = false;
if (!string.IsNullOrEmpty(this._EndDate))
{
DateTime time2;
if (!DateTime.TryParse(this._EndDate, out time2))
{
writer.Write("Poll Expiration Date Format Error: " + this._EndDate);
return;
}
flag = DateTime.Now > time2;
}
try
{
if (string.IsNullOrEmpty(this._PollQuestion) || string.IsNullOrEmpty(this._PollAnswers))
{
this.lit = this.lit + "No Poll Question and/or Answers defined yet";
}
else if ((this.justVoted || this.alreadyVoted) || flag)
{
if (!string.IsNullOrEmpty(this._PollQuestionStyle))
{
this.lit = this.lit + "<span style='" + this._PollQuestionStyle + "'>";
}
this.lit = this.lit + this._PollQuestion;
if (!string.IsNullOrEmpty(this._PollQuestionStyle))
{
this.lit = this.lit + "</span>";
}
this.lit = this.lit + "<br/><br/>";
char[] separator = new char[] { ';' };
string[] strArray2 = this.pollResult.Split(separator);
string[] strArray3 = this._PollAnswers.Split(separator);
string str5 = string.Concat(new object[] { "?chs=", this._PollWidth, "x", (strArray2.Length * 0x12) + (this._PollWidth / 2), "&cht=p&chts=000000,12&chco=80C65A,224499,FF0000&chd=t:" });
int num8 = 0;
for (int i = 0; i <= (strArray2.Length - 1); i++)
{
if (i > 0)
{
str5 = str5 + ",";
}
str5 = str5 + strArray2[i];
num8 += int.Parse(strArray2[i]);
}
if (this._PieChart)
{
string str6 = "";
for (int j = 0; j <= (strArray2.Length - 1); j++)
{
if (j > 0)
{
str6 = str6 + "|";
}
string str12 = str6;
string[] strArray4 = new string[] { str12, strArray2[j], " (", ((int.Parse(strArray2[j]) * 100) / num8).ToString(), "%)" };
str6 = string.Concat(strArray4);
}
str5 = (str5 + "&chl=" + str6) + "&chdlp=bv&chdl=";
for (int k = 0; k <= (strArray3.Length - 1); k++)
{
if (k > 0)
{
str5 = str5 + "|";
}
string str7 = "0";
if (k < strArray2.Length)
{
str7 = strArray2[k];
}
string str13 = str5;
str5 = str13 + strArray3[k].Replace("'", "%27") + " (" + str7 + ")";
}
this.lit = this.lit + "<img src='http://chart.apis.google.com/chart" + str5 + "' alt='Poll result chart' /><br/>";
}
else
{
string webResourceUrl = ConfigurationSettings.AppSettings["AEQP_ResultBar"];
if (string.IsNullOrEmpty(webResourceUrl))
{
webResourceUrl = this.Page.ClientScript.GetWebResourceUrl(base.GetType(), "AEPollWebpart.Bar7.jpg");
}
string str9 = ConfigurationSettings.AppSettings["AEQP_ResultBarPad"];
if (string.IsNullOrEmpty(str9))
{
str9 = this.Page.ClientScript.GetWebResourceUrl(base.GetType(), "AEPollWebpart.Bar0.gif");
}
string str10 = ConfigurationSettings.AppSettings["AEQP_ResultBarHeight"];
if (string.IsNullOrEmpty(str10))
{
str10 = "5";
}
object lit = this.lit;
this.lit = string.Concat(new object[] { lit, "<div style='width:", this._PollWidth, "px'>" });
this.lit = this.lit + "<table border=0 CELLPADDING=1 CELLSPACING=0>";
for (int m = 0; m <= (strArray3.Length - 1); m++)
{
string s = "0";
if (m < strArray2.Length)
{
s = strArray2[m];
}
int num13 = (int.Parse(s) * 100) / num8;
this.lit = this.lit + "<tr><td width=83% VALIGN=middle style='padding-top:5px'>" + strArray3[m] + "<br/>";
object obj3 = this.lit;
this.lit = string.Concat(new object[] { obj3, "<img src='", webResourceUrl, "' width='", (num13 * this._PollWidth) / 100, "' height='", str10, "' border='0' vspace=1>" });
object obj4 = this.lit;
this.lit = string.Concat(new object[] { obj4, "<img src='", str9, "' width='", ((100 - num13) * this._PollWidth) / 100, "' height='", str10, "' border='0' vspace=1>" });
object obj5 = this.lit;
this.lit = string.Concat(new object[] { obj5, "</td><td VALIGN=bottom WIDTH=9% ALIGN=RIGHT><FONT color=#008000>", num13, "%</FONT></td>" });
this.lit = this.lit + "<td VALIGN=bottom WIDTH=8% ALIGN=RIGHT><FONT color=#00B000>(" + s + ")</FONT></td></tr>";
}
this.lit = this.lit + "</table></div>";
}
this.lit = this.lit + "<br/><font color=gray>Total Votes: " + num8.ToString() + "</font>";
}
else
{
try
{
HttpContext.Current.Session[cookieName] = 0;
}
catch
{
writer.Write("Error: Session State must be enabled!<br>");
}
if (!string.IsNullOrEmpty(this._PollQuestionStyle))
{
writer.Write("<span style='" + this._PollQuestionStyle + "'>");
}
writer.Write(this._PollQuestion);
if (!string.IsNullOrEmpty(this._PollQuestionStyle))
{
writer.Write("</span>");
}
writer.Write("<br/>");
this.choices.RenderControl(writer);
if (!this.wpEditing)
{
this.validator.RenderControl(writer);
}
writer.Write("<hr style='color:#707070; <div style='float:right'>");
if (this.imgButton)
{
this.imgButSubmit.RenderControl(writer);
}
else
{
this.ButtonSubmit.RenderControl(writer);
}
writer.Write("</div>");
}
writer.Write(this.lit);
if (str != "")
{
writer.Write("<br>" + str);
}
}
catch (Exception exception2)
{
writer.Write("Render Error: " + exception2.Message);
}
}
[Personalizable, WebBrowsable(true), WebDisplayName("Poll Expiration Date"), WebDescription("Poll expiration date (Format: mm/dd/yyyy hh:mm:ss PM)")]
public string EndDate
{
get
{
return this._EndDate;
}
set
{
this._EndDate = value;
}
}
[Personalizable, WebBrowsable(false), WebDisplayName("Install Date")]
public DateTime InstallDate
{
get
{
return this._InstallDate;
}
set
{
this._InstallDate = value;
}
}
[Personalizable, WebBrowsable(true), WebDisplayName("License Key"), WebDescription("Enter your Product License Key")]
public string LicenseKey
{
get
{
return this._LicenseKey;
}
set
{
this._LicenseKey = value;
}
}
[WebDisplayName("Show Results as Pie Chart"), WebBrowsable(true), Personalizable, WebDescription("Toggle between Google Pie Chart and Bar Chart")]
public bool PieChart
{
get
{
return this._PieChart;
}
set
{
this._PieChart = value;
}
}
[WebBrowsable(true), WebDisplayName("Poll Answers"), Personalizable, WebDescription("Enter the poll answers separated by semincolons")]
public string PollAnswers
{
get
{
return this._PollAnswers;
}
set
{
this._PollAnswers = value;
}
}
[Personalizable, WebBrowsable(true), WebDisplayName("Answers CSS Style"), WebDescription("Enter the optional custom CSS style(s) to format the poll answers")]
public string PollAnswersStyle
{
get
{
return this._PollAnswersStyle;
}
set
{
this._PollAnswersStyle = value;
}
}
[WebDescription("Enter the poll question"), Personalizable, WebBrowsable(true), WebDisplayName("Poll Question")]
public string PollQuestion
{
get
{
return this._PollQuestion;
}
set
{
this._PollQuestion = value;
}
}
[WebBrowsable(true), WebDisplayName("Question CSS Style"), WebDescription("Enter the optional custom CSS style(s) to format the poll question"), Personalizable]
public string PollQuestionStyle
{
get
{
return this._PollQuestionStyle;
}
set
{
this._PollQuestionStyle = value;
}
}
[Personalizable, WebBrowsable(true), WebDisplayName("Poll Width"), WebDescription("Poll width in pixels")]
public int PollWidth
{
get
{
return this._PollWidth;
}
set
{
this._PollWidth = value;
}
}
[WebBrowsable(true), WebDisplayName("Published"), WebDescription("Show/Hide Poll Web Part"), Personalizable]
public bool Published
{
get
{
return this._Published;
}
set
{
this._Published = value;
}
}
[Personalizable, WebBrowsable(true), WebDescription("Enter the text displayed in the Submit button"), WebDisplayName("Submit Button Text")]
public string SubmitText
{
get
{
return this._SubmitText;
}
set
{
this._SubmitText = value;
}
}
}
}