]> BookStack Code Mirror - bookstack/blob - app/Entity.php
Fixes typo causing the message not to be displayed
[bookstack] / app / Entity.php
1 <?php namespace BookStack;
2
3
4 class Entity extends Ownable
5 {
6
7     protected $fieldsToSearch = ['name', 'description'];
8
9     /**
10      * Compares this entity to another given entity.
11      * Matches by comparing class and id.
12      * @param $entity
13      * @return bool
14      */
15     public function matches($entity)
16     {
17         return [get_class($this), $this->id] === [get_class($entity), $entity->id];
18     }
19
20     /**
21      * Checks if an entity matches or contains another given entity.
22      * @param Entity $entity
23      * @return bool
24      */
25     public function matchesOrContains(Entity $entity)
26     {
27         $matches = [get_class($this), $this->id] === [get_class($entity), $entity->id];
28
29         if ($matches) return true;
30
31         if (($entity->isA('chapter') || $entity->isA('page')) && $this->isA('book')) {
32             return $entity->book_id === $this->id;
33         }
34
35         if ($entity->isA('page') && $this->isA('chapter')) {
36             return $entity->chapter_id === $this->id;
37         }
38
39         return false;
40     }
41
42     /**
43      * Gets the activity objects for this entity.
44      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
45      */
46     public function activity()
47     {
48         return $this->morphMany(Activity::class, 'entity')->orderBy('created_at', 'desc');
49     }
50
51     /**
52      * Get View objects for this entity.
53      */
54     public function views()
55     {
56         return $this->morphMany(View::class, 'viewable');
57     }
58
59     /**
60      * Get the Tag models that have been user assigned to this entity.
61      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
62      */
63     public function tags()
64     {
65         return $this->morphMany(Tag::class, 'entity')->orderBy('order', 'asc');
66     }
67
68     /**
69      * Get this entities restrictions.
70      */
71     public function permissions()
72     {
73         return $this->morphMany(EntityPermission::class, 'restrictable');
74     }
75
76     /**
77      * Check if this entity has a specific restriction set against it.
78      * @param $role_id
79      * @param $action
80      * @return bool
81      */
82     public function hasRestriction($role_id, $action)
83     {
84         return $this->permissions()->where('role_id', '=', $role_id)
85             ->where('action', '=', $action)->count() > 0;
86     }
87
88     /**
89      * Check if this entity has live (active) restrictions in place.
90      * @param $role_id
91      * @param $action
92      * @return bool
93      */
94     public function hasActiveRestriction($role_id, $action)
95     {
96         return $this->getRawAttribute('restricted') && $this->hasRestriction($role_id, $action);
97     }
98
99     /**
100      * Get the entity jointPermissions this is connected to.
101      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
102      */
103     public function jointPermissions()
104     {
105         return $this->morphMany(JointPermission::class, 'entity');
106     }
107
108     /**
109      * Allows checking of the exact class, Used to check entity type.
110      * Cleaner method for is_a.
111      * @param $type
112      * @return bool
113      */
114     public static function isA($type)
115     {
116         return static::getType() === strtolower($type);
117     }
118
119     /**
120      * Get entity type.
121      * @return mixed
122      */
123     public static function getType()
124     {
125         return strtolower(static::getClassName());
126     }
127
128     /**
129      * Get an instance of an entity of the given type.
130      * @param $type
131      * @return Entity
132      */
133     public static function getEntityInstance($type)
134     {
135         $types = ['Page', 'Book', 'Chapter'];
136         $className = str_replace([' ', '-', '_'], '', ucwords($type));
137         if (!in_array($className, $types)) {
138             return null;
139         }
140
141         return app('BookStack\\' . $className);
142     }
143
144     /**
145      * Gets a limited-length version of the entities name.
146      * @param int $length
147      * @return string
148      */
149     public function getShortName($length = 25)
150     {
151         if (strlen($this->name) <= $length) return $this->name;
152         return substr($this->name, 0, $length - 3) . '...';
153     }
154
155     /**
156      * Perform a full-text search on this entity.
157      * @param string[] $fieldsToSearch
158      * @param string[] $terms
159      * @param string[] array $wheres
160      * @return mixed
161      */
162     public function fullTextSearchQuery($terms, $wheres = [])
163     {
164         $exactTerms = [];
165         $fuzzyTerms = [];
166         $search = static::newQuery();
167
168         foreach ($terms as $key => $term) {
169             $term = htmlentities($term, ENT_QUOTES);
170             $term = preg_replace('/[+\-><\(\)~*\"@]+/', ' ', $term);
171             if (preg_match('/&quot;.*?&quot;/', $term) || is_numeric($term)) {
172                 $term = str_replace('&quot;', '', $term);
173                 $exactTerms[] = '%' . $term . '%';
174             } else {
175                 $term = '' . $term . '*';
176                 if ($term !== '*') $fuzzyTerms[] = $term;
177             }
178         }
179
180         $isFuzzy = count($exactTerms) === 0 && count($fuzzyTerms) > 0;
181
182
183         // Perform fulltext search if relevant terms exist.
184         if ($isFuzzy) {
185             $termString = implode(' ', $fuzzyTerms);
186             $fields = implode(',', $this->fieldsToSearch);
187             $search = $search->selectRaw('*, MATCH(name) AGAINST(? IN BOOLEAN MODE) AS title_relevance', [$termString]);
188             $search = $search->whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
189         }
190
191         // Ensure at least one exact term matches if in search
192         if (count($exactTerms) > 0) {
193             $search = $search->where(function ($query) use ($exactTerms) {
194                 foreach ($exactTerms as $exactTerm) {
195                     foreach ($this->fieldsToSearch as $field) {
196                         $query->orWhere($field, 'like', $exactTerm);
197                     }
198                 }
199             });
200         }
201
202         $orderBy = $isFuzzy ? 'title_relevance' : 'updated_at';
203
204         // Add additional where terms
205         foreach ($wheres as $whereTerm) {
206             $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
207         }
208
209         // Load in relations
210         if ($this->isA('page')) {
211             $search = $search->with('book', 'chapter', 'createdBy', 'updatedBy');
212         } else if ($this->isA('chapter')) {
213             $search = $search->with('book');
214         }
215
216         return $search->orderBy($orderBy, 'desc');
217     }
218
219 }