Skip to content

Commit

Permalink
feat: report preview is now rendered using the correct template (#46)
Browse files Browse the repository at this point in the history
Closes #45
  • Loading branch information
gearsdigital authored Nov 11, 2021
1 parent a6f495d commit 30de8b3
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 60 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
},
"require-dev": {
"phpunit/phpunit": "^9.5.10",
"getkirby/cms": "3.6.0-rc.4"
"getkirby/cms": "3.6.0-rc.4",
"mockery/mockery": "^1.4"
},
"scripts": {
"test": "phpunit --bootstrap tests/bootstrap.php tests"
Expand Down
229 changes: 176 additions & 53 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Kirby\Cms\Response;
use KirbyReporter\Model\FormData;
use KirbyReporter\Report\ReportClient;
use KirbyReporter\Report\ReportMailTemplateParser;
use KirbyReporter\Report\ReportTemplateParser;
use KirbyReporter\Vendor\IssueTracker;
use KirbyReporter\Vendor\Mail;
Expand Down Expand Up @@ -96,10 +97,16 @@
'action' => function () {
$requestData = kirby()->request()->body()->data();
$formData = new FormData($requestData);
if (isset($formData->getFormFields()['description'])) {
if (is_array(option('gearsdigital.reporter-for-kirby.repository'))) {
$parsedTemplate = (new class {
use ReportTemplateParser;
})->parseTemplate($formData->getFormFields());
})->parseTemplate($formData);

return new Response(json_encode(trim($parsedTemplate)), 'application/json');
} elseif ($type = option('gearsdigital.reporter-for-kirby.mail.type')) {
$parsedTemplate = (new class {
use ReportMailTemplateParser;
})->parseTemplate($formData, $type ?? 'text');

return new Response(json_encode(trim($parsedTemplate)), 'application/json');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Report/ReportClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ public final function createReport(FormData $formData): ReportResponse

// we need to parse the reporer template first because we need to send a plain 'string'
// to external APIs
return $this->client->report($formData, $this->parseTemplate($formData->getFormFields()));
return $this->client->report($formData, $this->parseTemplate($formData));
}
}
26 changes: 26 additions & 0 deletions lib/Report/ReportMailTemplateParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace KirbyReporter\Report;

use Kirby\Toolkit\Tpl;
use KirbyReporter\Model\FormData;

trait ReportMailTemplateParser
{
private string $pluginName = 'gearsdigital/reporter-for-kirby';

public function parseTemplate(FormData $formData, string $type): string
{
return Tpl::load($this->getReportTemplate($type), ['title' => $formData->getTitle(), 'fields' => $formData->getFormFields()]);
}

private function getReportTemplate(string $type): string
{
$template = "report.${type}.php";
$templateRoot = kirby()->root('templates').DS."emails";
$pluginRoot = kirby()->plugin($this->pluginName);
$templatePath = file_exists($templateRoot.DS.$template) ? $templateRoot : $pluginRoot->root().DS."templates/emails";

return $templatePath.DS."$template";
}
}
5 changes: 3 additions & 2 deletions lib/Report/ReportTemplateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace KirbyReporter\Report;

use Kirby\Toolkit\Tpl;
use KirbyReporter\Model\FormData;

trait ReportTemplateParser
{
private string $pluginName = 'gearsdigital/reporter-for-kirby';

public function parseTemplate(array $templateData): string
public function parseTemplate(FormData $formData): string
{
return Tpl::load($this->getReportTemplate(), ['fields' => $templateData]);
return Tpl::load($this->getReportTemplate(), ['title' => $formData->getTitle(), 'fields' => $formData->getFormFields()]);
}

private function getReportTemplate(): string
Expand Down
2 changes: 1 addition & 1 deletion templates/emails/report.html.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Issue Report from "<?= site()->title() ?? ''; ?>" by <?= kirby()->user()->nameOrEmail() ?? ''; ?>

---
<?= $title ?? ''; ?>
<b><?= $title ?? ''; ?></b>

---
<?= $fields['description'] ?? ''; ?>
47 changes: 47 additions & 0 deletions tests/Traits/ReportMailTemplateParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace KirbyReporter\Traits;

use Kirby\Toolkit\Tpl;
use KirbyReporter\Model\FormData;
use KirbyReporter\Report\ReportMailTemplateParser;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;

class ReportMailTemplateParserTest extends MockeryTestCase
{

private array $formData = [
'title' => 'Lorem',
'formFields' => [
'description' => 'Test',
],
];

public function test_parse_report_mail_text_template(): void
{
$mock = Mockery::mock('alias:'.Tpl::class);
$mock->shouldReceive('load')->andReturn('my-loaded-text-template');

$formData = new FormData($this->formData);
$parser = $this->getObjectForTrait(ReportMailTemplateParser::class);
$parsedTemplate = $parser->parseTemplate($formData, 'text');

$this->assertTrue(is_string($parsedTemplate));
$this->assertEquals('my-loaded-text-template', $parsedTemplate);
}

public function test_parse_report_mail_html_template(): void
{
$mock = Mockery::mock('alias:'.Tpl::class);
$mock->shouldReceive('load')->andReturn('my-loaded-html-template');

$formData = new FormData($this->formData);
$parser = $this->getObjectForTrait(ReportMailTemplateParser::class);
$parsedTemplate = $parser->parseTemplate($formData, 'html');

$this->assertTrue(is_string($parsedTemplate));
$this->assertEquals('my-loaded-html-template', $parsedTemplate);
}

}
Loading

0 comments on commit 30de8b3

Please sign in to comment.