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