2018-01-09 Iterator for posts
This commit is contained in:
parent
19dbdb6bbd
commit
9a2dd0ba87
4 changed files with 158 additions and 14 deletions
|
@ -302,12 +302,13 @@ class Model extends DataModel
|
|||
INNER JOIN ::groups AS g ON g.g_id=u.group_id
|
||||
WHERE p.id IN (?ai:ids) ORDER BY p.id';
|
||||
|
||||
$posts = $this->c->DB->query($sql, $vars)->fetchAll();
|
||||
$stmt = $this->c->DB->query($sql, $vars);
|
||||
|
||||
$postCount = 0;
|
||||
$timeMax = 0;
|
||||
$result = [];
|
||||
|
||||
foreach ($posts as &$cur) {
|
||||
while ($cur = $stmt->fetch()) {
|
||||
if ($cur['posted'] > $timeMax) {
|
||||
$timeMax = $cur['posted'];
|
||||
}
|
||||
|
@ -324,11 +325,10 @@ class Model extends DataModel
|
|||
$cur['warnings'] = $warnings[$cur['id']];
|
||||
}
|
||||
|
||||
$cur = $this->c->posts->create($cur);
|
||||
$result[] = $this->c->posts->create($cur);
|
||||
}
|
||||
unset($cur);
|
||||
$this->timeMax = $timeMax;
|
||||
return $posts;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
144
app/Models/Topic/Posts.php
Normal file
144
app/Models/Topic/Posts.php
Normal file
|
@ -0,0 +1,144 @@
|
|||
<?php
|
||||
|
||||
namespace ForkBB\Models\Topic;
|
||||
|
||||
use ForkBB\Models\Method;
|
||||
use Iterator;
|
||||
use PDO;
|
||||
use InvalidArgumentException;
|
||||
use RuntimeException;
|
||||
|
||||
class Posts extends Method implements Iterator
|
||||
{
|
||||
protected $key;
|
||||
|
||||
protected $row;
|
||||
|
||||
protected $stmt;
|
||||
|
||||
protected $warnings;
|
||||
|
||||
protected $postCount;
|
||||
|
||||
protected $post;
|
||||
|
||||
protected $offset;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*
|
||||
* @return null|Method
|
||||
*/
|
||||
public function posts()
|
||||
{
|
||||
if ($this->model->id < 1) {
|
||||
throw new RuntimeException('The model does not have ID');
|
||||
}
|
||||
|
||||
if (! $this->model->hasPage()) {
|
||||
throw new InvalidArgumentException('Bad number of displayed page');
|
||||
}
|
||||
|
||||
$this->offset = ($this->model->page - 1) * $this->c->user->disp_posts;
|
||||
$vars = [
|
||||
':tid' => $this->model->id,
|
||||
':offset' => $this->offset,
|
||||
':rows' => $this->c->user->disp_posts,
|
||||
];
|
||||
$sql = 'SELECT id
|
||||
FROM ::posts
|
||||
WHERE topic_id=?i:tid
|
||||
ORDER BY id LIMIT ?i:offset, ?i:rows';
|
||||
|
||||
$ids = $this->c->DB->query($sql, $vars)->fetchAll(PDO::FETCH_COLUMN);
|
||||
if (empty($ids)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// приклейка первого сообщения темы
|
||||
if ($this->model->stick_fp || $this->model->poll_type) {
|
||||
$ids[] = $this->model->first_post_id;
|
||||
}
|
||||
|
||||
$vars = [
|
||||
':ids' => $ids,
|
||||
];
|
||||
$sql = 'SELECT id, message, poster, posted
|
||||
FROM ::warnings
|
||||
WHERE id IN (?ai:ids)';
|
||||
|
||||
$this->warnings = $this->c->DB->query($sql, $vars)->fetchAll(\PDO::FETCH_GROUP);
|
||||
|
||||
$vars = [
|
||||
':ids' => $ids,
|
||||
];
|
||||
$sql = 'SELECT u.warning_all, u.gender, u.email, u.title, u.url, u.location, u.signature,
|
||||
u.email_setting, u.num_posts, u.registered, u.admin_note, u.messages_enable,
|
||||
u.group_id,
|
||||
p.id, p.poster as username, p.poster_id, p.poster_ip, p.poster_email, p.message,
|
||||
p.hide_smilies, p.posted, p.edited, p.edited_by, p.edit_post, p.user_agent, p.topic_id,
|
||||
g.g_user_title, g.g_promote_next_group, g.g_pm
|
||||
FROM ::posts AS p
|
||||
INNER JOIN ::users AS u ON u.id=p.poster_id
|
||||
INNER JOIN ::groups AS g ON g.g_id=u.group_id
|
||||
WHERE p.id IN (?ai:ids) ORDER BY p.id';
|
||||
|
||||
$this->stmt = $this->c->DB->query($sql, $vars);
|
||||
$this->model->timeMax = 0;
|
||||
$this->postCount = 0;
|
||||
$this->post = $this->c->posts->create();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function rewind()
|
||||
{
|
||||
$this->key = 0;
|
||||
}
|
||||
|
||||
public function current()
|
||||
{
|
||||
if (empty($this->row)) { //????
|
||||
return false;
|
||||
}
|
||||
|
||||
$cur = $this->row;
|
||||
|
||||
if ($cur['posted'] > $this->model->timeMax) {
|
||||
$this->model->timeMax = $cur['posted'];
|
||||
}
|
||||
|
||||
// номер сообшения в теме
|
||||
if ($cur['id'] == $this->model->first_post_id && $this->offset > 0) {
|
||||
$cur['postNumber'] = 1;
|
||||
} else {
|
||||
++$this->postCount;
|
||||
$cur['postNumber'] = $this->offset + $this->postCount;
|
||||
}
|
||||
|
||||
if (isset($this->warnings[$cur['id']])) {
|
||||
$cur['warnings'] = $this->warnings[$cur['id']];
|
||||
}
|
||||
|
||||
return $this->post->setAttrs($cur);
|
||||
}
|
||||
|
||||
public function key()
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
public function next()
|
||||
{
|
||||
++$this->key;
|
||||
}
|
||||
|
||||
public function valid()
|
||||
{
|
||||
$this->row = $this->stmt->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, $this->key);
|
||||
return false !== $this->row;
|
||||
}
|
||||
|
||||
}
|
|
@ -16,7 +16,7 @@ class Model extends DataModel
|
|||
*/
|
||||
protected function getisUnverified()
|
||||
{
|
||||
return empty($this->group_id);
|
||||
return 0 === $this->group_id;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -26,9 +26,9 @@ class Model extends DataModel
|
|||
*/
|
||||
protected function getisGuest()
|
||||
{
|
||||
return $this->group_id == $this->c->GROUP_GUEST
|
||||
|| $this->id < 2 //????
|
||||
|| empty($this->group_id); //????
|
||||
return $this->group_id === $this->c->GROUP_GUEST
|
||||
|| $this->id < 2
|
||||
|| null === $this->group_id;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -38,7 +38,7 @@ class Model extends DataModel
|
|||
*/
|
||||
protected function getisAdmin()
|
||||
{
|
||||
return $this->group_id == $this->c->GROUP_ADMIN;
|
||||
return $this->group_id === $this->c->GROUP_ADMIN;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -48,8 +48,8 @@ class Model extends DataModel
|
|||
*/
|
||||
protected function getisAdmMod()
|
||||
{
|
||||
return $this->group_id == $this->c->GROUP_ADMIN
|
||||
|| $this->g_moderator == '1';
|
||||
return $this->group_id === $this->c->GROUP_ADMIN
|
||||
|| '1' == $this->g_moderator;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -63,7 +63,7 @@ class Model extends DataModel
|
|||
*/
|
||||
public function isModerator(BaseModel $model)
|
||||
{
|
||||
if ($this->g_moderator != '1') {
|
||||
if ('1' != $this->g_moderator) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -59,7 +59,7 @@
|
|||
@if ($post->edited)
|
||||
<span class="f-post-edited" title="{!! __('Last edit', $post->edited_by, dt($post->edited)) !!}">{!! __('Edited') !!}</span>
|
||||
@endif
|
||||
<span class="f-post-number"><a href="{!! $post->link !!}" rel="bookmark">#{!! $post->postNumber !!}</a></span>
|
||||
<span class="f-post-number"><a href="{!! $post->link !!}" rel="bookmark">№{!! $post->postNumber !!}</a></span>
|
||||
</header>
|
||||
<div class="f-post-body clearfix">
|
||||
<address class="f-post-left clearfix">
|
||||
|
|
Loading…
Reference in a new issue