| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- require_once "../path.php";
- require_once "../db/table.php";
- require_once '../hostsetting/function.php';
- class CustomBook extends Table
- {
- function __construct($redis=false) {
- parent::__construct(_FILE_DB_USER_CUSTOM_BOOK_, "custom_book", "", "",$redis);
- }
- public function new($title,$data,$lang)
- {
- $respond['status']=0;
- $respond['message']="";
- $respond['content']="";
- $sent = explode("\n",$data);
- if($sent && count($sent)>0){
- $setting = new Hostsetting();
- $max_book = $setting->get("max_book_number");
- if($max_book){
- $currBook = $max_book+1;
- $setbooknum = $setting->set("max_book_number",$currBook);
- if($setbooknum==false){
- $respond["status"]=1;
- $respond["message"]="设置书号错误";
- return $respond;
- }
- }
- else{
- $respond["status"]=1;
- $respond["message"]="获取书号错误";
- return $respond;
- }
- $query="INSERT INTO {$this->table} ('book_id','title','owner','lang','status','modify_time','create_time') VALUES (?, ?, ?, ?, ?, ?, ?)";
- $stmt = $this->execute($query,array($currBook,$title,$_COOKIE["userid"],$lang,10,mTime(),mTime()));
- if($stmt){
- $CSent = new CustomBookSentence($this->redis);
- $respond = $CSent->insert($currBook,$sent,$lang);
- }
- else{
- $respond["status"]=1;
- $respond["message"]="插入新书失败";
- }
- }
- return $respond;
- }
- }
- class CustomBookSentence extends Table
- {
- function __construct($redis=false) {
- parent::__construct(_FILE_DB_USER_CUSTOM_BOOK_, "custom_book_sentence", "", "",$redis);
- }
- public function insert($book,$content,$lang)
- {
- $respond['status']=0;
- $respond['message']="";
- $respond['content']="";
- # 开始一个事务,关闭自动提交
- $this->dbh->beginTransaction();
- $query="INSERT INTO custom_book_sentence ('book','paragraph','begin','end','length','text','lang','owner','status','create_time','modify_time') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
-
- $sth = $this->dbh->prepare($query);
-
- $para = 1;
- $sentNum = 0;
- $newText = "";
- foreach ($content as $data) {
- $data = trim($data);
- if($data==""){
- $para++;
- $sentNum = 0;
- $newText .="\n";
- continue;
- }
- else{
- $sentNum=$sentNum+10;
- }
- if(mb_substr($data,0,2,"UTF-8")=="{{"){
- $newText .=$data."\n";
- }
- else{
- $newText .='{{'."{$book}-{$para}-{$sentNum}-{$sentNum}"."}}\n";
- $sth->execute(
- array(
- $book,
- $para,
- $sentNum,
- $sentNum,
- mb_strlen($data,"UTF-8"),
- $data,
- $lang,
- $_COOKIE["userid"],
- 10,
- mTime(),
- mTime()
- ));
- }
- }
- $this->dbh->commit();
-
- if (!$sth || ($sth && $sth->errorCode() != 0)) {
- # 识别错误且回滚更改
- $this->dbh->rollBack();
- $error = $this->dbh->errorInfo();
- $respond['status']=1;
- $respond['message']=$error[2];
- $respond['content']="";
- }
- else{
- $respond['status']=0;
- $respond['message']="成功";
- $respond['content']=$newText;
- }
- return $respond;
- }
- public function getText($book,$para,$start,$end){
- $query="SELECT text FROM custom_book_sentence WHERE book = ? AND paragraph = ? AND begin=? AND end = ?";
- $result = $this->fetch($query,array($book,$para,$start,$end));
- if($result){
- return $result["text"];
- }
- else{
- return "unkow";
- }
- }
- }
- ?>
|