homepagePHP/app/Common/Model/NewsModel.class.php

148 lines
4.8 KiB
PHP
Executable File

<?php
namespace Common\Model;
use Gy_Library\DBCont;
use Illuminate\Support\Facades\DB;
//新闻资讯
class NewsModel extends \Gy_Library\GyListModel
{
protected $_validate = array(
array('title', 'require', '请填写标题', self::MUST_VALIDATE, '', 3),
array('cate_id', 'require', '请选择文章分类', self::MUST_VALIDATE, '', 3),
array('cover_id', 'require', '请上传封面图', self::MUST_VALIDATE, '', 3),
array('author', 'require', '请填写作者', self::MUST_VALIDATE, '', 3),
array('content', 'require', '请填写内容详情', self::MUST_VALIDATE, '', 3),
array('title', '0,255', '标题过长', self::EXISTS_VALIDATE, 'length', self::MODEL_BOTH),
array('author', '0,255', '作者过长', self::EXISTS_VALIDATE, 'length', self::MODEL_BOTH),
);
protected $_auto = array(
array('in_date', 'strtotime', parent::MODEL_BOTH, 'function'),
array('start_date', 'strtotime', parent::MODEL_BOTH, 'function'),
array('create_date', 'time', parent::MODEL_INSERT, 'function'),
);
public function getNewsData($topic_id)
{
$item = $this->where(['topic_id' => $topic_id])->select();
$datas = [];
foreach ($item as $vo) {
$tpm = [];
$tpm[] = ['name' => 'topic_id', 'type' => 'select2', 'value' => $vo['id']];
$datas[] = $tpm;
}
return $datas;
}
public function createSaveTopic($data)
{
$res['topic_id'] = $data['id'];
$this->where(['topic_id' => $data['id']])->setField('topic_id', 0);
if (!isset($data['news_id'])) {
$data['news_id'] = [
0 => ''
];
}
$r = $this->where(['id' => ['IN', array_unique($data['news_id'])]])->save($res);
if ($r === false) {
$this->error = '没有添加任何新闻';
return false;
}
return $r;
}
public function getExportList($map, $page = '', $rownum = '')
{
if (!empty($page) && !empty($rownum)) {
$this->page($page, $rownum);
}
$list = $this->where($map)->order('sort asc,id desc')->field('title,cate_id,keywords_id,author,status,click_time,in_date,start_date')->select();
$excel_list = array();
foreach ($list as &$a) {
$temp = array();
$temp['标题'] = $a['title'];
$temp['文章分类'] = $this->extractNumberGetName($a['cate_id'], D('NewsCate')->where(['status' => DBCont::YES_BOOL_STATUS])->order('sort asc,id desc')->getField('id,name', true));
$temp['关键词'] = $this->extractNumberGetNameForCondition($a['keywords_id'], 'Keywords', 'id', 'name');
$temp['作者'] = $a['author'];
$temp['状态'] = !empty($a['status']) ? '正常' : '禁用';
$temp['点击量'] = $a['click_time'];
$temp['入库时间'] = date('Y-m-d', $a['in_date']);
$temp['首发时间'] = date('Y-m-d', $a['start_date']);
$excel_list[] = $temp;
usleep(10000);
}
return $excel_list;
}
public function extractNumberGetName($strs, $countarr)
{
$patterns = "/\d+/";
$str = '';
preg_match_all($patterns, $strs, $arr);
$i = 0;
foreach ($arr[0] as $key => $value) {
if ($i == 0) {
$str .= $countarr[$value];
} else {
$str .= '、' . $countarr[$value];
}
$i++;
}
return $str;
}
/*
* 提取数字并去数据库取得相应的分类名
* $strs 需要处理的字符串
* $table 数据表名
* $condition 条件字段
* $field 获取的字段
*/
public function extractNumberGetNameForCondition($strs, $table, $condition, $field)
{
if (empty($strs)) {
return '';
}
$patterns = "/\d+/"; //第一种
preg_match_all($patterns, $strs, $arr);
if (empty($arr[0])) {
return '';
}
$map[$condition] = array('in', $arr[0]);
$map['status'] = DBCont::YES_BOOL_STATUS;
$model = D($table);
$result = $model->field($field)->where($map)->select();
$str = '';
$i = 0;
foreach ($result as $key => $item) {
if ($i == 0) {
$str .= $item[$field];
} else {
$str .= '、' . $item[$field];
}
$i++;
}
return $str;
}
public function genNewsId($news_ids)
{
$res = $this->where(['id' => ['IN', $news_ids]])->select();
$datas = [];
foreach ($res as $vo) {
$tem = [];
$tem[] = ['name' => 'news_id', 'type' => 'select2', 'value' => $vo['id']];
$datas[] = $tem;
}
return $datas;
}
}