1
0
Fork 0
friendica_2020-09-1_sharedH.../src/Module/Group.php

366 lines
11 KiB
PHP
Raw Normal View History

2019-05-01 21:29:04 +02:00
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
2019-05-01 21:29:04 +02:00
*/
namespace Friendica\Module;
use Friendica\BaseModule;
use Friendica\Core\Renderer;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
2019-05-01 21:29:04 +02:00
use Friendica\Model;
use Friendica\Util\Strings;
require_once 'boot.php';
class Group extends BaseModule
{
public static function post(array $parameters = [])
2019-05-01 21:29:04 +02:00
{
$a = DI::app();
2019-05-01 21:29:04 +02:00
if (DI::mode()->isAjax()) {
2019-05-01 21:29:04 +02:00
self::ajaxPost();
}
if (!local_user()) {
notice(DI::l10n()->t('Permission denied.'));
DI::baseUrl()->redirect();
2019-05-01 21:29:04 +02:00
}
// @TODO: Replace with parameter from router
if (($a->argc == 2) && ($a->argv[1] === 'new')) {
BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
$name = Strings::escapeTags(trim($_POST['groupname']));
$r = Model\Group::create(local_user(), $name);
if ($r) {
$r = Model\Group::getIdByName(local_user(), $name);
if ($r) {
DI::baseUrl()->redirect('group/' . $r);
2019-05-01 21:29:04 +02:00
}
} else {
notice(DI::l10n()->t('Could not create group.'));
2019-05-01 21:29:04 +02:00
}
DI::baseUrl()->redirect('group');
2019-05-01 21:29:04 +02:00
}
// @TODO: Replace with parameter from router
if (($a->argc == 2) && intval($a->argv[1])) {
BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
$group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user()]);
if (!DBA::isResult($group)) {
notice(DI::l10n()->t('Group not found.'));
DI::baseUrl()->redirect('contact');
2019-05-01 21:29:04 +02:00
}
$groupname = Strings::escapeTags(trim($_POST['groupname']));
if (strlen($groupname) && ($groupname != $group['name'])) {
2020-07-23 08:11:21 +02:00
if (!Model\Group::update($group['id'], $groupname)) {
notice(DI::l10n()->t('Group name was not changed.'));
2019-05-01 21:29:04 +02:00
}
}
}
}
public static function ajaxPost()
{
try {
$a = DI::app();
2019-05-01 21:29:04 +02:00
if (!local_user()) {
throw new \Exception(DI::l10n()->t('Permission denied.'), 403);
2019-05-01 21:29:04 +02:00
}
// POST /group/123/add/123
// POST /group/123/remove/123
// @TODO: Replace with parameter from router
if ($a->argc == 4) {
list($group_id, $command, $contact_id) = array_slice($a->argv, 1);
if (!Model\Group::exists($group_id, local_user())) {
throw new \Exception(DI::l10n()->t('Unknown group.'), 404);
2019-05-01 21:29:04 +02:00
}
$contact = DBA::selectFirst('contact', ['deleted'], ['id' => $contact_id, 'uid' => local_user()]);
2019-05-01 21:29:04 +02:00
if (!DBA::isResult($contact)) {
throw new \Exception(DI::l10n()->t('Contact not found.'), 404);
2019-05-01 21:29:04 +02:00
}
if ($contact['deleted']) {
throw new \Exception(DI::l10n()->t('Contact is deleted.'), 410);
2019-05-01 21:29:04 +02:00
}
switch($command) {
case 'add':
if (!Model\Group::addMember($group_id, $contact_id)) {
throw new \Exception(DI::l10n()->t('Unable to add the contact to the group.'), 500);
2019-05-01 21:29:04 +02:00
}
$message = DI::l10n()->t('Contact successfully added to group.');
2019-05-01 21:29:04 +02:00
break;
case 'remove':
if (!Model\Group::removeMember($group_id, $contact_id)) {
throw new \Exception(DI::l10n()->t('Unable to remove the contact from the group.'), 500);
2019-05-01 21:29:04 +02:00
}
$message = DI::l10n()->t('Contact successfully removed from group.');
2019-05-01 21:29:04 +02:00
break;
default:
throw new \Exception(DI::l10n()->t('Unknown group command.'), 400);
2019-05-01 21:29:04 +02:00
}
} else {
throw new \Exception(DI::l10n()->t('Bad request.'), 400);
2019-05-01 21:29:04 +02:00
}
info($message);
2019-05-01 21:29:04 +02:00
System::jsonExit(['status' => 'OK', 'message' => $message]);
} catch (\Exception $e) {
notice($e->getMessage());
System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
}
}
public static function content(array $parameters = [])
2019-05-01 21:29:04 +02:00
{
$change = false;
if (!local_user()) {
throw new \Friendica\Network\HTTPException\ForbiddenException();
2019-05-01 21:29:04 +02:00
}
$a = DI::app();
2019-05-01 21:29:04 +02:00
DI::page()['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone'));
2019-05-01 21:29:04 +02:00
// With no group number provided we jump to the unassigned contacts as a starting point
// @TODO: Replace with parameter from router
if ($a->argc == 1) {
DI::baseUrl()->redirect('group/none');
2019-05-01 21:29:04 +02:00
}
// Switch to text mode interface if we have more than 'n' contacts or group members
$switchtotext = DI::pConfig()->get(local_user(), 'system', 'groupedit_image_limit');
2019-05-01 21:29:04 +02:00
if (is_null($switchtotext)) {
$switchtotext = DI::config()->get('system', 'groupedit_image_limit', 200);
2019-05-01 21:29:04 +02:00
}
$tpl = Renderer::getMarkupTemplate('group_edit.tpl');
$context = [
'$submit' => DI::l10n()->t('Save Group'),
'$submit_filter' => DI::l10n()->t('Filter'),
2019-05-01 21:29:04 +02:00
];
// @TODO: Replace with parameter from router
if (($a->argc == 2) && ($a->argv[1] === 'new')) {
return Renderer::replaceMacros($tpl, $context + [
'$title' => DI::l10n()->t('Create a group of contacts/friends.'),
'$gname' => ['groupname', DI::l10n()->t('Group Name: '), '', ''],
2019-05-01 21:29:04 +02:00
'$gid' => 'new',
'$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
]);
}
$nogroup = false;
2019-05-03 08:50:17 +02:00
// @TODO: Replace with parameter from router
if (($a->argc == 2) && ($a->argv[1] === 'none') ||
($a->argc == 1) && ($a->argv[0] === 'nogroup')) {
2019-05-01 21:29:04 +02:00
$id = -1;
$nogroup = true;
$group = [
'id' => $id,
'name' => DI::l10n()->t('Contacts not in any group'),
2019-05-01 21:29:04 +02:00
];
$members = [];
$preselected = [];
$context = $context + [
'$title' => $group['name'],
'$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
2019-05-01 21:29:04 +02:00
'$gid' => $id,
'$editable' => 0,
];
}
// @TODO: Replace with parameter from router
if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
// @TODO: Replace with parameter from router
if (intval($a->argv[2])) {
if (!Model\Group::exists($a->argv[2], local_user())) {
notice(DI::l10n()->t('Group not found.'));
DI::baseUrl()->redirect('contact');
2019-05-01 21:29:04 +02:00
}
2020-07-23 08:11:21 +02:00
if (!Model\Group::remove($a->argv[2])) {
notice(DI::l10n()->t('Unable to remove group.'));
2019-05-01 21:29:04 +02:00
}
}
DI::baseUrl()->redirect('group');
2019-05-01 21:29:04 +02:00
}
// @TODO: Replace with parameter from router
if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
if (DBA::exists('contact', ['id' => $a->argv[2], 'uid' => local_user(), 'self' => false, 'pending' => false, 'blocked' => false])) {
$change = intval($a->argv[2]);
}
}
// @TODO: Replace with parameter from router
if (($a->argc > 1) && intval($a->argv[1])) {
$group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user(), 'deleted' => false]);
if (!DBA::isResult($group)) {
notice(DI::l10n()->t('Group not found.'));
DI::baseUrl()->redirect('contact');
2019-05-01 21:29:04 +02:00
}
$members = Model\Contact\Group::getById($group['id']);
2019-05-01 21:29:04 +02:00
$preselected = [];
if (count($members)) {
foreach ($members as $member) {
$preselected[] = $member['id'];
}
}
if ($change) {
if (in_array($change, $preselected)) {
Model\Group::removeMember($group['id'], $change);
} else {
Model\Group::addMember($group['id'], $change);
}
$members = Model\Contact\Group::getById($group['id']);
2019-05-01 21:29:04 +02:00
$preselected = [];
if (count($members)) {
foreach ($members as $member) {
$preselected[] = $member['id'];
}
}
}
$drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
$drop_txt = Renderer::replaceMacros($drop_tpl, [
'$id' => $group['id'],
'$delete' => DI::l10n()->t('Delete Group'),
2019-05-01 21:29:04 +02:00
'$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
]);
$context = $context + [
'$title' => $group['name'],
'$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
2019-05-01 21:29:04 +02:00
'$gid' => $group['id'],
'$drop' => $drop_txt,
'$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
'$edit_name' => DI::l10n()->t('Edit Group Name'),
2019-05-01 21:29:04 +02:00
'$editable' => 1,
];
}
if (!isset($group)) {
throw new \Friendica\Network\HTTPException\BadRequestException();
2019-05-01 21:29:04 +02:00
}
$groupeditor = [
'label_members' => DI::l10n()->t('Members'),
2019-05-01 21:29:04 +02:00
'members' => [],
'label_contacts' => DI::l10n()->t('All Contacts'),
'group_is_empty' => DI::l10n()->t('Group is empty'),
2019-05-01 21:29:04 +02:00
'contacts' => [],
];
$sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
// Format the data of the group members
foreach ($members as $member) {
if ($member['url']) {
$entry = Contact::getContactTemplateVars($member);
$entry['label'] = 'members';
$entry['photo_menu'] = '';
$entry['change_member'] = [
'title' => DI::l10n()->t("Remove contact from group"),
2019-05-01 21:29:04 +02:00
'gid' => $group['id'],
'cid' => $member['id'],
'sec_token' => $sec_token
];
$groupeditor['members'][] = $entry;
} else {
Model\Group::removeMember($group['id'], $member['id']);
}
}
if ($nogroup) {
2020-08-05 05:36:37 +02:00
$contacts = Model\Contact\Group::listUngrouped(local_user());
2019-05-01 21:29:04 +02:00
} else {
$contacts_stmt = DBA::select('contact', [],
2020-10-07 22:06:15 +02:00
['uid' => local_user(), 'pending' => false, 'blocked' => false, 'failed' => false, 'self' => false],
2019-05-01 21:29:04 +02:00
['order' => ['name']]
);
$contacts = DBA::toArray($contacts_stmt);
$context['$desc'] = DI::l10n()->t('Click on a contact to add or remove.');
2019-05-01 21:29:04 +02:00
}
if (DBA::isResult($contacts)) {
// Format the data of the contacts who aren't in the contact group
foreach ($contacts as $member) {
if (!in_array($member['id'], $preselected)) {
$entry = Contact::getContactTemplateVars($member);
$entry['label'] = 'contacts';
if (!$nogroup)
$entry['photo_menu'] = [];
if (!$nogroup) {
$entry['change_member'] = [
'title' => DI::l10n()->t("Add contact to group"),
2019-05-01 21:29:04 +02:00
'gid' => $group['id'],
'cid' => $member['id'],
'sec_token' => $sec_token
];
}
$groupeditor['contacts'][] = $entry;
}
}
}
$context['$groupeditor'] = $groupeditor;
// If there are to many contacts we could provide an alternative view mode
$total = count($groupeditor['members']) + count($groupeditor['contacts']);
$context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
if ($change) {
$tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
echo Renderer::replaceMacros($tpl, $context);
exit();
}
return Renderer::replaceMacros($tpl, $context);
}
2019-02-23 21:33:55 +01:00
}