1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Repositories; |
4
|
|
|
|
5
|
|
|
use A17\Twill\Models\Behaviors\HasFiles; |
6
|
|
|
use A17\Twill\Models\Behaviors\HasMedias; |
7
|
|
|
use A17\Twill\Models\Block; |
8
|
|
|
use A17\Twill\Repositories\Behaviors\HandleFiles; |
9
|
|
|
use A17\Twill\Repositories\Behaviors\HandleMedias; |
10
|
|
|
use A17\Twill\Services\Blocks\BlockCollection; |
11
|
|
|
use Illuminate\Config\Repository as Config; |
12
|
|
|
use Illuminate\Support\Collection; |
13
|
|
|
use Log; |
14
|
|
|
use ReflectionException; |
15
|
|
|
use Schema; |
16
|
|
|
|
17
|
|
|
class BlockRepository extends ModuleRepository |
18
|
|
|
{ |
19
|
|
|
use HandleMedias, HandleFiles; |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Config |
23
|
|
|
*/ |
24
|
|
|
protected $config; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Block $model |
28
|
|
|
*/ |
29
|
22 |
|
public function __construct(Block $model, Config $config) |
30
|
|
|
{ |
31
|
22 |
|
$this->model = $model; |
|
|
|
|
32
|
22 |
|
$this->config = $config; |
33
|
22 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $role |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function getCrops($role) |
40
|
|
|
{ |
41
|
|
|
return $this->config->get('twill.block_editor.crops')[$role]; |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
public function hydrate($object, $fields) |
45
|
|
|
{ |
46
|
2 |
|
if (Schema::hasTable(config('twill.related_table', 'twill_related'))) { |
47
|
2 |
|
$relatedItems = Collection::make(); |
48
|
|
|
|
49
|
|
|
Collection::make($fields['browsers'])->each(function ($items, $browserName) use (&$relatedItems) { |
50
|
|
|
Collection::make($items)->each(function ($item) use ($browserName, &$relatedItems) { |
51
|
|
|
try { |
52
|
|
|
$repository = $this->getModelRepository($item['endpointType'] ?? $browserName); |
53
|
|
|
$relatedItems->push((object) [ |
54
|
|
|
'related' => $repository->getById($item['id']), |
55
|
|
|
'browser_name' => $browserName, |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
} catch (ReflectionException $e) { |
59
|
|
|
Log::error($e); |
60
|
|
|
} |
61
|
|
|
}); |
62
|
2 |
|
}); |
63
|
|
|
|
64
|
2 |
|
$object->setRelation('relatedItems', $relatedItems); |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
return parent::hydrate($object, $fields); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param HasMedias|HasFiles $object |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
2 |
|
public function afterSave($object, $fields) |
75
|
|
|
{ |
76
|
2 |
|
if (Schema::hasTable(config('twill.related_table', 'twill_related'))) { |
77
|
2 |
|
if (isset($fields['browsers'])) { |
78
|
|
|
Collection::make($fields['browsers'])->each(function ($items, $browserName) use ($object) { |
79
|
|
|
$object->saveRelated($items, $browserName); |
|
|
|
|
80
|
|
|
}); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
parent::afterSave($object, $fields); |
|
|
|
|
85
|
2 |
|
} |
86
|
|
|
|
87
|
|
|
public function afterDelete($object) |
88
|
|
|
{ |
89
|
|
|
$object->medias()->sync([]); |
90
|
|
|
$object->files()->sync([]); |
91
|
|
|
|
92
|
|
|
if (Schema::hasTable(config('twill.related_table', 'twill_related'))) { |
93
|
|
|
$object->relatedItems()->delete(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param array $block |
99
|
|
|
* @param bool $repeater |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
3 |
|
public function buildFromCmsArray($block, $repeater = false) |
103
|
|
|
{ |
104
|
3 |
|
if ($repeater) { |
105
|
|
|
$blocksList = app(BlockCollection::class)->getRepeaterList(); |
106
|
|
|
} else { |
107
|
3 |
|
$blocksList = app(BlockCollection::class)->getBlockList(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$block['type'] = $blocksList->keyBy('name')->search(function ($blockConfig) use ($block) { |
111
|
3 |
|
return $blockConfig['component'] === $block['type']; |
112
|
3 |
|
}); |
113
|
|
|
|
114
|
3 |
|
$block['content'] = empty($block['content']) ? new \stdClass : (object) $block['content']; |
115
|
|
|
|
116
|
3 |
|
if ($block['browsers']) { |
117
|
|
|
$browsers = Collection::make($block['browsers'])->map(function ($items) { |
118
|
|
|
return Collection::make($items)->pluck('id'); |
119
|
|
|
})->toArray(); |
120
|
|
|
|
121
|
|
|
$block['content']->browsers = $browsers; |
122
|
|
|
} |
123
|
|
|
|
124
|
3 |
|
return $block; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|