]> BookStack Code Mirror - bookstack/blob - app/Entity.php
Added further tests, Fixed speed_update issues, improved search result query count
[bookstack] / app / Entity.php
1 <?php
2
3 namespace BookStack;
4
5 use Illuminate\Database\Eloquent\Model;
6
7 abstract class Entity extends Model
8 {
9
10     /**
11      * Relation for the user that created this entity.
12      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
13      */
14     public function createdBy()
15     {
16         return $this->belongsTo('BookStack\User', 'created_by');
17     }
18
19     /**
20      * Relation for the user that updated this entity.
21      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
22      */
23     public function updatedBy()
24     {
25         return $this->belongsTo('BookStack\User', 'updated_by');
26     }
27
28     /**
29      * Compares this entity to another given entity.
30      * Matches by comparing class and id.
31      * @param $entity
32      * @return bool
33      */
34     public function matches($entity)
35     {
36         return [get_class($this), $this->id] === [get_class($entity), $entity->id];
37     }
38
39     /**
40      * Gets the activity objects for this entity.
41      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
42      */
43     public function activity()
44     {
45         return $this->morphMany('BookStack\Activity', 'entity')->orderBy('created_at', 'desc');
46     }
47
48     /**
49      * Get View objects for this entity.
50      * @return mixed
51      */
52     public function views()
53     {
54         return $this->morphMany('BookStack\View', 'viewable');
55     }
56
57     /**
58      * Get just the views for the current user.
59      * @return mixed
60      */
61     public function userViews()
62     {
63         return $this->views()->where('user_id', '=', auth()->user()->id);
64     }
65
66     /**
67      * Allows checking of the exact class, Used to check entity type.
68      * Cleaner method for is_a.
69      * @param $type
70      * @return bool
71      */
72     public static function isA($type)
73     {
74         return static::getName() === strtolower($type);
75     }
76
77     /**
78      * Gets the class name.
79      * @return string
80      */
81     public static function getName()
82     {
83         return strtolower(array_slice(explode('\\', static::class), -1, 1)[0]);
84     }
85
86     /**
87      * Perform a full-text search on this entity.
88      * @param string[] $fieldsToSearch
89      * @param string[] $terms
90      * @param string[] array $wheres
91      * @return mixed
92      */
93     public static function fullTextSearch($fieldsToSearch, $terms, $wheres = [])
94     {
95         $termString = '';
96         foreach ($terms as $term) {
97             $termString .= $term . '* ';
98         }
99         $fields = implode(',', $fieldsToSearch);
100         $search = static::whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
101         foreach ($wheres as $whereTerm) {
102             $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
103         }
104
105         if (!static::isA('book')) {
106             $search = $search->with('book');
107         }
108
109         if(static::isA('page')) {
110             $search = $search->with('chapter');
111         }
112
113         return $search->get();
114     }
115
116     /**
117      * Get the url for this item.
118      * @return string
119      */
120     abstract public function getUrl();
121
122 }