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