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