]> git.mxchange.org Git - friendica.git/blob - mod/group.php
Redirect empty group to /contacts.
[friendica.git] / mod / group.php
1 <?php
2 /**
3  * @file mod/group.php
4  * @brief The group module (create and rename contact groups, add and
5  *      remove contacts to the contact groups
6  */
7
8 use Friendica\App;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\PConfig;
12 use Friendica\Core\System;
13 use Friendica\Database\DBM;
14 use Friendica\Model\Contact;
15 use Friendica\Model\Group;
16
17 function group_init(App $a) {
18         if (local_user()) {
19                 $a->page['aside'] = Group::sidebarWidget('contacts', 'group', 'extended', (($a->argc > 1) ? intval($a->argv[1]) : 0));
20         }
21 }
22
23 function group_post(App $a) {
24
25         if (! local_user()) {
26                 notice(L10n::t('Permission denied.') . EOL);
27                 return;
28         }
29
30         if (($a->argc == 2) && ($a->argv[1] === 'new')) {
31                 check_form_security_token_redirectOnErr('/group/new', 'group_edit');
32
33                 $name = notags(trim($_POST['groupname']));
34                 $r = Group::create(local_user(), $name);
35                 if ($r) {
36                         info(L10n::t('Group created.') . EOL);
37                         $r = Group::getIdByName(local_user(), $name);
38                         if ($r) {
39                                 goaway(System::baseUrl() . '/group/' . $r);
40                         }
41                 } else {
42                         notice(L10n::t('Could not create group.') . EOL);
43                 }
44                 goaway(System::baseUrl() . '/group');
45                 return; // NOTREACHED
46         }
47
48         if (($a->argc == 2) && (intval($a->argv[1]))) {
49                 check_form_security_token_redirectOnErr('/group', 'group_edit');
50
51                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
52                         intval($a->argv[1]),
53                         intval(local_user())
54                 );
55                 if (! DBM::is_result($r)) {
56                         notice(L10n::t('Group not found.') . EOL);
57                         goaway(System::baseUrl() . '/contacts');
58                         return; // NOTREACHED
59                 }
60                 $group = $r[0];
61                 $groupname = notags(trim($_POST['groupname']));
62                 if ((strlen($groupname))  && ($groupname != $group['name'])) {
63                         $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d",
64                                 dbesc($groupname),
65                                 intval(local_user()),
66                                 intval($group['id'])
67                         );
68
69                         if ($r) {
70                                 info(L10n::t('Group name changed.') . EOL);
71                         }
72                 }
73
74                 $a->page['aside'] = Group::sidebarWidget();
75         }
76         return;
77 }
78
79 function group_content(App $a) {
80         $change = false;
81
82         if (! local_user()) {
83                 notice(L10n::t('Permission denied') . EOL);
84                 return;
85         }
86
87         // Switch to text mode interface if we have more than 'n' contacts or group members
88         
89         if ($a->argc == 1) {
90                 goaway(System::baseUrl() . '/contacts');
91         }
92
93         $switchtotext = PConfig::get(local_user(), 'system', 'groupedit_image_limit');
94         if (is_null($switchtotext)) {
95                 $switchtotext = Config::get('system', 'groupedit_image_limit', 400);
96         }
97
98         $tpl = get_markup_template('group_edit.tpl');
99
100         $context = [
101                         '$submit' => L10n::t('Save Group'),
102         ];
103
104         if (($a->argc == 2) && ($a->argv[1] === 'new')) {
105                 return replace_macros($tpl, $context + [
106                         '$title' => L10n::t('Create a group of contacts/friends.'),
107                         '$gname' => ['groupname', L10n::t('Group Name: '), '', ''],
108                         '$gid' => 'new',
109                         '$form_security_token' => get_form_security_token("group_edit"),
110                 ]);
111
112
113         }
114
115         if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
116                 check_form_security_token_redirectOnErr('/group', 'group_drop', 't');
117
118                 if (intval($a->argv[2])) {
119                         $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
120                                 intval($a->argv[2]),
121                                 intval(local_user())
122                         );
123
124                         $result = null;
125
126                         if (DBM::is_result($r)) {
127                                 $result = Group::removeByName(local_user(), $r[0]['name']);
128                         }
129
130                         if ($result) {
131                                 info(L10n::t('Group removed.') . EOL);
132                         } else {
133                                 notice(L10n::t('Unable to remove group.') . EOL);
134                         }
135                 }
136                 goaway(System::baseUrl() . '/group');
137                 // NOTREACHED
138         }
139
140         if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
141                 check_form_security_token_ForbiddenOnErr('group_member_change', 't');
142
143                 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
144                         intval($a->argv[2]),
145                         intval(local_user())
146                 );
147                 if (DBM::is_result($r)) {
148                         $change = intval($a->argv[2]);
149                 }
150         }
151
152         if (($a->argc > 1) && (intval($a->argv[1]))) {
153                 require_once 'mod/contacts.php';
154
155                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
156                         intval($a->argv[1]),
157                         intval(local_user())
158                 );
159
160                 if (! DBM::is_result($r)) {
161                         notice(L10n::t('Group not found.') . EOL);
162                         goaway(System::baseUrl() . '/contacts');
163                 }
164
165                 $group = $r[0];
166                 $members = Contact::getByGroupId($group['id']);
167                 $preselected = [];
168                 $entry = [];
169                 $id = 0;
170
171                 if (count($members)) {
172                         foreach ($members as $member) {
173                                 $preselected[] = $member['id'];
174                         }
175                 }
176
177                 if ($change) {
178                         if (in_array($change, $preselected)) {
179                                 Group::removeMember($group['id'], $change);
180                         } else {
181                                 Group::addMember($group['id'], $change);
182                         }
183
184                         $members = Contact::getByGroupId($group['id']);
185                         $preselected = [];
186                         if (count($members)) {
187                                 foreach ($members as $member) {
188                                         $preselected[] = $member['id'];
189                                 }
190                         }
191                 }
192
193                 $drop_tpl = get_markup_template('group_drop.tpl');
194                 $drop_txt = replace_macros($drop_tpl, [
195                         '$id' => $group['id'],
196                         '$delete' => L10n::t('Delete Group'),
197                         '$form_security_token' => get_form_security_token("group_drop"),
198                 ]);
199
200
201                 $context = $context + [
202                         '$title' => L10n::t('Group Editor'),
203                         '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
204                         '$gid' => $group['id'],
205                         '$drop' => $drop_txt,
206                         '$form_security_token' => get_form_security_token('group_edit'),
207                         '$edit_name' => L10n::t('Edit Group Name')
208                 ];
209
210         }
211
212         if (! isset($group)) {
213                 return;
214         }
215
216         $groupeditor = [
217                 'label_members' => L10n::t('Members'),
218                 'members' => [],
219                 'label_contacts' => L10n::t('All Contacts'),
220                 'group_is_empty' => L10n::t('Group is empty'),
221                 'contacts' => [],
222         ];
223
224         $sec_token = addslashes(get_form_security_token('group_member_change'));
225
226         // Format the data of the group members
227         foreach ($members as $member) {
228                 if ($member['url']) {
229                         $entry = _contact_detail_for_template($member);
230                         $entry['label'] = 'members';
231                         $entry['photo_menu'] = '';
232                         $entry['change_member'] = [
233                                 'title'     => L10n::t("Remove contact from group"),
234                                 'gid'       => $group['id'],
235                                 'cid'       => $member['id'],
236                                 'sec_token' => $sec_token
237                         ];
238
239                         $groupeditor['members'][] = $entry;
240                 } else {
241                         Group::removeMember($group['id'], $member['id']);
242                 }
243         }
244
245         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
246                 intval(local_user())
247         );
248
249         if (DBM::is_result($r)) {
250                 // Format the data of the contacts who aren't in the contact group
251                 foreach ($r as $member) {
252                         if (! in_array($member['id'], $preselected)) {
253                                 $entry = _contact_detail_for_template($member);
254                                 $entry['label'] = 'contacts';
255                                 $entry['photo_menu'] = '';
256                                 $entry['change_member'] = [
257                                         'title'     => L10n::t("Add contact to group"),
258                                         'gid'       => $group['id'],
259                                         'cid'       => $member['id'],
260                                         'sec_token' => $sec_token
261                                 ];
262
263                                 $groupeditor['contacts'][] = $entry;
264                         }
265                 }
266         }
267
268         $context['$groupeditor'] = $groupeditor;
269         $context['$desc'] = L10n::t('Click on a contact to add or remove.');
270
271         // If there are to many contacts we could provide an alternative view mode
272         $total = count($groupeditor['members']) + count($groupeditor['contacts']);
273         $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
274
275         if ($change) {
276                 $tpl = get_markup_template('groupeditor.tpl');
277                 echo replace_macros($tpl, $context);
278                 killme();
279         }
280
281         return replace_macros($tpl, $context);
282
283 }