]> git.mxchange.org Git - friendica.git/blob - mod/group.php
fix json_encode on url's
[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();
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                 $name = notags(trim($_POST['groupname']));
25                 $r = group_add(local_user(),$name);
26                 if($r) {
27                         notice( t('Group created.') . EOL );
28                         $r = group_byname(local_user(),$name);
29                         if($r)
30                                 goaway($a->get_baseurl() . '/group/' . $r);
31                 }
32                 else
33                         notice( t('Could not create group.') . EOL );   
34                 goaway($a->get_baseurl() . '/group');
35                 return; // NOTREACHED
36         }
37         if(($a->argc == 2) && (intval($a->argv[1]))) {
38                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
39                         intval($a->argv[1]),
40                         intval(local_user())
41                 );
42                 if(! count($r)) {
43                         notice( t('Group not found.') . EOL );
44                         goaway($a->get_baseurl() . '/contacts');
45                         return; // NOTREACHED
46                 }
47                 $group = $r[0];
48                 $groupname = notags(trim($_POST['groupname']));
49                 if((strlen($groupname))  && ($groupname != $group['name'])) {
50                         $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
51                                 dbesc($groupname),
52                                 intval(local_user()),
53                                 intval($group['id'])
54                         );
55                         if($r)
56                                 notice( t('Group name changed.') . EOL );
57                 }
58
59                 $a->page['aside'] = group_side();
60         }
61         return; 
62 }
63
64 function group_content(&$a) {
65
66         if(! local_user()) {
67                 notice( t('Permission denied') . EOL);
68                 return;
69         }
70
71         if(($a->argc == 2) && ($a->argv[1] === 'new')) {
72                 $tpl = get_markup_template('group_new.tpl');
73                 $o .= replace_macros($tpl,array(
74                         '$desc' => t('Create a group of contacts/friends.'),
75                         '$name' => t('Group Name: '),
76                         '$submit' => t('Submit')
77                  ));
78                 return $o;
79         }
80
81         if(($a->argc == 3) && ($a->argv[1] === 'drop')) {
82                 if(intval($a->argv[2])) {
83                         $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
84                                 intval($a->argv[2]),
85                                 intval(local_user())
86                         );
87                         if(count($r)) 
88                                 $result = group_rmv(local_user(),$r[0]['name']);
89                         if($result)
90                                 notice( t('Group removed.') . EOL);
91                         else
92                                 notice( t('Unable to remove group.') . EOL);
93                 }
94                 goaway($a->get_baseurl() . '/group');
95                 return; // NOTREACHED
96         }
97
98         if(($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
99                 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
100                         intval($a->argv[2]),
101                         intval(local_user())
102                 );
103                 if(count($r))
104                         $change = intval($a->argv[2]);
105         }
106
107         if(($a->argc > 1) && (intval($a->argv[1]))) {
108
109                 require_once('include/acl_selectors.php');
110                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
111                         intval($a->argv[1]),
112                         intval(local_user())
113                 );
114                 if(! count($r)) {
115                         notice( t('Group not found.') . EOL );
116                         goaway($a->get_baseurl() . '/contacts');
117                 }
118                 $group = $r[0];
119                 $members = group_get_members($group['id']);
120                 $preselected = array();
121                 if(count($members))     {
122                         foreach($members as $member)
123                                 $preselected[] = $member['id'];
124                 }
125
126                 if($change) {
127                         if(in_array($change,$preselected)) {
128                                 group_rmv_member(local_user(),$group['name'],$change);
129                         }
130                         else {
131                                 group_add_member(local_user(),$group['name'],$change);
132                         }
133
134                         $members = group_get_members($group['id']);
135                         $preselected = array();
136                         if(count($members))     {
137                                 foreach($members as $member)
138                                         $preselected[] = $member['id'];
139                         }
140                 }
141
142
143                 $drop_tpl = get_markup_template('group_drop.tpl');
144                 $drop_txt = replace_macros($drop_tpl, array(
145                         '$id' => $group['id'],
146                         '$delete' => t('Delete')
147                 ));
148
149                 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
150
151                 $tpl = get_markup_template('group_edit.tpl');
152                 $o .= replace_macros($tpl, array(
153                         '$gid' => $group['id'],
154                         '$name' => $group['name'],
155                         '$drop' => $drop_txt,
156                         '$desc' => t('Click on a contact to add or remove.'),
157                         '$title' => t('Group Editor'),
158                         '$gname' => t('Group Name: '),
159                         '$submit' => t('Submit')
160                 ));
161
162         }
163
164         $o .= '<div id="group-update-wrapper">';
165         if($change) 
166                 $o = '';
167
168         $o .= '<div id="group-members">';
169         $o .= '<h3>' . t('Members') . '</h3>';
170         foreach($members as $member) {
171                 if($member['url']) {
172                         $member['click'] = 'groupChangeMember(' . $group['id'] . ',' . $member['id'] . '); return true;';
173                         $o .= micropro($member,true,'mpgroup');
174                 }
175                 else
176                         group_rmv_member(local_user(),$group['name'],$member['id']);
177         }
178
179         $o .= '</div><div id="group-members-end"></div>';
180         $o .= '<hr id="group-separator" />';
181         $o .= '<div id="group-all-contacts">';
182
183                 $o .= '<h3>' . t('All Contacts') . '</h3>';
184                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 and `pending` = 0 and `self` = 0 ORDER BY `name` ASC",
185                         intval(local_user())
186                 );
187
188                 if(count($r)) {
189                         foreach($r as $member) {
190                                 if(! in_array($member['id'],$preselected)) {
191                                         $member['click'] = 'groupChangeMember(' . $group['id'] . ',' . $member['id'] . '); return true;';
192                                         $o .= micropro($member,true,'mpall');
193                                 }
194                         }
195                 }
196
197                 $o .= '</div><div id="group-all-contacts-end"></div>';
198
199         if($change) {
200                 echo $o;
201                 killme();
202         }
203         $o .= '</div>';
204         return $o;
205
206 }
207