Add some comments to Phone.
[platal.git] / classes / profilevisibility.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
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 {
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';
32 const VIS_PUBLIC = 'public';
33 const VIS_AX = 'ax';
34 const VIS_PRIVATE = 'private';
35 const VIS_HIDDEN = 'hidden';
36
37 private $level;
38
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)
48 {
49 $this->level = $level;
50 }
51
52 public function level()
53 {
54 if ($this->level == null) {
55 return self::VIS_PUBLIC;
56 } else {
57 return $this->level;
58 }
59 }
60
61 public static function defaultForRead($max_level = null)
62 {
63 if (!S::logged()) {
64 $vis = new ProfileVisibility(self::VIS_PUBLIC);
65 } else {
66 $vis = S::user()->readVisibility();
67 }
68 if ($max_level != null) {
69 return $vis->restrict($max_level);
70 } else {
71 return $vis;
72 }
73 }
74
75 public static function defaultForEdit($max_level = null)
76 {
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;
86 }
87 }
88
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)
95 {
96 if ($level != null && !$this->isVisible($level)) {
97 $level = $this->level();
98 } else {
99 $level = $this->level();
100 }
101
102 return new ProfileVisibility($level);
103 }
104
105 public function levels()
106 {
107 return self::$v_levels[$this->level()];
108 }
109
110 public function isVisible($visibility)
111 {
112 return in_array($visibility, $this->levels());
113 }
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 }
122 }
123
124
125 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
126 ?>