amdp3-metaforums/Application/Models/Thread.php

67 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2019-11-19 03:36:39 +00:00
<?php
namespace Application\Models;
use Application\Foundations\Model as DBModel;
use Application\Foundations\QueryBuilder;
use Application\Foundations\DateHelper;
use Application\Services\ServiceContainer;
class Thread extends DBModel {
public function author_model_attribute() {
return User::find($this->author);
}
public function post_count_attribute() {
$query = new QueryBuilder();
$query = $query->select("COUNT(id) AS count")->from("post")->where("thread_id",$this->id)->build();
$result = ServiceContainer::Database()->select($query);
return $result[0]["count"];
}
public function elapsed_created_attribute() {
return DateHelper::elapsedString($this->created_at);
}
2019-11-20 05:19:02 +00:00
public function thread_age_attribute() {
$query = new QueryBuilder();
$query = $query->where('thread_id',$this->id)->orderBy('created_at','desc');
$post = Post::selectOne($query);
return time() - strtotime($post->created_at);
}
2019-11-19 03:36:39 +00:00
public function last_reply_attribute() {
$query = new QueryBuilder();
$query = $query->where('thread_id',$this->id)->orderBy('created_at','desc');
$post = Post::selectOne($query);
return DateHelper::elapsedString($post->created_at);
}
public function main_post_attribute() {
$query = new QueryBuilder();
$query = $query->where('thread_id',$this->id)->orderBy('created_at','asc');
return Post::selectOne($query);
}
public function is_hot_attribute() {
$query = new QueryBuilder();
$query = $query->where('thread_id',$this->id)->where('created_at','>',date('Y-m-d H:i:s',strtotime(' - 5 minutes')))->orderBy('created_at','desc');
$post = Post::select($query);
return count($post) > 10;
}
public function posts() {
$query = new QueryBuilder();
$query = $query->where('thread_id',$this->id);
$post = Post::select($query);
return $post;
}
public function category() {
2019-11-20 05:19:02 +00:00
$category = Category::find($this->category_id);
2019-11-19 03:36:39 +00:00
return $category;
}
2019-11-20 05:19:02 +00:00
public function isLocked() {
$action = $this->lock();
return isset($action);
}
public function lock() {
$where = new QueryBuilder();
$where = $where->where('thread_id',$this->id)->where('action_type','lock')->where('expired_at','>',date('Y-m-d H:i:s'))->orderBy('expired_at','desc');
$actions = UserAction::selectOne($where);
return $actions;
}
2019-11-19 03:36:39 +00:00
}