News: Show preview

main
Igor Scheller 4 years ago committed by msquare
parent f8d9cddcc6
commit a309d873a7

@ -51,12 +51,35 @@
{{ f.submit() }}
{{ f.submit(m.glyphicon('eye-close'), {'name': 'preview', 'btn_type': 'info'}) }}
{% if news %}
{{ f.submit(m.glyphicon('trash'), {'name': 'delete', 'btn_type': 'danger'}) }}
{% endif %}
</div>
</div>
{% if news %}
<div class="row">
<div class="col-md-12">
<h2>Preview</h2>
<div class="panel {% if not news.is_meeting %}panel-default{% else %}panel-info{% endif %}">
<div class="panel-heading">
<h3 class="panel-title">
{% if news.is_meeting %}{{ __('news.is_meeting') }}{% endif %}
{{ news.title }}
</h3>
</div>
<div class="panel-body">
{{ news.text|raw|nl2br }}
</div>
</div>
</div>
</div>
{% endif %}
</form>
</div>
{% endblock %}

@ -3,6 +3,7 @@
namespace Engelsystem\Controllers\Admin;
use Engelsystem\Controllers\BaseController;
use Engelsystem\Controllers\CleanupModel;
use Engelsystem\Controllers\HasUserNotifications;
use Engelsystem\Helpers\Authenticator;
use Engelsystem\Http\Redirector;
@ -14,6 +15,7 @@ use Psr\Log\LoggerInterface;
class NewsController extends BaseController
{
use HasUserNotifications;
use CleanupModel;
/** @var Authenticator */
protected $auth;
@ -58,6 +60,7 @@ class NewsController extends BaseController
/**
* @param Request $request
*
* @return Response
*/
public function edit(Request $request): Response
@ -66,6 +69,17 @@ class NewsController extends BaseController
$news = $this->news->find($id);
$isMeeting = $request->get('meeting', false);
return $this->showEdit($news, $isMeeting);
}
/**
* @param News|null $news
* @param bool $isMeetingDefault
*
* @return Response
*/
protected function showEdit(?News $news, bool $isMeetingDefault = false): Response
{
if (
$news
&& !$this->auth->can('admin_news_html')
@ -74,14 +88,22 @@ class NewsController extends BaseController
$this->addNotification('news.edit.contains-html', 'warnings');
}
if ($news) {
$this->cleanupModelNullValues($news);
}
return $this->response->withView(
'pages/news/edit.twig',
['news' => $news, 'is_meeting' => $news ? $news->is_meeting : $isMeeting] + $this->getNotifications(),
[
'news' => $news,
'is_meeting' => $news ? $news->is_meeting : $isMeetingDefault
] + $this->getNotifications(),
);
}
/**
* @param Request $request
*
* @return Response
*/
public function save(Request $request): Response
@ -95,6 +117,7 @@ class NewsController extends BaseController
'text' => 'required',
'is_meeting' => 'optional|checked',
'delete' => 'optional|checked',
'preview' => 'optional|checked',
]);
if (!is_null($data['delete'])) {
@ -123,6 +146,11 @@ class NewsController extends BaseController
$news->title = $data['title'];
$news->text = $data['text'];
$news->is_meeting = !is_null($data['is_meeting']);
if (!is_null($data['preview'])) {
return $this->showEdit($news);
}
$news->save();
$this->log->info(

@ -51,6 +51,7 @@ class NewsControllerTest extends TestCase
/**
* @covers \Engelsystem\Controllers\Admin\NewsController::edit
* @covers \Engelsystem\Controllers\Admin\NewsController::showEdit
*/
public function testEditHtmlWarning()
{
@ -224,6 +225,46 @@ class NewsControllerTest extends TestCase
$this->assertEquals($isMeeting, (bool)$news->is_meeting);
}
/**
* @covers \Engelsystem\Controllers\Admin\NewsController::save
*/
public function testSavePreview()
{
$this->request->attributes->set('id', 1);
$this->request = $this->request->withParsedBody([
'title' => 'New title',
'text' => 'New text',
'is_meeting' => '1',
'preview' => '1',
]);
$this->response->expects($this->once())
->method('withView')
->willReturnCallback(function ($view, $data) {
$this->assertEquals('pages/news/edit.twig', $view);
/** @var News $news */
$news = $data['news'];
// Contains new text
$this->assertTrue($news->is_meeting);
$this->assertEquals('New title', $news->title);
$this->assertEquals('New text', $news->text);
return $this->response;
});
/** @var NewsController $controller */
$controller = $this->app->make(NewsController::class);
$controller->setValidator(new Validator());
$controller->save($this->request);
// Assert no changes
$news = News::find(1);
$this->assertEquals('Foo', $news->title);
$this->assertEquals('<b>foo</b>', $news->text);
$this->assertFalse($news->is_meeting);
}
/**
* @covers \Engelsystem\Controllers\Admin\NewsController::save
*/

Loading…
Cancel
Save