]> git.mxchange.org Git - friendica.git/blob - mod/group.php
Use new Model methods for groups
[friendica.git] / mod / group.php
1 <?php
2 /**
3  * @file mod/group.php
4  * @brief The group module (create and rename contact groups, add and
5  *      remove contacts to the contact groups
6  */
7
8 use Friendica\App;
9 use Friendica\Core\Config;
10 use Friendica\Core\PConfig;
11 use Friendica\Core\System;
12 use Friendica\Database\DBM;
13 use Friendica\Model\Contact;
14 use Friendica\Model\Group;
15
16 function group_init(App $a) {
17         if (local_user()) {
18                 require_once 'include/group.php';
19                 $a->page['aside'] = Group::sidebarWidget('contacts', 'group', 'extended', (($a->argc > 1) ? intval($a->argv[1]) : 0));
20         }
21 }
22
23 function group_post(App $a) {
24
25         if (! local_user()) {
26                 notice(t('Permission denied.') . EOL);
27                 return;
28         }
29
30         if (($a->argc == 2) && ($a->argv[1] === 'new')) {
31                 check_form_security_token_redirectOnErr('/group/new', 'group_edit');
32
33                 $name = notags(trim($_POST['groupname']));
34                 $r = Group::create(local_user(), $name);
35                 if ($r) {
36                         info(t('Group created.') . EOL);
37                         $r = Group::getIdByName(local_user(), $name);
38                         if ($r) {
39                                 goaway(System::baseUrl() . '/group/' . $r);
40                         }
41                 } else {
42                         notice(t('Could not create group.') . EOL);
43                 }
44                 goaway(System::baseUrl() . '/group');
45                 return; // NOTREACHED
46         }
47
48         if (($a->argc == 2) && (intval($a->argv[1]))) {
49                 check_form_security_token_redirectOnErr('/group', 'group_edit');
50
51                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
52                         intval($a->argv[1]),
53                         intval(local_user())
54                 );
55                 if (! DBM::is_result($r)) {
56                         notice(t('Group not found.') . EOL);
57                         goaway(System::baseUrl() . '/contacts');
58                         return; // NOTREACHED
59                 }
60                 $group = $r[0];
61                 $groupname = notags(trim($_POST['groupname']));
62                 if ((strlen($groupname))  && ($groupname != $group['name'])) {
63                         $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d",
64                                 dbesc($groupname),
65                                 intval(local_user()),
66                                 intval($group['id'])
67                         );
68
69                         if ($r) {
70                                 info(t('Group name changed.') . EOL);
71                         }
72                 }
73
74                 $a->page['aside'] = Group::sidebarWidget();
75         }
76         return;
77 }
78
79 function group_content(App $a) {
80         $change = false;
81
82         if (! local_user()) {
83                 notice(t('Permission denied') . EOL);
84                 return;
85         }
86
87         // Switch to text mode interface if we have more than 'n' contacts or group members
88
89         $switchtotext = PConfig::get(local_user(), 'system', 'groupedit_image_limit');
90         if (is_null($switchtotext)) {
91                 $switchtotext = Config::get('system', 'groupedit_image_limit', 400);
92         }
93
94         $tpl = get_markup_template('group_edit.tpl');
95
96         $context = array(
97                         '$submit' => t('Save Group'),
98         );
99
100         if (($a->argc == 2) && ($a->argv[1] === 'new')) {
101                 return replace_macros($tpl, $context + array(
102                         '$title' => t('Create a group of contacts/friends.'),
103                         '$gname' => array('groupname', t('Group Name: '), '', ''),
104                         '$gid' => 'new',
105                         '$form_security_token' => get_form_security_token("group_edit"),
106                 ));
107
108
109         }
110
111         if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
112                 check_form_security_token_redirectOnErr('/group', 'group_drop', 't');
113
114                 if (intval($a->argv[2])) {
115                         $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
116                                 intval($a->argv[2]),
117                                 intval(local_user())
118                         );
119
120                         $result = null;
121
122                         if (DBM::is_result($r)) {
123                                 $result = Group::removeByName(local_user(), $r[0]['name']);
124                         }
125
126                         if ($result) {
127                                 info(t('Group removed.') . EOL);
128                         } else {
129                                 notice(t('Unable to remove group.') . EOL);
130                         }
131                 }
132                 goaway(System::baseUrl() . '/group');
133                 // NOTREACHED
134         }
135
136         if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
137                 check_form_security_token_ForbiddenOnErr('group_member_change', 't');
138
139                 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
140                         intval($a->argv[2]),
141                         intval(local_user())
142                 );
143                 if (DBM::is_result($r)) {
144                         $change = intval($a->argv[2]);
145                 }
146         }
147
148         if (($a->argc > 1) && (intval($a->argv[1]))) {
149                 require_once 'include/acl_selectors.php';
150                 require_once 'mod/contacts.php';
151
152                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
153                         intval($a->argv[1]),
154                         intval(local_user())
155                 );
156
157                 if (! DBM::is_result($r)) {
158                         notice(t('Group not found.') . EOL);
159                         goaway(System::baseUrl() . '/contacts');
160                 }
161
162                 $group = $r[0];
163                 $members = Contact::getByGroupId($group['id']);
164                 $preselected = array();
165                 $entry = array();
166                 $id = 0;
167
168                 if (count($members)) {
169                         foreach ($members as $member) {
170                                 $preselected[] = $member['id'];
171                         }
172                 }
173
174                 if ($change) {
175                         if (in_array($change, $preselected)) {
176                                 Group::removeMember($group['id'], $change);
177                         } else {
178                                 Group::create_member(local_user(), $group['name'], $change);
179                         }
180
181                         $members = Contact::getByGroupId($group['id']);
182                         $preselected = array();
183                         if (count($members)) {
184                                 foreach ($members as $member) {
185                                         $preselected[] = $member['id'];
186                                 }
187                         }
188                 }
189
190                 $drop_tpl = get_markup_template('group_drop.tpl');
191                 $drop_txt = replace_macros($drop_tpl, array(
192                         '$id' => $group['id'],
193                         '$delete' => t('Delete Group'),
194                         '$form_security_token' => get_form_security_token("group_drop"),
195                 ));
196
197
198                 $context = $context + array(
199                         '$title' => t('Group Editor'),
200                         '$gname' => array('groupname', t('Group Name: '), $group['name'], ''),
201                         '$gid' => $group['id'],
202                         '$drop' => $drop_txt,
203                         '$form_security_token' => get_form_security_token('group_edit'),
204                         '$edit_name' => t('Edit Group Name')
205                 );
206
207         }
208
209         if (! isset($group)) {
210                 return;
211         }
212
213         $groupeditor = array(
214                 'label_members' => t('Members'),
215                 'members' => array(),
216                 'label_contacts' => t('All Contacts'),
217                 'group_is_empty' => t('Group is empty'),
218                 'contacts' => array(),
219         );
220
221         $sec_token = addslashes(get_form_security_token('group_member_change'));
222
223         // Format the data of the group members
224         foreach ($members as $member) {
225                 if ($member['url']) {
226                         $entry = _contact_detail_for_template($member);
227                         $entry['label'] = 'members';
228                         $entry['photo_menu'] = '';
229                         $entry['change_member'] = array(
230                                 'title'     => t("Remove Contact"),
231                                 'gid'       => $group['id'],
232                                 'cid'       => $member['id'],
233                                 'sec_token' => $sec_token
234                         );
235
236                         $groupeditor['members'][] = $entry;
237                 } else {
238                         Group::removeMember($group['id'], $member['id']);
239                 }
240         }
241
242         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
243                 intval(local_user())
244         );
245
246         if (DBM::is_result($r)) {
247                 // Format the data of the contacts who aren't in the contact group
248                 foreach ($r as $member) {
249                         if (! in_array($member['id'], $preselected)) {
250                                 $entry = _contact_detail_for_template($member);
251                                 $entry['label'] = 'contacts';
252                                 $entry['photo_menu'] = '';
253                                 $entry['change_member'] = array(
254                                         'title'     => t("Add Contact"),
255                                         'gid'       => $group['id'],
256                                         'cid'       => $member['id'],
257                                         'sec_token' => $sec_token
258                                 );
259
260                                 $groupeditor['contacts'][] = $entry;
261                         }
262                 }
263         }
264
265         $context['$groupeditor'] = $groupeditor;
266         $context['$desc'] = t('Click on a contact to add or remove.');
267
268         // If there are to many contacts we could provide an alternative view mode
269         $total = count($groupeditor['members']) + count($groupeditor['contacts']);
270         $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
271
272         if ($change) {
273                 $tpl = get_markup_template('groupeditor.tpl');
274                 echo replace_macros($tpl, $context);
275                 killme();
276         }
277
278         return replace_macros($tpl, $context);
279
280 }