2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class ProfileVisibility
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
31 const VIS_NONE
= 'none';
32 const VIS_PUBLIC
= 'public';
34 const VIS_PRIVATE
= 'private';
35 const VIS_HIDDEN
= 'hidden';
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
),
47 public function __construct($level = null
)
49 $this->level
= $level;
52 public function level()
54 if ($this->level
== null
) {
55 return self
::VIS_PUBLIC
;
61 public static function defaultForRead($max_level = null
)
64 $vis = new ProfileVisibility(self
::VIS_PUBLIC
);
66 $vis = S
::user()->readVisibility();
68 if ($max_level != null
) {
69 return $vis->restrict($max_level);
75 public static function defaultForEdit($max_level = null
)
78 $vis = new ProfileVisibility(self
::VIS_NONE
);
80 $vis = S
::user()->editVisibility();
82 if ($max_level != null
) {
83 return $vis->restrict($max_level);
89 /** Retrieve a 'restricted' version of the current ProfileVisibility.
91 * @param $level The visibility level to restrict to
92 * @return A new ProfileVisibility instance, whose level is min($this->level, $level)
94 public function restrict($level = null
)
96 if ($level != null
&& !$this->isVisible($level)) {
97 $level = $this->level();
99 $level = $this->level();
102 return new ProfileVisibility($level);
105 public function levels()
107 return self
::$v_levels[$this->level()];
110 public function isVisible($visibility)
112 return in_array($visibility, $this->levels());
115 static public function comparePublicity($a, $b)
117 $a_pub = new ProfileVisibility($a['pub'], true
);
118 $b_pub = new ProfileVisibility($b['pub'], true
);
120 return !$a_pub->isVisible($b_pub->level());
125 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: