forked from asxzy/Program-O
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspellcheck.php
More file actions
385 lines (327 loc) · 11.8 KB
/
spellcheck.php
File metadata and controls
385 lines (327 loc) · 11.8 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
/***************************************
* http://www.program-o.com
* PROGRAM O
* Version: 2.6.11
* FILE: spellcheck.php
* AUTHOR: Elizabeth Perreau and Dave Morton
* DATE: 12-09-2014
* DETAILS: Displays the admin page for the spellcheck plugin and provides access to various features
***************************************/
require_once(_LIB_PATH_ . 'misc_functions.php');
$msg = '';
$options = $allowed_pages['spellcheck'];
$form_vars = clean_inputs($options);
$group = (isset($form_vars['group'])) ? $form_vars['group'] : 1;
$content = $template->getSection('SearchSpellForm');
$sc_action = isset($form_vars['action']) ? strtolower($form_vars['action']) : '';
$sc_id = isset($form_vars['id']) ? $form_vars['id'] : -1;
if (!empty($sc_action))
{
switch ($sc_action)
{
case 'search':
$content .= runSpellSearch();
$content .= spellCheckForm();
break;
case 'update':
updateSpell();
$content .= spellCheckForm();
break;
case 'delete':
$content .= ($sc_id >= 0) ? delSpell($sc_id) . spellCheckForm() : spellCheckForm();
break;
case 'edit':
$content .= ($sc_id >= 0) ? editSpellForm($sc_id) : spellCheckForm();
break;
case 'add':
$x = insertSpell();
$content .= spellCheckForm();
break;
default:
$content .= spellCheckForm();
}
}
else {
$content .= spellCheckForm();
}
$content = str_replace('[group]', $group, $content);
$sc_enabled = (USE_SPELL_CHECKER) ? 'enabled' : 'disabled';
$topNav = $template->getSection('TopNav');
$leftNav = $template->getSection('LeftNav');
$main = $template->getSection('Main');
$navHeader = $template->getSection('NavHeader');
$rightNav = $template->getSection('RightNav');
$rightNavLinks = getMisspelledWords();
$FooterInfo = getFooter();
$errMsgClass = (!empty($msg)) ? "ShowError" : "HideError";
$errMsgStyle = $template->getSection($errMsgClass);
$noLeftNav = '';
$noTopNav = '';
$noRightNav = '';
$headerTitle = 'Actions:';
$pageTitle = 'My-Program O - Spellcheck Editor';
$mainContent = $content;
$mainTitle = 'Spellcheck Editor';
$mainContent = str_replace('[spellCheckForm]', spellCheckForm(), $mainContent);
$mainContent = str_replace('[sc_enabled]', $sc_enabled, $mainContent);
$rightNav = str_replace('[rightNavLinks]', $rightNavLinks, $rightNav);
$rightNav = str_replace('[navHeader]', $navHeader, $rightNav);
$rightNav = str_replace('[headerTitle]', scPaginate(), $rightNav);
/**
* Function scPaginate
*
* @return string
*/
function scPaginate()
{
global $group;
/** @noinspection SqlDialectInspection */
$sql = "SELECT COUNT(*) FROM `spellcheck`";
$row = db_fetch($sql,null, __FILE__, __FUNCTION__, __LINE__);
$rowCount = $row['COUNT(*)'];
$lastPage = intval($rowCount / 50);
$remainder = ($rowCount / 50) - $lastPage;
if ($remainder > 0)
{
$lastPage++;
}
$out = "Missspelled Words<br />\n50 words per page:<br />\n";
$link = " - <a class=\"paginate\" href=\"index.php?page=spellcheck&group=[group]\">[label]</a>";
$curStart = $group;
$firstPage = 1;
$prev = ($curStart > ($firstPage + 1)) ? $curStart - 1 : -1;
$next = ($lastPage > ($curStart + 1)) ? $curStart + 1 : -1;
$firstLink = ($firstPage != $curStart) ? str_replace('[group]', '1', $link) : '';
$prevLink = ($prev > 0) ? str_replace('[group]', $prev, $link) : '';
$curLink = "- $curStart ";
if (empty($firstLink) && empty($prevLink))
{
$curLink = $curStart;
}
$nextLink = ($next > 0) ? str_replace('[group]', $next, $link) : '';
$lastLink = ($lastPage > $curStart) ? str_replace('[group]', $lastPage, $link) : '';
$firstLink = str_replace('[label]', 'first', $firstLink);
$prevLink = str_replace('[label]', '<<', $prevLink);
$nextLink = str_replace('[label]', '>>', $nextLink);
$lastLink = str_replace('[label]', 'last', $lastLink);
$out .= ltrim("$firstLink\n$prevLink\n$curLink\n$nextLink\n$lastLink", " - ");
return $out;
}
/**
* Function getMisspelledWords
*
* @return string
*/
function getMisspelledWords()
{
global $template, $group, $form_vars;
# pagination variables
$_SESSION['poadmin']['group'] = $group;
$startEntry = abs(($group - 1) * 50);
$end = $group + 50;
$_SESSION['poadmin']['page_start'] = $group;
$curID = (isset($form_vars['id'])) ? $form_vars['id'] : -1;
/** @noinspection SqlDialectInspection */
$sql = "SELECT `id`,`missspelling` FROM `spellcheck` limit $startEntry, 50;";
$baseLink = $template->getSection('NavLink');
$links = ' <div class="userlist">' . "\n";
$result = db_fetchAll($sql,null, __FILE__, __FUNCTION__, __LINE__);
$count = 0;
foreach ($result as $row)
{
$linkId = $row['id'];
$linkClass = ($linkId == $curID) ? 'selected' : 'noClass';
$missspelling = $row['missspelling'];
$tmpLink = str_replace('[linkClass]', " class=\"$linkClass\"", $baseLink);
$linkHref = " href=\"index.php?page=spellcheck&action=edit&id=$linkId&group=$group#$linkId\" name=\"$linkId\"";
$tmpLink = str_replace('[linkHref]', $linkHref, $tmpLink);
$tmpLink = str_replace('[linkOnclick]', '', $tmpLink);
$tmpLink = str_replace('[linkTitle]', " title=\"Edit spelling correction for the word '$missspelling'\"", $tmpLink);
$tmpLink = str_replace('[linkLabel]', $missspelling, $tmpLink);
$links .= "$tmpLink\n";
$count++;
}
$page_count = intval($count / 50);
$_SESSION['poadmin']['page_count'] = $page_count + (($count / 50) > $page_count) ? 1 : 0;
$links .= "\n </div>\n";
return $links;
}
/**
* Function spellCheckForm
*
* @return mixed|string
*/
function spellCheckForm()
{
global $template, $group;
$out = $template->getSection('SpellcheckForm');
$out = str_replace('[group]', $group, $out);
return $out;
}
/**
* Function insertSpell
*
* @return string
*/
function insertSpell()
{
global $template, $msg, $form_vars;
$correction = trim($form_vars['correction']);
$missspell = trim($form_vars['missspell']);
if (($correction == "") || ($missspell == ""))
{
$msg = ' <div id="errMsg">You must enter a spelling mistake and the correction.</div>' . "\n";
}
else {
/** @noinspection SqlDialectInspection */
$sql = "INSERT INTO `spellcheck` VALUES (:missspell, :correction)";
$params = array(
':missspell' => $missspell,
':correction' => $correction
);
$affectedRows = db_write($sql, $params, false, __FILE__, __FUNCTION__, __LINE__);
if ($affectedRows > 0)
{
$msg = '<div id="successMsg">Correction added.</div>';
}
else {
$msg = '<div id="errMsg">There was a problem editing the correction - no changes made.</div>';
}
}
return $msg;
}
/**
* Function delSpell
*
* * @param $id
* @return void
*/
function delSpell($id)
{
global $template, $msg;
if ($id == "")
{
$msg = '<div id="errMsg">There was a problem editing the correction - no changes made.</div>';
}
else
{
/** @noinspection SqlDialectInspection */
$sql = "DELETE FROM spellcheck WHERE id = :id";
$params = array(':id' => $id);
$affectedRows = db_write($sql, $params, false, __FILE__, __FUNCTION__, __LINE__);
if ($affectedRows > 0)
{
$msg = '<div id="successMsg">Correction deleted.</div>';
}
else {
$msg = '<div id="errMsg">There was a problem editing the correction - no changes made.</div>';
}
}
}
/**
* Function runSpellSearch
*
* @return string
*/
function runSpellSearch()
{
global $template, $form_vars;
$i = 0;
$search = trim($form_vars['search']);
/** @noinspection SqlDialectInspection */
$sql = "SELECT * FROM `spellcheck` WHERE `missspelling` LIKE :search1 OR `correction` LIKE :search2 LIMIT 50";
$params = array(
':search1' => "%{$search}%",
':search2' => "%{$search}%",
);
$result = db_fetchAll($sql, $params, __FILE__, __FUNCTION__, __LINE__);
$htmltbl = '<table>
<thead>
<tr>
<th class="sortable">missspelling</th>
<th class="sortable">Correction</th>
<th>Action</th>
</tr>
</thead>
<tbody>';
foreach ($result as $row)
{
$i++;
$misspell = _strtoupper($row['missspelling']);
$correction = _strtoupper($row['correction']);
$id = $row['id'];
$group = round(($id / 50));
$action = "<a href=\"index.php?page=spellcheck&action=edit&id=$id&group=$group#$id\"><img src=\"images/edit.png\" border=0 width=\"15\" height=\"15\" alt=\"Edit this entry\" title=\"Edit this entry\" /></a>
<a href=\"index.php?page=spellcheck&action=delete&id=$id&group=$group#$id\" onclick=\"return confirm('Do you really want to delete this missspelling? You will not be able to undo this!')\";><img src=\"images/del.png\" border=0 width=\"15\" height=\"15\" alt=\"Delete this entry\" title=\"Delete this entry\" /></a>";
$htmltbl .= "<tr valign=top>
<td>$misspell</td>
<td>$correction</td>
<td align=center>$action</td>
</tr>";
}
$htmltbl .= "</tbody></table>";
if ($i >= 50) {
$msg = "Found more than 50 results for '<b>$search</b>', please refine your search further";
}
elseif ($i == 0) {
$msg = "Found 0 results for '<b>$search</b>'. You can use the form below to add that entry.";
$htmltbl = "";
}
else {
$msg = "Found $i results for '<b>$search</b>'";
}
$htmlresults = "<div id=\"pTitle\">$msg</div>" . $htmltbl;
return $htmlresults;
}
/**
* Function editSpellForm
*
* @param $id
* @return mixed|string
*/
function editSpellForm($id)
{
global $template, $group;
$form = $template->getSection('EditSpellForm');
/** @noinspection SqlDialectInspection */
$sql = "SELECT * FROM `spellcheck` WHERE `id` = :id";
$params = array(':id' => $id);
$row = db_fetch($sql, $params, __FILE__, __FUNCTION__, __LINE__);
$uc_missspelling = _strtoupper($row['missspelling']);
$uc_correction = _strtoupper($row['correction']);
$form = str_replace('[id]', $row['id'], $form);
$form = str_replace('[missspelling]', $uc_missspelling, $form);
$form = str_replace('[correction]', $uc_correction, $form);
$form = str_replace('[group]', $group, $form);
return $form;
}
function updateSpell()
{
global $template, $msg, $form_vars;
$missspelling = trim($form_vars['missspelling']);
$correction = trim($form_vars['correction']);
$id = trim($form_vars['id']);
if (($id == "") || ($missspelling == "") || ($correction == ""))
{
$msg = '<div id="errMsg">There was a problem editing the correction - no changes made.</div>';
}
else
{
/** @noinspection SqlDialectInspection */
$sql = "UPDATE `spellcheck` SET `missspelling` = :missspelling,`correction` = :correction WHERE `id` = :id";
$params = array(
':missspelling' => $missspelling,
':correction' => $correction,
':id' => $id
);
$affectedRows = db_write($sql, $params, false, __FILE__, __FUNCTION__, __LINE__);
if ($affectedRows > 0)
{
$msg = '<div id="successMsg">Correction edited.</div>';
}
else {
$msg = '<div id="errMsg">There was a problem editing the correction - no changes made.</div>';
}
}
}