123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace app\user\controller;
- use app\base\controller\Base;
- class Notice extends Base{
-
- private $noticeModel;
-
- public function _initialize(){
- parent::_initialize();
-
- $this->noticeModel = new \app\user\model\Notice();
- }
-
- public function index(){
- $p = $this->request->get('page', 0);
- $nick = $this->request->get('n');
- $where = ['is_del'=>0];
-
- if($nick) $where['title'] = ['like', "%$nick%"];
-
- $info = $this->noticeModel->getInfo($p, $where);
- $page = $this->noticeModel->getPage($where);
-
- $this->assign("info", $info);
- $this->assign("page", $page);
- return $this->fetch();
- }
-
- public function edit(){
- $id = $this->request->get('id');
- $info = $this->noticeModel->getNoticeById($id);
-
- $this->assign("info", $info);
- return $this->fetch('edit');
- }
-
- public function create(){
- $res = $this->noticeModel->where(['is_del'=>0])->order('update_time', 'desc')->select();
-
- $dom = new \DOMDocument('1.0', 'utf-8');
- $filename = 'xml/announce.xml';
- $dom->formatOutput = true;
-
- $announce = $dom->createElement('announce');
- // 添加版本信息
- $version = $dom->createElement('version', count($res));
- $announce->appendChild($version);
-
- foreach ($res as $val) {
- $item = $dom->createElement('item');
- $id = $dom->createElement('id', $val['id']);
- $zh_t = $dom->createElement('title', $val['zh_t']);
- $zh_t->setAttribute('language', 'Chinese');
- $tw_t = $dom->createElement('title', $val['tw_t']);
- $tw_t->setAttribute('language', 'ChineseTraditional');
- $en_t = $dom->createElement('title', $val['en_t']);
- $en_t->setAttribute('language', 'English');
- $time = $dom->createElement('date', $val['time']);
- $content = $dom->createElement('content');
-
- $this->fillContent($dom, $content, $val['zh'], 'Chinese');
- $this->fillContent($dom, $content, $val['tw'], 'ChineseTraditional');
- $this->fillContent($dom, $content, $val['en'], 'English');
-
- $item->appendChild($id);
- $item->appendChild($zh_t);
- $item->appendChild($tw_t);
- $item->appendChild($en_t);
- $item->appendChild($time);
- $item->appendChild($content);
-
- $announce->appendChild($item);
- }
-
- $dom->appendChild($announce);
- $dom->save($filename);
- $this->success('创建xml成功!');
- }
-
- private function fillContent(\DOMDocument &$dom, \DOMElement &$node, string $content, string $type){
- $info = explode(PHP_EOL, $content);
-
- foreach ($info as $val) {
- $cdata = $dom->createCDATASection($val);
- $text = $dom->createElement('text');
- $text->setAttribute('language', $type);
- $text->appendChild($cdata);
-
- $node->appendChild($text);
- }
-
- }
-
- public function delete(){
- $res = $this->noticeModel->where(['id'=>$this->request->get('id')])->update(['is_del'=>1]);
-
- if($res){
- $this->success('删除成功!', 'index');
- }else{
- $this->error('删除失败!');
- }
- }
-
- public function save(){
- $res = $this->noticeModel->addAndSave();
-
- if($res){
- $this->success('保存成功!', 'index');
- }else{
- $this->error('保存失败!');
- }
- }
-
- }
|