-
Notifications
You must be signed in to change notification settings - Fork 2
/
edit_page.php
214 lines (192 loc) · 8.21 KB
/
edit_page.php
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php require_once("includes/session.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php confirm_logged_in(); ?>
<?php require_once("includes/connection.php"); ?>
<?php //find_selected_page();?>
<?php
if (intval($_GET["page"]) == 0)
{
redirect_to("content.php");
}
if (isset($_POST["submit"]))
{
$errors = array();
//Form validation
$required_fields = array("menu_name", "subject", "position", "visible", "content");
foreach ($required_fields as $fieldname)
{
if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && !is_numeric($_POST[$fieldname])))
{
$errors[] = $fieldname;
}
}
$fields_with_lengths = array("menu_name" => 30, "content" => 80);
foreach ($fields_with_lengths as $fieldname => $maxlength)
{
if (strlen(trim(mysql_prep($_POST["$fieldname"]))) > $maxlength)
{
$errors[] = $fieldname;
}
}
if (empty($errors))
{
//redirect_to("new_subject.php");
//Perform update
$id = mysql_prep($_GET["page"]);
$menu_name = mysql_prep($_POST["menu_name"]);
$subject_id = mysql_prep($_POST["subject"]);
$position = mysql_prep($_POST["position"]);
$visible = mysql_prep($_POST["visible"]);
$page_content = mysql_prep($_POST["content"]);
$query = "UPDATE pages SET
menu_name='{$menu_name}',
subject_id = {$subject_id},
position = {$position},
visible = {$visible},
content='{$page_content}'
WHERE id = {$id}";
$result = mysql_query($query, $connection);
if (mysql_affected_rows() == 1)
{
// Success
$message = "The page was successfully updated";
}
else
{
// Failed
$message = "The page update failed.";
$message .= "<br/>" . mysql_error();
}
}
else
{
//Errors occurred
$message = "There were " . count($errors) . " errors in the form.";
}
} //End: if (isset($_POST["submit"]))
?>
<?php find_selected_page();?> //This moved to here because can't be fetched before update
<?php include("includes/header.php"); ?>
<table id="structure">
<tr>
<td id="navigation">
<?php echo navigation($sel_subject, $sel_page_content); ?>
</td>
<td id="page">
<h2>Edit Page: <?php echo $sel_page_content["menu_name"]; ?></h2>
<?php
if (!empty($message))
{
echo "<p class=\"message\">" . $message . "</p>";
}
?>
<?php
//Output a list of the fields that had errors
if (!empty($errors))
{
echo "<p class=\"errors\">";
echo "Please review the following fields:<br/>";
foreach($errors as $error)
{
echo " - " . $error . "<br/>";
}
echo "</p>";
}
?>
<form action="edit_page.php?page=<?php echo urlencode($sel_page_content["id"]); ?>" method="POST">
<p>Page name: <input type="text" name="menu_name" value="<?php
// if (isset($_POST["menu_name"]))
// {
// echo $_POST["menu_name"];
// }
// else
// {
echo $sel_page_content["menu_name"];
// }
?>" id="menu_name"/></p>
<p>Subject:
<select name="subject">
<?php
// $subject_set = get_subject_by_id($sel_page_content["subject_id"]);
// $subject_count = mysql_num_rows($subject_set);
//
// for ($count = 0; $count < $subject_count; $count++)
// {
// $subject_record = mysql_fetch_array($subject_set);
// $subj_id = intval($subject_record["id"]);
// echo "<option value=\"{$subject_record["id"]}\">{$subject_record["menu_name"]}</option>";
// }
$subject_set = get_all_subjects();
$subject_count = mysql_num_rows($subject_set);
for ($count = 0; $count < $subject_count; $count++)
{
$subject_record = mysql_fetch_array($subject_set);
$subj_id = intval($subject_record["id"]);
echo "<option ";
if ($sel_page_content["subject_id"] == $subj_id)
{
echo "selected=\"selected\" ";
}
echo "value=\"{$subject_record["id"]}\">{$subject_record["menu_name"]}</option>";
}
?>
</select>
</p>
<p>Position:
<select name="position" >
<?php
$page_set = get_pages_for_subject($sel_page_content["subject_id"]);
$page_count = mysql_num_rows($page_set);
// $subject_count + 1 because we are adding something
for ($count = 1; $count <= $page_count + 1; $count++)
{
echo "<option ";
if ($sel_page_content["position"] == $count)
{
echo "selected=\"selected\" ";
}
echo "value=\"{$count}\">{$count}</option>";
}
?>
</select>
</p>
<p>Visible:
<input type="radio" name="visible" value="0" <?php
/*if (isset($_POST["visible"]) && $_POST["visible"] == 0)
{
echo "checked=\"checked\" ";
}
else*/if ($sel_page_content["visible"] == 0)
{
echo "checked=\"checked\" ";
}
?>/> No
<input type="radio" name="visible" value="1" <?php
/*if (isset($_POST["visible"]) && $_POST["visible"] == 1)
{
echo "checked=\"checked\" ";
}
else*/if ($sel_page_content["visible"] == 1)
{
echo "checked=\"checked\" ";
}
?>/> Yes
</p>
<p>Content:</p>
<textarea name="content" rows="4" cols="20">
<?php echo $sel_page_content["content"]; ?>
</textarea>
<br/>
<br/>
<br/>
<input type="submit" name="submit" value="Edit Page" />
<a href="delete_page.php?page=<?php echo urlencode($sel_page_content["id"]); ?>" onclick="return confirm('Are you sure?');">Delete Page</a>
</form>
<br/>
<a href="content.php">Cancel</a>
</td>
</tr>
</table>
<?php require("includes/footer.php"); ?>