]> git.mxchange.org Git - friendica.git/blob - mod/group.php
some restructuring in groups.php + Frio: provide own group template
[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 validate_members(&$item) {
10         $item = intval($item);
11 }
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(App::get_baseurl() . '/group/' . $r);
37                         }
38                 } else {
39                         notice(t('Could not create group.') . EOL);
40                 }
41                 goaway(App::get_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(App::get_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 = get_pconfig(local_user(), 'system', 'groupedit_image_limit');
87         if ($switchtotext === false) {
88                 $switchtotext = get_config('system', 'groupedit_image_limit');
89         }
90         if ($switchtotext === false) {
91                 $switchtotext = 400;
92         }
93
94         $tpl = get_markup_template('group_edit.tpl');
95
96         $context = array(
97                         '$submit' => t('Save Group'),
98         );
99
100         if (($a->argc == 2) && ($a->argv[1] === 'new')) {
101                 return replace_macros($tpl, $context + array(
102                         '$title' => t('Create a group of contacts/friends.'),
103                         '$gname' => array('groupname', t('Group Name: '), '', ''),
104                         '$gid' => 'new',
105                         '$form_security_token' => get_form_security_token("group_edit"),
106                 ));
107
108
109         }
110
111         if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
112                 check_form_security_token_redirectOnErr('/group', 'group_drop', 't');
113
114                 if (intval($a->argv[2])) {
115                         $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
116                                 intval($a->argv[2]),
117                                 intval(local_user())
118                         );
119
120                         $result = null;
121
122                         if (dbm::is_result($r)) {
123                                 $result = group_rmv(local_user(), $r[0]['name']);
124                         }
125
126                         if ($result) {
127                                 info(t('Group removed.') . EOL);
128                         } else {
129                                 notice(t('Unable to remove group.') . EOL);
130                         }
131                 }
132                 goaway(App::get_baseurl() . '/group');
133                 // NOTREACHED
134         }
135
136         if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
137                 check_form_security_token_ForbiddenOnErr('group_member_change', 't');
138
139                 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
140                         intval($a->argv[2]),
141                         intval(local_user())
142                 );
143                 if (dbm::is_result($r)) {
144                         $change = intval($a->argv[2]);
145                 }
146         }
147
148         if (($a->argc > 1) && (intval($a->argv[1]))) {
149                 require_once 'include/acl_selectors.php';
150                 require_once 'mod/contacts.php';
151
152                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
153                         intval($a->argv[1]),
154                         intval(local_user())
155                 );
156
157                 if (! dbm::is_result($r)) {
158                         notice(t('Group not found.') . EOL);
159                         goaway(App::get_baseurl() . '/contacts');
160                 }
161
162                 $group = $r[0];
163                 $members = group_get_members($group['id']);
164                 $preselected = array();
165                 $entry = array();
166                 $id = 0;
167
168                 if (count($members)) {
169                         foreach ($members as $member) {
170                                 $preselected[] = $member['id'];
171                         }
172                 }
173
174                 if ($change) {
175                         if (in_array($change, $preselected)) {
176                                 group_rmv_member(local_user(), $group['name'], $change);
177                         } else {
178                                 group_add_member(local_user(), $group['name'], $change);
179                         }
180
181                         $members = group_get_members($group['id']);
182                         $preselected = array();
183                         if (count($members)) {
184                                 foreach ($members as $member) {
185                                         $preselected[] = $member['id'];
186                                 }
187                         }
188                 }
189
190                 $drop_tpl = get_markup_template('group_drop.tpl');
191                 $drop_txt = replace_macros($drop_tpl, array(
192                         '$id' => $group['id'],
193                         '$delete' => t('Delete Group'),
194                         '$form_security_token' => get_form_security_token("group_drop"),
195                 ));
196
197
198                 $context = $context + array(
199                         '$title' => t('Group Editor'),
200                         '$gname' => array('groupname', t('Group Name: '), $group['name'], ''),
201                         '$gid' => $group['id'],
202                         '$drop' => $drop_txt,
203                         '$form_security_token' => get_form_security_token('group_edit'),
204                         '$edit_name' => t('Edit Group Name')
205                 );
206
207         }
208
209         if (! isset($group)) {
210                 return;
211         }
212
213         $groupeditor = array(
214                 'label_members' => t('Members'),
215                 'members' => array(),
216                 'label_contacts' => t('All Contacts'),
217                 'group_is_empty' => t('Group is empty'),
218                 'contacts' => array(),
219         );
220
221         $sec_token = addslashes(get_form_security_token('group_member_change'));
222
223         // Format the data of the group members
224         foreach ($members as $member) {
225                 if ($member['url']) {
226                         $entry = _contact_detail_for_template($member);
227                         $entry['label'] = 'members';
228                         $entry['photo_menu'] = '';
229                         $entry['change_member'] = array(
230                                 'title'     => t("Remove Contact"),
231                                 'gid'       => $group['id'],
232                                 'cid'       => $member['id'],
233                                 'sec_token' => $sec_token
234                         );
235
236                         $groupeditor['members'][] = $entry;
237                 } else {
238                         group_rmv_member(local_user(), $group['name'], $member['id']);
239                 }
240         }
241
242         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
243                 intval(local_user())
244         );
245
246         if (dbm::is_result($r)) {
247                 // Format the data of the contacts who aren't in the contact group
248                 foreach ($r as $member) {
249                         if (! in_array($member['id'], $preselected)) {
250                                 $entry = _contact_detail_for_template($member);
251                                 $entry['label'] = 'contacts';
252                                 $entry['photo_menu'] = '';
253                                 $entry['change_member'] = array(
254                                         'title'     => t("Add Contact"),
255                                         'gid'       => $group['id'],
256                                         'cid'       => $member['id'],
257                                         'sec_token' => $sec_token
258                                 );
259
260                                 $groupeditor['contacts'][] = $entry;
261                         }
262                 }
263         }
264
265         $context['$groupeditor'] = $groupeditor;
266         $context['$desc'] = t('Click on a contact to add or remove.');
267
268         // If there are to many contacts we could provide an alternative view mode
269         $total = count($groupeditor['members']) + count($groupeditor['contacts']);
270         $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
271
272         if ($change) {
273                 $tpl = get_markup_template('groupeditor.tpl');
274                 echo replace_macros($tpl, $context);
275                 killme();
276         }
277
278         return replace_macros($tpl, $context);
279
280 }