Merge branch 'master' of github.com:WoltLab/WCF
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / user / option / UserOption.class.php
1 <?php
2 namespace wcf\data\user\option;
3 use wcf\data\option\Option;
4 use wcf\data\user\User;
5 use wcf\system\WCF;
6
7 /**
8 * Represents a user option.
9 *
10 * @author Marcel Werk
11 * @copyright 2001-2013 WoltLab GmbH
12 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
13 * @package com.woltlab.wcf
14 * @subpackage data.user.option
15 * @category Community Framework
16 */
17 class UserOption extends Option {
18 /**
19 * visible for no one (no valid bit)
20 * @var integer
21 */
22 const VISIBILITY_NONE = 0;
23
24 /**
25 * visible for the owner
26 * @var integer
27 */
28 const VISIBILITY_OWNER = 1;
29
30 /**
31 * visible for admins
32 * @var integer
33 */
34 const VISIBILITY_ADMINISTRATOR = 2;
35
36 /**
37 * visible for users
38 * @var integer
39 */
40 const VISIBILITY_REGISTERED = 4;
41
42 /**
43 * visible for guests
44 * @var integer
45 */
46 const VISIBILITY_GUEST = 8;
47
48 /**
49 * visible for all (no valid bit)
50 * @var integer
51 */
52 const VISIBILITY_ALL = 15;
53
54 /**
55 * editable for no one (no valid bit)
56 * @var integer
57 */
58 const EDITABILITY_NONE = 0;
59
60 /**
61 * editable for the owner
62 * @var integer
63 */
64 const EDITABILITY_OWNER = 1;
65
66 /**
67 * editable for admins
68 * @var integer
69 */
70 const EDITABILITY_ADMINISTRATOR = 2;
71
72 /**
73 * editable for all (no valid bit)
74 * @var integer
75 */
76 const EDITABILITY_ALL = 3;
77
78 /**
79 * @see wcf\data\DatabaseObject::$databaseTableName
80 */
81 protected static $databaseTableName = 'user_option';
82
83 /**
84 * @see wcf\data\DatabaseObject::$databaseTableIndexName
85 */
86 protected static $databaseTableIndexName = 'optionID';
87
88 /**
89 * option value
90 * @var string
91 */
92 public $optionValue = '';
93
94 /**
95 * user object
96 * @var wcf\data\user\User
97 */
98 public $user = null;
99
100 /**
101 * Sets target user object.
102 *
103 * @param wcf\data\user\User $user
104 */
105 public function setUser(User $user) {
106 $this->user = $user;
107 }
108
109 /**
110 * @see wcf\data\option\Option::isVisible()
111 */
112 public function isVisible() {
113 // proceed if option is visible for all
114 if ($this->visible & self::VISIBILITY_GUEST) {
115 return true;
116 }
117
118 // proceed if option is visible for registered users and current user is logged in
119 if (($this->visible & self::VISIBILITY_REGISTERED) && WCF::getUser()->userID) {
120 return true;
121 }
122
123 // check admin permissions
124 if ($this->visible & self::VISIBILITY_ADMINISTRATOR) {
125 if (WCF::getSession()->getPermission('admin.general.canViewPrivateUserOptions')) {
126 return true;
127 }
128 }
129
130 // check owner state
131 if ($this->visible & self::VISIBILITY_OWNER) {
132 if ($this->user !== null && $this->user->userID == WCF::getUser()->userID) {
133 return true;
134 }
135 }
136
137 return false;
138 }
139
140 /**
141 * Returns true if this option is editable.
142 *
143 * @return boolean
144 */
145 public function isEditable() {
146 // check admin permissions
147 if ($this->editable & self::EDITABILITY_ADMINISTRATOR) {
148 if (WCF::getSession()->getPermission('admin.general.canViewPrivateUserOptions')) {
149 return true;
150 }
151 }
152
153 // check owner state
154 if ($this->editable & self::EDITABILITY_OWNER) {
155 if ($this->user === null || $this->user->userID == WCF::getUser()->userID) {
156 return true;
157 }
158 }
159
160 return false;
161 }
162 }