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