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