function.php 8.7 KB

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