Mass tag: fixed concurrent tag toggling

This commit is contained in:
Marcin Kurczewski 2013-11-25 11:59:59 +01:00
parent 9819416f35
commit 505fe1bac3
3 changed files with 12 additions and 7 deletions

View file

@ -15,14 +15,18 @@ $(function()
return; return;
aDom.addClass('inactive'); aDom.addClass('inactive');
var enable = !aDom.parents('.post').hasClass('tagged');
var url = $(this).attr('href') + '?json'; var url = $(this).attr('href') + '?json';
url = url.replace('_enable_', enable ? '1' : '0');
$.get(url, {submit: 1}).always(function(data) $.get(url, {submit: 1}).always(function(data)
{ {
if (data['success']) if (data['success'])
{ {
aDom.removeClass('inactive'); aDom.removeClass('inactive');
aDom.parents('.post').toggleClass('tagged'); aDom.parents('.post').removeClass('tagged');
aDom.text(aDom.parents('.post').hasClass('tagged') if (enable)
aDom.parents('.post').addClass('tagged');
aDom.text(enable
? aDom.attr('data-text-tagged') ? aDom.attr('data-text-tagged')
: aDom.attr('data-text-untagged')); : aDom.attr('data-text-untagged'));
} }

View file

@ -120,10 +120,11 @@ class PostController
/** /**
* @route /post/{id}/toggle-tag/{tag} * @route /post/{id}/toggle-tag/{tag}/{enable}
* @validate tag [^\/]* * @validate tag [^\/]*
* @validate enable 0|1
*/ */
public function toggleTagAction($id, $tag) public function toggleTagAction($id, $tag, $enable)
{ {
$post = Model_Post::locate($id); $post = Model_Post::locate($id);
$this->context->transport->post = $post; $this->context->transport->post = $post;
@ -137,12 +138,12 @@ class PostController
PrivilegesHelper::confirmWithException(Privilege::MassTag); PrivilegesHelper::confirmWithException(Privilege::MassTag);
$tags = array_map(function($x) { return $x->name; }, $post->sharedTag); $tags = array_map(function($x) { return $x->name; }, $post->sharedTag);
if (in_array($tag, $tags)) if (!$enable and in_array($tag, $tags))
{ {
$tags = array_diff($tags, [$tag]); $tags = array_diff($tags, [$tag]);
LogHelper::log('{user} untagged {post} with {tag}', ['post' => TextHelper::reprPost($post), 'tag' => TextHelper::reprTag($tag)]); LogHelper::log('{user} untagged {post} with {tag}', ['post' => TextHelper::reprPost($post), 'tag' => TextHelper::reprTag($tag)]);
} }
else elseif ($enable)
{ {
$tags += [$tag]; $tags += [$tag];
LogHelper::log('{user} tagged {post} with {tag}', ['post' => TextHelper::reprPost($post), 'tag' => TextHelper::reprTag($tag)]); LogHelper::log('{user} tagged {post} with {tag}', ['post' => TextHelper::reprPost($post), 'tag' => TextHelper::reprTag($tag)]);

View file

@ -9,7 +9,7 @@
<div class="<?php echo implode(' ', $classNames) ?>"> <div class="<?php echo implode(' ', $classNames) ?>">
<?php if ($masstag): ?> <?php if ($masstag): ?>
<a class="toggle-tag" href="<?php echo \Chibi\UrlHelper::route('post', 'toggle-tag', ['id' => $this->context->post->id, 'tag' => $this->context->additionalInfo]) ?>" data-text-tagged="Tagged" data-text-untagged="Untagged"> <a class="toggle-tag" href="<?php echo \Chibi\UrlHelper::route('post', 'toggle-tag', ['id' => $this->context->post->id, 'tag' => $this->context->additionalInfo, 'enable' => '_enable_']) ?>" data-text-tagged="Tagged" data-text-untagged="Untagged">
<?php echo in_array('tagged', $classNames) ? 'Tagged' : 'Untagged' ?> <?php echo in_array('tagged', $classNames) ? 'Tagged' : 'Untagged' ?>
</a> </a>
<?php endif ?> <?php endif ?>