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