News: Show preview

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

@ -51,12 +51,35 @@
{{ f.submit() }} {{ f.submit() }}
{{ f.submit(m.glyphicon('eye-close'), {'name': 'preview', 'btn_type': 'info'}) }}
{% if news %} {% if news %}
{{ f.submit(m.glyphicon('trash'), {'name': 'delete', 'btn_type': 'danger'}) }} {{ f.submit(m.glyphicon('trash'), {'name': 'delete', 'btn_type': 'danger'}) }}
{% endif %} {% endif %}
</div> </div>
</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> </form>
</div> </div>
{% endblock %} {% endblock %}

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

@ -51,6 +51,7 @@ class NewsControllerTest extends TestCase
/** /**
* @covers \Engelsystem\Controllers\Admin\NewsController::edit * @covers \Engelsystem\Controllers\Admin\NewsController::edit
* @covers \Engelsystem\Controllers\Admin\NewsController::showEdit
*/ */
public function testEditHtmlWarning() public function testEditHtmlWarning()
{ {
@ -224,6 +225,46 @@ class NewsControllerTest extends TestCase
$this->assertEquals($isMeeting, (bool)$news->is_meeting); $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 * @covers \Engelsystem\Controllers\Admin\NewsController::save
*/ */

Loading…
Cancel
Save