2016-01-09 15:39:34 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* AccountList.php
|
2016-04-01 16:44:46 +02:00
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
2016-01-09 15:39:34 +01:00
|
|
|
*
|
2016-10-05 06:52:15 +02:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-01-09 15:39:34 +01:00
|
|
|
*/
|
|
|
|
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2016-01-09 15:39:34 +01:00
|
|
|
|
|
|
|
namespace FireflyIII\Support\Binder;
|
|
|
|
|
|
|
|
|
|
|
|
use FireflyIII\Models\Account;
|
2016-02-05 09:25:15 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2016-01-09 15:39:34 +01:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AccountList
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Support\Binder
|
|
|
|
*/
|
|
|
|
class AccountList implements BinderInterface
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
* @param $route
|
|
|
|
*
|
2016-02-05 09:25:15 +01:00
|
|
|
* @return Collection
|
2016-01-09 15:39:34 +01:00
|
|
|
*/
|
2016-02-06 10:11:06 +01:00
|
|
|
public static function routeBinder($value, $route): Collection
|
2016-01-09 15:39:34 +01:00
|
|
|
{
|
|
|
|
|
2016-09-16 12:07:45 +02:00
|
|
|
if (auth()->check()) {
|
2016-01-09 15:39:34 +01:00
|
|
|
|
|
|
|
$ids = explode(',', $value);
|
2016-03-02 11:48:53 +01:00
|
|
|
// filter ids:
|
|
|
|
$ids = self::filterIds($ids);
|
|
|
|
|
2016-01-09 15:39:34 +01:00
|
|
|
/** @var \Illuminate\Support\Collection $object */
|
|
|
|
$object = Account::leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
|
|
|
|
->whereIn('accounts.id', $ids)
|
2016-09-16 12:15:58 +02:00
|
|
|
->where('user_id', auth()->user()->id)
|
2016-01-09 15:39:34 +01:00
|
|
|
->get(['accounts.*']);
|
|
|
|
if ($object->count() > 0) {
|
2016-12-14 18:44:56 +01:00
|
|
|
$object = $object->sortBy(
|
|
|
|
function (Account $account) {
|
|
|
|
return $account->name;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-01-09 15:39:34 +01:00
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new NotFoundHttpException;
|
|
|
|
}
|
2016-03-02 11:48:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $ids
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected static function filterIds(array $ids): array
|
|
|
|
{
|
|
|
|
$new = [];
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
if (intval($id) > 0) {
|
|
|
|
$new[] = $id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$new = array_unique($new);
|
|
|
|
|
|
|
|
return $new;
|
|
|
|
}
|
2016-01-28 21:50:20 +01:00
|
|
|
}
|