]> BookStack Code Mirror - bookstack/blob - app/Entity.php
1a8b02a4b1e7aee5e67706d843b74ce9e7b9d640
[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 function isA($type)
73     {
74         return $this->getName() === strtolower($type);
75     }
76
77     /**
78      * Gets the class name.
79      * @return string
80      */
81     public function getName()
82     {
83         $fullClassName = get_class($this);
84         return strtolower(array_slice(explode('\\', $fullClassName), -1, 1)[0]);
85     }
86
87     /**
88      * Perform a full-text search on this entity.
89      * @param string[] $fieldsToSearch
90      * @param string[] $terms
91      * @param string[] array $wheres
92      * @return mixed
93      */
94     public static function fullTextSearch($fieldsToSearch, $terms, $wheres = [])
95     {
96         $termString = '';
97         foreach ($terms as $term) {
98             $termString .= $term . '* ';
99         }
100         $fields = implode(',', $fieldsToSearch);
101         $search = static::whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
102         foreach ($wheres as $whereTerm) {
103             $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
104         }
105         return $search->get();
106     }
107
108     /**
109      * Get the url for this item.
110      * @return string
111      */
112     abstract public function getUrl();
113
114 }