| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SleepingOwl\Admin\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Database\Eloquent\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Database\Eloquent\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SleepingOwl\Admin\Contracts\Form\Element\Taggable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SleepingOwl\Admin\Contracts\Form\Element\HasSyncCallback; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SleepingOwl\Admin\Contracts\Form\Element\MustDeleteRelatedItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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())) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $attribute = $this->getModelAttributeKey(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if (is_null($request->input($this->getPath()))) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $values = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $values = $this->getValueFromModel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $relation = $this->getModel()->{$attribute}(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if ($relation instanceof \Illuminate\Database\Eloquent\Relations\BelongsToMany) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $this->syncBelongsToManyRelation($relation, $values); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } elseif ($relation instanceof \Illuminate\Database\Eloquent\Relations\HasMany) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $this->deleteOldItemsFromHasManyRelation($relation, $values); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $this->attachItemsToHasManyRelation($relation, $values); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     $model = clone $this->getModelForOptions(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                     $model->{$this->getDisplay()} = $value; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 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()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                 $model->{$this->getDisplay()} = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |                 $item = $model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             $relation->save($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 160 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |  |