]> BookStack Code Mirror - bookstack/blob - app/Entity.php
Closes #69. Implemented and tested memcached.
[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     use Ownable;
11
12     /**
13      * Compares this entity to another given entity.
14      * Matches by comparing class and id.
15      * @param $entity
16      * @return bool
17      */
18     public function matches($entity)
19     {
20         return [get_class($this), $this->id] === [get_class($entity), $entity->id];
21     }
22
23     /**
24      * Checks if an entity matches or contains another given entity.
25      * @param Entity $entity
26      * @return bool
27      */
28     public function matchesOrContains(Entity $entity)
29     {
30         $matches = [get_class($this), $this->id] === [get_class($entity), $entity->id];
31
32         if ($matches) return true;
33
34         if (($entity->isA('chapter') || $entity->isA('page')) && $this->isA('book')) {
35             return $entity->book_id === $this->id;
36         }
37
38         if ($entity->isA('page') && $this->isA('chapter')) {
39             return $entity->chapter_id === $this->id;
40         }
41
42         return false;
43     }
44
45     /**
46      * Gets the activity objects for this entity.
47      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
48      */
49     public function activity()
50     {
51         return $this->morphMany('BookStack\Activity', 'entity')->orderBy('created_at', 'desc');
52     }
53
54     /**
55      * Get View objects for this entity.
56      * @return mixed
57      */
58     public function views()
59     {
60         return $this->morphMany('BookStack\View', 'viewable');
61     }
62
63     /**
64      * Allows checking of the exact class, Used to check entity type.
65      * Cleaner method for is_a.
66      * @param $type
67      * @return bool
68      */
69     public static function isA($type)
70     {
71         return static::getClassName() === strtolower($type);
72     }
73
74     /**
75      * Gets the class name.
76      * @return string
77      */
78     public static function getClassName()
79     {
80         return strtolower(array_slice(explode('\\', static::class), -1, 1)[0]);
81     }
82
83     /**
84      *Gets a limited-length version of the entities name.
85      * @param int $length
86      * @return string
87      */
88     public function getShortName($length = 25)
89     {
90         if(strlen($this->name) <= $length) return $this->name;
91         return substr($this->name, 0, $length-3) . '...';
92     }
93
94     /**
95      * Perform a full-text search on this entity.
96      * @param string[] $fieldsToSearch
97      * @param string[] $terms
98      * @param string[] array $wheres
99      * @return mixed
100      */
101     public static function fullTextSearchQuery($fieldsToSearch, $terms, $wheres = [])
102     {
103         $termString = '';
104         foreach ($terms as $term) {
105             $termString .= htmlentities($term) . '* ';
106         }
107         $fields = implode(',', $fieldsToSearch);
108         $termStringEscaped = \DB::connection()->getPdo()->quote($termString);
109         $search = static::addSelect(\DB::raw('*, MATCH(name) AGAINST('.$termStringEscaped.' IN BOOLEAN MODE) AS title_relevance'));
110         $search = $search->whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
111
112         // Add additional where terms
113         foreach ($wheres as $whereTerm) {
114             $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
115         }
116
117         // Load in relations
118         if (static::isA('page'))  {
119             $search = $search->with('book', 'chapter', 'createdBy', 'updatedBy');
120         } else if (static::isA('chapter')) {
121             $search = $search->with('book');
122         }
123
124         return $search->orderBy('title_relevance', 'desc');
125     }
126
127     /**
128      * Get the url for this item.
129      * @return string
130      */
131     abstract public function getUrl();
132
133 }