1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * citoyen actions. |
---|
5 | * |
---|
6 | * @package cpc |
---|
7 | * @subpackage citoyen |
---|
8 | * @author Your name here |
---|
9 | * @version SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $ |
---|
10 | */ |
---|
11 | class citoyenActions extends sfActions |
---|
12 | { |
---|
13 | /** |
---|
14 | * Executes index action |
---|
15 | * |
---|
16 | * @param sfRequest $request A request object |
---|
17 | */ |
---|
18 | public function executeIndex(sfWebRequest $request) |
---|
19 | { |
---|
20 | $this->order = $request->getParameter('order'); |
---|
21 | if (!$this->order || !(preg_match('/^(alpha|comm|date|last)$/', $this->order))) |
---|
22 | $this->order = 'date'; |
---|
23 | $datecom = ""; |
---|
24 | if ($this->order === "last") |
---|
25 | $datecom = ", max(co.created_at) as date"; |
---|
26 | $query = Doctrine::getTable('Citoyen') |
---|
27 | ->createQuery('c') |
---|
28 | ->select('c.*, sum(co.is_public) as nb_comment'.$datecom) |
---|
29 | ->leftJoin('c.Commentaires co') |
---|
30 | ->where('c.is_active = ?', true) |
---|
31 | ->groupBy('c.id'); |
---|
32 | if ($this->order === "date") { |
---|
33 | $this->title = 'Les derniers citoyens inscrits'; |
---|
34 | $query->orderBy('c.created_at DESC'); |
---|
35 | } else if ($this->order === "comm") { |
---|
36 | $this->title = 'Les citoyens ayant le plus commenté'; |
---|
37 | $query->orderBy('nb_comment DESC'); |
---|
38 | } else if ($this->order === "alpha") { |
---|
39 | $this->title = 'Les citoyens inscrits'; |
---|
40 | $query->orderBy('c.login'); |
---|
41 | } else if ($this->order === "last") { |
---|
42 | $this->title = 'Les derniers citoyens ayant commenté'; |
---|
43 | $query->orderBy('date desc'); |
---|
44 | } |
---|
45 | $this->pager = Doctrine::getTable('Citoyen')->getPager($request, $query); |
---|
46 | $this->citoyens = $query->execute(); |
---|
47 | $this->getResponse()->setTitle($this->title." sur NosDéputés.fr"); |
---|
48 | $this->comments = Doctrine_Query::create() |
---|
49 | ->select('count(distinct(citoyen_id)) as auteurs, count(distinct(id)) as comments') |
---|
50 | ->from('Commentaire') |
---|
51 | ->where('is_public = 1') |
---|
52 | ->fetchOne(); |
---|
53 | } |
---|
54 | |
---|
55 | public function executeNotauthorized(sfWebRequest $request) |
---|
56 | { |
---|
57 | $this->getUser()->setFlash('notice', 'Vous ne pouvez pas accéder à cette page'); |
---|
58 | $this->getResponse()->setStatusCode(403); |
---|
59 | } |
---|
60 | |
---|
61 | public function executeRedirect(sfWebRequest $request) { |
---|
62 | $user = Doctrine::getTable('Citoyen')->find($request->getParameter('id')); |
---|
63 | $this->forward404Unless($user); |
---|
64 | $this->redirect('@citoyen?slug='.$user->slug); |
---|
65 | } |
---|
66 | |
---|
67 | public function executeShow(sfWebRequest $request) |
---|
68 | { |
---|
69 | $slug = $request->getParameter('slug'); |
---|
70 | $this->user = Doctrine::getTable('Citoyen')->findOneBySlug($slug); |
---|
71 | $this->forward404Unless($this->user->is_active); |
---|
72 | $response = $this->getResponse(); |
---|
73 | $response->setTitle('Profil de '.$this->user->login); |
---|
74 | $this->getUser()->setAttribute('token', md5(microtime(true) . mt_rand(0,10000))); |
---|
75 | } |
---|
76 | |
---|
77 | // Inscription |
---|
78 | public function executeNew(sfWebRequest $request) |
---|
79 | { |
---|
80 | if (!$this->getUser()->isAuthenticated()) { |
---|
81 | $this->form = new InscriptionForm(); |
---|
82 | if ($request->isMethod('post')) |
---|
83 | { |
---|
84 | $this->form->bind($request->getParameter('citoyen')); |
---|
85 | |
---|
86 | if ($this->form->isValid()) |
---|
87 | { |
---|
88 | $this->getUser()->setAttribute('partial', 'inscription'); |
---|
89 | if (!myUser::CreateAccount($this->form->getValue('login'), $this->form->getValue('email'), $this)) |
---|
90 | { return;} |
---|
91 | $this->redirect('@homepage'); |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | else if ($this->getUser()->isAuthenticated() and $this->getUser()->getAttribute('is_active') == true) |
---|
96 | { |
---|
97 | $slug = $this->getUser()->getAttribute('slug'); |
---|
98 | $this->getUser()->setFlash('notice', 'Vous êtes déja inscrit'); |
---|
99 | $this->redirect('@citoyen?slug='.$slug); |
---|
100 | } |
---|
101 | else |
---|
102 | { |
---|
103 | $this->getUser()->setFlash('notice', 'Vous allez recevoir un email de confirmation. Pour finaliser votre inscription, veuillez cliquer sur le lien d\'activation contenu dans cet email.'); |
---|
104 | $this->redirect('@homepage'); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | public function executeEdit(sfWebRequest $request) |
---|
109 | { |
---|
110 | if ($this->getUser()->isAuthenticated() and $this->getUser()->getAttribute('is_active') == true) { |
---|
111 | |
---|
112 | $this->user = Doctrine::getTable('Citoyen')->findOneById($this->getUser()->getAttribute('user_id')); |
---|
113 | |
---|
114 | $this->form = new EditUserForm($this->user); |
---|
115 | |
---|
116 | if ($request->isMethod('put')) |
---|
117 | { |
---|
118 | $this->form->bind($request->getParameter('citoyen')); |
---|
119 | |
---|
120 | if ($this->form->isValid()) |
---|
121 | { |
---|
122 | $this->form->save(); |
---|
123 | $this->getUser()->setFlash('notice', 'Vous avez modifié votre profil avec succès'); |
---|
124 | $this->redirect('@citoyen?slug='.$this->getUser()->getAttribute('slug')); |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | else { $this->redirect('@signin'); } |
---|
129 | } |
---|
130 | |
---|
131 | public function executeEditpassword(sfWebRequest $request) |
---|
132 | { |
---|
133 | if ($this->getUser()->isAuthenticated() and $this->getUser()->getAttribute('is_active') == true) { |
---|
134 | |
---|
135 | $this->user = Doctrine::getTable('Citoyen')->findOneById($this->getUser()->getAttribute('user_id')); |
---|
136 | |
---|
137 | $this->form = new ChangeMotdepasseForm(); |
---|
138 | |
---|
139 | if ($request->isMethod('post')) |
---|
140 | { |
---|
141 | $this->form->bind($request->getParameter('citoyen')); |
---|
142 | |
---|
143 | if ($this->form->isValid()) |
---|
144 | { |
---|
145 | if (sha1($this->form->getvalue('ancienpassword')) != $this->user->password) |
---|
146 | { |
---|
147 | $this->getUser()->setFlash('error', 'Veuillez indiquer votre ancien mot de passe'); |
---|
148 | return; |
---|
149 | } |
---|
150 | if ($this->form->getvalue('password') != $this->form->getvalue('password_bis')) |
---|
151 | { |
---|
152 | $this->getUser()->setFlash('error', 'Les 2 champs doivent être identiques'); |
---|
153 | return; |
---|
154 | } |
---|
155 | $this->user->password = $this->form->getvalue('password'); |
---|
156 | $this->user->save(); |
---|
157 | $this->getUser()->setFlash('notice', 'Vous avez modifié votre mot de passe avec succès'); |
---|
158 | $this->redirect('@citoyen?slug='.$this->getUser()->getAttribute('slug')); |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|
162 | else { $this->redirect('@signin'); } |
---|
163 | } |
---|
164 | |
---|
165 | public function executeActivation(sfWebRequest $request) |
---|
166 | { |
---|
167 | $this->slug = $request->getParameter('slug'); |
---|
168 | $this->activation_id = $request->getParameter('activation_id'); |
---|
169 | |
---|
170 | if ($this->getUser()->isAuthenticated() and $this->getUser()->getAttribute('is_active') != 0) |
---|
171 | { |
---|
172 | $userslug = $this->getUser()->getAttribute('slug'); |
---|
173 | if ($userslug === $this->slug) |
---|
174 | $this->getUser()->setFlash('notice', 'Vous avez déjà activé votre compte.'); |
---|
175 | else { |
---|
176 | sfProjectConfiguration::getActive()->loadHelpers(array('Url')); |
---|
177 | $this->getUser()->setFlash('notice', 'Vous êtes déjà connecté en tant que '.$this->getUser()->getAttribute('login').'. Pour activer le compte '.$this->slug.', veuillez d\'abord <a href="'.url_for('@signout').'">vous déconnecter</a>, puis cliquer de nouveau sur le lien dans l\'e-mail que vous avez reçu.'); |
---|
178 | } |
---|
179 | $this->redirect('@citoyen?slug='.$userslug); |
---|
180 | } |
---|
181 | |
---|
182 | if (Doctrine::getTable('Citoyen')->findOneBySlug($this->slug)) |
---|
183 | { |
---|
184 | self::setmotdepasse($this, $request); |
---|
185 | } |
---|
186 | else |
---|
187 | { |
---|
188 | $this->forward404(); |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | private function setmotdepasse($action, $request) |
---|
193 | { |
---|
194 | $user = Doctrine::getTable('Citoyen')->findOneBySlug($action->slug); |
---|
195 | if (!$user) $action->forward404(); |
---|
196 | if ($user->activation_id == $action->activation_id) |
---|
197 | { |
---|
198 | $action->form = new MotdepasseForm(); |
---|
199 | |
---|
200 | if ($request->isMethod('post')) |
---|
201 | { |
---|
202 | $action->form->bind($request->getParameter('citoyen')); |
---|
203 | |
---|
204 | if ($action->form->isValid()) |
---|
205 | { |
---|
206 | if ($action->form->getvalue('password') != $action->form->getvalue('password_bis')) |
---|
207 | { |
---|
208 | $action->getUser()->setFlash('error', 'Les 2 champs doivent être identiques'); |
---|
209 | return; |
---|
210 | } |
---|
211 | $user->password = $action->form->getvalue('password'); |
---|
212 | if ($user->is_active == 0) { $user->is_active = true; $msg = 'Votre compte a été activé avec succès.'; } |
---|
213 | else { $msg = 'Votre mot de passe a été réinitialisé avec succès.'; } |
---|
214 | $user->activation_id = null; |
---|
215 | $user->save(); |
---|
216 | |
---|
217 | $commentaires = Doctrine::getTable('Commentaire')->createQuery('c') |
---|
218 | ->where('is_public = 0') |
---|
219 | ->andWhere('citoyen_id = ?', $user->id) |
---|
220 | ->execute(); |
---|
221 | foreach ($commentaires as $c) |
---|
222 | { |
---|
223 | $c->is_public = 1; |
---|
224 | $c->save(); |
---|
225 | $c->updateNbCommentaires(); |
---|
226 | } |
---|
227 | if ($action->getUser()->isAuthenticated()) { $action->getUser()->setAttribute('is_active', $user->is_active); } |
---|
228 | else { myUser::SignIn($user->getLogin(), $action->form->getvalue('password'), false, $action); } |
---|
229 | $action->getUser()->setFlash('notice', $msg); |
---|
230 | $action->redirect('@citoyen?slug='.$action->slug); |
---|
231 | } |
---|
232 | } |
---|
233 | } |
---|
234 | else |
---|
235 | { |
---|
236 | if($user->activation_id != null) |
---|
237 | { |
---|
238 | $user->activation_id = null; |
---|
239 | $user->save(); |
---|
240 | $action->getUser()->setFlash('notice', 'Une erreur s\'est produite'); |
---|
241 | } else |
---|
242 | $action->getUser()->setFlash('notice', 'Ce compte a déjà été activé'); |
---|
243 | $action->redirect('@citoyen?slug='.$action->slug); |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | // Connection |
---|
248 | public function executeSignin(sfWebRequest $request) |
---|
249 | { |
---|
250 | if (!$this->getUser()->isAuthenticated()) { |
---|
251 | |
---|
252 | $this->form = new SigninForm(); |
---|
253 | |
---|
254 | if ($request->isMethod('post')) |
---|
255 | { |
---|
256 | $this->form->bind($request->getParameter('signin')); |
---|
257 | |
---|
258 | if ($this->form->isValid()) |
---|
259 | { |
---|
260 | myUser::SignIn($this->form->getValue('login'), $this->form->getValue('password'), $this->form->getValue('remember'), $this); |
---|
261 | $this->redirect($request->getReferer()); |
---|
262 | } |
---|
263 | } |
---|
264 | } |
---|
265 | else |
---|
266 | { |
---|
267 | $slug = $this->getUser()->getAttribute('slug'); |
---|
268 | $this->getUser()->setFlash('notice', 'Vous êtes connecté'); |
---|
269 | $this->redirect('@citoyen?slug='.$slug); |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | public function executeSignout(sfWebRequest $request) |
---|
274 | { |
---|
275 | $this->deconnexion(); |
---|
276 | $this->getUser()->setFlash('notice', 'Vous avez été déconnecté avec succès'); |
---|
277 | $this->redirect($request->getReferer()); |
---|
278 | } |
---|
279 | |
---|
280 | protected function deconnexion() |
---|
281 | { |
---|
282 | $this->getUser()->getAttributeHolder()->clear(); |
---|
283 | $this->getUser()->clearCredentials(); |
---|
284 | $this->getUser()->setAuthenticated(false); |
---|
285 | sfContext::getInstance()->getResponse()->setCookie('remember', ''); |
---|
286 | } |
---|
287 | |
---|
288 | public function executeDelete(sfWebRequest $request) |
---|
289 | { |
---|
290 | if ($this->getUser()->isAuthenticated()) |
---|
291 | { |
---|
292 | if($this->getUser()->getAttribute('token') != $request->getParameter('token')) |
---|
293 | $this->getUser()->setFlash('error','Jeton incorrect. Essayez de recharger la page d\'ou vous venez.'); |
---|
294 | else |
---|
295 | { |
---|
296 | $user = Doctrine::getTable('Citoyen')->findOneById($this->getUser()->getAttribute('user_id')); |
---|
297 | $user->delete(); |
---|
298 | $this->deconnexion(); |
---|
299 | $this->getUser()->setFlash('notice', 'Votre compte a été supprimé avec succès'); |
---|
300 | } |
---|
301 | } |
---|
302 | else |
---|
303 | $this->getUser()->setFlash('error', 'Vous devez être connecté pour exécuter cette action'); |
---|
304 | $this->redirect('@homepage'); |
---|
305 | } |
---|
306 | |
---|
307 | public function executeUploadavatar(sfWebRequest $request) |
---|
308 | { |
---|
309 | if ($this->getUser()->isAuthenticated() and $this->getUser()->getAttribute('is_active') == true) { |
---|
310 | |
---|
311 | $this->user = Doctrine::getTable('Citoyen')->findOneById($this->getUser()->getAttribute('user_id')); |
---|
312 | |
---|
313 | $this->form = new UploadAvatarForm(); |
---|
314 | |
---|
315 | if ($request->isMethod('post')) |
---|
316 | { |
---|
317 | $this->form->bind($request->getParameter('upload'), $request->getFiles('upload')); |
---|
318 | |
---|
319 | if ($this->form->isValid() and $this->form->getValue('photo')) |
---|
320 | { |
---|
321 | $file = $this->form->getValue('photo'); |
---|
322 | |
---|
323 | $photo = $file->getTempName(); |
---|
324 | list($largeur_source, $hauteur_source) = getimagesize($photo); |
---|
325 | |
---|
326 | $largeur = $hauteur = 100; |
---|
327 | if ($largeur_source >= $hauteur_source) { $hauteur = round($hauteur_source * $largeur / $largeur_source); } |
---|
328 | else { $largeur = round($largeur_source * $hauteur / $hauteur_source); } |
---|
329 | |
---|
330 | $source = imagecreatefromstring(file_get_contents($photo)); |
---|
331 | $destination = imagecreatetruecolor(100, 100); |
---|
332 | $white = imagecolorallocate($destination, 255, 255, 255); |
---|
333 | imagefilledrectangle($destination, 0, 0, 100, 100, $white); |
---|
334 | imagecopyresampled($destination, $source, (100-$largeur)/2, (100-$hauteur)/2, 0, 0, $largeur, $hauteur, $largeur_source, $hauteur_source); |
---|
335 | |
---|
336 | imagejpeg($destination, $photo); |
---|
337 | $this->user->photo = file_get_contents($photo); |
---|
338 | $this->user->save(); |
---|
339 | $this->getUser()->setFlash('notice', 'Vous avez modifié votre profil avec succès'); |
---|
340 | return $this->redirect('@edit_citoyen'); |
---|
341 | } |
---|
342 | else |
---|
343 | { |
---|
344 | $this->getUser()->setFlash('error', 'Veuillez indiquer votre photo/avatar'); |
---|
345 | } |
---|
346 | } |
---|
347 | } |
---|
348 | else { $this->redirect('@signin'); } |
---|
349 | } |
---|
350 | |
---|
351 | public function executePhoto($request) |
---|
352 | { |
---|
353 | $slug = $request->getParameter('slug'); |
---|
354 | $this->user = Doctrine::getTable('Citoyen')->findOneBySlug($slug); |
---|
355 | if (!$this->user || !$this->user->photo) { |
---|
356 | return $this->redirect('http://'.$_SERVER['HTTP_HOST'].'/images/xneth/avatar_citoyen.png'); |
---|
357 | } |
---|
358 | $this->setLayout(false); |
---|
359 | $this->getResponse()->setHttpHeader('content-type', 'image/jpeg'); |
---|
360 | $this->getResponse()->addCacheControlHttpHeader('max_age=60'); |
---|
361 | $this->getResponse()->setHttpHeader('Expires', $this->getResponse()->getDate(time()*2)); |
---|
362 | } |
---|
363 | |
---|
364 | public function executeResetmotdepasse(sfWebRequest $request) |
---|
365 | { |
---|
366 | $this->slug = $request->getParameter('slug'); |
---|
367 | $this->activation_id = $request->getParameter('activation_id'); |
---|
368 | if (Doctrine::getTable('Citoyen')->findOneBySlug($this->slug)->is_active < 0) { |
---|
369 | $this->getUser()->setFlash('error', 'Ce compte a été désactivé'); |
---|
370 | return; |
---|
371 | } |
---|
372 | if ($this->getUser()->isAuthenticated()) |
---|
373 | { |
---|
374 | $user = Doctrine::getTable('Citoyen')->findOneBySlug($this->getUser()->getAttribute('slug')); |
---|
375 | self::sendmailresetmotdepasse($user, $this); |
---|
376 | } |
---|
377 | else if ($this->slug and $this->activation_id) |
---|
378 | { |
---|
379 | if (Doctrine::getTable('Citoyen')->findOneBySlug($this->slug)) |
---|
380 | { |
---|
381 | self::setmotdepasse($this, $request); |
---|
382 | } |
---|
383 | } |
---|
384 | else |
---|
385 | { |
---|
386 | $this->first = true; |
---|
387 | $this->form = new ResetMotdepasseForm(); |
---|
388 | |
---|
389 | if ($request->isMethod('post')) |
---|
390 | { |
---|
391 | $this->form->bind($request->getParameter('reset')); |
---|
392 | |
---|
393 | if ($this->form->isValid()) |
---|
394 | { |
---|
395 | $login = $this->form->getValue('login'); |
---|
396 | |
---|
397 | if($this->form->getValue('code') != $this->getUser()->getAttribute('codesecu')) |
---|
398 | { |
---|
399 | $this->getUser()->setFlash('error', 'Le code de sécurité ne correspond pas'); |
---|
400 | return; |
---|
401 | } |
---|
402 | |
---|
403 | if ($login) |
---|
404 | { |
---|
405 | if (Doctrine::getTable('Citoyen')->findOneByEmail($login)) |
---|
406 | { |
---|
407 | $user = Doctrine::getTable('Citoyen')->findOneByEmail($login); |
---|
408 | } |
---|
409 | else if (Doctrine::getTable('Citoyen')->findOneByLogin($login)) |
---|
410 | { |
---|
411 | $user = Doctrine::getTable('Citoyen')->findOneByLogin($login); |
---|
412 | } |
---|
413 | else |
---|
414 | { |
---|
415 | $this->getUser()->setFlash('error', 'Aucun utilisateur enregistré ne correspond'); |
---|
416 | return; |
---|
417 | } |
---|
418 | } |
---|
419 | else |
---|
420 | { |
---|
421 | $this->getUser()->setFlash('error', 'Veuillez indiquer votre nom d\'utilisateur <strong>ou</strong> votre email'); |
---|
422 | return; |
---|
423 | } |
---|
424 | self::sendmailresetmotdepasse($user, $this); |
---|
425 | } |
---|
426 | } |
---|
427 | } |
---|
428 | } |
---|
429 | |
---|
430 | private function sendmailresetmotdepasse($user, $action) |
---|
431 | { |
---|
432 | $activation_id = md5(time()*rand()); |
---|
433 | $user->activation_id = $activation_id; |
---|
434 | $user->save(); |
---|
435 | |
---|
436 | $action->getComponent('mail', 'send', array( |
---|
437 | 'subject'=>'Réinitialisation de votre mot de passe - NosDéputés.fr', |
---|
438 | 'to'=>array($user->email), |
---|
439 | 'partial'=>'resetmotdepasse', |
---|
440 | 'mailContext'=>array('activation_id' => $activation_id, 'slug' => $user->slug) |
---|
441 | )); |
---|
442 | |
---|
443 | $action->getUser()->setFlash('notice', 'Un email de réinitialisation de mot de passe vient de vous être envoyé.<br />Si vous rencontrez un problème lors de cette procédure veuillez nous contacter par email à l\'adresse contact[at]regardscitoyens.org.'); |
---|
444 | $action->redirect('@homepage'); |
---|
445 | } |
---|
446 | |
---|
447 | public function executeConnected(sfWebRequest $request) |
---|
448 | { |
---|
449 | $this->setLayout(false); |
---|
450 | $this->getResponse()->setHttpHeader('content-type', 'text/plain'); |
---|
451 | } |
---|
452 | |
---|
453 | /* |
---|
454 | public function executeAddcirco(sfWebRequest $request) |
---|
455 | { |
---|
456 | if ($this->getUser()->isAuthenticated()) |
---|
457 | { |
---|
458 | $nom_circo = $request->getParameter('nom_circo'); |
---|
459 | $num_circo = $request->getParameter('num_circo'); |
---|
460 | $user = Doctrine::getTable('Citoyen')->findOneById($this->getUser()->getAttribute('user_id')); |
---|
461 | $user->nom_circo = $nom_circo; |
---|
462 | $user->num_circo = $num_circo; |
---|
463 | $user->save(); |
---|
464 | $this->redirect('@citoyen?slug='.$user->slug); |
---|
465 | } |
---|
466 | else |
---|
467 | { |
---|
468 | $this->redirect('@signin'); |
---|
469 | } |
---|
470 | } */ |
---|
471 | |
---|
472 | } |
---|