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