]> BookStack Code Mirror - bookstack/blob - app/Entities/Entity.php
Update entities.php
[bookstack] / app / Entities / Entity.php
1 <?php namespace BookStack\Entities;
2
3 use BookStack\Actions\Activity;
4 use BookStack\Actions\Comment;
5 use BookStack\Actions\Tag;
6 use BookStack\Actions\View;
7 use BookStack\Auth\Permissions\EntityPermission;
8 use BookStack\Auth\Permissions\JointPermission;
9 use BookStack\Ownable;
10 use Carbon\Carbon;
11 use Illuminate\Database\Eloquent\Relations\MorphMany;
12
13 /**
14  * Class Entity
15  * The base class for book-like items such as pages, chapters & books.
16  * This is not a database model in itself but extended.
17  *
18  * @property integer $id
19  * @property string $name
20  * @property string $slug
21  * @property Carbon $created_at
22  * @property Carbon $updated_at
23  * @property int $created_by
24  * @property int $updated_by
25  * @property boolean $restricted
26  *
27  * @package BookStack\Entities
28  */
29 class Entity extends Ownable
30 {
31
32     /**
33      * @var string - Name of property where the main text content is found
34      */
35     public $textField = 'description';
36
37     /**
38      * @var float - Multiplier for search indexing.
39      */
40     public $searchFactor = 1.0;
41
42     /**
43      * Get the morph class for this model.
44      * Set here since, due to folder changes, the namespace used
45      * in the database no longer matches the class namespace.
46      * @return string
47      */
48     public function getMorphClass()
49     {
50         return 'BookStack\\Entity';
51     }
52
53     /**
54      * Compares this entity to another given entity.
55      * Matches by comparing class and id.
56      * @param $entity
57      * @return bool
58      */
59     public function matches($entity)
60     {
61         return [get_class($this), $this->id] === [get_class($entity), $entity->id];
62     }
63
64     /**
65      * Checks if an entity matches or contains another given entity.
66      * @param Entity $entity
67      * @return bool
68      */
69     public function matchesOrContains(Entity $entity)
70     {
71         $matches = [get_class($this), $this->id] === [get_class($entity), $entity->id];
72
73         if ($matches) {
74             return true;
75         }
76
77         if (($entity->isA('chapter') || $entity->isA('page')) && $this->isA('book')) {
78             return $entity->book_id === $this->id;
79         }
80
81         if ($entity->isA('page') && $this->isA('chapter')) {
82             return $entity->chapter_id === $this->id;
83         }
84
85         return false;
86     }
87
88     /**
89      * Gets the activity objects for this entity.
90      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
91      */
92     public function activity()
93     {
94         return $this->morphMany(Activity::class, 'entity')->orderBy('created_at', 'desc');
95     }
96
97     /**
98      * Get View objects for this entity.
99      */
100     public function views()
101     {
102         return $this->morphMany(View::class, 'viewable');
103     }
104
105     public function viewCountQuery()
106     {
107         return $this->views()->selectRaw('viewable_id, sum(views) as view_count')->groupBy('viewable_id');
108     }
109
110     /**
111      * Get the Tag models that have been user assigned to this entity.
112      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
113      */
114     public function tags()
115     {
116         return $this->morphMany(Tag::class, 'entity')->orderBy('order', 'asc');
117     }
118
119     /**
120      * Get the comments for an entity
121      * @param bool $orderByCreated
122      * @return MorphMany
123      */
124     public function comments($orderByCreated = true)
125     {
126         $query = $this->morphMany(Comment::class, 'entity');
127         return $orderByCreated ? $query->orderBy('created_at', 'asc') : $query;
128     }
129
130     /**
131      * Get the related search terms.
132      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
133      */
134     public function searchTerms()
135     {
136         return $this->morphMany(SearchTerm::class, 'entity');
137     }
138
139     /**
140      * Get this entities restrictions.
141      */
142     public function permissions()
143     {
144         return $this->morphMany(EntityPermission::class, 'restrictable');
145     }
146
147     /**
148      * Check if this entity has a specific restriction set against it.
149      * @param $role_id
150      * @param $action
151      * @return bool
152      */
153     public function hasRestriction($role_id, $action)
154     {
155         return $this->permissions()->where('role_id', '=', $role_id)
156             ->where('action', '=', $action)->count() > 0;
157     }
158
159     /**
160      * Get the entity jointPermissions this is connected to.
161      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
162      */
163     public function jointPermissions()
164     {
165         return $this->morphMany(JointPermission::class, 'entity');
166     }
167
168     /**
169      * Allows checking of the exact class, Used to check entity type.
170      * Cleaner method for is_a.
171      * @param $type
172      * @return bool
173      */
174     public static function isA($type)
175     {
176         return static::getType() === strtolower($type);
177     }
178
179     /**
180      * Get entity type.
181      * @return mixed
182      */
183     public static function getType()
184     {
185         return strtolower(static::getClassName());
186     }
187
188     /**
189      * Get an instance of an entity of the given type.
190      * @param $type
191      * @return Entity
192      */
193     public static function getEntityInstance($type)
194     {
195         $types = ['Page', 'Book', 'Chapter', 'Bookshelf'];
196         $className = str_replace([' ', '-', '_'], '', ucwords($type));
197         if (!in_array($className, $types)) {
198             return null;
199         }
200
201         return app('BookStack\\Entities\\' . $className);
202     }
203
204     /**
205      * Gets a limited-length version of the entities name.
206      * @param int $length
207      * @return string
208      */
209     public function getShortName($length = 25)
210     {
211         if (mb_strlen($this->name) <= $length) {
212             return $this->name;
213         }
214         return mb_substr($this->name, 0, $length - 3) . '...';
215     }
216
217     /**
218      * Get the body text of this entity.
219      * @return mixed
220      */
221     public function getText()
222     {
223         return $this->{$this->textField};
224     }
225
226     /**
227      * Get an excerpt of this entity's descriptive content to the specified length.
228      * @param int $length
229      * @return mixed
230      */
231     public function getExcerpt(int $length = 100)
232     {
233         $text = $this->getText();
234         if (mb_strlen($text) > $length) {
235             $text = mb_substr($text, 0, $length-3) . '...';
236         }
237         return trim($text);
238     }
239
240     /**
241      * Return a generalised, common raw query that can be 'unioned' across entities.
242      * @return string
243      */
244     public function entityRawQuery()
245     {
246         return '';
247     }
248
249     /**
250      * Get the url of this entity
251      * @param $path
252      * @return string
253      */
254     public function getUrl($path = '/')
255     {
256         return $path;
257     }
258 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.