Commit 811866d6 authored by Okhin's avatar Okhin
Browse files

Adding a recompute total functions in the admin page

parent 094f8ebf
...@@ -10,6 +10,7 @@ use LQDN\Command\DonationResetPdfCommand; ...@@ -10,6 +10,7 @@ use LQDN\Command\DonationResetPdfCommand;
use LQDN\Command\DonationCreateCommand; use LQDN\Command\DonationCreateCommand;
use LQDN\Command\UserUpdateByAdminCommand; use LQDN\Command\UserUpdateByAdminCommand;
use LQDN\Command\UserCreateCommand; use LQDN\Command\UserCreateCommand;
use LQDN\Command\AdminUpdateTotalUsersCommand;
class Admin extends Controller class Admin extends Controller
{ {
...@@ -47,11 +48,20 @@ class Admin extends Controller ...@@ -47,11 +48,20 @@ class Admin extends Controller
100 => "non applicable", 100 => "non applicable",
)); ));
// Test si il y a des messages
if (!$f3->exists('SESSION.message')) {
$f3->push('SESSION.message', '');
}
if (!$f3->exists('SESSION.error')) {
$f3->push('SESSION.error', '');
}
if (!$f3->get('SESSION.admin')) { if (!$f3->get('SESSION.admin')) {
$mapper = new\DB\SQL\Mapper($f3->get('DB'), 'admins'); $mapper = new\DB\SQL\Mapper($f3->get('DB'), 'admins');
$auth = new \Auth($mapper, array('id'=>'user_id', 'pw'=>'password')); $auth = new \Auth($mapper, array('id'=>'user_id', 'pw'=>'password'));
$success = $auth->basic('\Controller\Admin::hash_password'); $success = $auth->basic('\Controller\Admin::hash_password');
if (!$success) { if (!$success) {
$f3->push('SESSION.error', 'Accès non autorisé');
$f3->error(401); $f3->error(401);
} else { } else {
$f3->set('SESSION.admin', true); $f3->set('SESSION.admin', true);
...@@ -61,6 +71,23 @@ class Admin extends Controller ...@@ -61,6 +71,23 @@ class Admin extends Controller
$f3->set('PIPLOME_URL', PIPLOME_URL); $f3->set('PIPLOME_URL', PIPLOME_URL);
} }
public function afterRoute($f3, $args)
{
// Rendu de la page
if ($this->template!='') {
echo \Template::instance()->render($this->template);
}
// Une fois que tout est affiché, on peut supprimer les notifications
$f3->clear('SESSION.message');
$f3->clear('SESSION.error');
}
public static function recompute($f3, $args)
{
$f3->get('container')['command_handler']->handle(new AdminUpdateTotalUsersCommand());
$f3->set('block_content', 'backend/dons.html');
}
public static function hash_password($password) public static function hash_password($password)
{ {
return hash('sha256', $password); return hash('sha256', $password);
...@@ -140,6 +167,7 @@ class Admin extends Controller ...@@ -140,6 +167,7 @@ class Admin extends Controller
$cumul = $user_amount['total'] + $result['somme']; $cumul = $user_amount['total'] + $result['somme'];
} }
if ($error != '') { if ($error != '') {
$f3->push('SESSION.error', $error);
} else { } else {
$db->query( $db->query(
"UPDATE dons "UPDATE dons
...@@ -190,6 +218,7 @@ class Admin extends Controller ...@@ -190,6 +218,7 @@ class Admin extends Controller
} }
} }
if ($error != '') { if ($error != '') {
$f3->push('SESSION.error', $error);
} else { } else {
$db->query( $db->query(
"UPDATE dons "UPDATE dons
...@@ -990,8 +1019,8 @@ class Admin extends Controller ...@@ -990,8 +1019,8 @@ class Admin extends Controller
} }
} }
$f3->set('result', $result); $f3->set('result', $result);
$f3->set('error', $error); $f3->push('SESSION.error', $error);
$f3->set('message', $message); $f3->push('SESSION.message', $message);
$f3->set('block_content', 'backend/banque.html'); $f3->set('block_content', 'backend/banque.html');
} }
......
...@@ -29,6 +29,7 @@ GET @edit_support:/admin/support/edit/@id=Controller\Admin->support ...@@ -29,6 +29,7 @@ GET @edit_support:/admin/support/edit/@id=Controller\Admin->support
POST @modify_support:/admin/support/edit=Controller\Admin->support POST @modify_support:/admin/support/edit=Controller\Admin->support
GET @invalidate_support:/admin/support/invalidate/@id=Controller\Admin->invalidate GET @invalidate_support:/admin/support/invalidate/@id=Controller\Admin->invalidate
GET @validate_support:/admin/support/validate/@id=Controller\Admin->validate GET @validate_support:/admin/support/validate/@id=Controller\Admin->validate
GET @recompute:/admin/recompute=Controlle\Admin->recompute
GET|POST @admin_accounts:/admin/accounts=Controller\Admin->accounts GET|POST @admin_accounts:/admin/accounts=Controller\Admin->accounts
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
<h2>Gestion banque</h2> <h2>Gestion banque</h2>
<check if="{{@result}}"><p class="result">{{@result | raw}}</p></check> <check if="{{@result}}"><p class="result">{{@result | raw}}</p></check>
<check if="{{@error}}"><p class="error">{{@error | raw}}</p></check> <check if="{{@SESSION.error}}"><message messages="{{@SESSION.error}}" class="alert alert-danger"/></check>
<check if="{{@message}}"><p class="message">{{@message | raw}}</p></check> <check if="{{@SESSION.message}}"><message messages="{{@SESSION.message}}" class="alert alert-danger" /></p></check>
<section id="cbs"> <section id="cbs">
<h3 >Mise à jour des dates d'expiration des CB</h3> <h3 >Mise à jour des dates d'expiration des CB</h3>
......
...@@ -52,9 +52,8 @@ ...@@ -52,9 +52,8 @@
<input type="submit" value="Voir ce don" /> <input type="submit" value="Voir ce don" />
</form> </form>
<br /> <br />
<check if="{{ @msg }}"> <check if="{{ @SESSION.error }}"> <message messages="{{ @SESSION.error }}" class="alert alert-danger" /> </check>
<span class="error">{{ @msg }}</span> <check if="{{ @SESSION.message }}"> <message messages="{{ @SESSION.message }}" class="alert alert-default" /> </check>
</check>
</section> </section>
<script type="text/javascript"> <script type="text/javascript">
......
...@@ -45,6 +45,8 @@ ...@@ -45,6 +45,8 @@
<input type="submit" value="Filtrer" /> <input type="submit" value="Filtrer" />
</form> </form>
<a href="{{ 'recompute' | alias }}">Recalculer les totaux et cumuls de toute la base de donnée</a>
<br />
<check if="{{ @row_count }}"><b>{{ @row_count }} résultats</b> <check if="{{ @row_count }}"><b>{{ @row_count }} résultats</b>
<br /> <br />
<table cellspacing="0" cellpadding="0"> <table cellspacing="0" cellpadding="0">
......
...@@ -154,7 +154,7 @@ ...@@ -154,7 +154,7 @@
</nav> </nav>
</div> </div>
<section id="messages"> <section id="messages">
<message messages="@SESSION.message" class="alert alert-default"> <message messages="{{ @SESSION.message }}" class="alert alert-default">
<message messages="@SESSION.error" class="alert alert-danger"> <message messages="{{ @SESSION.error }}" class="alert alert-danger">
</section> </section>
<?php
namespace LQDN\Command;
class AdminUpdateTotalUsersCommand
{
// There is no parameters for this command.
}
...@@ -7,6 +7,7 @@ use LQDN\Command\UserUpdateByAdminCommand; ...@@ -7,6 +7,7 @@ use LQDN\Command\UserUpdateByAdminCommand;
use LQDN\Command\UserUpdateTotalCommand; use LQDN\Command\UserUpdateTotalCommand;
use LQDN\Command\UserUpdateCumulCommand; use LQDN\Command\UserUpdateCumulCommand;
use LQDN\Command\UserCreateCommand; use LQDN\Command\UserCreateCommand;
use LQDN\Command\AdminUpdateTotalUsersCommand;
class UserHandler class UserHandler
{ {
...@@ -79,4 +80,23 @@ class UserHandler ...@@ -79,4 +80,23 @@ class UserHandler
] ]
); );
} }
/**
* Get through all the users and databases to recompute all total from users.
*
* @param AdminUpdateTotalUsersCommand $command
*/
public function handleAdminUpdateTotalUsersCommand(AdminUpdateTotalUsersCommand $command)
{
// Let's get all users first
$user_ids = $this->connection->executeQuery('SELECT id FROM users')->fetchAll(\PDO::FETCH_ASSOC);
foreach ($user_ids as $user_id) {
$user_id = (int) $user_id;
$total = $this->connection->executeQuery('SELECT SUM(somme) AS total FROM dons WHERE user_id = :user_id AND STATUS IN (1, 102)', [
'user_id' => $user_id
])->fetch(\PDO::FETCH_ASSOC);
$this->connection->executeUpdate('UPDATE users SET total = :total WHERE id = :user_id', ['total' => $total['total'], 'user_id' => $user_id]);
}
}
} }
...@@ -6,10 +6,17 @@ use LQDN\Command\UserUpdateByAdminCommand; ...@@ -6,10 +6,17 @@ use LQDN\Command\UserUpdateByAdminCommand;
use LQDN\Command\UserUpdateTotalCommand; use LQDN\Command\UserUpdateTotalCommand;
use LQDN\Command\UserUpdateCumulCommand; use LQDN\Command\UserUpdateCumulCommand;
use LQDN\Command\UserCreateCommand; use LQDN\Command\UserCreateCommand;
use LQDN\Command\AdminUpdateTotalUsersCommand;
use LQDN\Tests\Functional\FunctionalTest; use LQDN\Tests\Functional\FunctionalTest;
class UserHandlerTest extends FunctionalTest class UserHandlerTest extends FunctionalTest
{ {
public function testAdminUpdateTotalUsersCommand()
{
$this->container['command_handler']->handle(new AdminUpdateTotalUsersCommand());
$this->assertSame(1000, (int) $this->getUser(1)['total']);
}
public function testUserUpdateFromAdmin() public function testUserUpdateFromAdmin()
{ {
$user = $this->getUser(1); $user = $this->getUser(1);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment