Commit | Line | Data |
---|---|---|
8c39c698 FB |
1 | <?php |
2 | ||
3 | class xorgPostPermsWidget { | |
4 | public static function behavior_adminPostFormSidebar($post) { | |
5 | $choices = array('public' => array('text' => 'Visible par tous', | |
6 | 'selected' => false), | |
7 | 'auth' => array('text' => 'Visible par les X', | |
8 | 'selected' => false)); | |
9 | $pos = 'public'; | |
10 | if (!is_null($post)) { | |
11 | $metas = unserialize($post->field('post_meta')); | |
12 | if (isset($metas['post_xorg_perms'])) { | |
13 | $pos = $metas['post_xorg_perms']; | |
14 | } | |
15 | } else { | |
16 | global $core; | |
17 | $pos = $core->auth->getOption('post_xorg_perms'); | |
18 | if ($pos && !isset($choices[$pos])) { | |
19 | $pos = 'auth'; | |
20 | } | |
21 | } | |
22 | $choices[$pos]['selected'] = true; | |
23 | echo '<p><label>Visibilité du billet :' | |
24 | . ' <select name="post_xorg_perms">'; | |
25 | foreach ($choices as $val => $fields) { | |
26 | echo '<option value="' . $val . '"' . ($fields['selected'] ? ' selected="selected"' : '') . '>' | |
27 | . $fields['text'] . '</option>'; | |
28 | } | |
29 | echo ' </select>' | |
30 | . '</label></p>'; | |
31 | } | |
32 | ||
33 | private static function setPermsMeta(&$cur) { | |
34 | $meta = $cur->getField('post_meta'); | |
35 | if (is_string($meta)) { | |
36 | $meta = unserialize($meta); | |
37 | } | |
38 | if (!is_array($meta)) { | |
39 | $meta = array(); | |
40 | } | |
41 | $meta['post_xorg_perms'] = $_POST['post_xorg_perms']; | |
42 | $cur->setField('post_meta', serialize($meta)); | |
43 | } | |
44 | ||
45 | public static function behavior_adminBeforePostCreate(&$cur) { | |
46 | self::setPermsMeta($cur); | |
47 | } | |
48 | ||
49 | public static function behavior_adminBeforePostUpdate(&$cur, $post_id) { | |
50 | self::setPermsMeta($cur); | |
51 | } | |
52 | ||
53 | ||
54 | public static function behavior_adminPreferencesForm($core) { | |
55 | $levels = array('public' => array('text' => 'Visible par tous', | |
56 | 'selected' => false), | |
57 | 'auth' => array('text' => 'Visible par les X', | |
58 | 'selected' => false), | |
59 | 'group' => array('text' => 'Visible par les membres du groupe (1)', | |
60 | 'selected' => false)); | |
61 | $pos = $core->auth->getOption('post_xorg_perms'); | |
62 | if (!$pos || !isset($levels[$pos])) { | |
63 | $pos = 'public'; | |
64 | } | |
65 | $levels[$pos]['selected'] = true; | |
66 | echo '<p><label>Visibilité nouveaux billets par défaut :' | |
67 | . ' <select name="post_xorg_perms">'; | |
68 | foreach ($levels as $key => $fields) { | |
69 | echo '<option value="' . $key . '"' . ($fields['selected'] ? ' selected="selected"' : '') . '>' | |
70 | . $fields['text'] . '</option>'; | |
71 | } | |
72 | echo '</select>' | |
73 | . '(1) Ne concerne que les blogs de groupes X. Equivaut à "Visible par les X" sur les autres blogs"' | |
74 | . '</label></p>'; | |
75 | } | |
76 | ||
77 | public static function behavior_adminBeforeUserUpdate(&$cur, $user_id) { | |
78 | $opts = $cur->getField('user_options'); | |
79 | $opts['post_xorg_perms'] = $_POST['post_xorg_perms']; | |
80 | $cur->setField('user_options', $opts); | |
81 | } | |
d387689d FB |
82 | |
83 | public static function behavior_coreBlogGetPosts(&$rs) { | |
84 | $rs->extend('xorgPostPermsFilter'); | |
85 | } | |
86 | ||
87 | /* public static function behavior_coreBlogGetComments(&$rs) { | |
88 | $rs->extends('xorgCommentPermsFilter'); | |
89 | }*/ | |
90 | } | |
91 | ||
92 | if (class_exists('rsExtPostPublic')) { | |
93 | ||
94 | class xorgPostPermsFilter extends rsExtPostPublic { | |
95 | private static function canRead(&$rs) { | |
96 | $metas = unserialize($rs->field('post_meta')); | |
97 | global $core; | |
98 | if (!isset($metas['post_xorg_perms'])) { | |
99 | return true; | |
100 | } elseif ($metas['post_xorg_perms'] == 'public') { | |
101 | return true; | |
102 | } elseif ($metas['post_xorg_perms'] == 'auth' && $core->auth->userID()) { | |
103 | return true; | |
104 | } elseif ($metas['post_xorg_perms'] == 'group' && $core->auth->getInfo('xorg_group_member')) { | |
105 | return true; | |
106 | } | |
107 | return false; | |
108 | } | |
109 | ||
f4aeade7 FB |
110 | private static function showMessage(&$rs) { |
111 | $metas = unserialize($rs->field('post_meta')); | |
112 | global $core; | |
113 | $str = '<p class="error">' | |
114 | . 'Vous n\'avez pas les droits suffisants pour lire ce billet<br />'; | |
115 | if (!$core->auth->userID()) { | |
116 | $str .= 'Vous devez vous <a href="' . $core->blog->url . 'auth/Xorg?path=' . $_SERVER['REQUEST_URI'] .'">authentifier</a>'; | |
117 | } else { | |
118 | $str .= 'Tu dois être membre du groupe pour lire ce message'; | |
119 | } | |
120 | return $str . '</p>'; | |
121 | } | |
122 | ||
d387689d FB |
123 | public static function getContent(&$rs, $absolute_urls = false) { |
124 | if (self::canRead($rs)) { | |
125 | return parent::getContent(&$rs, $absolute_urls); | |
126 | } else if (!self::isExtended($rs)) { | |
f4aeade7 | 127 | return self::showMessage($rs); |
d387689d FB |
128 | } else { |
129 | return null; | |
130 | } | |
131 | } | |
132 | ||
133 | public static function getExcerpt(&$rs, $absolute_urls = false) { | |
134 | if (self::canRead($rs)) { | |
135 | return parent::getContent(&$rs, $absolute_urls); | |
136 | } else if (self::isExtended($rs)) { | |
f4aeade7 | 137 | return self::showMessage($rs); |
d387689d FB |
138 | } else { |
139 | return null; | |
140 | } | |
141 | } | |
142 | ||
143 | public static function commentsActive(&$rs) { | |
144 | return self::canRead($rs) && parent::commentsActive($rs); | |
145 | } | |
146 | ||
147 | public static function trackbacksActive(&$rs) { | |
148 | return self::canRead($rs) && parent::trackbacksActive($rs); | |
149 | } | |
150 | ||
151 | public static function hasComments(&$rs) { | |
152 | return self::canRead($rs) && parent::hasComments($rs); | |
153 | } | |
154 | } | |
8c39c698 FB |
155 | } |
156 | ?> |