In survey results, clusters all empty comments for free text
[platal.git] / include / banana / forum.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 require_once 'banana/hooks.inc.php';
23
24 function hook_checkcancel($_headers)
25 {
26 return ($_headers['x-org-id'] == S::v('hruid') or S::admin());
27 }
28
29 class ForumsBanana extends Banana
30 {
31 private $user;
32
33 public function __construct(User &$user, $params = null)
34 {
35 $this->user = &$user;
36
37 global $globals;
38 Banana::$msgedit_canattach = false;
39 Banana::$spool_root = $globals->banana->spool_root;
40 array_push(Banana::$msgparse_headers, 'x-org-id', 'x-org-mail');
41 Banana::$nntp_host = self::buildURL($user->login());
42 if (S::admin()) {
43 Banana::$msgshow_mimeparts[] = 'source';
44 }
45 Banana::$debug_nntp = ($globals->debug & DEBUG_BT);
46 Banana::$debug_smarty = ($globals->debug & DEBUG_SMARTY);
47 Banana::$feed_active = S::hasAuthToken();
48
49 parent::__construct($params, 'NNTP', 'PlatalBananaPage');
50 if (@$params['action'] == 'profile') {
51 Banana::$action = 'profile';
52 }
53 }
54
55 public static function buildURL($login = null)
56 {
57 global $globals;
58 $scheme = ($globals->banana->port == 563) ? "nntps" : "news";
59 $user = $globals->banana->web_user;
60 if ($login != null) {
61 $user .= '_' . $login;
62 }
63 return $scheme . '://' . $user
64 . ":{$globals->banana->password}@{$globals->banana->server}:{$globals->banana->port}/";
65
66 }
67
68 private function fetchProfile()
69 {
70 // Get user profile from SQL
71 $req = XDB::query("SELECT name, mail, sig,
72 FIND_IN_SET('threads',flags) AS threads,
73 FIND_IN_SET('automaj',flags) AS maj,
74 FIND_IN_SET('xface', flags) AS xface,
75 tree_unread, tree_read
76 FROM forum_profiles
77 WHERE uid = {?}", $this->user->id());
78 if ($req->numRows()) {
79 $infos = $req->fetchOneAssoc();
80 } else {
81 $infos = array();
82 }
83 if (empty($infos['name'])) {
84 $infos = array('name' => $this->user->fullName(),
85 'mail' => $this->user->forlifeEmail(),
86 'sig' => $this->user->displayName(),
87 'threads' => false,
88 'maj' => true,
89 'xface' => false,
90 'tree_unread' => 'o',
91 'tree_read' => 'dg' );
92 }
93 return $infos;
94 }
95
96 public function run()
97 {
98 global $platal, $globals;
99
100 // Update last unread time
101 $time = null;
102 if (!is_null($this->params) && isset($this->params['updateall'])) {
103 $time = intval($this->params['updateall']);
104 S::set('banana_last', $time);
105 }
106
107 $infos = $this->fetchProfile();
108 if ($infos['maj']) {
109 $time = time();
110 }
111
112 // Build user profile
113 $req = XDB::query("SELECT name
114 FROM forum_subs AS fs
115 LEFT JOIN forums AS f ON (f.fid = fs.fid)
116 WHERE uid={?}", $this->user->id());
117 Banana::$profile['headers']['From'] = $infos['name'] . ' <' . $infos['mail'] . '>';
118 Banana::$profile['headers']['Organization'] = make_Organization();
119 Banana::$profile['signature'] = $infos['sig'];
120 Banana::$profile['display'] = $infos['threads'];
121 Banana::$profile['autoup'] = $infos['maj'];
122 Banana::$profile['lastnews'] = S::v('banana_last');
123 Banana::$profile['subscribe'] = $req->fetchColumn();
124 Banana::$tree_unread = $infos['tree_unread'];
125 Banana::$tree_read = $infos['tree_read'];
126
127 // Update the "unread limit"
128 if (!is_null($time)) {
129 XDB::execute('UPDATE forum_profiles
130 SET last_seen = FROM_UNIXTIME({?})
131 WHERE uid = {?}',
132 $time, $this->user->id());
133 if (XDB::affectedRows() == 0) {
134 XDB::execute('INSERT INTO forum_profiles (uid, last_seen)
135 VALUES ({?}, FROM_UNIXTIME({?}))',
136 $this->user->id(), $time);
137 }
138 }
139
140 if (!empty($GLOBALS['IS_XNET_SITE'])) {
141 Banana::$page->killPage('forums');
142 Banana::$page->killPage('subscribe');
143 Banana::$spool_boxlist = false;
144 } else {
145 // Register custom Banana links and tabs
146 if (!Banana::$profile['autoup']) {
147 Banana::$page->registerAction('<a href=\'javascript:dynpostkv("'
148 . $platal->path . '", "updateall", ' . time() . ')\'>'
149 . 'Marquer tous les messages comme lus'
150 . '</a>', array('forums', 'thread', 'message'));
151 }
152 Banana::$page->registerPage('profile', 'Préférences', null);
153 }
154
155 // Run Bananai
156 if (Banana::$action == 'profile') {
157 Banana::$page->run();
158 return $this->action_updateProfile();
159 } else {
160 return parent::run();
161 }
162 }
163
164 public function post($dest, $reply, $subject, $body)
165 {
166 global $globals;
167 Banana::$profile['headers']['From'] = $this->user->fullName() . ' <' . $this->user->bestEmail() . '>';
168 Banana::$profile['headers']['Organization'] = make_Organization();
169 return parent::post($dest, $reply, $subject, $body);
170 }
171
172 protected function action_saveSubs($groups)
173 {
174 global $globals;
175 $uid = $this->user->id();
176
177 Banana::$profile['subscribe'] = array();
178 XDB::execute('DELETE FROM forum_subs
179 WHERE uid = {?}', $this->user->id());
180 if (!count($groups)) {
181 return true;
182 }
183
184 $fids = XDB::fetchAllAssoc('name', 'SELECT fid, name
185 FROM forums');
186 $diff = array_diff($groups, array_keys($fids));
187 foreach ($diff as $g) {
188 XDB::execute('INSERT INTO forums (name)
189 VALUES ({?})', $g);
190 $fids[$g] = XDB::insertId();
191 }
192
193 foreach ($groups as $g) {
194 XDB::execute('INSERT INTO forum_subs (fid, uid)
195 VALUES ({?}, {?})',
196 $fids[$g], $uid);
197 Banana::$profile['subscribe'][] = $g;
198 }
199 }
200
201 protected function action_updateProfile()
202 {
203 global $globals;
204 $page =& Platal::page();
205
206 $colors = glob(dirname(__FILE__) . '/../../htdocs/images/banana/m2*.gif');
207 foreach ($colors as $key=>$path) {
208 $path = basename($path, '.gif');
209 $colors[$key] = substr($path, 2);
210 }
211 $page->assign('colors', $colors);
212
213 if (Post::has('action') && Post::v('action') == 'Enregistrer') {
214 S::assert_xsrf_token();
215 $flags = new PlFlagSet();
216 if (Post::b('bananadisplay')) {
217 $flags->addFlag('threads');
218 }
219 if (Post::b('bananaupdate')) {
220 $flags->addFlag('automaj');
221 }
222 if (Post::b('bananaxface')) {
223 $flags->addFlag('xface');
224 }
225 $unread = Post::s('unread');
226 $read = Post::s('read');
227 if (!in_array($unread, $colors) || !in_array($read, $colors)) {
228 $page->trigError('Le choix de type pour l\'arborescence est invalide');
229 } else {
230 $last_seen = XDB::query('SELECT last_seen
231 FROM forum_profiles
232 WHERE uid = {?}', $this->user->id());
233 if ($last_seen->numRows() > 0) {
234 $last_seen = $last_seen->fetchOneCell();
235 } else {
236 $last_seen = '0000-00-00';
237 }
238 XDB::execute('REPLACE INTO forum_profiles (uid, sig, mail, name, flags, tree_unread, tree_read, last_seen)
239 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
240 $this->user->id(), Post::v('bananasig'),
241 Post::v('bananamail'), Post::v('banananame'),
242 $flags, $unread, $read, $last_seen);
243 $page->trigSuccess('Ton profil a été mis à jour');
244 }
245 }
246
247 $infos = $this->fetchProfile();
248 $page->assign('nom' , $infos['name']);
249 $page->assign('mail', $infos['mail']);
250 $page->assign('sig', $infos['sig']);
251 $page->assign('disp', $infos['threads']);
252 $page->assign('maj', $infos['maj']);
253 $page->assign('xface', $infos['xface']);
254 $page->assign('unread', $infos['tree_unread']);
255 $page->assign('read', $infos['tree_read']);
256 return null;
257 }
258 }
259
260 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
261 ?>