123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <?php
- namespace huo\model\posts;
- use huo\model\common\CommonModel;
- use huolib\constant\NewsConst;
- class PostsModel extends CommonModel {
- protected $name = 'posts';
-
-
- protected $readonly = ['user_id'];
-
- protected $autoWriteTimestamp = true;
-
- protected $type
- = [
- 'more' => 'array',
- ];
-
- protected function base($query) {
- $query->where('delete_time', 0)
- ->where('post_status', 2)
- ->whereTime('published_time', 'between', [1, time()]);
- }
-
- public function game() {
- return $this->belongsTo('huo\model\game\GameModel', 'app_id', 'id')->field('id,name');
- }
-
- public function user() {
- return $this->hasOne('huo\model\user\UserModel', 'id', 'user_id', [], 'left')->setEagerlyType(0);
- }
-
- public function getPostContentAttr($value) {
- return cmf_replace_content_file_url(htmlspecialchars_decode($value));
- }
-
- public function getContentAttr($value) {
- return cmf_replace_content_file_url(htmlspecialchars_decode($value));
- }
-
- public function setPostContentAttr($value) {
- return htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($value), true));
- }
-
- public function getTypeAttr($value) {
- $_rs = NewsConst::getNewsMsg($value);
- if (false == $_rs) {
- return $value;
- }
- return $_rs;
- }
-
- public function getMoreAttr($value) {
- $more = json_decode($value, true);
- if (!empty($more['thumbnail'])) {
- $more['thumbnail'] = cmf_get_image_url($more['thumbnail']);
- }
- if (!empty($more['photos'])) {
- foreach ($more['photos'] as $key => $value) {
- $more['photos'][$key]['url'] = cmf_get_image_url($value['url']);
- }
- }
- if (!empty($more['files'])) {
- foreach ($more['files'] as $key => $value) {
- $more['files'][$key]['url'] = cmf_get_image_url($value['url']);
- }
- }
-
- if (!empty($more['turntable'])) {
- $more['turntable'] = cmf_get_image_url($more['turntable']);
- }
- if (!empty($more['pointer'])) {
- $more['pointer'] = cmf_get_image_url($more['pointer']);
- }
- return $more;
- }
-
- public function setPublishedTimeAttr($value) {
- return strtotime($value);
- }
-
- public function setStartTimeAttr($value) {
- return strtotime($value);
- }
-
- public function setEndTimeAttr($value) {
- return strtotime($value);
- }
-
- public function adminAddPage($data) {
- $data['user_id'] = cmf_get_current_admin_id();
- $data['post_status'] = empty($data['post_status']) ? 1 : 2;
- $_rs = $this->allowField(true)->data($data, true)->save();
- return $_rs;
- }
-
- public function adminEditPage($data) {
- $data['user_id'] = cmf_get_current_admin_id();
- $data['post_status'] = empty($data['post_status']) ? 1 : 2;
- $data['update_time'] = time();
- $_rs = $this->allowField(true)->isUpdate(true)->data($data, true)->save();
- return $_rs;
- }
-
- public function adminDeletePage($data) {
- if (isset($data['id'])) {
- $id = $data['id'];
- $_rs = $this->useGlobalScope(false)->where(['id' => $id])->update(
- [
- 'delete_time' => time()
- ]
- );
- } elseif (isset($data['ids'])) {
- $ids = $data['ids'];
- $_rs = $this->useGlobalScope(false)->where(['id' => ['in', $ids]])
- ->update(
- [
- 'delete_time' => time()
- ]
- );
- } else {
- return false;
- }
- return $_rs;
- }
-
- public function getIdName($_map = []) {
- $_rs = $this->where($_map)->column('post_title', 'id');
- return $_rs;
- }
- }
|