Completed
Push — master ( b3827e...6a4c7f )
by
unknown
10:08
created

MiniCrudSingle::setKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
namespace Anavel\Crud\Abstractor\Eloquent\Relation;
3
4
use Anavel\Crud\Abstractor\Eloquent\Relation\Traits\CheckRelationCompatibility;
5
use Anavel\Crud\Contracts\Abstractor\Field;
6
use App;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Collection;
10
11
class MiniCrudSingle extends Relation
12
{
13
    use CheckRelationCompatibility;
14
15
    protected $compatibleEloquentRelations = array(
16
        'Illuminate\Database\Eloquent\Relations\MorphOne'
17
    );
18
19 5
    public function setup()
20
    {
21 5
        $this->checkRelationCompatibility();
22 4
    }
23
24
    /**
25
     * @return array
26
     */
27 1
    public function getEditFields($arrayKey = null)
28
    {
29 1
        if(empty($arrayKey)) {
30 1
            $arrayKey = $this->name;
31 1
        }
32
33 1
        $fields = [];
34
35 1
        $columns = $this->modelAbstractor->getColumns('edit');
36
37
        /** @var Model $result */
38 1
        $result = $this->eloquentRelation->getResults();
39
40
        $readOnly = [
41 1
            Model::CREATED_AT,
42 1
            Model::UPDATED_AT,
43 1
            $this->eloquentRelation->getPlainForeignKey(),
44 1
            $this->eloquentRelation->getPlainMorphType(),
45 1
            $this->eloquentRelation->getParent()->getKeyName()
46 1
        ];
47
48
49 1
        $this->readConfig('edit');
50
51 1
        if (! empty($columns)) {
52
            //Add field for model deletion
53
            $config = [
54 1
                'name' => '__delete',
55 1
                'presentation' => 'Delete',
56 1
                'form_type' => 'checkbox',
57 1
                'no_validate' => true,
58 1
                'validation' => null,
59
                'functions' => null
60 1
            ];
61
62
            /** @var Field $field */
63 1
            $field = $this->fieldFactory
64 1
                ->setConfig($config)
65 1
                ->get();
66 1
            $fields[$arrayKey]['__delete'] = $field;
67
68 1
            foreach ($columns as $columnName => $column) {
69 1
                if (in_array($columnName, $readOnly, true)) {
70
                    continue;
71
                }
72
73 1
                $formType = null;
74
75
                $config = [
76 1
                    'name'         => $columnName,
77 1
                    'presentation' => $this->name . ' ' . ucfirst(transcrud($columnName)),
78 1
                    'form_type'    => $formType,
79 1
                    'no_validate'  => true,
80 1
                    'validation'   => null,
81
                    'functions'    => null
82 1
                ];
83
84 1
                $config = $this->setConfig($config, $columnName);
85
86
                /** @var Field $field */
87 1
                $field = $this->fieldFactory
88 1
                    ->setColumn($column)
89 1
                    ->setConfig($config)
90 1
                    ->get();
91
92 1
                if (! empty($result->id)) {
93 1
                    $field->setValue($result->getAttribute($columnName));
94 1
                }
95
96 1
                $fields[$arrayKey][$columnName] = $field;
97 1
            }
98 1
        }
99
100 1
        $fields = $this->addSecondaryRelationFields($fields);
101
102
103 1
        return $fields;
104
    }
105
106
    /**
107
     * @param array|null $relationArray
108
     * @return mixed
109
     */
110 2
    public function persist(array $relationArray = null, Request $request)
111
    {
112 2
        if (! empty($relationArray)) {
113 2
            $currentRelation = $this->eloquentRelation->getResults();
114 2
            if (! empty($currentRelation)) {
115 1
                $relationModel = $currentRelation;
116 1
            } else {
117 1
                $relationModel = $this->eloquentRelation->getRelated()->newInstance();
118
            }
119
120 2
            $this->setKeys($relationModel);
121
122 2
            $shouldBeSkipped = true;
123 2
            foreach ($relationArray as $fieldKey => $fieldValue) {
124
                // This field can only come from existing models
125 2
                if ($fieldKey === '__delete') {
126
                    $relationModel->delete();
127
                    $shouldBeSkipped = true;
128
                    break;
129
                }
130 2
                if ($shouldBeSkipped) {
131 2
                    $shouldBeSkipped = ($shouldBeSkipped === ($fieldValue === ''));
132 2
                }
133 2
                $relationModel->setAttribute($fieldKey, $fieldValue);
134 2
            }
135
136 2
            if (! $shouldBeSkipped) {
137 2
                $relationModel->save();
138 2
            }
139 2
        }
140 2
    }
141
142 2
    protected function setKeys(Model $relationModel)
143
    {
144 2
        $relationModel->setAttribute($this->eloquentRelation->getForeignKey(), $this->relatedModel->id);
145 2
        $relationModel->setAttribute($this->eloquentRelation->getPlainMorphType(),
146 2
            $this->eloquentRelation->getMorphClass());
147 2
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getDisplayType()
153
    {
154
        return self::DISPLAY_TYPE_TAB;
155
    }
156
}
157
158