1console.log("formcontroller.js loaded"); 2 3angular.module('chilApp') 4.config(function ($interpolateProvider) { 5 $interpolateProvider.startSymbol('{[{').endSymbol('}]}'); 6}) 7.factory('postdetailApi', function($http, $q){ 8 9 return { 10 browse: function(postId) { 11 return $http.get('/api/post/' + postId ) 12 .then(function(r){ 13 return r.data; 14 }); 15 16 } 17 } 18}) 19.directive('checklist', function() { 20 return { 21 restrict: "A", 22 require: "?^ngController", 23 terminal: false, 24 scope: { 25 list: '@checklist', 26 value: '@' 27 }, 28 link: function(scope, elem, attrs, controller) { 29 30 31 var handler = function(setup) { 32 var checked = elem.prop('checked'); 33 var target = null; 34 35 if (controller.model != undefined) { 36 if (controller.model[scope.list] === undefined || 37 controller.model[scope.list] === null || 38 controller.model[scope.list] === '' 39 ) 40 { 41 controller.model[scope.list] = []; 42 } 43 target = controller.model[scope.list]; 44 } else { 45 if (controller[scope.list] === undefined || 46 controller[scope.list] === null) 47 { 48 controller[scope.list] = []; 49 } 50 target = controller[scope.list]; 51 } 52 53 var index = target.indexOf(scope.value); 54 55 56 if (checked && index == -1) { 57 if (setup) { 58 elem.prop('checked', false); 59 } else { 60 target.push(scope.value); 61 } 62 } else if (!checked && index != -1) { 63 if (setup) { 64 elem.prop('checked', true); 65 } 66 else { 67 target.splice(index, 1); 68 } 69 } 70 71 }; 72 73 var setupHandler = handler.bind(null, true); 74 var changeHandler = handler.bind(null, false); 75 76 elem.bind('change', function() { 77 scope.$apply(changeHandler); 78 }); 79 80 scope.$watch('list', setupHandler, true); 81 } 82 }; 83}) 84.controller("FormController", function($scope, $window, $sce, $timeout, siteId, postdetailApi){ 85 var scope = this; 86 var postId = ''; 87 // var commentCreatorId = _userId; 88 89 var commentCreatorId = '[email protected]'; 90 91 scope.post = ''; 92 scope.body = ''; 93 scope.comments = []; 94 scope.runningTime = '00:00'; 95 scope.totalresult = 0; 96 scope.result = 0; 97 98 scope.first = true; 99 scope.showform = false; 100 scope.after = false; 101 102 //scope.zoneid = _zoneId; 103 scope.zoneid = 0; 104 scope.isSending = false; 105 106 107 if (typeof _postId !== 'undefined') {postId = _postId;} 108 109 scope.init = function () { 110 scope.load(postId); 111 112 }; 113 114 scope.load = function(postId){ 115 return postdetailApi.browse(postId) 116 .then(function(data){ 117 118 if (data.post != null && 119 data.post.body !== undefined && data.post.body !== null ){ 120 scope.post = data.post; 121 scope.body = data.post.body.replace(/<[^>]+>/gm, ''); 122 123 scope.body = angular.fromJson(scope.body); 124 // console.log(scope.body); 125 angular.forEach(data.post.comments, function(d){ 126 if(d.creator.id == commentCreatorId){ 127 scope.comments.push(d); 128 } 129 }); 130 131 scope.comments = scope.comments.reverse(); 132 } 133 134 scope.createModel(scope.body); 135 // console.log("CUESTIONARIO CARGADO!!!"); 136 return data; 137 }); 138 } 139 140 scope.addOption = function(variable,option) { 141 console.log(variable); 142 console.log(option); 143 } 144 145 // Datos falsos para las ocasiones en que no se usane en los cuestionarios 146 scope.fakeName = 'Anonimous'; 147 scope.fakeTfn = '---------'; 148 scope.fakeEmail = '[email protected]'; 149 150 scope.send = function() { 151 152 scope.isSending = true; 153 154 console.log(scope); 155 156 var lang = scope.post.lang; 157 158 if (!scope.updateModel(scope.body)) { 159 console.log(scope.model); 160 if (lang === 'CA') { 161 $window.alert("Ha d'omplir els camps obligatoris marcats amb * vermell."); 162 } else { 163 $window.alert("Debe rellenar los campos obligatorios marcados con * rojo."); 164 } 165 scope.isSending = false;
166 return; 167 } 168 169 if (scope.model['tfn'] !== undefined && !scope.checkTfn(scope.model['tfn']) ) { 170 if (lang === 'CA') { 171 $window.alert("Vostè ha d'introduir un telèfon và lid."); 172 } else { 173 $window.alert("Debe introducir un teléfono válido."); 174 } 175 scope.isSending = false; 176 return; 177 } 178 if (scope.model['email'] !== undefined && !scope.checkEmail(scope.model['email']) ) { 179 if (lang === 'CA') { 180 $window.alert("Vostè ha d'introduir un correu electrònic và lid."); 181 } else { 182 $window.alert("Debe introducir un email válido."); 183 } 184 scope.isSending = false; 185 return; 186 } 187 if (scope.model['nombre'] !== undefined && 188 !scope.checkName(scope.model['nombre'], scope.model['apellidos']) ) { 189 if (lang === 'CA') { 190 $window.alert("Vostè ha d'introduir nom i cognoms và lids."); 191 } else { 192 $window.alert("Debe introducir nombre y apellidos válidos."); 193 } 194 scope.isSending = false; 195 return; 196 } 197 198 var tfn = scope.model['tfn'] !== undefined ? 199 scope.cleanTfn(scope.model['tfn']) : 200 scope.fakeTfn; 201 202 var email = scope.model['email'] !== undefined ? 203 scope.cleanEmail(scope.model['email']) : 204 scope.fakeEmail; 205 206 var nombre = scope.model['nombre'] !== undefined ? 207 scope.cleanName(scope.model['nombre'], scope.model['apellidos']) : 208 scope.fakeName; 209 210 var content = JSON.stringify(scope.model); 211 212 console.log("NOMBRE: " + nombre) 213 214 scope.checkDBTfn(tfn) 215 .then( 216 (count) => { 217 console.log("TFN EN LA BD: " + count.toString()) 218 if (count >0) { 219 if (lang === 'CA') { 220 $window.alert("Aquest telèfon ha estat utilitzat per a participar."); 221 } else { 222 $window.alert("Este teléfono ha sido utilizado para participar."); 223 } 224 scope.isSending = false; 225 return new Promise( (resolve, reject) => reject('Telefono ya utilizado') ); 226 } 227 else 228 return scope.checkDBEmail(email) 229 }) 230 .then( 231 (count) => { 232 console.log("EMAIL EN LA BD: " + count.toString()) 233 if (count >0) { 234 if (lang === 'CA') { 235 $window.alert("Aquest email ha estat utilitzat per a participar."); 236 } else { 237 $window.alert("Este email ha sido utilizado para participar."); 238 } 239 scope.isSending = false; 240 return new Promise( (resolve, reject) => reject('Email ya utilizado')); 241 } 242 else { 243 console.log(content); 244 return scope.saveForm(email, nombre, tfn, content); 245 } 246 }) 247 .then ( 248 (success) => { 249 scope.isSending = false; 250 if (success) { 251 $window.alert(scope.body.AcceptMessage); 252 location.href="https://launio.org"; 253 } else 254 if (lang === 'CA') { 255 $window.alert("Hi ha hagut un error, provi més tard."); 256 } else { 257 $window.alert("Ha habido un error, pruebe mas tarde."); 258 } 259 }); 260 261 }; 262 263 scope.checkDBTfn = function(tfn) { 264 return new Promise ( 265 function (resolve, reject) { 266 if (tfn === scope.fakeTfn) { 267 resolve(0); 268 } 269 else { 270 $.ajax({ 271 /* url: "https://api.chil.me/api/comment/check", */ 272 273 url: "/api/comment/check", 274 275 type: "POST", 276 data: { 277 "ObjectId": scope.post.id, 278 "Type" : 4, 279 "SubType" : 1, 280 "Tfn": tfn 281 }, 282 dataType:'json', 283 success: function (response) { 284 //console.log(response) 285 resolve(response.count); 286 }, 287 error: function(error){ 288 console.log("Something went wrong", error); 289 reject("checkDBTfn. Petición rechazada"); 290 } 291 }); 292 } 293 }); 294 295 296 //console.log("CUESTIONARIO POSTEADO!!!"); 297 //$window.alert("Please enter your name!"); 298 299 }; 300 301 scope.checkDBEmail = function(email) { 302 return new Promise ( 303 function (resolve, reject) { 304 if (email === scope.fakeEmail) { 305 resolve(0); 306 } else { 307 $.ajax({ 308 /* url: "https://api.chil.me/api/comment/check", */ 309 310 url: "/api/comment/check", 311 type: "POST", 312 data: { 313 "ObjectId": scope.post.id, 314 "Type" : 4, 315 "SubType" : 1, 316 "Email": email 317 }, 318 dataType:'json', 319 success: function (response) { 320 //console.log(response) 321 resolve(response.count); 322 }, 323 error: function(error){ 324 console.log("Something went wrong", error); 325 reject("checkDBEmail. Petición rechazada"); 326 } 327 }); 328 } 329 }); 330 331 332 //console.log("CUESTIONARIO POSTEADO!!!"); 333 //$window.alert("Please enter your name!"); 334 335 }; 336 337 scope.saveForm = function(email, nombre, tfn, content) { 338 return new Promise ( 339 function (resolve, reject) { 340 $.ajax({ 341 url: "/api/comment/add", 342 343 /* url: "https://api.chil.me/api/comment/add", */ 344 345 346 /* url: "http://localhost:59520/api/comment/add", */ 347 348 type: "POST", 349 data: { 350 "ObjectId": scope.post.id, 351 "Type" : 4, 352 "SubType" : 1, 353 "Email": email, 354 "UserName": nombre, 355 "Tfn": tfn, 356 "Message": content 357 }, 358 dataType:'json', 359 success: function (response) { 360 console.log('CUESTIONARIO POSTEADO!!!') 361 resolve(true); 362 }, 363 error: function(error){ 364 console.log("Something went wrong", error); 365 resolve(false); 366 } 367 }); 368 }); 369 }; 370 371 scope.isNullOrEmpty = function(data) { 372 return data == undefined || data == null || data == ''; 373 } 374 375 scope.createModel = function(origin) { 376 scope.model = new Object(); 377 if(origin.Groups !== undefined && origin.Groups !== null ) { 378 origin.Groups.forEach(function(group) { 379 group.Fields.forEach(function(field) { 380 if (field.Tag !== undefined && 381 field.Tag !== null && 382 field.Tag.trim() !== '' ) { 383 384 switch (field.Select) { 385 case 'slider': 386 field.value = scope.isNullOrEmpty(field.Default) ? "0" : field.Default; 387 break; 388 case 'multiple': 389 field.value = []; 390 break; 391 392 default: 393 if (field.Type == 'boolean') { 394 field.value = !scope.isNullOrEmpty(field.Default) && 395 (field.Default == '1' || field.Default == 'true') ? 396 true : false;
397 } else { 398 field.value = scope.isNullOrEmpty(field.Default) ? '' : field.Default; 399 } 400 } 401 //field.Tag.trim().toLowerCase(); 402 scope.model[field.Tag.trim().toLowerCase()] = null; 403 } 404 }); 405 }); 406 } 407 }; 408 409 scope.updateModel = function(origin) { 410 411 console.log('Actualizar datos'); 412 console.log('----------------'); 413 414 console.log(scope); 415 416 var mandatoryNotFilled = false; 417 418 origin.Groups.forEach(function(group) { 419 group.Fields.forEach(function(field) { 420 //console.log(field.Tag.trim().toLowerCase()); 421 //console.log(scope.model[field.Tag.trim().toLowerCase()]); 422 423 if (field.Tag !== undefined && 424 field.Tag !== null && 425 field.Tag.trim() !== '' && 426 !Array.isArray(scope.model[field.Tag.trim().toLowerCase()]) 427 ) { 428 429 scope.model[field.Tag.trim().toLowerCase()] = field.value; 430 } 431 432 // Verificar que si es un campo obligatorio, esta relleno 433 if (field.Mandatory) { 434 if ( 435 ( Array.isArray(scope.model[field.Tag.trim().toLowerCase()]) && 436 scope.model[field.Tag.trim().toLowerCase()].length === 0) 437 || 438 ( scope.model[field.Tag.trim().toLowerCase()] === '') 439 ) 440 mandatoryNotFilled = true; 441 } 442 }); 443 }); 444 445 return !mandatoryNotFilled; 446 }; 447 448 scope.checkTfn = function(tfn) { 449 450 var re = /^(\+34|0034|34)?[\s|\-|\.]?[6|7|9][\s|\-|\.]?([0-9][\s|\-|\.]?){8}$/; 451 if (tfn === null || tfn.trim() === '' 452 || !re.test(tfn.toLowerCase()) ) 453 return false; 454 else 455 return true; 456 }; 457 458 scope.checkEmail = function(email) { 459 var re = /\S+@\S+\.\S+/; 460 461 if (email === null || email.trim() === '' 462 || !re.test(email.toLowerCase()) 463 ) 464 return false; 465 else 466 return true; 467 }; 468 469 scope.checkName = function(nombre, apellidos) { 470 if (nombre === undefined || nombre === null || nombre.trim() === '' 471 /*|| apellidos === null || apellidos.trim() === '' */ 472 ) 473 return false; 474 else 475 return true; 476 }; 477 478 scope.cleanTfn = function(tfn) { 479 return tfn.replace(/[ .*+?^${}()|[\]\\]/g, ""); 480 }; 481 482 scope.cleanEmail = function(email) { 483 return email.trim().toLowerCase(); 484 }; 485 486 scope.cleanName = function(nombre, apellidos) { 487 488 if (apellidos == undefined || apellidos == null || apellidos.trim() == "") 489 return nombre.trim(); 490 else 491 return nombre.trim() + " " + apellidos.trim(); 492 }; 493 494})
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.