friend.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. {
  10. var $db;
  11. var $base;
  12. function __construct(&$base)
  13. {
  14. $this->friendmodel($base);
  15. }
  16. function friendmodel(&$base)
  17. {
  18. $this->base = $base;
  19. $this->db = $base->db;
  20. }
  21. function add($uid, $friendid, $comment = '')
  22. {
  23. $direction = $this->db->result_first("SELECT direction FROM " . UC_DBTABLEPRE . "friends WHERE uid='$friendid' AND friendid='$uid' LIMIT 1");
  24. if ($direction == 1)
  25. {
  26. $this->db->query("INSERT INTO " . UC_DBTABLEPRE . "friends SET uid='$uid', friendid='$friendid', comment='$comment', direction='3'", 'SILENT');
  27. $this->db->query("UPDATE " . UC_DBTABLEPRE . "friends SET direction='3' WHERE uid='$friendid' AND friendid='$uid'");
  28. return 1;
  29. }
  30. elseif ($direction == 2)
  31. {
  32. return 1;
  33. }
  34. elseif ($direction == 3)
  35. {
  36. return -1;
  37. }
  38. else
  39. {
  40. $this->db->query("INSERT INTO " . UC_DBTABLEPRE . "friends SET uid='$uid', friendid='$friendid', comment='$comment', direction='1'", 'SILENT');
  41. return $this->db->insert_id();
  42. }
  43. }
  44. function delete($uid, $friendids)
  45. {
  46. $friendids = $this->base->implode($friendids);
  47. $this->db->query("DELETE FROM " . UC_DBTABLEPRE . "friends WHERE uid='$uid' AND friendid IN ($friendids)");
  48. $affectedrows = $this->db->affected_rows();
  49. if ($affectedrows > 0)
  50. {
  51. $this->db->query("UPDATE " . UC_DBTABLEPRE . "friends SET direction=1 WHERE uid IN ($friendids) AND friendid='$uid' AND direction='3'");
  52. }
  53. return $affectedrows;
  54. }
  55. function get_totalnum_by_uid($uid, $direction = 0)
  56. {
  57. $sqladd = '';
  58. if ($direction == 0)
  59. {
  60. $sqladd = "uid='$uid'";
  61. }
  62. elseif ($direction == 1)
  63. {
  64. $sqladd = "uid='$uid' AND direction='1'";
  65. }
  66. elseif ($direction == 2)
  67. {
  68. $sqladd = "friendid='$uid' AND direction='1'";
  69. }
  70. elseif ($direction == 3)
  71. {
  72. $sqladd = "uid='$uid' AND direction='3'";
  73. }
  74. $totalnum = $this->db->result_first("SELECT COUNT(*) FROM " . UC_DBTABLEPRE . "friends WHERE $sqladd");
  75. return $totalnum;
  76. }
  77. function get_list($uid, $page, $pagesize, $totalnum, $direction = 0)
  78. {
  79. $start = $this->base->page_get_start($page, $pagesize, $totalnum);
  80. $sqladd = '';
  81. if ($direction == 0)
  82. {
  83. $sqladd = "f.uid='$uid'";
  84. }
  85. elseif ($direction == 1)
  86. {
  87. $sqladd = "f.uid='$uid' AND f.direction='1'";
  88. }
  89. elseif ($direction == 2)
  90. {
  91. $sqladd = "f.friendid='$uid' AND f.direction='1'";
  92. }
  93. elseif ($direction == 3)
  94. {
  95. $sqladd = "f.uid='$uid' AND f.direction='3'";
  96. }
  97. if ($sqladd)
  98. {
  99. $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");
  100. return $data;
  101. }
  102. else
  103. {
  104. return array();
  105. }
  106. }
  107. function is_friend($uid, $friendids, $direction = 0)
  108. {
  109. $friendid_str = implode("', '", $friendids);
  110. $sqladd = '';
  111. if ($direction == 0)
  112. {
  113. $sqladd = "uid='$uid'";
  114. }
  115. elseif ($direction == 1)
  116. {
  117. $sqladd = "uid='$uid' AND friendid IN ('$friendid_str') AND direction='1'";
  118. }
  119. elseif ($direction == 2)
  120. {
  121. $sqladd = "friendid='$uid' AND uid IN ('$friendid_str') AND direction='1'";
  122. }
  123. elseif ($direction == 3)
  124. {
  125. $sqladd = "uid='$uid' AND friendid IN ('$friendid_str') AND direction='3'";
  126. }
  127. if ($this->db->result_first("SELECT COUNT(*) FROM " . UC_DBTABLEPRE . "friends WHERE $sqladd") == count($friendids))
  128. {
  129. return true;
  130. }
  131. else
  132. {
  133. return false;
  134. }
  135. }
  136. }