Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

355 行
10 KiB

  1. <?php
  2. /**
  3. * @file src/Module/Group.php
  4. */
  5. namespace Friendica\Module;
  6. use Friendica\BaseModule;
  7. use Friendica\Core\Config;
  8. use Friendica\Core\L10n;
  9. use Friendica\Core\Renderer;
  10. use Friendica\Core\System;
  11. use Friendica\Database\DBA;
  12. use Friendica\DI;
  13. use Friendica\Model;
  14. use Friendica\Util\Strings;
  15. require_once 'boot.php';
  16. class Group extends BaseModule
  17. {
  18. public static function post(array $parameters = [])
  19. {
  20. $a = DI::app();
  21. if (DI::mode()->isAjax()) {
  22. self::ajaxPost();
  23. }
  24. if (!local_user()) {
  25. notice(L10n::t('Permission denied.'));
  26. DI::baseUrl()->redirect();
  27. }
  28. // @TODO: Replace with parameter from router
  29. if (($a->argc == 2) && ($a->argv[1] === 'new')) {
  30. BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
  31. $name = Strings::escapeTags(trim($_POST['groupname']));
  32. $r = Model\Group::create(local_user(), $name);
  33. if ($r) {
  34. info(L10n::t('Group created.'));
  35. $r = Model\Group::getIdByName(local_user(), $name);
  36. if ($r) {
  37. DI::baseUrl()->redirect('group/' . $r);
  38. }
  39. } else {
  40. notice(L10n::t('Could not create group.'));
  41. }
  42. DI::baseUrl()->redirect('group');
  43. }
  44. // @TODO: Replace with parameter from router
  45. if (($a->argc == 2) && intval($a->argv[1])) {
  46. BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
  47. $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user()]);
  48. if (!DBA::isResult($group)) {
  49. notice(L10n::t('Group not found.'));
  50. DI::baseUrl()->redirect('contact');
  51. }
  52. $groupname = Strings::escapeTags(trim($_POST['groupname']));
  53. if (strlen($groupname) && ($groupname != $group['name'])) {
  54. if (Model\Group::update($group['id'], $groupname)) {
  55. info(L10n::t('Group name changed.'));
  56. }
  57. }
  58. }
  59. }
  60. public static function ajaxPost()
  61. {
  62. try {
  63. $a = DI::app();
  64. if (!local_user()) {
  65. throw new \Exception(L10n::t('Permission denied.'), 403);
  66. }
  67. // POST /group/123/add/123
  68. // POST /group/123/remove/123
  69. // @TODO: Replace with parameter from router
  70. if ($a->argc == 4) {
  71. list($group_id, $command, $contact_id) = array_slice($a->argv, 1);
  72. if (!Model\Group::exists($group_id, local_user())) {
  73. throw new \Exception(L10n::t('Unknown group.'), 404);
  74. }
  75. $contact = DBA::selectFirst('contact', ['deleted'], ['id' => $contact_id, 'uid' => local_user()]);
  76. if (!DBA::isResult($contact)) {
  77. throw new \Exception(L10n::t('Contact not found.'), 404);
  78. }
  79. if ($contact['deleted']) {
  80. throw new \Exception(L10n::t('Contact is deleted.'), 410);
  81. }
  82. switch($command) {
  83. case 'add':
  84. if (!Model\Group::addMember($group_id, $contact_id)) {
  85. throw new \Exception(L10n::t('Unable to add the contact to the group.'), 500);
  86. }
  87. $message = L10n::t('Contact successfully added to group.');
  88. break;
  89. case 'remove':
  90. if (!Model\Group::removeMember($group_id, $contact_id)) {
  91. throw new \Exception(L10n::t('Unable to remove the contact from the group.'), 500);
  92. }
  93. $message = L10n::t('Contact successfully removed from group.');
  94. break;
  95. default:
  96. throw new \Exception(L10n::t('Unknown group command.'), 400);
  97. }
  98. } else {
  99. throw new \Exception(L10n::t('Bad request.'), 400);
  100. }
  101. notice($message);
  102. System::jsonExit(['status' => 'OK', 'message' => $message]);
  103. } catch (\Exception $e) {
  104. notice($e->getMessage());
  105. System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
  106. }
  107. }
  108. public static function content(array $parameters = [])
  109. {
  110. $change = false;
  111. if (!local_user()) {
  112. throw new \Friendica\Network\HTTPException\ForbiddenException();
  113. }
  114. $a = DI::app();
  115. DI::page()['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone'));
  116. // With no group number provided we jump to the unassigned contacts as a starting point
  117. // @TODO: Replace with parameter from router
  118. if ($a->argc == 1) {
  119. DI::baseUrl()->redirect('group/none');
  120. }
  121. // Switch to text mode interface if we have more than 'n' contacts or group members
  122. $switchtotext = DI::pConfig()->get(local_user(), 'system', 'groupedit_image_limit');
  123. if (is_null($switchtotext)) {
  124. $switchtotext = Config::get('system', 'groupedit_image_limit', 200);
  125. }
  126. $tpl = Renderer::getMarkupTemplate('group_edit.tpl');
  127. $context = [
  128. '$submit' => L10n::t('Save Group'),
  129. '$submit_filter' => L10n::t('Filter'),
  130. ];
  131. // @TODO: Replace with parameter from router
  132. if (($a->argc == 2) && ($a->argv[1] === 'new')) {
  133. return Renderer::replaceMacros($tpl, $context + [
  134. '$title' => L10n::t('Create a group of contacts/friends.'),
  135. '$gname' => ['groupname', L10n::t('Group Name: '), '', ''],
  136. '$gid' => 'new',
  137. '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
  138. ]);
  139. }
  140. $nogroup = false;
  141. // @TODO: Replace with parameter from router
  142. if (($a->argc == 2) && ($a->argv[1] === 'none') ||
  143. ($a->argc == 1) && ($a->argv[0] === 'nogroup')) {
  144. $id = -1;
  145. $nogroup = true;
  146. $group = [
  147. 'id' => $id,
  148. 'name' => L10n::t('Contacts not in any group'),
  149. ];
  150. $members = [];
  151. $preselected = [];
  152. $context = $context + [
  153. '$title' => $group['name'],
  154. '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
  155. '$gid' => $id,
  156. '$editable' => 0,
  157. ];
  158. }
  159. // @TODO: Replace with parameter from router
  160. if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
  161. BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
  162. // @TODO: Replace with parameter from router
  163. if (intval($a->argv[2])) {
  164. if (!Model\Group::exists($a->argv[2], local_user())) {
  165. notice(L10n::t('Group not found.'));
  166. DI::baseUrl()->redirect('contact');
  167. }
  168. if (Model\Group::remove($a->argv[2])) {
  169. info(L10n::t('Group removed.'));
  170. } else {
  171. notice(L10n::t('Unable to remove group.'));
  172. }
  173. }
  174. DI::baseUrl()->redirect('group');
  175. }
  176. // @TODO: Replace with parameter from router
  177. if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
  178. BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
  179. if (DBA::exists('contact', ['id' => $a->argv[2], 'uid' => local_user(), 'self' => false, 'pending' => false, 'blocked' => false])) {
  180. $change = intval($a->argv[2]);
  181. }
  182. }
  183. // @TODO: Replace with parameter from router
  184. if (($a->argc > 1) && intval($a->argv[1])) {
  185. $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user(), 'deleted' => false]);
  186. if (!DBA::isResult($group)) {
  187. notice(L10n::t('Group not found.'));
  188. DI::baseUrl()->redirect('contact');
  189. }
  190. $members = Model\Contact::getByGroupId($group['id']);
  191. $preselected = [];
  192. if (count($members)) {
  193. foreach ($members as $member) {
  194. $preselected[] = $member['id'];
  195. }
  196. }
  197. if ($change) {
  198. if (in_array($change, $preselected)) {
  199. Model\Group::removeMember($group['id'], $change);
  200. } else {
  201. Model\Group::addMember($group['id'], $change);
  202. }
  203. $members = Model\Contact::getByGroupId($group['id']);
  204. $preselected = [];
  205. if (count($members)) {
  206. foreach ($members as $member) {
  207. $preselected[] = $member['id'];
  208. }
  209. }
  210. }
  211. $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
  212. $drop_txt = Renderer::replaceMacros($drop_tpl, [
  213. '$id' => $group['id'],
  214. '$delete' => L10n::t('Delete Group'),
  215. '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
  216. ]);
  217. $context = $context + [
  218. '$title' => $group['name'],
  219. '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
  220. '$gid' => $group['id'],
  221. '$drop' => $drop_txt,
  222. '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
  223. '$edit_name' => L10n::t('Edit Group Name'),
  224. '$editable' => 1,
  225. ];
  226. }
  227. if (!isset($group)) {
  228. throw new \Friendica\Network\HTTPException\BadRequestException();
  229. }
  230. $groupeditor = [
  231. 'label_members' => L10n::t('Members'),
  232. 'members' => [],
  233. 'label_contacts' => L10n::t('All Contacts'),
  234. 'group_is_empty' => L10n::t('Group is empty'),
  235. 'contacts' => [],
  236. ];
  237. $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
  238. // Format the data of the group members
  239. foreach ($members as $member) {
  240. if ($member['url']) {
  241. $entry = Contact::getContactTemplateVars($member);
  242. $entry['label'] = 'members';
  243. $entry['photo_menu'] = '';
  244. $entry['change_member'] = [
  245. 'title' => L10n::t("Remove contact from group"),
  246. 'gid' => $group['id'],
  247. 'cid' => $member['id'],
  248. 'sec_token' => $sec_token
  249. ];
  250. $groupeditor['members'][] = $entry;
  251. } else {
  252. Model\Group::removeMember($group['id'], $member['id']);
  253. }
  254. }
  255. if ($nogroup) {
  256. $contacts = Model\Contact::getUngroupedList(local_user());
  257. } else {
  258. $contacts_stmt = DBA::select('contact', [],
  259. ['uid' => local_user(), 'pending' => false, 'blocked' => false, 'self' => false],
  260. ['order' => ['name']]
  261. );
  262. $contacts = DBA::toArray($contacts_stmt);
  263. $context['$desc'] = L10n::t('Click on a contact to add or remove.');
  264. }
  265. if (DBA::isResult($contacts)) {
  266. // Format the data of the contacts who aren't in the contact group
  267. foreach ($contacts as $member) {
  268. if (!in_array($member['id'], $preselected)) {
  269. $entry = Contact::getContactTemplateVars($member);
  270. $entry['label'] = 'contacts';
  271. if (!$nogroup)
  272. $entry['photo_menu'] = [];
  273. if (!$nogroup) {
  274. $entry['change_member'] = [
  275. 'title' => L10n::t("Add contact to group"),
  276. 'gid' => $group['id'],
  277. 'cid' => $member['id'],
  278. 'sec_token' => $sec_token
  279. ];
  280. }
  281. $groupeditor['contacts'][] = $entry;
  282. }
  283. }
  284. }
  285. $context['$groupeditor'] = $groupeditor;
  286. // If there are to many contacts we could provide an alternative view mode
  287. $total = count($groupeditor['members']) + count($groupeditor['contacts']);
  288. $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
  289. if ($change) {
  290. $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
  291. echo Renderer::replaceMacros($tpl, $context);
  292. exit();
  293. }
  294. return Renderer::replaceMacros($tpl, $context);
  295. }
  296. }