6
|
1 |
<?php
|
|
2 |
/*
|
|
3 |
* Decir
|
|
4 |
* Version 0.1
|
|
5 |
* Copyright (C) 2007 Dan Fuhry
|
|
6 |
* admin_forums.php - forum creation and management frontend
|
|
7 |
*
|
|
8 |
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
|
|
9 |
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
|
|
10 |
*
|
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
|
|
12 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
|
|
13 |
*/
|
|
14 |
|
|
15 |
$decir_menu['DecirForums'] = 'Manage forums';
|
|
16 |
require( DECIR_ROOT . '/constants.php' );
|
|
17 |
|
|
18 |
function page_Admin_DecirForums()
|
|
19 |
{
|
|
20 |
global $db, $session, $paths, $template, $plugins; // Common objects
|
|
21 |
if ( $session->auth_level < USER_LEVEL_ADMIN || $session->user_level < USER_LEVEL_ADMIN )
|
|
22 |
{
|
|
23 |
echo '<h3>Error: Not authenticated</h3><p>It looks like your administration session is invalid or you are not authorized to access this administration page. Please <a href="' . makeUrlNS('Special', 'Login/' . $paths->nslist['Special'] . 'Administration', 'level=' . USER_LEVEL_ADMIN, true) . '">re-authenticate</a> to continue.</p>';
|
|
24 |
return;
|
|
25 |
}
|
|
26 |
|
|
27 |
$show_main_menu = true;
|
|
28 |
|
|
29 |
if ( isset($_POST['act']) )
|
|
30 |
{
|
|
31 |
switch ( $_POST['act'] )
|
|
32 |
{
|
|
33 |
case "create":
|
|
34 |
case "create_finish":
|
|
35 |
|
|
36 |
// Do we have any categories yet?
|
|
37 |
$q = $db->sql_query('SELECT forum_id, forum_name FROM ' . table_prefix . 'decir_forums WHERE forum_type = ' . FORUM_CATEGORY . ';');
|
|
38 |
if ( !$q )
|
|
39 |
$db->_die('Decir admin_forums.php retrieving category count');
|
|
40 |
$need_category = ( $db->numrows() < 1 );
|
|
41 |
$cats = array();
|
|
42 |
if ( !$need_category )
|
|
43 |
{
|
|
44 |
while ( list($cat_id, $cat_name) = $db->fetchrow_num() )
|
|
45 |
{
|
|
46 |
$cats[ $cat_id ] = $cat_name;
|
|
47 |
}
|
|
48 |
}
|
|
49 |
|
|
50 |
$db->free_result();
|
|
51 |
|
|
52 |
if ( $_POST['act'] == 'create_finish' )
|
|
53 |
{
|
|
54 |
$errors = array();
|
|
55 |
$forum_type = intval($_POST['forum_type']);
|
|
56 |
if ( $forum_type != FORUM_FORUM && $forum_type != FORUM_CATEGORY )
|
|
57 |
$errors[] = 'Invalid forum type. <tt>X.X</tt>';
|
|
58 |
$forum_name = trim($_POST['forum_name']);
|
|
59 |
if ( empty($forum_name) )
|
|
60 |
$errors[] = 'Please enter a name for this forum.';
|
|
61 |
$forum_desc = '';
|
|
62 |
$forum_parent = 0;
|
|
63 |
if ( $forum_type == FORUM_FORUM )
|
|
64 |
{
|
|
65 |
$forum_desc = trim($_POST['forum_desc']);
|
|
66 |
if ( empty($forum_desc) )
|
|
67 |
$errors[] = 'Please enter a description for this forum.';
|
|
68 |
$forum_parent = intval($_POST['forum_parent']);
|
|
69 |
if ( !isset($cats[$forum_parent]) )
|
|
70 |
$errors[] = 'Invalid parent category';
|
|
71 |
}
|
|
72 |
if ( count($errors) > 0 )
|
|
73 |
{
|
|
74 |
// Errors encountered - bounce form back to the user
|
|
75 |
$show_main_menu = false;
|
|
76 |
$form = new Decir_Admin_SmartForm_Forum(DECIR_ADMIN_MODE_CREATE);
|
|
77 |
$form->forum_name = $forum_name;
|
|
78 |
$form->forum_desc = $forum_desc;
|
|
79 |
$form->forum_type = $forum_type;
|
|
80 |
$form->need_category = $need_category;
|
|
81 |
$form->category_list = $cats;
|
|
82 |
echo $form->html();
|
|
83 |
break;
|
|
84 |
}
|
|
85 |
// All checks passed. Create forum.
|
|
86 |
$forum_name_db = $db->escape($forum_name);
|
|
87 |
$forum_desc_db = $db->escape($forum_desc);
|
|
88 |
$sql = 'INSERT INTO ' . table_prefix . "decir_forums(forum_name, forum_desc, forum_type, parent, num_topics, num_posts) VALUES\n"
|
|
89 |
. " ( '$forum_name_db', '$forum_desc_db', $forum_type, $forum_parent, 0, 0 );";
|
|
90 |
if ( $db->sql_query($sql) )
|
|
91 |
{
|
|
92 |
$forum_name = htmlspecialchars($forum_name);
|
|
93 |
$type = ( $forum_type == FORUM_FORUM ) ? 'forum' : 'category';
|
|
94 |
echo "<div class=\"info-box\">The {$type} \"{$forum_name}\" has been created successfully.</div>";
|
|
95 |
}
|
|
96 |
break;
|
|
97 |
}
|
|
98 |
// Create a smartform
|
|
99 |
$show_main_menu = false;
|
|
100 |
$form = new Decir_Admin_SmartForm_Forum(DECIR_ADMIN_MODE_CREATE);
|
|
101 |
$form->need_category = $need_category;
|
|
102 |
$form->category_list = $cats;
|
|
103 |
echo $form->html();
|
|
104 |
break;
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
if ( $show_main_menu )
|
|
109 |
{
|
|
110 |
// Display the main forum admin interface
|
|
111 |
$form_url = makeUrlNS('Special', 'DecirAdmin', "module={$paths->nslist['Admin']}DecirForums", true);
|
|
112 |
echo "<form action=\"$form_url\" method=\"post\" enctype=\"multipart/form-data\">";
|
|
113 |
echo '<div class="tblholder">
|
|
114 |
<table border="0" cellspacing="1" cellpadding="4">
|
|
115 |
<tr>
|
|
116 |
<th colspan="4">Forum administration</th>
|
|
117 |
</tr>';
|
|
118 |
// Select and display all forums
|
|
119 |
$q = $db->sql_unbuffered_query('SELECT forum_id, forum_name, forum_type FROM ' . table_prefix . 'decir_forums ORDER BY ( forum_type = ' . FORUM_CATEGORY . ' ) DESC, forum_order;');
|
|
120 |
|
|
121 |
if ( !$q )
|
|
122 |
$db->_die('Decir admin_forums.php selecting main forum datum');
|
|
123 |
|
|
124 |
if ( $row = $db->fetchrow() )
|
|
125 |
{
|
|
126 |
do
|
|
127 |
{
|
|
128 |
}
|
|
129 |
while ( $row = $db->fetchrow() );
|
|
130 |
}
|
|
131 |
else
|
|
132 |
{
|
|
133 |
echo '<td colspan="4" class="row3">There are no forums on this board.</td>';
|
|
134 |
}
|
|
135 |
|
|
136 |
// Create forum button
|
|
137 |
echo ' <tr>
|
|
138 |
<th class="subhead">
|
|
139 |
<button name="act" value="create">Create new forum</button>
|
|
140 |
</th>
|
|
141 |
</tr>';
|
|
142 |
|
|
143 |
echo ' </table>
|
|
144 |
</div>';
|
|
145 |
echo "</form>";
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* Smart form for creating and editing Decir forums.
|
|
151 |
* @package Decir
|
|
152 |
* @subpackage Administration
|
|
153 |
* @copyright 2007 Dan Fuhry
|
|
154 |
* @license GPL
|
|
155 |
*/
|
|
156 |
|
|
157 |
class Decir_Admin_SmartForm_Forum
|
|
158 |
{
|
|
159 |
|
|
160 |
/**
|
|
161 |
* Whether we are creating or editing a forum.
|
|
162 |
* @var int
|
|
163 |
*/
|
|
164 |
|
|
165 |
var $form_mode;
|
|
166 |
|
|
167 |
/**
|
|
168 |
* The name of the forum - only used in edit mode.
|
|
169 |
* @var string
|
|
170 |
*/
|
|
171 |
|
|
172 |
var $forum_name = '';
|
|
173 |
|
|
174 |
/**
|
|
175 |
* The description of the forum - only used in edit mode.
|
|
176 |
* @var string
|
|
177 |
*/
|
|
178 |
|
|
179 |
var $forum_desc = '';
|
|
180 |
|
|
181 |
/**
|
|
182 |
* The type of entry this is (forum or category)
|
|
183 |
* @var int
|
|
184 |
*/
|
|
185 |
|
|
186 |
var $forum_type = -1;
|
|
187 |
|
|
188 |
/**
|
|
189 |
* Track if we need to make the user create a category as opposed to a forum.
|
|
190 |
* @var bool
|
|
191 |
*/
|
|
192 |
|
|
193 |
var $need_category = false;
|
|
194 |
|
|
195 |
/**
|
|
196 |
* The list of categories on the site.
|
|
197 |
* @var array
|
|
198 |
*/
|
|
199 |
|
|
200 |
var $category_list = array();
|
|
201 |
|
|
202 |
/**
|
|
203 |
* Instance ID for javascripting
|
|
204 |
* @var string
|
|
205 |
*/
|
|
206 |
|
|
207 |
var $instance_id;
|
|
208 |
|
|
209 |
/**
|
|
210 |
* Constructor
|
|
211 |
* @param int Form type - should be DECIR_ADMIN_MODE_CREATE or DECIR_ADMIN_MODE_EDIT
|
|
212 |
*/
|
|
213 |
|
|
214 |
function __construct($form_mode)
|
|
215 |
{
|
|
216 |
global $db, $session, $paths, $template, $plugins; // Common objects
|
|
217 |
$form_mode = intval($form_mode);
|
|
218 |
if ( $form_mode != DECIR_ADMIN_MODE_CREATE && $form_mode != DECIR_ADMIN_MODE_EDIT )
|
|
219 |
die('Syntax error: $form_mode to Decir_Admin_SmartForm_Forum::__construct should be DECIR_ADMIN_MODE_CREATE or DECIR_ADMIN_MODE_EDIT.');
|
|
220 |
|
|
221 |
$this->form_mode = $form_mode;
|
|
222 |
$this->instance_id = $session->dss_rand();
|
|
223 |
}
|
|
224 |
|
|
225 |
/**
|
|
226 |
* PHP4 compatibility constructor.
|
|
227 |
* @see Decir_Admin_SmartForm_Forum::__construct
|
|
228 |
*/
|
|
229 |
|
|
230 |
function Decir_Admin_SmartForm_Forum($form_type)
|
|
231 |
{
|
|
232 |
$this->__construct($form_type);
|
|
233 |
}
|
|
234 |
|
|
235 |
/**
|
|
236 |
* Render the form into HTML.
|
|
237 |
* @return string
|
|
238 |
*/
|
|
239 |
|
|
240 |
function html()
|
|
241 |
{
|
|
242 |
global $db, $session, $paths, $template, $plugins; // Common objects
|
|
243 |
$f_f = FORUM_FORUM;
|
|
244 |
$f_c = FORUM_CATEGORY;
|
|
245 |
$tpl_code = <<<EOF
|
|
246 |
<!-- Start forum creation/edit smartform {INSTANCE_ID} -->
|
|
247 |
|
|
248 |
<script type="text/javascript">
|
|
249 |
|
|
250 |
function set_form_type_category_{INSTANCE_ID}()
|
|
251 |
{
|
|
252 |
document.getElementById('type_category_{INSTANCE_ID}').style.display = 'block';
|
|
253 |
document.getElementById('type_forum_{INSTANCE_ID}').style.display = 'none';
|
|
254 |
}
|
|
255 |
|
|
256 |
function set_form_type_forum_{INSTANCE_ID}()
|
|
257 |
{
|
|
258 |
document.getElementById('type_category_{INSTANCE_ID}').style.display = 'none';
|
|
259 |
document.getElementById('type_forum_{INSTANCE_ID}').style.display = 'block';
|
|
260 |
}
|
|
261 |
var set_form_type_auto_{INSTANCE_ID} = function()
|
|
262 |
{
|
|
263 |
if ( document.getElementById('radio_forum_{INSTANCE_ID}').checked )
|
|
264 |
{
|
|
265 |
set_form_type_forum_{INSTANCE_ID}();
|
|
266 |
}
|
|
267 |
else if ( document.getElementById('radio_category_{INSTANCE_ID}').checked )
|
|
268 |
{
|
|
269 |
set_form_type_category_{INSTANCE_ID}();
|
|
270 |
}
|
|
271 |
}
|
|
272 |
|
|
273 |
addOnloadHook(set_form_type_auto_{INSTANCE_ID});
|
|
274 |
|
|
275 |
</script>
|
|
276 |
|
|
277 |
<form action="{FORM_ACTION}" name="decir_forum_smartform_{INSTANCE_ID}" method="post" enctype="multipart/form-data">
|
|
278 |
|
|
279 |
<div class="tblholder">
|
|
280 |
<table border="0" cellspacing="1" cellpadding="4">
|
|
281 |
<tr>
|
|
282 |
<th colspan="2">
|
|
283 |
<!-- BEGIN mode_is_create -->
|
|
284 |
Create new forum
|
|
285 |
<!-- BEGINELSE mode_is_create -->
|
|
286 |
Edit forum {FORUM_NAME}
|
|
287 |
<!-- END mode_is_create -->
|
|
288 |
</th>
|
|
289 |
</tr>
|
|
290 |
<!-- BEGIN mode_is_create -->
|
|
291 |
<tr>
|
|
292 |
<td class="row2" style="width: 50%;">
|
|
293 |
Forum type:
|
|
294 |
</td>
|
|
295 |
<td class="row1" style="width: 50%;">
|
|
296 |
<label>
|
|
297 |
<input id="radio_forum_{INSTANCE_ID}" type="radio" name="forum_type" value="{TYPE_FORUM}" onclick="set_form_type_forum_{INSTANCE_ID}();" <!-- BEGIN type_is_forum -->checked="checked" <!-- END type_is_forum -->/> Forum
|
|
298 |
</label>
|
|
299 |
<label>
|
|
300 |
<input id="radio_category_{INSTANCE_ID}" type="radio" name="forum_type" value="{TYPE_CATEGORY}" onclick="set_form_type_category_{INSTANCE_ID}();" <!-- BEGINNOT type_is_forum -->checked="checked" <!-- END type_is_forum -->/> Category
|
|
301 |
</label>
|
|
302 |
</td>
|
|
303 |
</tr>
|
|
304 |
<!-- END mode_is_create -->
|
|
305 |
<tr>
|
|
306 |
<td class="row2" style="width: 50%;">
|
|
307 |
Forum description:
|
|
308 |
</td>
|
|
309 |
<td class="row1" style="width: 50%;">
|
|
310 |
<input type="text" name="forum_name" size="40" value="{FORUM_NAME}" />
|
|
311 |
</td>
|
|
312 |
</tr>
|
|
313 |
</table>
|
|
314 |
</div>
|
|
315 |
|
|
316 |
<!-- BEGIN show_opts_category -->
|
|
317 |
<div class="tblholder" id="type_category_{INSTANCE_ID}"<!-- BEGIN mode_is_create --> style="display: none;" <!-- END mode_is_create -->>
|
|
318 |
<table border="0" cellspacing="1" cellpadding="4">
|
|
319 |
<tr>
|
|
320 |
<th colspan="2">
|
|
321 |
Category options
|
|
322 |
</th>
|
|
323 |
</tr>
|
|
324 |
<tr>
|
|
325 |
<td class="row2" style="width: 50%;">
|
|
326 |
Stub
|
|
327 |
</td>
|
|
328 |
<td class="row1" style="width: 50%;">
|
|
329 |
Stub
|
|
330 |
</td>
|
|
331 |
</tr>
|
|
332 |
<tr>
|
|
333 |
<th class="subhead" colspan="2">
|
|
334 |
<button name="act" value="create_finish"><b>Create category</b></button>
|
|
335 |
<button name="act" value="noop" style="font-weight: normal;">Cancel</button>
|
|
336 |
</th>
|
|
337 |
</tr>
|
|
338 |
</table>
|
|
339 |
</div>
|
|
340 |
<!-- END show_opts_category -->
|
|
341 |
|
|
342 |
<!-- BEGIN show_opts_forum -->
|
|
343 |
<div id="type_forum_{INSTANCE_ID}">
|
|
344 |
|
|
345 |
<!-- BEGIN need_category -->
|
|
346 |
|
|
347 |
<div class="error-box">
|
|
348 |
There aren't any categories on this site yet. You need to create at least one category before you can create a forum.
|
|
349 |
</div>
|
|
350 |
|
|
351 |
<!-- BEGINELSE need_category -->
|
|
352 |
|
|
353 |
<div class="tblholder">
|
|
354 |
<table border="0" cellspacing="1" cellpadding="4">
|
|
355 |
<tr>
|
|
356 |
<th colspan="2">
|
|
357 |
Forum options
|
|
358 |
</th>
|
|
359 |
</tr>
|
|
360 |
<tr>
|
|
361 |
<td class="row2" style="width: 50%;">
|
|
362 |
Forum description:
|
|
363 |
</td>
|
|
364 |
<td class="row1" style="width: 50%;">
|
|
365 |
<input type="text" name="forum_desc" size="40" value="{FORUM_DESC}" />
|
|
366 |
</td>
|
|
367 |
</tr>
|
|
368 |
<tr>
|
|
369 |
<td class="row2">
|
|
370 |
Create in category:
|
|
371 |
</td>
|
|
372 |
<td class="row1">
|
|
373 |
<select name="forum_parent">
|
|
374 |
{CATEGORY_LIST}</select>
|
|
375 |
</td>
|
|
376 |
</tr>
|
|
377 |
<tr>
|
|
378 |
<th class="subhead" colspan="2">
|
|
379 |
<button name="act" value="create_finish"><b>Create forum</b></button>
|
|
380 |
<button name="act" value="noop" style="font-weight: normal;">Cancel</button>
|
|
381 |
</th>
|
|
382 |
</tr>
|
|
383 |
</table>
|
|
384 |
</div>
|
|
385 |
|
|
386 |
<!-- END need_category -->
|
|
387 |
|
|
388 |
</div>
|
|
389 |
<!-- END show_opts_forum -->
|
|
390 |
|
|
391 |
</form>
|
|
392 |
|
|
393 |
<!-- Finish forum creation/edit smartform {INSTANCE_ID} -->
|
|
394 |
EOF;
|
|
395 |
$parser = $template->makeParserText($tpl_code);
|
|
396 |
|
|
397 |
$category_list = '';
|
|
398 |
foreach ( $this->category_list as $cat_id => $cat_name )
|
|
399 |
{
|
|
400 |
$cat_id = intval($cat_id);
|
|
401 |
$cat_name = htmlspecialchars($cat_name);
|
|
402 |
$category_list .= "<option value=\"$cat_id\">$cat_name</option>\n ";
|
|
403 |
}
|
|
404 |
|
|
405 |
// FIXME: these should really call addslashes and htmlspecialchars
|
|
406 |
|
|
407 |
$parser->assign_vars(array(
|
|
408 |
'INSTANCE_ID' => $this->instance_id,
|
|
409 |
'FORUM_NAME' => htmlspecialchars($this->forum_name),
|
|
410 |
'FORUM_DESC' => htmlspecialchars($this->forum_desc),
|
|
411 |
'FORM_ACTION' => makeUrlNS('Special', 'DecirAdmin', 'module=' . $paths->nslist['Admin'] . 'DecirForums', true),
|
|
412 |
'TYPE_FORUM' => FORUM_FORUM,
|
|
413 |
'TYPE_CATEGORY' => FORUM_CATEGORY,
|
|
414 |
'CATEGORY_LIST' => $category_list
|
|
415 |
));
|
|
416 |
$parser->assign_bool(array(
|
|
417 |
'mode_is_create' => ( $this->form_mode == DECIR_ADMIN_MODE_CREATE ),
|
|
418 |
'show_opts_category' => ( $this->form_mode == DECIR_ADMIN_MODE_CREATE ? true : $this->forum_type == FORUM_CATEGORY ),
|
|
419 |
'show_opts_forum' => ( $this->form_mode == DECIR_ADMIN_MODE_CREATE ? true : $this->forum_type == FORUM_FORUM ),
|
|
420 |
'type_is_forum' => ( $this->forum_type != FORUM_CATEGORY ),
|
|
421 |
'need_category' => ( $this->form_mode == DECIR_ADMIN_MODE_CREATE && $this->need_category )
|
|
422 |
));
|
|
423 |
|
|
424 |
return $parser->run();
|
|
425 |
}
|
|
426 |
|
|
427 |
}
|
|
428 |
|
|
429 |
?>
|