ForumsTrait

This commit is contained in:
Visman 2017-04-03 17:24:16 +07:00
parent 1481a2d889
commit 7a9c7b6d94
3 changed files with 152 additions and 284 deletions

View file

@ -4,6 +4,8 @@ namespace ForkBB\Models\Pages;
class Forum extends Page
{
use ForumsTrait;
/**
* Имя шаблона
* @var string
@ -224,146 +226,4 @@ class Forum extends Page
: $this->c->Router->link('Forum', ['id' => $args['id'], 'name' => $fDesc[$args['id']]['forum_name']]);
return $this;
}
/**
* Получение данных по разделам
* @param int $parent
* @return array
*/
protected function getForumsData($parent = 0)
{
list($fTree, $fDesc, $fAsc) = $this->c->forums;
// раздел $parent не имеет подразделов для вывода или они не доступны
if (empty($fTree[$parent])) {
return [];
}
$user = $this->c->user;
// текущие данные по подразделам
$vars = [
':id' => $user->id,
':forums' => array_slice($fAsc[$parent], 1),
];
if ($user->isGuest) {
$stmt = $this->c->DB->query('SELECT id, forum_desc, moderators, num_topics, num_posts, last_post, last_post_id, last_poster, last_topic FROM ::forums WHERE id IN (?ai:forums)', $vars);
} else {
$stmt = $this->c->DB->query('SELECT f.id, f.forum_desc, f.moderators, f.num_topics, f.num_posts, f.last_post, f.last_post_id, f.last_poster, f.last_topic, mof.mf_last_visit FROM ::forums AS f LEFT JOIN ::mark_of_forum AS mof ON (mof.uid=?i:id AND f.id=mof.fid) WHERE f.id IN (?ai:forums)', $vars);
}
$forums = [];
while ($cur = $stmt->fetch()) {
$forums[$cur['id']] = $cur;
}
// поиск новых
$new = [];
if (! $user->isGuest) {
// предварительная проверка разделов
$max = max((int) $user->lastVisit, (int) $user->uMarkAllRead);
foreach ($forums as $id => $cur) {
$t = max($max, (int) $cur['mf_last_visit']);
if ($cur['last_post'] > $t) {
$new[$id] = $t;
}
}
// проверка по темам
if (! empty($new)) {
$vars = [
':id' => $user->id,
':forums' => $new,
':max' => $max,
];
$stmt = $this->c->DB->query('SELECT t.forum_id, t.id, t.last_post FROM ::topics AS t LEFT JOIN ::mark_of_topic AS mot ON (mot.uid=?i:id AND mot.tid=t.id) WHERE t.forum_id IN(?ai:forums) AND t.last_post>?i:max AND t.moved_to IS NULL AND (mot.mt_last_visit IS NULL OR t.last_post>mot.mt_last_visit)', $vars);
$tmp = [];
while ($cur = $stmt->fetch()) {
if ($cur['last_post']>$new[$cur['forum_id']]) {
$tmp[$cur['forum_id']] = true;
}
}
$new = $tmp;
}
}
$r = $this->c->Router;
// формированием таблицы разделов
$result = [];
foreach ($fTree[$parent] as $fId => $cur) {
// список подразделов
$subForums = [];
if (isset($fTree[$fId])) {
foreach ($fTree[$fId] as $f) {
$subForums[] = [
$r->link('Forum', [
'id' => $f['fid'],
'name' => $f['forum_name']
]),
$f['forum_name']
];
}
}
// модераторы
$moderators = [];
if (!empty($forums[$fId]['moderators'])) {
$mods = unserialize($forums[$fId]['moderators']);
foreach ($mods as $name => $id) {
if ($user->gViewUsers == '1') {
$moderators[] = [
$r->link('User', [
'id' => $id,
'name' => $name,
]),
$name
];
} else {
$moderators[] = $name;
}
}
}
// статистика по разделам
$numT = 0;
$numP = 0;
$time = 0;
$postId = 0;
$poster = '';
$topic = '';
$fnew = false;
foreach ($fAsc[$fId] as $id) {
$fnew = $fnew || isset($new[$id]);
$numT += $forums[$id]['num_topics'];
$numP += $forums[$id]['num_posts'];
if ($forums[$id]['last_post'] > $time) {
$time = $forums[$id]['last_post'];
$postId = $forums[$id]['last_post_id'];
$poster = $forums[$id]['last_poster'];
$topic = $forums[$id]['last_topic'];
}
}
$result[$cur['cid']]['name'] = $cur['cat_name'];
$result[$cur['cid']]['forums'][] = [
'fid' => $fId,
'forum_name' => $cur['forum_name'],
'forum_desc' => $forums[$fId]['forum_desc'],
'forum_link' => $r->link('Forum', [
'id' => $fId,
'name' => $cur['forum_name']
]),
'redirect_url' => $cur['redirect_url'],
'subforums' => $subForums,
'moderators' => $moderators,
'num_topics' => $numT,
'num_posts' => $numP,
'topics' => $this->number($numT),
'posts' => $this->number($numP),
'last_post' => $this->time($time),
'last_post_id' => $postId > 0 ? $r->link('ViewPost', ['id' => $postId]) : null,
'last_poster' => $poster,
'last_topic' => $topic,
'new' => $fnew,
];
}
return $result;
}
}

View file

@ -0,0 +1,148 @@
<?php
namespace ForkBB\Models\Pages;
trait ForumsTrait
{
/**
* Получение данных по разделам
* @param int $parent
* @return array
*/
protected function getForumsData($parent = 0)
{
list($fTree, $fDesc, $fAsc) = $this->c->forums;
// раздел $parent не имеет подразделов для вывода или они не доступны
if (empty($fTree[$parent])) {
return [];
}
$user = $this->c->user;
// текущие данные по подразделам
$vars = [
':id' => $user->id,
':forums' => array_slice($fAsc[$parent], 1),
];
if ($user->isGuest) {
$stmt = $this->c->DB->query('SELECT id, forum_desc, moderators, num_topics, num_posts, last_post, last_post_id, last_poster, last_topic FROM ::forums WHERE id IN (?ai:forums)', $vars);
} else {
$stmt = $this->c->DB->query('SELECT f.id, f.forum_desc, f.moderators, f.num_topics, f.num_posts, f.last_post, f.last_post_id, f.last_poster, f.last_topic, mof.mf_last_visit FROM ::forums AS f LEFT JOIN ::mark_of_forum AS mof ON (mof.uid=?i:id AND f.id=mof.fid) WHERE f.id IN (?ai:forums)', $vars);
}
$forums = [];
while ($cur = $stmt->fetch()) {
$forums[$cur['id']] = $cur;
}
// поиск новых
$new = [];
if (! $user->isGuest) {
// предварительная проверка разделов
$max = max((int) $user->lastVisit, (int) $user->uMarkAllRead);
foreach ($forums as $id => $cur) {
$t = max($max, (int) $cur['mf_last_visit']);
if ($cur['last_post'] > $t) {
$new[$id] = $t;
}
}
// проверка по темам
if (! empty($new)) {
$vars = [
':id' => $user->id,
':forums' => $new,
':max' => $max,
];
$stmt = $this->c->DB->query('SELECT t.forum_id, t.id, t.last_post FROM ::topics AS t LEFT JOIN ::mark_of_topic AS mot ON (mot.uid=?i:id AND mot.tid=t.id) WHERE t.forum_id IN(?ai:forums) AND t.last_post>?i:max AND t.moved_to IS NULL AND (mot.mt_last_visit IS NULL OR t.last_post>mot.mt_last_visit)', $vars);
$tmp = [];
while ($cur = $stmt->fetch()) {
if ($cur['last_post']>$new[$cur['forum_id']]) {
$tmp[$cur['forum_id']] = true;
}
}
$new = $tmp;
}
}
$r = $this->c->Router;
// формированием таблицы разделов
$result = [];
foreach ($fTree[$parent] as $fId => $cur) {
// список подразделов
$subForums = [];
if (isset($fTree[$fId])) {
foreach ($fTree[$fId] as $f) {
$subForums[] = [
$r->link('Forum', [
'id' => $f['fid'],
'name' => $f['forum_name']
]),
$f['forum_name']
];
}
}
// модераторы
$moderators = [];
if (!empty($forums[$fId]['moderators'])) {
$mods = unserialize($forums[$fId]['moderators']);
foreach ($mods as $name => $id) {
if ($user->gViewUsers == '1') {
$moderators[] = [
$r->link('User', [
'id' => $id,
'name' => $name,
]),
$name
];
} else {
$moderators[] = $name;
}
}
}
// статистика по разделам
$numT = 0;
$numP = 0;
$time = 0;
$postId = 0;
$poster = '';
$topic = '';
$fnew = false;
foreach ($fAsc[$fId] as $id) {
$fnew = $fnew || isset($new[$id]);
$numT += $forums[$id]['num_topics'];
$numP += $forums[$id]['num_posts'];
if ($forums[$id]['last_post'] > $time) {
$time = $forums[$id]['last_post'];
$postId = $forums[$id]['last_post_id'];
$poster = $forums[$id]['last_poster'];
$topic = $forums[$id]['last_topic'];
}
}
$result[$cur['cid']]['name'] = $cur['cat_name'];
$result[$cur['cid']]['forums'][] = [
'fid' => $fId,
'forum_name' => $cur['forum_name'],
'forum_desc' => $forums[$fId]['forum_desc'],
'forum_link' => $r->link('Forum', [
'id' => $fId,
'name' => $cur['forum_name']
]),
'redirect_url' => $cur['redirect_url'],
'subforums' => $subForums,
'moderators' => $moderators,
'num_topics' => $numT,
'num_posts' => $numP,
'topics' => $this->number($numT),
'posts' => $this->number($numP),
'last_post' => $this->time($time),
'last_post_id' => $postId > 0 ? $r->link('ViewPost', ['id' => $postId]) : null,
'last_poster' => $poster,
'last_topic' => $topic,
'new' => $fnew,
];
}
return $result;
}
}

View file

@ -4,6 +4,8 @@ namespace ForkBB\Models\Pages;
class Index extends Page
{
use ForumsTrait;
/**
* Имя шаблона
* @var string
@ -107,146 +109,4 @@ class Index extends Page
$this->data['forums'] = $this->getForumsData();
return $this;
}
/**
* Получение данных по разделам
* @param int $root
* @return array
*/
protected function getForumsData($root = 0)
{
list($fTree, $fDesc, $fAsc) = $this->c->forums;
// раздел $root не имеет подразделов для вывода или они не доступны
if (empty($fTree[$root])) {
return [];
}
$user = $this->c->user;
// текущие данные по подразделам
$vars = [
':id' => $user->id,
':forums' => array_slice($fAsc[$root], 1),
];
if ($user->isGuest) {
$stmt = $this->c->DB->query('SELECT id, forum_desc, moderators, num_topics, num_posts, last_post, last_post_id, last_poster, last_topic FROM ::forums WHERE id IN (?ai:forums)', $vars);
} else {
$stmt = $this->c->DB->query('SELECT f.id, f.forum_desc, f.moderators, f.num_topics, f.num_posts, f.last_post, f.last_post_id, f.last_poster, f.last_topic, mof.mf_last_visit FROM ::forums AS f LEFT JOIN ::mark_of_forum AS mof ON (mof.uid=?i:id AND f.id=mof.fid) WHERE f.id IN (?ai:forums)', $vars);
}
$forums = [];
while ($cur = $stmt->fetch()) {
$forums[$cur['id']] = $cur;
}
// поиск новых
$new = [];
if (! $user->isGuest) {
// предварительная проверка разделов
$max = max((int) $user->lastVisit, (int) $user->uMarkAllRead);
foreach ($forums as $id => $cur) {
$t = max($max, (int) $cur['mf_last_visit']);
if ($cur['last_post'] > $t) {
$new[$id] = $t;
}
}
// проверка по темам
if (! empty($new)) {
$vars = [
':id' => $user->id,
':forums' => $new,
':max' => $max,
];
$stmt = $this->c->DB->query('SELECT t.forum_id, t.id, t.last_post FROM ::topics AS t LEFT JOIN ::mark_of_topic AS mot ON (mot.uid=?i:id AND mot.tid=t.id) WHERE t.forum_id IN(?ai:forums) AND t.last_post>?i:max AND t.moved_to IS NULL AND (mot.mt_last_visit IS NULL OR t.last_post>mot.mt_last_visit)', $vars);
$tmp = [];
while ($cur = $stmt->fetch()) {
if ($cur['last_post']>$new[$cur['forum_id']]) {
$tmp[$cur['forum_id']] = true;
}
}
$new = $tmp;
}
}
$r = $this->c->Router;
// формированием таблицы разделов
$result = [];
foreach ($fTree[$root] as $fId => $cur) {
// список подразделов
$subForums = [];
if (isset($fTree[$fId])) {
foreach ($fTree[$fId] as $f) {
$subForums[] = [
$r->link('Forum', [
'id' => $f['fid'],
'name' => $f['forum_name']
]),
$f['forum_name']
];
}
}
// модераторы
$moderators = [];
if (!empty($forums[$fId]['moderators'])) {
$mods = unserialize($forums[$fId]['moderators']);
foreach ($mods as $name => $id) {
if ($user->gViewUsers == '1') {
$moderators[] = [
$r->link('User', [
'id' => $id,
'name' => $name,
]),
$name
];
} else {
$moderators[] = $name;
}
}
}
// статистика по разделам
$numT = 0;
$numP = 0;
$time = 0;
$postId = 0;
$poster = '';
$topic = '';
$fnew = false;
foreach ($fAsc[$fId] as $id) {
$fnew = $fnew || isset($new[$id]);
$numT += $forums[$id]['num_topics'];
$numP += $forums[$id]['num_posts'];
if ($forums[$id]['last_post'] > $time) {
$time = $forums[$id]['last_post'];
$postId = $forums[$id]['last_post_id'];
$poster = $forums[$id]['last_poster'];
$topic = $forums[$id]['last_topic'];
}
}
$result[$cur['cid']]['name'] = $cur['cat_name'];
$result[$cur['cid']]['forums'][] = [
'fid' => $fId,
'forum_name' => $cur['forum_name'],
'forum_desc' => $forums[$fId]['forum_desc'],
'forum_link' => $r->link('Forum', [
'id' => $fId,
'name' => $cur['forum_name']
]),
'redirect_url' => $cur['redirect_url'],
'subforums' => $subForums,
'moderators' => $moderators,
'num_topics' => $numT,
'num_posts' => $numP,
'topics' => $this->number($numT),
'posts' => $this->number($numP),
'last_post' => $this->time($time),
'last_post_id' => $postId > 0 ? $r->link('ViewPost', ['id' => $postId]) : null,
'last_poster' => $poster,
'last_topic' => $topic,
'new' => $fnew,
];
}
return $result;
}
}