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