forked from pbombnz/TheBadShopLog
/
Store_Farmers.aspx.cs
146 lines (140 loc) · 6.32 KB
/
Store_Farmers.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
public partial class Store_Farmers : System.Web.UI.Page
{
// Declares a static int that is used to define how many blog entries to show
private static int numberOfBlogsToShow = 0;
protected void Page_Load(object sender, EventArgs e)
{
// when the page loads, we check if it isnt a postback (which is first time load)
if (!IsPostBack)
{
//Just reset numberOfBlogsToShow to zero just incase
numberOfBlogsToShow = 0;
// show the next five blog entries
getNextFiveBlogsFromList();
}
}
protected void newBlogTextBox_TextChanged(object sender, EventArgs e)
{
// When the new blog text box has changed, we need to update the characters left label which is what the function below calculates
this.changeCharacterLeftLabel();
}
protected void sendBlogBtn_Click(object sender, EventArgs e)
{
// When the new blog text box has changed, we need to update the characters left label which is what the function below calculates
this.changeCharacterLeftLabel();
// Declares an int that calcualuates how many characters are left
int charsLeft = newBlogTextBox.MaxLength - newBlogTextBox.Text.Length;
// checks if charsLeft is within the character limit
if (charsLeft >= 0)
{
// if it is, we write the blog to the user entry file
// Declares a BlogReaderWriter object that allows to save to the user entry file
BlogReaderWriter blogWriter = new BlogReaderWriter(Path.GetFullPath(Server.MapPath("~\\files\\FarmersUserEntry.txt")));
// Declares a int32 variable that gets and stores the current unix time
Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
// Writes the new blog entry information to the new user entry file
blogWriter.writeBlogToEntryFile("Farmers", (String)Session["loginID"], unixTimestamp.ToString(), newBlogTextBox.Text);
// redirects the users to a success page to confirm that there blog entry is awaiting moderation
Response.Redirect("Post_Success.aspx");
}
}
private void changeCharacterLeftLabel()
{
// sets the charLeftLabel's text colour to black as a precautionary measure
charLeftLabel.ForeColor = Color.Black;
// declares an int that calculates the characters left from the text box and text box's character limit
int charsLeft = newBlogTextBox.MaxLength - newBlogTextBox.Text.Length;
// Sets the charLeftLabel to the int above (with other string components)
charLeftLabel.Text = "Characters Left: " + charsLeft.ToString();
// checks if the characters left value is above or equal to zero
if (charsLeft >= 0)
{
// if the above is true, then the blog entry experience string is a valid, therefore
// we change the text colour to green to signify that the input is an acceptable length
charLeftLabel.ForeColor = Color.ForestGreen;
}
else
{
// if the above is false, then the blog entry experience string is a invalid, therefore
// we change the text colour to red to signify that the input is an unacceptable length
charLeftLabel.ForeColor = Color.Red;
}
}
protected void showNextFiveBlogsBtn_Click(object sender, EventArgs e)
{
// if the showNextFiveBlogsBtn button is clicked, the users wants to view more blog entries
// therefore we execute the method below which will load the next 5 entries
this.getNextFiveBlogsFromList();
}
private void getNextFiveBlogsFromList()
{
//create a BlogReaderWriter object that is used to read the store's entry file
BlogReaderWriter blogReader = new BlogReaderWriter(Path.GetFullPath(Server.MapPath("~\\files\\FarmersUserEntry.txt")));
//create a an arraylist object that holds the the blog entries is a formatted string
ArrayList blogListData = blogReader.getBlogListInformation();
// clears the blogListTable
blogListTable.Rows.Clear();
// add 5 to numberOfBlogsToShow (so it will show the next 5 blogs)
numberOfBlogsToShow += 5;
// check if the above actually goes over the length of the blog entries
if (numberOfBlogsToShow > blogListData.Count)
{
// if it does, set it to the maximum possible number (which will be the number of elements in the arraylist)
numberOfBlogsToShow = blogListData.Count;
}
// Declares a temporary int counter that is used for the loop (that initalised with numberOfBlogsToShow)
// This declares how many blogs will be shown
int numberOfBlogsToShowCounter = numberOfBlogsToShow;
// loop until numberOfBlogsToShowCounter is zero
while (numberOfBlogsToShowCounter > 0)
{
// create a new table row
TableRow tr = new TableRow();
// create a new table cell
TableCell tc = new TableCell();
// set the cell's text to the arraylist's first element
tc.Text = (String)blogListData[0];
// remove the arraylist's first element as its no longer needed
blogListData.RemoveAt(0);
// Add the cell to the table row
tr.Cells.Add(tc);
// add the table row to the table
blogListTable.Rows.Add(tr);
// decrease the counter by one
numberOfBlogsToShowCounter--;
}
}
protected void addBlogBtn_Click(object sender, EventArgs e)
{
// checks if the user is logged in
// checks if the "isLoggedIn" session key exists
if (Session["isLoggedIn"] != null)
{
// checks if the user is logged in
if ((bool)Session["isLoggedIn"])
{
// if the user is, then set the add blog section of the site to be visible so they add a blog entry
addBlogSection.Visible = true;
}
else
{
// if they are not logged in, redirect them to the login screen so they can post blog entries
Response.Redirect("Login.aspx");
}
}
else
{
// if the key doesn't exist, they are not logged in, redirect them to the login screen so they can post blog entries
Response.Redirect("Login.aspx");
}
}
}