File "controllers.js"

Full Path: /srv/www/www.cadoro.it/app/src/js/auth/controllers.js
File size: 9.79 KB
MIME-type: text/plain
Charset: utf-8

angular.module('app.auth.controllers', [
  'app.config',
  'ngCordova.plugins.nativeStorage',
  'ionic'
])

.controller('AuthWelcomeController', function(
    $rootScope,
    $scope,
    $http,
    $ionicHistory,
    $ionicLoading,
    $ionicPlatform,
    $ionicPopup,
    $location,
    $timeout,
    //$cordovaGoogleAnalytics,
    //localStorageService,
    $cordovaNativeStorage,
    API_END_POINT
  ) {
  $scope.$on('$ionicView.beforeEnter', function () {
    $scope.user = {};
    $ionicPlatform.ready(function() {
      //$timeout(function () {
        //var user = localStorageService.get('user');
        var user = false;
        console.log("CA27_get");
        $cordovaNativeStorage.getItem('user').then(function (value) {
          console.log("CA27_get OK");
          user = value;
          if (user) {
            $ionicHistory.nextViewOptions({
              disableAnimate: true,
              disableBack: true
            });
            $rootScope.user = user;
            $location.path('/app/home');
          } else {
            //$cordovaGoogleAnalytics.trackView('auth.welcome');
            window.FirebasePlugin.setScreenName("auth.welcome");
            window.FirebasePlugin.logEvent("page_view", {page: "auth.welcome"});
            if (navigator.splashscreen) {
              navigator.splashscreen.hide();
            }
          }
        }, function(error) {
          console.log("CA27_get ERROR");
          console.log(JSON.stringify(error));
          //$cordovaGoogleAnalytics.trackView('auth.welcome');
          window.FirebasePlugin.setScreenName("auth.welcome");
          window.FirebasePlugin.logEvent("page_view", {page: "auth.welcome"});
          if (navigator.splashscreen) {
            navigator.splashscreen.hide();
          }
        });
      //}, 50);
    });
  });
  $scope.$on('$ionicView.enter', function () {
    /**/
    $timeout(function () {
      if (navigator.splashscreen) {
        navigator.splashscreen.hide();
      }
    }, 1000);
    /**/
  });
  $scope.doLogin = function ($event, user) {
    $event.preventDefault();
    $event.stopPropagation();
    $ionicLoading.show({
      template: 'Attendere, invio dati in corso...'
    });
    $http({
        url: API_END_POINT + '/login',
        method: "POST",
        timeout: 10000,
        data: JSON.stringify(user),
        headers: {
          'Content-Type': 'application/json'
        }
      })
      .success(function(data, status, headers, config) {
        $ionicLoading.hide();
        if (data && data.user) {
          $rootScope.user = data.user;
          //localStorageService.set('user', data.user);
          console.log("CA83_set");
          $cordovaNativeStorage.setItem('user', data.user).then(function (value) {
            console.log("CA83_set OK");
          }, function (error) {
            console.log("CA83_set ERROR");
            console.log(JSON.stringify(error));
          });
          $ionicHistory.nextViewOptions({
            disableAnimate: false,
            disableBack: true
          });
          $location.path('/app/home');
        } else if (data && data.error && data.error == 2) {
          user.password = '';
          $ionicPopup.alert({
            title: 'ERRORE',
            template: '<p class="text-center">Non hai confermato la tua registrazione. Controlla la tua casella di posta e clicca nella mail che ti abbiamo appena inviato per confermare la registrazione (verifica anche nella cartella di spam).</p>',
          });
        } else {
          user.password = '';
          $ionicPopup.alert({
            title: 'ERRORE',
            template: '<p class="text-center">Autenticazione fallita, si prega di riprovare.</p>'
          });
        }
      })
      .error(function(data, status, headers, config) {
        $ionicLoading.hide();
        $ionicPopup.alert({
          title: 'ERRORE',
          template: '<p class="text-center">Il server non è raggiungibile, verifica la tua connessione di rete prima di riprovare!</p>'
        });
      });
  };
  $scope.doPassword = function () {
    var myPopup = $ionicPopup.show({
      template: '<input type="text" ng-model="user.email">',
      title: 'Recupero password',
      subTitle: 'Inserisci la tua email oppure il tuo numero di tessera e ti invieremo un\'email per reimpostae la tua password:',
      scope: $scope,
      buttons: [
        { text: 'Annulla' },
        {
          text: '<b>Invia</b>',
          type: 'button-positive',
          onTap: function(e) {
            if (!$scope.user.email) {
              e.preventDefault();
            } else {
              return $scope.user.email;
            }
          }
        }
      ]
    }).then(function(res) {
      if (res) {
        $ionicLoading.show({
          template: 'Attendere, invio dati in corso...'
        });
        $http({
            url: API_END_POINT + '/password',
            method: "POST",
            data: JSON.stringify({email: res}),
            timeout: 10000,
            headers: {
              'Content-Type': 'application/json'
            }
          })
          .success(function(data, status, headers, config) {
            $ionicLoading.hide();
            if (data.user) {
              $ionicPopup.alert({
                title: 'CONFERMA',
                template: '<p class="text-center">Ti abbiamo inviato un\'email con le istruzioni per reimpostae la tua password!</p>'
              });
            } else {
              $ionicPopup.alert({
                title: 'CONFERMA',
                template: '<p class="text-center">L\'indirizzo email o il numero di tessera inserito non risultano registrati!</p>'
              });
            }
          })
          .error(function(data, status, headers, config) {
            $ionicLoading.hide();
            $ionicPopup.alert({
              title: 'ERRORE',
              template: '<p class="text-center">Il server non è raggiungibile, verifica la tua connessione di rete prima di riprovare!</p>'
            });
          });
      }
    });
  };
  $scope.openBrowser = function ($event, href) {
    $event.preventDefault();
    $event.stopPropagation();
    if (window.cordova && window.cordova.InAppBrowser) {
      window.cordova.InAppBrowser.open(href, '_blank', 'closebuttoncaption=Chiudi');
    } else {
      window.open(href, '_system');
    }
  };
})

.controller('AuthWelcomeRegistrazioneController', function(
    $rootScope,
    $scope,
    $http,
    $ionicHistory,
    $ionicLoading,
    $ionicPlatform,
    $ionicPopup,
    $location,
    $timeout,
    //$cordovaGoogleAnalytics,
    //localStorageService,
    $cordovaNativeStorage,
    API_END_POINT
  ) {
  $scope.$on('$ionicView.enter', function () {
    $scope.user = {};
    //$cordovaGoogleAnalytics.trackView('auth.welcome_registrazione');
    window.FirebasePlugin.setScreenName("auth.welcome_registrazione");
    window.FirebasePlugin.logEvent("page_view", {page: "auth.welcome_registrazione"});
  });
  $scope.registration = function (user) {
    if (!user.firstname ||
        !user.lastname ||
        !user.card ||
        !user.email ||
        !user.email2 ||
        !user.password ||
        !user.password2 ||
        !user.privacy) {
      $ionicPopup.alert({
        title: 'ATTENZIONE',
        template: '<p class="text-center">Tutti i campi sono obbligatori, si prega di riprovare.</p>'
      });
    } else if (user.email != user.email2) {
      $ionicPopup.alert({
        title: 'ATTENZIONE',
        template: '<p class="text-center">Gli indirizzi email inseriti non corrispondono!</p>'
      });
    } else if (user.password != user.password2) {
      $ionicPopup.alert({
        title: 'ATTENZIONE',
        template: '<p class="text-center">Le password inserite non corrispondono!</p>'
      });
    } else {
      $ionicLoading.show({
        template: 'Attendere, invio dati in corso...'
      });
      $http({
          url: API_END_POINT + '/registrazione',
          method: "POST",
          timeout: 10000,
          data: JSON.stringify(user),
          headers: {
            'Content-Type': 'application/json'
          }
        })
        .success(function(data, status, headers, config) {
          $ionicLoading.hide();
          if (data && data.user) {
            $ionicPopup.alert({
              title: 'CONFERMA',
              template: '<p class="text-center">Ti abbiamo inviato un\'email per confermare la tua registrazione!</p>'
            });
            $ionicHistory.goBack(-1);
          } else if (data.error && data.error == 'email') {
            $ionicPopup.alert({
              title: 'ERRORE',
              template: '<p class="text-center">Impossibile procedere con la registrazione: l\'indirizzo email specificato risulta già registrato.</p>'
            });
          } else if (data.error && data.error == 'card') {
            $ionicPopup.alert({
              title: 'ERRORE',
              template: '<p class="text-center">Impossibile procedere con la registrazione: il numero tessera specificato risulta già registrato.</p>'
            });
          } else if (data.error && data.error == 'check') {
            $ionicPopup.alert({
              title: 'ERRORE',
              template: '<p class="text-center">Impossibile procedere con la registrazione: il nome e cognome specificati non corrispondono a quelli della tessera.</p>'
            });
          } else {
            $ionicPopup.alert({
              title: 'ERRORE',
              template: '<p class="text-center">Impossibile procedere con la registrazione.</p>'
            });
          }
        })
        .error(function(data, status, headers, config) {
          $ionicLoading.hide();
          $ionicPopup.alert({
            title: 'ERRORE',
            template: '<p class="text-center">Il server non è raggiungibile, verifica la tua connessione di rete prima di riprovare!</p>'
          });
        });
    }
  };
  $scope.back = function () {
    $ionicHistory.goBack(-1);
  };
});