term.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. //查询term字典
  3. require_once "../config.php";
  4. require_once "../public/_pdo.php";
  5. require_once '../public/load_lang.php';
  6. require_once '../public/function.php';
  7. require_once __DIR__."/../public/snowflakeid.php";
  8. $snowflake = new SnowFlakeId();
  9. //is login
  10. if (isset($_COOKIE["username"])) {
  11. $username = $_COOKIE["username"];
  12. } else {
  13. $username = "";
  14. }
  15. if (isset($_GET["language"])) {
  16. $currLanguage = $_GET["language"];
  17. } else {
  18. if (isset($_COOKIE["language"])) {
  19. $currLanguage = $_COOKIE["language"];
  20. } else {
  21. $currLanguage = "en";
  22. }
  23. }
  24. if (isset($_GET["op"])) {
  25. $op = $_GET["op"];
  26. } else if (isset($_POST["op"])) {
  27. $op = $_POST["op"];
  28. }
  29. if (isset($_GET["word"])) {
  30. $word = mb_strtolower($_GET["word"], 'UTF-8');
  31. $org_word = $word;
  32. }
  33. if (isset($_GET["guid"])) {
  34. $_guid = $_GET["guid"];
  35. }
  36. if (isset($_GET["username"])) {
  37. $username = $_GET["username"];
  38. }
  39. global $PDO;
  40. PDO_Connect( _FILE_DB_TERM_);
  41. switch ($op) {
  42. case "pre": //预查询
  43. {
  44. if(trim($word)==""){
  45. echo json_encode(array(), JSON_UNESCAPED_UNICODE);
  46. break;
  47. }
  48. $query = "SELECT word,meaning from "._TABLE_TERM_." where \"word_en\" like " . $PDO->quote($word . '%') . " OR \"word\" like " . $PDO->quote($word . '%') . " group by word limit 0,10";
  49. $Fetch = PDO_FetchAll($query);
  50. if (count($Fetch) < 3) {
  51. $query = "SELECT word,meaning from "._TABLE_TERM_." where \"word_en\" like " . $PDO->quote('%' . $word . '%') . " OR \"word\" like " . $PDO->quote('%' . $word . '%') . " group by word limit 0,10";
  52. $Fetch2 = PDO_FetchAll($query);
  53. //去掉重复的
  54. foreach ($Fetch2 as $onerow) {
  55. $found = false;
  56. foreach ($Fetch as $oldArray) {
  57. if ($onerow["word"] == $oldArray["word"]) {
  58. $found = true;
  59. break;
  60. }
  61. }
  62. if ($found == false) {
  63. array_push($Fetch, $onerow);
  64. }
  65. }
  66. if (count($Fetch) < 8) {
  67. $query = "SELECT word,meaning from "._TABLE_TERM_." where \"meaning\" like " . $PDO->quote($word . '%') . " OR \"other_meaning\" like " . $PDO->quote($word . '%') . " group by word limit 0,10";
  68. $Fetch3 = PDO_FetchAll($query);
  69. $Fetch = array_merge($Fetch, $Fetch3);
  70. if (count($Fetch) < 8) {
  71. $query = "SELECT word,meaning from "._TABLE_TERM_." where \"meaning\" like " . $PDO->quote('%' . $word . '%') . " OR \"other_meaning\" like " . $PDO->quote('%' . $word . '%') . " group by word limit 0,10";
  72. $Fetch4 = PDO_FetchAll($query);
  73. //去掉重复的
  74. foreach ($Fetch4 as $onerow) {
  75. $found = false;
  76. foreach ($Fetch as $oldArray) {
  77. if ($onerow["word"] == $oldArray["word"]) {
  78. $found = true;
  79. break;
  80. }
  81. }
  82. if ($found == false) {
  83. array_push($Fetch, $onerow);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. echo json_encode($Fetch, JSON_UNESCAPED_UNICODE);
  90. break;
  91. }
  92. case "my":
  93. {
  94. $query = "select guid,word,meaning,other_meaning,language from "._TABLE_TERM_." where owner= ? ";
  95. $Fetch = PDO_FetchAll($query, array($_COOKIE["userid"]));
  96. $iFetch = count($Fetch);
  97. if ($iFetch > 0) {
  98. echo json_encode($Fetch, JSON_UNESCAPED_UNICODE);
  99. } else {
  100. echo json_encode(array(), JSON_UNESCAPED_UNICODE);
  101. }
  102. break;
  103. }
  104. case "allpali":
  105. {
  106. $query = "select word from "._TABLE_TERM_." group by word";
  107. $Fetch = PDO_FetchAll($query);
  108. $iFetch = count($Fetch);
  109. if ($iFetch > 0) {
  110. echo json_encode($Fetch, JSON_UNESCAPED_UNICODE);
  111. }
  112. break;
  113. }
  114. case "allmean":
  115. {
  116. $query = "select meaning from "._TABLE_TERM_." where \"word\" = " . $PDO->quote($word) . " group by meaning";
  117. $Fetch = PDO_FetchAll($query);
  118. foreach ($Fetch as $one) {
  119. echo "<a>" . $one["meaning"] . "</a> ";
  120. }
  121. //echo json_encode($Fetch, JSON_UNESCAPED_UNICODE);
  122. break;
  123. }
  124. case "load_id":
  125. {
  126. if (isset($_GET["id"])) {
  127. $id = $_GET["id"];
  128. $query = "select * from "._TABLE_TERM_." where \"guid\" = " . $PDO->quote($id);
  129. $Fetch = PDO_FetchAll($query);
  130. echo json_encode($Fetch, JSON_UNESCAPED_UNICODE);
  131. } else {
  132. echo json_encode(array(), JSON_UNESCAPED_UNICODE);
  133. }
  134. break;
  135. }
  136. case "search":
  137. {
  138. if (!isset($word)) {
  139. return;
  140. }
  141. if (trim($word) == "") {
  142. return;
  143. }
  144. echo "<div class='pali'>{$word}</div>";
  145. //查本人数据
  146. echo "<div></div>"; //My Term
  147. $query = "select * from "._TABLE_TERM_." where word = ? AND owner = ? limit 30";
  148. $Fetch = PDO_FetchAll($query, array($word, $_COOKIE["userid"]));
  149. $iFetch = count($Fetch);
  150. if ($iFetch > 0) {
  151. for ($i = 0; $i < $iFetch; $i++) {
  152. $mean = $Fetch[$i]["meaning"];
  153. $guid = $Fetch[$i]["guid"];
  154. $dict_list[$guid] = $Fetch[$i]["owner"];
  155. echo "<div class='dict_word'>";
  156. echo "<a name='ref_dict_$guid'></a>";
  157. echo "<div id='term_dict_my_$guid'>";
  158. echo "<div class='dict'>{$_local->gui->my_term}</div>";
  159. echo "<div class='mean'><span>" . $mean . "</span>";
  160. echo "<span class='other_mean' style='margin-right: auto;'>(" . $Fetch[$i]["other_meaning"] . ")</span></div>";
  161. echo "<div class='tag'>{$Fetch[$i]["tag"]}</div>";
  162. echo "<div class='mean'>{$Fetch[$i]["channal"]}</div>";
  163. echo "<div class='mean'>{$Fetch[$i]["language"]}</div>";
  164. echo "<div class='term_note' status=0>" . $Fetch[$i]["note"] . "</div>";
  165. echo "</div>";
  166. //编辑词条表单
  167. echo "<div id='term_dict_my_edit_$guid' style='display:none'>";
  168. echo "<input type='hidden' id='term_edit_word_$guid' value='$word' />";
  169. echo "<div class='mean' style='display:flex;'><span style='flex:1;'>{$_local->gui->first_choice_word}:</span>";
  170. echo "<input type='input' style='flex:3;' placeholder='{$_local->gui->required}' id='term_edit_mean_{$guid}' value='$mean' /></div>"; //'意思'
  171. echo "<div class='mean' style='display:flex;'><span style='flex:1;'>{$_local->gui->other_meaning}:</span>";
  172. echo "<input type='input' style='flex:3;' placeholder='{$_local->gui->optional}' id='term_edit_mean2_{$guid}' value='" . $Fetch[$i]["other_meaning"] . "'/></div>"; //'备选意思(可选项)'
  173. echo "<div class='mean' style='display:flex;'><span style='flex:1;'>{$_local->gui->tag}:</span>";
  174. echo "<input type='input' style='flex:3;' placeholder='{$_local->gui->optional}' id='term_edit_tag_{$guid}' value='" . $Fetch[$i]["tag"] . "'/></div>"; //'标签'
  175. echo "<div class='mean' style='display:flex;'><span style='flex:1;'>{$_local->gui->channel}:</span>";
  176. echo "<input type='input' style='flex:3;' placeholder='{$_local->gui->optional}' id='term_edit_channal_{$guid}' value='" . $Fetch[$i]["channal"] . "'/></div>"; //'版风'
  177. echo "<div class='mean' style='display:flex;'><span style='flex:1;'>{$_local->gui->language}:</span>";
  178. echo "<input type='input' style='flex:3;' placeholder='{$_local->gui->optional}' id='term_edit_language_{$guid}' value='" . $Fetch[$i]["language"] . "'/></div>"; //'语言'
  179. echo "<div class='note'><span style='display:flex;'><span>{$_local->gui->encyclopedia} & {$_local->gui->note}:</span>";
  180. echo "<guide gid='term_pedia_sys' style='margin-left: auto;'></guide></span>";
  181. echo "<textarea width='100%' height='3em' placeholder='{$_local->gui->optional}' id='term_edit_note_$guid'>" . $Fetch[$i]["note"] . "</textarea></div>"; //'注解'
  182. echo "</div>";
  183. echo "<div id='term_edit_btn1_$guid'>";
  184. //echo "<button onclick=\"term_apply('$guid')\">{$_local->gui->apply}</button>";//Apply
  185. echo "<button onclick=\"term_edit('$guid')\">{$_local->gui->edit}</button>"; //Edit
  186. echo "</div>";
  187. echo "<div id='term_edit_btn2_{$guid}' style='display:none'>";
  188. echo "<button onclick=\"term_data_esc_edit('$guid')\">{$_local->gui->cancel}</button>"; //Cancel
  189. echo "<button onclick=\"term_data_save('$guid')\">{$_local->gui->save}</button>"; //保存
  190. echo "</div>";
  191. echo "</div>";
  192. }
  193. }
  194. //新建词条
  195. echo "<div class='dict_word'>";
  196. echo "<button id='new_term_button' onclick=\"term_show_new()\">{$_local->gui->new}</button>";
  197. echo "<div id='term_new_recorder' style='display:none;'>";
  198. echo "<div class='dict'>" . $_local->gui->new_technic_term . "</div>"; //New Techinc Term
  199. echo "<div class='mean' style='display:flex;'><span style='flex:1;'>{$_local->gui->pali_word}:</span>";
  200. echo "<input type='input' style='flex:3;' placeholder='{$_local->gui->required}' id='term_new_word' value='{$word}' /></div>"; //'拼写'
  201. echo "<div class='mean' style='display:flex;'><span style='flex:1;'>{$_local->gui->first_choice_word}:</span>";
  202. echo "<input type='input' style='flex:3;' placeholder='{$_local->gui->required}' id='term_new_mean'/></div>"; //'意思'
  203. echo "<div class='mean' style='display:flex;'><span style='flex:1;'>{$_local->gui->other_meaning}:</span>";
  204. echo "<input type='input' style='flex:3;' placeholder='{$_local->gui->optional}' id='term_new_mean2'/></div>"; //'备选意思(可选项)'
  205. echo "<div class='mean' style='display:flex;'><span style='flex:1;'>{$_local->gui->tag}:</span>";
  206. echo "<input type='input' style='flex:3;' placeholder='{$_local->gui->optional}' id='term_new_tag'/></div>"; //'标签'
  207. echo "<div class='mean' style='display:flex;'><span style='flex:1;'>{$_local->gui->channel}:</span>";
  208. echo "<input type='input' style='flex:3;' placeholder='{$_local->gui->optional}' id='term_new_channal'/></div>"; //'标签'
  209. echo "<div class='mean' style='display:flex;'><span style='flex:1;'>{$_local->gui->language}:</span>";
  210. echo "<input type='input' style='flex:3;' placeholder='{$_local->gui->optional}' id='term_new_language'/></div>"; //'标签'
  211. echo "<div class='note'><span style='display:flex;'><span>{$_local->gui->encyclopedia} & {$_local->gui->note}:</span>";
  212. echo "<guide gid='term_pedia_sys' style='margin-left: auto;'></guide></span>";
  213. echo "<textarea width='100%' height='3em' placeholder='{$_local->gui->optional}' id='term_new_note'></textarea></div>"; //'注解'
  214. echo "<button onclick=\"term_data_save('')\">{$_local->gui->save}</button>"; //保存
  215. echo "</div>";
  216. echo "</div>";
  217. //查他人数据
  218. $query = "SELECT * FROM "._TABLE_TERM_." WHERE word = ? AND owner <> ? LIMIT 30";
  219. $Fetch = PDO_FetchAll($query, array($word, $_COOKIE["userid"]));
  220. $iFetch = count($Fetch);
  221. if ($iFetch > 0) {
  222. for ($i = 0; $i < $iFetch; $i++) {
  223. $mean = $Fetch[$i]["meaning"];
  224. $guid = $Fetch[$i]["guid"];
  225. $dict_list[$guid] = $Fetch[$i]["owner"];
  226. echo "<div class='dict_word'>";
  227. echo "<a name='ref_dict_$guid'></a>";
  228. echo "<div class='dict'>" . $Fetch[$i]["owner"] . "</div>";
  229. echo "<div class='mean'>" . $mean . "</div>";
  230. echo "<div class='other_mean'>" . $Fetch[$i]["other_meaning"] . "</div>";
  231. echo "<div class='term_note'>" . $Fetch[$i]["note"] . "</div>";
  232. echo "<button onclick=\"term_data_copy_to_me($guid)\">{$_local->gui->copy}</button>"; //复制
  233. echo "</div>";
  234. }
  235. }
  236. echo "<div id='dictlist'>";
  237. echo "</div>";
  238. break;
  239. }
  240. case "copy": //拷贝到我的字典
  241. {
  242. $query = "select * from "._TABLE_TERM_." where \"guid\" = " . $PDO->quote($_GET["wordid"]);
  243. $Fetch = PDO_FetchAll($query);
  244. $iFetch = count($Fetch);
  245. if ($iFetch > 0) {
  246. /* 开始一个事务,关闭自动提交 */
  247. $PDO->beginTransaction();
  248. $query = "INSERT INTO "._TABLE_TERM_."
  249. (
  250. 'id',
  251. 'guid',
  252. 'word',
  253. 'word_en',
  254. 'meaning',
  255. 'other_meaning',
  256. 'note',
  257. 'tag',
  258. 'owner',
  259. 'editor_id',
  260. 'create_time',
  261. 'update_time',
  262. ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
  263. $stmt = $PDO->prepare($query);
  264. {
  265. $stmt->execute(array(
  266. $snowflake->id(),
  267. UUID::v4,
  268. $Fetch[0]["word"],
  269. $Fetch[0]["word_en"],
  270. $Fetch[0]["meaning"],
  271. $Fetch[0]["other_meaning"],
  272. $Fetch[0]["note"],
  273. $Fetch[0]["tag"],
  274. $_COOKIE['user_uid'],
  275. $_COOKIE['user_id'],
  276. mTime(),
  277. mTime()
  278. ));
  279. }
  280. /* 提交更改 */
  281. $PDO->commit();
  282. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  283. $error = PDO_ErrorInfo();
  284. echo "error - $error[2] <br>";
  285. } else {
  286. echo "updata ok.";
  287. }
  288. }
  289. break;
  290. }
  291. case "extract":
  292. {
  293. if (isset($_POST["words"])) {
  294. $words = $_POST["words"];
  295. }
  296. if (isset($_POST["authors"])) {
  297. $authors = str_getcsv($_POST["authors"]);
  298. }
  299. $queryLang = $currLanguage . "%";
  300. $query = "SELECT * from "._TABLE_TERM_." where \"word\" in {$words} AND language like ? limit 1000";
  301. $Fetch = PDO_FetchAll($query, array($queryLang));
  302. $iFetch = count($Fetch);
  303. echo json_encode($Fetch, JSON_UNESCAPED_UNICODE);
  304. break;
  305. }
  306. case "sync":
  307. {
  308. $time = $_GET["time"];
  309. $query = "SELECT guid,modify_time from "._TABLE_TERM_." where receive_time>'{$time}' limit 1000";
  310. $Fetch = PDO_FetchAll($query);
  311. $iFetch = count($Fetch);
  312. echo json_encode($Fetch, JSON_UNESCAPED_UNICODE);
  313. break;
  314. }
  315. case "get":
  316. {
  317. $Fetch = array();
  318. if (isset($guid)) {
  319. $query = "select * from "._TABLE_TERM_." where \"guid\" = '{$guid}'";
  320. } else if (isset($word)) {
  321. $query = "select * from "._TABLE_TERM_." where \"word\" = '{$word}'";
  322. } else {
  323. echo "[]";
  324. return;
  325. }
  326. $Fetch = PDO_FetchAll($query);
  327. $iFetch = count($Fetch);
  328. echo json_encode($Fetch, JSON_UNESCAPED_UNICODE);
  329. break;
  330. }
  331. }