*
* PHP version 5
*
- * Copyright © 2020 The Galette Team
+ * Copyright © 2020-2023 The Galette Team
*
* This file is part of Galette (http://galette.tuxfamily.org).
*
* @package Galette
*
* @author Johan Cwiklinski <johan@x-tnd.be>
- * @copyright 2020 The Galette Team
+ * @copyright 2020-2023 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-05-02
namespace Galette\Controllers\Crud;
+use Galette\Core\Galette;
+use Galette\IO\File;
+use Galette\Repository\DynamicFieldsSet;
+use Throwable;
use Galette\Controllers\CrudController;
-use Slim\Http\Request;
-use Slim\Http\Response;
+use Slim\Psr7\Request;
+use Slim\Psr7\Response;
use Galette\DynamicFields\DynamicField;
use Analog\Analog;
* @name DynamicFieldsController
* @package Galette
* @author Johan Cwiklinski <johan@x-tnd.be>
- * @copyright 2020 The Galette Team
+ * @copyright 2020-2023 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-05-02
/**
* Add page
*
- * @param Request $request PSR Request
- * @param Response $response PSR Response
- * @param array $args Request arguments
+ * @param Request $request PSR Request
+ * @param Response $response PSR Response
+ * @param string $form_name Form name
*
* @return Response
*/
- public function add(Request $request, Response $response, array $args = []): Response
+ public function add(Request $request, Response $response, string $form_name = null): Response
{
- $form_name = $args['form'];
-
- $df = null;
- if ($this->session->dynamicfieldtype) {
- $df = $this->session->dynamicfieldtype;
- $this->session->dynamicfieldtype = null;
- }
-
$params = [
'page_title' => _T("Add field"),
'form_name' => $form_name,
'action' => 'add',
'perm_names' => DynamicField::getPermsNames(),
- 'mode' => ($request->isXhr() ? 'ajax' : ''),
+ 'mode' => (($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') ? 'ajax' : ''),
'field_type_names' => DynamicField::getFieldsTypesNames()
];
+ if ($this->session->dynamicfieldtype) {
+ $params['df'] = $this->session->dynamicfieldtype;
+ $this->session->dynamicfieldtype = null;
+ }
+
// display page
$this->view->render(
$response,
- 'editer_champ.tpl',
+ 'pages/configuration_dynamic_field_form.html.twig',
$params
);
return $response;
/**
* Add action
*
- * @param Request $request PSR Request
- * @param Response $response PSR Response
- * @param array $args Request arguments
+ * @param Request $request PSR Request
+ * @param Response $response PSR Response
+ * @param string $form_name Form name
*
* @return Response
*/
- public function doAdd(Request $request, Response $response, array $args = []): Response
+ public function doAdd(Request $request, Response $response, string $form_name = null): Response
{
$post = $request->getParsedBody();
- $post['form'] = $args['form'];
+ $post['form_name'] = $form_name;
$error_detected = [];
$warning_detected = [];
+ if (isset($post['cancel'])) {
+ return $response
+ ->withStatus(301)
+ ->withHeader('Location', $this->cancelUri($this->getArgs($request)));
+ }
+
$df = DynamicField::getFieldType($this->zdb, $post['field_type']);
try {
$df->store($post);
$error_detected = $df->getErrors();
$warning_detected = $df->getWarnings();
- } catch (\Exception $e) {
+ } catch (Throwable $e) {
$msg = 'An error occurred adding new dynamic field.';
Analog::log(
$msg . ' | ' .
$e->getMessage(),
Analog::ERROR
);
- if (GALETTE_MODE == 'DEV') {
+ if (Galette::isDebugEnabled()) {
throw $e;
}
$error_detected[] = _T('An error occurred adding dynamic field :(');
->withStatus(301)
->withHeader(
'Location',
- $this->router->pathFor(
+ $this->routeparser->urlFor(
'addDynamicField',
- $args
+ ['form_name' => $form_name]
)
);
} else {
->withStatus(301)
->withHeader(
'Location',
- $this->router->pathFor(
+ $this->routeparser->urlFor(
'editDynamicField',
[
- 'form' => $args['form'],
+ 'form_name' => $form_name,
'id' => $df->getId()
]
)
->withStatus(301)
->withHeader(
'Location',
- $this->router->pathFor(
+ $this->routeparser->urlFor(
'configureDynamicFields',
- ['form' => $args['form']]
+ ['form_name' => $form_name]
)
);
}
/**
* List page
*
- * @param Request $request PSR Request
- * @param Response $response PSR Response
- * @param array $args Request arguments
+ * @param Request $request PSR Request
+ * @param Response $response PSR Response
+ * @param string $option One of 'page' or 'order'
+ * @param string|integer $value Value of the option
+ * @param string $form_name Form name
*
* @return Response
*/
- public function list(Request $request, Response $response, array $args = []): Response
- {
- $form_name = $args['form'] ?? 'adh';
- if (isset($_POST['form']) && trim($_POST['form']) != '') {
- $form_name = $_POST['form'];
+ public function list(
+ Request $request,
+ Response $response,
+ $option = null,
+ $value = null,
+ $form_name = 'adh'
+ ): Response {
+ if (isset($_POST['form_name']) && trim($_POST['form_name']) != '') {
+ $form_name = $_POST['form_name'];
}
- $fields = new \Galette\Repository\DynamicFieldsSet($this->zdb, $this->login);
- $fields_list = $fields->getList($form_name, $this->login);
-
- $field_type_names = DynamicField::getFieldsTypesNames();
+ $fields = new DynamicFieldsSet($this->zdb, $this->login);
+ $fields_list = $fields->getList($form_name);
$params = [
'fields_list' => $fields_list,
'form_name' => $form_name,
'form_title' => DynamicField::getFormTitle($form_name),
- 'page_title' => _T("Dynamic fields configuration")
+ 'page_title' => _T("Dynamic fields configuration"),
+ 'html_editor' => true,
+ 'html_editor_active' => $this->preferences->pref_editor_enabled
+
];
- $tpl = 'configurer_fiches.tpl';
+ $tpl = 'pages/configuration_dynamic_fields.html.twig';
//Render directly template if we called from ajax,
//render in a full page otherwise
if (
- $request->isXhr()
+ ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest')
|| isset($request->getQueryParams()['ajax'])
&& $request->getQueryParams()['ajax'] == 'true'
) {
- $tpl = 'configurer_fiche_content.tpl';
+ $tpl = 'elements/edit_dynamic_fields.html.twig';
} else {
$all_forms = DynamicField::getFormsNames();
$params['all_forms'] = $all_forms;
public function filter(Request $request, Response $response): Response
{
//no filtering
+ return $response;
+ }
+
+ /**
+ * Get a dynamic file
+ *
+ * @param Request $request PSR Request
+ * @param Response $response PSR Response
+ * @param string $form_name Form name
+ * @param integer $id Object ID
+ * @param integer $fid Dynamic fields ID
+ * @param integer $pos Dynamic field position
+ * @param string $name File name
+ *
+ * @return Response
+ */
+ public function getDynamicFile(
+ Request $request,
+ Response $response,
+ string $form_name,
+ int $id,
+ int $fid,
+ int $pos,
+ string $name
+ ): Response {
+ $object_class = DynamicFieldsSet::getClasses()[$form_name];
+ if ($object_class === 'Galette\Entity\Adherent') {
+ $object = new $object_class($this->zdb);
+ } else {
+ $object = new $object_class($this->zdb, $this->login);
+ }
+
+ $object
+ ->disableAllDeps()
+ ->enableDep('dynamics')
+ ->load($id);
+ $fields = $object->getDynamicFields()->getFields();
+ $field = $fields[$fid] ?? null;
+
+ $denied = null;
+ if (!$object->canShow($this->login)) {
+ if (!isset($fields[$fid])) {
+ //field does not exist or access is forbidden
+ $denied = true;
+ } else {
+ $denied = false;
+ }
+ }
+
+ if ($denied === true) {
+ $this->flash->addMessage(
+ 'error_detected',
+ _T("You do not have permission for requested URL.")
+ );
+
+ $route_name = 'member';
+ if ($form_name == 'contrib') {
+ $route_name = 'contribution';
+ } elseif ($form_name == 'trans') {
+ $route_name = 'transaction';
+ }
+ return $response
+ ->withHeader(
+ 'Location',
+ $this->routeparser->urlFor(
+ $route_name,
+ ['id' => $id]
+ )
+ );
+ }
+
+ $filename = $field->getFileName($id, $pos);
+
+ if ($form_name !== 'member' && !file_exists(GALETTE_FILES_PATH . $filename)) {
+ //handle old names for non adh dynamic files
+ $test_filename = $field->getFileName($id, $pos, 'member');
+ if (file_exists(GALETTE_FILES_PATH . $test_filename)) {
+ //rename old file to new name
+ rename(GALETTE_FILES_PATH . $test_filename, GALETTE_FILES_PATH . $filename);
+ }
+ }
+
+ if (file_exists(GALETTE_FILES_PATH . $filename)) {
+ $type = File::getMimeType(GALETTE_FILES_PATH . $filename);
+
+ $response = $response->withHeader('Content-Description', 'File Transfer')
+ ->withHeader('Content-Type', $type)
+ ->withHeader('Content-Disposition', 'attachment;filename="' . $name . '"')
+ ->withHeader('Pragma', 'no-cache')
+ ->withHeader('Content-Transfer-Encoding', 'binary')
+ ->withHeader('Expires', '0')
+ ->withHeader('Cache-Control', 'must-revalidate')
+ ->withHeader('Pragma', 'public');
+
+ $stream = fopen('php://memory', 'r+');
+ fwrite($stream, file_get_contents(GALETTE_FILES_PATH . $filename));
+ rewind($stream);
+
+ return $response->withBody(new \Slim\Psr7\Stream($stream));
+ } else {
+ Analog::log(
+ 'A request has been made to get a dynamic file named `' .
+ $filename . '` that does not exists.',
+ Analog::WARNING
+ );
+
+ $this->flash->addMessage(
+ 'error_detected',
+ _T("The file does not exists or cannot be read :(")
+ );
+
+ $route_name = 'member';
+ if ($form_name == 'contrib') {
+ $route_name = 'contribution';
+ } elseif ($form_name == 'trans') {
+ $route_name = 'transaction';
+ }
+
+ return $response
+ ->withHeader(
+ 'Location',
+ $this->routeparser->urlFor($route_name, ['id' => (string)$id])
+ );
+ }
}
// /CRUD - Read
/**
* Edit page
*
- * @param Request $request PSR Request
- * @param Response $response PSR Response
- * @param array $args Request arguments
+ * @param Request $request PSR Request
+ * @param Response $response PSR Response
+ * @param integer $id Dynamic field id
+ * @param string $form_name Form name
*
* @return Response
*/
- public function edit(Request $request, Response $response, array $args = []): Response
+ public function edit(Request $request, Response $response, int $id, $form_name = null): Response
{
- $id_dynf = (int)$args['id'];
- $form_name = $args['form'];
-
$df = null;
if ($this->session->dynamicfieldtype) {
$df = $this->session->dynamicfieldtype;
$this->session->dynamicfieldtype = null;
} else {
- $df = DynamicField::loadFieldType($this->zdb, $id_dynf);
+ $df = DynamicField::loadFieldType($this->zdb, $id);
if ($df === false) {
$this->flash->addMessage(
'error_detected',
);
return $response
->withStatus(301)
- ->withHeader('Location', $this->router->pathFor('configureDynamicFields'));
+ ->withHeader('Location', $this->routeparser->urlFor('configureDynamicFields'));
}
}
'action' => 'edit',
'form_name' => $form_name,
'perm_names' => DynamicField::getPermsNames(),
- 'mode' => ($request->isXhr() ? 'ajax' : ''),
- 'df' => $df
+ 'mode' => (($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') ? 'ajax' : ''),
+ 'df' => $df,
+ 'html_editor' => true,
+ 'html_editor_active' => $this->preferences->pref_editor_enabled
];
// display page
$this->view->render(
$response,
- 'editer_champ.tpl',
+ 'pages/configuration_dynamic_field_form.html.twig',
$params
);
return $response;
/**
* Edit action
*
- * @param Request $request PSR Request
- * @param Response $response PSR Response
- * @param array $args Request arguments
+ * @param Request $request PSR Request
+ * @param Response $response PSR Response
+ * @param integer $id Dynamic field id
+ * @param string $form_name Form name
*
* @return Response
*/
- public function doEdit(Request $request, Response $response, array $args = []): Response
+ public function doEdit(Request $request, Response $response, int $id = null, string $form_name = null): Response
{
$post = $request->getParsedBody();
- $post['form'] = $args['form'];
+ $post['form_name'] = $form_name;
+
+ if (isset($post['cancel'])) {
+ return $response
+ ->withStatus(301)
+ ->withHeader('Location', $this->cancelUri($this->getArgs($request)));
+ }
$error_detected = [];
$warning_detected = [];
- $field_id = (int)$args['id'];
+ $field_id = $id;
$df = DynamicField::loadFieldType($this->zdb, $field_id);
try {
$df->store($post);
$error_detected = $df->getErrors();
$warning_detected = $df->getWarnings();
- } catch (\Exception $e) {
+ } catch (Throwable $e) {
$msg = 'An error occurred storing dynamic field ' . $df->getId() . '.';
Analog::log(
$msg . ' | ' .
$e->getMessage(),
Analog::ERROR
);
- if (GALETTE_MODE == 'DEV') {
+ if (Galette::isDebugEnabled()) {
throw $e;
}
$error_detected[] = _T('An error occurred editing dynamic field :(');
->withStatus(301)
->withHeader(
'Location',
- $this->router->pathFor(
+ $this->routeparser->urlFor(
'editDynamicField',
- $args
+ [
+ 'form_name' => $form_name,
+ 'id' => $id
+ ]
)
);
} else {
->withStatus(301)
->withHeader(
'Location',
- $this->router->pathFor(
+ $this->routeparser->urlFor(
'configureDynamicFields',
- ['form' => $args['form']]
+ ['form_name' => $form_name]
)
);
}
*
* @return string
*/
- public function redirectUri(array $args = [])
+ public function redirectUri(array $args)
{
- return $this->router->pathFor('configureDynamicFields');
+ return $this->routeparser->urlFor('configureDynamicFields', ['form_name' => $args['form_name']]);
}
/**
*
* @return string
*/
- public function formUri(array $args = [])
+ public function formUri(array $args)
{
- return $this->router->pathFor(
+ return $this->routeparser->urlFor(
'doRemoveDynamicField',
- ['id' => $args['id'], 'form' => $args['form']]
+ ['id' => $args['id'], 'form_name' => $args['form_name']]
);
}
*
* @return string
*/
- public function confirmRemoveTitle(array $args = [])
+ public function confirmRemoveTitle(array $args)
{
$field = DynamicField::loadFieldType($this->zdb, (int)$args['id']);
if ($field === false) {
'error_detected',
_T("Requested field does not exists!")
);
- return $response
- ->withStatus(301)
- ->withHeader('Location', $this->router->pathFor('configureDynamicFields', ['form' => $args['form']]));
+ return _T("Requested field does not exists!");
}
return sprintf(
/**
* Move field
*
- * @param Request $request PSR Request
- * @param Response $response PSR Response
- * @param array $args Request arguments
+ * @param Request $request PSR Request
+ * @param Response $response PSR Response
+ * @param integer $id Field id
+ * @param string $form_name Form name
+ * @param string $direction One of DynamicField::MOVE_*
*
* @return Response
*/
- public function move(Request $request, Response $response, array $args = []): Response
- {
- $field_id = (int)$args['id'];
- $form_name = $args['form'];
-
- $field = DynamicField::loadFieldType($this->zdb, $field_id);
- if ($field->move($args['direction'])) {
+ public function move(
+ Request $request,
+ Response $response,
+ int $id,
+ string $form_name,
+ string $direction
+ ): Response {
+ $field = DynamicField::loadFieldType($this->zdb, $id);
+ if ($field->move($direction)) {
$this->flash->addMessage(
'success_detected',
_T("Field has been successfully moved")
return $response
->withStatus(301)
- ->withHeader('Location', $this->router->pathFor('configureDynamicFields', ['form' => $form_name]));
+ ->withHeader('Location', $this->routeparser->urlFor('configureDynamicFields', ['form_name' => $form_name]));
}
}