<commit_before>'use strict';

/**
 * @ngdoc function
 * @name Pear2Pear.controller:ThanksappCtrl
 * @description
 * # Thanksapp Ctrl
 * Controller of the Pear2Pear
 */

angular.module('Pear2Pear')
  .config(['$routeProvider', function($routeProvider) {
    $routeProvider
      .when('/thanksapp/:mode/:id', {
        templateUrl: 'views/thanksapp/thanksapp.html',
        controller: 'ThanksappCtrl'
      })
      .when('/thanksapp-register/:id', {
        templateUrl: 'views/thanksapp/register.html',
        controller: 'ThanksappCtrl'
      });
  }])
  .controller('ThanksappCtrl', ['$scope', '$location', '$route', function($scope, $location, $route){
    var apply = function () {
      var p = $scope.$$phase;
      if (p !== '$digest' && p !== '$apply') {
        $scope.$apply();
      }
    };

    if (!location.origin) {
      location.origin = location.protocol + '//' + location.host;
    }


    $scope.$parent.hideNavigation = true;
    $scope.mode = $route.current.params['mode'];
    $scope.userId = $route.current.params['id'];

    $scope.init = function () {
      // following if avoids concurrency control error in wave
      if (window.WaveJS.model) {
        window.WaveJS.closeModel(
          window.configTimelineTests.thanksappWaveId);
        window.WaveJS.model = null;
      }

      window.WaveJS.openModel(
        window.configTimelineTests.thanksappWaveId,
        function (model) {
          window.WaveJS.model = model;
          if (typeof model.root.get($scope.userId) == 'undefined'){
            var list = model.createList();
            list = model.root.put($scope.userId,list); // list is attached to the sub map: root->map->list
          }
          model.root.get($scope.userId).registerEventHandler(
            WaveJS.events.ITEM_ADDED, function (item) {
              var index = -1;
              var i = 0;
              while ( index == -1 && i < model.root.get($scope.userId).values.length) {
                if (model.root.get($scope.userId).values[i].getValue() == item.getValue())
                  index = i;
                i ++;
              }
              $scope.thanks[index] = JSON.parse(item.getValue());
              apply();
            });
          window.WaveJS.model.root.get($scope.userId).registerEventHandler(
             WaveJS.events.ITEM_REMOVED, function (item) {
              var index = window.WaveJS.model.root.get($scope.userId).values.indexOf(item);
              $scope.thanks.splice(index, 1);
              apply();
            });
          $scope.thanks = [];
          for (var i = 0; i < model.root.get($scope.userId).values.length; i++) {
            $scope.thanks[i] = JSON.parse(model.root.get($scope.userId).values[i].getValue());
          }
          apply();

        }, function (error) {
          window.alert('Error accessing the collaborative list ' + error);
        });
    };

    $scope.clear = function () {
      var wjsList = window.WaveJS.model.root.get($scope.userId);
      for (var i = wjsList.size()-1; i >= 0 ; i--) {
        wjsList.remove(i);
      }
      $scope.testIndex = 0;
    };

    $scope.isControl = function () {
      return $location.url().indexOf('control') > -1;
    };

    $scope.init();

    $scope.thanksForm = {};

    $scope.customThanks = function(name, text) {
      var s = JSON.stringify({
        'name': name,
        'text': text,
        'photo': 'images/profile1.jpg'
      });
      var str = window.WaveJS.model.createString(s);
      var reg = new RegExp('(\<@)(\\w+)', 'g');
      var rr;
      var receiver;
      while ((rr = reg.exec(text)) !== null){
      var str = window.WaveJS.model.createString(s);
        console.log(rr);
        receiver =  rr[2];
        console.log(receiver);
        if (typeof window.WaveJS.model.root.get(receiver) === 'undefined'){
          var list =  window.WaveJS.model.createList();
          var map = window.WaveJS.model.root;
          list = map.put(receiver,list);
//          list.add(str);
        }
        str = window.WaveJS.model.root.get(receiver).add(str);
      }

     var emails = $scope.thanksappMails(text,name);
     console.log(emails);
    };

    $scope.newWaveId = function () {
      window.alert(window.WaveJS.createModel());
    };

    $scope.profile = function (name){
      $location.url('thanksapp/view/'+name);
    };

    //display the add thanks form
    $scope.formDisp = false;
    $scope.switchForm = function(){
      $scope.formDisp = ! $scope.formDisp;
    };

    $scope.open = function (size) {
      $location.url('/thanksapp-register/' + $scope.userId);
    };

    $scope.thanksappify = function (string){
      var re = new RegExp('(\<@)(\\w+)', 'g');
      return string.replace(re, '<a href= "' + location.origin + '/#/thanksapp/view/$2"><strong><i class="icon-thanksapplogo"></i>$2</strong></a>');
    };

    $scope.thanksappMails = function (string, sender){
      var rt = [];
      if (string){
        var re = new RegExp('(\<@)(\\w+)', 'g');
        var re2 = new RegExp('(\<@)(\\w+)', 'g');
        var r;
        while ((r = re2.exec(string))!== null) {
          var receiver = r[2];
          var salut = 'Hola ' + receiver + ',<br/><br/>';
          var body = '';
          if (sender){
            body += sender + ' te ha enviado un agracecimiento';
          }
          else {
            body += 'Te han enviado un agradecimiento';
          }

          var s = string
            .replace(re, '<a href= "' + location.origin + '/#/thanksapp/view/$2"><strong>$1$2</strong></a>');
          s = 'Subject: ' + body + '\n\n' + salut + body + ': "<i>' + s + '</i>".<br/><br/>puedes ver tus agradecimientos y activar tu cuenta en <a href= "' + location.origin + '/#/thanksapp/self/' + receiver + '">tu Thanksapp</a>.';
          rt.push(s);
        }
      }
      return rt;
    };

    $scope.ok = function () {
      $location.url('/thanksapp/commingsoon/' + $route.current.params['id']);
      _paq.push(['trackEvent', 'MoreInfo', $scope.thanksForm.email]);
      _paq.push(['trackPageView']);

    };

    $scope.cancel = function () {
      $location.url('/thanksapp/self/' + $route.current.params['id']);
    };

    // $scope.control = {};

  }]);
<commit_msg>Added tip to send the email<commit_after>'use strict';

/**
 * @ngdoc function
 * @name Pear2Pear.controller:ThanksappCtrl
 * @description
 * # Thanksapp Ctrl
 * Controller of the Pear2Pear
 */

angular.module('Pear2Pear')
  .config(['$routeProvider', function($routeProvider) {
    $routeProvider
      .when('/thanksapp/:mode/:id', {
        templateUrl: 'views/thanksapp/thanksapp.html',
        controller: 'ThanksappCtrl'
      })
      .when('/thanksapp-register/:id', {
        templateUrl: 'views/thanksapp/register.html',
        controller: 'ThanksappCtrl'
      });
  }])
  .controller('ThanksappCtrl', ['$scope', '$location', '$route', function($scope, $location, $route){
    var apply = function () {
      var p = $scope.$$phase;
      if (p !== '$digest' && p !== '$apply') {
        $scope.$apply();
      }
    };

    if (!location.origin) {
      location.origin = location.protocol + '//' + location.host;
    }


    $scope.$parent.hideNavigation = true;
    $scope.mode = $route.current.params['mode'];
    $scope.userId = $route.current.params['id'];

    $scope.init = function () {
      // following if avoids concurrency control error in wave
      if (window.WaveJS.model) {
        window.WaveJS.closeModel(
          window.configTimelineTests.thanksappWaveId);
        window.WaveJS.model = null;
      }

      window.WaveJS.openModel(
        window.configTimelineTests.thanksappWaveId,
        function (model) {
          window.WaveJS.model = model;
          if (typeof model.root.get($scope.userId) == 'undefined'){
            var list = model.createList();
            list = model.root.put($scope.userId,list); // list is attached to the sub map: root->map->list
          }
          model.root.get($scope.userId).registerEventHandler(
            WaveJS.events.ITEM_ADDED, function (item) {
              var index = -1;
              var i = 0;
              while ( index == -1 && i < model.root.get($scope.userId).values.length) {
                if (model.root.get($scope.userId).values[i].getValue() == item.getValue())
                  index = i;
                i ++;
              }
              $scope.thanks[index] = JSON.parse(item.getValue());
              apply();
            });
          window.WaveJS.model.root.get($scope.userId).registerEventHandler(
             WaveJS.events.ITEM_REMOVED, function (item) {
              var index = window.WaveJS.model.root.get($scope.userId).values.indexOf(item);
              $scope.thanks.splice(index, 1);
              apply();
            });
          $scope.thanks = [];
          for (var i = 0; i < model.root.get($scope.userId).values.length; i++) {
            $scope.thanks[i] = JSON.parse(model.root.get($scope.userId).values[i].getValue());
          }
          apply();

        }, function (error) {
          window.alert('Error accessing the collaborative list ' + error);
        });
    };

    $scope.clear = function () {
      var wjsList = window.WaveJS.model.root.get($scope.userId);
      for (var i = wjsList.size()-1; i >= 0 ; i--) {
        wjsList.remove(i);
      }
      $scope.testIndex = 0;
    };

    $scope.isControl = function () {
      return $location.url().indexOf('control') > -1;
    };

    $scope.init();

    $scope.thanksForm = {};

    $scope.customThanks = function(name, text) {
      var s = JSON.stringify({
        'name': name,
        'text': text,
        'photo': 'images/profile1.jpg'
      });
      var str = window.WaveJS.model.createString(s);
      var reg = new RegExp('(\<@)(\\w+)', 'g');
      var rr;
      var receiver;
      while ((rr = reg.exec(text)) !== null){
      var str = window.WaveJS.model.createString(s);
        console.log(rr);
        receiver =  rr[2];
        console.log(receiver);
        if (typeof window.WaveJS.model.root.get(receiver) === 'undefined'){
          var list =  window.WaveJS.model.createList();
          var map = window.WaveJS.model.root;
          list = map.put(receiver,list);
//          list.add(str);
        }
        str = window.WaveJS.model.root.get(receiver).add(str);
      }

      var emails = $scope.thanksappMails(text, name);
      console.log(emails);
      for (var e in emails) {
        window.alert('to send the email:\n\n1.- Write in a file called <filename> "To: <EMAIL>"\n2.-Copy the following text: \n' + emails[e] + '\n3.- run the following comand:\n  (echo "Content-Type: text/html"; echo "MIME-Version: 1.0"; cat <filename>) | /usr/sbin/sendmail -t');
      }
    };

    $scope.newWaveId = function () {
      window.alert(window.WaveJS.createModel());
    };

    $scope.profile = function (name){
      $location.url('thanksapp/view/'+name);
    };

    //display the add thanks form
    $scope.formDisp = false;
    $scope.switchForm = function(){
      $scope.formDisp = ! $scope.formDisp;
    };

    $scope.open = function (size) {
      $location.url('/thanksapp-register/' + $scope.userId);
    };

    $scope.thanksappify = function (string){
      var re = new RegExp('(\<@)(\\w+)', 'g');
      return string.replace(re, '<a href= "' + location.origin + '/#/thanksapp/view/$2"><strong><i class="icon-thanksapplogo"></i>$2</strong></a>');
    };

    $scope.thanksappMails = function (string, sender){
      var rt = [];
      if (string){
        var re = new RegExp('(\<@)(\\w+)', 'g');
        var re2 = new RegExp('(\<@)(\\w+)', 'g');
        var r;
        while ((r = re2.exec(string))!== null) {
          var receiver = r[2];
          var salut = 'Hola ' + receiver + ',<br/><br/>';
          var body = '';
          if (sender){
            body += sender + ' te ha enviado un agracecimiento';
          }
          else {
            body += 'Te han enviado un agradecimiento';
          }

          var s = string
            .replace(re, '<a href= "' + location.origin + '/#/thanksapp/view/$2"><strong>$1$2</strong></a>');
          s = 'Subject: ' + body + '\n\n' + salut + body + ': "<i>' + s + '</i>".<br/><br/>puedes ver tus agradecimientos y activar tu cuenta en <a href= "' + location.origin + '/#/thanksapp/self/' + receiver + '">tu Thanksapp</a>.';
          rt.push(s);
        }
      }
      return rt;
    };

    $scope.ok = function () {
      $location.url('/thanksapp/commingsoon/' + $route.current.params['id']);
      _paq.push(['trackEvent', 'MoreInfo', $scope.thanksForm.email]);
      _paq.push(['trackPageView']);

    };

    $scope.cancel = function () {
      $location.url('/thanksapp/self/' + $route.current.params['id']);
    };

    // $scope.control = {};

  }]);

{'fraction_non_alphanumeric': 0.17909631019387118, 'fraction_numerical': 0.0038305190744215133, 'mean_word_length': 2.114925736547358, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4921294', 'n_tokens_mistral': 4141, 'n_tokens_neox': 3989, 'n_words': 765}