2 /***************************************************************************
3 * Copyright (C) 2003-2014 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 ***************************************************************************/
24 /** Visibility levels.
25 * The VIEW_* constants describe the access level
26 * The EXPORT_* constants describe the degree of confidentiality of the data.
28 const VIEW_NONE
= 'none';
29 const VIEW_PUBLIC
= 'public';
31 const VIEW_PRIVATE
= 'private';
32 const VIEW_ADMIN
= 'admin';
34 const EXPORT_PUBLIC
= 'public';
35 const EXPORT_AX
= 'ax';
36 const EXPORT_PRIVATE
= 'private';
37 const EXPORT_HIDDEN
= 'hidden';
39 /** Map each VIEW_ level to the list of EXPORT_ levels it can view.
41 static private $view_levels = array(
42 self
::VIEW_NONE
=> array(),
43 self
::VIEW_PUBLIC
=> array(self
::EXPORT_PUBLIC
),
44 self
::VIEW_AX
=> array(self
::EXPORT_AX
, self
::EXPORT_PUBLIC
),
45 self
::VIEW_PRIVATE
=> array(self
::EXPORT_PRIVATE
, self
::EXPORT_AX
, self
::EXPORT_PUBLIC
),
46 self
::VIEW_ADMIN
=> array(self
::EXPORT_HIDDEN
, self
::EXPORT_PRIVATE
, self
::EXPORT_AX
, self
::EXPORT_PUBLIC
),
49 static private $display_levels = array(
50 self
::EXPORT_PUBLIC
=> 0,
52 self
::EXPORT_PRIVATE
=> 2,
53 self
::EXPORT_HIDDEN
=> 3,
58 private function __construct($level)
60 $this->level
= $level;
63 static private $vis_list = array();
64 public static function get($level)
66 Platal
::assert(array_key_exists($level, self
::$view_levels), "Invalid visibility access level $level.");
67 if (!array_key_exists($level, self
::$vis_list)) {
68 self
::$vis_list[$level] = new Visibility($level);
70 return self
::$vis_list[$level];
73 public function level()
78 public static function defaultForRead($max_level = null
)
81 $vis = self
::get(self
::VIEW_PUBLIC
);
83 $vis = S
::user()->readVisibility();
85 if ($max_level != null
) {
86 return $vis->restrict($max_level);
92 public static function defaultForEdit($max_level = null
)
95 $vis = self
::get(self
::VIEW_NONE
);
97 $vis = S
::user()->editVisibility();
99 if ($max_level != null
) {
100 return $vis->restrict($max_level);
106 /** Retrieve a 'restricted' version of the current Visibility.
108 * @param $level The visibility level to restrict to
109 * @return A new Visibility instance, whose level is min($this->level, $level)
111 public function restrict($level = null
)
113 if ($level != null
&& !$this->isVisible($level)) {
114 $level = $this->level();
117 return self
::get($level);
120 public function isVisible($visibility)
122 return in_array($visibility, self
::$view_levels[$this->level()]);
125 public function equals($visibility)
127 return $visibility !== null
&& $this->level() == $visibility->level();
130 static public function isLessRestrictive($level_a, $level_b)
132 // self::$display_levels is order from least restrictive
133 // to most restrictive.
134 return self
::$display_levels[$level_a] >= self
::$display_levels[$level_b];
137 /** Compare the visibility of two fields.
139 * >0 if $a is less restrictive than $b,
140 * <0 if $a is more restrictive than $b,
141 * 0 if $a is equal to $b.
143 static public function cmpLessRestrictive($a, $b)
145 $a_pub = self
::$display_levels[$a];
146 $b_pub = self
::$display_levels[$b];
147 /* self::$display_levels is ordered from least restrictive to
149 * This will be 0 if both levels are equal, < 0 if $b_pub is less
150 * than $a_pub, thus less restrictive, which means that $a comes
151 * before $b in descending restrictiveness order.
153 return $b_pub - $a_pub;
156 static public function comparePublicity($a, $b)
158 return self
::cmpLessRestrictive($a['pub'], $b['pub']);
162 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: