]> git.mxchange.org Git - friendica.git/blob - mod/group.php
Merge pull request #1338 from annando/1501-global-contacts
[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",
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         $context = array(
87                         '$submit' => t('Save Group'),
88         );
89
90         if(($a->argc == 2) && ($a->argv[1] === 'new')) {
91
92                 return replace_macros($tpl, $context + array(
93                         '$title' => t('Create a group of contacts/friends.'),
94                         '$gname' => array('groupname',t('Group Name: '), '', ''),
95                         '$gid' => 'new',
96                         '$form_security_token' => get_form_security_token("group_edit"),
97                 ));
98
99
100         }
101
102         if(($a->argc == 3) && ($a->argv[1] === 'drop')) {
103                 check_form_security_token_redirectOnErr('/group', 'group_drop', 't');
104
105                 if(intval($a->argv[2])) {
106                         $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
107                                 intval($a->argv[2]),
108                                 intval(local_user())
109                         );
110                         if(count($r))
111                                 $result = group_rmv(local_user(),$r[0]['name']);
112                         if($result)
113                                 info( t('Group removed.') . EOL);
114                         else
115                                 notice( t('Unable to remove group.') . EOL);
116                 }
117                 goaway($a->get_baseurl() . '/group');
118                 // NOTREACHED
119         }
120
121         if(($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
122                 check_form_security_token_ForbiddenOnErr('group_member_change', 't');
123
124                 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
125                         intval($a->argv[2]),
126                         intval(local_user())
127                 );
128                 if(count($r))
129                         $change = intval($a->argv[2]);
130         }
131
132         if(($a->argc > 1) && (intval($a->argv[1]))) {
133
134                 require_once('include/acl_selectors.php');
135                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
136                         intval($a->argv[1]),
137                         intval(local_user())
138                 );
139                 if(! count($r)) {
140                         notice( t('Group not found.') . EOL );
141                         goaway($a->get_baseurl() . '/contacts');
142                 }
143                 $group = $r[0];
144                 $members = group_get_members($group['id']);
145                 $preselected = array();
146                 if(count($members))     {
147                         foreach($members as $member)
148                                 $preselected[] = $member['id'];
149                 }
150
151                 if($change) {
152                         if(in_array($change,$preselected)) {
153                                 group_rmv_member(local_user(),$group['name'],$change);
154                         }
155                         else {
156                                 group_add_member(local_user(),$group['name'],$change);
157                         }
158
159                         $members = group_get_members($group['id']);
160                         $preselected = array();
161                         if(count($members))     {
162                                 foreach($members as $member)
163                                         $preselected[] = $member['id'];
164                         }
165                 }
166
167
168                 $drop_tpl = get_markup_template('group_drop.tpl');
169                 $drop_txt = replace_macros($drop_tpl, array(
170                         '$id' => $group['id'],
171                         '$delete' => t('Delete'),
172                         '$form_security_token' => get_form_security_token("group_drop"),
173                 ));
174
175                 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
176
177                 
178                 $context = $context + array(
179                         '$title' => t('Group Editor'),
180                         '$gname' => array('groupname',t('Group Name: '),$group['name'], ''),
181                         '$gid' => $group['id'],
182                         '$drop' => $drop_txt,
183                         '$form_security_token' => get_form_security_token('group_edit'),
184                 );
185
186         }
187
188         if(! isset($group))
189                 return;
190
191         $groupeditor = array(
192                 'label_members' => t('Members'),
193                 'members' => array(),
194                 'label_contacts' => t('All Contacts'),
195                 'contacts' => array(),
196         );
197                 
198         $sec_token = addslashes(get_form_security_token('group_member_change'));
199         $textmode = (($switchtotext && (count($members) > $switchtotext)) ? true : false);
200         foreach($members as $member) {
201                 if($member['url']) {
202                         $member['click'] = 'groupChangeMember(' . $group['id'] . ',' . $member['id'] . ',\'' . $sec_token . '\'); return true;';
203                         $groupeditor['members'][] = micropro($member,true,'mpgroup', $textmode);
204                 }
205                 else
206                         group_rmv_member(local_user(),$group['name'],$member['id']);
207         }
208
209         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 and `pending` = 0 and `self` = 0 ORDER BY `name` ASC",
210                 intval(local_user())
211         );
212
213         if(count($r)) {
214                 $textmode = (($switchtotext && (count($r) > $switchtotext)) ? true : false);
215                 foreach($r as $member) {
216                         if(! in_array($member['id'],$preselected)) {
217                                 $member['click'] = 'groupChangeMember(' . $group['id'] . ',' . $member['id'] . ',\'' . $sec_token . '\'); return true;';
218                                 $groupeditor['contacts'][] = micropro($member,true,'mpall', $textmode);
219                         }
220                 }
221         }
222
223         $context['$groupeditor'] = $groupeditor;
224         $context['$desc'] = t('Click on a contact to add or remove.');
225
226         if($change) {
227                 $tpl = get_markup_template('groupeditor.tpl');
228                 echo replace_macros($tpl, $context);
229                 killme();
230         }
231         
232         return replace_macros($tpl, $context);
233
234 }
235