]> BookStack Code Mirror - bookstack/blob - app/Entity.php
Updated Spanish translation
[bookstack] / app / Entity.php
1 <?php namespace BookStack;
2
3 use Illuminate\Database\Eloquent\Relations\MorphMany;
4
5 class Entity extends Ownable
6 {
7
8     /**
9      * @var string - Name of property where the main text content is found
10      */
11     public $textField = 'description';
12
13     /**
14      * @var float - Multiplier for search indexing.
15      */
16     public $searchFactor = 1.0;
17
18     /**
19      * Compares this entity to another given entity.
20      * Matches by comparing class and id.
21      * @param $entity
22      * @return bool
23      */
24     public function matches($entity)
25     {
26         return [get_class($this), $this->id] === [get_class($entity), $entity->id];
27     }
28
29     /**
30      * Checks if an entity matches or contains another given entity.
31      * @param Entity $entity
32      * @return bool
33      */
34     public function matchesOrContains(Entity $entity)
35     {
36         $matches = [get_class($this), $this->id] === [get_class($entity), $entity->id];
37
38         if ($matches) {
39             return true;
40         }
41
42         if (($entity->isA('chapter') || $entity->isA('page')) && $this->isA('book')) {
43             return $entity->book_id === $this->id;
44         }
45
46         if ($entity->isA('page') && $this->isA('chapter')) {
47             return $entity->chapter_id === $this->id;
48         }
49
50         return false;
51     }
52
53     /**
54      * Gets the activity objects for this entity.
55      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
56      */
57     public function activity()
58     {
59         return $this->morphMany(Activity::class, 'entity')->orderBy('created_at', 'desc');
60     }
61
62     /**
63      * Get View objects for this entity.
64      */
65     public function views()
66     {
67         return $this->morphMany(View::class, 'viewable');
68     }
69
70     /**
71      * Get the Tag models that have been user assigned to this entity.
72      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
73      */
74     public function tags()
75     {
76         return $this->morphMany(Tag::class, 'entity')->orderBy('order', 'asc');
77     }
78
79     /**
80      * Get the comments for an entity
81      * @param bool $orderByCreated
82      * @return MorphMany
83      */
84     public function comments($orderByCreated = true)
85     {
86         $query = $this->morphMany(Comment::class, 'entity');
87         return $orderByCreated ? $query->orderBy('created_at', 'asc') : $query;
88     }
89
90     /**
91      * Get the related search terms.
92      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
93      */
94     public function searchTerms()
95     {
96         return $this->morphMany(SearchTerm::class, 'entity');
97     }
98
99     /**
100      * Get this entities restrictions.
101      */
102     public function permissions()
103     {
104         return $this->morphMany(EntityPermission::class, 'restrictable');
105     }
106
107     /**
108      * Check if this entity has a specific restriction set against it.
109      * @param $role_id
110      * @param $action
111      * @return bool
112      */
113     public function hasRestriction($role_id, $action)
114     {
115         return $this->permissions()->where('role_id', '=', $role_id)
116             ->where('action', '=', $action)->count() > 0;
117     }
118
119     /**
120      * Get the entity jointPermissions this is connected to.
121      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
122      */
123     public function jointPermissions()
124     {
125         return $this->morphMany(JointPermission::class, 'entity');
126     }
127
128     /**
129      * Allows checking of the exact class, Used to check entity type.
130      * Cleaner method for is_a.
131      * @param $type
132      * @return bool
133      */
134     public static function isA($type)
135     {
136         return static::getType() === strtolower($type);
137     }
138
139     /**
140      * Get entity type.
141      * @return mixed
142      */
143     public static function getType()
144     {
145         return strtolower(static::getClassName());
146     }
147
148     /**
149      * Get an instance of an entity of the given type.
150      * @param $type
151      * @return Entity
152      */
153     public static function getEntityInstance($type)
154     {
155         $types = ['Page', 'Book', 'Chapter'];
156         $className = str_replace([' ', '-', '_'], '', ucwords($type));
157         if (!in_array($className, $types)) {
158             return null;
159         }
160
161         return app('BookStack\\' . $className);
162     }
163
164     /**
165      * Gets a limited-length version of the entities name.
166      * @param int $length
167      * @return string
168      */
169     public function getShortName($length = 25)
170     {
171         if (strlen($this->name) <= $length) {
172             return $this->name;
173         }
174         return substr($this->name, 0, $length - 3) . '...';
175     }
176
177     /**
178      * Get the body text of this entity.
179      * @return mixed
180      */
181     public function getText()
182     {
183         return $this->{$this->textField};
184     }
185
186     /**
187      * Return a generalised, common raw query that can be 'unioned' across entities.
188      * @return string
189      */
190     public function entityRawQuery()
191     {
192         return '';
193     }
194
195     /**
196      * Get the url of this entity
197      * @param $path
198      * @return string
199      */
200     public function getUrl($path)
201     {
202         return '/';
203     }
204 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.