LaravelRUS /
SleepingOwlAdmin
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace SleepingOwl\Admin\Traits; |
||||||
| 4 | |||||||
| 5 | use Illuminate\Database\Eloquent\Collection; |
||||||
| 6 | use Illuminate\Database\Eloquent\Model; |
||||||
| 7 | use SleepingOwl\Admin\Contracts\Form\Element\HasSyncCallback; |
||||||
| 8 | use SleepingOwl\Admin\Contracts\Form\Element\MustDeleteRelatedItem; |
||||||
| 9 | use SleepingOwl\Admin\Contracts\Form\Element\Taggable; |
||||||
| 10 | |||||||
| 11 | trait ElementSaveRelation |
||||||
| 12 | { |
||||||
| 13 | /** |
||||||
| 14 | * @return array|string |
||||||
| 15 | */ |
||||||
| 16 | public function getValueFromModel() |
||||||
| 17 | { |
||||||
| 18 | $value = parent::getValueFromModel(); |
||||||
| 19 | |||||||
| 20 | if (is_array($value)) { |
||||||
| 21 | foreach ($value as $key => $val) { |
||||||
| 22 | $value[$key] = $val; |
||||||
| 23 | } |
||||||
| 24 | } |
||||||
| 25 | |||||||
| 26 | if ($value instanceof Collection && $value->count() > 0) { |
||||||
| 27 | $value = $value->pluck($value->first()->getKeyName())->all(); |
||||||
| 28 | } |
||||||
| 29 | |||||||
| 30 | if ($value instanceof Collection) { |
||||||
| 31 | $value = $value->toArray(); |
||||||
| 32 | } |
||||||
| 33 | |||||||
| 34 | return $value; |
||||||
| 35 | } |
||||||
| 36 | |||||||
| 37 | /** |
||||||
| 38 | * @param \Illuminate\Http\Request $request |
||||||
| 39 | * |
||||||
| 40 | * @return void |
||||||
| 41 | */ |
||||||
| 42 | public function save(\Illuminate\Http\Request $request) |
||||||
| 43 | { |
||||||
| 44 | if (is_null($this->getModelForOptions())) { |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 45 | parent::save($request); |
||||||
| 46 | } |
||||||
| 47 | } |
||||||
| 48 | |||||||
| 49 | /** |
||||||
| 50 | * @param \Illuminate\Http\Request $request |
||||||
| 51 | * |
||||||
| 52 | * @return void |
||||||
| 53 | */ |
||||||
| 54 | public function afterSave(\Illuminate\Http\Request $request) |
||||||
| 55 | { |
||||||
| 56 | if (is_null($this->getModelForOptions())) { |
||||||
| 57 | return; |
||||||
| 58 | } |
||||||
| 59 | |||||||
| 60 | if ($this->isValueSkipped()) { |
||||||
|
0 ignored issues
–
show
It seems like
isValueSkipped() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 61 | return; |
||||||
| 62 | } |
||||||
| 63 | |||||||
| 64 | $attribute = $this->getModelAttributeKey(); |
||||||
|
0 ignored issues
–
show
It seems like
getModelAttributeKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 65 | |||||||
| 66 | if (is_null($request->input($this->getPath()))) { |
||||||
|
0 ignored issues
–
show
It seems like
getPath() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 67 | $values = []; |
||||||
| 68 | } else { |
||||||
| 69 | $values = $this->getValueFromModel(); |
||||||
| 70 | } |
||||||
| 71 | |||||||
| 72 | $relation = $this->getModel()->{$attribute}(); |
||||||
|
0 ignored issues
–
show
It seems like
getModel() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 73 | if ($relation instanceof \Illuminate\Database\Eloquent\Relations\BelongsToMany) { |
||||||
| 74 | $this->syncBelongsToManyRelation($relation, $values); |
||||||
|
0 ignored issues
–
show
It seems like
$values can also be of type string; however, parameter $values of SleepingOwl\Admin\Traits...BelongsToManyRelation() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 75 | } elseif ($relation instanceof \Illuminate\Database\Eloquent\Relations\HasMany) { |
||||||
| 76 | $this->deleteOldItemsFromHasManyRelation($relation, $values); |
||||||
|
0 ignored issues
–
show
It seems like
$values can also be of type string; however, parameter $values of SleepingOwl\Admin\Traits...msFromHasManyRelation() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 77 | $this->attachItemsToHasManyRelation($relation, $values); |
||||||
|
0 ignored issues
–
show
It seems like
$values can also be of type string; however, parameter $values of SleepingOwl\Admin\Traits...temsToHasManyRelation() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 78 | } |
||||||
| 79 | } |
||||||
| 80 | |||||||
| 81 | /** |
||||||
| 82 | * @param \Illuminate\Database\Eloquent\Relations\BelongsToMany $relation |
||||||
| 83 | * @param array $values |
||||||
| 84 | * |
||||||
| 85 | * @return void |
||||||
| 86 | */ |
||||||
| 87 | protected function syncBelongsToManyRelation( |
||||||
| 88 | \Illuminate\Database\Eloquent\Relations\BelongsToMany $relation, |
||||||
| 89 | array $values |
||||||
| 90 | ) { |
||||||
| 91 | if ($this instanceof Taggable) { |
||||||
| 92 | foreach ($values as $i => $value) { |
||||||
| 93 | if (! array_key_exists($value, $this->getOptions()) && $this->isTaggable()) { |
||||||
|
0 ignored issues
–
show
The method
getOptions() does not exist on SleepingOwl\Admin\Contracts\Form\Element\Taggable. Since it exists in all sub-types, consider adding an abstract or default implementation to SleepingOwl\Admin\Contracts\Form\Element\Taggable.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
getOptions() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 94 | $model = clone $this->getModelForOptions(); |
||||||
|
0 ignored issues
–
show
The method
getModelForOptions() does not exist on SleepingOwl\Admin\Contracts\Form\Element\Taggable. Since it exists in all sub-types, consider adding an abstract or default implementation to SleepingOwl\Admin\Contracts\Form\Element\Taggable.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 95 | $model->{$this->getDisplay()} = $value; |
||||||
|
0 ignored issues
–
show
It seems like
getDisplay() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The method
getDisplay() does not exist on SleepingOwl\Admin\Contracts\Form\Element\Taggable. Since it exists in all sub-types, consider adding an abstract or default implementation to SleepingOwl\Admin\Contracts\Form\Element\Taggable.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 96 | $model->save(); |
||||||
| 97 | |||||||
| 98 | $values[$i] = $model->getKey(); |
||||||
| 99 | } |
||||||
| 100 | } |
||||||
| 101 | } |
||||||
| 102 | |||||||
| 103 | if (($this instanceof HasSyncCallback) && is_callable($callback = $this->getSyncCallback())) { |
||||||
| 104 | $callbackModel = $this->getModel(); |
||||||
|
0 ignored issues
–
show
The method
getModel() does not exist on SleepingOwl\Admin\Contra...Element\HasSyncCallback. Since it exists in all sub-types, consider adding an abstract or default implementation to SleepingOwl\Admin\Contra...Element\HasSyncCallback.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 105 | $callback($values, $callbackModel); |
||||||
| 106 | |||||||
| 107 | return; |
||||||
| 108 | } |
||||||
| 109 | |||||||
| 110 | $relation->sync($values); |
||||||
| 111 | } |
||||||
| 112 | |||||||
| 113 | /** |
||||||
| 114 | * @param \Illuminate\Database\Eloquent\Relations\HasMany $relation |
||||||
| 115 | * @param array $values |
||||||
| 116 | */ |
||||||
| 117 | protected function deleteOldItemsFromHasManyRelation( |
||||||
| 118 | \Illuminate\Database\Eloquent\Relations\HasMany $relation, |
||||||
| 119 | array $values |
||||||
| 120 | ) { |
||||||
| 121 | $items = $relation->get(); |
||||||
| 122 | |||||||
| 123 | foreach ($items as $item) { |
||||||
| 124 | if (! in_array($item->getKey(), $values)) { |
||||||
| 125 | if (($this instanceof MustDeleteRelatedItem) && $this->isDeleteRelatedItem()) { |
||||||
| 126 | $item->delete(); |
||||||
| 127 | } else { |
||||||
| 128 | $item->{$this->getForeignKeyNameFromRelation($relation)} = null; |
||||||
|
0 ignored issues
–
show
It seems like
getForeignKeyNameFromRelation() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 129 | $item->save(); |
||||||
| 130 | } |
||||||
| 131 | } |
||||||
| 132 | } |
||||||
| 133 | } |
||||||
| 134 | |||||||
| 135 | /** |
||||||
| 136 | * @param \Illuminate\Database\Eloquent\Relations\HasMany $relation |
||||||
| 137 | * @param array $values |
||||||
| 138 | */ |
||||||
| 139 | protected function attachItemsToHasManyRelation( |
||||||
| 140 | \Illuminate\Database\Eloquent\Relations\HasMany $relation, |
||||||
| 141 | array $values |
||||||
| 142 | ) { |
||||||
| 143 | foreach ($values as $i => $value) { |
||||||
| 144 | /** @var Model $model */ |
||||||
| 145 | $model = clone $this->getModelForOptions(); |
||||||
| 146 | $item = $model->find($value); |
||||||
| 147 | |||||||
| 148 | if (is_null($item)) { |
||||||
| 149 | if (! ($this instanceof Taggable) && $this->isTaggable()) { |
||||||
|
0 ignored issues
–
show
It seems like
isTaggable() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 150 | continue; |
||||||
| 151 | } |
||||||
| 152 | |||||||
| 153 | $model->{$this->getDisplay()} = $value; |
||||||
| 154 | $item = $model; |
||||||
| 155 | } |
||||||
| 156 | |||||||
| 157 | $relation->save($item); |
||||||
| 158 | } |
||||||
| 159 | } |
||||||
| 160 | } |
||||||
| 161 |