From: Johan Cwiklinski Date: Mon, 8 Jun 2020 06:47:46 +0000 (+0200) Subject: Move texts routes to a controller; closes #1354 X-Git-Tag: 0.9.4~52 X-Git-Url: https://git.agnieray.net/?a=commitdiff_plain;h=26cf4bf1c4c61ccdb6abb05b687203af0571737d;p=galette.git Move texts routes to a controller; closes #1354 --- diff --git a/galette/includes/routes/management.routes.php b/galette/includes/routes/management.routes.php index 56e80bc54..4a6484d90 100644 --- a/galette/includes/routes/management.routes.php +++ b/galette/includes/routes/management.routes.php @@ -42,6 +42,7 @@ use Galette\Controllers\Crud; use Galette\Controllers\PdfController; use Galette\Controllers\CsvController; use Galette\Controllers\AdminToolsController; +use Galette\Controllers\TextController; use Galette\DynamicFields\DynamicField; use Galette\Repository\Members; @@ -245,100 +246,17 @@ $app->post( $app->get( '/texts[/{lang}/{ref}]', - function ($request, $response, $args) { - if (!isset($args['lang'])) { - $args['lang'] = $this->preferences->pref_lang; - } - if (!isset($args['ref'])) { - $args['ref'] = Texts::DEFAULT_REF; - } - - $texts = new Texts( - $this->preferences, - $this->router - ); - - $mtxt = $texts->getTexts($args['ref'], $args['lang']); - - // display page - $this->view->render( - $response, - 'gestion_textes.tpl', - [ - 'page_title' => _T("Automatic emails texts edition"), - 'reflist' => $texts->getRefs($args['lang']), - 'langlist' => $this->i18n->getList(), - 'cur_lang' => $args['lang'], - 'cur_ref' => $args['ref'], - 'mtxt' => $mtxt, - ] - ); - return $response; - } + TextController::class . ':list' )->setName('texts')->add($authenticate); $app->post( '/texts/change', - function ($request, $response) { - $post = $request->getParsedBody(); - return $response - ->withStatus(301) - ->withHeader( - 'Location', - $this->router->pathFor( - 'texts', - [ - 'lang' => $post['sel_lang'], - 'ref' => $post['sel_ref'] - ] - ) - ); - } + TextController::class . ':change' )->setName('changeText')->add($authenticate); $app->post( '/texts', - function ($request, $response) { - $post = $request->getParsedBody(); - $texts = new Texts($this->preferences, $this->router); - - //set the language - $cur_lang = $post['cur_lang']; - //set the text entry - $cur_ref = $post['cur_ref']; - - $mtxt = $texts->getTexts($cur_ref, $cur_lang, $this->router); - $res = $texts->setTexts( - $cur_ref, - $cur_lang, - $post['text_subject'], - $post['text_body'] - ); - - if (!$res) { - $this->flash->addMessage( - 'error_detected', - preg_replace( - '(%s)', - $mtxt->tcomment, - _T("Email: '%s' has not been modified!") - ) - ); - } else { - $this->flash->addMessage( - 'success_detected', - preg_replace( - '(%s)', - $mtxt->tcomment, - _T("Email: '%s' has been successfully modified.") - ) - ); - } - - return $response - ->withStatus(301) - ->withHeader('Location', $this->router->pathFor('texts')); - } + TextController::class . ':edit' )->setName('texts')->add($authenticate); $app->get( diff --git a/galette/lib/Galette/Controllers/TextController.php b/galette/lib/Galette/Controllers/TextController.php new file mode 100644 index 000000000..8e9e7199f --- /dev/null +++ b/galette/lib/Galette/Controllers/TextController.php @@ -0,0 +1,178 @@ +. + * + * @category Controllers + * @package Galette + * + * @author Johan Cwiklinski + * @copyright 2020 The Galette Team + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version + * @version SVN: $Id$ + * @link http://galette.tuxfamily.org + * @since Available since 0.9.4dev - 2020-06-08 + */ + +namespace Galette\Controllers; + +use Slim\Http\Request; +use Slim\Http\Response; +use Galette\Entity\Texts; +use Analog\Analog; + +/** + * Galette texts controller + * + * @category Controllers + * @name TextController + * @package Galette + * @author Johan Cwiklinski + * @copyright 2020 The Galette Team + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version + * @link http://galette.tuxfamily.org + * @since Available since 0.9.4dev - 2020-06-08 + */ + +class TextController extends AbstractController +{ + /** + * List texts + * + * @param Request $request PSR Request + * @param Response $response PSR Response + * @param array $args Request arguments ['r'] + * + * @return void + */ + public function list(Request $request, Response $response, array $args = []) + { + if (!isset($args['lang'])) { + $args['lang'] = $this->preferences->pref_lang; + } + if (!isset($args['ref'])) { + $args['ref'] = Texts::DEFAULT_REF; + } + + $texts = new Texts( + $this->preferences, + $this->router + ); + + $mtxt = $texts->getTexts($args['ref'], $args['lang']); + + // display page + $this->view->render( + $response, + 'gestion_textes.tpl', + [ + 'page_title' => _T("Automatic emails texts edition"), + 'reflist' => $texts->getRefs($args['lang']), + 'langlist' => $this->i18n->getList(), + 'cur_lang' => $args['lang'], + 'cur_ref' => $args['ref'], + 'mtxt' => $mtxt, + ] + ); + return $response; + } + + /** + * Change texts + * + * @param Request $request PSR Request + * @param Response $response PSR Response + * @param array $args Request arguments ['r'] + * + * @return void + */ + public function change(Request $request, Response $response, array $args = []) + { + $post = $request->getParsedBody(); + return $response + ->withStatus(301) + ->withHeader( + 'Location', + $this->router->pathFor( + 'texts', + [ + 'lang' => $post['sel_lang'], + 'ref' => $post['sel_ref'] + ] + ) + ); + } + + /** + * Edit text + * + * @param Request $request PSR Request + * @param Response $response PSR Response + * @param array $args Request arguments ['r'] + * + * @return void + */ + public function edit(Request $request, Response $response, array $args = []) + { + $post = $request->getParsedBody(); + $texts = new Texts($this->preferences, $this->router); + + //set the language + $cur_lang = $post['cur_lang']; + //set the text entry + $cur_ref = $post['cur_ref']; + + $mtxt = $texts->getTexts($cur_ref, $cur_lang, $this->router); + $res = $texts->setTexts( + $cur_ref, + $cur_lang, + $post['text_subject'], + $post['text_body'] + ); + + if (!$res) { + $this->flash->addMessage( + 'error_detected', + preg_replace( + '(%s)', + $mtxt->tcomment, + _T("Email: '%s' has not been modified!") + ) + ); + } else { + $this->flash->addMessage( + 'success_detected', + preg_replace( + '(%s)', + $mtxt->tcomment, + _T("Email: '%s' has been successfully modified.") + ) + ); + } + + return $response + ->withStatus(301) + ->withHeader('Location', $this->router->pathFor('texts')); + } +}