Better unicity for objects.

This commit is contained in:
James Cole 2015-03-31 14:16:25 +02:00
parent 776b37f4ea
commit a079eec2cb
9 changed files with 70 additions and 29 deletions

View File

@ -274,7 +274,9 @@ class TransactionController extends Controller
$journalData = $request->getJournalData();
$journal = $repository->store($journalData);
// rescan journal, UpdateJournalConnection
event(new JournalSaved($journal));
// ConnectJournalToPiggyBank
event(new JournalCreated($journal, intval($request->get('piggy_bank_id'))));
if (intval($request->get('reminder_id')) > 0) {

View File

@ -47,14 +47,16 @@ class BillFormRequest extends Request
*/
public function rules()
{
$nameRule = 'required|between:1,255|uniqueForUser:bills,name';
$nameRule = 'required|between:1,255|uniqueObjectForUser:bills,name,name_encrypted';
$matchRule = 'required|between:1,255|uniqueObjectForUser:bills,match,match_encrypted';
if (intval(Input::get('id')) > 0) {
$nameRule = 'required|between:1,255';
$nameRule .= ',' . intval(Input::get('id'));
$matchRule .= ',' . intval(Input::get('id'));
}
$rules = [
'name' => $nameRule,
'match' => 'required|between:1,255',
'match' => $matchRule,
'amount_min' => 'required|numeric|min:0.01',
'amount_max' => 'required|numeric|min:0.01',
'amount_currency_id' => 'required|exists:transaction_currencies,id',

View File

@ -28,9 +28,9 @@ class BudgetFormRequest extends Request
public function rules()
{
$nameRule = 'required|between:1,100|uniqueForUser:budgets,name';
$nameRule = 'required|between:1,100|uniqueObjectForUser:budgets,name,encrypted';
if (Budget::find(Input::get('id'))) {
$nameRule = 'required|between:1,100';
$nameRule = 'required|between:1,100|uniqueObjectForUser:budgets,name,encrypted,'.intval(Input::get('id'));
}
return [

View File

@ -28,9 +28,9 @@ class CategoryFormRequest extends Request
public function rules()
{
$nameRule = 'required|between:1,100|uniqueForUser:categories,name';
$nameRule = 'required|between:1,100|uniqueObjectForUser:categories,name,encrypted';
if (Category::find(Input::get('id'))) {
$nameRule = 'required|between:1,100';
$nameRule = 'required|between:1,100|uniqueObjectForUser:categories,name,encrypted,'.intval(Input::get('id'));
}
return [

View File

@ -29,10 +29,10 @@ class PiggyBankFormRequest extends Request
public function rules()
{
$nameRule = 'required|between:1,255|uniquePiggyBankForUser:piggy_banks,name';
$nameRule = 'required|between:1,255|uniquePiggyBankForUser';
$targetDateRule = 'date';
if (intval(Input::get('id'))) {
$nameRule = 'required|between:1,255';
$nameRule = 'required|between:1,255|uniquePiggyBankForUser:'.intval(Input::get('id'));
}

View File

@ -165,6 +165,46 @@ class FireflyValidator extends Validator
}
/**
* Validate an object and its unicity. Checks for encryption / encrypted values as well.
*
* parameter 0: the table
* parameter 1: the field
* parameter 2: the encrypted / not encrypted boolean. Defaults to "encrypted".
* parameter 3: an id to ignore (when editing)
*
* @param $attribute
* @param $value
* @param $parameters
*
* @return bool
*/
public function validateUniqueObjectForUser($attribute, $value, $parameters)
{
$table = $parameters[0];
$field = $parameters[1];
$encrypted = isset($parameters[2]) ? $parameters[2] : 'encrypted';
$exclude = isset($parameters[3]) ? $parameters[3] : null;
$query = DB::table($table)->where('user_id', Auth::user()->id);
if (!is_null($exclude)) {
$query->where('id', '!=', $exclude);
}
$set = $query->get();
foreach ($set as $entry) {
$isEncrypted = intval($entry->$encrypted) == 1 ? true : false;
$checkValue = $isEncrypted ? Crypt::decrypt($entry->$field) : $entry->$field;
if ($checkValue == $value) {
return false;
}
}
return true;
}
/**
* @param $attribute
* @param $value
@ -174,18 +214,24 @@ class FireflyValidator extends Validator
*/
public function validateUniquePiggyBankForUser($attribute, $value, $parameters)
{
$query = DB::table($parameters[0])->where('piggy_banks.' . $parameters[1], $value);
$exclude = isset($parameters[0]) ? $parameters[0] : null;
$query = DB::table('piggy_banks');
$query->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id');
$query->where('accounts.user_id', Auth::user()->id);
if (isset($paramers[2])) {
$query->where('piggy_banks.id', '!=', $parameters[2]);
if (!is_null($exclude)) {
$query->where('piggy_banks.id', '!=', $exclude);
}
$count = $query->count();
if ($count == 0) {
return true;
$set = $query->get(['piggy_banks.*']);
foreach($set as $entry) {
$isEncrypted = intval($entry->encrypted) == 1 ? true : false;
$checkValue = $isEncrypted ? Crypt::decrypt($entry->name) : $entry->name;
if($checkValue == $value) {
return false;
}
}
return false;
return true;
}
}

View File

@ -1,6 +1,5 @@
<!DOCTYPE html>
<html lang="en">
<?php $r = Route::getCurrentRoute()->getName();?>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">

View File

@ -1,4 +1,6 @@
<!-- Navigation -->
<?php $r = Route::getCurrentRoute()->getName();?>
<nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
@ -147,10 +149,9 @@
</li>
<?php
$isMM = !(strpos($r,'piggy-banks') === false) || !(strpos($r,'bills') === false) | !(strpos($r,'repeated') === false);
$isMM = !(strpos($r,'piggy-banks') === false) || !(strpos($r,'bills') === false);
$isPiggy = !(strpos($r,'piggy-banks') === false);
$isBill = !(strpos($r,'bills') === false) && strpos($r,'bills.create') === false;
$isRep = !(strpos($r,'repeated') === false);
?>
<li @if($isMM)class="active"@endif>
<a href="#"><i class="fa fa-euro fa-fw"></i> Money management<span class="fa arrow"></span></a>

View File

@ -233,7 +233,7 @@
</div>
</div>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-sort-amount-asc fa-fw"></i>
@ -242,15 +242,6 @@
<div class="panel-body">Body</div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-rotate-left fa-fw"></i>
Repeated expenses
</div>
<div class="panel-body">Body</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">