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