]> git.mxchange.org Git - friendica.git/blob - mod/group.php
aa4751961775d8da4055133c173a3760cd5e93d7
[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(get_uid(),$name);
26                 if($r) {
27                         notice( t('Group created.') . EOL );
28                         $r = group_byname(get_uid(),$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(get_uid())
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(get_uid()),
53                                 intval($group['id'])
54                         );
55                         if($r)
56                                 notice( t('Group name changed.') . EOL );
57                 }
58                 $members = $_POST['group_members_select'];
59                 array_walk($members,'validate_members');
60                 $r = q("DELETE FROM `group_member` WHERE `gid` = %d AND `uid` = %d",
61                         intval($a->argv[1]),
62                         intval(get_uid())
63                 );
64                 $result = true;
65                 if(count($members)) {
66                         foreach($members as $member) {
67                                 $r = q("INSERT INTO `group_member` ( `uid`, `gid`, `contact-id`)
68                                         VALUES ( %d, %d, %d )",
69                                         intval(get_uid()),
70                                         intval($group['id']),
71                                         intval($member)
72                                 );
73                                 if(! $r)
74                                         $result = false;
75                         }
76                 }
77                 if($result)
78                         notice( t('Membership list updated.') . EOL);
79                 $a->page['aside'] = group_side();
80         }
81         return; 
82 }
83
84 function group_content(&$a) {
85
86         if(! local_user()) {
87                 notice( t('Permission denied') . EOL);
88                 return;
89         }
90
91         if(($a->argc == 2) && ($a->argv[1] == 'new')) {
92                 $tpl = load_view_file('view/group_new.tpl');
93                 $o .= replace_macros($tpl,array());
94                 return $o;
95         }
96
97         if(($a->argc == 3) && ($a->argv[1] == 'drop')) {
98                 if(intval($a->argv[2])) {
99                         $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
100                                 intval($a->argv[2]),
101                                 intval(get_uid())
102                         );
103                         if(count($r)) 
104                                 $result = group_rmv(get_uid(),$r[0]['name']);
105                         if($result)
106                                 notice( t('Group removed.') . EOL);
107                         else
108                                 notice( t('Unable to remove group.') . EOL);
109                 }
110                 goaway($a->get_baseurl() . '/group');
111                 return; // NOTREACHED
112         }
113
114
115         if(($a->argc == 2) && (intval($a->argv[1]))) {
116                 require_once('view/acl_selectors.php');
117                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
118                         intval($a->argv[1]),
119                         intval(get_uid())
120                 );
121                 if(! count($r)) {
122                         notice( t('Group not found.') . EOL );
123                         goaway($a->get_baseurl() . '/contacts');
124                 }
125                 $group = $r[0];
126                 $ret = group_get_members($group['id']);
127                 $preselected = array();
128                 if(count($ret)) {
129                         foreach($ret as $p)
130                                 $preselected[] = $p['id'];
131                 }
132
133                 $drop_tpl = load_view_file('view/group_drop.tpl');
134                 $drop_txt = replace_macros($drop_tpl, array(
135                         '$id' => $group['id'],
136                         '$delete' => t('Delete')
137                 ));
138
139                 $tpl = load_view_file('view/group_edit.tpl');
140                 $o .= replace_macros($tpl, array(
141                         '$gid' => $group['id'],
142                         '$name' => $group['name'],
143                         '$drop' => $drop_txt,
144                         '$selector' => contact_select('group_members_select','group_members_select',$preselected,25)
145                 ));
146
147         }
148         return $o;
149
150 }