]> git.mxchange.org Git - friendica.git/blob - mod/group.php
08b7801a7b3f32dbd2c361cd7a55d675f5ee2db6
[friendica.git] / mod / group.php
1 <?php
2
3 function validate_members(&$item) {
4         $item = intval($item);
5 }
6
7 function group_init(App &$a) {
8         if(local_user()) {
9                 require_once('include/group.php');
10                 $a->page['aside'] = group_side('contacts','group','extended',(($a->argc > 1) ? intval($a->argv[1]) : 0));
11         }
12 }
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                 }
35                 else {
36                         notice( t('Could not create group.') . EOL );
37                 }
38                 goaway(App::get_baseurl() . '/group');
39                 return; // NOTREACHED
40         }
41
42         if (($a->argc == 2) && (intval($a->argv[1]))) {
43                 check_form_security_token_redirectOnErr('/group', 'group_edit');
44
45                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
46                         intval($a->argv[1]),
47                         intval(local_user())
48                 );
49                 if (! dbm::is_result($r)) {
50                         notice( t('Group not found.') . EOL );
51                         goaway(App::get_baseurl() . '/contacts');
52                         return; // NOTREACHED
53                 }
54                 $group = $r[0];
55                 $groupname = notags(trim($_POST['groupname']));
56                 if ((strlen($groupname))  && ($groupname != $group['name'])) {
57                         $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d",
58                                 dbesc($groupname),
59                                 intval(local_user()),
60                                 intval($group['id'])
61                         );
62
63                         if ($r) {
64                                 info( t('Group name changed.') . EOL );
65                         }
66                 }
67
68                 $a->page['aside'] = group_side();
69         }
70         return;
71 }
72
73 function group_content(App &$a) {
74         $change = false;
75
76         if(! local_user()) {
77                 notice( t('Permission denied') . EOL);
78                 return;
79         }
80
81         // Switch to text mode interface if we have more than 'n' contacts or group members
82
83         $switchtotext = get_pconfig(local_user(),'system','groupedit_image_limit');
84         if($switchtotext === false)
85                 $switchtotext = get_config('system','groupedit_image_limit');
86         if($switchtotext === false)
87                 $switchtotext = 400;
88
89         $tpl = get_markup_template('group_edit.tpl');
90
91         $context = array(
92                         '$submit' => t('Save Group'),
93         );
94
95         if(($a->argc == 2) && ($a->argv[1] === 'new')) {
96
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                         if (dbm::is_result($r))
116                                 $result = group_rmv(local_user(),$r[0]['name']);
117                         if($result)
118                                 info( t('Group removed.') . EOL);
119                         else
120                                 notice( t('Unable to remove group.') . EOL);
121                 }
122                 goaway(App::get_baseurl() . '/group');
123                 // NOTREACHED
124         }
125
126         if(($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
127                 check_form_security_token_ForbiddenOnErr('group_member_change', 't');
128
129                 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
130                         intval($a->argv[2]),
131                         intval(local_user())
132                 );
133                 if (dbm::is_result($r))
134                         $change = intval($a->argv[2]);
135         }
136
137         if(($a->argc > 1) && (intval($a->argv[1]))) {
138
139                 require_once('include/acl_selectors.php');
140                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
141                         intval($a->argv[1]),
142                         intval(local_user())
143                 );
144                 if (! dbm::is_result($r)) {
145                         notice( t('Group not found.') . EOL );
146                         goaway(App::get_baseurl() . '/contacts');
147                 }
148                 $group = $r[0];
149                 $members = group_get_members($group['id']);
150                 $preselected = array();
151                 if(count($members))     {
152                         foreach($members as $member)
153                                 $preselected[] = $member['id'];
154                 }
155
156                 if($change) {
157                         if(in_array($change,$preselected)) {
158                                 group_rmv_member(local_user(),$group['name'],$change);
159                         }
160                         else {
161                                 group_add_member(local_user(),$group['name'],$change);
162                         }
163
164                         $members = group_get_members($group['id']);
165                         $preselected = array();
166                         if(count($members))     {
167                                 foreach($members as $member)
168                                         $preselected[] = $member['id'];
169                         }
170                 }
171
172
173                 $drop_tpl = get_markup_template('group_drop.tpl');
174                 $drop_txt = replace_macros($drop_tpl, array(
175                         '$id' => $group['id'],
176                         '$delete' => t('Delete'),
177                         '$form_security_token' => get_form_security_token("group_drop"),
178                 ));
179
180
181                 $context = $context + array(
182                         '$title' => t('Group Editor'),
183                         '$gname' => array('groupname',t('Group Name: '),$group['name'], ''),
184                         '$gid' => $group['id'],
185                         '$drop' => $drop_txt,
186                         '$form_security_token' => get_form_security_token('group_edit'),
187                 );
188
189         }
190
191         if(! isset($group))
192                 return;
193
194         $groupeditor = array(
195                 'label_members' => t('Members'),
196                 'members' => array(),
197                 'label_contacts' => t('All Contacts'),
198                 'group_is_empty' => t('Group is empty'),
199                 'contacts' => array(),
200         );
201
202         $sec_token = addslashes(get_form_security_token('group_member_change'));
203         $textmode = (($switchtotext && (count($members) > $switchtotext)) ? true : false);
204         foreach($members as $member) {
205                 if($member['url']) {
206                         $member['click'] = 'groupChangeMember(' . $group['id'] . ',' . $member['id'] . ',\'' . $sec_token . '\'); return true;';
207                         $groupeditor['members'][] = micropro($member,true,'mpgroup', $textmode);
208                 }
209                 else
210                         group_rmv_member(local_user(),$group['name'],$member['id']);
211         }
212
213         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
214                 intval(local_user())
215         );
216
217         if (dbm::is_result($r)) {
218                 $textmode = (($switchtotext && (count($r) > $switchtotext)) ? true : false);
219                 foreach($r as $member) {
220                         if(! in_array($member['id'],$preselected)) {
221                                 $member['click'] = 'groupChangeMember(' . $group['id'] . ',' . $member['id'] . ',\'' . $sec_token . '\'); return true;';
222                                 $groupeditor['contacts'][] = micropro($member,true,'mpall', $textmode);
223                         }
224                 }
225         }
226
227         $context['$groupeditor'] = $groupeditor;
228         $context['$desc'] = t('Click on a contact to add or remove.');
229
230         if($change) {
231                 $tpl = get_markup_template('groupeditor.tpl');
232                 echo replace_macros($tpl, $context);
233                 killme();
234         }
235
236         return replace_macros($tpl, $context);
237
238 }