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