Notice.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\user\controller;
  3. use app\base\controller\Base;
  4. class Notice extends Base{
  5. private $noticeModel;
  6. public function _initialize(){
  7. parent::_initialize();
  8. $this->noticeModel = new \app\user\model\Notice();
  9. }
  10. public function index(){
  11. $p = $this->request->get('page', 0);
  12. $nick = $this->request->get('n');
  13. $where = ['is_del'=>0];
  14. if($nick) $where['title'] = ['like', "%$nick%"];
  15. $info = $this->noticeModel->getInfo($p, $where);
  16. $page = $this->noticeModel->getPage($where);
  17. $this->assign("info", $info);
  18. $this->assign("page", $page);
  19. return $this->fetch();
  20. }
  21. public function edit(){
  22. $id = $this->request->get('id');
  23. $info = $this->noticeModel->getNoticeById($id);
  24. $this->assign("info", $info);
  25. return $this->fetch('edit');
  26. }
  27. public function create(){
  28. $res = $this->noticeModel->where(['is_del'=>0])->order('update_time', 'desc')->select();
  29. $dom = new \DOMDocument('1.0', 'utf-8');
  30. $filename = 'xml/announce.xml';
  31. $dom->formatOutput = true;
  32. $announce = $dom->createElement('announce');
  33. // 添加版本信息
  34. $version = $dom->createElement('version', count($res));
  35. $announce->appendChild($version);
  36. foreach ($res as $val) {
  37. $item = $dom->createElement('item');
  38. $id = $dom->createElement('id', $val['id']);
  39. $zh_t = $dom->createElement('title', $val['zh_t']);
  40. $zh_t->setAttribute('language', 'Chinese');
  41. $tw_t = $dom->createElement('title', $val['tw_t']);
  42. $tw_t->setAttribute('language', 'ChineseTraditional');
  43. $en_t = $dom->createElement('title', $val['en_t']);
  44. $en_t->setAttribute('language', 'English');
  45. $time = $dom->createElement('date', $val['time']);
  46. $content = $dom->createElement('content');
  47. $this->fillContent($dom, $content, $val['zh'], 'Chinese');
  48. $this->fillContent($dom, $content, $val['tw'], 'ChineseTraditional');
  49. $this->fillContent($dom, $content, $val['en'], 'English');
  50. $item->appendChild($id);
  51. $item->appendChild($zh_t);
  52. $item->appendChild($tw_t);
  53. $item->appendChild($en_t);
  54. $item->appendChild($time);
  55. $item->appendChild($content);
  56. $announce->appendChild($item);
  57. }
  58. $dom->appendChild($announce);
  59. $dom->save($filename);
  60. $this->success('创建xml成功!');
  61. }
  62. private function fillContent(\DOMDocument &$dom, \DOMElement &$node, string $content, string $type){
  63. $info = explode(PHP_EOL, $content);
  64. foreach ($info as $val) {
  65. $cdata = $dom->createCDATASection($val);
  66. $text = $dom->createElement('text');
  67. $text->setAttribute('language', $type);
  68. $text->appendChild($cdata);
  69. $node->appendChild($text);
  70. }
  71. }
  72. public function delete(){
  73. $res = $this->noticeModel->where(['id'=>$this->request->get('id')])->update(['is_del'=>1]);
  74. if($res){
  75. $this->success('删除成功!', 'index');
  76. }else{
  77. $this->error('删除失败!');
  78. }
  79. }
  80. public function save(){
  81. $res = $this->noticeModel->addAndSave();
  82. if($res){
  83. $this->success('保存成功!', 'index');
  84. }else{
  85. $this->error('保存失败!');
  86. }
  87. }
  88. }