Files
firefly-iii/app/Validation/FireflyValidator.php

248 lines
7.3 KiB
PHP
Raw Normal View History

<?php
namespace FireflyIII\Validation;
2015-02-24 22:53:38 +01:00
use Auth;
use Carbon\Carbon;
2015-03-26 18:05:23 +01:00
use Config;
use Crypt;
use DB;
2015-03-30 20:08:27 +02:00
use FireflyIII\Models\Account;
2015-03-26 18:05:23 +01:00
use FireflyIII\Models\AccountType;
2015-03-30 20:08:27 +02:00
use Illuminate\Contracts\Encryption\DecryptException;
2015-02-11 07:35:10 +01:00
use Illuminate\Validation\Validator;
2015-03-30 20:08:27 +02:00
use Log;
use Navigation;
2015-02-11 07:35:10 +01:00
/**
* Class FireflyValidator
*
* @package FireflyIII\Validation
*/
class FireflyValidator extends Validator
{
2015-02-24 22:53:38 +01:00
/**
* @param $attribute
* @param $value
* @param $parameters
*
* @return bool
*/
public function validateBelongsToUser($attribute, $value, $parameters)
{
2015-03-30 20:08:27 +02:00
2015-02-24 22:53:38 +01:00
$count = DB::table($parameters[0])->where('user_id', Auth::user()->id)->where('id', $value)->count();
if ($count == 1) {
return true;
}
return false;
}
2015-03-26 18:05:23 +01:00
/**
* @param $attribute
* @param $value
* @param $parameters
*
* @return bool
*/
public function validatePiggyBankReminder($attribute, $value, $parameters)
{
$array = $this->data;
// no reminder? dont care.
if (!isset($array['remind_me'])) {
return true;
}
// get or set start date & target date:
$startDate = isset($array['startdate']) ? new Carbon($array['startdate']) : new Carbon;
$targetDate = isset($array['targetdate']) && strlen($array['targetdate']) > 0 ? new Carbon($array['targetdate']) : null;
// target date is null? reminder period is always good.
if ($array['remind_me'] == '1' && is_null($targetDate)) {
return true;
}
2015-03-26 18:05:23 +01:00
$nextReminder = Navigation::addPeriod($startDate, $array['reminder'], 0);
// reminder is beyond target?
2015-03-26 18:05:23 +01:00
if ($nextReminder > $targetDate) {
return false;
}
2015-03-26 18:05:23 +01:00
return true;
}
2015-03-26 18:05:23 +01:00
/**
* @param $attribute
* @param $value
* @param $parameters
*
* @return bool
*/
public function validateUniqueAccountForUser($attribute, $value, $parameters)
{
2015-03-30 20:08:27 +02:00
$type = null;
2015-03-27 09:24:26 +01:00
2015-03-27 09:21:09 +01:00
2015-03-30 20:08:27 +02:00
/**
* Switch on different cases on which this method can respond:
*/
$hasWhat = isset($this->data['what']);
$hasAccountTypeId = isset($this->data['account_type_id']) && isset($this->data['name']);
$hasAccountId = isset($this->data['id']);
$ignoreId = 0;
2015-03-30 20:08:27 +02:00
if ($hasWhat) {
$search = Config::get('firefly.accountTypeByIdentifier.' . $this->data['what']);
$type = AccountType::whereType($search)->first();
// this field can be used to find the exact type, and continue.
}
if ($hasAccountTypeId) {
$type = AccountType::find($this->data['account_type_id']);
}
if ($hasAccountId) {
/** @var Account $account */
$account = Account::find($this->data['id']);
$ignoreId = intval($this->data['id']);
$type = AccountType::find($account->account_type_id);
unset($account);
2015-03-26 18:05:23 +01:00
}
2015-03-30 20:08:27 +02:00
/**
* Try to decrypt data just in case:
*/
try {
$value = Crypt::decrypt($value);
} catch (DecryptException $e) {
}
2015-03-30 20:08:27 +02:00
if (is_null($type)) {
Log::error('Could not determine type of account to validate.');
2015-03-30 20:08:27 +02:00
return false;
2015-03-26 18:05:23 +01:00
}
2015-03-30 20:08:27 +02:00
// get all accounts with this type, and find the name.
$userId = Auth::check() ? Auth::user()->id : 0;
$set = Account::where('account_type_id', $type->id)->where('id', '!=', $ignoreId)->where('user_id', $userId)->get();
2015-03-30 20:08:27 +02:00
/** @var Account $entry */
foreach ($set as $entry) {
if ($entry->name == $value) {
return false;
}
2015-03-26 18:05:23 +01:00
}
2015-03-30 20:08:27 +02:00
return true;
// exit;
//
//
// // get account type from data, we must have this:
// $validTypes = array_keys(Config::get('firefly.subTitlesByIdentifier'));
// $dbType = null;
// $type = isset($this->data['what']) && in_array($this->data['what'], $validTypes) ? $this->data['what'] : null;
// // some fallback:
// if (is_null($type)) {
// $type = in_array(Input::get('what'), $validTypes) ? Input::get('what') : null;
//
// }
//
// // still null?
// if (is_null($type)) {
// // find by other field:
// $findType = isset($this->data['account_type_id']) ? $this->data['account_type_id'] : 0;
// $dbType = AccountType::find($findType);
// $type = $findType == 0 ? null : $findType;
// }
// // STILL null?
//
// if (is_null($type) && isset($this->data['id'])) {
// // check ID thingie
// $dbAccount = Account::find($this->data['id']);
// if (!$dbAccount) {
// Log::error('False because $dbAccount does not exist.');
//
// return false;
// }
// $dbType = AccountType::find($dbAccount->account_type_id);
// } else {
// $dbType = AccountType::whereType()
// }
//
// if (is_null($dbType)) {
// Log::error('False because $dbType is null.');
//
// return false;
// }
//
// // user id?
// $userId = Auth::check() ? Auth::user()->id : $this->data['user_id'];
//
// $query = DB::table('accounts')->where('name', $value)->where('account_type_id', $dbType->id)->where('user_id', $userId);
//
// if (isset($parameters[0])) {
// $query->where('id', '!=', $parameters[0]);
// }
// $count = $query->count();
// if ($count == 0) {
//
// return true;
// }
//
// return false;
2015-03-26 18:05:23 +01:00
}
2015-02-11 07:35:10 +01:00
/**
* @param $attribute
* @param $value
* @param $parameters
*
* @return bool
*/
public function validateUniqueForUser($attribute, $value, $parameters)
{
$query = DB::table($parameters[0])->where($parameters[1], $value);
2015-03-26 18:05:23 +01:00
$query->where('user_id', Auth::user()->id);
if (isset($paramers[2])) {
$query->where('id', '!=', $parameters[2]);
}
$count = $query->count();
2015-02-11 07:35:10 +01:00
if ($count == 0) {
return true;
}
2015-02-11 07:35:10 +01:00
return false;
}
2015-03-27 20:20:52 +01:00
/**
* @param $attribute
* @param $value
* @param $parameters
*
* @return bool
*/
public function validateUniquePiggyBankForUser($attribute, $value, $parameters)
{
2015-03-29 21:27:51 +02:00
$query = DB::table($parameters[0])->where('piggy_banks.' . $parameters[1], $value);
2015-03-27 20:20:52 +01:00
$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]);
}
$count = $query->count();
if ($count == 0) {
return true;
}
return false;
}
}