diff --git a/mod/contactgroup.php b/mod/contactgroup.php deleted file mode 100644 index 07997cd11..000000000 --- a/mod/contactgroup.php +++ /dev/null @@ -1,53 +0,0 @@ -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(); -} diff --git a/mod/group.php b/mod/group.php deleted file mode 100644 index a6f649bf3..000000000 --- a/mod/group.php +++ /dev/null @@ -1,313 +0,0 @@ -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); - -} diff --git a/src/Core/System.php b/src/Core/System.php index 69d15a176..45a88fe09 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -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. * diff --git a/src/Model/Contact.php b/src/Model/Contact.php index d38d1cc10..bf7d806db 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -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]); + } } diff --git a/src/Model/Group.php b/src/Model/Group.php index 0af10995b..feff4661a 100644 --- a/src/Model/Group.php +++ b/src/Model/Group.php @@ -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 * diff --git a/src/Module/Group.php b/src/Module/Group.php new file mode 100644 index 000000000..747ec1e56 --- /dev/null +++ b/src/Module/Group.php @@ -0,0 +1,350 @@ +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); + } +} \ No newline at end of file diff --git a/view/js/main.js b/view/js/main.js index 3370d7ccd..cd09de014 100644 --- a/view/js/main.js +++ b/view/js/main.js @@ -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) { diff --git a/view/templates/group_side.tpl b/view/templates/group_side.tpl index 5796bb735..31b9287ba 100644 --- a/view/templates/group_side.tpl +++ b/view/templates/group_side.tpl @@ -9,7 +9,7 @@ {{if $group.cid}} {{/if}} diff --git a/view/theme/frio/templates/group_side.tpl b/view/theme/frio/templates/group_side.tpl index 947ae974e..75983b36e 100644 --- a/view/theme/frio/templates/group_side.tpl +++ b/view/theme/frio/templates/group_side.tpl @@ -28,7 +28,7 @@ diff --git a/view/theme/quattro/templates/group_side.tpl b/view/theme/quattro/templates/group_side.tpl index 620d2de75..18a6105a6 100644 --- a/view/theme/quattro/templates/group_side.tpl +++ b/view/theme/quattro/templates/group_side.tpl @@ -17,7 +17,7 @@ {{if $group.cid}} {{/if}}