Skip to content

Commit

Permalink
Users can now reorder budgets #1108
Browse files Browse the repository at this point in the history
  • Loading branch information
JC5 committed Oct 17, 2018
1 parent b12773b commit d0d2189
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 58 deletions.
26 changes: 25 additions & 1 deletion app/Http/Controllers/Budget/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Support\Http\Controllers\DateCalculation;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;

Expand Down Expand Up @@ -63,7 +64,6 @@ function ($request, $next) {
);
}


/**
* Show all budgets.
*
Expand Down Expand Up @@ -134,5 +134,29 @@ public function index(Request $request, Carbon $start = null, Carbon $end = null
);
}

/**
* @param Request $request
*
* @return JsonResponse
*/
public function reorder(Request $request, BudgetRepositoryInterface $repository): JsonResponse
{
$budgetIds = $request->get('budgetIds');
$page = (int)$request->get('page');
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;

$currentOrder = (($page - 1) * $pageSize) + 1;
foreach ($budgetIds as $budgetId) {
$budgetId = (int)$budgetId;
$budget = $repository->findNull($budgetId);
if (null !== $budget) {
$repository->setBudgetOrder($budget, $currentOrder);
}
$currentOrder++;
}

return response()->json(['OK']);
}


}
3 changes: 2 additions & 1 deletion app/Models/Budget.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* @property-read string $email
* @property bool encrypted
* @property Collection budgetlimits
* @property int $order
*/
class Budget extends Model
{
Expand All @@ -61,7 +62,7 @@ class Budget extends Model
'encrypted' => 'boolean',
];
/** @var array Fields that can be filled */
protected $fillable = ['user_id', 'name', 'active'];
protected $fillable = ['user_id', 'name', 'active','order'];
/** @var array Hidden from view */
protected $hidden = ['encrypted'];

Expand Down
32 changes: 26 additions & 6 deletions app/Repositories/Budget/BudgetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function cleanupBudgets(): bool
} catch (Exception $e) {
Log::debug(sprintf('Could not delete budget limit: %s', $e->getMessage()));
}
Budget::where('order',0)->update(['order' => 100]);

// do the clean up by hand because Sqlite can be tricky with this.
$budgetLimits = BudgetLimit::orderBy('created_at', 'DESC')->get(['id', 'budget_id', 'start_date', 'end_date']);
Expand Down Expand Up @@ -289,11 +290,14 @@ public function firstUseDate(Budget $budget): ?Carbon
public function getActiveBudgets(): Collection
{
/** @var Collection $set */
$set = $this->user->budgets()->where('active', 1)->get();
$set = $this->user->budgets()->where('active', 1)
->get();

$set = $set->sortBy(
function (Budget $budget) {
return strtolower($budget->name);
$str = str_pad((string)$budget->order, 4, '0', STR_PAD_LEFT) . strtolower($budget->name);

return $str;
}
);

Expand Down Expand Up @@ -554,7 +558,9 @@ public function getBudgets(): Collection

$set = $set->sortBy(
function (Budget $budget) {
return strtolower($budget->name);
$str = str_pad((string)$budget->order, 4, '0', STR_PAD_LEFT) . strtolower($budget->name);

return $str;
}
);

Expand Down Expand Up @@ -583,7 +589,9 @@ public function getInactiveBudgets(): Collection

$set = $set->sortBy(
function (Budget $budget) {
return strtolower($budget->name);
$str = str_pad((string)$budget->order, 4, '0', STR_PAD_LEFT) . strtolower($budget->name);

return $str;
}
);

Expand Down Expand Up @@ -652,6 +660,18 @@ public function setAvailableBudget(TransactionCurrency $currency, Carbon $start,
return $availableBudget;
}

/**
* @param Budget $budget
* @param int $order
*/
public function setBudgetOrder(Budget $budget, int $order): void
{
$budget->order = $order;
$budget->save();
}

/** @noinspection MoreThanThreeArgumentsInspection */

/**
* @param User $user
*/
Expand All @@ -660,7 +680,6 @@ public function setUser(User $user): void
$this->user = $user;
}

/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param Collection $budgets
* @param Collection $accounts
Expand Down Expand Up @@ -825,6 +844,8 @@ public function updateAvailableBudget(AvailableBudget $availableBudget, array $d

}

/** @noinspection MoreThanThreeArgumentsInspection */

/**
* @param BudgetLimit $budgetLimit
* @param array $data
Expand All @@ -848,7 +869,6 @@ public function updateBudgetLimit(BudgetLimit $budgetLimit, array $data): Budget
return $budgetLimit;
}

/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param Budget $budget
* @param Carbon $start
Expand Down
12 changes: 10 additions & 2 deletions app/Repositories/Budget/BudgetRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ public function getAverageAvailable(Carbon $start, Carbon $end): string;
*/
public function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection;

/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param Collection $budgets
* @param Collection $accounts
Expand All @@ -167,6 +166,8 @@ public function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $en
*/
public function getBudgetPeriodReport(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array;

/** @noinspection MoreThanThreeArgumentsInspection */

/**
* @return Collection
*/
Expand Down Expand Up @@ -195,7 +196,6 @@ public function getInactiveBudgets(): Collection;
*/
public function getNoBudgetPeriodReport(Collection $accounts, Carbon $start, Carbon $end): array;

/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param TransactionCurrency $currency
* @param Carbon $start
Expand All @@ -206,6 +206,14 @@ public function getNoBudgetPeriodReport(Collection $accounts, Carbon $start, Car
*/
public function setAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end, string $amount): AvailableBudget;

/** @noinspection MoreThanThreeArgumentsInspection */

/**
* @param Budget $budget
* @param int $order
*/
public function setBudgetOrder(Budget $budget, int $order): void;

/**
* @param User $user
*/
Expand Down
93 changes: 93 additions & 0 deletions public/js/ff/budgets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,101 @@ $(function () {
}
});

// sortable!
if (typeof $(".sortable-table tbody").sortable !== "undefined") {
$(".sortable-table tbody").sortable(
{
helper: fixHelper,
items: 'tr:not(.ignore)',
stop: sortStop,
handle: '.handle',
start: function (event, ui) {
// Build a placeholder cell that spans all the cells in the row
var cellCount = 0;
$('td, th', ui.helper).each(function () {
// For each TD or TH try and get it's colspan attribute, and add that or 1 to the total
var colspan = 1;
var colspanAttr = $(this).attr('colspan');
if (colspanAttr > 1) {
colspan = colspanAttr;
}
cellCount += colspan;
});

// Add the placeholder UI - note that this is the item's content, so TD rather than TR
ui.placeholder.html('<td colspan="' + cellCount + '">&nbsp;</td>');
}
}
);
}
});

var fixHelper = function (e, tr) {
"use strict";
var $originals = tr.children();
var $helper = tr.clone();
$helper.children().each(function (index) {
// Set helper cell sizes to match the original sizes
$(this).width($originals.eq(index).width());
});
return $helper;
};


function sortStop(event, ui) {
"use strict";


//var current = $(ui.item);
var list = $('.sortable-table tbody tr');
var submit = [];
$.each(list, function (i, v) {
var row = $(v);
var id = parseInt(row.data('id'));
if (id > 0) {
submit.push(id);
}
});
var arr = {
budgetIds: submit,
page: page,
_token: token
};
// var thisDate = current.data('date');
// var originalBG = current.css('backgroundColor');
//
//
// if (current.prev().data('date') !== thisDate && current.next().data('date') !== thisDate) {
// // animate something with color:
// current.animate({backgroundColor: "#d9534f"}, 200, function () {
// $(this).animate({backgroundColor: originalBG}, 200);
// return undefined;
// });
//
// return false;
// }
//
// // do update
// var list = $('tr[data-date="' + thisDate + '"]');
// var submit = [];
// $.each(list, function (i, v) {
// var row = $(v);
// var id = row.data('id');
// submit.push(id);
// });
//
// // do extra animation when done?
$.get('budgets/reorder', arr);
//
// current.animate({backgroundColor: "#5cb85c"}, 200, function () {
// $(this).animate({backgroundColor: originalBG}, 200);
// return undefined;
// });
// return undefined;
//alert('drop!');
}


function drawSpentBar() {
"use strict";
if ($('.spentBar').length > 0) {
Expand Down

0 comments on commit d0d2189

Please sign in to comment.