From 65e909d053a4150c0680ef8d1b8bb902f62b5c1e Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 20 May 2014 17:03:47 +0200 Subject: [PATCH] Refactored form rendering --- public_html/media/js/post-view.js | 2 +- src/Controllers/TagController.php | 8 +- src/View.php | 4 +- src/Views/auth-login.phtml | 18 +-- src/Views/input/input-basic.phtml | 36 +++++ src/Views/input/input-checkboxes.phtml | 38 ++++++ src/Views/input/input-file.phtml | 3 + src/Views/input/input-hidden.phtml | 6 + src/Views/input/input-password.phtml | 3 + src/Views/input/input-radioboxes.phtml | 33 +++++ src/Views/input/input-select.phtml | 28 ++++ src/Views/input/input-text.phtml | 3 + src/Views/post/post-edit.phtml | 180 +++++++++++-------------- src/Views/tag/tag-mass-tag.phtml | 70 +++++----- src/Views/tag/tag-merge.phtml | 25 ++-- src/Views/tag/tag-rename.phtml | 25 ++-- src/Views/user/user-delete.phtml | 18 +-- src/Views/user/user-edit.phtml | 165 ++++++++++------------- src/Views/user/user-registration.phtml | 74 ++++------ src/Views/user/user-settings.phtml | 124 +++++------------ 20 files changed, 443 insertions(+), 420 deletions(-) create mode 100644 src/Views/input/input-basic.phtml create mode 100644 src/Views/input/input-checkboxes.phtml create mode 100644 src/Views/input/input-file.phtml create mode 100644 src/Views/input/input-hidden.phtml create mode 100644 src/Views/input/input-password.phtml create mode 100644 src/Views/input/input-radioboxes.phtml create mode 100644 src/Views/input/input-select.phtml create mode 100644 src/Views/input/input-text.phtml diff --git a/public_html/media/js/post-view.js b/public_html/media/js/post-view.js index 66bedcaa..f8eeaa81 100644 --- a/public_html/media/js/post-view.js +++ b/public_html/media/js/post-view.js @@ -14,7 +14,7 @@ $(function() var formDom = $('form.edit-post'); if (formDom.find('.tagit').length == 0) { - attachTagIt($('.tags input')); + attachTagIt($('input[name=tags]')); aDom.removeClass('inactive'); formDom.find('input[type=text]:visible:eq(0)').focus(); diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index eff9672e..4c819bf0 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -85,8 +85,8 @@ class TagController extends AbstractController Api::run( new MergeTagsJob(), [ - JobArgs::ARG_SOURCE_TAG_NAME => InputHelper::get('source-tag'), - JobArgs::ARG_TARGET_TAG_NAME => InputHelper::get('target-tag'), + JobArgs::ARG_SOURCE_TAG_NAME => trim(InputHelper::get('source-tag')), + JobArgs::ARG_TARGET_TAG_NAME => trim(InputHelper::get('target-tag')), ]); Messenger::success('Tags merged successfully.'); @@ -111,8 +111,8 @@ class TagController extends AbstractController Api::run( new RenameTagsJob(), [ - JobArgs::ARG_SOURCE_TAG_NAME => InputHelper::get('source-tag'), - JobArgs::ARG_TARGET_TAG_NAME => InputHelper::get('target-tag'), + JobArgs::ARG_SOURCE_TAG_NAME => trim(InputHelper::get('source-tag')), + JobArgs::ARG_TARGET_TAG_NAME => trim(InputHelper::get('target-tag')), ]); Messenger::success('Tag renamed successfully.'); diff --git a/src/View.php b/src/View.php index 23c51dc0..c7c109b1 100644 --- a/src/View.php +++ b/src/View.php @@ -12,10 +12,10 @@ class View extends \Chibi\View $view->render(); } - protected function renderExternal($viewName) + protected function renderExternal($viewName, $context = null) { $view = new View($viewName); - $view->context = $this->context; + $view->context = $context !== null ? $context : $this->context; $view->assets = $this->assets; $view->render(); } diff --git a/src/Views/auth-login.phtml b/src/Views/auth-login.phtml index 880ee1d8..fa3d3a0c 100644 --- a/src/Views/auth-login.phtml +++ b/src/Views/auth-login.phtml @@ -14,15 +14,17 @@ $this->assets->addStylesheet('auth.css'); click here to create a new one.

-
- -
-
+ name = 'name'; + $context->label = 'User name'; + $this->renderExternal('input-text', $context); -
- -
-
+ $context = new StdClass; + $context->name = 'password'; + $context->label = 'Password'; + $this->renderExternal('input-password', $context); + ?>
diff --git a/src/Views/input/input-basic.phtml b/src/Views/input/input-basic.phtml new file mode 100644 index 00000000..83e86b64 --- /dev/null +++ b/src/Views/input/input-basic.phtml @@ -0,0 +1,36 @@ +context->type; +$name = $this->context->name; +$label = isset($this->context->label) ? $this->context->label . ':' : ''; +$value = isset($this->context->value) ? $this->context->value : ''; +$placeholder = isset($this->context->placeholder) ? $this->context->placeholder : ''; +$additionalInfo = isset($this->context->additionalInfo) ? $this->context->additionalInfo : ''; +$inputClass = isset($this->context->inputClass) ? $this->context->inputClass : ''; +$noAutocomplete = isset($this->context->noAutocomplete) ? true : false; +?> + +
+ +
+ + autocomplete="off" + + + class="" + + type="" + name="" + id="" + placeholder="" + value=""/> + + + + +
+
+ diff --git a/src/Views/input/input-checkboxes.phtml b/src/Views/input/input-checkboxes.phtml new file mode 100644 index 00000000..32a636c2 --- /dev/null +++ b/src/Views/input/input-checkboxes.phtml @@ -0,0 +1,38 @@ +context->label) ? $this->context->label . ':' : ''; +$optionValuesDisabled = $this->context->optionValuesDisabled; +$optionValuesEnabled = $this->context->optionValuesEnabled; +$optionLabels = $this->context->optionLabels; +$optionNames = $this->context->optionNames; +$optionStates = $this->context->optionStates; +$keys = array_keys($optionNames); +?> + +
+ +
+ + + + + + +
+
diff --git a/src/Views/input/input-file.phtml b/src/Views/input/input-file.phtml new file mode 100644 index 00000000..eecf8d07 --- /dev/null +++ b/src/Views/input/input-file.phtml @@ -0,0 +1,3 @@ +context->type = 'file'; +$this->renderExternal('input-basic', $this->context); diff --git a/src/Views/input/input-hidden.phtml b/src/Views/input/input-hidden.phtml new file mode 100644 index 00000000..68f7a419 --- /dev/null +++ b/src/Views/input/input-hidden.phtml @@ -0,0 +1,6 @@ +context->name; +$value = $this->context->value; +?> + + diff --git a/src/Views/input/input-password.phtml b/src/Views/input/input-password.phtml new file mode 100644 index 00000000..bf1a98a7 --- /dev/null +++ b/src/Views/input/input-password.phtml @@ -0,0 +1,3 @@ +context->type = 'password'; +$this->renderExternal('input-basic', $this->context); diff --git a/src/Views/input/input-radioboxes.phtml b/src/Views/input/input-radioboxes.phtml new file mode 100644 index 00000000..94538b8b --- /dev/null +++ b/src/Views/input/input-radioboxes.phtml @@ -0,0 +1,33 @@ +context->name; +$label = isset($this->context->label) ? $this->context->label . ':' : ''; +$optionValues = $this->context->optionValues; +$optionLabels = $this->context->optionLabels; +$activeOptionValue = $this->context->activeOptionValue; +$keys = array_keys($optionValues); +?> + +
+ +
+ + + + +
+
diff --git a/src/Views/input/input-select.phtml b/src/Views/input/input-select.phtml new file mode 100644 index 00000000..e349e8e1 --- /dev/null +++ b/src/Views/input/input-select.phtml @@ -0,0 +1,28 @@ +context->name; +$label = isset($this->context->label) ? $this->context->label . ':' : ''; +$optionValues = $this->context->optionValues; +$optionLabels = $this->context->optionLabels; +$activeOptionValue = isset($this->context->activeOptionValue) ? $this->context->activeOptionValue : null; +$keys = array_keys($optionValues); +?> + +
+ +
+ +
+
diff --git a/src/Views/input/input-text.phtml b/src/Views/input/input-text.phtml new file mode 100644 index 00000000..dac36792 --- /dev/null +++ b/src/Views/input/input-text.phtml @@ -0,0 +1,3 @@ +context->type = 'text'; +$this->renderExternal('input-basic', $this->context); diff --git a/src/Views/post/post-edit.phtml b/src/Views/post/post-edit.phtml index f23753ee..1a88f499 100644 --- a/src/Views/post/post-edit.phtml +++ b/src/Views/post/post-edit.phtml @@ -12,112 +12,94 @@ id="edit-token" value="context->transport->post->getEditToken()) ?>"/> - context->transport->post->getUploader())))): ?> + context->transport->post->getUploader())))) + { + $safety = PostSafety::getAll(); -
- -
- - - -
-
- + $context = new StdClass; + $context->label = 'Safety'; + $context->name = 'safety'; + $context->optionValues = array_map(function($s) { return $s->toInteger(); }, $safety); + $context->optionLabels = array_map(function($s) { return ucfirst($s->toDisplayString()); }, $safety); + $context->activeOptionValue = $this->context->transport->post->getSafety()->toInteger(); + $this->renderExternal('input-radioboxes', $context); + } - context->transport->post->getUploader())))): ?> + if (Access::check(new Privilege( + Privilege::EditPostTags, + Access::getIdentity($this->context->transport->post->getUploader())))) + { + $context = new StdClass; + $context->label = 'Tags'; + $context->name = 'tags'; + $context->placeholder = 'Enter some tags…'; + $context->value = join(',', array_map( + function($tag) + { + return htmlspecialchars($tag->getName()); + }, + $this->context->transport->post->getTags())); + $this->renderExternal('input-text', $context); + } -
- -
- -
-
- + if (Access::check(new Privilege( + Privilege::EditPostSource, + Access::getIdentity($this->context->transport->post->getUploader())))) + { + $context = new StdClass; + $context->label = 'Source'; + $context->name = 'source'; + $context->value = htmlspecialchars($this->context->transport->post->getSource()); + $this->renderExternal('input-text', $context); + } - context->transport->post->getUploader())))): ?> + if (Access::check(new Privilege( + Privilege::EditPostRelations, + Access::getIdentity($this->context->transport->post->getUploader())))) + { + $context = new StdClass; + $context->label = 'Relations'; + $context->name = 'relations'; + $context->placeholder = 'id1,id2,…'; + $context->value = join(',', array_map( + function($post) + { + return $post->getId(); + }, + $this->context->transport->post->getRelations())); + $this->renderExternal('input-text', $context); + } -
- -
- -
-
- + if (Access::check(new Privilege( + Privilege::EditPostContent, + Access::getIdentity($this->context->transport->post->getUploader())))) + { + $context = new StdClass; + $context->label = 'File'; + $context->name = 'url'; + $context->placeholder = 'Some URL…'; + $this->renderExternal('input-text', $context); - context->transport->post->getUploader())))): ?> + $context = new StdClass; + $context->name = 'file'; + $this->renderExternal('input-file', $context); + } -
- -
- -
-
- - - context->transport->post->getUploader())))): ?> - -
- -
- -
-
- -
- -
- -
-
- - - context->transport->post->getUploader())))): ?> - -
- -
- - context->transport->post->hasCustomThumb()): ?> - (Currently using custom thumb) - -
-
- + if (Access::check(new Privilege( + Privilege::EditPostThumb, + Access::getIdentity($this->context->transport->post->getUploader())))) + { + $context = new StdClass; + $context->name = 'thumb'; + $context->label = 'Thumb'; + if ($this->context->transport->post->hasCustomThumb()) + $context->additionalInfo = '(Currently using custom thumb)'; + $this->renderExternal('input-file', $context); + } + ?>
diff --git a/src/Views/tag/tag-mass-tag.phtml b/src/Views/tag/tag-mass-tag.phtml index cf28eb79..4817cfc0 100644 --- a/src/Views/tag/tag-mass-tag.phtml +++ b/src/Views/tag/tag-mass-tag.phtml @@ -4,47 +4,41 @@ ['source' => 'mass-tag']) ?>">

mass tag

-
- -
- -
-
+ name = 'query'; + $context->label = 'Search query'; + $context->inputClass = 'autocomplete'; + if (isset($this->context->massTagQuery)) + $context->value = htmlspecialchars($this->context->massTagQuery); + $this->renderExternal('input-text', $context); + ?> -
- -
- -
-
+ name = 'tag'; + $context->label = 'Tag'; + $context->inputClass = 'autocomplete'; + if (isset($this->context->massTagTag)) + $context->value = htmlspecialchars($this->context->massTagTag); + $this->renderExternal('input-text', $context); - + if (isset($this->context->transport->paginator)) + { + $context = new StdClass; + $context->name = 'old-page'; + $context->value = intval($this->context->transport->paginator->page); + $this->renderExternal('input-hidden', $context); + } - + if (isset($this->context->massTagQuery)) + { + $context = new StdClass; + $context->name = 'old-query'; + $context->value = htmlspecialchars($this->context->massTagQuery); + $this->renderExternal('input-hidden', $context); + } + ?>
diff --git a/src/Views/tag/tag-merge.phtml b/src/Views/tag/tag-merge.phtml index fef7b43c..c49d1d3a 100644 --- a/src/Views/tag/tag-merge.phtml +++ b/src/Views/tag/tag-merge.phtml @@ -2,21 +2,20 @@

merge tags

-
- -
- -
-
+ name = 'source-tag'; + $context->label = 'Source tag'; + $context->inputClass = 'autocomplete'; + $this->renderExternal('input-text', $context); -
- -
- -
-
+ $context = new StdClass; + $context->name = 'target-tag'; + $context->label = 'Target tag'; + $this->renderExternal('input-text', $context); - renderExternal('message') ?> + $this->renderExternal('message'); + ?>
diff --git a/src/Views/tag/tag-rename.phtml b/src/Views/tag/tag-rename.phtml index 4222f348..75b4e1d0 100644 --- a/src/Views/tag/tag-rename.phtml +++ b/src/Views/tag/tag-rename.phtml @@ -2,21 +2,20 @@

rename tags

-
- -
- -
-
+ name = 'source-tag'; + $context->label = 'Source tag'; + $context->inputClass = 'autocomplete'; + $this->renderExternal('input-text', $context); -
- -
- -
-
+ $context = new StdClass; + $context->name = 'target-tag'; + $context->label = 'Target tag'; + $this->renderExternal('input-text', $context); - renderExternal('message') ?> + $this->renderExternal('message'); + ?>
diff --git a/src/Views/user/user-delete.phtml b/src/Views/user/user-delete.phtml index a09a21fe..a4aee927 100644 --- a/src/Views/user/user-delete.phtml +++ b/src/Views/user/user-delete.phtml @@ -7,14 +7,16 @@ autocomplete="off" data-confirm-text="Are you sure you want to delete your account?"> - getId() == $this->context->transport->user->getId()): ?> -
- -
- -
-
- + getId() == $this->context->transport->user->getId()) + { + $context = new StdClass; + $context->label = 'Current password'; + $context->name = 'current-password'; + $context->placeholder = 'Current password'; + $this->renderExternal('input-password', $context); + } + ?> renderExternal('message') ?> diff --git a/src/Views/user/user-edit.phtml b/src/Views/user/user-edit.phtml index 29de48c5..97006c51 100644 --- a/src/Views/user/user-edit.phtml +++ b/src/Views/user/user-edit.phtml @@ -6,108 +6,83 @@ class="edit" autocomplete="off"> - getId() == $this->context->transport->user->getId()): ?> -
- -
- -
-
-
- + getId() == $this->context->transport->user->getId()) + { + $context = new StdClass; + $context->label = 'Current password'; + $context->name = 'current-password'; + $context->placeholder = 'Current password'; + $this->renderExternal('input-password', $context); + echo '
'; + } - context->transport->user)))): ?> + if (Access::check(new Privilege( + Privilege::EditUserName, + Access::getIdentity($this->context->transport->user)))) + { + $context = new StdClass; + $context->label = 'Name'; + $context->name = 'name'; + $context->placeholder = 'New name…'; + $context->value = htmlspecialchars(InputHelper::get('name')); + $this->renderExternal('input-text', $context); + } -
- -
- -
-
- + if (Access::check(new Privilege( + Privilege::EditUserEmail, + Access::getIdentity($this->context->transport->user)))) + { + $context = new StdClass; + $context->label = 'E-mail'; + $context->name = 'email'; + $context->placeholder = 'New e-mail…'; + $context->value = htmlspecialchars(InputHelper::get('email')); + $this->renderExternal('input-text', $context); + } - context->transport->user)))): ?> + if (Access::check(new Privilege( + Privilege::EditUserPassword, + Access::getIdentity($this->context->transport->user)))) + { + $context = new StdClass; + $context->label = 'New password'; + $context->name = 'password1'; + $context->placeholder = 'New password…'; + $context->value = htmlspecialchars(InputHelper::get('password1')); + $this->renderExternal('input-password', $context); - - + $context = new StdClass; + $context->name = 'password2'; + $context->placeholder = 'New password… (repeat)'; + $context->value = htmlspecialchars(InputHelper::get('password2')); + $this->renderExternal('input-password', $context); + } - context->transport->user)))): ?> + if (Access::check(new Privilege( + Privilege::EditUserAccessRank, + Access::getIdentity($this->context->transport->user)))) + { + $accessRanks = array_filter( + AccessRank::getAll(), + function($ar) + { + return $ar->toInteger() != AccessRank::Nobody; + }); -
- -
- -
-
-
- -
- -
-
- + $context = new StdClass; + $context->name = 'access-rank'; + $context->label = 'Access rank'; + $context->placeholder = 'New password… (repeat)'; + $context->optionValues = array_map(function($ar) { return $ar->toInteger(); }, $accessRanks); + $context->optionLabels = array_map(function($ar) { return $ar->toDisplayString(); }, $accessRanks); + $context->activeOptionValue = InputHelper::get('access-rank') + ?: $this->context->transport->user->getAccessRank()->toInteger(); + $this->renderExternal('input-select', $context); + } - context->transport->user)))): ?> - -
- -
- -
-
- - - renderExternal('message') ?> + $this->renderExternal('message'); + ?>
diff --git a/src/Views/user/user-registration.phtml b/src/Views/user/user-registration.phtml index 38bf204a..7419043e 100644 --- a/src/Views/user/user-registration.phtml +++ b/src/Views/user/user-registration.phtml @@ -16,56 +16,36 @@ $this->assets->setSubTitle('registration form');

Registered users can view more content,
upload files and add posts to favorites.

-
- -
- -
-
+ name = 'name'; + $context->label = 'User name'; + $context->value = htmlspecialchars(InputHelper::get('name')); + $context->placeholder = 'e.g. darth_vader'; + $context->noAutocomplete = true; + $this->renderExternal('input-text', $context); -
- -
- -
-
+ $context = new StdClass; + $context->name = 'password1'; + $context->label = 'Password'; + $context->value = htmlspecialchars(InputHelper::get('password1')); + $context->placeholder = 'e.g. ' . str_repeat('●', 8); + $context->noAutocomplete = true; + $this->renderExternal('input-password', $context); -
- -
- -
-
+ $context->name = 'password2'; + $context->label = 'Password (repeat)'; + $context->value = htmlspecialchars(InputHelper::get('password2')); + $this->renderExternal('input-password', $context); -
- -
- -
-
+ $context = new StdClass; + $context->name = 'email'; + $context->label = 'E-mail address'; + $context->value = htmlspecialchars(InputHelper::get('email')); + $context->placeholder = 'e.g. vader@empire.gov'; + $context->noAutocomplete = true; + $this->renderExternal('input-text', $context); + ?>

Your e-mail will be used to show your Gravatar.
diff --git a/src/Views/user/user-settings.phtml b/src/Views/user/user-settings.phtml index 53c033c8..b1398c53 100644 --- a/src/Views/user/user-settings.phtml +++ b/src/Views/user/user-settings.phtml @@ -9,103 +9,43 @@ $settings = $this->context->transport->user->getSettings(); method="post" class="settings"> -

- -
- - toString()))): ?> - - - -
-
+ $context = new StdClass; + $context->label = 'Safety'; + $context->optionValuesDisabled = array_map(function($s) { return null; }, $safety); + $context->optionValuesEnabled = array_map(function($s) { return $s->toInteger(); }, $safety); + $context->optionLabels = array_map(function($s) { return ucfirst($s->toDisplayString()); }, $safety); + $context->optionNames = array_map(function($s) { return 'safety[]'; }, $safety); + $context->optionStates = array_map(function($s) use ($settings) { return $settings->hasEnabledSafety($s); }, $safety); + $this->renderExternal('input-checkboxes', $context); -
- -
- - -
-
+ $context->label = 'Endless scrolling'; + $context->optionNames = ['endless-scrolling']; + $context->optionStates = [$settings->hasEnabledEndlessScrolling()]; + $this->renderExternal('input-checkboxes', $context); - + $context->label = 'Tags in thumbs'; + $context->optionNames = ['post-tag-titles']; + $context->optionStates = [$settings->hasEnabledPostTagTitles()]; + $this->renderExternal('input-checkboxes', $context); -
- -
- - -
-
+ $context->label = 'Hide down-voted'; + $context->optionNames = ['hide-disliked-posts']; + $context->optionStates = [$settings->hasEnabledHidingDislikedPosts()]; + $this->renderExternal('input-checkboxes', $context); + ?> renderExternal('message') ?>