Global paybacks should be ok. To be tested

This commit is contained in:
Phyks 2013-09-25 20:28:09 +02:00
parent 32cd8a4fe7
commit e5014cb7f9
9 changed files with 119 additions and 66 deletions

5
TODO
View File

@ -1,7 +1,6 @@
Issues :
========
* Check CSRF everywhere
* Close a global payback
Improvements :
==============
@ -11,7 +10,3 @@ Improvements :
* API
* cf TODO in files
* README file
Manage paybacks :
=================
* TODO : Global payback

View File

@ -4,7 +4,7 @@
require_once('UsersInGlobalPayback.class.php');
class GlobalPayback extends Storage {
protected $id = 0, $date, $users_in;
protected $id = 0, $date, $users_in, $closed;
// date is a DateTime object
// buyer is a User object
// users_in is a UsersIn objects
@ -12,11 +12,13 @@
protected $fields = array(
'id'=>'key',
'date'=>'date',
'closed'=>'bool'
);
public function __construct() {
parent::__construct();
$this->users_in = new UsersInGlobalPayback();
$this->date = new Datetime();
}
// Getters
@ -36,6 +38,10 @@
return $this->users_in;
}
public function getClosed() {
return (bool) $this->closed;
}
// Setters
// =======
public function setId($id) {
@ -44,7 +50,7 @@
}
public function setDate($minute, $hour, $day, $month, $year) {
if((int) $minute < 10) $minute = '0'.$minute;
if((int) $minute < 10) $minute = '0'.(int) $minute;
$this->date = DateTime::createFromFormat('Y-n-j G:i', $year.'-'.(int) $month.'-'.(int) $day.' '.(int) $hour.':'.$minute);
}
@ -54,6 +60,10 @@
$this->users_in->set($users_in);
}
public function setClosed($closed) {
$this->closed = (bool) $closed;
}
// Maps htmlspecialchars on the class before display
// =================================================
public function secureDisplay() {
@ -71,6 +81,7 @@
$this->setId($data['id']);
$this->date = DateTime::createFromFormat('Y-m-d H:i:s', $data['date']);
$this->setClosed($data['closed']);
}
// Override parent load() method

View File

@ -59,7 +59,7 @@
}
public function setDate($minute, $hour, $day, $month, $year) {
if((int) $minute < 10) $minute = '0'.$minute;
if((int) $minute < 10) $minute = '0'.(int) $minute;
$this->date = DateTime::createFromFormat('Y-n-j G:i', $year.'-'.(int) $month.'-'.(int) $day.' '.(int) $hour.':'.$minute);
}

View File

@ -55,6 +55,19 @@
return $this;
}
// Test if the payback should be closed
// ====================================
public function isEmpty() {
foreach($this->users_list as $user1=>$temp) {
foreach($temp as $user2=>$amount) {
if($amount != 0) {
return false;
}
}
}
return true;
}
// Override load() method
// ======================
public function load() {
@ -85,7 +98,7 @@
public function save() {
// TODO : Optimize ?
$query = 'SELECT COUNT(payback_id) FROM '.MYSQL_PREFIX.$this->TABLE_NAME.' WHERE payback_id=:payback_id';
$query = 'SELECT COUNT(global_payback_id) FROM '.MYSQL_PREFIX.$this->TABLE_NAME.' WHERE global_payback_id=:payback_id';
$query = $this->getConnection()->prepare($query);
$query->bindParam(':payback_id', $this->payback_id);
$query->execute();
@ -94,7 +107,7 @@
if($count != 0) {
// If there are already some records, delete them first
$query = $this->getConnection()->prepare('DELETE FROM '.MYSQL_PREFIX.$this->TABLE_NAME.' WHERE payback_id=:payback_id');
$query = $this->getConnection()->prepare('DELETE FROM '.MYSQL_PREFIX.$this->TABLE_NAME.' WHERE global_payback_id=:payback_id');
$query->bindParam(':payback_id', $this->payback_id);
$query->execute();
}

View File

@ -719,13 +719,20 @@
$global_payback->setUsersIn($users_in);
if($global_payback->getUsersIn()->isEmpty()) {
$global_payback->setClosed(true);
}
else {
$global_payback->setClosed(false);
}
$global_payback->save();
// Clear the cache
($cached_files = glob(raintpl::$cache_dir."*.rtpl.php")) or ($cached_files = array());
array_map("unlink", $cached_files);
header('location: see_paybacks.php?id='.(int)$_GET['payback_id']);
header('location: ?do=see_paybacks&id='.(int)$_GET['payback_id']);
exit();
}
@ -839,6 +846,14 @@
}
$global_payback->setUsersIn($users_in);
if($global_payback->getUsersIn()->isEmpty()) {
$global_payback->setClosed(true);
}
else {
$global_payback->setClosed(false);
}
$global_payback->setDate(date('i'), date('G'), date('j'), date('n'), date('Y'));
$global_payback->save();

View File

@ -9,7 +9,7 @@
<dl>
{loop="$global_paybacks"}
<dt>{$value->getDate()}</dt>
<dd><a href="?do=see_paybacks&id={$value->getId()}">Payback n°{$value->getId()}</a></dd>
<dd>{if condition="$value->getClosed() === true"}[Closed] {/if}<a href="?do=see_paybacks&id={$value->getId()}">Payback n°{$value->getId()}</a></dd>
{/loop}
</dl>
{/if}
@ -36,7 +36,7 @@
{/loop}
</fieldset>
<p>
<input type="submit" onclick="confirm('You are going to add a global payback which includes the selected user. Confirm ?');" value="Confirm"/>
<input type="submit" onclick="return confirm('You are going to add a global payback which includes the selected user. Confirm ?');" value="Confirm"/>
</p>
</form>
{/if}

View File

@ -6,13 +6,22 @@
<dl>
{loop="$global_paybacks"}
<dt>{$value->getDate()}</dt>
<dd><a href="?do=see_paybacks&id={$value->getId()}">Payback n°{$value->getId()}</a></dd>
<dd>
{if condition="$value->getClosed() !== false"}
<a href="?do=see_paybacks&id={$value->getId()}" onclick="return confirm("Are you sure you want to confirm this payback ? This action can't be undone easily");">Payback n°{$value->getId()}</a>
{else}
[Closed] Payback n°{$value->getId()}
{/if}
</dd>
{/loop}
</dl>
{else}
<p>No global paybacks available.</p>
{/if}
{else}
{if condition="$global_paybacks === false"}
Payback doesn't exist.
{else}
<h2>Payback n°{$global_paybacks->getId()}</h2>
{$table=$global_paybacks->getUsersIn()->get()}
@ -42,6 +51,7 @@
</tr>
{/loop}
</table>
{/if}
<p class="center"><a href="?do=see_paybacks">Go back to global paybacks list</a></p>
{/if}

View File

@ -9,7 +9,7 @@
<dl>
{loop="$global_paybacks"}
<dt>{$value->getDate()}</dt>
<dd><a href="?do=see_paybacks&id={$value->getId()}">Remboursement n°{$value->getId()}</a></dd>
<dd>{if condition="$value->getClosed() === true"}[Terminé] {/if}<a href="?do=see_paybacks&id={$value->getId()}">Remboursement n°{$value->getId()}</a></dd>
{/loop}
</dl>
{/if}
@ -36,7 +36,7 @@
{/loop}
</fieldset>
<p>
<input type="submit" onclick="confirm('Vous êtes sur le point d\'ajouter un remboursement global incluant les utilisateurs sélectionnés.');" value="Valider"/>
<input type="submit" onclick="return confirm('Vous êtes sur le point d\'ajouter un remboursement global incluant les utilisateurs sélectionnés.');" value="Valider"/>
</p>
</form>
{/if}

View File

@ -6,15 +6,23 @@
<dl>
{loop="$global_paybacks"}
<dt>{$value->getDate()}</dt>
<dd><a href="?do=see_paybacks&id={$value->getId()}">Remboursement n°{$value->getId()}</a></dd>
<dd>
{if condition="$value->getClosed() !== false"}
<a href="?do=see_paybacks&id={$value->getId()}" onclick="return confirm('Etes-vous sur de vouloir confirmer ce remboursement ? Cette action ne peut etre annulée facilement.');">Remboursement n°{$value->getId()}</a>
{else}
[Terminé] Remboursement n°{$value->getId()}
{/if}
</dd>
{/loop}
</dl>
{else}
<p>Aucun remboursement global disponible pour l'instant.</p>
{/if}
{else}
{if condition="$global_paybacks === false"}
Remboursement inexistant.
{else}
<h2>Remboursement n°{$global_paybacks->getId()}</h2>
{$table=$global_paybacks->getUsersIn()->get()}
<table id="global_paybacks_table">
<tr>
@ -42,6 +50,7 @@
</tr>
{/loop}
</table>
{/if}
<p class="center"><a href="?do=see_paybacks">Retour à la liste des remboursements</a></p>
{/if}