friendica/src/Module/Circle.php

370 lines
12 KiB
PHP
Raw Normal View History

2019-05-01 19:29:04 +00:00
<?php
/**
2023-01-01 14:36:24 +00:00
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @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 19:29:04 +00:00
*/
namespace Friendica\Module;
use Friendica\BaseModule;
use Friendica\Content\Widget;
2019-05-01 19:29:04 +00:00
use Friendica\Core\Renderer;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
2019-05-01 19:29:04 +00:00
use Friendica\Model;
class Circle extends BaseModule
2019-05-01 19:29:04 +00:00
{
protected function post(array $request = [])
2019-05-01 19:29:04 +00:00
{
if (DI::mode()->isAjax()) {
$this->ajaxPost();
2019-05-01 19:29:04 +00:00
}
if (!DI::userSession()->getLocalUserId()) {
2022-10-17 18:55:22 +00:00
DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
DI::baseUrl()->redirect();
2019-05-01 19:29:04 +00:00
}
// @TODO: Replace with parameter from router
2021-07-25 15:23:37 +00:00
if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'new')) {
BaseModule::checkFormSecurityTokenRedirectOnError('/circle/new', 'circle_edit');
2019-05-01 19:29:04 +00:00
$name = trim($request['circle_name']);
$r = Model\Circle::create(DI::userSession()->getLocalUserId(), $name);
2019-05-01 19:29:04 +00:00
if ($r) {
$r = Model\Circle::getIdByName(DI::userSession()->getLocalUserId(), $name);
2019-05-01 19:29:04 +00:00
if ($r) {
DI::baseUrl()->redirect('circle/' . $r);
2019-05-01 19:29:04 +00:00
}
} else {
DI::sysmsg()->addNotice(DI::l10n()->t('Could not create circle.'));
2019-05-01 19:29:04 +00:00
}
DI::baseUrl()->redirect('circle');
2019-05-01 19:29:04 +00:00
}
// @TODO: Replace with parameter from router
2021-07-25 15:23:37 +00:00
if ((DI::args()->getArgc() == 2) && intval(DI::args()->getArgv()[1])) {
BaseModule::checkFormSecurityTokenRedirectOnError('/circle', 'circle_edit');
2019-05-01 19:29:04 +00:00
$circle = DBA::selectFirst('group', ['id', 'name'], ['id' => DI::args()->getArgv()[1], 'uid' => DI::userSession()->getLocalUserId()]);
if (!DBA::isResult($circle)) {
DI::sysmsg()->addNotice(DI::l10n()->t('Circle not found.'));
DI::baseUrl()->redirect('contact');
2019-05-01 19:29:04 +00:00
}
$circlename = trim($_POST['circle_name']);
if (strlen($circlename) && ($circlename != $circle['name'])) {
if (!Model\Circle::update($circle['id'], $circlename)) {
DI::sysmsg()->addNotice(DI::l10n()->t('Circle name was not changed.'));
2019-05-01 19:29:04 +00:00
}
}
}
}
public function ajaxPost()
2019-05-01 19:29:04 +00:00
{
try {
if (!DI::userSession()->getLocalUserId()) {
throw new \Exception(DI::l10n()->t('Permission denied.'), 403);
2019-05-01 19:29:04 +00:00
}
if (isset($this->parameters['command'])) {
$circle_id = $this->parameters['circle'];
$contact_id = $this->parameters['contact'];
2019-05-01 19:29:04 +00:00
if (!Model\Circle::exists($circle_id, DI::userSession()->getLocalUserId())) {
throw new \Exception(DI::l10n()->t('Unknown circle.'), 404);
2019-05-01 19:29:04 +00:00
}
// @TODO Backward compatibility with user contacts, remove by version 2022.03
$cdata = Model\Contact::getPublicAndUserContactID($contact_id, DI::userSession()->getLocalUserId());
if (empty($cdata['public'])) {
throw new \Exception(DI::l10n()->t('Contact not found.'), 404);
}
if (empty($cdata['user'])) {
throw new \Exception(DI::l10n()->t('Invalid contact.'), 404);
}
$contact = Model\Contact::getById($cdata['user'], ['deleted']);
2019-05-01 19:29:04 +00:00
if (!DBA::isResult($contact)) {
throw new \Exception(DI::l10n()->t('Contact not found.'), 404);
2019-05-01 19:29:04 +00:00
}
if ($contact['deleted']) {
throw new \Exception(DI::l10n()->t('Contact is deleted.'), 410);
2019-05-01 19:29:04 +00:00
}
switch($this->parameters['command']) {
2019-05-01 19:29:04 +00:00
case 'add':
if (!Model\Circle::addMember($circle_id, $cdata['user'])) {
throw new \Exception(DI::l10n()->t('Unable to add the contact to the circle.'), 500);
2019-05-01 19:29:04 +00:00
}
$message = DI::l10n()->t('Contact successfully added to circle.');
2019-05-01 19:29:04 +00:00
break;
case 'remove':
if (!Model\Circle::removeMember($circle_id, $cdata['user'])) {
throw new \Exception(DI::l10n()->t('Unable to remove the contact from the circle.'), 500);
2019-05-01 19:29:04 +00:00
}
$message = DI::l10n()->t('Contact successfully removed from circle.');
2019-05-01 19:29:04 +00:00
break;
}
} else {
throw new \Exception(DI::l10n()->t('Bad request.'), 400);
2019-05-01 19:29:04 +00:00
}
2022-10-17 18:55:22 +00:00
DI::sysmsg()->addInfo($message);
$this->jsonExit(['status' => 'OK', 'message' => $message]);
2019-05-01 19:29:04 +00:00
} catch (\Exception $e) {
2022-10-17 18:55:22 +00:00
DI::sysmsg()->addNotice($e->getMessage());
$this->jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
2019-05-01 19:29:04 +00:00
}
}
protected function content(array $request = []): string
2019-05-01 19:29:04 +00:00
{
$change = false;
if (!DI::userSession()->getLocalUserId()) {
throw new \Friendica\Network\HTTPException\ForbiddenException();
2019-05-01 19:29:04 +00:00
}
DI::page()['aside'] = Model\Circle::sidebarWidget('contact', 'circle', 'extended', ((DI::args()->getArgc() > 1) ? DI::args()->getArgv()[1] : 'everyone'));
2019-05-01 19:29:04 +00:00
// With no circle number provided we jump to the unassigned contacts as a starting point
2019-05-01 19:29:04 +00:00
// @TODO: Replace with parameter from router
2021-07-25 15:23:37 +00:00
if (DI::args()->getArgc() == 1) {
DI::baseUrl()->redirect('circle/none');
2019-05-01 19:29:04 +00:00
}
// Switch to text mode interface if we have more than 'n' contacts or circle members
$switchtotext = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'circle_edit_image_limit') ??
DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'groupedit_image_limit');
2019-05-01 19:29:04 +00:00
if (is_null($switchtotext)) {
$switchtotext = DI::config()->get('system', 'groupedit_image_limit') ??
DI::config()->get('system', 'circle_edit_image_limit');
2019-05-01 19:29:04 +00:00
}
$tpl = Renderer::getMarkupTemplate('circle_edit.tpl');
2019-05-01 19:29:04 +00:00
$context = [
'$submit' => DI::l10n()->t('Save Circle'),
'$submit_filter' => DI::l10n()->t('Filter'),
2019-05-01 19:29:04 +00:00
];
// @TODO: Replace with parameter from router
2021-07-25 15:23:37 +00:00
if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'new')) {
2019-05-01 19:29:04 +00:00
return Renderer::replaceMacros($tpl, $context + [
'$title' => DI::l10n()->t('Create a circle of contacts/friends.'),
'$gname' => ['circle_name', DI::l10n()->t('Circle Name: '), '', ''],
2019-05-01 19:29:04 +00:00
'$gid' => 'new',
'$form_security_token' => BaseModule::getFormSecurityToken('circle_edit'),
2019-05-01 19:29:04 +00:00
]);
}
$nocircle = false;
2019-05-01 19:29:04 +00:00
2019-05-03 06:50:17 +00:00
// @TODO: Replace with parameter from router
2021-07-25 15:23:37 +00:00
if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'none') ||
(DI::args()->getArgc() == 1) && (DI::args()->getArgv()[0] === 'nocircle')) {
2019-05-01 19:29:04 +00:00
$id = -1;
$nocircle = true;
$circle = [
2019-05-01 19:29:04 +00:00
'id' => $id,
'name' => DI::l10n()->t('Contacts not in any circle'),
2019-05-01 19:29:04 +00:00
];
$members = [];
$preselected = [];
$context = $context + [
'$title' => $circle['name'],
'$gname' => ['circle_name', DI::l10n()->t('Circle Name: '), $circle['name'], ''],
2019-05-01 19:29:04 +00:00
'$gid' => $id,
'$editable' => 0,
];
}
// @TODO: Replace with parameter from router
2021-07-25 15:23:37 +00:00
if ((DI::args()->getArgc() == 3) && (DI::args()->getArgv()[1] === 'drop')) {
BaseModule::checkFormSecurityTokenRedirectOnError('/circle', 'circle_drop', 't');
2019-05-01 19:29:04 +00:00
// @TODO: Replace with parameter from router
2021-07-25 15:23:37 +00:00
if (intval(DI::args()->getArgv()[2])) {
if (!Model\Circle::exists(DI::args()->getArgv()[2], DI::userSession()->getLocalUserId())) {
DI::sysmsg()->addNotice(DI::l10n()->t('Circle not found.'));
DI::baseUrl()->redirect('contact');
2019-05-01 19:29:04 +00:00
}
if (!Model\Circle::remove(DI::args()->getArgv()[2])) {
DI::sysmsg()->addNotice(DI::l10n()->t('Unable to remove circle.'));
2019-05-01 19:29:04 +00:00
}
}
DI::baseUrl()->redirect('circle');
2019-05-01 19:29:04 +00:00
}
// @TODO: Replace with parameter from router
2021-07-25 15:23:37 +00:00
if ((DI::args()->getArgc() > 2) && intval(DI::args()->getArgv()[1]) && intval(DI::args()->getArgv()[2])) {
BaseModule::checkFormSecurityTokenForbiddenOnError('circle_member_change', 't');
2019-05-01 19:29:04 +00:00
if (DBA::exists('contact', ['id' => DI::args()->getArgv()[2], 'uid' => DI::userSession()->getLocalUserId(), 'self' => false, 'pending' => false, 'blocked' => false])) {
2021-07-25 15:23:37 +00:00
$change = intval(DI::args()->getArgv()[2]);
2019-05-01 19:29:04 +00:00
}
}
// @TODO: Replace with parameter from router
2021-07-25 15:23:37 +00:00
if ((DI::args()->getArgc() > 1) && intval(DI::args()->getArgv()[1])) {
$circle = DBA::selectFirst('group', ['id', 'name'], ['id' => DI::args()->getArgv()[1], 'uid' => DI::userSession()->getLocalUserId(), 'deleted' => false]);
if (!DBA::isResult($circle)) {
DI::sysmsg()->addNotice(DI::l10n()->t('Circle not found.'));
DI::baseUrl()->redirect('contact');
2019-05-01 19:29:04 +00:00
}
$members = Model\Contact\Circle::getById($circle['id']);
2019-05-01 19:29:04 +00:00
$preselected = [];
if (count($members)) {
foreach ($members as $member) {
$preselected[] = $member['id'];
}
}
if ($change) {
if (in_array($change, $preselected)) {
Model\Circle::removeMember($circle['id'], $change);
2019-05-01 19:29:04 +00:00
} else {
Model\Circle::addMember($circle['id'], $change);
2019-05-01 19:29:04 +00:00
}
$members = Model\Contact\Circle::getById($circle['id']);
2019-05-01 19:29:04 +00:00
$preselected = [];
if (count($members)) {
foreach ($members as $member) {
$preselected[] = $member['id'];
}
}
}
$drop_tpl = Renderer::getMarkupTemplate('circle_drop.tpl');
2019-05-01 19:29:04 +00:00
$drop_txt = Renderer::replaceMacros($drop_tpl, [
'$id' => $circle['id'],
'$delete' => DI::l10n()->t('Delete Circle'),
'$form_security_token' => BaseModule::getFormSecurityToken('circle_drop'),
2019-05-01 19:29:04 +00:00
]);
$context = $context + [
'$title' => $circle['name'],
'$gname' => ['circle_name', DI::l10n()->t('Circle Name: '), $circle['name'], ''],
'$gid' => $circle['id'],
2019-05-01 19:29:04 +00:00
'$drop' => $drop_txt,
'$form_security_token' => BaseModule::getFormSecurityToken('circle_edit'),
'$edit_name' => DI::l10n()->t('Edit Circle Name'),
2019-05-01 19:29:04 +00:00
'$editable' => 1,
];
}
if (!isset($circle)) {
throw new \Friendica\Network\HTTPException\BadRequestException();
2019-05-01 19:29:04 +00:00
}
$circle_editor = [
'label_members' => DI::l10n()->t('Members'),
2019-05-01 19:29:04 +00:00
'members' => [],
'label_contacts' => DI::l10n()->t('All Contacts'),
'circle_is_empty' => DI::l10n()->t('Circle is empty'),
2019-05-01 19:29:04 +00:00
'contacts' => [],
];
$sec_token = addslashes(BaseModule::getFormSecurityToken('circle_member_change'));
2019-05-01 19:29:04 +00:00
// Format the data of the circle members
2019-05-01 19:29:04 +00:00
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 circle'),
'gid' => $circle['id'],
2019-05-01 19:29:04 +00:00
'cid' => $member['id'],
'sec_token' => $sec_token
];
$circle_editor['members'][] = $entry;
2019-05-01 19:29:04 +00:00
} else {
Model\Circle::removeMember($circle['id'], $member['id']);
2019-05-01 19:29:04 +00:00
}
}
if ($nocircle) {
$contacts = Model\Contact\Circle::listUncircled(DI::userSession()->getLocalUserId());
2019-05-01 19:29:04 +00:00
} else {
$networks = Widget::unavailableNetworks();
$query = "`uid` = ? AND NOT `self` AND NOT `deleted` AND NOT `blocked` AND NOT `pending` AND NOT `failed`
AND `rel` IN (?, ?, ?)
AND NOT `network` IN (" . substr(str_repeat('?, ', count($networks)), 0, -2) . ")";
$condition = array_merge([$query], [DI::userSession()->getLocalUserId(), Model\Contact::FOLLOWER, Model\Contact::FRIEND, Model\Contact::SHARING], $networks);
$contacts_stmt = DBA::select('contact', [], $condition, ['order' => ['name']]);
2019-05-01 19:29:04 +00:00
$contacts = DBA::toArray($contacts_stmt);
$context['$desc'] = DI::l10n()->t('Click on a contact to add or remove.');
2019-05-01 19:29:04 +00:00
}
if (DBA::isResult($contacts)) {
// Format the data of the contacts who aren't in the contact circle
2019-05-01 19:29:04 +00:00
foreach ($contacts as $member) {
if (!in_array($member['id'], $preselected)) {
$entry = Contact::getContactTemplateVars($member);
$entry['label'] = 'contacts';
if (!$nocircle)
2019-05-01 19:29:04 +00:00
$entry['photo_menu'] = [];
if (!$nocircle) {
2019-05-01 19:29:04 +00:00
$entry['change_member'] = [
'title' => DI::l10n()->t('Add contact to circle'),
'gid' => $circle['id'],
2019-05-01 19:29:04 +00:00
'cid' => $member['id'],
'sec_token' => $sec_token
];
}
$circle_editor['contacts'][] = $entry;
2019-05-01 19:29:04 +00:00
}
}
}
$context['$circle_editor'] = $circle_editor;
2019-05-01 19:29:04 +00:00
// If there are to many contacts we could provide an alternative view mode
$total = count($circle_editor['members']) + count($circle_editor['contacts']);
2019-05-01 19:29:04 +00:00
$context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
if ($change) {
$tpl = Renderer::getMarkupTemplate('circle_editor.tpl');
2019-05-01 19:29:04 +00:00
echo Renderer::replaceMacros($tpl, $context);
2022-05-18 02:13:54 +00:00
System::exit();
2019-05-01 19:29:04 +00:00
}
return Renderer::replaceMacros($tpl, $context);
}
}