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('contacts', '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'),
134 $context = $context + [
135 '$title' => $group['name'],
136 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
143 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
144 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
146 if (intval($a->argv[2])) {
147 $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
154 if (DBA::isResult($r)) {
155 $result = Model\Group::removeByName(local_user(), $r[0]['name']);
159 info(L10n::t('Group removed.') . EOL);
161 notice(L10n::t('Unable to remove group.') . EOL);
164 $a->internalRedirect('group');
168 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
169 BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
171 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
175 if (DBA::isResult($r)) {
176 $change = intval($a->argv[2]);
180 if (($a->argc > 1) && intval($a->argv[1])) {
181 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
186 if (!DBA::isResult($r)) {
187 notice(L10n::t('Group not found.') . EOL);
188 $a->internalRedirect('contact');
192 $members = Model\Contact::getByGroupId($group['id']);
197 if (count($members)) {
198 foreach ($members as $member) {
199 $preselected[] = $member['id'];
204 if (in_array($change, $preselected)) {
205 Model\Group::removeMember($group['id'], $change);
207 Model\Group::addMember($group['id'], $change);
210 $members = Model\Contact::getByGroupId($group['id']);
212 if (count($members)) {
213 foreach ($members as $member) {
214 $preselected[] = $member['id'];
219 $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
220 $drop_txt = Renderer::replaceMacros($drop_tpl, [
221 '$id' => $group['id'],
222 '$delete' => L10n::t('Delete Group'),
223 '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
227 $context = $context + [
228 '$title' => $group['name'],
229 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
230 '$gid' => $group['id'],
231 '$drop' => $drop_txt,
232 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
233 '$edit_name' => L10n::t('Edit Group Name'),
239 if (!isset($group)) {
244 'label_members' => L10n::t('Members'),
246 'label_contacts' => L10n::t('All Contacts'),
247 'group_is_empty' => L10n::t('Group is empty'),
251 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
253 // Format the data of the group members
254 foreach ($members as $member) {
255 if ($member['url']) {
256 $entry = Module\Contact::getContactTemplateVars($member);
257 $entry['label'] = 'members';
258 $entry['photo_menu'] = '';
259 $entry['change_member'] = [
260 'title' => L10n::t("Remove contact from group"),
261 'gid' => $group['id'],
262 'cid' => $member['id'],
263 'sec_token' => $sec_token
266 $groupeditor['members'][] = $entry;
268 Model\Group::removeMember($group['id'], $member['id']);
273 $r = Model\Contact::getUngroupedList(local_user());
275 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
278 $context['$desc'] = L10n::t('Click on a contact to add or remove.');
281 if (DBA::isResult($r)) {
282 // Format the data of the contacts who aren't in the contact group
283 foreach ($r as $member) {
284 if (!in_array($member['id'], $preselected)) {
285 $entry = Module\Contact::getContactTemplateVars($member);
286 $entry['label'] = 'contacts';
288 $entry['photo_menu'] = [];
291 $entry['change_member'] = [
292 'title' => L10n::t("Add contact to group"),
293 'gid' => $group['id'],
294 'cid' => $member['id'],
295 'sec_token' => $sec_token
299 $groupeditor['contacts'][] = $entry;
304 $context['$groupeditor'] = $groupeditor;
306 // If there are to many contacts we could provide an alternative view mode
307 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
308 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
311 $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
312 echo Renderer::replaceMacros($tpl, $context);
316 return Renderer::replaceMacros($tpl, $context);