]> BookStack Code Mirror - bookstack/blob - app/Entity.php
977b02e77c8aa06b5710877cde41a96b6b3fb37c
[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      * Checks if an entity matches or contains another given entity.
41      * @param Entity $entity
42      * @return bool
43      */
44     public function matchesOrContains(Entity $entity)
45     {
46         $matches = [get_class($this), $this->id] === [get_class($entity), $entity->id];
47
48         if ($matches) return true;
49
50         if ($entity->isA('chapter') && $this->isA('book')) {
51             return $entity->book_id === $this->id;
52         }
53
54         if ($entity->isA('page') && $this->isA('book')) {
55             return $entity->book_id === $this->id;
56         }
57
58         if ($entity->isA('page') && $this->isA('chapter')) {
59             return $entity->chapter_id === $this->id;
60         }
61
62         return false;
63     }
64
65     /**
66      * Gets the activity objects for this entity.
67      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
68      */
69     public function activity()
70     {
71         return $this->morphMany('BookStack\Activity', 'entity')->orderBy('created_at', 'desc');
72     }
73
74     /**
75      * Get View objects for this entity.
76      * @return mixed
77      */
78     public function views()
79     {
80         return $this->morphMany('BookStack\View', 'viewable');
81     }
82
83     /**
84      * Get just the views for the current user.
85      * @return mixed
86      */
87     public function userViews()
88     {
89         return $this->views()->where('user_id', '=', auth()->user()->id);
90     }
91
92     /**
93      * Allows checking of the exact class, Used to check entity type.
94      * Cleaner method for is_a.
95      * @param $type
96      * @return bool
97      */
98     public static function isA($type)
99     {
100         return static::getName() === strtolower($type);
101     }
102
103     /**
104      * Gets the class name.
105      * @return string
106      */
107     public static function getName()
108     {
109         return strtolower(array_slice(explode('\\', static::class), -1, 1)[0]);
110     }
111
112     /**
113      * Perform a full-text search on this entity.
114      * @param string[] $fieldsToSearch
115      * @param string[] $terms
116      * @param string[] array $wheres
117      * @return mixed
118      */
119     public static function fullTextSearch($fieldsToSearch, $terms, $wheres = [])
120     {
121         $termString = '';
122         foreach ($terms as $term) {
123             $termString .= $term . '* ';
124         }
125         $fields = implode(',', $fieldsToSearch);
126         $search = static::whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
127         foreach ($wheres as $whereTerm) {
128             $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
129         }
130
131         if (!static::isA('book')) {
132             $search = $search->with('book');
133         }
134
135         if (static::isA('page')) {
136             $search = $search->with('chapter');
137         }
138
139         return $search->get();
140     }
141
142     /**
143      * Get the url for this item.
144      * @return string
145      */
146     abstract public function getUrl();
147
148 }