Commit | Line | Data |
---|---|---|
3c60ccca RB |
1 | <?php |
2 | /*************************************************************************** | |
12262f13 | 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
3c60ccca RB |
4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
22 | class ProfileVisibility | |
23 | { | |
38a86f60 RB |
24 | /** Visibility levels. |
25 | * none => Can't see anything | |
26 | * public => Can see public data | |
27 | * ax => Can see AX and public data | |
28 | * private => Can see private, AX and public data | |
29 | * hidden => Can only be seen by admins | |
30 | */ | |
31 | const VIS_NONE = 'none'; | |
3c60ccca RB |
32 | const VIS_PUBLIC = 'public'; |
33 | const VIS_AX = 'ax'; | |
34 | const VIS_PRIVATE = 'private'; | |
38a86f60 | 35 | const VIS_HIDDEN = 'hidden'; |
3c60ccca RB |
36 | |
37 | private $level; | |
38 | ||
38a86f60 RB |
39 | static private $v_levels = array( |
40 | self::VIS_NONE => array(), | |
41 | self::VIS_PUBLIC => array(self::VIS_PUBLIC), | |
42 | self::VIS_AX => array(self::VIS_AX, self::VIS_PUBLIC), | |
43 | self::VIS_PRIVATE => array(self::VIS_PRIVATE, self::VIS_AX, self::VIS_PUBLIC), | |
44 | self::VIS_HIDDEN => array(self::VIS_HIDDEN, self::VIS_PRIVATE, self::VIS_AX, self::VIS_PUBLIC), | |
45 | ); | |
46 | ||
47 | public function __construct($level = null) | |
3c60ccca | 48 | { |
38a86f60 | 49 | $this->level = $level; |
3c60ccca RB |
50 | } |
51 | ||
38a86f60 | 52 | public function level() |
3c60ccca | 53 | { |
38a86f60 RB |
54 | if ($this->level == null) { |
55 | return self::VIS_PUBLIC; | |
56 | } else { | |
57 | return $this->level; | |
3c60ccca | 58 | } |
38a86f60 | 59 | } |
3c60ccca | 60 | |
38a86f60 RB |
61 | public static function defaultForRead($max_level = null) |
62 | { | |
63 | if (!S::logged()) { | |
64 | $vis = new ProfileVisibility(self::VIS_PUBLIC); | |
3c60ccca | 65 | } else { |
38a86f60 | 66 | $vis = S::user()->readVisibility(); |
3c60ccca | 67 | } |
38a86f60 RB |
68 | if ($max_level != null) { |
69 | return $vis->restrict($max_level); | |
3c60ccca | 70 | } else { |
38a86f60 | 71 | return $vis; |
3c60ccca RB |
72 | } |
73 | } | |
74 | ||
38a86f60 | 75 | public static function defaultForEdit($max_level = null) |
b3d5464e | 76 | { |
38a86f60 RB |
77 | if (!S::logged()) { |
78 | $vis = new ProfileVisibility(self::VIS_NONE); | |
79 | } else { | |
80 | $vis = S::user()->editVisibility(); | |
81 | } | |
82 | if ($max_level != null) { | |
83 | return $vis->restrict($max_level); | |
84 | } else { | |
85 | return $vis; | |
b3d5464e | 86 | } |
b3d5464e SJ |
87 | } |
88 | ||
38a86f60 RB |
89 | /** Retrieve a 'restricted' version of the current ProfileVisibility. |
90 | * | |
91 | * @param $level The visibility level to restrict to | |
92 | * @return A new ProfileVisibility instance, whose level is min($this->level, $level) | |
93 | */ | |
94 | public function restrict($level = null) | |
3c60ccca | 95 | { |
38a86f60 RB |
96 | if ($level != null && !$this->isVisible($level)) { |
97 | $level = $this->level(); | |
3c60ccca | 98 | } else { |
38a86f60 | 99 | $level = $this->level(); |
3c60ccca | 100 | } |
38a86f60 RB |
101 | |
102 | return new ProfileVisibility($level); | |
3c60ccca RB |
103 | } |
104 | ||
105 | public function levels() | |
106 | { | |
38a86f60 | 107 | return self::$v_levels[$this->level()]; |
3c60ccca RB |
108 | } |
109 | ||
110 | public function isVisible($visibility) | |
111 | { | |
112 | return in_array($visibility, $this->levels()); | |
113 | } | |
b3d5464e SJ |
114 | |
115 | static public function comparePublicity($a, $b) | |
116 | { | |
117 | $a_pub = new ProfileVisibility($a['pub'], true); | |
118 | $b_pub = new ProfileVisibility($b['pub'], true); | |
119 | ||
120 | return !$a_pub->isVisible($b_pub->level()); | |
121 | } | |
3c60ccca RB |
122 | } |
123 | ||
124 | ||
125 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
126 | ?> |