function.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. require_once "../config.php";
  3. require_once "../share/function.php";
  4. require_once "../channal/function.php";
  5. require_once "../db/table.php";
  6. function update_historay($sent_id, $user_id, $text, $landmark)
  7. {
  8. # 更新historay
  9. PDO_Connect("" . _FILE_DB_USER_SENTENCE_HISTORAY_);
  10. $query = "INSERT INTO sent_historay (sent_id, user_id, text, date, landmark) VALUES (? , ? , ? , ? , ? )";
  11. $stmt = PDO_Execute($query,
  12. array($sent_id,
  13. $user_id,
  14. $text,
  15. mTime(),
  16. $landmark,
  17. ));
  18. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  19. /* 识别错误 */
  20. $error = PDO_ErrorInfo();
  21. return $error[2];
  22. } else {
  23. #没错误
  24. return "";
  25. }
  26. }
  27. class SentPr{
  28. private $dbh_sent;
  29. private $redis;
  30. public function __construct($redis=false) {
  31. $this->dbh_sent = new PDO(_FILE_DB_SENTENCE_, "", "",array(PDO::ATTR_PERSISTENT=>true));
  32. $this->dbh_sent->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  33. $this->redis=$redis;
  34. }
  35. public function getNewPrNumber($book,$para,$begin,$end,$channel){
  36. if ($this->dbh_sent) {
  37. $query = "SELECT count(*) as ct FROM sent_pr WHERE book = ? and paragraph= ? and begin=? and end=? and channel=? and status=1 ";
  38. $stmt = $this->dbh_sent->prepare($query);
  39. $stmt->execute(array($book,$para,$begin,$end,$channel));
  40. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  41. if($result){
  42. return $result["ct"];
  43. }
  44. else{
  45. return false;
  46. }
  47. }
  48. else{
  49. return false;
  50. }
  51. }
  52. public function getAllPrNumber($book,$para,$begin,$end,$channel){
  53. if ($this->dbh_sent) {
  54. $query = "SELECT count(*) as ct FROM sent_pr WHERE book = ? and paragraph= ? and begin=? and end=? and channel=? ";
  55. $stmt = $this->dbh_sent->prepare($query);
  56. $stmt->execute(array($book,$para,$begin,$end,$channel));
  57. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  58. if($result){
  59. return $result["ct"];
  60. }
  61. else{
  62. return false;
  63. }
  64. }
  65. else{
  66. return false;
  67. }
  68. }
  69. public function getPrData($book,$para,$begin,$end,$channel){
  70. if ($this->dbh_sent) {
  71. $query = "SELECT id,book,paragraph,begin,end,channel,text,editor,modify_time FROM sent_pr WHERE book = ? and paragraph= ? and begin=? and end=? and channel=? and status=1 limit 0,100";
  72. $stmt = $this->dbh_sent->prepare($query);
  73. $stmt->execute(array($book,$para,$begin,$end,$channel));
  74. $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
  75. if($result){
  76. return $result;
  77. }
  78. else{
  79. return false;
  80. }
  81. }
  82. else{
  83. return false;
  84. }
  85. }
  86. public function getPrDataById($id){
  87. if ($this->dbh_sent) {
  88. $query = "SELECT id,book,paragraph,begin,end,channel,text,editor,modify_time FROM sent_pr WHERE id = ? ";
  89. $stmt = $this->dbh_sent->prepare($query);
  90. $stmt->execute(array($id));
  91. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  92. if($result){
  93. return $result;
  94. }
  95. else{
  96. return false;
  97. }
  98. }
  99. else{
  100. return false;
  101. }
  102. }
  103. public function setPrData($id,$text){
  104. if ($this->dbh_sent) {
  105. $query = "UPDATE sent_pr set text=? ,modify_time=? WHERE id = ? and editor= ? ";
  106. $stmt = $this->dbh_sent->prepare($query);
  107. $stmt->execute(array($text,mTime(),$id,$_COOKIE["userid"]));
  108. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  109. /* 识别错误 */
  110. $error = $this->dbh_sent->errorInfo();
  111. //$respond['message'] = $error[2];
  112. return false;
  113. } else {
  114. #没错误 更新历史记录
  115. return true;
  116. }
  117. }
  118. else{
  119. return false;
  120. }
  121. }
  122. }
  123. class Sent_his
  124. {
  125. private $dbh_his;
  126. private $errorMsg="";
  127. }
  128. class Sent_DB
  129. {
  130. private $dbh_sent;
  131. private $dbh_his;
  132. private $errorMsg="";
  133. public function __construct($redis=false) {
  134. $this->dbh_sent = new PDO(_FILE_DB_SENTENCE_, "", "",array(PDO::ATTR_PERSISTENT=>true));
  135. $this->dbh_sent->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  136. $this->dbh_his = new PDO(_FILE_DB_USER_SENTENCE_HISTORAY_, "", "",array(PDO::ATTR_PERSISTENT=>true));
  137. $this->dbh_his->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  138. }
  139. public function getError(){
  140. return $errorMsg;
  141. }
  142. #获取单个句子数据
  143. public function getSent($book,$para,$begin,$end,$channel){
  144. $query = "SELECT * FROM sentence WHERE book= ? AND paragraph= ? AND begin= ? AND end= ? AND channal = ? ";
  145. $stmt = $this->dbh_sent->prepare($query);
  146. if($stmt){
  147. $stmt->execute(array($book,$para,$begin,$end,$channel));
  148. $fetchDest = $stmt->fetch(PDO::FETCH_ASSOC);
  149. return $fetchDest;
  150. }
  151. else{
  152. return false;
  153. }
  154. }
  155. public function getSentDefaultByLan($book,$para,$begin,$end,$lang){
  156. $query = "SELECT * FROM sentence WHERE book= ? AND paragraph= ? AND begin= ? AND end= ? AND channal = ? ";
  157. $stmt = $this->dbh_sent->prepare($query);
  158. if($stmt){
  159. $stmt->execute(array($book,$para,$begin,$end,$channel));
  160. $fetchDest = $stmt->fetch(PDO::FETCH_ASSOC);
  161. return $fetchDest;
  162. }
  163. else{
  164. return false;
  165. }
  166. }
  167. public function update($arrData){
  168. /* 修改现有数据 */
  169. if (count($arrData) > 0) {
  170. //add_edit_event(_SENT_EDIT_, "{$oldList[0]["book"]}-{$oldList[0]["paragraph"]}-{$oldList[0]["begin"]}-{$oldList[0]["end"]}@{$oldList[0]["channal"]}");
  171. $this->dbh_sent->beginTransaction();
  172. if(isset($arrData[0]["book"]) && isset($arrData[0]["paragraph"]) && isset($arrData[0]["begin"]) && isset($arrData[0]["end"]) && isset($arrData[0]["channal"])){
  173. $query = "UPDATE sentence SET text = ? , strlen = ? , editor=?, modify_time= ? , receive_time = ? where book = ? and paragraph=? and [begin]=? and [end]=? and channal=? ";
  174. $sth = $this->dbh_sent->prepare($query);
  175. foreach ($arrData as $data) {
  176. if(!isset($data["modify_time"])){
  177. $data["modify_time"] = mTime();
  178. }
  179. $sth->execute(array($data["text"],mb_strlen($data["text"],"UTF-8"),$data["editor"],$data["modify_time"],mTime(),$data["book"],$data["paragraph"],$data["begin"],$data["end"],$data["channal"]));
  180. }
  181. }
  182. else if(isset($arrData[0]["id"])){
  183. $query = "UPDATE sentence SET text = ? , strlen = ? , editor=?, modify_time= ? ,receive_time = ? where id= ? ";
  184. $sth = $this->dbh_sent->prepare($query);
  185. foreach ($arrData as $data) {
  186. if(!isset($data["modify_time"])){
  187. $data["modify_time"]=mTime();
  188. }
  189. $sth->execute(array($data["text"],mb_strlen($data["text"],"UTF-8"),$data["editor"],$data["modify_time"],mTime(),$data["id"]));
  190. }
  191. }
  192. $this->dbh_sent->commit();
  193. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  194. /* 识别错误且回滚更改 */
  195. $this->dbh_sent->rollBack();
  196. $error = $this->dbh_sent->errorInfo();
  197. $this->errorMsg = $error[2];
  198. return false;
  199. } else {
  200. #没错误 添加log
  201. $this->errorMsg = "";
  202. return true;
  203. }
  204. }
  205. else{
  206. $this->errorMsg = "";
  207. return true;
  208. }
  209. }
  210. public function insert($arrData){
  211. # 插入新数据
  212. if (count($arrData) > 0) {
  213. //add_edit_event(_SENT_NEW_, "{$newList[0]["book"]}-{$newList[0]["paragraph"]}-{$newList[0]["begin"]}-{$newList[0]["end"]}@{$newList[0]["channal"]}");
  214. $this->dbh_sent->beginTransaction();
  215. $query = "INSERT INTO sentence (id,
  216. parent,
  217. book,
  218. paragraph,
  219. begin,
  220. end,
  221. channal,
  222. tag,
  223. author,
  224. editor,
  225. text,
  226. language,
  227. ver,
  228. status,
  229. strlen,
  230. modify_time,
  231. receive_time,
  232. create_time
  233. )
  234. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";
  235. $sth = $this->dbh_sent->prepare($query);
  236. #查询channel语言
  237. $channel_info = new Channal();
  238. foreach ($arrData as $data) {
  239. if($data["id"]==""){
  240. $data["id"] = UUID::v4();
  241. }
  242. if(!isset($data["create_time"])){
  243. $data["create_time"]=mTime();
  244. }
  245. if(!isset($data["modify_time"])){
  246. $data["modify_time"]=mTime();
  247. }
  248. $data["receive_time"]=mTime();
  249. $queryChannel = $channel_info->getChannal($data["channal"]);
  250. if ($queryChannel == false) {
  251. $lang = $data["language"];
  252. $status = 10;
  253. } else {
  254. $lang = $queryChannel["lang"];
  255. $status = $queryChannel["status"];
  256. }
  257. $sth->execute(array($data["id"],
  258. isset($data["parent"]) ? $data["parent"] : "",
  259. $data["book"],
  260. $data["paragraph"],
  261. $data["begin"],
  262. $data["end"],
  263. $data["channal"],
  264. isset($data["tag"]) ? $data["tag"] : "",
  265. $data["author"],
  266. $data["editor"],
  267. $data["text"],
  268. $lang,
  269. 1,
  270. $status,
  271. mb_strlen($data["text"], "UTF-8"),
  272. $data["modify_time"],
  273. $data["receive_time"],
  274. $data["create_time"]
  275. ));
  276. }
  277. $this->dbh_sent->commit();
  278. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  279. /* 识别错误且回滚更改 */
  280. $this->dbh_sent->rollBack();
  281. $error = $this->dbh_sent->errorInfo();
  282. $this->errorMsg = $error[2];
  283. return false;
  284. } else {
  285. $this->errorMsg = "";
  286. return true;
  287. }
  288. }
  289. else{
  290. $this->errorMsg = "";
  291. return true;
  292. }
  293. }
  294. public function send_pr($arrData){
  295. if (count($arrData) ==0) {
  296. $this->errorMsg = "";
  297. return true;
  298. }
  299. $this->dbh_sent->beginTransaction();
  300. $query = "INSERT INTO sent_pr (id,
  301. book,
  302. paragraph,
  303. begin,
  304. end,
  305. channel,
  306. tag,
  307. author,
  308. editor,
  309. text,
  310. language,
  311. status,
  312. strlen,
  313. modify_time,
  314. receive_time,
  315. create_time
  316. )
  317. VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";
  318. $stmt = $this->dbh_sent->prepare($query);
  319. foreach ($arrData as $data) {
  320. # 初始状态 1 未处理
  321. $stmt->execute(array(
  322. $data["book"],
  323. $data["para"],
  324. $data["begin"],
  325. $data["end"],
  326. $data["channal"],
  327. "",
  328. "[]",
  329. $data["editor"],
  330. $data["text"],
  331. $data["language"],
  332. 1,
  333. mb_strlen($data["text"], "UTF-8"),
  334. mTime(),
  335. mTime(),
  336. mTime(),
  337. ));
  338. }
  339. $this->dbh_sent->commit();
  340. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  341. # 识别错误
  342. $this->dbh_sent->rollBack();
  343. $error = $this->dbh_sent->errorInfo();
  344. $this->errorMsg = $error[2];
  345. return false;
  346. } else {
  347. # 没错误
  348. $this->errorMsg = "";
  349. return true;
  350. }
  351. }
  352. public function historay($arrData){
  353. if (count($arrData) ==0) {
  354. $this->errorMsg = "";
  355. return true;
  356. }
  357. $this->dbh_his->beginTransaction();
  358. # 更新historay
  359. $query = "INSERT INTO sent_historay (sent_id, user_id, text, date, landmark) VALUES (? , ? , ? , ? , ? )";
  360. $stmt = $this->dbh_his->prepare($query);
  361. foreach ($arrData as $data) {
  362. $stmt->execute(array($data["id"],
  363. $data["editor"],
  364. $data["text"],
  365. mTime(),
  366. $data["landmark"]));
  367. }
  368. $this->dbh_his->commit();
  369. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  370. /* 识别错误 */
  371. $this->dbh_his->rollBack();
  372. $error = $this->dbh_his->errorInfo();
  373. $this->errorMsg = $error[2];
  374. return false;
  375. } else {
  376. #没错误
  377. $this->errorMsg = "";
  378. return true;
  379. }
  380. }
  381. }