Slowly making my way through sorting out all the spaghetti code on pdo version and moving all the html to views so if any one has experience and wouldnt mind having a look
heres the controller for forum page (the sql will be moved to model classes when im happy with it)
and here is the view/template
its this part causing the headache if anyone has any ideas on how to move this out of the view, i was thinking of passing the $forums_arr array to a function but there might be a better way
heres the controller for forum page (the sql will be moved to model classes when im happy with it)
/**
* View Forum Index.
*/
public function index()
{
dbconn();
global $config;
// todo add to validation class
if (!$config["FORUMS_GUESTREAD"]) {
loggedinonly();
}
if ($_SESSION["forumbanned"] == "yes" || $_SESSION["view_forum"] == "no") {
Session::flashmsg('danger', Lang::T("FORUM_BANNED"), 1);
}
//Here we decide if the forums is on or off
if ($config["FORUMS"]) {
$themedir = $config['SITEURL'] . "/themes/" . ($_SESSION['stylesheet'] ?: $config['default_theme']) . "/forums/";
// Action: DEFAULT ACTION (VIEW FORUMS)
if (isset($_GET["catchup"])) {
catch_up();
}
// Action: SHOW MAIN FORUM INDEX
$forums_res = DB::run("SELECT forumcats.id AS fcid, forumcats.name AS fcname, forum_forums.* FROM forum_forums LEFT JOIN forumcats ON forumcats.id = forum_forums.category ORDER BY forumcats.sort, forum_forums.sort, forum_forums.name");
if ($forums_res->rowCount() == 0) {
Redirect::autolink1($config['SITEURL'] . '/index', 'danger', 'There is no Forums available');
}
//topic count and post counts
$postcount = number_format(get_row_count("forum_posts"));
$topiccount = number_format(get_row_count("forum_topics"));
$data = [
'mainquery' => $forums_res,
'themedir' => $themedir,
'postcount' => $postcount,
'topiccount' => $topiccount,
];
$this->view('forum/index', $data, true);
} else {
//HEY IF FORUMS ARE OFF, SHOW THIS FLASH MESSAGE
Session::flashmsg('warning', Lang::T("Unfortunatley the forums are not currently available."), 1);
}
}and here is the view/template
<div class="card">
<div class="card-header">
<?php echo Lang::T("Forum Home"); ?>
</div>
<div class="card-body">
<div>
<img src='$config[SITEURL]/images/forum/help.png' alt='' />
<a href='$config[SITEURL]/faq'><?php echo Lang::T("FORUM_FAQ"); ?></a>
<img src='<?php echo $config['SITEURL']; ?>/images/forum/search.png' alt='' />
<a href='<?php echo $config['SITEURL']; ?>/forums/search'><?php echo Lang::T("SEARCH"); ?></a>
<b><?php echo Lang::T("FORUM_CONTROL"); ?></b>
· <a href='<?php echo $config['SITEURL']; ?>/forums/viewunread'><?php echo Lang::T("FORUM_NEW_POSTS"); ?></a>
· <a href='<?php echo $config['SITEURL']; ?>/forums?catchup'><?php echo Lang::T("FORUM_MARK_READ"); ?></a>
</div><br>
<div><?php echo Lang::T("YOU_ARE_IN"); ?>: <a href='<?php echo $config['SITEURL']; ?>/forums'><?php echo Lang::T("FORUMS"); ?></a> <b style='vertical-align:middle'>/ index</b></div>
<?php
latestforumposts();
// MAIN LAYOUT
$fcid = 0;
while ($forums_arr = $data['mainquery']->fetch(PDO::FETCH_ASSOC)) {
if ($_SESSION['class'] < $forums_arr["minclassread"] && $forums_arr["guest_read"] == "no") {
continue;
}
if ($forums_arr['fcid'] != $fcid) {
?>
<div class='table'><table class='table table-striped'>
<thead><tr>
<th style='border: 1px solid black' align='left' colspan='2'><?php echo htmlspecialchars($forums_arr['fcname']); ?></th>
<th style='border: 1px solid black' width='37' align='right'>Topics</th>
<th style='border: 1px solid black' width='47' align='right'>Posts</th>
<th style='border: 1px solid black' align='right' width='180'>Last post</th>
</tr></thead><tbody>
<?php
$fcid = $forums_arr['fcid'];
}
$forumid = 0 + $forums_arr["id"];
$forumname = htmlspecialchars($forums_arr["name"]);
$forumdescription = htmlspecialchars($forums_arr["description"]);
$postcount = number_format(get_row_count("forum_posts", "WHERE topicid IN (SELECT id FROM forum_topics WHERE forumid=$forumid)"));
$topiccount = number_format(get_row_count("forum_topics", "WHERE forumid = $forumid"));
$lastpostid = get_forum_last_post($forumid);
// Get last post info
$post_res = DB::run("SELECT added,topicid,userid FROM forum_posts WHERE id=$lastpostid");
if ($post_res->rowCount() == 1) {
$post_arr = $post_res->fetch(PDO::FETCH_ASSOC) or showerror(Lang::T("ERROR"), "Bad forum last_post");
$lastposterid = $post_arr["userid"];
$lastpostdate = utc_to_tz($post_arr["added"]);
$lasttopicid = $post_arr["topicid"];
$user_res = DB::run("SELECT username FROM users WHERE id=$lastposterid");
$user_arr = $user_res->fetch(PDO::FETCH_ASSOC);
$lastposter = class_user_colour($user_arr["username"]);
$topic_res = DB::run("SELECT subject FROM forum_topics WHERE id=$lasttopicid");
$topic_arr = $topic_res->fetch(PDO::FETCH_ASSOC);
$lasttopic = stripslashes(htmlspecialchars($topic_arr['subject']));
//cut last topic
$latestleng = 10;
$lastpost = "<small><a href='$config[SITEURL]/forums/viewtopic&topicid=$lasttopicid&page=last#last'><b>" . CutName($lasttopic, $latestleng) . "</b></a> by <a href='$config[SITEURL]/profile?id=$lastposterid'><b>$lastposter</b></a><br />$lastpostdate</small>";
if ($_SESSION['loggedin'] == true) {
$a = DB::run("SELECT lastpostread FROM forum_readposts WHERE userid=$_SESSION[id] AND topicid=$lasttopicid")->fetch();
}
//define the images for new posts or not on index
if ($a && $a[0] == $lastpostid) {
$img = "folder";
} else {
$img = "folder_new";
}
} else {
$lastpost = "<span class='small'>No Posts</span>";
$img = "folder";
}
//following line is each forums display ?>
<tr>
<td style='border: 1px solid black' width='5%' >
<img src='<?php echo $data['themedir']; ?><?php echo $img; ?>.png' alt='' width='100% heigh= '100%' /></td>
<td style='border: 1px solid black' >
<a href='<?php echo $config['SITEURL']; ?>/forums/viewforum&forumid=<?php echo $forumid; ?>'><b><?php echo $forumname; ?></b></a><br>
<small>- <?php echo $forumdescription; ?></small></td>
<td style='border: 1px solid black' ><?php echo $topiccount; ?></td>
<td style='border: 1px solid black' ><?php echo $postcount; ?></td>
<td style='border: 1px solid black' ><b><?php echo $lastpost; ?></b></td></tr>
<?php } ?>
</tbody></table></div>
<table cellspacing='0' cellpadding='3'><tr valign='middle'>
<td><img src='<?php echo $data['themedir']; ?>folder_new.png' style='margin: 5px' alt='' /></td><td>New posts</td>
<td><img src='<?php echo $data['themedir']; ?>folder.png' style='margin: 5px' alt='' /></td><td>No New posts</td>;
<td><img src='<?php echo $data['themedir']; ?>folder_locked.png' style='margin: 5px' alt='' /></td><td><?php echo Lang::T("FORUMS_LOCKED"); ?> topic</td>
<td><img src='<?php echo $data['themedir']; ?>folder_sticky.png' style='margin: 5px' alt='' /></td><td><?php echo Lang::T("FORUMS_STICKY"); ?> topic</td>
</tr></table>
<center>Our members have made <?php echo $data[' $postcount']; ?> posts in <?php echo $data['topiccount']; ?> topics</center>
<?php insert_quick_jump_menu(); ?>
</div>
</div><br />its this part causing the headache if anyone has any ideas on how to move this out of the view, i was thinking of passing the $forums_arr array to a function but there might be a better way
$forumid = 0 + $forums_arr["id"];
$forumname = htmlspecialchars($forums_arr["name"]);
$forumdescription = htmlspecialchars($forums_arr["description"]);
$postcount = number_format(get_row_count("forum_posts", "WHERE topicid IN (SELECT id FROM forum_topics WHERE forumid=$forumid)"));
$topiccount = number_format(get_row_count("forum_topics", "WHERE forumid = $forumid"));
$lastpostid = get_forum_last_post($forumid);
// Get last post info
$post_res = DB::run("SELECT added,topicid,userid FROM forum_posts WHERE id=$lastpostid");
if ($post_res->rowCount() == 1) {
$post_arr = $post_res->fetch(PDO::FETCH_ASSOC) or showerror(Lang::T("ERROR"), "Bad forum last_post");
$lastposterid = $post_arr["userid"];
$lastpostdate = utc_to_tz($post_arr["added"]);
$lasttopicid = $post_arr["topicid"];
$user_res = DB::run("SELECT username FROM users WHERE id=$lastposterid");
$user_arr = $user_res->fetch(PDO::FETCH_ASSOC);
$lastposter = class_user_colour($user_arr["username"]);
$topic_res = DB::run("SELECT subject FROM forum_topics WHERE id=$lasttopicid");
$topic_arr = $topic_res->fetch(PDO::FETCH_ASSOC);
$lasttopic = stripslashes(htmlspecialchars($topic_arr['subject']));
//cut last topic
$latestleng = 10;
$lastpost = "<small><a href='$config[SITEURL]/forums/viewtopic&topicid=$lasttopicid&page=last#last'><b>" . CutName($lasttopic, $latestleng) . "</b></a> by <a href='$config[SITEURL]/profile?id=$lastposterid'><b>$lastposter</b></a><br />$lastpostdate</small>";
if ($_SESSION['loggedin'] == true) {
$a = DB::run("SELECT lastpostread FROM forum_readposts WHERE userid=$_SESSION[id] AND topicid=$lasttopicid")->fetch();
}
//define the images for new posts or not on index
if ($a && $a[0] == $lastpostid) {
$img = "folder";
} else {
$img = "folder_new";
}
} else {
$lastpost = "<span class='small'>No Posts</span>";
$img = "folder";
}
