]> BookStack Code Mirror - bookstack/blob - app/Entity.php
Added restriction tests and fixed any bugs in the process
[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      */
52     public function views()
53     {
54         return $this->morphMany('BookStack\View', 'viewable');
55     }
56
57     /**
58      * Get this entities restrictions.
59      */
60     public function restrictions()
61     {
62         return $this->morphMany('BookStack\Restriction', 'restrictable');
63     }
64
65     /**
66      * Check if this entity has a specific restriction set against it.
67      * @param $role_id
68      * @param $action
69      * @return bool
70      */
71     public function hasRestriction($role_id, $action)
72     {
73         return $this->restrictions->where('role_id', $role_id)->where('action', $action)->count() > 0;
74     }
75
76     /**
77      * Allows checking of the exact class, Used to check entity type.
78      * Cleaner method for is_a.
79      * @param $type
80      * @return bool
81      */
82     public static function isA($type)
83     {
84         return static::getClassName() === strtolower($type);
85     }
86
87     /**
88      * Gets a limited-length version of the entities name.
89      * @param int $length
90      * @return string
91      */
92     public function getShortName($length = 25)
93     {
94         if(strlen($this->name) <= $length) return $this->name;
95         return substr($this->name, 0, $length-3) . '...';
96     }
97
98     /**
99      * Perform a full-text search on this entity.
100      * @param string[] $fieldsToSearch
101      * @param string[] $terms
102      * @param string[] array $wheres
103      * @return mixed
104      */
105     public static function fullTextSearchQuery($fieldsToSearch, $terms, $wheres = [])
106     {
107         $termString = '';
108         foreach ($terms as $term) {
109             $termString .= htmlentities($term) . '* ';
110         }
111         $fields = implode(',', $fieldsToSearch);
112         $termStringEscaped = \DB::connection()->getPdo()->quote($termString);
113         $search = static::addSelect(\DB::raw('*, MATCH(name) AGAINST('.$termStringEscaped.' IN BOOLEAN MODE) AS title_relevance'));
114         $search = $search->whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
115
116         // Add additional where terms
117         foreach ($wheres as $whereTerm) {
118             $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
119         }
120
121         // Load in relations
122         if (static::isA('page'))  {
123             $search = $search->with('book', 'chapter', 'createdBy', 'updatedBy');
124         } else if (static::isA('chapter')) {
125             $search = $search->with('book');
126         }
127
128         return $search->orderBy('title_relevance', 'desc');
129     }
130
131     /**
132      * Get the url for this item.
133      * @return string
134      */
135     abstract public function getUrl();
136
137 }