1
|
|
|
<?php namespace VojtaSvoboda\Reservations\Updates; |
2
|
|
|
|
3
|
|
|
use File; |
4
|
|
|
use VojtaSvoboda\Reservations\Models\Status; |
5
|
|
|
use VojtaSvoboda\Reservations\Updates\Classes\Seeder; |
6
|
|
|
use Yaml; |
7
|
|
|
|
8
|
|
|
class SeedStatusesTable extends Seeder |
9
|
|
|
{ |
10
|
|
|
protected $seedFileName = '/statuses.yaml'; |
11
|
|
|
|
12
|
|
|
protected $seedDirPath = '/sources'; |
13
|
|
|
|
14
|
|
|
protected $mediaFolderPath = '/statuses'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Run seeding. |
18
|
|
|
*/ |
19
|
20 |
|
public function run() |
20
|
|
|
{ |
21
|
|
|
// fetch data for seed |
22
|
20 |
|
$defaultSeed = __DIR__ . $this->seedDirPath . $this->seedFileName; |
23
|
20 |
|
$seedFile = $this->getSeedFile($defaultSeed); |
24
|
20 |
|
$seedMediaFolder = pathinfo($seedFile)['dirname'] . $this->mediaFolderPath; |
25
|
20 |
|
$items = Yaml::parse(File::get($seedFile)); |
26
|
|
|
|
27
|
|
|
// process each line |
28
|
20 |
|
foreach ($items as $item) |
29
|
|
|
{ |
30
|
|
|
// create new Status |
31
|
20 |
|
$status = Status::create([ |
32
|
20 |
|
'name' => trim($item['name']), |
33
|
20 |
|
'ident' => trim($item['ident']), |
34
|
20 |
|
'color' => trim($item['color']), |
35
|
20 |
|
'enabled' => isset($item['enabled']) ? !!$item['enabled'] : true, |
36
|
|
|
]); |
37
|
|
|
|
38
|
|
|
// save attachments |
39
|
20 |
|
if (!empty($item['imagename'])) { |
40
|
20 |
|
$this->saveImageAttachments($status, $seedMediaFolder, $item['imagename']); |
41
|
|
|
} |
42
|
|
|
} |
43
|
20 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Save attachments |
47
|
|
|
* |
48
|
|
|
* @param object $item |
49
|
|
|
* @param string $path |
50
|
|
|
* @param string $imagename |
51
|
|
|
*/ |
52
|
|
|
private function saveImageAttachments($item, $path, $imagename) |
53
|
|
|
{ |
54
|
|
|
$filePath = $path . '/' . $imagename; |
55
|
|
|
if (file_exists($filePath) && is_file($filePath)) { |
56
|
|
|
$isPublic = true; |
57
|
|
|
$fileObject = $this->getSavedFile($filePath, $isPublic); |
58
|
|
|
$item->image()->add($fileObject); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|