Sfoglia il codice sorgente

Evolution #147: Profil : Configuration de son adresse (gmap et tout)

bastien 13 anni fa
parent
commit
17d19f9423

+ 10 - 0
app/Resources/translations/userui.fr.yml Vedi File

@@ -19,6 +19,16 @@ my_account:
19 19
   favorite_tags:    Vos tags préférés
20 20
   change_password:  Changer mon mot de passe
21 21
   change_email:     Changer mon adresse email
22
+  address:
23
+    title:          Mon adresse
24
+    description:    Avec votre adresse Muzi.ch pourra vous avertir plus aisément des événements à venirs dans votre région.
25
+    form:
26
+      ilivenear:    J'habite vers
27
+      in:           en
28
+      submit:       Mettre a jour
29
+      errors:
30
+        notown:     Veuillez saisir la ville
31
+        nocountry:  Veuillez saisir le pays
22 32
   
23 33
 connexion:
24 34
   password_lost:    Mot de passe perdu ?

+ 60 - 0
src/Muzich/CoreBundle/Entity/User.php Vedi File

@@ -107,6 +107,18 @@ class User extends BaseUser
107 107
   protected $bad_report_count;
108 108
 
109 109
   /**
110
+   * @ORM\Column(type="text", nullable=true)
111
+   * @ORM\Column(length=256)
112
+   */
113
+  protected $town;
114
+
115
+  /**
116
+   * @ORM\Column(type="text", nullable=true)
117
+   * @ORM\Column(length=128)
118
+   */
119
+  protected $country;
120
+  
121
+  /**
110 122
    * 
111 123
    */
112 124
   public function __construct()
@@ -332,6 +344,26 @@ class User extends BaseUser
332 344
     $this->setBadReportCount($this->getBadReportCount()+1);
333 345
   }
334 346
   
347
+  public function getTown()
348
+  {
349
+    return $this->town;
350
+  }
351
+  
352
+  public function setTown($town)
353
+  {
354
+    $this->town = $town;
355
+  }
356
+  
357
+  public function getCountry()
358
+  {
359
+    return $this->country;
360
+  }
361
+  
362
+  public function setCountry($country)
363
+  {
364
+    $this->country = $country;
365
+  }
366
+  
335 367
   /*
336 368
    * 
337 369
    * 
@@ -342,6 +374,34 @@ class User extends BaseUser
342 374
     return $this->getUsername();
343 375
   }
344 376
   
377
+//  public function getLocalisationExploded()
378
+//  {
379
+//    $town = null;
380
+//    $country = null;
381
+//    if ($this->localisation)
382
+//    {
383
+//      if (($explode = explode(', ', $this->localisation)))
384
+//      {
385
+//        $town = $explode[0];
386
+//        $country = $explode[1];
387
+//      }
388
+//    }
389
+//    return array(
390
+//      'town'    => $town,
391
+//      'country' => $country
392
+//    );
393
+//  }
394
+//  
395
+//  public function setLocalisationExploded($town, $country)
396
+//  {
397
+//    $town = str_replace(', ', '', $town);
398
+//    $town = str_replace(',', '', $town);
399
+//    $country = str_replace(', ', '', $country);
400
+//    $country = str_replace(',', '', $country);
401
+//    
402
+//    $this->localisation = $town. ', ' .$country;
403
+//  }
404
+  
345 405
 //  /**
346 406
 //  * @ORM\prePersist
347 407
 //  */

+ 84 - 0
src/Muzich/CoreBundle/Tests/Controller/UserControllerTest.php Vedi File

@@ -446,4 +446,88 @@ class UserControllerTest extends FunctionalTest
446 446
     
447 447
   }
448 448
   
449
+  public function testUpdateAddress()
450
+  {
451
+    $this->client = self::createClient();
452
+    $this->connectUser('paul', 'toor');
453
+    
454
+    $paul = $this->getUser();
455
+    
456
+    // D'après les fixtures, pas d'adresse pour paul
457
+    $this->assertEquals($paul->getTown(), null);
458
+    $this->assertEquals($paul->getCountry(), null);
459
+    
460
+    $crawler = $this->client->request(
461
+      'POST', 
462
+      $this->generateUrl('update_address', array('token' => $this->getUser()->getPersonalHash())), 
463
+      array(
464
+          'town' => '',
465
+          'country' => ''
466
+      ), 
467
+      array(), 
468
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
469
+    );
470
+    
471
+    $this->isResponseSuccess();
472
+    $response = json_decode($this->client->getResponse()->getContent(), true);
473
+    
474
+    $this->assertEquals($response['status'], 'error');
475
+    $this->assertEquals(count($response['errors']), '2');
476
+    $this->assertEquals($response['errors'], array(
477
+      $this->getContainer()->get('translator')->trans('my_account.address.form.errors.notown', array(), 'userui'),
478
+      $this->getContainer()->get('translator')->trans('my_account.address.form.errors.nocountry', array(), 'userui')
479
+    ));
480
+    $paul = $this->getUser();
481
+    $this->assertEquals($paul->getTown(), null);
482
+    $this->assertEquals($paul->getCountry(), null);
483
+    
484
+    /////
485
+    
486
+    $crawler = $this->client->request(
487
+      'POST', 
488
+      $this->generateUrl('update_address', array('token' => $this->getUser()->getPersonalHash())), 
489
+      array(
490
+          'town' => 'peyruis',
491
+          'country' => ''
492
+      ), 
493
+      array(), 
494
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
495
+    );
496
+    
497
+    $this->isResponseSuccess();
498
+    $response = json_decode($this->client->getResponse()->getContent(), true);
499
+    
500
+    $this->assertEquals($response['status'], 'error');
501
+    $this->assertEquals(count($response['errors']), '1');
502
+    $this->assertEquals($response['errors'], array(
503
+      $this->getContainer()->get('translator')->trans('my_account.address.form.errors.nocountry', array(), 'userui')
504
+    ));
505
+    $paul = $this->getUser();
506
+    $this->assertEquals($paul->getTown(), null);
507
+    $this->assertEquals($paul->getCountry(), null);
508
+    
509
+    /////
510
+    
511
+    $crawler = $this->client->request(
512
+      'POST', 
513
+      $this->generateUrl('update_address', array('token' => $this->getUser()->getPersonalHash())), 
514
+      array(
515
+          'town' => 'peyruis',
516
+          'country' => 'france'
517
+      ), 
518
+      array(), 
519
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
520
+    );
521
+    
522
+    $this->outputDebug();
523
+    $this->isResponseSuccess();
524
+    $response = json_decode($this->client->getResponse()->getContent(), true);
525
+    
526
+    $paul = $this->getUser();
527
+    $this->assertEquals($response['status'], 'success');
528
+    $this->assertEquals($paul->getTown(), 'peyruis');
529
+    $this->assertEquals($paul->getCountry(), 'france');
530
+    
531
+  }
532
+  
449 533
 }

+ 60 - 0
src/Muzich/UserBundle/Controller/UserController.php Vedi File

@@ -506,5 +506,65 @@ class UserController extends Controller
506 506
     $this->setFlash('success', 'user.changeemail.success');
507 507
     return new RedirectResponse($this->generateUrl('my_account'));
508 508
   }
509
+  
510
+  /**
511
+   *
512
+   * @param string $town
513
+   * @param string $country
514
+   * @param string $token
515
+   * @return Response 
516
+   */
517
+  public function updateAddressAction($token)
518
+  {
519
+    if (($response = $this->mustBeConnected(true)))
520
+    {
521
+      return $response;
522
+    }
523
+    
524
+    /**
525
+     * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
526
+     * Docrine le voit si on faire une requete directe.
527
+     */
528
+    $user = $this->getUser();
529
+    if ($this->container->getParameter('env') == 'test')
530
+    {
531
+      $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
532
+        $this->container->get('security.context')->getToken()->getUser()->getId(),
533
+        array()
534
+      )->getSingleResult();
535
+    }
536
+    
537
+    $errors = array();
538
+    if ($user->getPersonalHash() != $token)
539
+    {
540
+      $errors[] = 'NotAllowed';
541
+    }
542
+    
543
+    if (!trim($this->getRequest()->request->get('town')))
544
+    {
545
+      $errors[] = $this->trans('my_account.address.form.errors.notown', array(), 'userui');
546
+    }
547
+    if (!trim($this->getRequest()->request->get('country')))
548
+    {
549
+      $errors[] = $this->trans('my_account.address.form.errors.nocountry', array(), 'userui');
550
+    }
551
+    
552
+    if (count($errors))
553
+    {
554
+      return $this->jsonResponse(array(
555
+        'status' => 'error',
556
+        'errors' => $errors
557
+      ));
558
+    }
559
+    
560
+    $user->setTown(trim($this->getRequest()->request->get('town')));
561
+    $user->setCountry(trim($this->getRequest()->request->get('country')));
562
+    $this->getDoctrine()->getEntityManager()->persist($user);
563
+    $this->getDoctrine()->getEntityManager()->flush();
564
+    
565
+    return $this->jsonResponse(array(
566
+      'status' => 'success'
567
+    ));
568
+  }
509 569
     
510 570
 }

+ 5 - 1
src/Muzich/UserBundle/Resources/config/routing.yml Vedi File

@@ -28,4 +28,8 @@ change_email_request:
28 28
     
29 29
 change_email_confirm:
30 30
   pattern: /account/change-email/confirm/{token}
31
-  defaults: { _controller: MuzichUserBundle:User:changeEmailConfirm }
31
+  defaults: { _controller: MuzichUserBundle:User:changeEmailConfirm }
32
+  
33
+update_address:
34
+  pattern: /ajax/update-address/{token}
35
+  defaults: { _controller: MuzichUserBundle:User:updateAddress }

+ 15 - 0
src/Muzich/UserBundle/Resources/views/User/account.html.twig Vedi File

@@ -45,5 +45,20 @@
45 45
 
46 46
     <input type="submit" class="button" />
47 47
   </form>
48
+  
49
+  <h3>{{ 'my_account.address.title'|trans({}, 'userui') }}</h3>
50
+  
51
+  <p>{{ 'my_account.address.description'|trans({}, 'userui') }}</p>
52
+  
53
+  <form id="address_update" action="{{ path('update_address', {"token":app.user.getPersonalHash}) }}" method="post">
54
+    <p>
55
+      {{ 'my_account.address.form.ilivenear'|trans({}, 'userui') }}
56
+      <input type="text" name="town" class="intext" value="{{ app.user.town }}" />
57
+      {{ 'my_account.address.form.in'|trans({}, 'userui') }}
58
+      <input type="text" name="country" class="intext" value="{{ app.user.country }}" />
59
+      <input type="submit" value="{{ 'my_account.address.form.submit'|trans({}, 'userui') }}" class="button address_submit" />
60
+      <img class="loader" style="display: none;" src="{{ asset('/bundles/muzichcore/img/ajax-loader.gif') }}" alt="loading"/>
61
+    </p>
62
+  </form>
48 63
     
49 64
 {% endblock %}

+ 3 - 0
src/Muzich/UserBundle/Resources/views/layout.html.twig Vedi File

@@ -3,6 +3,9 @@
3 3
 {% block css %}
4 4
 	<link href="{{ asset('bundles/muzichuser/css/user.css') }}" rel="stylesheet" media="screen" type="text/css" />
5 5
 {% endblock %}
6
+{% block js %}
7
+	<script src="{{ asset('bundles/muzichuser/js/user.js') }}" type="text/javascript"></script>
8
+{% endblock %}
6 9
 
7 10
 {% block main_content %}
8 11
   

+ 13 - 0
web/bundles/muzichcore/css/main.css Vedi File

@@ -1038,4 +1038,17 @@ div#added_element_to_group
1038 1038
   padding: 20px;
1039 1039
   font-size: 115%;
1040 1040
   font-weight: bold;
1041
+}
1042
+
1043
+input.intext
1044
+{
1045
+  border: none;
1046
+  border-bottom-color: #000;
1047
+  border-bottom-style: dashed;
1048
+  border-bottom-width: 1px;
1049
+  width: 110px;
1050
+  text-align: center;
1051
+  margin-left: 15px;
1052
+  margin-right: 15px;
1053
+  font-weight: bold;
1041 1054
 }

+ 29 - 0
web/bundles/muzichuser/js/user.js Vedi File

@@ -0,0 +1,29 @@
1
+/*  */
2
+
3
+$(document).ready(function(){
4
+    
5
+  $('form#address_update input[type="submit"]').click(function(){
6
+    $('form#address_update img.loader').show();
7
+  });
8
+    
9
+  $('form#address_update').ajaxForm(function(response){
10
+      
11
+      $('form#address_update img.loader').hide();
12
+      form = $('form#address_update');
13
+      
14
+      form.find('ul.error_list').remove();
15
+      if (response.status == 'error')
16
+      {
17
+        ul_errors = $('<ul>').addClass('error_list');
18
+
19
+        for (i in response.errors)
20
+        {
21
+          ul_errors.append($('<li>').append(response.errors[i]));
22
+        }
23
+
24
+        form.prepend(ul_errors);
25
+      }
26
+      
27
+  });
28
+   
29
+});