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;
20 function group_init(App $a) {
22 $a->page['aside'] = Model\Group::sidebarWidget('contacts', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone'));
26 function group_post(App $a) {
29 notice(L10n::t('Permission denied.') . EOL);
33 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
34 BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
36 $name = notags(trim($_POST['groupname']));
37 $r = Model\Group::create(local_user(), $name);
39 info(L10n::t('Group created.') . EOL);
40 $r = Model\Group::getIdByName(local_user(), $name);
42 $a->internalRedirect('group/' . $r);
45 notice(L10n::t('Could not create group.') . EOL);
47 $a->internalRedirect('group');
51 if (($a->argc == 2) && intval($a->argv[1])) {
52 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
54 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
58 if (!DBA::isResult($r)) {
59 notice(L10n::t('Group not found.') . EOL);
60 $a->internalRedirect('contact');
64 $groupname = notags(trim($_POST['groupname']));
65 if (strlen($groupname) && ($groupname != $group['name'])) {
66 $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d",
67 DBA::escape($groupname),
73 info(L10n::t('Group name changed.') . EOL);
77 $a->page['aside'] = Model\Group::sidebarWidget();
82 function group_content(App $a) {
86 notice(L10n::t('Permission denied') . EOL);
90 // With no group number provided we jump to the unassigned contacts as a starting point
92 $a->internalRedirect('group/none');
95 // Switch to text mode interface if we have more than 'n' contacts or group members
96 $switchtotext = PConfig::get(local_user(), 'system', 'groupedit_image_limit');
97 if (is_null($switchtotext)) {
98 $switchtotext = Config::get('system', 'groupedit_image_limit', 400);
101 $tpl = Renderer::getMarkupTemplate('group_edit.tpl');
104 '$submit' => L10n::t('Save Group'),
105 '$submit_filter' => L10n::t('Filter'),
108 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
109 return Renderer::replaceMacros($tpl, $context + [
110 '$title' => L10n::t('Create a group of contacts/friends.'),
111 '$gname' => ['groupname', L10n::t('Group Name: '), '', ''],
113 '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
121 if (($a->argc == 2) && ($a->argv[1] === 'none')) {
126 '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']);
196 if (count($members)) {
197 foreach ($members as $member) {
198 $preselected[] = $member['id'];
203 if (in_array($change, $preselected)) {
204 Model\Group::removeMember($group['id'], $change);
206 Model\Group::addMember($group['id'], $change);
209 $members = Model\Contact::getByGroupId($group['id']);
211 if (count($members)) {
212 foreach ($members as $member) {
213 $preselected[] = $member['id'];
218 $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
219 $drop_txt = Renderer::replaceMacros($drop_tpl, [
220 '$id' => $group['id'],
221 '$delete' => L10n::t('Delete Group'),
222 '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
226 $context = $context + [
227 '$title' => $group['name'],
228 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
229 '$gid' => $group['id'],
230 '$drop' => $drop_txt,
231 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
232 '$edit_name' => L10n::t('Edit Group Name'),
238 if (!isset($group)) {
243 'label_members' => L10n::t('Members'),
245 'label_contacts' => L10n::t('All Contacts'),
246 'group_is_empty' => L10n::t('Group is empty'),
250 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
252 // Format the data of the group members
253 foreach ($members as $member) {
254 if ($member['url']) {
255 $entry = Module\Contact::getContactTemplateVars($member);
256 $entry['label'] = 'members';
257 $entry['photo_menu'] = '';
258 $entry['change_member'] = [
259 'title' => L10n::t("Remove contact from group"),
260 'gid' => $group['id'],
261 'cid' => $member['id'],
262 'sec_token' => $sec_token
265 $groupeditor['members'][] = $entry;
267 Model\Group::removeMember($group['id'], $member['id']);
272 $r = Model\Contact::getUngroupedList(local_user());
274 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
277 $context['$desc'] = L10n::t('Click on a contact to add or remove.');
280 if (DBA::isResult($r)) {
281 // Format the data of the contacts who aren't in the contact group
282 foreach ($r as $member) {
283 if (!in_array($member['id'], $preselected)) {
284 $entry = Module\Contact::getContactTemplateVars($member);
285 $entry['label'] = 'contacts';
287 $entry['photo_menu'] = [];
290 $entry['change_member'] = [
291 'title' => L10n::t("Add contact to group"),
292 'gid' => $group['id'],
293 'cid' => $member['id'],
294 'sec_token' => $sec_token
298 $groupeditor['contacts'][] = $entry;
303 $context['$groupeditor'] = $groupeditor;
305 // If there are to many contacts we could provide an alternative view mode
306 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
307 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
310 $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
311 echo Renderer::replaceMacros($tpl, $context);
315 return Renderer::replaceMacros($tpl, $context);