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.

424 lines
10.0KB

  1. <?php
  2. /**
  3. * @file src/Model/Group.php
  4. */
  5. namespace Friendica\Model;
  6. use Friendica\BaseModule;
  7. use Friendica\BaseObject;
  8. use Friendica\Core\L10n;
  9. use Friendica\Core\Logger;
  10. use Friendica\Core\Renderer;
  11. use Friendica\Database\DBA;
  12. use Friendica\Util\Security;
  13. /**
  14. * @brief functions for interacting with the group database table
  15. */
  16. class Group extends BaseObject
  17. {
  18. /**
  19. * @brief Create a new contact group
  20. *
  21. * Note: If we found a deleted group with the same name, we restore it
  22. *
  23. * @param int $uid
  24. * @param string $name
  25. * @return boolean
  26. */
  27. public static function create($uid, $name)
  28. {
  29. $return = false;
  30. if (!empty($uid) && !empty($name)) {
  31. $gid = self::getIdByName($uid, $name); // check for dupes
  32. if ($gid !== false) {
  33. // This could be a problem.
  34. // Let's assume we've just created a group which we once deleted
  35. // all the old members are gone, but the group remains so we don't break any security
  36. // access lists. What we're doing here is reviving the dead group, but old content which
  37. // was restricted to this group may now be seen by the new group members.
  38. $group = DBA::selectFirst('group', ['deleted'], ['id' => $gid]);
  39. if (DBA::isResult($group) && $group['deleted']) {
  40. DBA::update('group', ['deleted' => 0], ['id' => $gid]);
  41. notice(L10n::t('A deleted group with this name was revived. Existing item permissions <strong>may</strong> apply to this group and any future members. If this is not what you intended, please create another group with a different name.') . EOL);
  42. }
  43. return true;
  44. }
  45. $return = DBA::insert('group', ['uid' => $uid, 'name' => $name]);
  46. if ($return) {
  47. $return = DBA::lastInsertId();
  48. }
  49. }
  50. return $return;
  51. }
  52. /**
  53. * Update group information.
  54. *
  55. * @param int $id Group ID
  56. * @param string $name Group name
  57. *
  58. * @return bool Was the update successful?
  59. */
  60. public static function update($id, $name)
  61. {
  62. return DBA::update('group', ['name' => $name], ['id' => $id]);
  63. }
  64. /**
  65. * @brief Get a list of group ids a contact belongs to
  66. *
  67. * @param int $cid
  68. * @return array
  69. */
  70. public static function getIdsByContactId($cid)
  71. {
  72. $condition = ['contact-id' => $cid];
  73. $stmt = DBA::select('group_member', ['gid'], $condition);
  74. $return = [];
  75. while ($group = DBA::fetch($stmt)) {
  76. $return[] = $group['gid'];
  77. }
  78. return $return;
  79. }
  80. /**
  81. * @brief count unread group items
  82. *
  83. * Count unread items of each groups of the local user
  84. *
  85. * @return array
  86. * 'id' => group id
  87. * 'name' => group name
  88. * 'count' => counted unseen group items
  89. */
  90. public static function countUnseen()
  91. {
  92. $stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
  93. (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
  94. WHERE `uid` = ?
  95. AND `unseen`
  96. AND `contact-id` IN
  97. (SELECT `contact-id`
  98. FROM `group_member`
  99. WHERE `group_member`.`gid` = `group`.`id`)
  100. ) AS `count`
  101. FROM `group`
  102. WHERE `group`.`uid` = ?;",
  103. local_user(),
  104. local_user()
  105. );
  106. return DBA::toArray($stmt);
  107. }
  108. /**
  109. * @brief Get the group id for a user/name couple
  110. *
  111. * Returns false if no group has been found.
  112. *
  113. * @param int $uid
  114. * @param string $name
  115. * @return int|boolean
  116. */
  117. public static function getIdByName($uid, $name)
  118. {
  119. if (!$uid || !strlen($name)) {
  120. return false;
  121. }
  122. $group = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
  123. if (DBA::isResult($group)) {
  124. return $group['id'];
  125. }
  126. return false;
  127. }
  128. /**
  129. * @brief Mark a group as deleted
  130. *
  131. * @param int $gid
  132. * @return boolean
  133. */
  134. public static function remove($gid) {
  135. if (! $gid) {
  136. return false;
  137. }
  138. $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
  139. if (!DBA::isResult($group)) {
  140. return false;
  141. }
  142. // remove group from default posting lists
  143. $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
  144. if (DBA::isResult($user)) {
  145. $change = false;
  146. if ($user['def_gid'] == $gid) {
  147. $user['def_gid'] = 0;
  148. $change = true;
  149. }
  150. if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
  151. $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
  152. $change = true;
  153. }
  154. if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
  155. $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
  156. $change = true;
  157. }
  158. if ($change) {
  159. DBA::update('user', $user, ['uid' => $group['uid']]);
  160. }
  161. }
  162. // remove all members
  163. DBA::delete('group_member', ['gid' => $gid]);
  164. // remove group
  165. $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
  166. return $return;
  167. }
  168. /**
  169. * @brief Mark a group as deleted based on its name
  170. *
  171. * @deprecated Use Group::remove instead
  172. *
  173. * @param int $uid
  174. * @param string $name
  175. * @return bool
  176. */
  177. public static function removeByName($uid, $name) {
  178. $return = false;
  179. if (!empty($uid) && !empty($name)) {
  180. $gid = self::getIdByName($uid, $name);
  181. $return = self::remove($gid);
  182. }
  183. return $return;
  184. }
  185. /**
  186. * @brief Adds a contact to a group
  187. *
  188. * @param int $gid
  189. * @param int $cid
  190. * @return boolean
  191. */
  192. public static function addMember($gid, $cid)
  193. {
  194. if (!$gid || !$cid) {
  195. return false;
  196. }
  197. $row_exists = DBA::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
  198. if ($row_exists) {
  199. // Row already existing, nothing to do
  200. $return = true;
  201. } else {
  202. $return = DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
  203. }
  204. return $return;
  205. }
  206. /**
  207. * @brief Removes a contact from a group
  208. *
  209. * @param int $gid
  210. * @param int $cid
  211. * @return boolean
  212. */
  213. public static function removeMember($gid, $cid)
  214. {
  215. if (!$gid || !$cid) {
  216. return false;
  217. }
  218. $return = DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
  219. return $return;
  220. }
  221. /**
  222. * @brief Removes a contact from a group based on its name
  223. *
  224. * @deprecated Use Group::removeMember instead
  225. *
  226. * @param int $uid
  227. * @param string $name
  228. * @param int $cid
  229. * @return boolean
  230. */
  231. public static function removeMemberByName($uid, $name, $cid)
  232. {
  233. $gid = self::getIdByName($uid, $name);
  234. $return = self::removeMember($gid, $cid);
  235. return $return;
  236. }
  237. /**
  238. * @brief Returns the combined list of contact ids from a group id list
  239. *
  240. * @param array $group_ids
  241. * @param boolean $check_dead
  242. * @return array
  243. */
  244. public static function expand($group_ids, $check_dead = false)
  245. {
  246. if (!is_array($group_ids) || !count($group_ids)) {
  247. return [];
  248. }
  249. $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
  250. $return = [];
  251. while($group_member = DBA::fetch($stmt)) {
  252. $return[] = $group_member['contact-id'];
  253. }
  254. if ($check_dead) {
  255. Contact::pruneUnavailable($return);
  256. }
  257. return $return;
  258. }
  259. /**
  260. * @brief Returns a templated group selection list
  261. *
  262. * @param int $uid
  263. * @param int $gid An optional pre-selected group
  264. * @param string $label An optional label of the list
  265. * @return string
  266. */
  267. public static function displayGroupSelection($uid, $gid = 0, $label = '')
  268. {
  269. $o = '';
  270. $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
  271. $display_groups = [
  272. [
  273. 'name' => '',
  274. 'id' => '0',
  275. 'selected' => ''
  276. ]
  277. ];
  278. while ($group = DBA::fetch($stmt)) {
  279. $display_groups[] = [
  280. 'name' => $group['name'],
  281. 'id' => $group['id'],
  282. 'selected' => $gid == $group['id'] ? 'true' : ''
  283. ];
  284. }
  285. Logger::log('groups: ' . print_r($display_groups, true));
  286. if ($label == '') {
  287. $label = L10n::t('Default privacy group for new contacts');
  288. }
  289. $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('group_selection.tpl'), [
  290. '$label' => $label,
  291. '$groups' => $display_groups
  292. ]);
  293. return $o;
  294. }
  295. /**
  296. * @brief Create group sidebar widget
  297. *
  298. * @param string $every
  299. * @param string $each
  300. * @param string $editmode
  301. * 'standard' => include link 'Edit groups'
  302. * 'extended' => include link 'Create new group'
  303. * 'full' => include link 'Create new group' and provide for each group a link to edit this group
  304. * @param int $group_id
  305. * @param int $cid
  306. * @return string
  307. */
  308. public static function sidebarWidget($every = 'contact', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
  309. {
  310. $o = '';
  311. if (!local_user()) {
  312. return '';
  313. }
  314. $display_groups = [
  315. [
  316. 'text' => L10n::t('Everybody'),
  317. 'id' => 0,
  318. 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
  319. 'href' => $every,
  320. ]
  321. ];
  322. $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
  323. $member_of = [];
  324. if ($cid) {
  325. $member_of = self::getIdsByContactId($cid);
  326. }
  327. while ($group = DBA::fetch($stmt)) {
  328. $selected = (($group_id == $group['id']) ? ' group-selected' : '');
  329. if ($editmode == 'full') {
  330. $groupedit = [
  331. 'href' => 'group/' . $group['id'],
  332. 'title' => L10n::t('edit'),
  333. ];
  334. } else {
  335. $groupedit = null;
  336. }
  337. $display_groups[] = [
  338. 'id' => $group['id'],
  339. 'cid' => $cid,
  340. 'text' => $group['name'],
  341. 'href' => $each . '/' . $group['id'],
  342. 'edit' => $groupedit,
  343. 'selected' => $selected,
  344. 'ismember' => in_array($group['id'], $member_of),
  345. ];
  346. }
  347. // Don't show the groups on the network page when there is only one
  348. if ((count($display_groups) <= 2) && ($each == 'network')) {
  349. return '';
  350. }
  351. $tpl = Renderer::getMarkupTemplate('group_side.tpl');
  352. $o = Renderer::replaceMacros($tpl, [
  353. '$add' => L10n::t('add'),
  354. '$title' => L10n::t('Groups'),
  355. '$groups' => $display_groups,
  356. 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
  357. 'grouppage' => 'group/',
  358. '$edittext' => L10n::t('Edit group'),
  359. '$ungrouped' => $every === 'contact' ? L10n::t('Contacts not in any group') : '',
  360. '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
  361. '$createtext' => L10n::t('Create a new group'),
  362. '$creategroup' => L10n::t('Group Name: '),
  363. '$editgroupstext' => L10n::t('Edit groups'),
  364. '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
  365. ]);
  366. return $o;
  367. }
  368. }