custom_book.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. require_once "../path.php";
  3. require_once "../db/table.php";
  4. require_once '../hostsetting/function.php';
  5. class CustomBook extends Table
  6. {
  7. function __construct($redis=false) {
  8. parent::__construct(_FILE_DB_USER_CUSTOM_BOOK_, "custom_book", "", "",$redis);
  9. }
  10. public function new($title,$data,$lang)
  11. {
  12. $respond['status']=0;
  13. $respond['message']="";
  14. $respond['content']="";
  15. $sent = explode("\n",$data);
  16. if($sent && count($sent)>0){
  17. $setting = new Hostsetting();
  18. $max_book = $setting->get("max_book_number");
  19. if($max_book){
  20. $currBook = $max_book+1;
  21. $setbooknum = $setting->set("max_book_number",$currBook);
  22. if($setbooknum==false){
  23. $respond["status"]=1;
  24. $respond["message"]="设置书号错误";
  25. return $respond;
  26. }
  27. }
  28. else{
  29. $respond["status"]=1;
  30. $respond["message"]="获取书号错误";
  31. return $respond;
  32. }
  33. $query="INSERT INTO {$this->table} ('book_id','title','owner','lang','status','modify_time','create_time') VALUES (?, ?, ?, ?, ?, ?, ?)";
  34. $stmt = $this->execute($query,array($currBook,$title,$_COOKIE["userid"],$lang,10,mTime(),mTime()));
  35. if($stmt){
  36. $CSent = new CustomBookSentence($this->redis);
  37. $respond = $CSent->insert($currBook,$sent,$lang);
  38. }
  39. else{
  40. $respond["status"]=1;
  41. $respond["message"]="插入新书失败";
  42. }
  43. }
  44. return $respond;
  45. }
  46. }
  47. class CustomBookSentence extends Table
  48. {
  49. function __construct($redis=false) {
  50. parent::__construct(_FILE_DB_USER_CUSTOM_BOOK_, "custom_book_sentence", "", "",$redis);
  51. }
  52. public function insert($book,$content,$lang)
  53. {
  54. $respond['status']=0;
  55. $respond['message']="";
  56. $respond['content']="";
  57. # 开始一个事务,关闭自动提交
  58. $this->dbh->beginTransaction();
  59. $query="INSERT INTO custom_book_sentence ('book','paragraph','begin','end','length','text','lang','owner','status','create_time','modify_time') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  60. $sth = $this->dbh->prepare($query);
  61. $para = 1;
  62. $sentNum = 0;
  63. $newText = "";
  64. foreach ($content as $data) {
  65. $data = trim($data);
  66. if($data==""){
  67. $para++;
  68. $sentNum = 0;
  69. $newText .="\n";
  70. continue;
  71. }
  72. else{
  73. $sentNum=$sentNum+10;
  74. }
  75. if(mb_substr($data,0,2,"UTF-8")=="{{"){
  76. $newText .=$data."\n";
  77. }
  78. else{
  79. $newText .='{{'."{$book}-{$para}-{$sentNum}-{$sentNum}"."}}\n";
  80. $sth->execute(
  81. array(
  82. $book,
  83. $para,
  84. $sentNum,
  85. $sentNum,
  86. mb_strlen($data,"UTF-8"),
  87. $data,
  88. $lang,
  89. $_COOKIE["userid"],
  90. 10,
  91. mTime(),
  92. mTime()
  93. ));
  94. }
  95. }
  96. $this->dbh->commit();
  97. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  98. # 识别错误且回滚更改
  99. $this->dbh->rollBack();
  100. $error = $this->dbh->errorInfo();
  101. $respond['status']=1;
  102. $respond['message']=$error[2];
  103. $respond['content']="";
  104. }
  105. else{
  106. $respond['status']=0;
  107. $respond['message']="成功";
  108. $respond['content']=$newText;
  109. }
  110. return $respond;
  111. }
  112. public function getText($book,$para,$start,$end){
  113. $query="SELECT text FROM custom_book_sentence WHERE book = ? AND paragraph = ? AND begin=? AND end = ?";
  114. $result = $this->fetch($query,array($book,$para,$start,$end));
  115. if($result){
  116. return $result["text"];
  117. }
  118. else{
  119. return "unkow";
  120. }
  121. }
  122. }
  123. ?>