Skip to content

Commit

Permalink
Merge pull request #25 from pvullioud/master
Browse files Browse the repository at this point in the history
Add ability to order fields (thanks pvullioud)
  • Loading branch information
Bradie Tilley authored Oct 8, 2020
2 parents 6fd8067 + 3b45782 commit c89b932
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 49 deletions.
60 changes: 60 additions & 0 deletions controllers/Form.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php namespace ABWebDevelopers\Forms\Controllers;

use ABWebDevelopers\Forms\Models\Field;
use Backend\Classes\Controller;
use BackendMenu;
use October\Rain\Support\Facades\Flash;

class Form extends Controller
{
Expand All @@ -20,4 +22,62 @@ public function __construct()
parent::__construct();
BackendMenu::setContext('ABWebDevelopers.Forms', 'bradie-forms', 'forms-forms');
}

public function onMoveFieldUp()
{
$field = Field::findOrFail(post('field_id'));

$fieldBefore = $field->form->fields()
->where('id', '<>', $field->id)
->where('sort_order', '<', $field->sort_order)
->orderBy('sort_order', 'desc')
->first();

if (empty($fieldBefore)) {
return; // already first
}

$this->swapFieldOrder($field, $fieldBefore);

Flash::success(trans('abwebdevelopers.forms::lang.models.all.sort_order.up'));

return $this->refreshFieldsRelation($field->form);
}

public function onMoveFieldDown()
{
$field = Field::findOrFail(post('field_id'));

$fieldAfter = $field->form->fields()
->where('id', '<>', $field->id)
->where('sort_order', '>', $field->sort_order)
->orderBy('sort_order', 'asc')
->first();

if (empty($fieldAfter)) {
return; // already last
}

$this->swapFieldOrder($field, $fieldAfter);

Flash::success(trans('abwebdevelopers.forms::lang.models.all.sort_order.down'));

return $this->refreshFieldsRelation($field->form);
}

private function refreshFieldsRelation(\ABWebDevelopers\Forms\Models\Form $model)
{
$this->initForm($model);
$this->initRelation($model, 'fields');

return $this->relationRefresh('fields');
}

private function swapFieldOrder(Field $current, Field $other)
{
$current->setSortableOrder(
[$current->id, $other->id],
[$other->sort_order, $current->sort_order]
);
}
}
15 changes: 15 additions & 0 deletions controllers/field/_order.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<a href="#"
data-request="onMoveFieldUp"
data-request-data="field_id:<?= $record->id ?>"
data-toggle="tooltip" title="<?= e(trans('abwebdevelopers.forms::lang.models.all.sort_order.up')) ?>">
<i class="icon-caret-up"></i>
</a>

&nbsp; <span class="text-muted">/</span> &nbsp;

<a href="#"
data-request="onMoveFieldDown"
data-request-data="field_id:<?= $record->id ?>"
data-toggle="tooltip" title="<?= e(trans('abwebdevelopers.forms::lang.models.all.sort_order.down')) ?>">
<i class="icon-caret-down"></i>
</a>
4 changes: 0 additions & 4 deletions controllers/field/_reorder_toolbar.htm

This file was deleted.

14 changes: 0 additions & 14 deletions controllers/field/config_reorder.yaml

This file was deleted.

20 changes: 0 additions & 20 deletions controllers/field/reorder.htm

This file was deleted.

4 changes: 3 additions & 1 deletion lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@
'label' => 'Updated At',
],
'sort_order' => [
'label' => 'Display Order',
'label' => 'Order',
'up' => 'Up',
'down' => 'Down',
],
'override' => [
'label' => 'Override system value',
Expand Down
4 changes: 4 additions & 0 deletions models/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class Field extends Model
'html_attributes',
];

public $belongsTo = [
'form' => Form::class
];

/**
* After fetching the Field event
* Create override_{field} Fields which represent the fields' states on whether or not
Expand Down
20 changes: 11 additions & 9 deletions models/field/columns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,32 @@ columns:
label: abwebdevelopers.forms::lang.models.field.name.label
type: text
searchable: true
sortable: true
sortable: false
code:
label: abwebdevelopers.forms::lang.models.field.code.label
type: text
searchable: true
sortable: true
sortable: false
type:
label: abwebdevelopers.forms::lang.models.field.type.label
type: text
searchable: true
sortable: true
sortable: false
required:
label: abwebdevelopers.forms::lang.models.field.required.label
type: switch
sortable: true
sortable: false
created_at:
label: abwebdevelopers.forms::lang.models.all.created_at.label
type: datetime
sortable: true
sortable: false
updated_at:
label: abwebdevelopers.forms::lang.models.all.updated_at.label
type: datetime
sortable: true
sort_order:
sortable: false
order:
label: abwebdevelopers.forms::lang.models.all.sort_order.label
type: number
sortable: true
type: partial
sortable: false
clickable: false
path: ~/plugins/abwebdevelopers/forms/controllers/field/_order.htm
4 changes: 3 additions & 1 deletion updates/version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@
- 'Remove obsolete foreign key constraints for tables'
- remove_foreign_key_constraints.php
1.2.2:
- 'Add persmissions to the plugin'
- 'Add permissions to the plugin'
1.2.3:
- 'Add ordering for fields'

0 comments on commit c89b932

Please sign in to comment.