]> BookStack Code Mirror - bookstack/blob - app/Entity.php
c084a28703cc9b2ec713a0f65e0bba50a7c84a24
[bookstack] / app / Entity.php
1 <?php namespace BookStack;
2
3
4 abstract class Entity extends Ownable
5 {
6
7     /**
8      * Compares this entity to another given entity.
9      * Matches by comparing class and id.
10      * @param $entity
11      * @return bool
12      */
13     public function matches($entity)
14     {
15         return [get_class($this), $this->id] === [get_class($entity), $entity->id];
16     }
17
18     /**
19      * Checks if an entity matches or contains another given entity.
20      * @param Entity $entity
21      * @return bool
22      */
23     public function matchesOrContains(Entity $entity)
24     {
25         $matches = [get_class($this), $this->id] === [get_class($entity), $entity->id];
26
27         if ($matches) return true;
28
29         if (($entity->isA('chapter') || $entity->isA('page')) && $this->isA('book')) {
30             return $entity->book_id === $this->id;
31         }
32
33         if ($entity->isA('page') && $this->isA('chapter')) {
34             return $entity->chapter_id === $this->id;
35         }
36
37         return false;
38     }
39
40     /**
41      * Gets the activity objects for this entity.
42      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
43      */
44     public function activity()
45     {
46         return $this->morphMany('BookStack\Activity', 'entity')->orderBy('created_at', 'desc');
47     }
48
49     /**
50      * Get View objects for this entity.
51      */
52     public function views()
53     {
54         return $this->morphMany('BookStack\View', 'viewable');
55     }
56
57     /**
58      * Get this entities restrictions.
59      */
60     public function restrictions()
61     {
62         return $this->morphMany('BookStack\Restriction', 'restrictable');
63     }
64
65     /**
66      * Check if this entity has a specific restriction set against it.
67      * @param $role_id
68      * @param $action
69      * @return bool
70      */
71     public function hasRestriction($role_id, $action)
72     {
73         return $this->restrictions()->where('role_id', '=', $role_id)
74             ->where('action', '=', $action)->count() > 0;
75     }
76
77     /**
78      * Check if this entity has live (active) restrictions in place.
79      * @param $role_id
80      * @param $action
81      * @return bool
82      */
83     public function hasActiveRestriction($role_id, $action)
84     {
85         return $this->restricted && $this->restrictions()
86             ->where('role_id', '=', $role_id)->where('action', '=', $action)->count() > 0;
87     }
88
89     /**
90      * Get the entity permissions this is connected to.
91      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
92      */
93     public function permissions()
94     {
95         return $this->morphMany(EntityPermission::class, 'entity');
96     }
97
98     /**
99      * Allows checking of the exact class, Used to check entity type.
100      * Cleaner method for is_a.
101      * @param $type
102      * @return bool
103      */
104     public static function isA($type)
105     {
106         return static::getType() === strtolower($type);
107     }
108
109     /**
110      * Get entity type.
111      * @return mixed
112      */
113     public static function getType()
114     {
115         return strtolower(static::getClassName());
116     }
117
118     /**
119      * Gets a limited-length version of the entities name.
120      * @param int $length
121      * @return string
122      */
123     public function getShortName($length = 25)
124     {
125         if (strlen($this->name) <= $length) return $this->name;
126         return substr($this->name, 0, $length - 3) . '...';
127     }
128
129     /**
130      * Perform a full-text search on this entity.
131      * @param string[] $fieldsToSearch
132      * @param string[] $terms
133      * @param string[] array $wheres
134      * @return mixed
135      */
136     public static function fullTextSearchQuery($fieldsToSearch, $terms, $wheres = [])
137     {
138         $exactTerms = [];
139         foreach ($terms as $key => $term) {
140             $term = htmlentities($term, ENT_QUOTES);
141             $term = preg_replace('/[+\-><\(\)~*\"@]+/', ' ', $term);
142             if (preg_match('/\s/', $term)) {
143                 $exactTerms[] = '%' . $term . '%';
144                 $term = '"' . $term . '"';
145             } else {
146                 $term = '' . $term . '*';
147             }
148             if ($term !== '*') $terms[$key] = $term;
149         }
150         $termString = implode(' ', $terms);
151         $fields = implode(',', $fieldsToSearch);
152         $search = static::selectRaw('*, MATCH(name) AGAINST(? IN BOOLEAN MODE) AS title_relevance', [$termString]);
153         $search = $search->whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
154
155         // Ensure at least one exact term matches if in search
156         if (count($exactTerms) > 0) {
157             $search = $search->where(function ($query) use ($exactTerms, $fieldsToSearch) {
158                 foreach ($exactTerms as $exactTerm) {
159                     foreach ($fieldsToSearch as $field) {
160                         $query->orWhere($field, 'like', $exactTerm);
161                     }
162                 }
163             });
164         }
165
166         // Add additional where terms
167         foreach ($wheres as $whereTerm) {
168             $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
169         }
170         // Load in relations
171         if (static::isA('page')) {
172             $search = $search->with('book', 'chapter', 'createdBy', 'updatedBy');
173         } else if (static::isA('chapter')) {
174             $search = $search->with('book');
175         }
176
177         return $search->orderBy('title_relevance', 'desc');
178     }
179
180     /**
181      * Get the url for this item.
182      * @return string
183      */
184     abstract public function getUrl();
185
186 }