]> git.mxchange.org Git - friendica.git/blob - mod/group.php
change links from /contacts to /contact
[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;
15 use Friendica\Module\Contact;
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() . '/contact');
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                 $id = -1;
120                 $nogroup = true;
121                 $group = [
122                         'id' => $id,
123                         'name' => L10n::t('Contacts not in any group'),
124                 ];
125
126                 $members = [];
127                 $preselected = [];
128                 $entry = [];
129
130                 $context = $context + [
131                         '$title' => $group['name'],
132                         '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
133                         '$gid' => $id,
134                         '$editable' => 0,
135                 ];
136         }
137
138
139         if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
140                 check_form_security_token_redirectOnErr('/group', 'group_drop', 't');
141
142                 if (intval($a->argv[2])) {
143                         $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
144                                 intval($a->argv[2]),
145                                 intval(local_user())
146                         );
147
148                         $result = null;
149
150                         if (DBA::isResult($r)) {
151                                 $result = Group::removeByName(local_user(), $r[0]['name']);
152                         }
153
154                         if ($result) {
155                                 info(L10n::t('Group removed.') . EOL);
156                         } else {
157                                 notice(L10n::t('Unable to remove group.') . EOL);
158                         }
159                 }
160                 goaway(System::baseUrl() . '/group');
161                 // NOTREACHED
162         }
163
164         if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
165                 check_form_security_token_ForbiddenOnErr('group_member_change', 't');
166
167                 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
168                         intval($a->argv[2]),
169                         intval(local_user())
170                 );
171                 if (DBA::isResult($r)) {
172                         $change = intval($a->argv[2]);
173                 }
174         }
175
176         if (($a->argc > 1) && intval($a->argv[1])) {
177                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
178                         intval($a->argv[1]),
179                         intval(local_user())
180                 );
181
182                 if (!DBA::isResult($r)) {
183                         notice(L10n::t('Group not found.') . EOL);
184                         goaway(System::baseUrl() . '/contact');
185                 }
186
187                 $group = $r[0];
188                 $members = Model\Contact::getByGroupId($group['id']);
189                 $preselected = [];
190                 $entry = [];
191                 $id = 0;
192
193                 if (count($members)) {
194                         foreach ($members as $member) {
195                                 $preselected[] = $member['id'];
196                         }
197                 }
198
199                 if ($change) {
200                         if (in_array($change, $preselected)) {
201                                 Group::removeMember($group['id'], $change);
202                         } else {
203                                 Group::addMember($group['id'], $change);
204                         }
205
206                         $members = Model\Contact::getByGroupId($group['id']);
207                         $preselected = [];
208                         if (count($members)) {
209                                 foreach ($members as $member) {
210                                         $preselected[] = $member['id'];
211                                 }
212                         }
213                 }
214
215                 $drop_tpl = get_markup_template('group_drop.tpl');
216                 $drop_txt = replace_macros($drop_tpl, [
217                         '$id' => $group['id'],
218                         '$delete' => L10n::t('Delete Group'),
219                         '$form_security_token' => get_form_security_token("group_drop"),
220                 ]);
221
222
223                 $context = $context + [
224                         '$title' => $group['name'],
225                         '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
226                         '$gid' => $group['id'],
227                         '$drop' => $drop_txt,
228                         '$form_security_token' => get_form_security_token('group_edit'),
229                         '$edit_name' => L10n::t('Edit Group Name'),
230                         '$editable' => 1,
231                 ];
232
233         }
234
235         if (!isset($group)) {
236                 return;
237         }
238
239         $groupeditor = [
240                 'label_members' => L10n::t('Members'),
241                 'members' => [],
242                 'label_contacts' => L10n::t('All Contacts'),
243                 'group_is_empty' => L10n::t('Group is empty'),
244                 'contacts' => [],
245         ];
246
247         $sec_token = addslashes(get_form_security_token('group_member_change'));
248
249         // Format the data of the group members
250         foreach ($members as $member) {
251                 if ($member['url']) {
252                         $entry = Contact::getContactTemplateVars($member);
253                         $entry['label'] = 'members';
254                         $entry['photo_menu'] = '';
255                         $entry['change_member'] = [
256                                 'title'     => L10n::t("Remove contact from group"),
257                                 'gid'       => $group['id'],
258                                 'cid'       => $member['id'],
259                                 'sec_token' => $sec_token
260                         ];
261
262                         $groupeditor['members'][] = $entry;
263                 } else {
264                         Group::removeMember($group['id'], $member['id']);
265                 }
266         }
267
268         if ($nogroup) {
269                 $r = Model\Contact::getUngroupedList(local_user());
270         } else {
271                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
272                         intval(local_user())
273                 );
274                 $context['$desc'] = L10n::t('Click on a contact to add or remove.');
275         }
276
277         if (DBA::isResult($r)) {
278                 // Format the data of the contacts who aren't in the contact group
279                 foreach ($r as $member) {
280                         if (!in_array($member['id'], $preselected)) {
281                                 $entry = Contact::getContactTemplateVars($member);
282                                 $entry['label'] = 'contacts';
283                                 if (!$nogroup)
284                                         $entry['photo_menu'] = [];
285
286                                 if (!$nogroup) {
287                                         $entry['change_member'] = [
288                                                 'title'     => L10n::t("Add contact to group"),
289                                                 'gid'       => $group['id'],
290                                                 'cid'       => $member['id'],
291                                                 'sec_token' => $sec_token
292                                         ];
293                                 }
294
295                                 $groupeditor['contacts'][] = $entry;
296                         }
297                 }
298         }
299
300         $context['$groupeditor'] = $groupeditor;
301
302         // If there are to many contacts we could provide an alternative view mode
303         $total = count($groupeditor['members']) + count($groupeditor['contacts']);
304         $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
305
306         if ($change) {
307                 $tpl = get_markup_template('groupeditor.tpl');
308                 echo replace_macros($tpl, $context);
309                 killme();
310         }
311
312         return replace_macros($tpl, $context);
313
314 }