Merge pull request #6732 from MrPetovan/bug/1777-fix-blocked-contact-group

Allow to remove blocked contact from groups
This commit is contained in:
Michael Vogel 2019-02-24 14:30:36 +01:00 committed by GitHub
commit df5ea1fab0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 406 additions and 372 deletions

View file

@ -1,53 +0,0 @@
<?php
use Friendica\App;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Model\Group;
function contactgroup_content(App $a)
{
if (!local_user()) {
exit();
}
$change = null;
if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
$r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
intval($a->argv[2]),
intval(local_user())
);
if (DBA::isResult($r)) {
$change = intval($a->argv[2]);
}
}
if (($a->argc > 1) && (intval($a->argv[1]))) {
$r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
intval($a->argv[1]),
intval(local_user())
);
if (!DBA::isResult($r)) {
exit();
}
$group = $r[0];
$members = Contact::getByGroupId($group['id']);
$preselected = [];
if (count($members)) {
foreach ($members as $member) {
$preselected[] = $member['id'];
}
}
if (!empty($change)) {
if (in_array($change, $preselected)) {
Group::removeMember($group['id'], $change);
} else {
Group::addMember($group['id'], $change);
}
}
}
exit();
}

View file

@ -1,313 +0,0 @@
<?php
/**
* @file mod/group.php
* @brief The group module (create and rename contact groups, add and
* remove contacts to the contact groups
*/
use Friendica\App;
use Friendica\BaseModule;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\PConfig;
use Friendica\Core\Renderer;
use Friendica\Database\DBA;
use Friendica\Model;
use Friendica\Module;
use Friendica\Util\Strings;
function group_init(App $a) {
if (local_user()) {
$a->page['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone'));
}
}
function group_post(App $a) {
if (!local_user()) {
notice(L10n::t('Permission denied.') . EOL);
return;
}
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) {
info(L10n::t('Group created.') . EOL);
$r = Model\Group::getIdByName(local_user(), $name);
if ($r) {
$a->internalRedirect('group/' . $r);
}
} else {
notice(L10n::t('Could not create group.') . EOL);
}
$a->internalRedirect('group');
return; // NOTREACHED
}
if (($a->argc == 2) && intval($a->argv[1])) {
BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
$r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
intval($a->argv[1]),
intval(local_user())
);
if (!DBA::isResult($r)) {
notice(L10n::t('Group not found.') . EOL);
$a->internalRedirect('contact');
return; // NOTREACHED
}
$group = $r[0];
$groupname = Strings::escapeTags(trim($_POST['groupname']));
if (strlen($groupname) && ($groupname != $group['name'])) {
$r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d",
DBA::escape($groupname),
intval(local_user()),
intval($group['id'])
);
if ($r) {
info(L10n::t('Group name changed.') . EOL);
}
}
$a->page['aside'] = Model\Group::sidebarWidget();
}
return;
}
function group_content(App $a) {
$change = false;
if (!local_user()) {
notice(L10n::t('Permission denied') . EOL);
return;
}
// With no group number provided we jump to the unassigned contacts as a starting point
if ($a->argc == 1) {
$a->internalRedirect('group/none');
}
// Switch to text mode interface if we have more than 'n' contacts or group members
$switchtotext = PConfig::get(local_user(), 'system', 'groupedit_image_limit');
if (is_null($switchtotext)) {
$switchtotext = Config::get('system', 'groupedit_image_limit', 400);
}
$tpl = Renderer::getMarkupTemplate('group_edit.tpl');
$context = [
'$submit' => L10n::t('Save Group'),
'$submit_filter' => L10n::t('Filter'),
];
if (($a->argc == 2) && ($a->argv[1] === 'new')) {
return Renderer::replaceMacros($tpl, $context + [
'$title' => L10n::t('Create a group of contacts/friends.'),
'$gname' => ['groupname', L10n::t('Group Name: '), '', ''],
'$gid' => 'new',
'$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
]);
}
$nogroup = false;
if (($a->argc == 2) && ($a->argv[1] === 'none')) {
$id = -1;
$nogroup = true;
$group = [
'id' => $id,
'name' => L10n::t('Contacts not in any group'),
];
$members = [];
$preselected = [];
$context = $context + [
'$title' => $group['name'],
'$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
'$gid' => $id,
'$editable' => 0,
];
}
if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
if (intval($a->argv[2])) {
$r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
intval($a->argv[2]),
intval(local_user())
);
$result = null;
if (DBA::isResult($r)) {
$result = Model\Group::removeByName(local_user(), $r[0]['name']);
}
if ($result) {
info(L10n::t('Group removed.') . EOL);
} else {
notice(L10n::t('Unable to remove group.') . EOL);
}
}
$a->internalRedirect('group');
// NOTREACHED
}
if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
$r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
intval($a->argv[2]),
intval(local_user())
);
if (DBA::isResult($r)) {
$change = intval($a->argv[2]);
}
}
if (($a->argc > 1) && intval($a->argv[1])) {
$r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
intval($a->argv[1]),
intval(local_user())
);
if (!DBA::isResult($r)) {
notice(L10n::t('Group not found.') . EOL);
$a->internalRedirect('contact');
}
$group = $r[0];
$members = Model\Contact::getByGroupId($group['id']);
$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::getByGroupId($group['id']);
$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' => L10n::t('Delete Group'),
'$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
]);
$context = $context + [
'$title' => $group['name'],
'$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
'$gid' => $group['id'],
'$drop' => $drop_txt,
'$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
'$edit_name' => L10n::t('Edit Group Name'),
'$editable' => 1,
];
}
if (!isset($group)) {
return;
}
$groupeditor = [
'label_members' => L10n::t('Members'),
'members' => [],
'label_contacts' => L10n::t('All Contacts'),
'group_is_empty' => L10n::t('Group is empty'),
'contacts' => [],
];
$sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
// Format the data of the group members
foreach ($members as $member) {
if ($member['url']) {
$entry = Module\Contact::getContactTemplateVars($member);
$entry['label'] = 'members';
$entry['photo_menu'] = '';
$entry['change_member'] = [
'title' => L10n::t("Remove contact from group"),
'gid' => $group['id'],
'cid' => $member['id'],
'sec_token' => $sec_token
];
$groupeditor['members'][] = $entry;
} else {
Model\Group::removeMember($group['id'], $member['id']);
}
}
if ($nogroup) {
$r = Model\Contact::getUngroupedList(local_user());
} else {
$r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
intval(local_user())
);
$context['$desc'] = L10n::t('Click on a contact to add or remove.');
}
if (DBA::isResult($r)) {
// Format the data of the contacts who aren't in the contact group
foreach ($r as $member) {
if (!in_array($member['id'], $preselected)) {
$entry = Module\Contact::getContactTemplateVars($member);
$entry['label'] = 'contacts';
if (!$nogroup)
$entry['photo_menu'] = [];
if (!$nogroup) {
$entry['change_member'] = [
'title' => L10n::t("Add contact to group"),
'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);
}

View file

@ -176,6 +176,12 @@ class System extends BaseObject
exit();
}
public static function jsonError($httpCode, $data, $content_type = 'application/json')
{
header($_SERVER["SERVER_PROTOCOL"] . ' ' . $httpCode);
self::jsonExit($data, $content_type);
}
/**
* @brief Encodes content to json.
*

View file

@ -232,6 +232,11 @@ class Contact extends BaseObject
}
DBA::update('user-contact', ['blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
if ($blocked) {
// Blocked contact can't be in any group
self::removeFromGroups($cid);
}
}
/**
@ -2220,4 +2225,9 @@ class Contact extends BaseObject
return $redirect;
}
public static function removeFromGroups($contact_id)
{
return DBA::delete('group_member', ['contact-id' => $contact_id]);
}
}

View file

@ -16,6 +16,26 @@ use Friendica\Database\DBA;
*/
class Group extends BaseObject
{
/**
*
*
* @param int $group_id
* @return bool
* @throws \Exception
*/
public static function exists($group_id, $uid = null)
{
$condition = ['id' => $group_id, 'deleted' => false];
if (isset($uid)) {
$condition = [
'uid' => $uid
];
}
return DBA::exists('group', $condition);
}
/**
* @brief Create a new contact group
*

350
src/Module/Group.php Normal file
View file

@ -0,0 +1,350 @@
<?php
/**
* @file src/Module/Group.php
*/
namespace Friendica\Module;
use Friendica\BaseModule;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\PConfig;
use Friendica\Core\Renderer;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\Model;
use Friendica\Util\Strings;
require_once 'boot.php';
class Group extends BaseModule
{
public static function post()
{
$a = self::getApp();
if ($a->isAjax()) {
self::ajaxPost();
}
if (!local_user()) {
notice(L10n::t('Permission denied.'));
$a->internalRedirect();
}
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) {
info(L10n::t('Group created.'));
$r = Model\Group::getIdByName(local_user(), $name);
if ($r) {
$a->internalRedirect('group/' . $r);
}
} else {
notice(L10n::t('Could not create group.'));
}
$a->internalRedirect('group');
}
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(L10n::t('Group not found.'));
$a->internalRedirect('contact');
}
$groupname = Strings::escapeTags(trim($_POST['groupname']));
if (strlen($groupname) && ($groupname != $group['name'])) {
if (Model\Group::update($group['id'], $groupname)) {
info(L10n::t('Group name changed.'));
}
}
}
}
public static function ajaxPost()
{
try {
$a = self::getApp();
if (!local_user()) {
throw new \Exception(L10n::t('Permission denied.'), 403);
}
// POST /group/123/add/123
// POST /group/123/remove/123
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(L10n::t('Unknown group.'), 404);
}
$contact = DBA::selectFirst('contact', ['pending', 'blocked', 'deleted'], ['id' => $contact_id, 'uid' => local_user()]);
if (!DBA::isResult($contact)) {
throw new \Exception(L10n::t('Contact not found.'), 404);
}
if ($contact['pending']) {
throw new \Exception(L10n::t('Contact is unavailable.'), 400);
}
if ($contact['deleted']) {
throw new \Exception(L10n::t('Contact is deleted.'), 410);
}
switch($command) {
case 'add':
if ($contact['blocked']) {
throw new \Exception(L10n::t('Contact is blocked, unable to add it to a group.'), 400);
}
if (!Model\Group::addMember($group_id, $contact_id)) {
throw new \Exception(L10n::t('Unable to add the contact to the group.'), 500);
}
$message = L10n::t('Contact successfully added to group.');
break;
case 'remove':
if (!Model\Group::removeMember($group_id, $contact_id)) {
throw new \Exception(L10n::t('Unable to remove the contact from the group.'), 500);
}
$message = L10n::t('Contact successfully removed from group.');
break;
default:
throw new \Exception(L10n::t('Unknown group command.'), 400);
}
} else {
throw new \Exception(L10n::t('Bad request.'), 400);
}
notice($message);
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()
{
$change = false;
if (!local_user()) {
System::httpExit(403);
}
$a = self::getApp();
$a->page['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone'));
// With no group number provided we jump to the unassigned contacts as a starting point
if ($a->argc == 1) {
$a->internalRedirect('group/none');
}
// Switch to text mode interface if we have more than 'n' contacts or group members
$switchtotext = PConfig::get(local_user(), 'system', 'groupedit_image_limit');
if (is_null($switchtotext)) {
$switchtotext = Config::get('system', 'groupedit_image_limit', 200);
}
$tpl = Renderer::getMarkupTemplate('group_edit.tpl');
$context = [
'$submit' => L10n::t('Save Group'),
'$submit_filter' => L10n::t('Filter'),
];
if (($a->argc == 2) && ($a->argv[1] === 'new')) {
return Renderer::replaceMacros($tpl, $context + [
'$title' => L10n::t('Create a group of contacts/friends.'),
'$gname' => ['groupname', L10n::t('Group Name: '), '', ''],
'$gid' => 'new',
'$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
]);
}
$nogroup = false;
if (($a->argc == 2) && ($a->argv[1] === 'none')) {
$id = -1;
$nogroup = true;
$group = [
'id' => $id,
'name' => L10n::t('Contacts not in any group'),
];
$members = [];
$preselected = [];
$context = $context + [
'$title' => $group['name'],
'$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
'$gid' => $id,
'$editable' => 0,
];
}
if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
if (intval($a->argv[2])) {
if (!Model\Group::exists($a->argv[2], local_user())) {
notice(L10n::t('Group not found.'));
$a->internalRedirect('contact');
}
if (Model\Group::remove($a->argv[2])) {
info(L10n::t('Group removed.'));
} else {
notice(L10n::t('Unable to remove group.'));
}
}
$a->internalRedirect('group');
}
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]);
}
}
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(L10n::t('Group not found.'));
$a->internalRedirect('contact');
}
$members = Model\Contact::getByGroupId($group['id']);
$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::getByGroupId($group['id']);
$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' => L10n::t('Delete Group'),
'$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
]);
$context = $context + [
'$title' => $group['name'],
'$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
'$gid' => $group['id'],
'$drop' => $drop_txt,
'$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
'$edit_name' => L10n::t('Edit Group Name'),
'$editable' => 1,
];
}
if (!isset($group)) {
System::httpExit(400);
}
$groupeditor = [
'label_members' => L10n::t('Members'),
'members' => [],
'label_contacts' => L10n::t('All Contacts'),
'group_is_empty' => L10n::t('Group is empty'),
'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' => L10n::t("Remove contact from group"),
'gid' => $group['id'],
'cid' => $member['id'],
'sec_token' => $sec_token
];
$groupeditor['members'][] = $entry;
} else {
Model\Group::removeMember($group['id'], $member['id']);
}
}
if ($nogroup) {
$contacts = Model\Contact::getUngroupedList(local_user());
} else {
$contacts_stmt = DBA::select('contact', [],
['uid' => local_user(), 'pending' => false, 'blocked' => false, 'self' => false],
['order' => ['name']]
);
$contacts = DBA::toArray($contacts_stmt);
$context['$desc'] = L10n::t('Click on a contact to add or remove.');
}
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' => L10n::t("Add contact to group"),
'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);
}
}

View file

@ -793,11 +793,25 @@ function profChangeMember(gid,cid) {
});
}
function contactgroupChangeMember(gid,cid) {
function contactgroupChangeMember(checkbox, gid, cid) {
let url;
// checkbox.checked is the checkbox state after the click
if (checkbox.checked) {
url = 'group/' + gid + '/add/' + cid;
} else {
url = 'group/' + gid + '/remove/' + cid;
}
$('body').css('cursor', 'wait');
$.get('contactgroup/' + gid + '/' + cid, function(data) {
$('body').css('cursor', 'auto');
$.post(url)
.error(function () {
// Restores previous state in case of error
checkbox.checked = !checkbox.checked;
})
.always(function() {
$('body').css('cursor', 'auto');
});
return true;
}
function checkboxhighlight(box) {

View file

@ -9,7 +9,7 @@
{{if $group.cid}}
<input type="checkbox"
class="{{if $group.selected}}ticked{{else}}unticked {{/if}} action"
onclick="contactgroupChangeMember('{{$group.id}}','{{$group.cid}}');return true;"
onclick="return contactgroupChangeMember(this, '{{$group.id}}','{{$group.cid}}');"
{{if $group.ismember}}checked="checked"{{/if}}
/>
{{/if}}

View file

@ -28,7 +28,7 @@
<input type="checkbox"
id="sidebar-group-checkbox-{{$group.id}}"
class="{{if $group.selected}}ticked{{else}}unticked {{/if}} action"
onclick="contactgroupChangeMember('{{$group.id}}','{{$group.cid}}');return true;"
onclick="return contactgroupChangeMember(this, '{{$group.id}}','{{$group.cid}}');"
{{if $group.ismember}}checked="checked"{{/if}}
aria-checked="{{if $group.ismember}}true{{else}}false{{/if}}"
/>

View file

@ -17,7 +17,7 @@
{{if $group.cid}}
<input type="checkbox"
class="{{if $group.selected}}ticked{{else}}unticked {{/if}} action"
onclick="contactgroupChangeMember('{{$group.id}}','{{$group.cid}}');return true;"
onclick="return contactgroupChangeMember(this, '{{$group.id}}','{{$group.cid}}');"
{{if $group.ismember}}checked="checked"{{/if}}
/>
{{/if}}