sns_friend.model.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * SNS好友管理
  4. *
  5. */
  6. defined('InShopNC') or exit('Access Invalid!');
  7. class sns_friendModel{
  8. /**
  9. * 好友添加
  10. *
  11. * @param array $param 添加信息数组
  12. */
  13. public function addFriend($param) {
  14. if (empty($param)){
  15. return false;
  16. }
  17. if (is_array($param)){
  18. $result = Db::insert('sns_friend',$param);
  19. return $result;
  20. }else {
  21. return false;
  22. }
  23. }
  24. /**
  25. * 好友列表
  26. *
  27. * @param array $condition 条件数组
  28. * @param string $field 显示字段
  29. * @param obj $obj_page 分页
  30. * @param string $type 查询类型
  31. */
  32. public function listFriend($condition,$field='*',$obj_page='',$type='simple') {
  33. //得到条件语句
  34. $condition_str = $this->getCondition($condition);
  35. $param = array();
  36. switch ($type){
  37. case 'simple':
  38. $param['table'] = 'sns_friend';
  39. break;
  40. case 'detail':
  41. $param['table'] = 'sns_friend,member';
  42. $param['join_type'] = 'INNER JOIN';
  43. $param['join_on'] = array('sns_friend.friend_tomid=member.member_id');
  44. break;
  45. case 'fromdetail':
  46. $param['table'] = 'sns_friend,member';
  47. $param['join_type'] = 'INNER JOIN';
  48. $param['join_on'] = array('sns_friend.friend_frommid=member.member_id');
  49. break;
  50. }
  51. $param['where'] = $condition_str;
  52. $param['field'] = $field;
  53. $param['order'] = $condition['order'] ? $condition['order'] : 'sns_friend.friend_id desc';
  54. $param['limit'] = $condition['limit'];
  55. $param['group'] = $condition['group'];
  56. $friend_list = Db::select($param,$obj_page);
  57. return $friend_list;
  58. }
  59. /**
  60. * 获取好友详细
  61. *
  62. * @param $condition 查询条件
  63. * @param $field 查询字段
  64. */
  65. public function getFriendRow($condition,$field='*'){
  66. $param = array();
  67. $param['table'] = 'sns_friend';
  68. $param['field'] = array_keys($condition);
  69. $param['value'] = array_values($condition);
  70. return Db::getRow($param,$field);
  71. }
  72. /**
  73. * 好友总数
  74. */
  75. public function countFriend($condition){
  76. //得到条件语句
  77. $condition_str = $this->getCondition($condition);
  78. $count = Db::getCount('sns_friend',$condition_str);
  79. return $count;
  80. }
  81. /**
  82. * 更新好友信息
  83. * @param $param 更新内容
  84. * @param $condition 更新条件
  85. */
  86. public function editFriend($param,$condition) {
  87. if(empty($param)) {
  88. return false;
  89. }
  90. //得到条件语句
  91. $condition_str = $this->getCondition($condition);
  92. $result = Db::update('sns_friend',$param,$condition_str);
  93. return $result;
  94. }
  95. /**
  96. * 删除关注
  97. */
  98. public function delFriend($condition){
  99. if (empty($condition)){
  100. return false;
  101. }
  102. if ($condition['friend_frommid'] != ''){
  103. $condition_str .= " and friend_frommid='{$condition['friend_frommid']}' ";
  104. }
  105. if ($condition['friend_tomid'] != ''){
  106. $condition_str .= " and friend_tomid='{$condition['friend_tomid']}' ";
  107. }
  108. return Db::delete('sns_friend',$condition_str);
  109. }
  110. /**
  111. * 将条件数组组合为SQL语句的条件部分
  112. *
  113. * @param array $conditon_array
  114. * @return string
  115. */
  116. private function getCondition($conditon_array){
  117. $condition_sql = '';
  118. //自增编号
  119. if($conditon_array['friend_id'] != '') {
  120. $condition_sql .= " and sns_friend.friend_id= '{$conditon_array['friend_id']}'";
  121. }
  122. //会员编号
  123. if($conditon_array['friend_frommid'] != '') {
  124. $condition_sql .= " and sns_friend.friend_frommid= '{$conditon_array['friend_frommid']}'";
  125. }
  126. //朋友编号
  127. if($conditon_array['friend_tomid'] != '') {
  128. $condition_sql .= " and sns_friend.friend_tomid = '{$conditon_array['friend_tomid']}'";
  129. }
  130. //关注状态
  131. if($conditon_array['friend_followstate'] != '') {
  132. $condition_sql .= " and sns_friend.friend_followstate = '{$conditon_array['friend_followstate']}'";
  133. }
  134. return $condition_sql;
  135. }
  136. }