]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BaseRepo.php
fix image delete confirm text
[bookstack] / app / Entities / Repos / BaseRepo.php
1 <?php
2
3 namespace BookStack\Entities\Repos;
4
5 use BookStack\Actions\ActivityType;
6 use BookStack\Actions\TagRepo;
7 use BookStack\Auth\User;
8 use BookStack\Entities\Models\Entity;
9 use BookStack\Entities\Models\HasCoverImage;
10 use BookStack\Exceptions\ImageUploadException;
11 use BookStack\Facades\Activity;
12 use BookStack\Uploads\ImageRepo;
13 use Illuminate\Http\UploadedFile;
14 use Illuminate\Support\Collection;
15
16 class BaseRepo
17 {
18
19     protected $tagRepo;
20     protected $imageRepo;
21
22
23     public function __construct(TagRepo $tagRepo, ImageRepo $imageRepo)
24     {
25         $this->tagRepo = $tagRepo;
26         $this->imageRepo = $imageRepo;
27     }
28
29     /**
30      * Create a new entity in the system
31      */
32     public function create(Entity $entity, array $input)
33     {
34         $entity->fill($input);
35         $entity->forceFill([
36             'created_by' => user()->id,
37             'updated_by' => user()->id,
38             'owned_by' => user()->id,
39         ]);
40         $entity->refreshSlug();
41         $entity->save();
42
43         if (isset($input['tags'])) {
44             $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
45         }
46
47         $entity->rebuildPermissions();
48         $entity->indexForSearch();
49     }
50
51     /**
52      * Update the given entity.
53      */
54     public function update(Entity $entity, array $input)
55     {
56         $entity->fill($input);
57         $entity->updated_by = user()->id;
58
59         if ($entity->isDirty('name')) {
60             $entity->refreshSlug();
61         }
62
63         $entity->save();
64
65         if (isset($input['tags'])) {
66             $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
67         }
68
69         $entity->rebuildPermissions();
70         $entity->indexForSearch();
71     }
72
73     /**
74      * Update the given items' cover image, or clear it.
75      * @throws ImageUploadException
76      * @throws \Exception
77      */
78     public function updateCoverImage(HasCoverImage $entity, ?UploadedFile $coverImage, bool $removeImage = false)
79     {
80         if ($coverImage) {
81             $this->imageRepo->destroyImage($entity->cover);
82             $image = $this->imageRepo->saveNew($coverImage, 'cover_book', $entity->id, 512, 512, true);
83             $entity->cover()->associate($image);
84             $entity->save();
85         }
86
87         if ($removeImage) {
88             $this->imageRepo->destroyImage($entity->cover);
89             $entity->image_id = 0;
90             $entity->save();
91         }
92     }
93 }