PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/src/applications/word/admin/ManageController.php

https://gitlab.com/wuhang2003/phpwind
PHP | 372 lines | 259 code | 88 blank | 25 comment | 44 complexity | 8e06d23a79f7b4db34c9dcf04aeebe4f MD5 | raw file
  1. <?php
  2. defined('WEKIT_VERSION') || exit('Forbidden');
  3. Wind::import('ADMIN:library.AdminBaseController');
  4. /**
  5. *
  6. * 词语过滤Controller
  7. *
  8. * @author Mingqu Luo <luo.mingqu@gmail.com>
  9. * @copyright ©2003-2103 phpwind.com
  10. * @license http://www.windframework.com
  11. * @version $Id: ManageController.php 28865 2013-05-28 03:34:43Z jieyin $
  12. * @package wind
  13. */
  14. class ManageController extends AdminBaseController {
  15. private $_configName = 'word';
  16. public function run() {
  17. $total = $this->_getWordDS()->count();
  18. $this->setOutput($total, 'total');
  19. $this->setOutput($total ? $this->_getWordDS()->getWordList() : array(), 'wordList');
  20. $this->setOutput($this->_getWordDS()->getTypeMap(), 'typeList');
  21. $this->setOutput(Wekit::C($this->_configName), 'config');
  22. $this->setOutput(1, 'page');
  23. $this->setOutput(20, 'perpage');
  24. $this->setOutput(array(), 'args');
  25. }
  26. public function addAction() {
  27. $this->setOutput($this->_getWordDS()->getTypeMap(), 'typeList');
  28. }
  29. public function doaddAction() {
  30. $word = $this->getInput('word', 'post');
  31. $word['word'] = trim($word['word']);
  32. if (!$word['word']) $this->showError('WORD:word.empty');
  33. if (!$word['type']) $this->showError('WORD:type.empty');
  34. $wordList = explode("\n", $word['word']);
  35. $wordList = array_unique($wordList);
  36. $wordService = $this->_getWordService();
  37. $findWord = $wordService->findWord($wordList);
  38. if ($findWord) {
  39. $existWord = implode(',', $findWord);
  40. $this->showError(array('WORD:show.exist.word', array('{showword}'=>$existWord)));
  41. }
  42. if ($this->_getWordDS()->isReplaceWord($word['type']) && !$word['replace']) {
  43. $this->showError('WORD:replaceword.empty');
  44. }
  45. Wind::import('SRV:word.dm.PwWordDm');
  46. foreach ($wordList as $value) {
  47. if (!$value) continue;
  48. $dm = new PwWordDm();/* @var $dm PwWordDm */
  49. $dm->setWord($value)->setWordType($word['type']);
  50. $this->_getWordDS()->isReplaceWord($word['type']) && $dm->setWordReplace(($word['replace'] ? $word['replace'] : '****'));
  51. $result = $this->_getWordDS()->add($dm);
  52. if ($result instanceof PwError) {
  53. $this->showError($result->getError());
  54. }
  55. }
  56. $this->_getWordFilter()->updateCache();
  57. $this->showMessage('success');
  58. }
  59. public function editAction () {
  60. $id = intval($this->getInput('id'));
  61. if (!$id) $this->showError('WORD:id_not_exist');
  62. $this->setOutput($this->_getWordDS()->get($id), 'detail');
  63. $this->setOutput($this->_getWordDS()->getTypeMap(), 'typeList');
  64. }
  65. public function doeditAction () {
  66. list($id, $word) = $this->getInput(array('id', 'word'), 'post');
  67. if (!$id) $this->showError('WORD:id_not_exist');
  68. $word['word'] = trim($word['word']);
  69. if (!$word['word']) $this->showError('WORD:word.empty');
  70. if (!$word['type']) $this->showError('WORD:type.empty');
  71. $wordService = $this->_getWordService();
  72. if ($wordService->isExistWord($word['word'], $id)) {
  73. $this->showError('WORD:word.is.exist');
  74. }
  75. if ($this->_getWordDS()->isReplaceWord($word['type']) && !$word['replace']) {
  76. $this->showError('WORD:replaceword.empty');
  77. }
  78. Wind::import('SRV:word.dm.PwWordDm');
  79. $dm = new PwWordDm($id);/* @var $dm PwWordDm */
  80. $dm->setWord($word['word'])->setWordType($word['type']);
  81. $word['replace'] = $this->_getWordDS()->isReplaceWord($word['type']) ? ($word['replace'] ? $word['replace'] : '****') : '';
  82. $dm->setWordReplace($word['replace']);
  83. if (($result = $this->_getWordDS()->update($dm))instanceof PwError) {
  84. $this->showError($result->getError());
  85. };
  86. $this->_getWordFilter()->updateCache();
  87. $this->showMessage('success');
  88. }
  89. public function deleteAction() {
  90. $id = intval($this->getInput('id'), 'post');
  91. if (!$id) $this->showError('WORD:id_not_exist');
  92. $this->_getWordDS()->delete($id);
  93. $this->_getWordFilter()->updateCache();
  94. $this->showMessage('success');
  95. }
  96. public function batchdeleteAction() {
  97. list($ids, $checkAll) = $this->getInput(array('ids', 'checkall'), 'post');
  98. if ($checkAll) {
  99. list($type, $keyword) = $this->getInput(array('type', 'keyword'));
  100. $this->_getWordService()->deleteByCondition($type, $keyword);
  101. $this->_getWordFilter()->updateCache();
  102. $this->showMessage('success');
  103. }
  104. if (empty($ids) || !is_array($ids)) $this->showError('WORD:no_operate_object');
  105. $this->_getWordDS()->batchDelete($ids);
  106. $this->_getWordFilter()->updateCache();
  107. $this->showMessage('success');
  108. }
  109. public function batcheditAction() {
  110. list($ids, $checkAll) = $this->getInput(array('ids', 'checkall'));
  111. if (empty($ids) || !is_array($ids)) $this->showError('WORD:no_operate_object');
  112. $wordList = $this->_getWordDS()->fetch($ids);
  113. $word = $wordIds = array();
  114. foreach ($wordList as $key=> $value) {
  115. $word[] = $value['word'];
  116. $wordIdList[] = $value['word_id'];
  117. }
  118. $word = array_unique($word);
  119. $this->setOutput($word ? implode("\n", $word) : '', 'word');
  120. $this->setOutput($wordIdList ? implode(",", $wordIdList) : '', 'wordId');
  121. $this->setOutput($this->_getWordDS()->getTypeMap(), 'typeList');
  122. $this->setOutput($checkAll, 'checkall');
  123. }
  124. public function dobatcheditAction() {
  125. list($word, $checkAll) = $this->getInput(array('word', 'checkall'), 'post');
  126. if ($checkAll) {
  127. $wordService = $this->_getWordService();
  128. $word['replace'] = $this->_getWordDS()->isReplaceWord($word['type']) ? ($word['replace'] ? $word['replace'] : '****') : '';
  129. $this->_getWordDS()->updateAllByTypeAndRelpace($word['type'], $word['replace']);
  130. $this->showMessage('success');
  131. }
  132. $ids = $word['ids'] ? explode(',', $word['ids']) : array();
  133. $ids = array_unique($ids);
  134. if (empty($ids) || !is_array($ids)) $this->showError('operate.fail');
  135. $wordService = $this->_getWordService();
  136. if ($this->_getWordDS()->isReplaceWord($word['type']) && !$word['replace']) {
  137. $this->showError('WORD:replaceword.empty');
  138. }
  139. Wind::import('SRV:word.dm.PwWordDm');
  140. $dm = new PwWordDm();/* @var $dm PwWordDm */
  141. $dm->setWordType($word['type'] ? $word['type'] : 1);
  142. $word['replace'] && $dm->setWordReplace($word['replace']);
  143. if (($result = $this->_getWordDS()->batchUpdate($ids, $dm))instanceof PwError) {
  144. $this->showError($result->getError());
  145. };
  146. $this->_getWordFilter()->updateCache();
  147. $this->showMessage('success');
  148. }
  149. public function searchAction() {
  150. list($keyword, $type, $ischeckAll, $page, $perpage) = $this->getInput(array('keyword', 'type', '_check', 'page', 'perpage'));
  151. $page < 1 && $page = 1;
  152. $perpage = $perpage ? $perpage : 20;
  153. list($offset, $limit) = Pw::page2limit($page, $perpage);
  154. Wind::import('SRV:word.vo.PwWordSo');
  155. $wordSo = new PwWordSo(); /* @var $wordSo PwWordSo */
  156. $keyword && $wordSo->setWord($keyword);
  157. $type > 0 && $wordSo->setWordType($type);
  158. $total = $this->_getWordDS()->countSearchWord($wordSo);
  159. $wordList = $total ? $this->_getWordDS()->searchWord($wordSo, $limit, $offset) : array();
  160. $this->setOutput($total, 'total');
  161. $this->setOutput($wordList, 'wordList');
  162. $this->setOutput($this->_getWordDS()->getTypeMap(), 'typeList');
  163. $this->setOutput(Wekit::C($this->_configName), 'config');
  164. $this->setOutput($page, 'page');
  165. $this->setOutput('search', 'action');
  166. $this->setOutput($perpage, 'perpage');
  167. $this->setOutput($ischeckAll, 'ischeckAll');
  168. $this->setOutput(array(
  169. 'keyword' => $keyword,
  170. 'type' => $type,
  171. '_check'=> $ischeckAll,
  172. 'perpage'=> $perpage
  173. ), 'args');
  174. $this->setTemplate('manage_run');
  175. }
  176. public function exportAction() {
  177. $wordService = $this->_getWordService();
  178. $word = $this->_getWordDS()->fetchAllWord();
  179. $content = '';
  180. foreach ($word as $value) {
  181. $content .= sprintf('%s|%s', $value['word'], $value['word_type']);
  182. $content .= $this->_getWordDS()->isReplaceWord($value['word_type']) ? sprintf('|%s', $value['word_replace']) : '';
  183. $content .= "\r\n";
  184. }
  185. $filename = sprintf('%s.txt','PwFilterWord');
  186. header('Last-Modified: '.gmdate('D, d M Y H:i:s',Pw::getTime()+86400).' GMT');
  187. header('Cache-control: no-cache');
  188. header('Content-Encoding: none');
  189. header('Content-Disposition: attachment; filename="'.$filename.'"');
  190. header('Content-type: txt');
  191. header('Content-Length: '.strlen($content));
  192. echo $content;
  193. exit;
  194. }
  195. public function importAction() {
  196. }
  197. public function doimportAction() {
  198. Wind::import('SRV:upload.action.PwWordUpload');
  199. Wind::import('LIB:upload.PwUpload');
  200. $bhv = new PwWordUpload();
  201. $upload = new PwUpload($bhv);
  202. if (($result = $upload->check()) === true) {
  203. $result = $upload->execute();
  204. }
  205. if ($result !== true) {
  206. $error = $result->getError();
  207. if (is_array($error)) {
  208. list($error, ) = $error;
  209. if ($error == 'upload.ext.error') {
  210. $this->showError('WORD:ext.error');
  211. }
  212. }
  213. $this->showError($result->getError());
  214. }
  215. $source = $bhv->getAbsoluteFile();
  216. if (!WindFile::isFile($source)) $this->showError('operate.fail');
  217. $content = WindFile::read($source);
  218. pw::deleteAttach($bhv->dir.$bhv->filename, 0, $bhv->isLocal);
  219. $content = explode("\n", $content);
  220. if (!$content) $this->showError('WORD:import.data.empty');
  221. $wordService = $this->_getWordService();
  222. $typeMap = $this->_getWordDS()->getTypeMap();
  223. Wind::import('SRV:word.dm.PwWordDm');
  224. foreach ($content as $value) {
  225. list($word, $type, $replace) = $this->_parseTextUseInImport($value, $typeMap);
  226. if (!$word || !$type || ($wordService->isExistWord($word))) continue;
  227. $dm = new PwWordDm();/* @var $dm PwWordDm */
  228. $dm->setWord($word)->setWordType($type);
  229. $replace = $this->_getWordDS()->isReplaceWord($type) ? ($replace ? $replace : '****') : '';
  230. $dm->setWordReplace($replace);
  231. $this->_getWordDS()->add($dm);
  232. }
  233. $this->_getWordFilter()->updateCache();
  234. $this->showMessage('success');
  235. }
  236. public function setconfigAction() {
  237. $config = $this->getInput('config');
  238. $configService = new PwConfigSet($this->_configName);
  239. $configService->set('istip', intval($config['tip']))->flush();
  240. $this->showMessage('success');
  241. }
  242. public function _parseTextUseInImport($text, $typeMap) {
  243. list($word, $type, $replace) = explode("|", $text);
  244. $word = trim($word);
  245. $type = in_array($type, array_keys($typeMap)) ? $type : 1;
  246. $replace = trim($replace);
  247. return array($word, $type, $replace);
  248. }
  249. private function _syncHelper() {
  250. $syncStatus = $this->_getWordSyncService()->status;
  251. $this->setOutput($syncStatus, 'syncStatus');
  252. if (!$syncStatus) return false;
  253. $this->_getWordSyncService()->setSyncType(($this->_getWordDS()->countByFrom(1) ? 'increase' : 'all'));
  254. $this->setOutput(array(
  255. 'lasttime' => $this->_getWordSyncService()->lastTimeFromPlatform,
  256. 'syncnum' => $this->_getWordSyncService()->getSyncNum()
  257. ), 'sync');
  258. return true;
  259. }
  260. /**
  261. * get PwWordService
  262. *
  263. * @return PwWordService
  264. */
  265. private function _getWordService() {
  266. return Wekit::load('word.srv.PwWordService');
  267. }
  268. /**
  269. * get PwWordFilter
  270. *
  271. * @return PwWordFilter
  272. */
  273. private function _getWordFilter() {
  274. return Wekit::load('word.srv.PwWordFilter');
  275. }
  276. /**
  277. * get PwWord
  278. *
  279. * @return PwWord
  280. */
  281. private function _getWordDS() {
  282. return Wekit::load('word.PwWord');
  283. }
  284. }