]> git.mxchange.org Git - friendica.git/blob - mod/group.php
fbd38bfeb33eadf8e404c5413f6d9f6693ef1d7d
[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         require_once('include/group.php');
9         $a->page['aside'] = group_side();
10
11 }
12
13
14
15 function group_post(&$a) {
16
17         if(! local_user()) {
18                 notice( t('Permission denied.') . EOL);
19                 return;
20         }
21
22         if(($a->argc == 2) && ($a->argv[1] == 'new')) {
23                 $name = notags(trim($_POST['groupname']));
24                 $r = group_add($_SESSION['uid'],$name);
25                 if($r) {
26                         notice( t('Group created.') . EOL );
27                         $r = group_byname($_SESSION['uid'],$name);
28                         if($r)
29                                 goaway($a->get_baseurl() . '/group/' . $r);
30                 }
31                 else
32                         notice( t('Could not create group.') . EOL );   
33                 goaway($a->get_baseurl() . '/group');
34                 return; // NOTREACHED
35         }
36         if(($a->argc == 2) && (intval($a->argv[1]))) {
37                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
38                         intval($a->argv[1]),
39                         intval($_SESSION['uid'])
40                 );
41                 if(! count($r)) {
42                         notice( t('Group not found.') . EOL );
43                         goaway($a->get_baseurl() . '/contacts');
44                 }
45                 $group = $r[0];
46                 $groupname = notags(trim($_POST['groupname']));
47                 if((strlen($groupname))  && ($groupname != $group['name'])) {
48                         $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
49                                 dbesc($groupname),
50                                 intval($_SESSION['uid']),
51                                 intval($group['id'])
52                         );
53                         if($r)
54                                 notice( t('Group name changed.') . EOL );
55                 }
56                 $members = $_POST['group_members_select'];
57                 array_walk($members,'validate_members');
58                 $r = q("DELETE FROM `group_member` WHERE `gid` = %d AND `uid` = %d",
59                         intval($a->argv[1]),
60                         intval($_SESSION['uid'])
61                 );
62                 $result = true;
63                 if(count($members)) {
64                         foreach($members as $member) {
65                                 $r = q("INSERT INTO `group_member` ( `uid`, `gid`, `contact-id`)
66                                         VALUES ( %d, %d, %d )",
67                                         intval($_SESSION['uid']),
68                                         intval($group['id']),
69                                         intval($member)
70                                 );
71                                 if(! $r)
72                                         $result = false;
73                         }
74                 }
75                 if($result)
76                         notice( t('Membership list updated.') . EOL);
77         $a->page['aside'] = group_side();
78         }
79         
80 }
81
82 function group_content(&$a) {
83
84         if(! local_user()) {
85                 notice( t('Permission denied') . EOL);
86                 return;
87         }
88
89         if(($a->argc == 2) && ($a->argv[1] == 'new')) {
90                 $tpl = file_get_contents('view/group_new.tpl');
91                 $o .= replace_macros($tpl,array(
92
93                 ));
94
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($_SESSION['uid'])
102                         );
103                         if(count($r)) 
104                                 $result = group_rmv($_SESSION['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($_SESSION['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 = file_get_contents('view/group_drop.tpl');
134                 $drop_txt = replace_macros($drop_tpl, array(
135                         '$id' => $group['id'],
136                         '$delete' => t('Delete')
137                 ));
138
139                 $tpl = file_get_contents('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
149
150
151
152
153         return $o;
154
155 }