Skip to content

Commit

Permalink
Update forum module add last post box
Browse files Browse the repository at this point in the history
  • Loading branch information
kveldscholten committed Feb 6, 2016
1 parent 18a76f3 commit 99aa855
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 18 deletions.
40 changes: 40 additions & 0 deletions application/modules/forum/boxes/Forum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* @copyright Ilch 2.0
* @package ilch
*/

namespace Modules\Forum\Boxes;

use Modules\Forum\Mappers\Forum as ForumMapper;
use Modules\Forum\Mappers\Topic as TopicMapper;
use Modules\User\Mappers\User as UserMapper;

class Forum extends \Ilch\Box
{
public function render()
{
$forumMapper = new ForumMapper();
$topicMapper = new TopicMapper();
$userMapper = new UserMapper();

$groupIds = array(0);

if ($this->getUser()) {
$userId = $this->getUser()->getId();
$user = $userMapper->getUserById($userId);

$groupIds = array();
foreach ($user->getGroups() as $groups) {
$groupIds[] = $groups->getId();
}
}

$groupIdsArray = explode(',',implode(',', $groupIds));

$this->getView()->set('forumMapper', $forumMapper);
$this->getView()->set('topicMapper', $topicMapper);
$this->getView()->set('topics', $topicMapper->getTopics('', 5));
$this->getView()->set('groupIdsArray', $groupIdsArray);
}
}
39 changes: 39 additions & 0 deletions application/modules/forum/boxes/views/forum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
$forumMapper = $this->get('forumMapper');
$topicMapper = $this->get('topicMapper');
$groupIdsArray = $this->get('groupIdsArray');
$adminAccess = null;
if ($this->getUser()) {
$adminAccess = $this->getUser()->isAdmin();
}
?>

<?php if (!empty($this->get('topics'))): ?>
<ul class="list-unstyled">
<?php foreach ($this->get('topics') as $topic): ?>
<?php $forum = $forumMapper->getForumById($topic->getForumId()); ?>
<?php if (is_in_array($groupIdsArray, explode(',', $forum->getReadAccess())) || $adminAccess == true): ?>
<?php $lastPost = $topicMapper->getLastPostByTopicId($topic->getId()) ?>
<?php $date = new \Ilch\Date($lastPost->getDateCreated()); ?>
<li style="line-height: 25px;">
<?php if ($this->getUser()): ?>
<?php if (in_array($this->getUser()->getId(), explode(',', $lastPost->getRead()))): ?>
<img src="<?=$this->getStaticUrl('../application/modules/forum/static/img/forum_read.png') ?>" style="float: left; margin-top: 8px;">
<?php else: ?>
<img src="<?=$this->getStaticUrl('../application/modules/forum/static/img/topic_unread.png') ?>" style="float: left; margin-top: 8px;">
<?php endif; ?>
<?php else: ?>
<img src="<?=$this->getStaticUrl('../application/modules/forum/static/img/forum_read.png') ?>" style="float: left; margin-top: 8px;">
<?php endif; ?>
<a href="<?=$this->getUrl(array('module' => 'forum', 'controller' => 'showposts', 'action' => 'index', 'topicid' => $lastPost->getTopicId())) ?>">
<?=$topic->getTopicTitle() ?>
</a>
<br />
<small><?=$date->format("d.m.y - H:i", true) ?> <?=$this->getTrans('clock') ?></small>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php else: ?>
<?=$this->getTrans('noPosts') ?>
<?php endif; ?>
45 changes: 29 additions & 16 deletions application/modules/forum/mappers/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class Topic extends \Ilch\Mapper
public function getTopicsByForumId($id, $pagination = NULL)
{
$sql = 'SELECT SQL_CALC_FOUND_ROWS *
FROM `[prefix]_forum_topics`
WHERE forum_id = '.$id.'
GROUP by type, id
ORDER by type DESC, id DESC
LIMIT '.implode(',',$pagination->getLimit());
FROM `[prefix]_forum_topics`
WHERE forum_id = '.$id.'
GROUP by type, id
ORDER by type DESC, id DESC
LIMIT '.implode(',',$pagination->getLimit());

$fileArray = $this->db()->queryArray($sql);
$pagination->setRows($this->db()->querycell('SELECT FOUND_ROWS()'));
Expand All @@ -40,19 +40,27 @@ public function getTopicsByForumId($id, $pagination = NULL)
$entryModel->setDateCreated($entries['date_created']);
$entry[] = $entryModel;
}

return $entry;
}

public function getTopics($pagination = NULL)
public function getTopics($pagination = NULL, $limit = NULL)
{
$sql = 'SELECT SQL_CALC_FOUND_ROWS *
FROM `[prefix]_forum_topics`
GROUP by type, id
ORDER by type DESC, id DESC
LIMIT '.implode(',',$pagination->getLimit());
ORDER by type DESC, id DESC';
if ($pagination != null) {
$sql .= ' LIMIT '.implode(',',$pagination->getLimit());
} elseif ($limit != null) {
$sql .= ' LIMIT '.$limit;
}

$fileArray = $this->db()->queryArray($sql);
$pagination->setRows($this->db()->querycell('SELECT FOUND_ROWS()'));

if ($pagination != null) {
$pagination->setRows($this->db()->querycell('SELECT FOUND_ROWS()'));
}

$entry = array();

Expand All @@ -61,6 +69,7 @@ public function getTopics($pagination = NULL)
$userMapper = new UserMapper();
$entryModel->setId($entries['id']);
$entryModel->setText($entries['text']);
$entryModel->setForumId($entries['forum_id']);
$entryModel->setTopicId($entries['topic_id']);
$entryModel->setVisits($entries['visits']);
$entryModel->setType($entries['type']);
Expand All @@ -69,15 +78,17 @@ public function getTopics($pagination = NULL)
$entryModel->setDateCreated($entries['date_created']);
$entry[] = $entryModel;
}

return $entry;
}

public function getPostById($id)
{
$sql = 'SELECT *
FROM `[prefix]_forum_topics`
WHERE id = '.$id;
FROM `[prefix]_forum_topics`
WHERE id = '.$id;
$fileRow = $this->db()->queryRow($sql);

$entryModel = new TopicModel();
$userMapper = new UserMapper();
$entryModel->setId($fileRow['id']);
Expand All @@ -88,7 +99,6 @@ public function getPostById($id)
$entryModel->setAuthor($userMapper->getUserById($fileRow['creator_id']));
$entryModel->setDateCreated($fileRow['date_created']);


return $entryModel;
}

Expand All @@ -97,11 +107,13 @@ public function getLastPostByTopicId($id)
$sql = 'SELECT p.id, p.topic_id, p.date_created, p.user_id, p.read
FROM [prefix]_forum_posts as p
WHERE p.topic_id = '.$id.'
ORDER BY p.id DESC ';
ORDER BY p.date_created DESC';
$fileRow = $this->db()->queryRow($sql);

if (empty($fileRow)) {
return null;
}

$entryModel = new PostModel();
$userMapper = new UserMapper();
$forumMapper = new ForumMapper();
Expand Down Expand Up @@ -155,9 +167,9 @@ public function getLastInsertId()
public function getPostByTopicId($id, $pagination = NULL)
{
$sql = 'SELECT SQL_CALC_FOUND_ROWS *
FROM `[prefix]_forum_topics`
WHERE topic_id = '.$id.'
LIMIT '.implode(',',$pagination->getLimit());
FROM `[prefix]_forum_topics`
WHERE topic_id = '.$id.'
LIMIT '.implode(',',$pagination->getLimit());

$fileArray = $this->db()->queryArray($sql);
$pagination->setRows($this->db()->querycell('SELECT FOUND_ROWS()'));
Expand All @@ -172,6 +184,7 @@ public function getPostByTopicId($id, $pagination = NULL)
$entryModel->setTopicTitle($entries['topic_title']);
$entry[] = $entryModel;
}

return $entry;
}

Expand Down
2 changes: 1 addition & 1 deletion application/modules/forum/translations/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@
'no' => 'Nein',
'joined' => 'Dabei seit',
'showNewPosts' => 'Neue Beiträge',

'noPosts' => 'Keine Beiträge vorhanden',
);
2 changes: 1 addition & 1 deletion application/modules/forum/translations/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@
'no' => 'No',
'joined' => 'Joined',
'showNewPosts' => 'New posts',

'noPosts' => 'No posts available',
);

0 comments on commit 99aa855

Please sign in to comment.