]> BookStack Code Mirror - bookstack/blob - app/Entity.php
Polish translation
[bookstack] / app / Entity.php
1 <?php namespace BookStack;
2
3
4 class Entity extends Ownable
5 {
6
7     public $textField = '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 the related search terms.
70      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
71      */
72     public function searchTerms()
73     {
74         return $this->morphMany(SearchTerm::class, 'entity');
75     }
76
77     /**
78      * Get this entities restrictions.
79      */
80     public function permissions()
81     {
82         return $this->morphMany(EntityPermission::class, 'restrictable');
83     }
84
85     /**
86      * Check if this entity has a specific restriction set against it.
87      * @param $role_id
88      * @param $action
89      * @return bool
90      */
91     public function hasRestriction($role_id, $action)
92     {
93         return $this->permissions()->where('role_id', '=', $role_id)
94             ->where('action', '=', $action)->count() > 0;
95     }
96
97     /**
98      * Get the entity jointPermissions this is connected to.
99      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
100      */
101     public function jointPermissions()
102     {
103         return $this->morphMany(JointPermission::class, 'entity');
104     }
105
106     /**
107      * Allows checking of the exact class, Used to check entity type.
108      * Cleaner method for is_a.
109      * @param $type
110      * @return bool
111      */
112     public static function isA($type)
113     {
114         return static::getType() === strtolower($type);
115     }
116
117     /**
118      * Get entity type.
119      * @return mixed
120      */
121     public static function getType()
122     {
123         return strtolower(static::getClassName());
124     }
125
126     /**
127      * Get an instance of an entity of the given type.
128      * @param $type
129      * @return Entity
130      */
131     public static function getEntityInstance($type)
132     {
133         $types = ['Page', 'Book', 'Chapter'];
134         $className = str_replace([' ', '-', '_'], '', ucwords($type));
135         if (!in_array($className, $types)) {
136             return null;
137         }
138
139         return app('BookStack\\' . $className);
140     }
141
142     /**
143      * Gets a limited-length version of the entities name.
144      * @param int $length
145      * @return string
146      */
147     public function getShortName($length = 25)
148     {
149         if (strlen($this->name) <= $length) return $this->name;
150         return substr($this->name, 0, $length - 3) . '...';
151     }
152
153     /**
154      * Get the body text of this entity.
155      * @return mixed
156      */
157     public function getText()
158     {
159         return $this->{$this->textField};
160     }
161
162     /**
163      * Return a generalised, common raw query that can be 'unioned' across entities.
164      * @return string
165      */
166     public function entityRawQuery(){return '';}
167
168     /**
169      * Get the url of this entity
170      * @param $path
171      * @return string
172      */
173     public function getUrl($path){return '/';}
174
175 }