]> git.mxchange.org Git - friendica.git/blob - mod/group.php
Correct content type
[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\DBA;
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) ? $a->argv[1] : 'everyone'));
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 (!DBA::isResult($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                                 DBA::escape($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         // With no group number provided we jump to the unassigned contacts as a starting point
88         if ($a->argc == 1) {
89                 goaway('group/none');
90         }
91
92         // Switch to text mode interface if we have more than 'n' contacts or group members
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                 '$submit_filter' => L10n::t('Filter'),
103         ];
104
105         if (($a->argc == 2) && ($a->argv[1] === 'new')) {
106                 return replace_macros($tpl, $context + [
107                         '$title' => L10n::t('Create a group of contacts/friends.'),
108                         '$gname' => ['groupname', L10n::t('Group Name: '), '', ''],
109                         '$gid' => 'new',
110                         '$form_security_token' => get_form_security_token("group_edit"),
111                 ]);
112
113
114         }
115
116         $nogroup = false;
117
118         if (($a->argc == 2) && ($a->argv[1] === 'none')) {
119                 require_once 'mod/contacts.php';
120
121                 $id = -1;
122                 $nogroup = true;
123                 $group = [
124                         'id' => $id,
125                         'name' => L10n::t('Contacts not in any group'),
126                 ];
127
128                 $members = [];
129                 $preselected = [];
130                 $entry = [];
131
132                 $context = $context + [
133                         '$title' => $group['name'],
134                         '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
135                         '$gid' => $id,
136                         '$editable' => 0,
137                 ];
138         }
139
140
141         if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
142                 check_form_security_token_redirectOnErr('/group', 'group_drop', 't');
143
144                 if (intval($a->argv[2])) {
145                         $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
146                                 intval($a->argv[2]),
147                                 intval(local_user())
148                         );
149
150                         $result = null;
151
152                         if (DBA::isResult($r)) {
153                                 $result = Group::removeByName(local_user(), $r[0]['name']);
154                         }
155
156                         if ($result) {
157                                 info(L10n::t('Group removed.') . EOL);
158                         } else {
159                                 notice(L10n::t('Unable to remove group.') . EOL);
160                         }
161                 }
162                 goaway(System::baseUrl() . '/group');
163                 // NOTREACHED
164         }
165
166         if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
167                 check_form_security_token_ForbiddenOnErr('group_member_change', 't');
168
169                 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
170                         intval($a->argv[2]),
171                         intval(local_user())
172                 );
173                 if (DBA::isResult($r)) {
174                         $change = intval($a->argv[2]);
175                 }
176         }
177
178         if (($a->argc > 1) && intval($a->argv[1])) {
179                 require_once 'mod/contacts.php';
180
181                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
182                         intval($a->argv[1]),
183                         intval(local_user())
184                 );
185
186                 if (!DBA::isResult($r)) {
187                         notice(L10n::t('Group not found.') . EOL);
188                         goaway(System::baseUrl() . '/contacts');
189                 }
190
191                 $group = $r[0];
192                 $members = Contact::getByGroupId($group['id']);
193                 $preselected = [];
194                 $entry = [];
195                 $id = 0;
196
197                 if (count($members)) {
198                         foreach ($members as $member) {
199                                 $preselected[] = $member['id'];
200                         }
201                 }
202
203                 if ($change) {
204                         if (in_array($change, $preselected)) {
205                                 Group::removeMember($group['id'], $change);
206                         } else {
207                                 Group::addMember($group['id'], $change);
208                         }
209
210                         $members = Contact::getByGroupId($group['id']);
211                         $preselected = [];
212                         if (count($members)) {
213                                 foreach ($members as $member) {
214                                         $preselected[] = $member['id'];
215                                 }
216                         }
217                 }
218
219                 $drop_tpl = get_markup_template('group_drop.tpl');
220                 $drop_txt = replace_macros($drop_tpl, [
221                         '$id' => $group['id'],
222                         '$delete' => L10n::t('Delete Group'),
223                         '$form_security_token' => get_form_security_token("group_drop"),
224                 ]);
225
226
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' => get_form_security_token('group_edit'),
233                         '$edit_name' => L10n::t('Edit Group Name'),
234                         '$editable' => 1,
235                 ];
236
237         }
238
239         if (!isset($group)) {
240                 return;
241         }
242
243         $groupeditor = [
244                 'label_members' => L10n::t('Members'),
245                 'members' => [],
246                 'label_contacts' => L10n::t('All Contacts'),
247                 'group_is_empty' => L10n::t('Group is empty'),
248                 'contacts' => [],
249         ];
250
251         $sec_token = addslashes(get_form_security_token('group_member_change'));
252
253         // Format the data of the group members
254         foreach ($members as $member) {
255                 if ($member['url']) {
256                         $entry = _contact_detail_for_template($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
264                         ];
265
266                         $groupeditor['members'][] = $entry;
267                 } else {
268                         Group::removeMember($group['id'], $member['id']);
269                 }
270         }
271
272         if ($nogroup) {
273                 $r = Contact::getUngroupedList(local_user());
274         } else {
275                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
276                         intval(local_user())
277                 );
278                 $context['$desc'] = L10n::t('Click on a contact to add or remove.');
279         }
280
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 = _contact_detail_for_template($member);
286                                 $entry['label'] = 'contacts';
287                                 if (!$nogroup)
288                                         $entry['photo_menu'] = [];
289
290                                 if (!$nogroup) {
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
296                                         ];
297                                 }
298
299                                 $groupeditor['contacts'][] = $entry;
300                         }
301                 }
302         }
303
304         $context['$groupeditor'] = $groupeditor;
305
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);
309
310         if ($change) {
311                 $tpl = get_markup_template('groupeditor.tpl');
312                 echo replace_macros($tpl, $context);
313                 killme();
314         }
315
316         return replace_macros($tpl, $context);
317
318 }