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