4 * @brief The group module (create and rename contact groups, add and
5 * remove contacts to the contact groups
9 use Friendica\BaseModule;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\PConfig;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\System;
15 use Friendica\Database\DBA;
18 use Friendica\Util\Security;
19 use Friendica\Util\Strings;
21 function group_init(App $a) {
23 $a->page['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone'));
27 function group_post(App $a) {
30 notice(L10n::t('Permission denied.') . EOL);
34 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
35 BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
37 $name = Strings::escapeTags(trim($_POST['groupname']));
38 $r = Model\Group::create(local_user(), $name);
40 info(L10n::t('Group created.') . EOL);
41 $r = Model\Group::getIdByName(local_user(), $name);
43 $a->internalRedirect('group/' . $r);
46 notice(L10n::t('Could not create group.') . EOL);
48 $a->internalRedirect('group');
52 if (($a->argc == 2) && intval($a->argv[1])) {
53 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
55 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
59 if (!DBA::isResult($r)) {
60 notice(L10n::t('Group not found.') . EOL);
61 $a->internalRedirect('contact');
65 $groupname = Strings::escapeTags(trim($_POST['groupname']));
66 if (strlen($groupname) && ($groupname != $group['name'])) {
67 $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d",
68 DBA::escape($groupname),
74 info(L10n::t('Group name changed.') . EOL);
78 $a->page['aside'] = Model\Group::sidebarWidget();
83 function group_content(App $a) {
87 notice(L10n::t('Permission denied') . EOL);
91 // With no group number provided we jump to the unassigned contacts as a starting point
93 $a->internalRedirect('group/none');
96 // Switch to text mode interface if we have more than 'n' contacts or group members
97 $switchtotext = PConfig::get(local_user(), 'system', 'groupedit_image_limit');
98 if (is_null($switchtotext)) {
99 $switchtotext = Config::get('system', 'groupedit_image_limit', 400);
102 $tpl = Renderer::getMarkupTemplate('group_edit.tpl');
105 '$submit' => L10n::t('Save Group'),
106 '$submit_filter' => L10n::t('Filter'),
109 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
110 return Renderer::replaceMacros($tpl, $context + [
111 '$title' => L10n::t('Create a group of contacts/friends.'),
112 '$gname' => ['groupname', L10n::t('Group Name: '), '', ''],
114 '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
122 if (($a->argc == 2) && ($a->argv[1] === 'none')) {
127 'name' => L10n::t('Contacts not in any group'),
133 $context = $context + [
134 '$title' => $group['name'],
135 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
142 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
143 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
145 if (intval($a->argv[2])) {
146 $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
153 if (DBA::isResult($r)) {
154 $result = Model\Group::removeByName(local_user(), $r[0]['name']);
158 info(L10n::t('Group removed.') . EOL);
160 notice(L10n::t('Unable to remove group.') . EOL);
163 $a->internalRedirect('group');
167 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
168 BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
170 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
174 if (DBA::isResult($r)) {
175 $change = intval($a->argv[2]);
179 if (($a->argc > 1) && intval($a->argv[1])) {
180 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
185 if (!DBA::isResult($r)) {
186 notice(L10n::t('Group not found.') . EOL);
187 $a->internalRedirect('contact');
191 $members = Model\Contact::getByGroupId($group['id']);
194 if (count($members)) {
195 foreach ($members as $member) {
196 $preselected[] = $member['id'];
201 if (in_array($change, $preselected)) {
202 Model\Group::removeMember($group['id'], $change);
204 Model\Group::addMember($group['id'], $change);
207 $members = Model\Contact::getByGroupId($group['id']);
209 if (count($members)) {
210 foreach ($members as $member) {
211 $preselected[] = $member['id'];
216 $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
217 $drop_txt = Renderer::replaceMacros($drop_tpl, [
218 '$id' => $group['id'],
219 '$delete' => L10n::t('Delete Group'),
220 '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
224 $context = $context + [
225 '$title' => $group['name'],
226 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
227 '$gid' => $group['id'],
228 '$drop' => $drop_txt,
229 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
230 '$edit_name' => L10n::t('Edit Group Name'),
236 if (!isset($group)) {
241 'label_members' => L10n::t('Members'),
243 'label_contacts' => L10n::t('All Contacts'),
244 'group_is_empty' => L10n::t('Group is empty'),
248 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
250 // Format the data of the group members
251 foreach ($members as $member) {
252 if ($member['url']) {
253 $entry = Module\Contact::getContactTemplateVars($member);
254 $entry['label'] = 'members';
255 $entry['photo_menu'] = '';
256 $entry['change_member'] = [
257 'title' => L10n::t("Remove contact from group"),
258 'gid' => $group['id'],
259 'cid' => $member['id'],
260 'sec_token' => $sec_token
263 $groupeditor['members'][] = $entry;
265 Model\Group::removeMember($group['id'], $member['id']);
270 $r = Model\Contact::getUngroupedList(local_user());
272 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
275 $context['$desc'] = L10n::t('Click on a contact to add or remove.');
278 if (DBA::isResult($r)) {
279 // Format the data of the contacts who aren't in the contact group
280 foreach ($r as $member) {
281 if (!in_array($member['id'], $preselected)) {
282 $entry = Module\Contact::getContactTemplateVars($member);
283 $entry['label'] = 'contacts';
285 $entry['photo_menu'] = [];
288 $entry['change_member'] = [
289 'title' => L10n::t("Add contact to group"),
290 'gid' => $group['id'],
291 'cid' => $member['id'],
292 'sec_token' => $sec_token
296 $groupeditor['contacts'][] = $entry;
301 $context['$groupeditor'] = $groupeditor;
303 // If there are to many contacts we could provide an alternative view mode
304 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
305 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
308 $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
309 echo Renderer::replaceMacros($tpl, $context);
313 return Renderer::replaceMacros($tpl, $context);