3 * @file src/Module/Group.php
6 namespace Friendica\Module;
8 use Friendica\BaseModule;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\PConfig;
12 use Friendica\Core\Renderer;
13 use Friendica\Core\System;
14 use Friendica\Database\DBA;
16 use Friendica\Util\Strings;
18 require_once 'boot.php';
20 class Group extends BaseModule
22 public static function post(array $parameters = [])
31 notice(L10n::t('Permission denied.'));
32 $a->internalRedirect();
35 // @TODO: Replace with parameter from router
36 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
37 BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
39 $name = Strings::escapeTags(trim($_POST['groupname']));
40 $r = Model\Group::create(local_user(), $name);
42 info(L10n::t('Group created.'));
43 $r = Model\Group::getIdByName(local_user(), $name);
45 $a->internalRedirect('group/' . $r);
48 notice(L10n::t('Could not create group.'));
50 $a->internalRedirect('group');
53 // @TODO: Replace with parameter from router
54 if (($a->argc == 2) && intval($a->argv[1])) {
55 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
57 $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user()]);
58 if (!DBA::isResult($group)) {
59 notice(L10n::t('Group not found.'));
60 $a->internalRedirect('contact');
62 $groupname = Strings::escapeTags(trim($_POST['groupname']));
63 if (strlen($groupname) && ($groupname != $group['name'])) {
64 if (Model\Group::update($group['id'], $groupname)) {
65 info(L10n::t('Group name changed.'));
71 public static function ajaxPost()
77 throw new \Exception(L10n::t('Permission denied.'), 403);
80 // POST /group/123/add/123
81 // POST /group/123/remove/123
82 // @TODO: Replace with parameter from router
84 list($group_id, $command, $contact_id) = array_slice($a->argv, 1);
86 if (!Model\Group::exists($group_id, local_user())) {
87 throw new \Exception(L10n::t('Unknown group.'), 404);
90 $contact = DBA::selectFirst('contact', ['pending', 'blocked', 'deleted'], ['id' => $contact_id, 'uid' => local_user()]);
91 if (!DBA::isResult($contact)) {
92 throw new \Exception(L10n::t('Contact not found.'), 404);
95 if ($contact['pending']) {
96 throw new \Exception(L10n::t('Contact is unavailable.'), 400);
99 if ($contact['deleted']) {
100 throw new \Exception(L10n::t('Contact is deleted.'), 410);
105 if ($contact['blocked']) {
106 throw new \Exception(L10n::t('Contact is blocked, unable to add it to a group.'), 400);
109 if (!Model\Group::addMember($group_id, $contact_id)) {
110 throw new \Exception(L10n::t('Unable to add the contact to the group.'), 500);
112 $message = L10n::t('Contact successfully added to group.');
115 if (!Model\Group::removeMember($group_id, $contact_id)) {
116 throw new \Exception(L10n::t('Unable to remove the contact from the group.'), 500);
118 $message = L10n::t('Contact successfully removed from group.');
121 throw new \Exception(L10n::t('Unknown group command.'), 400);
124 throw new \Exception(L10n::t('Bad request.'), 400);
128 System::jsonExit(['status' => 'OK', 'message' => $message]);
129 } catch (\Exception $e) {
130 notice($e->getMessage());
131 System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
135 public static function content(array $parameters = [])
140 throw new \Friendica\Network\HTTPException\ForbiddenException();
145 $a->page['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone'));
147 // With no group number provided we jump to the unassigned contacts as a starting point
148 // @TODO: Replace with parameter from router
150 $a->internalRedirect('group/none');
153 // Switch to text mode interface if we have more than 'n' contacts or group members
154 $switchtotext = PConfig::get(local_user(), 'system', 'groupedit_image_limit');
155 if (is_null($switchtotext)) {
156 $switchtotext = Config::get('system', 'groupedit_image_limit', 200);
159 $tpl = Renderer::getMarkupTemplate('group_edit.tpl');
163 '$submit' => L10n::t('Save Group'),
164 '$submit_filter' => L10n::t('Filter'),
167 // @TODO: Replace with parameter from router
168 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
169 return Renderer::replaceMacros($tpl, $context + [
170 '$title' => L10n::t('Create a group of contacts/friends.'),
171 '$gname' => ['groupname', L10n::t('Group Name: '), '', ''],
173 '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
179 // @TODO: Replace with parameter from router
180 if (($a->argc == 2) && ($a->argv[1] === 'none') ||
181 ($a->argc == 1) && ($a->argv[0] === 'nogroup')) {
186 'name' => L10n::t('Contacts not in any group'),
192 $context = $context + [
193 '$title' => $group['name'],
194 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
200 // @TODO: Replace with parameter from router
201 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
202 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
204 // @TODO: Replace with parameter from router
205 if (intval($a->argv[2])) {
206 if (!Model\Group::exists($a->argv[2], local_user())) {
207 notice(L10n::t('Group not found.'));
208 $a->internalRedirect('contact');
211 if (Model\Group::remove($a->argv[2])) {
212 info(L10n::t('Group removed.'));
214 notice(L10n::t('Unable to remove group.'));
217 $a->internalRedirect('group');
220 // @TODO: Replace with parameter from router
221 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
222 BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
224 if (DBA::exists('contact', ['id' => $a->argv[2], 'uid' => local_user(), 'self' => false, 'pending' => false, 'blocked' => false])) {
225 $change = intval($a->argv[2]);
229 // @TODO: Replace with parameter from router
230 if (($a->argc > 1) && intval($a->argv[1])) {
231 $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user(), 'deleted' => false]);
232 if (!DBA::isResult($group)) {
233 notice(L10n::t('Group not found.'));
234 $a->internalRedirect('contact');
237 $members = Model\Contact::getByGroupId($group['id']);
240 if (count($members)) {
241 foreach ($members as $member) {
242 $preselected[] = $member['id'];
247 if (in_array($change, $preselected)) {
248 Model\Group::removeMember($group['id'], $change);
250 Model\Group::addMember($group['id'], $change);
253 $members = Model\Contact::getByGroupId($group['id']);
255 if (count($members)) {
256 foreach ($members as $member) {
257 $preselected[] = $member['id'];
262 $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
263 $drop_txt = Renderer::replaceMacros($drop_tpl, [
264 '$id' => $group['id'],
265 '$delete' => L10n::t('Delete Group'),
266 '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
269 $context = $context + [
270 '$title' => $group['name'],
271 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
272 '$gid' => $group['id'],
273 '$drop' => $drop_txt,
274 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
275 '$edit_name' => L10n::t('Edit Group Name'),
280 if (!isset($group)) {
281 throw new \Friendica\Network\HTTPException\BadRequestException();
285 'label_members' => L10n::t('Members'),
287 'label_contacts' => L10n::t('All Contacts'),
288 'group_is_empty' => L10n::t('Group is empty'),
292 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
294 // Format the data of the group members
295 foreach ($members as $member) {
296 if ($member['url']) {
297 $entry = Contact::getContactTemplateVars($member);
298 $entry['label'] = 'members';
299 $entry['photo_menu'] = '';
300 $entry['change_member'] = [
301 'title' => L10n::t("Remove contact from group"),
302 'gid' => $group['id'],
303 'cid' => $member['id'],
304 'sec_token' => $sec_token
307 $groupeditor['members'][] = $entry;
309 Model\Group::removeMember($group['id'], $member['id']);
314 $contacts = Model\Contact::getUngroupedList(local_user());
316 $contacts_stmt = DBA::select('contact', [],
317 ['uid' => local_user(), 'pending' => false, 'blocked' => false, 'self' => false],
318 ['order' => ['name']]
320 $contacts = DBA::toArray($contacts_stmt);
321 $context['$desc'] = L10n::t('Click on a contact to add or remove.');
324 if (DBA::isResult($contacts)) {
325 // Format the data of the contacts who aren't in the contact group
326 foreach ($contacts as $member) {
327 if (!in_array($member['id'], $preselected)) {
328 $entry = Contact::getContactTemplateVars($member);
329 $entry['label'] = 'contacts';
331 $entry['photo_menu'] = [];
334 $entry['change_member'] = [
335 'title' => L10n::t("Add contact to group"),
336 'gid' => $group['id'],
337 'cid' => $member['id'],
338 'sec_token' => $sec_token
342 $groupeditor['contacts'][] = $entry;
347 $context['$groupeditor'] = $groupeditor;
349 // If there are to many contacts we could provide an alternative view mode
350 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
351 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
354 $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
355 echo Renderer::replaceMacros($tpl, $context);
359 return Renderer::replaceMacros($tpl, $context);