Skip to content

Commit 8f90a34

Browse files
committed
Merge branch 'table-multiple-bug'
2 parents fb24ebb + 4308a45 commit 8f90a34

36 files changed

+1033
-149
lines changed

CHANGELOG.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,90 @@ All Notable changes to `Backpack CRUD` will be documented in this file
2020
- Nothing
2121

2222

23+
## [3.1.25] - 2016-09-30
24+
25+
### Fixed
26+
- bug fix for 'table' field type - you can now have multiple fields on the same form;
27+
28+
29+
## [3.1.24] - 2016-09-27
30+
31+
### Added
32+
- address field type - thanks to [Owen Melbourne](https://github.com/OwenMelbz);
33+
34+
35+
## [3.1.23] - 2016-09-27
36+
37+
### Added
38+
- autoFocus() and autoFocusOnFirstField() - thanks to [Owen Melbourne](https://github.com/OwenMelbz);
39+
40+
41+
## [3.1.22] - 2016-09-27
42+
43+
### Fixed
44+
- checklist and checklist_dependency fields allow html on labels;
45+
46+
47+
## [3.1.21] - 2016-09-26
48+
49+
### Added
50+
- "table" field type - thanks to [Owen Melbourne](https://github.com/OwenMelbz);
51+
- "multidimensional_array" column type - thanks to [Owen Melbourne](https://github.com/OwenMelbz);
52+
53+
54+
## [3.1.20] - 2016-09-26
55+
56+
### Added
57+
- Non-core CRUD features are now separated into traits;
58+
59+
### Fixed
60+
- The 'password' field is no longer filtered before the create event;
61+
- CrudPanels can now be defined in the new EntityCrudController::setup() method;
62+
63+
## [3.1.19] - 2016-09-26
64+
65+
### Fixed
66+
- AJAX datatables can now have select_multiple columns;
67+
68+
69+
## [3.1.18] - 2016-09-25
70+
71+
### Fixed
72+
- checkbox field has default value;
73+
74+
75+
76+
## [3.1.17] - 2016-09-25
77+
78+
### Fixed
79+
- Raw DB queries did not account for DB prefixes;
80+
81+
82+
## [3.1.16] - 2016-09-22
83+
84+
### Added
85+
- Radio field and column - thanks to [Owen Melbourne](https://github.com/OwenMelbz);
86+
87+
88+
## [3.1.15] - 2016-09-21
89+
90+
### Fixed
91+
- Missing $fillable item in model will now throw correct error, because _token is ignored;
92+
- Correct and complete language files;
93+
94+
95+
## [3.1.14] - 2016-09-19
96+
97+
### Fixed
98+
- Checkbox storing issue in Laravel 5.3 - #115 thanks to [timdiels1](https://github.com/timdiels1);
99+
100+
101+
## [3.1.13] - 2016-09-19
102+
103+
### Added
104+
- Revisions functionality, thanks to [se1exin](https://github.com/se1exin);
105+
106+
23107
## [3.1.12] - 2016-09-19
24108

25109
### Added

README.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Features:
2323
- Easily extend fields (customising a field type or adding a new one is as easy as creating a new view with a particular name)
2424
- Easily overwrite functionality (customising how the create/update/delete/reorder process works is as easy as creating a new function with the proper name in your EntityCrudCrontroller)
2525

26-
**Subscribe to the [Mailchimp list](http://eepurl.com/bUEGjf) to be announced of any major features or breaking changes (once every 1-3 months).**
26+
**Subscribe to the [Mailchimp list](http://eepurl.com/bUEGjf) to be announced of any major features or breaking changes (once every 1-3 months).**
2727

2828
![List / table view for Backpack/CRUD](https://dl.dropboxusercontent.com/u/2431352/backpack_crud_list.png)
2929

@@ -59,14 +59,14 @@ $ php artisan vendor:publish --provider="Backpack\CRUD\CrudServiceProvider" --ta
5959

6060
5) If you haven't already, go through [steps 3-5 from the Backpack\Base installation](https://github.com/Laravel-Backpack/Base#install) (it provides the general views for the admin panel - layout, menu, notification bubbles, etc).
6161

62-
6) [Optional] You can now the file manager to the menu, in resources/views/vendor/backpack/base/inc/sidebar.blade.php or menu.blade.php:
62+
6) [Optional] You can now the file manager to the menu, in `resources/views/vendor/backpack/base/inc/sidebar.blade.php` or `menu.blade.php`:
6363
```html
6464
<li><a href="{{ url('admin/elfinder') }}"><i class="fa fa-files-o"></i> <span>File manager</span></a></li>
6565
```
6666

6767
## Usage
6868

69-
Check out the documentation at https://laravelbackpack.com
69+
Check out the documentation at https://laravelbackpack.com
7070

7171

7272
In short:
@@ -79,6 +79,40 @@ In short:
7979

8080
4. **(optional)** Define your validation rules in a Request files.
8181

82+
83+
## **(Optional)** Enable Revisions
84+
85+
CRUD supports tracking and restoring Model change Revisions with the help of [VentureCraft/revisionable](https://github.com/VentureCraft/revisionable).
86+
87+
To enable revisions on your Model do the following:
88+
89+
1. Run:
90+
```bash
91+
$ php artisan migrate --path=vendor/venturecraft/revisionable/src/migrations #run revisionable migrations
92+
```
93+
94+
2. Add the `\Venturecraft\Revisionable\RevisionableTrait` Trait to your Model. E.g:
95+
```php
96+
namespace MyApp\Models;
97+
98+
class Article extends Eloquent {
99+
use \Backpack\CRUD\CrudTrait, \Venturecraft\Revisionable\RevisionableTrait;
100+
101+
// If you are using another bootable trait the be sure to override the boot method in your model
102+
public static function boot()
103+
{
104+
parent::boot();
105+
}
106+
}
107+
```
108+
109+
3. Enable access to Revisions in your CrudController with:
110+
```php
111+
$this->crud->allowAccess('revisions');
112+
```
113+
114+
Head on over to the [VentureCraft/revisionable](https://github.com/VentureCraft/revisionable) GitHub repo to see the full documentation and extra configuration options.
115+
82116
## Screenshots
83117

84118
- List view pictured above.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"barryvdh/laravel-elfinder": "^0.3.5",
2828
"livecontrol/eloquent-datatable": "^0.1.5",
2929
"doctrine/dbal": "^2.5",
30+
"venturecraft/revisionable": "1.*",
3031
"intervention/image": "^2.3"
3132
},
3233
"require-dev": {

src/CrudPanel.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
use Backpack\CRUD\PanelTraits\Read;
1616
use Backpack\CRUD\PanelTraits\Reorder;
1717
use Backpack\CRUD\PanelTraits\Update;
18+
use Backpack\CRUD\PanelTraits\ViewsAndRestoresRevisions;
19+
use Backpack\CRUD\PanelTraits\AutoFocus;
1820

1921
class CrudPanel
2022
{
21-
use Create, Read, Update, Delete, Reorder, Access, Columns, Fields, Query, Buttons, AutoSet, FakeFields, FakeColumns;
23+
use Create, Read, Update, Delete, Reorder, Access, Columns, Fields, Query, Buttons, AutoSet, FakeFields, FakeColumns, ViewsAndRestoresRevisions, AutoFocus;
2224

2325
// --------------
2426
// CRUD variables
@@ -34,7 +36,7 @@ class CrudPanel
3436
public $entity_name = 'entry'; // what name will show up on the buttons, in singural (ex: Add entity)
3537
public $entity_name_plural = 'entries'; // what name will show up on the buttons, in plural (ex: Delete 5 entities)
3638

37-
public $access = ['list', 'create', 'update', 'delete'/* 'reorder', 'show', 'details_row' */];
39+
public $access = ['list', 'create', 'update', 'delete'/* 'revisions', reorder', 'show', 'details_row' */];
3840

3941
public $reorder = false;
4042
public $reorder_label = false;

src/CrudServiceProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ public static function resource($name, $controller, array $options = [])
106106
'as' => 'crud.'.$name.'.translateItem',
107107
'uses' => $controller.'@translateItem',
108108
]);
109+
Route::get($name.'/{id}/revisions', [
110+
'as' => 'crud.'.$name.'.listRevisions',
111+
'uses' => $controller.'@listRevisions',
112+
]);
113+
Route::post($name.'/{id}/revisions/{revisionId}/restore', [
114+
'as' => 'crud.'.$name.'.restoreRevision',
115+
'uses' => $controller.'@restoreRevision',
116+
]);
109117

110118
$options_with_default_route_names = array_merge([
111119
'names' => [

src/CrudTrait.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DB;
66
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Support\Facades\Config;
78

89
trait CrudTrait
910
{
@@ -16,7 +17,7 @@ trait CrudTrait
1617
public static function getPossibleEnumValues($field_name)
1718
{
1819
$instance = new static(); // create an instance of the model to be able to get the table name
19-
$type = DB::select(DB::raw('SHOW COLUMNS FROM '.$instance->getTable().' WHERE Field = "'.$field_name.'"'))[0]->Type;
20+
$type = DB::select(DB::raw('SHOW COLUMNS FROM '.Config::get('database.connections.'.env('DB_CONNECTION').'.prefix').$instance->getTable().' WHERE Field = "'.$field_name.'"'))[0]->Type;
2021
preg_match('/^enum\((.*)\)$/', $type, $matches);
2122
$enum = [];
2223
foreach (explode(',', $matches[1]) as $value) {
@@ -29,7 +30,7 @@ public static function getPossibleEnumValues($field_name)
2930
public static function isColumnNullable($column_name)
3031
{
3132
$instance = new static(); // create an instance of the model to be able to get the table name
32-
$answer = DB::select(DB::raw("SELECT IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='".$instance->getTable()."' AND COLUMN_NAME='".$column_name."' AND table_schema='".env('DB_DATABASE')."'"))[0];
33+
$answer = DB::select(DB::raw("SELECT IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='".Config::get('database.connections.'.env('DB_CONNECTION').'.prefix').$instance->getTable()."' AND COLUMN_NAME='".$column_name."' AND table_schema='".env('DB_DATABASE')."'"))[0];
3334

3435
return $answer->IS_NULLABLE === 'YES';
3536
}

src/PanelTraits/AutoFocus.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Backpack\CRUD\PanelTraits;
4+
5+
trait AutoFocus
6+
{
7+
public $autoFocusOnFirstField = true;
8+
9+
public function getAutoFocusOnFirstField()
10+
{
11+
return $this->autoFocusOnFirstField;
12+
}
13+
14+
public function setAutoFocusOnFirstField($value)
15+
{
16+
return $this->autoFocusOnFirstField = (bool) $value;
17+
}
18+
19+
public function enableAutoFocus()
20+
{
21+
return $this->setAutoFocusOnFirstField(true);
22+
}
23+
24+
public function disableAutoFocus()
25+
{
26+
return $this->setAutoFocusOnFirstField(false);
27+
}
28+
}

src/PanelTraits/Buttons.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function initButtons()
6767
// line stack
6868
$this->addButton('line', 'preview', 'view', 'crud::buttons.preview', 'end');
6969
$this->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
70+
$this->addButton('line', 'revisions', 'view', 'crud::buttons.revisions', 'end');
7071
$this->addButton('line', 'delete', 'view', 'crud::buttons.delete', 'end');
7172

7273
// top stack
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Backpack\CRUD\PanelTraits;
4+
5+
use Venturecraft\Revisionable\Revision;
6+
7+
trait ViewsAndRestoresRevisions
8+
{
9+
/**
10+
* Build a list of Revisions, grouped by revision date.
11+
*
12+
* @return [Array] of revision groups, keyed by revision date
13+
*/
14+
public function listRevisions($id)
15+
{
16+
$revisions = [];
17+
// Group revisions by change date
18+
foreach ($this->getEntry($id)->revisionHistory as $history) {
19+
20+
// Get just the date from the revision created timestamp
21+
$revisionDate = date('Y-m-d', strtotime((string) $history->created_at));
22+
23+
// Be sure to instantiate the initial grouping array
24+
if (! array_key_exists($revisionDate, $revisions)) {
25+
$revisions[$revisionDate] = [];
26+
}
27+
28+
// Push onto the top of the current group - so we get orderBy decending timestamp
29+
array_unshift($revisions[$revisionDate], $history);
30+
}
31+
32+
// Sort the array by timestamp descending (so that the most recent are at the top)
33+
arsort($revisions);
34+
35+
return $revisions;
36+
}
37+
38+
/**
39+
* Restore a single revision.
40+
*
41+
* @param [int] $id The ID of the source CRUD Model instance to update
42+
* @param [int] $revisionId The ID of the revision to use for the update
43+
*/
44+
public function restoreRevision($id, $revisionId)
45+
{
46+
$entry = $this->getEntry($id);
47+
$revision = Revision::findOrFail($revisionId);
48+
49+
// Update the revisioned field with the old value
50+
$entry->{$revision->fieldName()} = $revision->oldValue();
51+
$entry->save();
52+
53+
// Reload the entry so we have the latest revisions
54+
$entry = $this->getEntry($id);
55+
}
56+
}

0 commit comments

Comments
 (0)