Skip to content

Commit 7045801

Browse files
authored
v1.0.2 (#2)
* feat(console): Add form:list command for displaying available forms and their submissions * feat(view): Add `@htmlform` blade directive for calling the form in views * enhance(provider): Clean up ServiceProvider * enhance(admin): Better implementation of the admin menu clean up
1 parent 4d5204a commit 7045801

File tree

4 files changed

+118
-13
lines changed

4 files changed

+118
-13
lines changed

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Console/FormListCommand.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Log1x\HtmlForms\Console;
4+
5+
use Roots\Acorn\Console\Commands\Command;
6+
use Illuminate\Support\Str;
7+
8+
class FormListCommand extends Command
9+
{
10+
/**
11+
* The console command signature.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'form:list {name?}';
16+
17+
/**
18+
* The description of the command.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'List the available forms and submissions managed by HTML Forms';
23+
24+
/**
25+
* Execute the console command.
26+
*
27+
* @return mixed
28+
*/
29+
public function handle()
30+
{
31+
if (! empty($this->argument('name'))) {
32+
$form = collect(
33+
get_posts([
34+
'post_type' => 'html-form',
35+
'name' => $this->argument('name'),
36+
'post_status' => 'publish',
37+
'posts_per_page' => 1,
38+
'ignore_sticky_posts' => true,
39+
'no_found_rows' => true,
40+
])
41+
);
42+
43+
if ($form->isEmpty()) {
44+
return $this->error(
45+
sprintf('The form %s could not be found.', $this->argument('name'))
46+
);
47+
}
48+
49+
$submissions = collect(
50+
hf_get_form_submissions($form->shift()->ID)
51+
)->filter();
52+
53+
if ($submissions->isEmpty()) {
54+
return $this->line('There were no submissions found for the %s form.', $form->shift()->title);
55+
}
56+
57+
return $this->table(
58+
collect($submissions->first()->data)->keys()->map(function ($value) {
59+
return Str::title(str_replace('_', ' ', $value));
60+
})->all(),
61+
$submissions->map(function ($value) {
62+
return array_values($value->data);
63+
})->all()
64+
);
65+
}
66+
67+
return $this->table(['ID', 'Name', 'Slug', 'Actions', 'Submissions'], collect(
68+
get_posts(['post_type' => 'html-form'])
69+
)->map(function ($value) {
70+
$value = hf_get_form($value->ID);
71+
72+
return [
73+
$value->ID,
74+
$value->title,
75+
$value->slug,
76+
collect($value->settings['actions'])->map(function ($value) {
77+
return Str::title($value['type']);
78+
})->implode(', '),
79+
collect(
80+
hf_get_form_submissions($value->ID)
81+
)->count(),
82+
];
83+
})->all());
84+
}
85+
}

src/HtmlForms.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Log1x\HtmlForms;
44

5+
use Illuminate\Support\Str;
6+
57
use function Roots\view;
68

79
class HtmlForms
@@ -14,7 +16,10 @@ class HtmlForms
1416
public function __construct()
1517
{
1618
$this->render();
17-
$this->clean();
19+
20+
if (is_admin()) {
21+
$this->cleanAdmin();
22+
}
1823
}
1924

2025
/**
@@ -31,7 +36,10 @@ protected function render()
3136
return $html;
3237
}
3338

34-
return view('forms.' . $form->slug, ['form' => $form->ID])->render();
39+
return view(
40+
Str::start($form->slug, 'forms.'),
41+
['form' => $form->ID]
42+
)->render();
3543
}, 10, 2);
3644
}
3745

@@ -41,20 +49,21 @@ protected function render()
4149
*
4250
* @return void
4351
*/
44-
protected function clean()
52+
protected function cleanAdmin()
4553
{
4654
add_filter('hf_admin_output_misc_settings', function () {
4755
echo '<style>.hf-sidebar { display: none; }</style>';
4856
});
4957

5058
add_filter('admin_menu', function () {
5159
remove_menu_page('html-forms');
52-
53-
array_push($GLOBALS['submenu']['options-general.php'], [
60+
add_submenu_page(
61+
'options-general.php',
62+
'HTML Forms',
5463
'HTML Forms',
5564
'edit_forms',
56-
admin_url('admin.php?page=html-forms'),
57-
]);
65+
'html-forms'
66+
);
5867
});
5968
}
6069
}

src/HtmlFormsServiceProvider.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Log1x\HtmlForms;
44

5-
use Roots\Acorn\ServiceProvider;
5+
use Illuminate\Support\Facades\Blade;
66
use Illuminate\View\Compilers\BladeCompiler;
7+
use Roots\Acorn\ServiceProvider;
78
use Log1x\HtmlForms\HtmlForms;
89
use Log1x\HtmlForms\Console\FormMakeCommand;
10+
use Log1x\HtmlForms\Console\FormListCommand;
911
use Log1x\HtmlForms\View\Components\HtmlForms as HtmlFormsComponent;
1012

1113
class HtmlFormsServiceProvider extends ServiceProvider
@@ -29,8 +31,13 @@ public function register()
2931
*/
3032
public function boot()
3133
{
34+
if (! class_exists('\HTML_Forms\Form')) {
35+
return;
36+
}
37+
3238
$this->commands([
3339
FormMakeCommand::class,
40+
FormListCommand::class,
3441
]);
3542

3643
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'HtmlForms');
@@ -39,6 +46,10 @@ public function boot()
3946
$view->component(HtmlFormsComponent::class);
4047
});
4148

49+
Blade::directive('htmlform', function ($expression) {
50+
return "<?php echo \hf_get_form($expression); ?>";
51+
});
52+
4253
$this->app->make('Log1x\HtmlForms');
4354
}
4455
}

0 commit comments

Comments
 (0)