Files
orico-official-website-old/app/admin/controller/Video.php
2024-10-29 14:04:59 +08:00

570 lines
24 KiB
PHP
Executable File

<?php
namespace app\admin\controller;
use think\Lang;
use think\Loader;
use think\Config;
class Video extends BaseController {
public function index() {
$this->redirect('/admin/video/lists');
}
private function init_search(&$search)
{
$search['name'] = '';
$search['tags'] = '';
$search['timebegin'] = '';
$search['timeend'] = '';
}
public function lists() {
$data = $this->request->param();
$cid = isset($data['cid']) ? intval($data['cid']) : 0;
$arg_where = ['a.siteid' => $this->siteid, 'a.country_code' => $this->country_code];
$search = [];
$this->init_search($search);
if (isset($data['name']) && $data['name'] != '')
{
$arg_where['a.name'] = ['like', "%$data[name]%"];
$search['name'] = $data['name'];
}
if (isset($data['tags']) && $data['tags'] != '')
{
$arg_where['a.tags'] = ['like', "%$data[tags]%"];
$search['tags'] = $data['tags'];
}
if ((isset($data['timebegin']) && $data['timebegin'] != '') || (isset($data['timeend']) && $data['timeend'] != ''))
{
// 时间有一个不为空就初始化
$arg_where['a.createtime'] = [];
if (isset($data['timebegin']) && $data['timebegin'] != '')
{
$time = strtotime($data['timebegin']);
array_push($arg_where['a.createtime'], ['>=', $time]);
$search['timebegin'] = $data['timebegin'];
}
if (isset($data['timeend']) && $data['timeend'] != '')
{
$time = strtotime($data['timeend']);
array_push($arg_where['a.createtime'], ['<=', $time]);
$search['timeend'] = $data['timeend'];
}
}
$arg_order = ['a.sort' => 'asc', 'a.id' => 'desc'];
$arg_field = ['a.*', 'c.id' => 'categoryid', 'c.name' => 'categoryname'];
if ($cid > 0) {
$arg_where['a.cid'] = $cid;
}
$dataObject = model('video')->getCategoryVideoLists($arg_where, $arg_order, $arg_field, 24);
$argc_where = array('pid' => 0, 'stat' => 0, 'country_code' => $this->country_code);
$argc_order = array('sort' => 'asc', 'id' => 'asc');
$argc_field = array('id', 'pid', 'haschild', 'name', 'sort');
$categoryOptions = model('video_category')->getCategoryOptions($cid, $argc_where, $argc_order, $argc_field, 100);
$value = [
'categoryOptions' => $categoryOptions,
'list' => $dataObject->isEmpty() ? null : $dataObject->items(), //$dataObject->getCollection()->toArray()
'page' => $dataObject->render(),
//'page_previous' => $dataObject->getUrl($dataObject->currentPage() - 1),
//'page_next' => $dataObject->getUrl($dataObject->currentPage() + 1),
'search' => $search
];
$this->assign($value);
return $this->fetch();
}
public function add($cid = 0) {
$cid = is_numeric($cid) ? intval($cid) : 0;
$arg_where = array('pid' => 0, 'stat' => 0, 'country_code' => $this->country_code);
$arg_order = array('sort' => 'asc', 'id' => 'asc');
$arg_field = array('id', 'pid', 'haschild', 'name', 'sort');
$categoryOptions = model('video_category')->getCategoryOptions($cid, $arg_where, $arg_order, $arg_field, 100);
$value = ['categoryOptions' => $categoryOptions];
$this->assign($value);
return $this->fetch();
}
public function create() {
if ($this->request->isPost()) {
$data = $this->request->post();
if (empty($data) || !is_array($data)) {
return $this->error(Lang::get('incorrect operation'));
}
$validaterule = [ 'name' => 'require', 'cid' => 'number|between:0,2147483647',];
$validatemsg = [ 'name.require' => '名称不能为空', 'cid.between' => '所属上级值无效',];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
$data['sort'] = intval($data['sort']);
$data['siteid'] = $this->siteid;
$data['user_id'] = $this->user_id;
$data['country_code'] = $this->country_code;
$model = model('video')->insertRow($data);
if ($model && $model->getData('id')) {
return $this->redirect(url('/admin/video/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function edit($id = 0) {
$id = intval($id);
if ($id > 0) {
$video = model('video')->getRow($id);
if (empty($video)) {
return $this->error(lang::get('incorrect operation'));
}
$value['video'] = $video;
} else {
return $this->error(lang::get('incorrect operation'));
}
$cid = isset($video['cid']) ? $video['cid'] : 0;
$arg_where = array('pid' => 0, 'stat' => 0, 'country_code' => $this->country_code);
$arg_order = array('sort' => 'asc', 'id' => 'asc');
$arg_field = array('id', 'pid', 'haschild', 'name', 'sort');
$categoryOptions = model('video_category')->getCategoryOptions($cid, $arg_where, $arg_order, $arg_field, 100);
$value['categoryOptions'] = $categoryOptions;
$value['cid'] = $cid;
$this->assign($value);
return $this->fetch();
}
public function update() {
if ($this->request->isPost()) {
$data = $this->request->post();
if (empty($data) || !is_array($data)) {
return $this->error(Lang::get('incorrect operation'));
}
$validaterule = [ 'name' => 'require', 'cid' => 'number|between:0,2147483647',];
$validatemsg = [ 'name.require' => '名称不能为空', 'cid.between' => '所属上级值无效',];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
$data['sort'] = intval($data['sort']);
$model = model('video')->updateRow($data);
if ($model && $model->getData('id')) {
return $this->redirect(url('/admin/video/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function modallists() {
$inputid = $this->request->param('inputid', '', 'urldecode');
$titleid = $this->request->param('titleid', '', 'urldecode');
$callback = $this->request->param('callback', '', 'urldecode');
$filter_name = $this->request->param('filter_name', '', 'urldecode');
$arg_where = array('stat' => 0, 'country_code' => $this->country_code);
$arg_order = array('sort' => 'asc', 'id' => 'desc');
$arg_field = array('id', 'name', 'tags', 'picture', 'videopath','videourl',);
if (!empty($filter_name)) {
$arg_where['name'] = ['like', '%' . trim($filter_name) . '%'];
}
Config::set('url_common_param', true);
Config::set('paginate.query', array_filter(['inputid' => $inputid, 'titleid' => $titleid, 'callback' => $callback, 'filter_name' => $filter_name])); //分页参数
$dataObject = model('video')->getPageList($arg_where, $arg_order, $arg_field, 12, false);
$value = [
'list' => $dataObject->isEmpty() ? null : $dataObject->items(),
'page' => $dataObject->render(),
'total' => $dataObject->total(),
'inputid' => $inputid,
'titleid' => $titleid,
'callback' => $callback,
'filter_name' => $filter_name,
];
$this->assign($value);
Config::set('default_ajax_return', 'html');
$this->view->engine(['type' => 'php', 'view_suffix' => 'html']);
return $this->fetch();
}
public function updatesort() {
$id = $this->request->param('id', 0);
$sort = $this->request->param('sort', 0);
$sort = intval($sort);
$id = intval($id);
if ($id > 0 && $sort < 2147483647) {
$model = model('video')->updateRow(['id' => $id, 'sort' => $sort]);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/video/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function toggleheadline() {
$id = $this->request->param('id', 0);
$flag = $this->request->param('flag', 0);
$id = intval($id);
if ($id > 0) {
$model = model('video')->updateRow(['id' => $id, 'headline' => $flag]);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/video/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function toggleishot() {
$id = $this->request->param('id', 0);
$flag = $this->request->param('flag', 0);
$id = intval($id);
if ($id > 0) {
$model = model('video')->updateRow(['id' => $id, 'ishot' => $flag]);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/video/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function togglerecommend() {
$id = $this->request->param('id', 0);
$flag = $this->request->param('flag', 0);
$id = intval($id);
if ($id > 0) {
$model = model('video')->updateRow(['id' => $id, 'recommend' => $flag]);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/video/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function copy($id = 0) {
$id = intval($id);
if ($id > 0) {
$video = model('video')->getRow($id);
if (empty($video)) {
return $this->error(Lang::get('incorrect operation'));
}
$value['video'] = $video;
//$value['goods'] = Loader::model('Product')->getRow($video['product']);
} else {
return $this->error(Lang::get('incorrect operation'));
}
$cid = isset($video['cid']) ? $video['cid'] : 0;
$arg_where = array('pid' => 0, 'stat' => 0);
$arg_order = array('sort' => 'asc', 'id' => 'asc');
$arg_field = array('id', 'pid', 'haschild', 'name', 'sort');
$categoryOptions = model('video_category')->getCategoryOptions($cid, $arg_where, $arg_order, $arg_field, 100);
$value['categoryOptions'] = $categoryOptions;
$value['cid'] = $cid;
$this->assign($value);
return $this->fetch();
}
public function movecategory() {
$cid = $this->request->post('cid', 0);
$ids = $this->request->post('ids');
$cid = intval($cid);
if ($this->request->isPost() && $cid && $ids) {
$result = model('video')->updateRows(['cid' => $cid], ['id' => ['in', $ids]]);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/video/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function recommends() {
$ids = $this->request->post('ids');
if ($this->request->isPost() && $ids) {
$result = model('video')->updateRows(['recommend' => 1], ['id' => ['in', $ids]]);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/video/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function delete($id = 0) {
$id = intval($id);
if ($id > 0) {
$result = model('video')->deleteRow($id);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/video/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function deletes() {
$ids = $this->request->post('ids');
if ($this->request->isPost() && $ids) {
$result = model('video')->deleteRows($ids);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/video/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function recycle() {
$skeyword = $this->request->get('skeyword', '', 'urldecode');
$arg_where = ['a.stat' => -1, 'a.siteid' => $this->siteid, 'a.country_code' => $this->country_code];
$arg_order = ['a.sort' => 'asc', 'a.id' => 'desc'];
$arg_field = ['a.*', 'c.id' => 'categoryid', 'c.name' => 'categoryname'];
if (!empty($skeyword)) {
$skeyword = trim($skeyword);
$arg_where['a.name|a.tags'] = ['like', '%' . $skeyword . '%'];
$search['skeyword'] = $skeyword;
Config::set('paginate.query', ['skeyword' => $skeyword]); //分页参数
} else {
$search['skeyword'] = '';
}
$dataObject = model('video')->getRecycleLists($arg_where, $arg_order, $arg_field, 24);
$argc_where = array('pid' => 0, 'stat' => 0, 'country_code' => $this->country_code);
$argc_order = array('sort' => 'asc', 'id' => 'asc');
$argc_field = array('id', 'pid', 'haschild', 'name', 'sort');
$categoryOptions = model('video_category')->getCategoryOptions(0, $argc_where, $argc_order, $argc_field, 100);
$value = [
'categoryOptions' => $categoryOptions,
'list' => $dataObject->isEmpty() ? null : $dataObject->items(),
'page' => $dataObject->render(),
'search' => $search,
];
$this->assign($value);
return $this->fetch();
}
public function recovery($id = 0) {
$id = intval($id);
if ($id > 0) {
$model = model('video')->updateRow(['id' => $id, 'stat' => 0]);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/video/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function recoverys() {
$ids = $this->request->post('ids');
if ($this->request->isPost() && $ids) {
$result = model('video')->updateRows(['stat' => 0], ['id' => ['in', $ids]]);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/video/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function destroy($id = 0) {
$id = intval($id);
if ($id > 0) {
$result = model('video')->destroyRow($id);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/video/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function destroys() {
$ids = $this->request->post('ids');
if ($this->request->isPost() && $ids) {
$result = model('video')->destroyRows($ids);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/video/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function export() {
$arg_where = ['a.stat' => 0, 'a.siteid' => $this->siteid, 'country_code' => $this->country_code];
$data = $this->request->param();
if (isset($data['name']) && $data['name']) {
$arg_where['a.name'] = ['like', '%' . $data['name'] . '%'];
$search['name'] = $data['name'];
} else {
$search['name'] = '';
}
if (isset($data['cid']) && $data['cid']) {
$childIDArray = city(session('cit'),'product_category')->getChildIDArray($data['cid']);
$arg_where['a.cid'] = count($childIDArray) == 1 ? $data['cid'] : ['in', $childIDArray];
$search['cid'] = $data['cid'];
} else {
$search['cid'] = 0;
}
if (isset($data['tags']) && $data['tags']) {
$arg_where['a.tags'] = ['like', '%' . $data['tags'] . '%'];
$search['tags'] = $data['tags'];
} else {
$search['tags'] = '';
}
$search['timebegin'] = isset($data['timebegin']) ? strtotime($data['timebegin']) : 0;
$search['timeend'] = isset($data['timeend']) ? strtotime($data['timeend']) : 0;
if ($search['timeend'] - $search['timebegin'] > 0) {
$arg_where['a.createtime'] = ['between', [$search['timebegin'], $search['timeend']]];
} else {
if ($search['timebegin'] > 0 && empty($search['timeend'])) {
$arg_where['a.createtime'] = ['gt', $search['timebegin']];
}
}
if (!empty($data['field'])) {
$fields = $data['field'];
} else {
$fields = array('id' => 'ID', 'cid' => '视频分类', 'name' => '视频名称', 'description' => '视频描述', 'createtime' => '发布时间');
}
$argc_where = array('pid' => 0, 'stat' => 0, 'country_code' => $this->country_code);
$argc_order = array('sort' => 'asc', 'id' => 'asc');
$argc_field = array('id', 'pid', 'haschild', 'name', 'sort');
$categoryOptions = model('video_category')->getCategoryOptions($search['cid'], $argc_where, $argc_order, $argc_field, 100);
if (empty($data['submit'])) {
$value = ['categoryOptions' => $categoryOptions, 'fields' => $fields];
$this->assign($value);
return $this->fetch();
}
$arg_order = ['a.cid' => 'asc', 'a.sort' => 'asc', 'a.id' => 'asc'];
$arg_field = array_map(function($value) {
return 'a.' . $value;
}, array_keys($fields));
$arg_field['c.id'] = 'categoryid';
$arg_field['c.name'] = 'categoryname';
$arg_field['c.pid'] = 'pid';
set_time_limit(36000);
ini_set('memory_limit', '512M');
$total = model('video')->getExportSearchProductLists($arg_where, $arg_order, null, true);
$page_size = 1000;
$totalpage = ceil($total / $page_size);
$sheet_size = 5 * $page_size;
$cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
Loader::import('phpexcel.Classes.PHPExcel', EXTEND_PATH);
Loader::import('phpexcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
$objPHPExcel = new \PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Call of Duty")
->setLastModifiedBy("Call of Duty")
->setTitle("Office 2007 XLSX Cod Document")
->setSubject("Office 2007 XLSX Cod Document")
->setDescription("Cod document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Cod result file");
$page = 0;
$sheet = 0;
do {
$start_index = $page * $page_size;
$datainfo = model('video')->getExportSearchProductLists($arg_where, $arg_order, [$start_index, $page_size], false, $arg_field);
// echo \think\Db::table('video')->getLastSql();die;
// tiaoshi($datainfo);die;
if (!empty($datainfo)) {
if (($start_index % $sheet_size) == 0) {
if ($sheet) {
$objPHPExcel->createSheet();
}
$sheet++;
$i = 0;
foreach ($fields as $key => $value) {
$objPHPExcel->setActiveSheetIndex($sheet - 1)->setCellValue($cellName[$i] . '1', $value);
$i++;
}
$objPHPExcel->getActiveSheet()->setTitle('sheet' . $sheet);
$index = 1;
}
foreach ($datainfo as $value) {
if (isset($value['createtime'])) {
$value->data('createtime', date('Y-m-d H:i:s', $value['createtime']));
}
if (isset($value['cid'])) {
$value->data('cid', $value['categoryname']);
}
$index++;
$i = 0;
foreach ($fields as $key => $field) {
if ($key == 'picture') {
$image = '.' . $this->request->root() . $value['picture'];
if (@fopen($image, 'r')) {
$objDrawing = new \PHPExcel_Worksheet_Drawing();
$objDrawing->setPath($image);
$objDrawing->setHeight(50);
$objDrawing->setWidth(50);
$objDrawing->setCoordinates($cellName[$i] . $index);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
}
} else {
$objPHPExcel->setActiveSheetIndex($sheet - 1)->setCellValue($cellName[$i] . $index, $value[$key]);
}
$i++;
}
}
usleep(10000);
}
$page++;
if ($page > 650) {
break;
}
} while ($page < $totalpage);
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client's web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . date('YmdHis') . '.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
}