]>
Commit | Line | Data |
---|---|---|
f1900b68 NL |
1 | <?php |
2 | ||
b073a0b4 | 3 | namespace Wallabag\GroupBundle\Entity; |
f1900b68 | 4 | |
6a50b4cc | 5 | use Doctrine\Common\Collections\ArrayCollection; |
f1900b68 NL |
6 | use FOS\UserBundle\Model\Group as BaseGroup; |
7 | use Doctrine\ORM\Mapping as ORM; | |
56c81a2f | 8 | use Wallabag\UserBundle\Entity\User; |
f1900b68 NL |
9 | |
10 | /** | |
2041810a | 11 | * @ORM\Entity(repositoryClass="Wallabag\GroupBundle\Repository\GroupRepository") |
f1900b68 NL |
12 | * @ORM\Table(name="`group`") |
13 | */ | |
14 | class Group extends BaseGroup | |
15 | { | |
2041810a | 16 | /** |
2585953e | 17 | * User Roles. |
2041810a TC |
18 | */ |
19 | ||
20 | /** User can only preview presentations */ | |
21 | const ROLE_READ_ONLY = 1; | |
22 | ||
23 | /** User can create new presentations */ | |
24 | const ROLE_WRITE = 2; | |
25 | ||
26 | /** User can manage all group presentations */ | |
27 | const ROLE_MANAGE_ENTRIES = 3; | |
28 | ||
29 | /** User can manage users in the group */ | |
30 | const ROLE_MANAGE_USERS = 5; | |
31 | ||
32 | /** User can rename and delete the group */ | |
33 | const ROLE_ADMIN = 10; | |
34 | ||
35 | /** | |
2585953e | 36 | * Group join access. |
2041810a TC |
37 | */ |
38 | ||
39 | /** Any user can join the group */ | |
40 | const ACCESS_OPEN = 1; | |
41 | ||
42 | /** An user needs to request to join the group */ | |
43 | const ACCESS_REQUEST = 2; | |
44 | ||
45 | /** An user need the password to access the group */ | |
46 | const ACCESS_PASSWORD = 3; | |
47 | ||
48 | /** An user needs to be invited to join the group */ | |
49 | const ACCESS_INVITATION_ONLY = 4; | |
50 | ||
51 | /** An user needs to be invited to join the group, and the group is not publicly listed */ | |
52 | const ACCESS_HIDDEN = 10; | |
53 | ||
f1900b68 NL |
54 | /** |
55 | * @ORM\Id | |
56 | * @ORM\Column(type="integer") | |
57 | * @ORM\GeneratedValue(strategy="AUTO") | |
58 | */ | |
59 | protected $id; | |
56c81a2f NL |
60 | |
61 | /** | |
2041810a TC |
62 | * @ORM\Column(type="integer", options={"default" : 1}) |
63 | */ | |
64 | protected $acceptSystem; | |
65 | ||
66 | /** | |
67 | * @ORM\Column(type="integer", options={"default" : 2}) | |
68 | */ | |
69 | protected $defaultRole; | |
70 | ||
71 | /** | |
72 | * @ORM\Column(type="string", nullable=true) | |
73 | */ | |
74 | protected $password; | |
75 | protected $plainPassword; | |
76 | ||
77 | /** | |
78 | * @ORM\ManyToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="groupShares", cascade={"persist"}) | |
79 | */ | |
80 | protected $entries; | |
81 | ||
82 | /** | |
83 | * @ORM\OneToMany(targetEntity="UserGroup", mappedBy="group", cascade={"persist"}) | |
56c81a2f NL |
84 | */ |
85 | protected $users; | |
86 | ||
2041810a | 87 | public function __construct($name = '', array $roles = []) |
56c81a2f | 88 | { |
2041810a TC |
89 | parent::__construct($name, $roles); |
90 | $this->defaultRole = self::ROLE_READ_ONLY; | |
91 | $this->acceptSystem = self::ACCESS_REQUEST; | |
56c81a2f NL |
92 | } |
93 | ||
2041810a TC |
94 | /** |
95 | * @return ArrayCollection | |
96 | */ | |
97 | public function getUsers() | |
56c81a2f | 98 | { |
2041810a TC |
99 | $userObj = new ArrayCollection(); |
100 | foreach ($this->users as $userGroup) { | |
2585953e | 101 | /* @var UserGroup $userGroup */ |
2041810a | 102 | $userObj->add($userGroup->getUser()); |
56c81a2f | 103 | } |
2585953e | 104 | |
2041810a TC |
105 | return $userObj; |
106 | } | |
56c81a2f | 107 | |
2041810a TC |
108 | /** |
109 | * @return int | |
110 | */ | |
111 | public function getDefaultRole() | |
112 | { | |
113 | return $this->defaultRole; | |
56c81a2f NL |
114 | } |
115 | ||
116 | /** | |
2041810a | 117 | * @return int |
56c81a2f | 118 | */ |
2041810a | 119 | public function getAcceptSystem() |
56c81a2f | 120 | { |
2041810a TC |
121 | return $this->acceptSystem; |
122 | } | |
123 | ||
124 | /** | |
125 | * @param int $acceptSystem | |
126 | */ | |
127 | public function setAcceptSystem($acceptSystem) | |
128 | { | |
129 | $this->acceptSystem = $acceptSystem; | |
130 | } | |
56c81a2f | 131 | |
2041810a TC |
132 | /** |
133 | * @return string | |
134 | */ | |
135 | public function getPassword() | |
136 | { | |
137 | return $this->password ?: ''; | |
138 | } | |
139 | ||
140 | /** | |
141 | * @param string $password | |
142 | */ | |
143 | public function setPassword($password) | |
144 | { | |
145 | $this->password = $password; | |
146 | } | |
147 | ||
148 | /** | |
149 | * @return string | |
150 | */ | |
151 | public function getPlainPassword() | |
152 | { | |
153 | return $this->plainPassword ?: ''; | |
154 | } | |
155 | ||
156 | /** | |
157 | * @param string $plainPassword | |
158 | */ | |
159 | public function setPlainPassword($plainPassword) | |
160 | { | |
161 | $this->plainPassword = $plainPassword; | |
162 | } | |
163 | ||
164 | /** | |
165 | * @param int $defaultRole | |
166 | */ | |
167 | public function setDefaultRole($defaultRole) | |
168 | { | |
169 | $this->defaultRole = $defaultRole; | |
56c81a2f NL |
170 | } |
171 | ||
172 | public function __toString() | |
173 | { | |
2041810a TC |
174 | return $this->name; |
175 | } | |
176 | ||
177 | public function getRequests() | |
178 | { | |
179 | $requests = new ArrayCollection(); | |
2585953e | 180 | foreach ($this->users as $user) /* @var UserGroup $user */ |
2041810a TC |
181 | { |
182 | if (!$user->isAccepted()) { | |
183 | $requests->add($user->getUser()); | |
184 | } | |
185 | } | |
2585953e | 186 | |
2041810a TC |
187 | return $requests; |
188 | } | |
189 | ||
190 | public function getInvited() | |
191 | { | |
192 | $invited = new ArrayCollection(); | |
2585953e | 193 | foreach ($this->users as $userGroup) /* @var UserGroup $userGroup */ |
2041810a TC |
194 | { |
195 | if ($userGroup->getInvitation()) { | |
196 | $invited->add($userGroup); | |
197 | } | |
198 | } | |
2585953e | 199 | |
2041810a | 200 | return $invited; |
56c81a2f | 201 | } |
f1900b68 | 202 | } |