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\Database\DBA;
17 use Friendica\Util\Strings;
19 function group_init(App $a) {
21 $a->page['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone'));
25 function group_post(App $a) {
28 notice(L10n::t('Permission denied.') . EOL);
32 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
33 BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
35 $name = Strings::escapeTags(trim($_POST['groupname']));
36 $r = Model\Group::create(local_user(), $name);
38 info(L10n::t('Group created.') . EOL);
39 $r = Model\Group::getIdByName(local_user(), $name);
41 $a->internalRedirect('group/' . $r);
44 notice(L10n::t('Could not create group.') . EOL);
46 $a->internalRedirect('group');
50 if (($a->argc == 2) && intval($a->argv[1])) {
51 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
53 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
57 if (!DBA::isResult($r)) {
58 notice(L10n::t('Group not found.') . EOL);
59 $a->internalRedirect('contact');
63 $groupname = Strings::escapeTags(trim($_POST['groupname']));
64 if (strlen($groupname) && ($groupname != $group['name'])) {
65 $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d",
66 DBA::escape($groupname),
72 info(L10n::t('Group name changed.') . EOL);
76 $a->page['aside'] = Model\Group::sidebarWidget();
81 function group_content(App $a) {
85 notice(L10n::t('Permission denied') . EOL);
89 // With no group number provided we jump to the unassigned contacts as a starting point
91 $a->internalRedirect('group/none');
94 // Switch to text mode interface if we have more than 'n' contacts or group members
95 $switchtotext = PConfig::get(local_user(), 'system', 'groupedit_image_limit');
96 if (is_null($switchtotext)) {
97 $switchtotext = Config::get('system', 'groupedit_image_limit', 400);
100 $tpl = Renderer::getMarkupTemplate('group_edit.tpl');
103 '$submit' => L10n::t('Save Group'),
104 '$submit_filter' => L10n::t('Filter'),
107 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
108 return Renderer::replaceMacros($tpl, $context + [
109 '$title' => L10n::t('Create a group of contacts/friends.'),
110 '$gname' => ['groupname', L10n::t('Group Name: '), '', ''],
112 '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
120 if (($a->argc == 2) && ($a->argv[1] === 'none')) {
125 'name' => L10n::t('Contacts not in any group'),
131 $context = $context + [
132 '$title' => $group['name'],
133 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
140 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
141 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
143 if (intval($a->argv[2])) {
144 $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
151 if (DBA::isResult($r)) {
152 $result = Model\Group::removeByName(local_user(), $r[0]['name']);
156 info(L10n::t('Group removed.') . EOL);
158 notice(L10n::t('Unable to remove group.') . EOL);
161 $a->internalRedirect('group');
165 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
166 BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
168 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
172 if (DBA::isResult($r)) {
173 $change = intval($a->argv[2]);
177 if (($a->argc > 1) && intval($a->argv[1])) {
178 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
183 if (!DBA::isResult($r)) {
184 notice(L10n::t('Group not found.') . EOL);
185 $a->internalRedirect('contact');
189 $members = Model\Contact::getByGroupId($group['id']);
192 if (count($members)) {
193 foreach ($members as $member) {
194 $preselected[] = $member['id'];
199 if (in_array($change, $preselected)) {
200 Model\Group::removeMember($group['id'], $change);
202 Model\Group::addMember($group['id'], $change);
205 $members = Model\Contact::getByGroupId($group['id']);
207 if (count($members)) {
208 foreach ($members as $member) {
209 $preselected[] = $member['id'];
214 $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
215 $drop_txt = Renderer::replaceMacros($drop_tpl, [
216 '$id' => $group['id'],
217 '$delete' => L10n::t('Delete Group'),
218 '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
222 $context = $context + [
223 '$title' => $group['name'],
224 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
225 '$gid' => $group['id'],
226 '$drop' => $drop_txt,
227 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
228 '$edit_name' => L10n::t('Edit Group Name'),
234 if (!isset($group)) {
239 'label_members' => L10n::t('Members'),
241 'label_contacts' => L10n::t('All Contacts'),
242 'group_is_empty' => L10n::t('Group is empty'),
246 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
248 // Format the data of the group members
249 foreach ($members as $member) {
250 if ($member['url']) {
251 $entry = Module\Contact::getContactTemplateVars($member);
252 $entry['label'] = 'members';
253 $entry['photo_menu'] = '';
254 $entry['change_member'] = [
255 'title' => L10n::t("Remove contact from group"),
256 'gid' => $group['id'],
257 'cid' => $member['id'],
258 'sec_token' => $sec_token
261 $groupeditor['members'][] = $entry;
263 Model\Group::removeMember($group['id'], $member['id']);
268 $r = Model\Contact::getUngroupedList(local_user());
270 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
273 $context['$desc'] = L10n::t('Click on a contact to add or remove.');
276 if (DBA::isResult($r)) {
277 // Format the data of the contacts who aren't in the contact group
278 foreach ($r as $member) {
279 if (!in_array($member['id'], $preselected)) {
280 $entry = Module\Contact::getContactTemplateVars($member);
281 $entry['label'] = 'contacts';
283 $entry['photo_menu'] = [];
286 $entry['change_member'] = [
287 'title' => L10n::t("Add contact to group"),
288 'gid' => $group['id'],
289 'cid' => $member['id'],
290 'sec_token' => $sec_token
294 $groupeditor['contacts'][] = $entry;
299 $context['$groupeditor'] = $groupeditor;
301 // If there are to many contacts we could provide an alternative view mode
302 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
303 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
306 $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
307 echo Renderer::replaceMacros($tpl, $context);
311 return Renderer::replaceMacros($tpl, $context);