]> git.mxchange.org Git - friendica.git/blob - mod/group.php
Merge branch 'develop' into rewrites/dbm_is_result
[friendica.git] / mod / group.php
1 <?php
2
3 function validate_members(&$item) {
4         $item = intval($item);
5 }
6
7 function group_init(App &$a) {
8         if(local_user()) {
9                 require_once('include/group.php');
10                 $a->page['aside'] = group_side('contacts','group','extended',(($a->argc > 1) ? intval($a->argv[1]) : 0));
11         }
12 }
13
14
15
16 function group_post(App &$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(App::get_baseurl() . '/group/' . $r);
33                         }
34                 }
35                 else {
36                         notice( t('Could not create group.') . EOL );
37                 }
38                 goaway(App::get_baseurl() . '/group');
39                 return; // NOTREACHED
40         }
41
42         if (($a->argc == 2) && (intval($a->argv[1]))) {
43                 check_form_security_token_redirectOnErr('/group', 'group_edit');
44
45                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
46                         intval($a->argv[1]),
47                         intval(local_user())
48                 );
49                 if (! dbm::is_result($r)) {
50                         notice( t('Group not found.') . EOL );
51                         goaway(App::get_baseurl() . '/contacts');
52                         return; // NOTREACHED
53                 }
54                 $group = $r[0];
55                 $groupname = notags(trim($_POST['groupname']));
56                 if ((strlen($groupname))  && ($groupname != $group['name'])) {
57                         $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d",
58                                 dbesc($groupname),
59                                 intval(local_user()),
60                                 intval($group['id'])
61                         );
62
63                         if ($r) {
64                                 info( t('Group name changed.') . EOL );
65                         }
66                 }
67
68                 $a->page['aside'] = group_side();
69         }
70         return;
71 }
72
73 function group_content(App &$a) {
74         $change = false;
75
76         if (! local_user()) {
77                 notice( t('Permission denied') . EOL);
78                 return;
79         }
80
81         // Switch to text mode interface if we have more than 'n' contacts or group members
82
83         $switchtotext = get_pconfig(local_user(),'system','groupedit_image_limit');
84         if($switchtotext === false)
85                 $switchtotext = get_config('system','groupedit_image_limit');
86         if($switchtotext === false)
87                 $switchtotext = 400;
88
89         $tpl = get_markup_template('group_edit.tpl');
90
91         $context = array(
92                         '$submit' => t('Save Group'),
93         );
94
95         if(($a->argc == 2) && ($a->argv[1] === 'new')) {
96
97                 return replace_macros($tpl, $context + array(
98                         '$title' => t('Create a group of contacts/friends.'),
99                         '$gname' => array('groupname',t('Group Name: '), '', ''),
100                         '$gid' => 'new',
101                         '$form_security_token' => get_form_security_token("group_edit"),
102                 ));
103
104
105         }
106
107         if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
108                 check_form_security_token_redirectOnErr('/group', 'group_drop', 't');
109
110                 if (intval($a->argv[2])) {
111                         $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
112                                 intval($a->argv[2]),
113                                 intval(local_user())
114                         );
115
116                         $result = null;
117
118                         if (dbm::is_result($r)) {
119                                 $result = group_rmv(local_user(),$r[0]['name']);
120                         }
121
122                         if ($result) {
123                                 info( t('Group removed.') . EOL);
124                         } else {
125                                 notice( t('Unable to remove group.') . EOL);
126                         }
127                 }
128                 goaway(App::get_baseurl() . '/group');
129                 // NOTREACHED
130         }
131
132         if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
133                 check_form_security_token_ForbiddenOnErr('group_member_change', 't');
134
135                 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
136                         intval($a->argv[2]),
137                         intval(local_user())
138                 );
139                 if (dbm::is_result($r))
140                         $change = intval($a->argv[2]);
141         }
142
143         if (($a->argc > 1) && (intval($a->argv[1]))) {
144
145                 require_once('include/acl_selectors.php');
146                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
147                         intval($a->argv[1]),
148                         intval(local_user())
149                 );
150                 if (! dbm::is_result($r)) {
151                         notice( t('Group not found.') . EOL );
152                         goaway(App::get_baseurl() . '/contacts');
153                 }
154                 $group = $r[0];
155                 $members = group_get_members($group['id']);
156                 $preselected = array();
157                 if(count($members))     {
158                         foreach($members as $member)
159                                 $preselected[] = $member['id'];
160                 }
161
162                 if($change) {
163                         if(in_array($change,$preselected)) {
164                                 group_rmv_member(local_user(),$group['name'],$change);
165                         }
166                         else {
167                                 group_add_member(local_user(),$group['name'],$change);
168                         }
169
170                         $members = group_get_members($group['id']);
171                         $preselected = array();
172                         if(count($members))     {
173                                 foreach($members as $member)
174                                         $preselected[] = $member['id'];
175                         }
176                 }
177
178
179                 $drop_tpl = get_markup_template('group_drop.tpl');
180                 $drop_txt = replace_macros($drop_tpl, array(
181                         '$id' => $group['id'],
182                         '$delete' => t('Delete'),
183                         '$form_security_token' => get_form_security_token("group_drop"),
184                 ));
185
186
187                 $context = $context + array(
188                         '$title' => t('Group Editor'),
189                         '$gname' => array('groupname',t('Group Name: '),$group['name'], ''),
190                         '$gid' => $group['id'],
191                         '$drop' => $drop_txt,
192                         '$form_security_token' => get_form_security_token('group_edit'),
193                 );
194
195         }
196
197         if(! isset($group))
198                 return;
199
200         $groupeditor = array(
201                 'label_members' => t('Members'),
202                 'members' => array(),
203                 'label_contacts' => t('All Contacts'),
204                 'group_is_empty' => t('Group is empty'),
205                 'contacts' => array(),
206         );
207
208         $sec_token = addslashes(get_form_security_token('group_member_change'));
209         $textmode = (($switchtotext && (count($members) > $switchtotext)) ? true : false);
210         foreach($members as $member) {
211                 if($member['url']) {
212                         $member['click'] = 'groupChangeMember(' . $group['id'] . ',' . $member['id'] . ',\'' . $sec_token . '\'); return true;';
213                         $groupeditor['members'][] = micropro($member,true,'mpgroup', $textmode);
214                 }
215                 else
216                         group_rmv_member(local_user(),$group['name'],$member['id']);
217         }
218
219         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
220                 intval(local_user())
221         );
222
223         if (dbm::is_result($r)) {
224                 $textmode = (($switchtotext && (count($r) > $switchtotext)) ? true : false);
225                 foreach($r as $member) {
226                         if(! in_array($member['id'],$preselected)) {
227                                 $member['click'] = 'groupChangeMember(' . $group['id'] . ',' . $member['id'] . ',\'' . $sec_token . '\'); return true;';
228                                 $groupeditor['contacts'][] = micropro($member,true,'mpall', $textmode);
229                         }
230                 }
231         }
232
233         $context['$groupeditor'] = $groupeditor;
234         $context['$desc'] = t('Click on a contact to add or remove.');
235
236         if($change) {
237                 $tpl = get_markup_template('groupeditor.tpl');
238                 echo replace_macros($tpl, $context);
239                 killme();
240         }
241
242         return replace_macros($tpl, $context);
243
244 }