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