Skip to content

Commit 43e5d20

Browse files
committed
Merge branch 'master' into fix-enum-sqlite
2 parents a6aa917 + e019150 commit 43e5d20

File tree

140 files changed

+12089
-21346
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+12089
-21346
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ src/public/packages/nestedSortable/package.json
2424
src/public/packages/source-sans-pro/package.json
2525
src/public/packages/tinymce/package.json
2626
src/public/packages/tinymce/composer.json
27+

package-lock.json

Lines changed: 11533 additions & 20740 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
"devDependencies": {
1313
"cross-env": "^7.0.3",
1414
"css-loader": "^5.2.6",
15-
"laravel-mix": "^6.0.19",
15+
"laravel-mix": "^6.0.27",
1616
"lodash": "^4.17.21",
1717
"pace": "0.0.4",
18-
"resolve-url-loader": "^2.3.2",
19-
"sass": "^1.35.1",
18+
"resolve-url-loader": "^4.0.0",
19+
"sass": "^1.37.0",
2020
"sass-loader": "^9.0.3",
2121
"vue-template-compiler": "^2.6.14"
2222
},
@@ -25,7 +25,7 @@
2525
"@digitallyhappy/backstrap": "^0.3.4",
2626
"animate.css": "^3.7.2",
2727
"bootstrap": "^4.6.0",
28-
"bootstrap-colorpicker": "^3.3.0",
28+
"bootstrap-colorpicker": "^3.4.0",
2929
"bootstrap-datepicker": "^1.9.0",
3030
"bootstrap-daterangepicker": "^3.1.0",
3131
"bootstrap-iconpicker": "^1.8.2",
@@ -49,7 +49,7 @@
4949
"noty": "^3.2.0-beta",
5050
"pace-js": "^1.2.4",
5151
"pc-bootstrap4-datetimepicker": "^4.17.51",
52-
"perfect-scrollbar": "^1.5.0",
52+
"perfect-scrollbar": "^1.5.2",
5353
"places.js": "^1.19.0",
5454
"popper.js": "^1.16.1",
5555
"select2": "^4.0.13",

src/app/Library/CrudPanel/Traits/Create.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ public function create($data)
2525
{
2626
$data = $this->decodeJsonCastedAttributes($data);
2727
$data = $this->compactFakeFields($data);
28+
$data = $this->changeBelongsToNamesFromRelationshipToForeignKey($data);
2829

2930
// omit the n-n relationships when updating the eloquent item
3031
$nn_relationships = Arr::pluck($this->getRelationFieldsWithPivot(), 'name');
32+
3133
$item = $this->model->create(Arr::except($data, $nn_relationships));
3234

3335
// if there are any relationships available, also sync those
@@ -237,25 +239,4 @@ private function getRelationDataFromFormData($data)
237239

238240
return $relationData;
239241
}
240-
241-
public function getOnlyRelationEntity($relation_field)
242-
{
243-
$entity_array = explode('.', $relation_field['entity']);
244-
245-
$relation_model = $this->getRelationModel($relation_field['entity'], -1);
246-
247-
$related_method = Arr::last($entity_array);
248-
249-
if (! method_exists($relation_model, $related_method)) {
250-
if (count($entity_array) <= 1) {
251-
return $relation_field['entity'];
252-
} else {
253-
array_pop($entity_array);
254-
}
255-
256-
return implode('.', $entity_array);
257-
}
258-
259-
return $relation_field['entity'];
260-
}
261242
}

src/app/Library/CrudPanel/Traits/FieldsProtectedMethods.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -151,24 +151,6 @@ protected function makeSureFieldHasEntity($field)
151151
return $field;
152152
}
153153

154-
protected function makeSureFieldHasRelationshipData($field)
155-
{
156-
// only do this if "entity" is defined on the field
157-
if (! isset($field['entity'])) {
158-
return $field;
159-
}
160-
161-
$extraFieldAttributes = $this->inferFieldAttributesFromRelationship($field);
162-
163-
if (! empty($extraFieldAttributes)) {
164-
$field = array_merge($field, $extraFieldAttributes);
165-
} else {
166-
abort(500, 'Unable to process relationship data: '.$field['name']);
167-
}
168-
169-
return $field;
170-
}
171-
172154
protected function overwriteFieldNameFromEntity($field)
173155
{
174156
// if the entity doesn't have a dot, it means we don't need to overwrite the name

src/app/Library/CrudPanel/Traits/Relationships.php

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Backpack\CRUD\app\Library\CrudPanel\Traits;
44

55
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Str;
67

78
trait Relationships
89
{
@@ -15,33 +16,19 @@ trait Relationships
1516
public function getRelationInstance($field)
1617
{
1718
$entity = $this->getOnlyRelationEntity($field);
18-
$entity_array = explode('.', $entity);
19-
$relation_model = $this->getRelationModel($entity);
20-
21-
$related_method = Arr::last($entity_array);
22-
if (count(explode('.', $entity)) == count(explode('.', $field['entity']))) {
23-
$relation_model = $this->getRelationModel($entity, -1);
24-
}
25-
$relation_model = new $relation_model();
26-
27-
//if counts are diferent means that last element of entity is the field in relation.
28-
if (count(explode('.', $entity)) != count(explode('.', $field['entity']))) {
29-
if (in_array($related_method, $relation_model->getFillable())) {
30-
if (count($entity_array) > 1) {
31-
$related_method = $entity_array[(count($entity_array) - 2)];
32-
$relation_model = $this->getRelationModel($entity, -2);
33-
} else {
34-
$relation_model = $this->model;
35-
}
36-
}
37-
}
38-
if (count($entity_array) == 1) {
39-
if (method_exists($this->model, $related_method)) {
40-
return $this->model->{$related_method}();
19+
$possible_method = Str::before($entity, '.');
20+
$model = $this->model;
21+
22+
if (method_exists($model, $possible_method)) {
23+
$parts = explode('.', $entity);
24+
// here we are going to iterate through all relation parts to check
25+
foreach ($parts as $i => $part) {
26+
$relation = $model->$part();
27+
$model = $relation->getRelated();
4128
}
42-
}
4329

44-
return $relation_model->{$related_method}();
30+
return $relation;
31+
}
4532
}
4633

4734
/**
@@ -70,6 +57,41 @@ public function inferRelationTypeFromRelationship($field)
7057
return Arr::last(explode('\\', get_class($relation)));
7158
}
7259

60+
public function getOnlyRelationEntity($relation_field)
61+
{
62+
$relation_model = $this->getRelationModel($relation_field['entity'], -1);
63+
$related_method = Str::afterLast($relation_field['entity'], '.');
64+
65+
if (! method_exists($relation_model, $related_method)) {
66+
return Str::beforeLast($relation_field['entity'], '.');
67+
}
68+
69+
return $relation_field['entity'];
70+
}
71+
72+
/**
73+
* Get the fields for relationships, according to the relation type. It looks only for direct
74+
* relations - it will NOT look through relationships of relationships.
75+
*
76+
* @param string|array $relation_types Eloquent relation class or array of Eloquent relation classes. Eg: BelongsTo
77+
*
78+
* @return array The fields with corresponding relation types.
79+
*/
80+
public function getFieldsWithRelationType($relation_types): array
81+
{
82+
$relation_types = (array) $relation_types;
83+
84+
return collect($this->fields())
85+
->where('model')
86+
->whereIn('relation_type', $relation_types)
87+
->filter(function ($item) {
88+
$related_model = get_class($this->model->{Str::before($item['entity'], '.')}()->getRelated());
89+
90+
return Str::contains($item['entity'], '.') && $item['model'] !== $related_model ? false : true;
91+
})
92+
->toArray();
93+
}
94+
7395
/**
7496
* Parse the field name back to the related entity after the form is submited.
7597
* Its called in getAllFieldNames().
@@ -96,6 +118,21 @@ public function parseRelationFieldNamesFromHtml($fields)
96118
return $fields;
97119
}
98120

121+
protected function changeBelongsToNamesFromRelationshipToForeignKey($data)
122+
{
123+
$belongs_to_fields = $this->getFieldsWithRelationType('BelongsTo');
124+
125+
foreach ($belongs_to_fields as $relation_field) {
126+
$relation = $this->getRelationInstance($relation_field);
127+
if (Arr::has($data, $relation->getRelationName())) {
128+
$data[$relation->getForeignKeyName()] = Arr::get($data, $relation->getRelationName());
129+
unset($data[$relation->getRelationName()]);
130+
}
131+
}
132+
133+
return $data;
134+
}
135+
99136
/**
100137
* Based on relation type returns the default field type.
101138
*

src/app/Library/CrudPanel/Traits/Update.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public function update($id, $data)
2626
$data = $this->compactFakeFields($data);
2727
$item = $this->model->findOrFail($id);
2828

29+
$data = $this->changeBelongsToNamesFromRelationshipToForeignKey($data);
30+
2931
$this->createRelations($item, $data);
3032

3133
// omit the n-n relationships when updating the eloquent item

src/app/Models/Traits/SpatieTranslatable/Sluggable.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,15 @@
44

55
use Cviebrock\EloquentSluggable\Sluggable as OriginalSluggable;
66
use Illuminate\Database\Eloquent\Builder;
7-
use Illuminate\Database\Eloquent\Model;
87

98
trait Sluggable
109
{
1110
use OriginalSluggable;
1211

13-
/**
14-
* Hook into the Eloquent model events to create or
15-
* update the slug as required.
16-
*/
17-
public static function bootSluggable()
18-
{
19-
static::observe(app(SluggableObserver::class));
20-
}
21-
22-
/**
23-
* Clone the model into a new, non-existing instance.
24-
*
25-
* @param array|null $except
26-
*
27-
* @return Model
28-
*/
29-
public function replicate(array $except = null): Model
30-
{
31-
$instance = parent::replicate($except);
32-
(new SlugService())->slug($instance, true);
33-
34-
return $instance;
35-
}
36-
3712
/**
3813
* Query scope for finding "similar" slugs, used to determine uniqueness.
3914
*
4015
* @param \Illuminate\Database\Eloquent\Builder $query
41-
* @param \Illuminate\Database\Eloquent\Model $model
4216
* @param string $attribute
4317
* @param array $config
4418
* @param string $slug

src/app/Models/Traits/SpatieTranslatable/SluggableObserver.php

Lines changed: 0 additions & 84 deletions
This file was deleted.

src/app/Models/Traits/SpatieTranslatable/SluggableScopeHelpers.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,4 @@ public function scopeWhereSlug(Builder $scope, string $slug): Builder
2121
{
2222
return $scope->where($this->getSlugKeyName().'->'.$this->getLocale(), $slug);
2323
}
24-
25-
/**
26-
* Find a model by its primary slug.
27-
*
28-
* @param string $slug
29-
* @param array $columns
30-
*
31-
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|static[]|static|null
32-
*/
33-
public static function findBySlug(string $slug, array $columns = ['*'])
34-
{
35-
return static::whereSlug($slug)->first($columns);
36-
}
37-
38-
/**
39-
* Find a model by its primary slug or throw an exception.
40-
*
41-
* @param string $slug
42-
* @param array $columns
43-
*
44-
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
45-
*
46-
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection
47-
*/
48-
public static function findBySlugOrFail(string $slug, array $columns = ['*'])
49-
{
50-
return static::whereSlug($slug)->firstOrFail($columns);
51-
}
5224
}

0 commit comments

Comments
 (0)