redirect('/admin/article/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.createtime' => 'desc', 'a.sort' => 'asc', 'a.id' => 'desc']; $arg_field = ['a.id,a.picture,a.name,a.sort,a.headline,a.createtime,a.stat,a.cid,a.tags,a.siteid,a.country_code,a.ishot,a.recommend', 'c.id' => 'categoryid', 'c.name' => 'categoryname']; if ($cid > 0) { $arg_where['a.cid'] = $cid; } $dataObject = model('article')->getArticleLists($arg_where, $arg_order, $arg_field, 12); $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('articleCategory')->getCategoryOptions($cid, $argc_where, $argc_order, $argc_field, 100); $value = [ 'categoryOptions' => $categoryOptions, 'list' => $dataObject->isEmpty() ? null : $dataObject->items(), 'page' => $dataObject->render(), 'cid' => $cid, '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('articleCategory')->getCategoryOptions($cid, $arg_where, $arg_order, $arg_field, 100); $seriesOptions = model('ProductSeries')->getSeriesOptions( ['stat' => 0, 'country_code' => $this->country_code], ['sort' => 'asc', 'id' => 'asc'], ['id', 'name', 'sort'] ); $value = ['categoryOptions' => $categoryOptions, 'seriesOptions' => $seriesOptions]; $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('article')->insertRow($data); if ($model && $model->getData('id')) { return $this->redirect(url('/admin/article/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) { $article = model('article')->getRow($id); if (empty($article)) { return $this->error(Lang::get('incorrect operation')); } $value['article'] = $article; } else { return $this->error(Lang::get('incorrect operation')); } $cid = isset($article['cid']) ? $article['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('articleCategory')->getCategoryOptions($cid, $arg_where, $arg_order, $arg_field, 100); $seriesOptions = model('ProductSeries')->getSeriesOptions( ['stat' => 0, 'country_code' => $this->country_code], ['sort' => 'asc', 'id' => 'asc'], ['id', 'name', 'sort'], null, explode(",", $article['product_series']) ); $value['categoryOptions'] = $categoryOptions; $value['seriesOptions'] = $seriesOptions; $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('article')->updateRow($data); if ($model && $model->getData('id')) { return $this->redirect(url('/admin/article/lists')); } else { return $this->error(Lang::get('operation failed')); } } return $this->error(Lang::get('incorrect operation')); } 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('article')->updateRow(['id' => $id, 'sort' => $sort]); if ($model && $model->getData('id')) { return $this->success(Lang::get('operation successed'), url('/admin/article/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('article')->updateRow(['id' => $id, 'headline' => $flag]); if ($model && $model->getData('id')) { return $this->success(Lang::get('operation successed'), url('/admin/article/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('article')->updateRow(['id' => $id, 'ishot' => $flag]); if ($model && $model->getData('id')) { return $this->success(Lang::get('operation successed'), url('/admin/article/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('article')->updateRow(['id' => $id, 'recommend' => $flag]); if ($model && $model->getData('id')) { return $this->success(Lang::get('operation successed'), url('/admin/article/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) { $article = model('article')->getRow($id); if (empty($article)) { return $this->error(Lang::get('incorrect operation')); } $value['article'] = $article; } else { return $this->error(Lang::get('incorrect operation')); } $cid = isset($article['cid']) ? $article['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('articleCategory')->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('article')->updateRows(['cid' => $cid,'jump_link' => ''], ['id' => ['in', $ids]]); if ($result) { return $this->success(Lang::get('operation successed'), url('/admin/article/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('article')->updateRows(['recommend' => 1], ['id' => ['in', $ids]]); if ($result) { return $this->success(Lang::get('operation successed'), url('/admin/article/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('article')->deleteRow($id); if ($result) { return $this->success(Lang::get('operation successed'), url('/admin/article/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('article')->deleteRows($ids); if ($result) { return $this->success(Lang::get('operation successed'), url('/admin/article/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.createtime' => 'desc', '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('article')->getRecycleLists($arg_where, $arg_order, $arg_field, 24); $argc_where = array('pid' => 0, 'stat' => 0); $argc_order = array('sort' => 'asc', 'id' => 'asc'); $argc_field = array('id', 'pid', 'haschild', 'name', 'sort'); $categoryOptions = model('articleCategory')->getCategoryOptions(0, $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 recovery($id = 0) { $id = intval($id); if ($id > 0) { $model = model('article')->updateRow(['id' => $id, 'stat' => 0]); if ($model && $model->getData('id')) { return $this->success(Lang::get('operation successed'), url('/admin/article/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('article')->updateRows(['stat' => 0], ['id' => ['in', $ids]]); if ($result) { return $this->success(Lang::get('operation successed'), url('/admin/article/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('article')->destroyRow($id); if ($result) { return $this->success(Lang::get('operation successed'), url('/admin/article/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('article')->destroyRows($ids); if ($result) { return $this->success(Lang::get('operation successed'), url('/admin/article/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, 'a.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'),'ArticleCategory')->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 = array_merge(array('id' => 'ID'), $data['field']); } else { $fields = array('id' => 'ID', 'cid' => '所属分类', 'name' => '名称', 'sort' => '排序', 'headline' => '首页推荐', 'picture' => '图片', 'writer' => '新闻作者', 'source' => '新闻来源', 'viewcount' => '浏览数量', 'zancount' => '点赞数量', /* 'commentcount' => '评论数量', */ 'description' => '新闻描述', 'picture' => '新闻图片', 'tags' => '新闻标签', 'content' => '内容详情', 'seo_title' => 'SEO标题', 'seo_keyword' => 'SEO关键词', 'seo_description' => 'SEO描述', 'createtime' => '发布时间'); } $argc_where = array('pid' => 0, 'stat' => 0, 'siteid' => $this->siteid, 'country_code' => $this->country_code); $argc_order = array('sort' => 'asc', 'id' => 'asc'); $argc_field = array('id', 'pid', 'haschild', 'name', 'sort'); $categoryOptions = model('articleCategory')->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.createtime' => 'desc', 'a.sort' => 'asc', 'a.id' => 'desc']; $arg_field = array_map(function($value) { return 'a.' . $value; }, array_keys($fields)); $arg_field['c.id'] = 'categoryid'; $arg_field['c.name'] = 'categoryname'; set_time_limit(36000); ini_set('memory_limit', '512M'); $total = model('article')->getExportSearchArticleLists($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('article')->getExportSearchArticleLists($arg_where, $arg_order, [$start_index, $page_size], false, $arg_field); if (!empty($datainfo)) { if (($start_index % $sheet_size) == 0) { if ($sheet) { $objPHPExcel->createSheet(); } $sheet++; $i = 0; foreach ($fields as $key => $field) { $objPHPExcel->setActiveSheetIndex($sheet - 1)->setCellValue($cellName[$i] . '1', $field); $i++; } $objPHPExcel->getActiveSheet()->setTitle('sheet' . $sheet); $index = 1; } foreach ($datainfo as $row) { if (isset($row['cid'])) { $row->data('cid', $row['categoryname']); } if (isset($row['createtime'])) { $row->data('createtime', date('Y-m-d H:i:s', $row['createtime'])); } $index++; $i = 0; foreach ($fields as $key => $field) { if ($key == 'picture') { $image = '.' . $this->request->root() . $row['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, $row[$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; } }