/
editorpage.php
315 lines (295 loc) · 14.1 KB
/
editorpage.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
ob_start();
session_start();
function redirectHome() {
header('Location: index.php');
}
include 'util/loginCheck.php';
include('util/articleUtils.php');
include('util/userUtils.php');
include('util/db.php');
if (!$loggedin || ($_SESSION['usertype'] == 'U')) {
header("HTTP/1.1 403 Forbidden", true, 403);
echo "You must be an administrator. Redirecting in 1 second...";
echo '<meta http-equiv="refresh" content="1; url=/index.php">';
exit;
}
if(isset($_GET['articleid'])) {
/* Get articleid if the URL paramater exists */
$articleData = getArticleByID($_GET['articleid'], $db);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>The Poacher | Editor Page</title>
<link rel="stylesheet" href="res/css/editorpage.css"/>
<link rel="stylesheet" href="res/css/tools.css"/>
<link rel="stylesheet" href="res/css/quill.snow.css"/>
<?php include 'includes/globalHead.html'?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Include the Quill librarys -->
<!-- <link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet"> -->
<script src="https://cdn.quilljs.com/1.3.6/quill.js"></script>
<!-- Inclusion for Select2 jQuery replacement for default select statement -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
</head>
<body>
<?php
include 'includes/header.php';
include 'includes/nav.php';
include 'includes/toolsNav.php';
?>
<div class="editor-tab">
<button class="tablinks" onclick="editorTab(event, 'article');"><i class="fas fa-pencil-alt"></i></button>
<button class="tablinks" onclick="editorTab(event, 'picture');"><i class="fas fa-camera"></i></button>
<button class="tablinks" onclick="editorTab(event, 'save');getInfo();"><i class="fas fa-save"></i></button>
</div>
<form id="action-form" action="/submitArticle.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="action">
<div id="article" class="tabcontent">
<?php
$isDraft = ($articleData['IsDraft'] == 1 && $articleData['IsSubmitted'] == 0 ? TRUE : FALSE);
$isAuthor = ($articleData['UserID'] == $_SESSION['userid'] ? TRUE : FALSE);
$draftOk = TRUE;
if($articleData || isset($_GET['articleid'])) {
if(!$isAuthor || !$isDraft) {
redirectHome();
$draftOk = FALSE;
} else {
/* Grab ArticleID via POST method */
print "<input type=hidden name=article_id value={$articleData['ArticleID']}>";
$draftOk = TRUE;
}
}
?>
<input type="text" id="title" placeholder="Title" name="title" value="<?php if($draftOk){print $articleData['Headline'];}?>" required/>
<select name="category" id="category" class="js-categories-dropdown">
<option value="Politics"<?php if($articleData['Category'] == "Politics"){print "selected";}?>>Politics</option>
<option value="Sports"<?php if($articleData['Category'] == "Sports"){print "selected";}?>>Sports</option>
<option value="Entertainment"<?php if($articleData['Category'] == "Entertainment"){print "selected";}?>>Entertainment</option>
<option value="Video"<?php if($articleData['Category'] == "Video"){print "selected";}?>>Video</option>
<option value="Local"<?php if($articleData['Category'] == "Local"){print "selected";}?>>Local</option>
<option value="Opinion"<?php if($articleData['Category'] == "Opinion"){print "selected";}?>>Opinion</option>
</select>
<!-- Article Tags Select2 Dropdown -->
<select class="js-tags-dropdown" name="tags" multiple="multiple">
<?php
// Runs qeury to pull Tags from Tag table
$sql = "SELECT TagName FROM Tag ORDER BY TagName ASC";
$result = $db->query($sql);
// Populates select statement with individual Tag options
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<option value="' . $row["TagName"] . '">' . $row["TagName"] . '</option>';
}
} else {
echo "0 results";
}
php?>
</select>
<!-- EDITOR -->
<div id="editor" style="height: 800px;"><?php if($articleData['Body'])print decodeArticleBodyFormatting($articleData['Body']);?></div>
</div>
<div id="picture" class="tabcontent">
<h3>Choose a picture</h3>
<div class="editor-image">
<div id="image-container">
<div id="image-wrapper">
<div id="image-box">
<?php
$hashed_subdir = hash_hmac('md5', $articleData['UserID'], $articleData['PublishDate']);
$isDraft = ($articleData['IsDraft'] == 1 && $articleData['IsSubmitted'] == 0 ? TRUE : FALSE);
if($isDraft) {
print "<img id=image src=https://poachernews.com/res/img/articlePictures/{$hashed_subdir}/".$articleData['ArticleImage']." alt=Image width=250 height=250/><br>";
} else {
print "<img id=image src=https://poachernews.com/res/img/articlePictures/defaultArticleImage.png alt=Image width=250 height=250/><br>";
}
?>
<span id="image-path"><?php if($isDraft) print $articleData['ArticleImage'];?></span>
</div>
<label for="upload-image">Upload</label>
<?php
if($isDraft) {
print "<input id=upload-image type=file name=image onchange=getImagePath();uploadImage(this); accept=image/jpeg,image/png style=display:none; /> ";
} else {
print "<input id=upload-image type=file name=image onchange=getImagePath();uploadImage(this); accept=image/jpeg,image/png style=display:none; required/> ";
}
?>
</div>
</div>
</div>
</div>
<div id="save" class="tabcontent" onclick="getInfo();">
<h3>Save/Submit</h3>
<div class="get-info">
<h3>Article Info</h3>
<label>Title: </label>
<input type="text" id="getTitle" readonly/><br>
<label>Category: </label>
<input type="text" id="getCategory" readonly/><br>
<label>Tags: </label>
<input type="text" id="getTags" readonly/><br>
<!-- TODO: V 2.10
<label>Date: </label>
<input type="text" id="getDate" readonly>
<label>Picture: </label>
<input type="text" id="getImage" readonly>
-->
<input onclick="submitBtn();" type="button" id="submit-button" value="Submit"><br>
<div id="submit-draft" class="submit">
<div class="modal-content">
<span onclick="exitModal();" class="close">×</span>
<p>Are you sure you want to submit?</p>
<input type="submit" id="verify-submit" value="Yes" name="submit">
<input onclick="cancel();" type="button" id="cancel-submit" value="No" name="no-submit">
</div>
</div>
<!-- Saving Article -->
<input onclick="saveBtn();" type="submit" id="save-button" value="Save" name="save">
</div>
</div>
</form>
<script type="text/javascript">
/******************************* TABS *******************************/
/* Upload text editor */
var quill = new Quill('#editor', {
theme: 'snow',
});
function loadArticle() {
document.getElementById('article').style.display = "block";
}
window.onload = loadArticle();
function editorTab(evt, tabName) { // Tabs for article content, choosing a picture, and save/submit
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName('tabcontent');
for(i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none"
}
tablinks = document.getElementsByClassName('tablinks');
for(i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}
function getInfo() {
/* Gets info from the article and displays it in 'Article Info' */
var title = document.getElementById('title').value;
document.getElementById('getTitle').value = title;
var category = document.getElementById('category').value;
document.getElementById('getCategory').value = category;
var tags = $('.js-tags-dropdown').select2('data');
document.getElementById('getTags').value = '';
for (i = 0; i < tags.length; i++) {
document.getElementById('getTags').value += tags[i].text;
if(i != tags.length - 1) {
document.getElementById('getTags').value += ', ';
}
}
}
/******************************* ARTICLE TAGS SELECT2 *******************************/
$(document).ready(function() {
$('.js-tags-dropdown').select2({
placeholder: 'Select tags',
tags: true,
tokenSeparators: [',', ' '],
maximumSelectionLength: 7,
closeOnSelect: false
});
});
/******************************* ARTICLE CATEGORIES SELECT2 *******************************/
$(document).ready(function() {
$('.js-categories-dropdown').select2();
});
/******************************* EDITIOR COMPATIBILITY *******************************/
document.getElementById('editor').addEventListener('paste', function(event) { // All pasted text converted to default font
event.preventDefault();
var text = event.clipboardData.getData('text/plain');
document.execCommand('insertHTML', false, text);
});
/******************************* SUBMISSION *******************************/
$(document).ready(function() { // Submitting articles
$("#action-form").on("submit", function () {
var hvalue = $('.ql-editor').html();
$(this).append("<input type='hidden' name='body' value=' " + hvalue + " '/>");
});
});
var modal = document.getElementById('submit-draft');
function submitBtn() { // Submit button; Returns false if NOT successful otherise true
modal.style.display = "block";
}
function cancel() { // Cancels the submission of the article (No) button
modal.style.display = "none";
}
window.onclick = function(event) { // Close link via 'blur'-ing
if (event.target == modal) {
modal.style.display = "none";
}
}
function exitModal() {
var span = document.getElementsByClassName("close")[0];
modal.style.display = "none";
}
/******************************* FILE UPLOADING *******************************/
function uploadImage(event) {
/* Calls a PHP Ajax request to see if the image is in the correct format. If it isn't it will alert
an error otherwise display the image. */
var formData = new FormData();
formData.append('image', $('#upload-image')[0].files[0]);
$.ajax({
url: 'readImageFile.php',
type: 'POST',
data: formData,
success: function (output) {
switch(output) {
case 'falseType':
alert('Invalid file input. Please use .jpeg, .jpg, or .png files.');
document.getElementById('upload-image').value = null;
$('#image').attr('src', '');
break;
case 'falseSize':
alert('Invalid file size. Please try again.');
document.getElementById('upload-image').value = null;
$('#image').attr('src', '');
break;
default:
readURL(event);
break;
}
},
cache: false,
contentType: false,
processData: false
});
}
function getImagePath() {
var input = document.getElementById('upload-image');
var path = document.getElementById('image-path');
path.innerHTML = imageBasename(input.value);
}
function imageBasename(input) {
return input.split(/[\\/]/).pop();
}
function readURL(input) {
/* Reads in an image using FileReader then displays it in the image box. Removes image if contents are
not present. */
var upload_image = document.getElementById('upload-image').value;
if(!(upload_image.length == 0)) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(event) {
$('#image').attr('src', event.target.result);
}
reader.readAsDataURL(input.files[0]);
}
} else {
var defaultImage = 'https://poachernews.com/res/img/articlePictures/defaultArticleImage.png';
$('#image').attr('src', defaultImage);
}
}
$("#upload-image").change(function(){readURL(this);});
</script>
</body>
</html>