homepagePHP/app/Admin/Controller/TopicFromNewsController.cla...

132 lines
3.6 KiB
PHP
Executable File

<?php
namespace Admin\Controller;
use Common\Model\MenuModel;
use Gy_Library\DBCont;
use Gy_Library\GyListController;
use Qscmf\Builder\FormBuilder;
use Qscmf\Builder\ListBuilder;
class TopicFromNewsController extends GyListController
{
public function index($topic_id)
{
$news_ids = D('TopicFromNews')->where(['topic_id'=>$topic_id])->getField('news_id',true);
$topic_name = D('Topic')->where(['id'=>$topic_id])->getField('name');
if(!empty($news_ids)){
$map['news_id']= ['IN',$news_ids];
$map['topic_id']= $topic_id;
}else{
$map['_string'] = '1=0';
}
$get_data = I('get.');
if (!empty($get_data['word']) && $get_data['key'] === 'title') {
$map1['title'] = array('like', '%' . $get_data['word'] . '%');
$map['news_id'] = D('News')->where($map1)->getField('id');
}
$map['status']= DBCont::YES_BOOL_STATUS;
$model = D('TopicFromNews');
$count = $model->getListForCount($map);
$per_page = C('ADMIN_PER_PAGE_NUM', null, false);
if ($per_page === false) {
$page = new \Gy_Library\GyPage($count);
} else {
$page = new \Gy_Library\GyPage($count, $per_page);
}
$news_list = $model->getListForPage($map, $page->nowPage, $page->listRows, 'sort asc,id asc');
foreach ($news_list as &$value) {
$value['title'] = D('News')->where(['id'=>$value['news_id']])->getField('title');
}
// 使用Builder快速建立列表页面。
$builder = new ListBuilder();
$builder = $builder->setMetaTitle($topic_name.'的新闻列表');// 设置页面标题
$builder->addTopButton('self', array('title' => '添加新闻', 'href' => U('News/index',['topic_id'=>$topic_id]), 'class' => 'btn btn-primary'));
$builder->addTopButton('save', array('title' => '保存排序','href' => U('saveTopic',['topic_id'=>$topic_id]), 'class' => 'btn btn-primary ajax-post confirm'));
$builder
->setSearchUrl(U('',['topic_id'=>$topic_id]))
->setNIDByNode(MODULE_NAME, 'Topic', 'index')
->addSearchItem('', 'select_text', '单行输入', array('title' => '新闻标题'))
->addTableColumn('title', '新闻标题')
->addTableColumn('sort', '排序','','',true)
->addTableColumn('right_button', '操作', 'btn')
->addRightButton('self', array('title' => '删除', 'href' => U('delete',['topic_id'=>$topic_id,'ids'=>'__data_id__']), 'class' => 'label label-danger ajax-get confirm'))// 添加删除按钮
->setTableDataList($news_list)// 数据列表
->setTableDataPage($page->show())// 数据列表分页
->display();
// $news_list = D('TopicFromNews')->where($map)->select();
}
public function saveTopic($topic_id){
if(IS_POST){
$data = I('post.');
foreach($data['id'] as $k=>$v){
$save_data['sort'] = $data['sort'][$k];
D('TopicFromNews')->where('id=' . $v)->save($save_data);
}
$this->success('保存成功',U('index',['topic_id'=>$topic_id]));
}
}
public function delete($topic_id)
{
$ids = I('ids');
if (!$ids) {
$this->error('请选择要删除的数据');
}
$r = parent::_del($ids);
if ($r === false) {
$this->error($this->_getError());
} else {
$this->success('删除成功', U('index',['topic_id'=>$topic_id]));
}
}
}