Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

187 lines
4.1 KiB

  1. <?php
  2. function group_add($uid,$name) {
  3. $ret = false;
  4. if(x($uid) && x($name)) {
  5. $r = group_byname($uid,$name); // check for dups
  6. if($r !== false)
  7. return true;
  8. $r = q("INSERT INTO `group` ( `uid`, `name` )
  9. VALUES( %d, '%s' ) ",
  10. intval($uid),
  11. dbesc($name)
  12. );
  13. $ret = $r;
  14. }
  15. return $ret;
  16. }
  17. function group_rmv($uid,$name) {
  18. $ret = false;
  19. if(x($uid) && x($name)) {
  20. $r = q("SELECT * FROM `group` WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
  21. intval($uid),
  22. dbesc($name)
  23. );
  24. if(count($r))
  25. $group_id = $r[0]['id'];
  26. if(! $group_id)
  27. return false;
  28. // remove all members
  29. $r = q("DELETE FROM `group_member` WHERE `uid` = %d AND `gid` = %d ",
  30. intval($uid),
  31. intval($group_id)
  32. );
  33. // remove group
  34. $r = q("UPDATE `group` SET `deleted` = 1 WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
  35. intval($uid),
  36. dbesc($name)
  37. );
  38. $ret = $r;
  39. }
  40. return $ret;
  41. }
  42. function group_byname($uid,$name) {
  43. if((! $uid) || (! strlen($name)))
  44. return false;
  45. $r = q("SELECT * FROM `group` WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
  46. intval($uid),
  47. dbesc($name)
  48. );
  49. if(count($r))
  50. return $r[0]['id'];
  51. return false;
  52. }
  53. function group_rmv_member($uid,$name,$member) {
  54. $gid = group_byname($uid,$name);
  55. if(! $gid)
  56. return false;
  57. if(! ( $uid && $gid && $member))
  58. return false;
  59. $r = q("DELETE FROM `group_member` WHERE `uid` = %d AND `gid` = %d AND `contact-id` = %d LIMIT 1 ",
  60. intval($uid),
  61. intval($gid),
  62. intval($member)
  63. );
  64. return $r;
  65. }
  66. function group_add_member($uid,$name,$member) {
  67. $gid = group_byname($uid,$name);
  68. if((! $gid) || (! $uid) || (! $member))
  69. return false;
  70. $r = q("SELECT * FROM `group_member` WHERE `uid` = %d AND `id` = %d AND `contact-id` = %d LIMIT 1",
  71. intval($uid),
  72. intval($gid),
  73. intval($member)
  74. );
  75. if(count($r))
  76. return true; // You might question this, but
  77. // we indicate success because the group was in fact created
  78. // -- It was just created at another time
  79. if(! count($r))
  80. $r = q("INSERT INTO `group_member` (`uid`, `gid`, `contact-id`)
  81. VALUES( %d, %d, %d ) ",
  82. intval($uid),
  83. intval($gid),
  84. intval($member)
  85. );
  86. return $r;
  87. }
  88. function group_get_members($gid) {
  89. $ret = array();
  90. if(intval($gid)) {
  91. $r = q("SELECT `group_member`.`contact-id`, `contact`.* FROM `group_member`
  92. LEFT JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
  93. WHERE `gid` = %d AND `group_member`.`uid` = %d ORDER BY `contact`.`name` ASC ",
  94. intval($gid),
  95. intval(local_user())
  96. );
  97. if(count($r))
  98. $ret = $r;
  99. }
  100. return $ret;
  101. }
  102. function group_public_members($gid) {
  103. $ret = 0;
  104. if(intval($gid)) {
  105. $r = q("SELECT `contact`.`id` AS `contact-id` FROM `group_member`
  106. LEFT JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
  107. WHERE `gid` = %d AND `group_member`.`uid` = %d AND `contact`.`network` != 'dfrn' ",
  108. intval($gid),
  109. intval(local_user())
  110. );
  111. if(count($r))
  112. $ret = count($r);
  113. }
  114. return $ret;
  115. }
  116. function group_side($every="contacts",$each="group",$edit = false) {
  117. $o = '';
  118. if(! local_user())
  119. return '';
  120. $createtext = t('Create a new group');
  121. $linktext= t('Everybody');
  122. $o .= <<< EOT
  123. <div id="group-sidebar">
  124. <h3>Groups</h3>
  125. <div id="sidebar-new-group">
  126. <a href="group/new">$createtext</a>
  127. </div>
  128. <div id="sidebar-group-list">
  129. <ul id="sidebar-group-ul">
  130. <li class="sidebar-group-li" ><a href="$every" >$linktext</a></li>
  131. EOT;
  132. $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d",
  133. intval($_SESSION['uid'])
  134. );
  135. if(count($r)) {
  136. foreach($r as $rr)
  137. $o .= ' <li class="sidebar-group-li">' . (($edit) ? "<a href=\"group/{$rr['id']}\" title=\"" . t('Edit') . "\" ><img src=\"images/spencil.gif\" alt=\"" . t('Edit') . "\"></a> " : "") . "<a href=\"$each/{$rr['id']}\">{$rr['name']}</a></li>\r\n";
  138. }
  139. $o .= " </ul>\r\n </div>\r\n</div>";
  140. return $o;
  141. }
  142. function expand_groups($a) {
  143. if(! (is_array($a) && count($a)))
  144. return array();
  145. $groups = implode(',', $a);
  146. $groups = dbesc($groups);
  147. $r = q("SELECT `contact-id` FROM `group_member` WHERE `gid` IN ( $groups )");
  148. $ret = array();
  149. if(count($r))
  150. foreach($r as $rr)
  151. $ret[] = $rr['contact-id'];
  152. return $ret;
  153. }