function.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. require_once '../path.php';
  3. require_once "../share/function.php";
  4. require_once "../db/table.php";
  5. require_once "../article/function.php";
  6. class CollectInfo
  7. {
  8. private $dbh;
  9. private $buffer;
  10. private $_redis;
  11. private $errorMsg;
  12. public function __construct($redis=false) {
  13. $dns = ""._FILE_DB_USER_ARTICLE_;
  14. $this->dbh = new PDO($dns, "", "",array(PDO::ATTR_PERSISTENT=>true));
  15. $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  16. $this->_redis=$redis;
  17. $buffer = array();
  18. }
  19. public function get($id){
  20. if(empty($id)){
  21. return array("title"=>"","id"=>"");
  22. }
  23. if(isset($buffer[$id])){
  24. return $buffer[$id];
  25. }
  26. if($this->dbh){
  27. $query = "SELECT id,title,owner,status,lang, FROM collect WHERE id= ?";
  28. $stmt = $this->dbh->prepare($query);
  29. $stmt->execute(array($id));
  30. $collect = $stmt->fetch(PDO::FETCH_ASSOC);
  31. if($collect){
  32. $buffer[$id] = $collect;
  33. return $buffer[$id];
  34. }
  35. else{
  36. $buffer[$id] =false;
  37. return $buffer[$id];
  38. }
  39. }
  40. else{
  41. $buffer[$id] = false;
  42. return $buffer[$id];
  43. }
  44. }
  45. public function getArticleList($id){
  46. if(empty($id)){
  47. return array();
  48. }
  49. if($this->dbh){
  50. $query = "SELECT article_id FROM article_list WHERE collect_id= ? limit 0,1000";
  51. $stmt = $this->dbh->prepare($query);
  52. $stmt->execute(array($id));
  53. $article_list = $stmt->fetchAll(PDO::FETCH_ASSOC);
  54. $output=array();
  55. foreach ($article_list as $key => $value) {
  56. # code...
  57. $output[]=$value["article_id"];
  58. }
  59. return $output;
  60. }
  61. else{
  62. return array();
  63. }
  64. }
  65. public function getPower($id){
  66. #查询用户对此是否有权限
  67. if(isset($_COOKIE["userid"])){
  68. $userId = $_COOKIE["userid"];
  69. }
  70. else{
  71. $userId='0';
  72. }
  73. if($this->_redis!==false){
  74. $power = $this->_redis->hGet("power://collection/".$id,$userId);
  75. if($power!==FALSE){
  76. return $power;
  77. }
  78. }
  79. $iPower = 0;
  80. $query = "SELECT owner,status FROM collect WHERE id=? ";
  81. $stmt = $this->dbh->prepare($query);
  82. $stmt->execute(array($id));
  83. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  84. if($result){
  85. if(!isset($_COOKIE["userid"])){
  86. #未登录用户
  87. if($result["status"]==30){
  88. #全网公开有读取和建议权限
  89. return 10;
  90. }
  91. else{
  92. #其他状态没有任何权限
  93. return 0;
  94. }
  95. }
  96. else{
  97. if($result["owner"]==$_COOKIE["userid"]){
  98. #自己的
  99. return 30;
  100. }
  101. else if($result["status"]>=30){
  102. #全网公开的 可以提交pr
  103. $iPower = 10;
  104. }
  105. }
  106. #查询共享权限,如果共享权限更大,覆盖上面的的
  107. $sharePower = share_get_res_power($userId,$id);
  108. if($sharePower>$iPower){
  109. $iPower=$sharePower;
  110. }
  111. $this->_redis->hSet("power://collection/".$id,$userId,$iPower);
  112. }
  113. return $iPower;
  114. }
  115. public function update($arrData){
  116. /* 修改现有数据 */
  117. if (count($arrData) > 0) {
  118. $this->dbh->beginTransaction();
  119. $query="UPDATE collect SET title = ? , subtitle = ? , summary = ?, article_list = ? , status = ? , lang = ? , receive_time= ? , modify_time= ? where id = ? ";
  120. $sth = $this->dbh->prepare($query);
  121. foreach ($arrData as $data) {
  122. $sth->execute(array($data["title"] , $data["subtitle"] ,$data["summary"], $data["article_list"] , $data["status"] , $data["lang"] , mTime() , mTime() , $data["id"]));
  123. }
  124. $this->dbh->commit();
  125. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  126. /* 识别错误且回滚更改 */
  127. $this->dbh->rollBack();
  128. $error = $this->dbh->errorInfo();
  129. $this->errorMsg = $error[2];
  130. return false;
  131. } else {
  132. #没错误 添加log
  133. $this->errorMsg = "";
  134. //更新列表
  135. $ArticleList = new ArticleList($this->_redis);
  136. foreach ($arrData as $data) {
  137. $arrList = json_decode($data["article_list"],true);
  138. $ArticleList->upgrade($data["id"],$arrList);
  139. }
  140. return true;
  141. }
  142. }
  143. else{
  144. $this->errorMsg = "";
  145. return true;
  146. }
  147. }
  148. public function insert($arrData){
  149. /* 插入新数据 */
  150. $respond=array("status"=>0,"message"=>"");
  151. if (count($arrData) > 0) {
  152. $this->dbh->beginTransaction();
  153. $query="INSERT INTO collect ( id, title , subtitle , summary , article_list , owner, lang , status , create_time , modify_time , receive_time ) VALUES ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ) ";
  154. $sth = $this->dbh->prepare($query);
  155. $newDataList=array();
  156. foreach ($arrData as $data) {
  157. $newData = array();
  158. if(isset($data["title"]) && !empty($data["title"])){
  159. $newData["title"]=$data["title"];
  160. if(isset($data["id"])){
  161. $newData["id"]=$data["id"];
  162. }
  163. else{
  164. $newData["id"]=UUID::v4();
  165. }
  166. if(isset($data["subtitle"])){
  167. $newData["subtitle"]=$data["subtitle"];
  168. }
  169. else{
  170. $newData["subtitle"]="";
  171. }
  172. if(isset($data["summary"])){
  173. $newData["summary"]=$data["summary"];
  174. }
  175. else{
  176. $newData["summary"]="";
  177. }
  178. if(isset($data["article_list"])){
  179. $newData["article_list"]=$data["article_list"];
  180. }
  181. else{
  182. $newData["article_list"]="";
  183. }
  184. if(isset($data["lang"])){
  185. $newData["lang"]=$data["lang"];
  186. }
  187. else{
  188. $newData["lang"]="";
  189. }
  190. if(isset($data["status"])){
  191. $newData["status"]=$data["status"];
  192. }
  193. else{
  194. $newData["status"]="1";
  195. }
  196. $newDataList[]=$newData;
  197. $sth->execute(array($newData["id"] , $newData["title"] , $newData["subtitle"] ,$newData["summary"], $newData["article_list"] , $_COOKIE["userid"] , $newData["lang"] , $newData["status"] , mTime() , mTime() , mTime() ));
  198. }
  199. else{
  200. $this->errorMsg="标题不能为空";
  201. }
  202. }
  203. $this->dbh->commit();
  204. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  205. /* 识别错误且回滚更改 */
  206. $this->dbh->rollBack();
  207. $error = $this->dbh->errorInfo();
  208. $this->errorMsg = $error[2];
  209. return false;
  210. } else {
  211. $this->errorMsg = "";
  212. //更新列表
  213. $ArticleList = new ArticleList($this->_redis);
  214. foreach ($newDataList as $data) {
  215. $arrList = json_decode($data["article_list"],true);
  216. $ArticleList->upgrade($data["id"],$arrList);
  217. }
  218. return true;
  219. }
  220. }
  221. else{
  222. $this->errorMsg = "";
  223. return true;
  224. }
  225. }
  226. }
  227. ?>