]> BookStack Code Mirror - bookstack/blob - app/Repos/AttributeRepo.php
7318b253b8eb5c3f3a085db6ceb138b6c18d6a56
[bookstack] / app / Repos / AttributeRepo.php
1 <?php namespace BookStack\Repos;
2
3 use BookStack\Attribute;
4 use BookStack\Entity;
5 use BookStack\Services\PermissionService;
6
7 /**
8  * Class AttributeRepo
9  * @package BookStack\Repos
10  */
11 class AttributeRepo
12 {
13
14     protected $attribute;
15     protected $entity;
16     protected $permissionService;
17
18     /**
19      * AttributeRepo constructor.
20      * @param Attribute $attr
21      * @param Entity $ent
22      * @param PermissionService $ps
23      */
24     public function __construct(Attribute $attr, Entity $ent, PermissionService $ps)
25     {
26         $this->attribute = $attr;
27         $this->entity = $ent;
28         $this->permissionService = $ps;
29     }
30
31     /**
32      * Get an entity instance of its particular type.
33      * @param $entityType
34      * @param $entityId
35      * @param string $action
36      */
37     public function getEntity($entityType, $entityId, $action = 'view')
38     {
39         $entityInstance = $this->entity->getEntityInstance($entityType);
40         $searchQuery = $entityInstance->where('id', '=', $entityId)->with('attributes');
41         $searchQuery = $this->permissionService->enforceEntityRestrictions($searchQuery, $action);
42         return $searchQuery->first();
43     }
44
45     /**
46      * Get all attributes for a particular entity.
47      * @param string $entityType
48      * @param int $entityId
49      * @return mixed
50      */
51     public function getForEntity($entityType, $entityId)
52     {
53         $entity = $this->getEntity($entityType, $entityId);
54         if ($entity === null) return collect();
55
56         return $entity->attributes;
57     }
58
59     /**
60      * Get attribute name suggestions from scanning existing attribute names.
61      * @param $searchTerm
62      * @return array
63      */
64     public function getNameSuggestions($searchTerm)
65     {
66         if ($searchTerm === '') return [];
67         $query = $this->attribute->where('name', 'LIKE', $searchTerm . '%')->groupBy('name')->orderBy('name', 'desc');
68         $query = $this->permissionService->filterRestrictedEntityRelations($query, 'attributes', 'entity_id', 'entity_type');
69         return $query->get(['name'])->pluck('name');
70     }
71
72     /**
73      * Save an array of attributes to an entity
74      * @param Entity $entity
75      * @param array $attributes
76      * @return array|\Illuminate\Database\Eloquent\Collection
77      */
78     public function saveAttributesToEntity(Entity $entity, $attributes = [])
79     {
80         $entity->attributes()->delete();
81         $newAttributes = [];
82         foreach ($attributes as $attribute) {
83             $newAttributes[] = $this->newInstanceFromInput($attribute);
84         }
85
86         return $entity->attributes()->saveMany($newAttributes);
87     }
88
89     /**
90      * Create a new Attribute instance from user input.
91      * @param $input
92      * @return static
93      */
94     protected function newInstanceFromInput($input)
95     {
96         $name = trim($input['name']);
97         $value = isset($input['value']) ? trim($input['value']) : '';
98         // Any other modification or cleanup required can go here
99         $values = ['name' => $name, 'value' => $value];
100         return $this->attribute->newInstance($values);
101     }
102
103 }