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