]> BookStack Code Mirror - bookstack/blob - app/Entity.php
Got standard form-based registration working
[bookstack] / app / Entity.php
1 <?php
2
3 namespace Oxbow;
4
5 use Illuminate\Database\Eloquent\Model;
6
7 abstract class Entity extends Model
8 {
9     /**
10      * Relation for the user that created this entity.
11      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
12      */
13     public function createdBy()
14     {
15         return $this->belongsTo('Oxbow\User', 'created_by');
16     }
17
18     /**
19      * Relation for the user that updated this entity.
20      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
21      */
22     public function updatedBy()
23     {
24         return $this->belongsTo('Oxbow\User', 'updated_by');
25     }
26
27     /**
28      * Compares this entity to another given entity.
29      * Matches by comparing class and id.
30      * @param $entity
31      * @return bool
32      */
33     public function matches($entity)
34     {
35         return [get_class($this), $this->id] === [get_class($entity), $entity->id];
36     }
37
38     /**
39      * Gets the activity for this entity.
40      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
41      */
42     public function activity()
43     {
44         return $this->morphMany('Oxbow\Activity', 'entity')->orderBy('created_at', 'desc');
45     }
46
47     /**
48      * Allows checking of the exact class, Used to check entity type.
49      * Cleaner method for is_a.
50      * @param $type
51      * @return bool
52      */
53     public function isA($type)
54     {
55         return $this->getName() === strtolower($type);
56     }
57
58     /**
59      * Gets the class name.
60      * @return string
61      */
62     public function getName()
63     {
64         $fullClassName = get_class($this);
65         return strtolower(array_slice(explode('\\', $fullClassName), -1, 1)[0]);
66     }
67
68     /**
69      * Perform a full-text search on this entity.
70      * @param string[] $fieldsToSearch
71      * @param string[] $terms
72      * @param string[] array $wheres
73      * @return mixed
74      */
75     public static function fullTextSearch($fieldsToSearch, $terms, $wheres = [])
76     {
77         $termString = '';
78         foreach ($terms as $term) {
79             $termString .= $term . '* ';
80         }
81         $fields = implode(',', $fieldsToSearch);
82         $search = static::whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
83         foreach ($wheres as $whereTerm) {
84             $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
85         }
86         return $search->get();
87     }
88
89     /**
90      * Get the url for this item.
91      * @return string
92      */
93     abstract public function getUrl();
94
95 }