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