Optimalization: sidebar options structure

- options rendering moved to separate file
- simplified template code
- removed redundant JS
This commit is contained in:
Marcin Kurczewski 2013-11-30 00:07:23 +01:00
parent 5e58488f3e
commit d570bc1790
4 changed files with 255 additions and 163 deletions

View file

@ -233,4 +233,20 @@ class TextHelper
$path = self::cleanPath($path); $path = self::cleanPath($path);
return $path; return $path;
} }
public static function openHtmlTag($tagName, array $attributes)
{
$html = '<' . $tagName;
foreach ($attributes as $key => $value)
$html .= ' ' . $key . '="' . $value . '"';
$html .= '>';
echo $html;
}
public static function closeHtmlTag($tagName)
{
echo '</' . $tagName . '>';
}
} }

View file

@ -183,102 +183,126 @@
</div> </div>
<?php endif ?> <?php endif ?>
<div class="unit options"> <?php
<h1>options</h1> $editPostPrivileges = [
Privilege::EditPostSafety,
Privilege::EditPostTags,
Privilege::EditPostThumb,
Privilege::EditPostSource,
];
$editPostPrivileges = array_fill_keys($editPostPrivileges, false);
foreach (array_keys($editPostPrivileges) as $privilege)
{
if (PrivilegesHelper::confirm($privilege, PrivilegesHelper::getIdentitySubPrivilege($this->context->transport->post->uploader)))
$editPostPrivileges[$privilege] = true;
}
$canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<ul> $options = [];
<?php if (PrivilegesHelper::confirm(Privilege::FavoritePost)): ?>
<?php if (!$this->context->favorite): ?>
<li class="add-fav">
<a class="simple-action" href="<?php echo \Chibi\UrlHelper::route('post', 'add-favorite', ['id' => $this->context->transport->post->id]) ?>">
Add to favorites
</a>
</li>
<?php else: ?>
<li class="rem-fav">
<a class="simple-action" href="<?php echo \Chibi\UrlHelper::route('post', 'rem-favorite', ['id' => $this->context->transport->post->id]) ?>">
Remove from favorites
</a>
</li>
<?php endif ?>
<?php endif ?>
<?php if (PrivilegesHelper::confirm(Privilege::FavoritePost))
$editPostPrivileges = [ {
Privilege::EditPostSafety, if (!$this->context->favorite)
Privilege::EditPostTags, {
Privilege::EditPostThumb, $options []=
Privilege::EditPostSource, [
'class' => 'add-fav',
'text' => 'Add to favorites',
'simple-action' => \Chibi\UrlHelper::route('post', 'add-favorite', ['id' => $this->context->transport->post->id]),
]; ];
$editPostPrivileges = array_fill_keys($editPostPrivileges, false); }
foreach (array_keys($editPostPrivileges) as $privilege) else
{ {
if (PrivilegesHelper::confirm($privilege, PrivilegesHelper::getIdentitySubPrivilege($this->context->transport->post->uploader))) $options []=
$editPostPrivileges[$privilege] = true; [
} 'class' => 'rem-fav',
$canEditAnything = count(array_filter($editPostPrivileges)) > 0; 'text' => 'Remove from favorites',
?> 'simple-action' => \Chibi\UrlHelper::route('post', 'rem-favorite', ['id' => $this->context->transport->post->id]),
];
}
}
<?php if ($canEditAnything): ?> if ($canEditAnything)
<li class="edit"> {
<a href="#"> $options []=
Edit [
</a> 'class' => 'edit',
</li> 'text' => 'Edit',
<?php endif ?> ];
}
<?php if (PrivilegesHelper::confirm(Privilege::HidePost, PrivilegesHelper::getIdentitySubPrivilege($this->context->transport->post->uploader))): ?> if (PrivilegesHelper::confirm(Privilege::HidePost, PrivilegesHelper::getIdentitySubPrivilege($this->context->transport->post->uploader)))
<?php if ($this->context->transport->post->hidden): ?> {
<li class="unhide"> if ($this->context->transport->post->hidden)
<a class="simple-action" href="<?php echo \Chibi\UrlHelper::route('post', 'unhide', ['id' => $this->context->transport->post->id]) ?>"> {
Unhide $options []=
</a> [
</li> 'class' => 'unhide',
<?php else: ?> 'text' => 'Unhide',
<li class="hide"> 'simple-action' => \Chibi\UrlHelper::route('post', 'unhide', ['id' => $this->context->transport->post->id]),
<a class="simple-action" href="<?php echo \Chibi\UrlHelper::route('post', 'hide', ['id' => $this->context->transport->post->id]) ?>"> ];
Hide }
</a> else
</li> {
<?php endif ?> $options []=
<?php endif ?> [
'class' => 'hide',
'text' => 'Hide',
'simple-action' => \Chibi\UrlHelper::route('post', 'hide', ['id' => $this->context->transport->post->id]),
];
}
}
<?php if (PrivilegesHelper::confirm(Privilege::FeaturePost)): ?> if (PrivilegesHelper::confirm(Privilege::FeaturePost))
<li class="feature"> {
<a class="simple-action" href="<?php echo \Chibi\UrlHelper::route('post', 'feature', ['id' => $this->context->transport->post->id]) ?>" data-confirm-text="Are you sure you want to feature this post on the main page?" data-redirect-url="<?php echo \Chibi\UrlHelper::route('index', 'index') ?>"> $options []=
Feature on main page [
</a> 'class' => 'feature',
</li> 'text' => 'Feature on main page',
<?php endif ?> 'simple-action' => \Chibi\UrlHelper::route('post', 'feature', ['id' => $this->context->transport->post->id]),
'data-confirm-text' => 'Are you sure you want to feature this post on the main page?',
'data-redirect-url' => \Chibi\UrlHelper::route('index', 'index'),
];
}
<?php if (PrivilegesHelper::confirm(Privilege::FlagPost)): ?> if (PrivilegesHelper::confirm(Privilege::FlagPost))
<li class="flag"> {
<?php if ($this->context->flagged): ?> if ($this->context->flagged)
<a class="simple-action inactive" href="#"> {
Flagged $options []=
</a> [
<?php else: ?> 'class' => 'flag',
<a class="simple-action" href="<?php echo \Chibi\UrlHelper::route('post', 'flag', ['id' => $this->context->transport->post->id]) ?>" data-confirm-text="Are you sure you want to flag this post?"> 'text' => 'Flagged',
Flag for moderator attention 'inactive' => true,
</a> ];
<?php endif ?> }
</li> else
<?php endif ?> {
$options []=
[
'class' => 'flag',
'text' => 'Flag for moderator attention',
'simple-action' => \Chibi\UrlHelper::route('post', 'flag', ['id' => $this->context->transport->post->id]),
'data-confirm-text' => 'Are you sure you want to flag this post?',
];
}
}
<?php if (PrivilegesHelper::confirm(Privilege::DeletePost, PrivilegesHelper::getIdentitySubPrivilege($this->context->transport->post->uploader))): ?> if (PrivilegesHelper::confirm(Privilege::DeletePost, PrivilegesHelper::getIdentitySubPrivilege($this->context->transport->post->uploader)))
<li class="delete"> {
<a class="simple-action" href="<?php echo \Chibi\UrlHelper::route('post', 'delete', ['id' => $this->context->transport->post->id]) ?>" data-confirm-text="Are you sure you want to delete this post?" data-redirect-url="<?php echo \Chibi\UrlHelper::route('post', 'list') ?>"> $options []=
Delete [
</a> 'class' => 'delete',
</li> 'text' => 'Delete',
<?php endif ?> 'simple-action' => \Chibi\UrlHelper::route('post', 'delete', ['id' => $this->context->transport->post->id]),
</ul> 'data-confirm-text' => 'Are you sure you want to delete this post?',
'data-redirect-url' => \Chibi\UrlHelper::route('post', 'list'),
];
}
<script type="text/javascript"> $this->context->options = $options;
if (!$('.options ul li').length) $this->renderFile('sidebar-options');
$('.options').hide(); ?>
</script>
</div>
</div> </div>
<div id="inner-content"> <div id="inner-content">

View file

@ -0,0 +1,35 @@
<?php if (!empty($this->context->options)): ?>
<div class="unit options">
<h1>options</h1>
<ul>
<?php foreach ($this->context->options as $option): ?>
<li class="<?php echo $option['class']; if (isset($option['inactive'])) echo ' inactive' ?>">
<?php
$attrs = [];
if (!empty($option['link']))
{
$attrs['href'] = $option['link'];
}
elseif (!empty($option['simple-action']))
{
$attrs['href'] = $option['simple-action'];
$attrs['class'] = 'simple-action';
}
else
$attrs['href'] = '#';
foreach ($option as $key => $val)
if (strpos($key, 'data-') === 0)
$attrs[$key] = $val;
TextHelper::openHtmlTag('a', $attrs);
echo $option['text'];
TextHelper::closeHtmlTag('a');
?>
</li>
<?php endforeach ?>
</ul>
</div>
<?php endif ?>

View file

@ -28,87 +28,104 @@
<?php endif ?> <?php endif ?>
</div> </div>
<div class="unit options"> <?php
<h1>options</h1> $userModificationPrivileges = [
Privilege::ChangeUserName,
Privilege::ChangeUserEmail,
Privilege::ChangeUserPassword,
Privilege::ChangeUserAccessRank,
];
$userModificationPrivileges = array_fill_keys($userModificationPrivileges, false);
foreach (array_keys($userModificationPrivileges) as $privilege)
{
<ul> if (PrivilegesHelper::confirm($privilege, PrivilegesHelper::getIdentitySubPrivilege($this->context->transport->user)))
<?php $userModificationPrivileges[$privilege] = true;
$userModificationPrivileges = [ }
Privilege::ChangeUserName, $canModifyAnything = count(array_filter($userModificationPrivileges)) > 0;
Privilege::ChangeUserEmail,
Privilege::ChangeUserPassword, $options = [];
Privilege::ChangeUserAccessRank,
if ($canModifyAnything)
{
$options []=
[
'class' => 'edit',
'text' => 'Edit account settings',
'link' => \Chibi\UrlHelper::route('user', 'edit', ['name' => $this->context->transport->user->name, 'tab' => 'edit']),
];
}
if (PrivilegesHelper::confirm(Privilege::DeleteUser, PrivilegesHelper::getIdentitySubPrivilege($this->context->transport->user)))
{
$options []=
[
'class' => 'delete',
'text' => 'Delete account',
'link' => \Chibi\UrlHelper::route('user', 'delete', ['name' => $this->context->transport->user->name, 'tab' => 'delete']),
];
}
if (PrivilegesHelper::confirm(Privilege::FlagUser))
{
if ($this->context->flagged)
{
$options []=
[
'class' => 'flag',
'text' => 'Flagged',
'inactive' => true,
]; ];
$userModificationPrivileges = array_fill_keys($userModificationPrivileges, false); }
foreach (array_keys($userModificationPrivileges) as $privilege) else
{ {
$options []=
[
'class' => 'flag',
'text' => 'Flag for moderator attention',
'simple-action' => \Chibi\UrlHelper::route('user', 'flag', ['name' => $this->context->transport->user->name]),
'data-confirm-text' => 'Are you sure you want to flag this user?',
];
}
}
if (PrivilegesHelper::confirm($privilege, PrivilegesHelper::getIdentitySubPrivilege($this->context->transport->user))) if (PrivilegesHelper::confirm(Privilege::BanUser, PrivilegesHelper::getIdentitySubPrivilege($this->context->transport->user)))
$userModificationPrivileges[$privilege] = true; {
} if (!$this->context->transport->user->banned)
$canModifyAnything = count(array_filter($userModificationPrivileges)) > 0; {
?> $options []=
[
'class' => 'ban',
'text' => 'Ban user',
'simple-action' => \Chibi\UrlHelper::route('user', 'ban', ['name' => $this->context->transport->user->name]),
'data-confirm-text' => 'Are you sure?',
];
}
else
{
$options []=
[
'class' => 'unban',
'text' => 'Unban user',
'simple-action' => \Chibi\UrlHelper::route('user', 'unban', ['name' => $this->context->transport->user->name]),
'data-confirm-text' => 'Are you sure?',
];
}
}
<?php if ($canModifyAnything): ?> if (PrivilegesHelper::confirm(Privilege::AcceptUserRegistration) and !$this->context->transport->user->staff_confirmed and $this->config->registration->staffActivation)
<li class="edit"> {
<a href="<?php echo \Chibi\UrlHelper::route('user', 'edit', ['name' => $this->context->transport->user->name, 'tab' => 'edit']) ?>"> $options []=
Edit account settings [
</a> 'class' => 'accept-registration',
</li> 'text' => 'Accept registration',
<?php endif ?> 'simple-action' => \Chibi\UrlHelper::route('user', 'accept-registration', ['name' => $this->context->transport->user->name]),
];
}
<?php if (PrivilegesHelper::confirm(Privilege::DeleteUser, PrivilegesHelper::getIdentitySubPrivilege($this->context->transport->user))): ?> $this->context->options = $options;
<li class="edit"> $this->renderFile('sidebar-options');
<a href="<?php echo \Chibi\UrlHelper::route('user', 'delete', ['name' => $this->context->transport->user->name, 'tab' => 'delete']) ?>"> ?>
Delete account
</a>
</li>
<?php endif ?>
<?php if (PrivilegesHelper::confirm(Privilege::FlagUser)): ?>
<li class="flag">
<?php if ($this->context->flagged): ?>
<a class="simple-action inactive" href="#">
Flagged
</a>
<?php else: ?>
<a class="simple-action" href="<?php echo \Chibi\UrlHelper::route('user', 'flag', ['name' => $this->context->transport->user->name]) ?>" data-confirm-text="Are you sure you want to flag this user?">
Flag for moderator attention
</a>
<?php endif ?>
</li>
<?php endif ?>
<?php if (PrivilegesHelper::confirm(Privilege::BanUser, PrivilegesHelper::getIdentitySubPrivilege($this->context->transport->user))): ?>
<?php if (!$this->context->transport->user->banned): ?>
<li class="ban">
<a class="simple-action" href="<?php echo \Chibi\UrlHelper::route('user', 'ban', ['name' => $this->context->transport->user->name]) ?>" data-confirm-text="Are you sure?">
Ban user
</a>
</li>
<?php else: ?>
<li class="unban">
<a class="simple-action" href="<?php echo \Chibi\UrlHelper::route('user', 'unban', ['name' => $this->context->transport->user->name]) ?>" data-confirm-text="Are you sure?">
Unban user
</a>
</li>
<?php endif ?>
<?php endif ?>
<?php if (PrivilegesHelper::confirm(Privilege::AcceptUserRegistration) and !$this->context->transport->user->staff_confirmed and $this->config->registration->staffActivation): ?>
<li class="accept-registration">
<a class="simple-action" href="<?php echo \Chibi\UrlHelper::route('user', 'accept-registration', ['name' => $this->context->transport->user->name]) ?>">
Accept registration
</a>
</li>
<?php endif ?>
</ul>
<script type="text/javascript">
if (!$('.options ul li').length)
$('.options').hide();
</script>
</div>
</div> </div>
<div id="inner-content"> <div id="inner-content">