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