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