*
* 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
- * @version SVN: $Id$
* @link http://galette.tuxfamily.org
* @since Available since 0.9.4dev - 2020-05-08
*/
namespace Galette\Controllers\Crud;
+use Galette\Features\BatchList;
+use Analog\Analog;
use Galette\Controllers\CrudController;
-
-use Slim\Http\Request;
-use Slim\Http\Response;
+use Slim\Psr7\Request;
+use Slim\Psr7\Response;
use Galette\Entity\Adherent;
use Galette\Entity\Contribution;
use Galette\Entity\Transaction;
-
-
-
-use Galette\Repository\Contributions;
-use Galette\Repository\Transactions;
use Galette\Repository\Members;
use Galette\Entity\ContributionsTypes;
-use Galette\Core\GaletteMail;
-use Galette\Entity\Texts;
-use Galette\IO\PdfMembersCards;
use Galette\Repository\PaymentTypes;
-use Galette\Core\Links;
-
-
-
-use Analog\Analog;
/**
* Galette contributions controller
* @name ContributionsController
* @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
class ContributionsController extends CrudController
{
+ use BatchList;
+
// CRUD - Create
/**
*
* @param Request $request PSR Request
* @param Response $response PSR Response
- * @param array $args Request arguments
+ * @param string $type Contribution type
* @param Contribution $contrib Contribution instance
*
* @return Response
public function addEditPage(
Request $request,
Response $response,
- array $args,
+ string $type,
Contribution $contrib
- ) :Response {
- $get = $request->getQueryParams();
+ ): Response {
+ $post = $request->getParsedBody();
+
+ // check for ajax mode
+ $ajax = false;
+ if (
+ ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest')
+ || isset($post['ajax'])
+ && $post['ajax'] == 'true'
+ ) {
+ $ajax = true;
+ }
// contribution types
$ct = new ContributionsTypes($this->zdb);
- $contributions_types = $ct->getList($args['type'] === 'fee');
-
- $disabled = array();
-
- if (!is_int($contrib->id)) {
- // initialiser la structure contribution à vide (nouvelle contribution)
- $contribution['duree_mois_cotis'] = $this->preferences->pref_membership_ext;
- }
+ $contributions_types = $ct->getList($type === Contribution::TYPE_FEE);
// template variable declaration
$title = null;
- if ($args['type'] === 'fee') {
+ if ($type === Contribution::TYPE_FEE) {
$title = _T("Membership fee");
} else {
$title = _T("Donation");
$title .= ' (' . _T("creation") . ')';
}
- // required fields
- $required = [
- 'id_type_cotis' => 1,
- 'id_adh' => 1,
- 'date_enreg' => 1,
- 'date_debut_cotis' => 1,
- 'date_fin_cotis' => $contrib->isCotis(),
- 'montant_cotis' => $contrib->isCotis() ? 1 : 0
- ];
-
$params = [
'page_title' => $title,
- 'required' => $required,
- 'disabled' => $disabled,
+ 'required' => $contrib->getRequired(),
'contribution' => $contrib,
'adh_selected' => $contrib->member,
- 'type' => $args['type']
+ 'type' => $type
];
// contribution types
// members
$m = new Members();
- $members = $m->getSelectizedMembers(
+ $members = $m->getDropdownMembers(
$this->zdb,
- isset($contrib) && $contrib->member > 0 ? $contrib->member : null
+ $this->login,
+ $contrib->member > 0 ? $contrib->member : null
);
$params['members'] = [
}
$ext_membership = '';
- if (isset($contrib) && $contrib->isCotis() || !isset($contrib) && $args['type'] === 'fee') {
+ if ($contrib->isFee() || $type === Contribution::TYPE_FEE) {
$ext_membership = $this->preferences->pref_membership_ext;
}
$params['pref_membership_ext'] = $ext_membership;
$params['autocomplete'] = true;
+ $params['mode'] = ($ajax ? 'ajax' : '');
// display page
$this->view->render(
$response,
- 'ajouter_contribution.tpl',
+ 'pages/contribution_form.html.twig',
$params
);
return $response;
/**
* 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|null $type Contribution type
*
* @return Response
*/
- public function add(Request $request, Response $response, array $args = []) :Response
+ public function add(Request $request, Response $response, string $type = null): Response
{
if ($this->session->contribution !== null) {
$contrib = $this->session->contribution;
$get = $request->getQueryParams();
$ct = new ContributionsTypes($this->zdb);
- $contributions_types = $ct->getList($args['type'] === 'fee');
+ $contributions_types = $ct->getList($type === Contribution::TYPE_FEE);
$cparams = ['type' => array_keys($contributions_types)[0]];
}
//transaction id
- $trans_id = null;
if (isset($get[Transaction::PK]) && $get[Transaction::PK] > 0) {
$cparams['trans'] = $get[Transaction::PK];
}
$contrib = new Contribution(
$this->zdb,
$this->login,
- (count($cparams) > 0 ? $cparams : null)
+ $cparams
);
if (isset($cparams['adh'])) {
}
}
- return $this->addEditPage($request, $response, $args, $contrib);
+ return $this->addEditPage($request, $response, $type, $contrib);
}
/**
* Add action
*
+ * @param Request $request PSR Request
+ * @param Response $response PSR Response
+ * @param string|null $type Contribution type
+ *
+ * @return Response
+ */
+ public function doAdd(Request $request, Response $response, string $type = null): Response
+ {
+ return $this->store($request, $response, 'add', $type);
+ }
+
+ /**
+ * Choose contribution type to mass add contribution
+ *
* @param Request $request PSR Request
* @param Response $response PSR Response
- * @param array $args Request arguments
*
* @return Response
*/
- public function doAdd(Request $request, Response $response, array $args = []) :Response
+ public function massAddChooseType(Request $request, Response $response): Response
{
- $args['action'] = 'add';
- return $this->store($request, $response, $args);
+ $filters = $this->session->filter_members;
+ $data = [
+ 'id' => $filters->selected,
+ 'redirect_uri' => $this->routeparser->urlFor('members')
+ ];
+
+ // display page
+ $this->view->render(
+ $response,
+ 'modals/mass_choose_contributions_type.html.twig',
+ array(
+ 'mode' => ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') ? 'ajax' : '',
+ 'page_title' => str_replace(
+ '%count',
+ count($data['id']),
+ _T('Mass add contribution on %count members')
+ ),
+ 'data' => $data,
+ 'form_url' => $this->routeparser->urlFor('massAddContributions'),
+ 'cancel_uri' => $this->routeparser->urlFor('members')
+ )
+ );
+ return $response;
+ }
+
+ /**
+ * Massive change page
+ *
+ * @param Request $request PSR Request
+ * @param Response $response PSR Response
+ *
+ * @return Response
+ */
+ public function massAddContributions(Request $request, Response $response): Response
+ {
+ $post = $request->getParsedBody();
+ $filters = $this->session->filter_members;
+ $type = $post['type'];
+
+ $ct = new ContributionsTypes($this->zdb);
+ $contributions_types = $ct->getList($type === Contribution::TYPE_FEE);
+
+ $contribution = new Contribution(
+ $this->zdb,
+ $this->login,
+ ['type' => array_keys($contributions_types)[0]]
+ );
+
+ $data = [
+ 'id' => $filters->selected,
+ 'redirect_uri' => $this->routeparser->urlFor('members'),
+ 'type' => $type
+ ];
+
+ // display page
+ $this->view->render(
+ $response,
+ 'modals/mass_add_contributions.html.twig',
+ array(
+ 'mode' => ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') ? 'ajax' : '',
+ 'page_title' => str_replace(
+ '%count',
+ count($data['id']),
+ _T('Mass add contribution on %count members')
+ ),
+ 'form_url' => $this->routeparser->urlFor('doMassAddContributions'),
+ 'cancel_uri' => $this->routeparser->urlFor('members'),
+ 'data' => $data,
+ 'contribution' => $contribution,
+ 'type' => $type,
+ 'require_mass' => true,
+ 'required' => $contribution->getRequired(),
+ 'type_cotis_options' => $contributions_types
+ )
+ );
+ return $response;
+ }
+
+ /**
+ * Do massive contribution add
+ *
+ * @param Request $request PSR Request
+ * @param Response $response PSR Response
+ *
+ * @return Response
+ */
+ public function doMassAddContributions(Request $request, Response $response): Response
+ {
+ $post = $request->getParsedBody();
+ $members_ids = $post['id'];
+ unset($post['id']);
+
+ $error_detected = [];
+
+ // flagging required fields for first step only
+ $disabled = [];
+ $success = 0;
+ $errors = 0;
+
+ foreach ($members_ids as $member_id) {
+ $post[Adherent::PK] = (int)$member_id;
+ $contrib = new Contribution($this->zdb, $this->login);
+ $contrib->disableEvents();
+
+ // regular fields
+ $valid = $contrib->check($post, $contrib->getRequired(), $disabled);
+ if ($valid !== true) {
+ $error_detected = array_merge($error_detected, $valid);
+ }
+
+ //all goes well, we can proceed
+ if (count($error_detected) == 0) {
+ $store = $contrib->store();
+ if ($store === true) {
+ ++$success;
+ $files_res = $contrib->handleFiles($_FILES);
+ if (is_array($files_res)) {
+ $error_detected = array_merge($error_detected, $files_res);
+ }
+ } else {
+ ++$errors;
+ }
+ }
+ }
+
+ if (count($error_detected) == 0) {
+ $redirect_url = $this->routeparser->urlFor('members');
+ } else {
+ //something went wrong.
+ //store entity in session
+ $redirect_url = $this->routeparser->urlFor('massAddContributions');
+ //report errors
+ foreach ($error_detected as $error) {
+ $this->flash->addMessage(
+ 'error_detected',
+ $error
+ );
+ }
+ }
+
+ //redirect to calling action
+ return $response
+ ->withStatus(301)
+ ->withHeader('Location', $redirect_url);
}
// /CRUD - Create
/**
* 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 $type One of 'transactions' or 'contributions'
*
* @return Response
*/
- public function list(Request $request, Response $response, array $args = []) :Response
+ public function list(Request $request, Response $response, $option = null, $value = null, $type = null): Response
{
$ajax = false;
- if ($request->isXhr()
- || isset($request->getQueryParams()['ajax'])
- && $request->getQueryParams()['ajax'] == 'true'
- ) {
- $ajax = true;
- }
$get = $request->getQueryParams();
- $option = $args['option'] ?? null;
- $value = $args['value'] ?? null;
- $raw_type = null;
-
- switch ($args['type']) {
+ switch ($type) {
case 'transactions':
$raw_type = 'transactions';
break;
case 'contributions':
$raw_type = 'contributions';
break;
+ default:
+ Analog::log(
+ 'Trying to load unknown contribution type ' . $type,
+ Analog::WARNING
+ );
+ return $response
+ ->withStatus(301)
+ ->withHeader(
+ 'Location',
+ $this->routeparser->urlFor('me')
+ );
}
$filter_name = 'filter_' . $raw_type;
+ if (
+ ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest')
+ || isset($get['ajax'])
+ && $get['ajax'] == 'true'
+ ) {
+ $ajax = true;
+ $filter_name .= '_ajax';
+ }
- if (isset($this->session->$filter_name) && $ajax === false) {
+ if (isset($this->session->$filter_name)) {
$filters = $this->session->$filter_name;
} else {
$filter_class = '\\Galette\\Filters\\' . ucwords($raw_type . 'List');
$filters->filtre_cotis_adh = (int)$get[Adherent::PK];
}
- $max_amount = null;
- if (isset($request->getQueryParams()['max_amount'])) {
- $filters->filtre_transactions = true;
- $filters->max_amount = (int)$request->getQueryParams()['max_amount'];
- } else {
- $filters->filtre_transactions = false;
- $filters->max_amount = null;
+ if ($type === 'contributions') {
+ if (isset($request->getQueryParams()['max_amount'])) {
+ $filters->filtre_transactions = true;
+ $filters->max_amount = (int)$request->getQueryParams()['max_amount'];
+ }
}
if ($option !== null) {
$filters->orderby = $value;
break;
case 'member':
- if (($this->login->isAdmin()
- || $this->login->isStaff())
- ) {
- if ($value == 'all') {
- $filters->filtre_cotis_adh = null;
- } else {
- $filters->filtre_cotis_adh = $value;
- }
- }
+ $filters->filtre_cotis_adh = ($value === 'all' ? null : $value);
break;
}
}
- if (!$this->login->isAdmin() && !$this->login->isStaff()) {
- $filters->filtre_cotis_adh = $this->login->id;
+ if (!$this->login->isAdmin() && !$this->login->isStaff() && $value != $this->login->id) {
+ if ($value === 'all' || empty($value)) {
+ $value = $this->login->id;
+ } else {
+ $member = new Adherent(
+ $this->zdb,
+ (int)$value,
+ [
+ 'picture' => false,
+ 'groups' => false,
+ 'dues' => false,
+ 'parent' => true
+ ]
+ );
+ if (
+ !$member->hasParent() ||
+ $member->parent->id != $this->login->id
+ ) {
+ $value = $this->login->id;
+ Analog::log(
+ 'Trying to display contributions for member #' . $value .
+ ' without appropriate ACLs',
+ Analog::WARNING
+ );
+ }
+ }
+ $filters->filtre_cotis_children = $value;
+ }
+
+ $class = '\\Galette\\Entity\\' . ucwords(trim($raw_type, 's'));
+ $contrib = new $class($this->zdb, $this->login);
+
+ if (!$contrib->canShow($this->login)) {
+ Analog::log(
+ 'Trying to display contributions without appropriate ACLs',
+ Analog::WARNING
+ );
+ return $response
+ ->withStatus(301)
+ ->withHeader(
+ 'Location',
+ $this->routeparser->urlFor('me')
+ );
}
$class = '\\Galette\\Repository\\' . ucwords($raw_type);
}
//assign pagination variables to the template and add pagination links
- $filters->setSmartyPagination($this->router, $this->view->getSmarty());
+ $filters->setSmartyPagination($this->routeparser, $this->view);
$tpl_vars = [
'page_title' => $raw_type === 'contributions' ?
- _T("Contributions management") :
- _T("Transactions management"),
+ _T("Contributions management") : _T("Transactions management"),
'contribs' => $contrib,
'list' => $contribs_list,
'nb' => $contrib->getCount(),
if ($filters->filtre_cotis_adh != null) {
$member = new Adherent($this->zdb);
+ $member->enableDep('children');
$member->load($filters->filtre_cotis_adh);
$tpl_vars['member'] = $member;
}
+ if ($filters->filtre_cotis_children != false) {
+ $member = new Adherent(
+ $this->zdb,
+ $filters->filtre_cotis_children,
+ [
+ 'picture' => false,
+ 'groups' => false,
+ 'dues' => false,
+ 'parent' => true
+ ]
+ );
+ $tpl_vars['pmember'] = $member;
+ }
+
+ // hide column action in ajax mode
+ if ($ajax === true) {
+ $tpl_vars['no_action'] = true;
+ }
+
// display page
$this->view->render(
$response,
- 'gestion_' . $raw_type . '.tpl',
+ 'pages/' . $raw_type . '_list.html.twig',
$tpl_vars
);
return $response;
}
/**
- * Filtering
+ * List page for logged-in member
*
* @param Request $request PSR Request
* @param Response $response PSR Response
- * @param array $args Request arguments
+ * @param string $type One of 'transactions' or 'contributions'
*
* @return Response
*/
- public function filter(Request $request, Response $response, array $args = []) :Response
+ public function myList(Request $request, Response $response, string $type = null): Response
{
- $raw_type = null;
- switch ($args['type']) {
- case 'transactions':
- $raw_type = 'transactions';
- break;
- case 'contributions':
- $raw_type = 'contributions';
- break;
+ return $this->list(
+ $request->withQueryParams(
+ $request->getQueryParams() + [
+ Adherent::PK => $this->login->id
+ ]
+ ),
+ $response,
+ null,
+ null,
+ $type
+ );
+ }
+
+ /**
+ * Filtering
+ *
+ * @param Request $request PSR Request
+ * @param Response $response PSR Response
+ * @param string|null $type One of 'transactions' or 'contributions'
+ *
+ * @return Response
+ */
+ public function filter(Request $request, Response $response, string $type = null): Response
+ {
+ $ajax = false;
+ $filter_name = 'filter_' . $type;
+ if ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') {
+ $ajax = true;
+ $filter_name .= '_ajax';
}
- $type = 'filter_' . $raw_type;
$post = $request->getParsedBody();
$error_detected = [];
- if ($this->session->$type !== null) {
- $filters = $this->session->$type;
+ if ($this->session->$filter_name !== null) {
+ $filters = $this->session->$filter_name;
} else {
- $filter_class = '\\Galette\\Filters\\' . ucwords($raw_type) . 'List';
+ $filter_class = '\\Galette\\Filters\\' . ucwords($type) . 'List';
$filters = new $filter_class();
}
if (isset($post['clear_filter'])) {
- $filters->reinit();
+ $filters->reinit($ajax);
} else {
- if (isset($post['max_amount'])) {
+ if (!isset($post['max_amount'])) {
$filters->max_amount = null;
}
- if ((isset($post['nbshow']) && is_numeric($post['nbshow']))
+ if (
+ (isset($post['nbshow']) && is_numeric($post['nbshow']))
) {
$filters->show = $post['nbshow'];
}
+ if (isset($post['date_field'])) {
+ $filters->date_field = $post['date_field'];
+ }
+
if (isset($post['end_date_filter']) || isset($post['start_date_filter'])) {
- try {
- if (isset($post['start_date_filter'])) {
- $field = _T("start date filter");
- $filters->start_date_filter = $post['start_date_filter'];
- }
- if (isset($post['end_date_filter'])) {
- $field = _T("end date filter");
- $filters->end_date_filter = $post['end_date_filter'];
- }
- } catch (Exception $e) {
- $error_detected[] = $e->getMessage();
+ if (isset($post['start_date_filter'])) {
+ $filters->start_date_filter = $post['start_date_filter'];
+ }
+ if (isset($post['end_date_filter'])) {
+ $filters->end_date_filter = $post['end_date_filter'];
}
}
}
}
- $this->session->$type = $filters;
+ $this->session->$filter_name = $filters;
if (count($error_detected) > 0) {
//report errors
return $response
->withStatus(301)
- ->withHeader('Location', $this->router->pathFor('contributions', ['type' => $raw_type]));
+ ->withHeader('Location', $this->routeparser->urlFor('contributions', ['type' => $type]));
+ }
+
+ /**
+ * Batch actions handler
+ *
+ * @param Request $request PSR Request
+ * @param Response $response PSR Response
+ * @param string $type One of 'transactions' or 'contributions'
+ *
+ * @return Response
+ */
+ public function handleBatch(Request $request, Response $response, string $type): Response
+ {
+ $filter_name = 'filter_' . $type;
+ $post = $request->getParsedBody();
+
+ if (isset($post['entries_sel'])) {
+ $filter_class = '\\Galette\\Filters\\' . ucwords($type . 'List');
+ $filters = $this->session->$filter_name ?? new $filter_class();
+ $filters->selected = $post['entries_sel'];
+ $this->session->$filter_name = $filters;
+
+ if (isset($post['csv'])) {
+ return $response
+ ->withStatus(301)
+ ->withHeader('Location', $this->routeparser->urlFor('csv-contributionslist', ['type' => $type]));
+ }
+
+ if (isset($post['delete'])) {
+ return $response
+ ->withStatus(301)
+ ->withHeader('Location', $this->routeparser->urlFor('removeContributions', ['type' => $type]));
+ }
+
+ throw new \RuntimeException('Does not know what to batch :(');
+ } else {
+ $this->flash->addMessage(
+ 'error_detected',
+ _T("No contribution was selected, please check at least one.")
+ );
+
+ return $response
+ ->withStatus(301)
+ ->withHeader('Location', $this->routeparser->urlFor('contributions', ['type' => $type]));
+ }
}
// /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 int $id Contribution id
+ * @param string|null $type Contribution type
*
* @return Response
*/
- public function edit(Request $request, Response $response, array $args = []) :Response
+ public function edit(Request $request, Response $response, int $id, string $type = null): Response
{
if ($this->session->contribution !== null) {
$contrib = $this->session->contribution;
$this->session->contribution = null;
} else {
- $contrib = new Contribution($this->zdb, $this->login, (int)$args['id']);
+ $contrib = new Contribution($this->zdb, $this->login, $id);
if ($contrib->id == '') {
//not possible to load contribution, exit
$this->flash->addMessage(
'error_detected',
str_replace(
'%id',
- $args['id'],
+ $id,
_T("Unable to load contribution #%id!")
)
);
return $response
->withStatus(301)
- ->withHeader('Location', $this->router->pathFor(
+ ->withHeader('Location', $this->routeparser->urlFor(
'contributions',
['type' => 'contributions']
));
}
}
- return $this->addEditPage($request, $response, $args, $contrib);
+ return $this->addEditPage($request, $response, $type, $contrib);
}
/**
* 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 Contribution id
+ * @param string|null $type Contribution type
*
* @return Response
*/
- public function doEdit(Request $request, Response $response, array $args = []) :Response
+ public function doEdit(Request $request, Response $response, int $id, string $type = null): Response
{
- $args['action'] = 'edit';
- return $this->store($request, $response, $args);
+ return $this->store($request, $response, 'edit', $type, $id);
}
/**
*
* @param Request $request PSR Request
* @param Response $response PSR Response
- * @param array $args Request arguments
+ * @param string $action Action ('edit' or 'add')
+ * @param string $type Contribution type
+ * @param integer $id Contribution id
*
* @return Response
*/
- public function store(Request $request, Response $response, array $args = []) :Response
+ public function store(Request $request, Response $response, $action, string $type, $id = null): Response
{
$post = $request->getParsedBody();
- $action = $args['action'];
+ $url_args = [
+ 'action' => $action,
+ 'type' => $type
+ ];
+ if ($id !== null) {
+ $url_args['id'] = $id;
+ }
if ($action == 'edit' && isset($post['btnreload'])) {
- $redirect_url = $this->router->pathFor($action . 'Contribution', $args);
+ $redirect_url = $this->routeparser->urlFor($action . 'Contribution', $url_args);
$redirect_url .= '?' . Adherent::PK . '=' . $post[Adherent::PK] . '&' .
ContributionsTypes::PK . '=' . $post[ContributionsTypes::PK] . '&' .
'montant_cotis=' . $post['montant_cotis'];
->withHeader('Location', $redirect_url);
}
- $success_detected = [];
$error_detected = [];
- $warning_detected = [];
- $redirect_url = null;
-
- $id_cotis = null;
- if (isset($args['id'])) {
- $id_cotis = $args['id'];
- }
-
- $id_adh = $post['id_adh'];
if ($this->session->contribution !== null) {
$contrib = $this->session->contribution;
$this->session->contribution = null;
} else {
- if ($id_cotis === null) {
+ if ($id === null) {
$contrib = new Contribution($this->zdb, $this->login);
} else {
- $contrib = new Contribution($this->zdb, $this->login, (int)$id_cotis);
+ $contrib = new Contribution($this->zdb, $this->login, $id);
}
}
- // flagging required fields for first step only
- $required = [
- 'id_type_cotis' => 1,
- 'id_adh' => 1,
- 'date_enreg' => 1,
- 'montant_cotis' => 1, //TODO: not always required, see #196
- 'date_debut_cotis' => 1,
- 'date_fin_cotis' => ($args['type'] === 'fee')
- ];
$disabled = [];
// regular fields
- $valid = $contrib->check($post, $required, $disabled);
+ $valid = $contrib->check($post, $contrib->getRequired(), $disabled);
if ($valid !== true) {
$error_detected = array_merge($error_detected, $valid);
}
- if (count($error_detected) == 0) {
- //all goes well, we can proceed
- $new = false;
- if ($contrib->id == '') {
- $new = true;
- }
-
- if (count($error_detected) == 0) {
- $store = $contrib->store();
- if ($store === true) {
- $success_detected[] = _T('Contribution has been successfully stored');
- //contribution has been stored :)
- if ($new) {
- //if an external script has been configured, we call it
- if ($this->preferences->pref_new_contrib_script) {
- $es = new \Galette\IO\ExternalScript($this->preferences);
- $res = $contrib->executePostScript($es);
-
- if ($res !== true) {
- //send admin an email with all details
- if ($this->preferences->pref_mail_method > GaletteMail::METHOD_DISABLED) {
- $mail = new GaletteMail($this->preferences);
- $mail->setSubject(
- _T("Post contribution script failed")
- );
-
- $recipients = [];
- foreach ($this->preferences->vpref_email_newadh as $pref_email) {
- $recipients[$pref_email] = $pref_email;
- }
- $mail->setRecipients($recipients);
-
- $message = _T("The configured post contribution script has failed.");
- $message .= "\n" . _T("You can find contribution information and script output below.");
- $message .= "\n\n";
- $message .= $res;
-
- $mail->setMessage($message);
- $sent = $mail->send();
-
- if (!$sent) {
- $txt = preg_replace(
- array('/%name/', '/%email/'),
- array($adh->sname, $adh->getEmail()),
- _T("A problem happened while sending to admin post contribution notification for user %name (%email) contribution")
- );
- $this->history->add($txt);
- $warning_detected[] = $txt;
- //Mails are disabled... We log (not safe, but)...
- Analog::log(
- 'Email to admin has not been sent. Here was the data: ' .
- "\n" . print_r($res, true),
- Analog::ERROR
- );
- }
- } else {
- //Mails are disabled... We log (not safe, but)...
- Analog::log(
- 'Post contribution script has failed. Here was the data: ' .
- "\n" . print_r($res, true),
- Analog::ERROR
- );
- }
- }
- }
- }
- } else {
- //something went wrong :'(
- $error_detected[] = _T("An error occurred while storing the contribution.");
- }
- }
+ // send email to member
+ if (isset($post['mail_confirm']) && $post['mail_confirm'] == '1') {
+ $contrib->setSendmail(); //flag to send creation email
}
+ //all goes well, we can proceed
if (count($error_detected) == 0) {
- // Get member information
- $adh = new Adherent($this->zdb);
- $adh->load($contrib->member);
-
- if ($this->preferences->pref_mail_method > GaletteMail::METHOD_DISABLED) {
- $texts = new Texts(
- $this->preferences,
- $this->router,
- array(
- 'name_adh' => custom_html_entity_decode($adh->sname),
- 'firstname_adh' => custom_html_entity_decode($adh->surname),
- 'lastname_adh' => custom_html_entity_decode($adh->name),
- 'mail_adh' => custom_html_entity_decode($adh->getEmail()),
- 'login_adh' => custom_html_entity_decode($adh->login),
- 'deadline' => custom_html_entity_decode($contrib->end_date),
- 'contrib_info' => custom_html_entity_decode($contrib->info),
- 'contrib_amount' => custom_html_entity_decode($contrib->amount),
- 'contrib_type' => custom_html_entity_decode($contrib->type->libelle)
- )
+ $store = $contrib->store();
+ if ($store === true) {
+ $this->flash->addMessage(
+ 'success_detected',
+ _T('Contribution has been successfully stored')
);
- if ($new && isset($_POST['mail_confirm'])
- && $_POST['mail_confirm'] == '1'
- ) {
- if (GaletteMail::isValidEmail($adh->getEmail())) {
- $text = 'contrib';
- if (!$contrib->isCotis()) {
- $text = 'donation';
- }
- $mtxt = $texts->getTexts($text, $adh->language);
-
- $mail = new GaletteMail($this->preferences);
- $mail->setSubject($texts->getSubject());
- $mail->setRecipients(
- array(
- $adh->getEmail() => $adh->sname
- )
- );
-
- $link_card = '';
- if (strpos($mtxt->tbody, '{LINK_MEMBERCARD}') !== false) {
- //member card link is present in mail model, let's add it
- $links = new Links($this->zdb);
- if ($hash = $links->generateNewLink(Links::TARGET_MEMBERCARD, $contrib->member)) {
- $link_card = $this->preferences->getURL() .
- $this->router->pathFor('directlink', ['hash' => $hash]);
- }
- }
-
- $link_pdf = '';
- if (strpos($mtxt->tbody, '{LINK_MEMBERCARD}') !== false) {
- //contribution receipt link is present in mail model, let's add it
- $links = new Links($this->zdb);
- $ltype = $contrib->type->isExtension() ? Links::TARGET_INVOICE : Links::TARGET_RECEIPT;
- if ($hash = $links->generateNewLink($ltype, $contrib->id)) {
- $link_pdf = $this->preferences->getURL() .
- $this->router->pathFor('directlink', ['hash' => $hash]);
- }
- }
-
- //set replacements, even if empty, to be sure.
- $texts->setReplaces([
- 'link_membercard' => $link_card,
- 'link_contribpdf' => $link_pdf
- ]);
-
- $mail->setMessage($texts->getBody());
- $sent = $mail->send();
-
- if ($sent) {
- $this->history->add(
- preg_replace(
- array('/%name/', '/%email/'),
- array($adh->sname, $adh->getEmail()),
- _T("Email sent to user %name (%email)")
- )
- );
- } else {
- $txt = preg_replace(
- array('/%name/', '/%email/'),
- array($adh->sname, $adh->getEmail()),
- _T("A problem happened while sending contribution receipt to user %name (%email)")
- );
- $this->history->add($txt);
- $error_detected[] = $txt;
- }
- } else {
- $txt = preg_replace(
- array('/%name/', '/%email/'),
- array($adh->sname, $adh->getEmail()),
- _T("Trying to send an email to a member (%name) with an invalid address: %email")
- );
- $this->history->add($txt);
- $warning_detected[] = $txt;
- }
- }
-
- // Sent email to admin if pref checked
- if ($new && $this->preferences->pref_bool_mailadh) {
- // Get email text in database
- $text = 'newcont';
- if (!$contrib->isCotis()) {
- $text = 'newdonation';
- }
- $mtxt = $texts->getTexts($text, $this->preferences->pref_lang);
-
- $mail = new GaletteMail($this->preferences);
- $mail->setSubject($texts->getSubject());
-
- $recipients = [];
- foreach ($this->preferences->vpref_email_newadh as $pref_email) {
- $recipients[$pref_email] = $pref_email;
- }
- $mail->setRecipients($recipients);
-
- $mail->setMessage($texts->getBody());
- $sent = $mail->send();
-
- if ($sent) {
- $this->history->add(
- preg_replace(
- array('/%name/', '/%email/'),
- array($adh->sname, $adh->getEmail()),
- _T("Email sent to admin for user %name (%email)")
- )
- );
- } else {
- $txt = preg_replace(
- array('/%name/', '/%email/'),
- array($adh->sname, $adh->getEmail()),
- _T("A problem happened while sending to admin notification for user %name (%email) contribution")
- );
- $this->history->add($txt);
- $warning_detected[] = $txt;
- }
- }
- }
-
- if (count($success_detected) > 0) {
- foreach ($success_detected as $success) {
- $this->flash->addMessage(
- 'success_detected',
- $success
- );
- }
- }
-
-
- if (count($warning_detected) > 0) {
- foreach ($warning_detected as $warning) {
- $this->flash->addMessage(
- 'warning_detected',
- $warning
- );
- }
+ } else {
+ //something went wrong :'(
+ $error_detected[] = _T("An error occurred while storing the contribution.");
}
+ }
- if (count($error_detected) == 0) {
- if ($contrib->isTransactionPart() && $contrib->transaction->getMissingAmount() > 0) {
- //new contribution
- $redirect_url = $this->router->pathFor(
- 'addContribution',
- [
- 'type' => $post['contrib_type']
- ]
- ) . '?' . Transaction::PK . '=' . $contrib->transaction->id .
- '&' . Adherent::PK . '=' . $contrib->member;
- } else {
- //contributions list for member
- $redirect_url = $this->router->pathFor(
- 'contributions',
- [
- 'type' => 'contributions'
- ]
- ) . '?' . Adherent::PK . '=' . $contrib->member;
- }
+ if (count($error_detected) === 0) {
+ $files_res = $contrib->handleFiles($_FILES);
+ if (is_array($files_res)) {
+ $error_detected = array_merge($error_detected, $files_res);
}
}
- /* TODO: remove */
- if (!isset($contribution['duree_mois_cotis'])
- || $contribution['duree_mois_cotis'] == ''
- ) {
- // On error restore entered value or default to display the form again
- if (isset($_POST['duree_mois_cotis'])
- && $_POST['duree_mois_cotis'] != ''
- ) {
- $contribution['duree_mois_cotis'] = $_POST['duree_mois_cotis'];
+ if (count($error_detected) == 0) {
+ $this->session->contribution = null;
+ if ($contrib->isTransactionPart() && $contrib->transaction->getMissingAmount() > 0) {
+ //new contribution
+ $redirect_url = $this->routeparser->urlFor(
+ 'addContribution',
+ [
+ 'type' => $post['contrib_type'] ?? $type
+ ]
+ ) . '?' . Transaction::PK . '=' . $contrib->transaction->id .
+ '&' . Adherent::PK . '=' . $contrib->member;
} else {
- $contribution['duree_mois_cotis'] = $this->preferences->pref_membership_ext;
+ //contributions list for member
+ $redirect_url = $this->routeparser->urlFor(
+ 'contributions',
+ [
+ 'type' => 'contributions'
+ ]
+ ) . '?' . Adherent::PK . '=' . $contrib->member;
}
- }
-
- if (count($error_detected) > 0) {
+ } else {
//something went wrong.
//store entity in session
$this->session->contribution = $contrib;
- $redirect_url = $this->router->pathFor($args['action'] . 'Contribution', $args);
+ $redirect_url = $this->routeparser->urlFor($action . 'Contribution', $url_args);
//report errors
foreach ($error_detected as $error) {
$error
);
}
- } else {
- $this->session->contribution = null;
- if ($redirect_url === null) {
- $redirect_url = $this->router->pathFor('contributions', ['type' => $args['type']]);
- }
}
//redirect to calling action
*
* @return string
*/
- public function redirectUri(array $args = [])
+ public function redirectUri(array $args)
{
- return $this->router->pathFor('contributions', ['type' => $args['type']]);
+ return $this->routeparser->urlFor('contributions', ['type' => $args['type']]);
}
/**
*
* @return string
*/
- public function formUri(array $args = [])
+ public function formUri(array $args)
{
- return $this->router->pathFor(
+ return $this->routeparser->urlFor(
'doRemoveContribution',
$args
);
*
* @return string
*/
- public function confirmRemoveTitle(array $args = [])
+ public function confirmRemoveTitle(array $args)
{
$raw_type = null;
// /CRUD - Delete
// /CRUD
+
+ /**
+ * Get filter name in session
+ *
+ * @param array|null $args Route arguments
+ *
+ * @return string
+ */
+ public function getFilterName(array $args = null): string
+ {
+ return 'filter_' . $args['type'];
+ }
}