friend.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. [UCenter] (C)2001-2099 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: friend.php 1059 2011-03-01 07:25:09Z monkey $
  6. */
  7. !defined('IN_UC') && exit('Access Denied');
  8. class friendmodel {
  9. var $db;
  10. var $base;
  11. function __construct(&$base) {
  12. $this->friendmodel($base);
  13. }
  14. function friendmodel(&$base) {
  15. $this->base = $base;
  16. $this->db = $base->db;
  17. }
  18. function add($uid, $friendid, $comment='') {
  19. $direction = $this->db->result_first("SELECT direction FROM ".UC_DBTABLEPRE."friends WHERE uid='$friendid' AND friendid='$uid' LIMIT 1");
  20. if($direction == 1) {
  21. $this->db->query("INSERT INTO ".UC_DBTABLEPRE."friends SET uid='$uid', friendid='$friendid', comment='$comment', direction='3'", 'SILENT');
  22. $this->db->query("UPDATE ".UC_DBTABLEPRE."friends SET direction='3' WHERE uid='$friendid' AND friendid='$uid'");
  23. return 1;
  24. } elseif($direction == 2) {
  25. return 1;
  26. } elseif($direction == 3) {
  27. return -1;
  28. } else {
  29. $this->db->query("INSERT INTO ".UC_DBTABLEPRE."friends SET uid='$uid', friendid='$friendid', comment='$comment', direction='1'", 'SILENT');
  30. return $this->db->insert_id();
  31. }
  32. }
  33. function delete($uid, $friendids) {
  34. $friendids = $this->base->implode($friendids);
  35. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."friends WHERE uid='$uid' AND friendid IN ($friendids)");
  36. $affectedrows = $this->db->affected_rows();
  37. if($affectedrows > 0) {
  38. $this->db->query("UPDATE ".UC_DBTABLEPRE."friends SET direction=1 WHERE uid IN ($friendids) AND friendid='$uid' AND direction='3'");
  39. }
  40. return $affectedrows;
  41. }
  42. function get_totalnum_by_uid($uid, $direction = 0) {
  43. $sqladd = '';
  44. if($direction == 0) {
  45. $sqladd = "uid='$uid'";
  46. } elseif($direction == 1) {
  47. $sqladd = "uid='$uid' AND direction='1'";
  48. } elseif($direction == 2) {
  49. $sqladd = "friendid='$uid' AND direction='1'";
  50. } elseif($direction == 3) {
  51. $sqladd = "uid='$uid' AND direction='3'";
  52. }
  53. $totalnum = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."friends WHERE $sqladd");
  54. return $totalnum;
  55. }
  56. function get_list($uid, $page, $pagesize, $totalnum, $direction = 0) {
  57. $start = $this->base->page_get_start($page, $pagesize, $totalnum);
  58. $sqladd = '';
  59. if($direction == 0) {
  60. $sqladd = "f.uid='$uid'";
  61. } elseif($direction == 1) {
  62. $sqladd = "f.uid='$uid' AND f.direction='1'";
  63. } elseif($direction == 2) {
  64. $sqladd = "f.friendid='$uid' AND f.direction='1'";
  65. } elseif($direction == 3) {
  66. $sqladd = "f.uid='$uid' AND f.direction='3'";
  67. }
  68. if($sqladd) {
  69. $data = $this->db->fetch_all("SELECT f.*, m.username FROM ".UC_DBTABLEPRE."friends f LEFT JOIN ".UC_DBTABLEPRE."members m ON f.friendid=m.uid WHERE $sqladd LIMIT $start, $pagesize");
  70. return $data;
  71. } else {
  72. return array();
  73. }
  74. }
  75. function is_friend($uid, $friendids, $direction = 0) {
  76. $friendid_str = implode("', '", $friendids);
  77. $sqladd = '';
  78. if($direction == 0) {
  79. $sqladd = "uid='$uid'";
  80. } elseif($direction == 1) {
  81. $sqladd = "uid='$uid' AND friendid IN ('$friendid_str') AND direction='1'";
  82. } elseif($direction == 2) {
  83. $sqladd = "friendid='$uid' AND uid IN ('$friendid_str') AND direction='1'";
  84. } elseif($direction == 3) {
  85. $sqladd = "uid='$uid' AND friendid IN ('$friendid_str') AND direction='3'";
  86. }
  87. if($this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."friends WHERE $sqladd") == count($friendids)) {
  88. return true;
  89. } else {
  90. return false;
  91. }
  92. }
  93. }
  94. ?>