From d570bc1790826f2e3efeeaa9c51977953b4dfd9b Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 30 Nov 2013 00:07:23 +0100 Subject: [PATCH] Optimalization: sidebar options structure - options rendering moved to separate file - simplified template code - removed redundant JS --- src/Helpers/TextHelper.php | 16 +++ src/Views/post-view.phtml | 198 ++++++++++++++++++-------------- src/Views/sidebar-options.phtml | 35 ++++++ src/Views/user-view.phtml | 169 +++++++++++++++------------ 4 files changed, 255 insertions(+), 163 deletions(-) create mode 100644 src/Views/sidebar-options.phtml diff --git a/src/Helpers/TextHelper.php b/src/Helpers/TextHelper.php index 1f063f94..83e8e760 100644 --- a/src/Helpers/TextHelper.php +++ b/src/Helpers/TextHelper.php @@ -233,4 +233,20 @@ class TextHelper $path = self::cleanPath($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 ''; + } } diff --git a/src/Views/post-view.phtml b/src/Views/post-view.phtml index d128d5ff..1681e1ca 100644 --- a/src/Views/post-view.phtml +++ b/src/Views/post-view.phtml @@ -183,102 +183,126 @@ -
-

options

+ context->transport->post->uploader))) + $editPostPrivileges[$privilege] = true; + } + $canEditAnything = count(array_filter($editPostPrivileges)) > 0; - + if (PrivilegesHelper::confirm(Privilege::DeletePost, PrivilegesHelper::getIdentitySubPrivilege($this->context->transport->post->uploader))) + { + $options []= + [ + 'class' => 'delete', + 'text' => 'Delete', + 'simple-action' => \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' => \Chibi\UrlHelper::route('post', 'list'), + ]; + } - -
+ $this->context->options = $options; + $this->renderFile('sidebar-options'); + ?>
diff --git a/src/Views/sidebar-options.phtml b/src/Views/sidebar-options.phtml new file mode 100644 index 00000000..c3b251c7 --- /dev/null +++ b/src/Views/sidebar-options.phtml @@ -0,0 +1,35 @@ +context->options)): ?> +
+

options

+ +
    + context->options as $option): ?> +
  • + $val) + if (strpos($key, 'data-') === 0) + $attrs[$key] = $val; + + TextHelper::openHtmlTag('a', $attrs); + echo $option['text']; + TextHelper::closeHtmlTag('a'); + ?> +
  • + +
+
+ + diff --git a/src/Views/user-view.phtml b/src/Views/user-view.phtml index 34c1815f..49caefaf 100644 --- a/src/Views/user-view.phtml +++ b/src/Views/user-view.phtml @@ -28,87 +28,104 @@
-
-

options

+ - context->transport->user))) + $userModificationPrivileges[$privilege] = true; + } + $canModifyAnything = count(array_filter($userModificationPrivileges)) > 0; + + $options = []; + + 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))) - $userModificationPrivileges[$privilege] = true; - } - $canModifyAnything = count(array_filter($userModificationPrivileges)) > 0; - ?> + if (PrivilegesHelper::confirm(Privilege::BanUser, PrivilegesHelper::getIdentitySubPrivilege($this->context->transport->user))) + { + if (!$this->context->transport->user->banned) + { + $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?', + ]; + } + } - -
  • - - Edit account settings - -
  • - + if (PrivilegesHelper::confirm(Privilege::AcceptUserRegistration) and !$this->context->transport->user->staff_confirmed and $this->config->registration->staffActivation) + { + $options []= + [ + 'class' => 'accept-registration', + 'text' => 'Accept registration', + 'simple-action' => \Chibi\UrlHelper::route('user', 'accept-registration', ['name' => $this->context->transport->user->name]), + ]; + } - context->transport->user))): ?> -
  • - - Delete account - -
  • - - - -
  • - context->flagged): ?> - - Flagged - - - - Flag for moderator attention - - -
  • - - - context->transport->user))): ?> - context->transport->user->banned): ?> -
  • - - Ban user - -
  • - -
  • - - Unban user - -
  • - - - - context->transport->user->staff_confirmed and $this->config->registration->staffActivation): ?> -
  • - - Accept registration - -
  • - - - - -
    + $this->context->options = $options; + $this->renderFile('sidebar-options'); + ?>