You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

527 lines
22 KiB

  1. /**
  2. * Created by Benny on 12.10.2014.
  3. */
  4. "use strict";
  5. var TB = TB || {};
  6. TB.Control = TB.Control || {};
  7. TB.Control.Auth = {
  8. login : function( params )
  9. {
  10. var page = new TB.Page( 'auth' ),
  11. redirect = params && params.redirect ? params.redirect : null;
  12. page.onEnter = function( params )
  13. {
  14. $( '#main').hide();
  15. $( '#main_auth').show();
  16. this.$container = $( '#main_auth' );
  17. TB.Page.prototype.onEnter.call( this, params );
  18. };
  19. page.onLeave = function()
  20. {
  21. TB.Page.prototype.onLeave.call( this );
  22. $( '#main_auth').hide();
  23. $( '#main').show();
  24. };
  25. TB.GUI.show( page, { activeTab: 'login' } );
  26. // Login Button
  27. page.$container.find( '#btn_login').click( function() {
  28. if ( !TB.GUI.performFormValidation( $( '#form_login' ) ) )
  29. {
  30. return false;
  31. }
  32. TB.Service.request( 'Auth', 'login', {
  33. 'email' : page.$container.find( 'input[name="login.email"]' ).val(),
  34. 'pass' : page.$container.find( 'input[name="login.pass"]' ).val()
  35. },
  36. function( data ) {
  37. if ( data && data.account && data.profile && data.session && data.profiles && data.teams ) {
  38. TB.Client.setSessionData( data.account, data.profile, data.session, data.profiles, data.teams );
  39. TB.Ext.Push.register();
  40. TB.GUI.setTeamName();
  41. if ( redirect )
  42. {
  43. TB.Controller.do( 'Auth', 'redirect', { redirect : redirect } );
  44. TB.Utils.trackEvent( 'auth', 'redirect' );
  45. }
  46. else
  47. {
  48. TB.Controller.do( 'News' );
  49. TB.Utils.trackEvent( 'auth', 'login' );
  50. }
  51. } else if ( data && data.error && data.error == 'not_validated' )
  52. {
  53. TB.Ext.Notification.show( 'Deine Email Adresse ist noch nicht validiert. Bitte schau in deine Emails und klicke auf den Aktivierungslink.', TB.Ext.Notification.TYPE_WARNING );
  54. }
  55. else
  56. {
  57. TB.Ext.Notification.show( 'Anmeldung gescheitert. Passwort und/oder Email sind fehlerhaft.', TB.Ext.Notification.TYPE_DANGER );
  58. }
  59. },
  60. $( this )
  61. );
  62. return false;
  63. });
  64. // Forgot passwort link / Register actions
  65. page.$container.find( '[data-refdivid]').click( function() {
  66. TB.Controller.do( 'Auth', $(this).attr( 'data-refdivid') );
  67. });
  68. },
  69. registertrainer : function( params ) {
  70. var page = new TB.Page( 'auth' );
  71. page.onEnter = function( params ) {
  72. $( '#main').hide();
  73. $( '#main_auth').show();
  74. this.$container = $( '#main_auth' );
  75. TB.Page.prototype.onEnter.call( this, params );
  76. // Track registration
  77. try {
  78. TB.Utils.trackEvent( 'form', 'contactform' );
  79. } catch (e) {
  80. // Fail silently
  81. }
  82. };
  83. page.onLeave = function() {
  84. TB.Page.prototype.onLeave.call( this );
  85. $( '#main_auth').hide();
  86. $( '#main').show();
  87. };
  88. var buildRegistration = function( tmplData ) {
  89. TB.GUI.show( page, tmplData );
  90. $( '#main_auth').on( 'blur', "#form_register_trainer input", function() {
  91. TB.GUI.performInputValidation( $( this ) );
  92. });
  93. page.$container.find( '#btn_register_trainer').click( function() {
  94. if ( !TB.GUI.performFormValidation( $( '#form_register_trainer' ) ) ) {
  95. return false;
  96. }
  97. TB.Service.request( 'Auth', 'registerTrainer', {
  98. 'gender' : page.$container.find( '#form_register_trainer input[name="register.gender"]:checked' ).val(),
  99. 'team_name' : page.$container.find( '#form_register_trainer input[name="register.team_name"]' ).val(),
  100. 'team_category' : page.$container.find( '#form_register_trainer input[name="register.team_category"]' ).val(),
  101. 'email' : page.$container.find( '#form_register_trainer input[name="register.email"]' ).val(),
  102. 'pass' : page.$container.find( '#form_register_trainer input[name="register.pass"]' ).val(),
  103. 'first_name' : page.$container.find( '#form_register_trainer input[name="register.first_name"]').val(),
  104. 'last_name' : page.$container.find( '#form_register_trainer input[name="register.last_name"]').val(),
  105. 'subscribed_news' : page.$container.find( '#form_register_trainer input[name="register.subscribe_news"]').is(":checked" ) ? "on" : "off",
  106. 'token' : page.$container.find( '#form_register_trainer input[name="register.token"]').val()
  107. },
  108. function( data ) {
  109. if ( data && data.account && data.profile ) {
  110. TB.GUI.show( page, { activeTab: 'register_trainer_done' } );
  111. page.$container.find( '[data-refdivid]').click( function() {
  112. TB.Controller.do( 'Auth', $(this).attr( 'data-refdivid') );
  113. });
  114. // Tracking
  115. try {
  116. TB.Utils.trackEvent( 'auth', 'team_created' );
  117. // Google Tracking
  118. window.goog_report_conversion && window.goog_report_conversion();
  119. // Facebook Tracking
  120. window.fbq && fbq('track', 'CompleteRegistration');
  121. } catch( err ) {
  122. // Fail silently
  123. }
  124. } else {
  125. if ( data.error == "invalid-credentials" ) {
  126. TB.Ext.Alert.show( 'Accountdaten', 'Die eingegebene Email+Password Kombination stimmt nicht überein.' );
  127. }
  128. }
  129. },
  130. $( this ),
  131. function( data ) {
  132. if ( data && data.gen_err && "email_already_exists" == data.gen_err ) {
  133. TB.Ext.Notification.show( 'Die EMail Adresse existiert bereits. Wenn Du schon einen Account hast, dann kannst Du unter den Menüpunkt "Profile" im Seitenmenü ein neues Team anlegen.', TB.Ext.Notification.TYPE_INFO);
  134. } else {
  135. TB.Ext.Notification.show( 'Registrierung fehlgeschlagen. Bitte versuche es zu einem späteren Zeitpunkt nochmal.', TB.Ext.Notification.TYPE_DANGER );
  136. }
  137. }
  138. );
  139. return false;
  140. });
  141. // Forgot passwort link / Register actions
  142. page.$container.find( '[data-refdivid]').click( function() {
  143. TB.Controller.do( 'Auth', $(this).attr( 'data-refdivid') );
  144. });
  145. page.$container.find( '#registerteamcategory' ).easyAutocomplete({
  146. // Note:
  147. // Duplicate list
  148. data : TB.Config.General.team_categories,
  149. list: {
  150. maxNumberOfElements: 10,
  151. match: {
  152. enabled: true
  153. }
  154. }
  155. });
  156. };
  157. buildRegistration( { activeTab: 'register_trainer' } );
  158. },
  159. registerplayer : function( params ) {
  160. var page = new TB.Page( 'auth' );
  161. page.onEnter = function( params ) {
  162. $( '#main').hide();
  163. $( '#main_auth').show();
  164. this.$container = $( '#main_auth' );
  165. TB.Page.prototype.onEnter.call( this, params );
  166. };
  167. page.onLeave = function() {
  168. TB.Page.prototype.onLeave.call( this );
  169. $( '#main_auth').hide();
  170. $( '#main').show();
  171. };
  172. var token = ( params && params.t ) ? params.t : null;
  173. $( '#main_auth').on( 'blur', "#form_register_player input", function() {
  174. TB.GUI.performInputValidation( $( this ) );
  175. });
  176. var buildRegistration = function( tmplData ) {
  177. TB.GUI.show( page, tmplData );
  178. // Forgot passwort link / Register actions
  179. page.$container.find( '[data-refdivid]').click( function() {
  180. TB.Controller.do( 'Auth', $(this).attr( 'data-refdivid') );
  181. });
  182. page.$container.find( '#btn_register_player').click( function() {
  183. if ( !TB.GUI.performFormValidation( $( '#form_register_player' ) ) ) {
  184. return false;
  185. }
  186. TB.Service.request( 'Auth', 'registerPlayer', {
  187. 'pass' : page.$container.find( '#form_register_player input[name="register.pass"]' ).val(),
  188. 'subscribed_news' : page.$container.find( '#form_register_player input[name="register.subscribe_news"]').is(":checked" ) ? "on" : "off",
  189. 'token' : page.$container.find( '#form_register_player input[name="register.token"]').val()
  190. },
  191. function( data ) {
  192. if ( data && data.account && data.profile && data.session && data.profiles && data.teams ) {
  193. TB.Client.setSessionData( data.account, data.profile, data.session, data.profiles, data.teams );
  194. TB.Ext.Push.register();
  195. TB.Controller.do( 'News' );
  196. TB.Utils.trackEvent( 'auth', 'player_created' );
  197. } else {
  198. if ( data.error == "invalid-credentials" ) {
  199. TB.Ext.Alert.show( 'Accountdaten', 'Die eingegebene Email+Password Kombination stimmt nicht überein.' );
  200. } else if ( data.error == "invalid-token" ) {
  201. TB.Ext.Alert.show( 'Einladung ungültig', 'Deine Einladung ist leider nicht mehr gültig. Bitte frage den Trainer nach einer neuen.' );
  202. }
  203. }
  204. },
  205. $( this )
  206. );
  207. return false;
  208. });
  209. };
  210. TB.Service.request( 'Auth', 'getInvitation', { token: token },
  211. function( data ) {
  212. buildRegistration( {
  213. activeTab: 'register_player',
  214. token: data.token,
  215. invitation: data.invitation,
  216. account : data.account,
  217. inviter : data.inviter,
  218. team : data.team } );
  219. }
  220. );
  221. },
  222. forgot_password : function() {
  223. var page = new TB.Page( 'auth' );
  224. page.onEnter = function( params ) {
  225. $( '#main').hide();
  226. $( '#main_auth').show(); this.$container = $( '#main_auth' );
  227. TB.Page.prototype.onEnter.call( this, params );
  228. };
  229. page.onLeave = function() {
  230. TB.Page.prototype.onLeave.call( this );
  231. $( '#main_auth').hide();
  232. $( '#main').show();
  233. };
  234. TB.GUI.show( page, { activeTab: 'forgot_password' } );
  235. $( '#main_auth').on( 'blur', "form input", function() {
  236. TB.GUI.performInputValidation( $( this ) );
  237. });
  238. // Login Button
  239. page.$container.find( '#btn_forgot_password').click( function() {
  240. if (!TB.GUI.performFormValidation($('#form_forgot_password'))) {
  241. return false;
  242. }
  243. TB.Service.request( 'Auth', 'requestPasswordReset', {
  244. 'email' : page.$container.find( '#form_forgot_password input[name="forgot_password.email"]' ).val()
  245. },
  246. function( data ) {
  247. if ( data.res == "account_not_found" ) {
  248. TB.Ext.Notification.show( 'Es existiert kein Account mit angegebener Email Adresse.', TB.Ext.Notification.TYPE_DANGER );
  249. } else if ( data.res == "already_sent" ) {
  250. TB.Ext.Notification.show( 'Die Email ist bereits unterwegs. Ist sie etwa in Deinem Spam Ordner gelandet?', TB.Ext.Notification.TYPE_WARNING );
  251. } else {
  252. TB.Ext.Alert.modal( {
  253. title: 'Erfolg!',
  254. text: 'Eine Email an Dich ist unterwegs. Bitte schau in Dein Postfach und folge den Anweisungen, um ein neues Passwort zu erstellen.',
  255. type: 'success',
  256. showCancelButton: false
  257. }, function() {
  258. TB.Controller.do( 'Auth', 'login' );
  259. });
  260. }
  261. },
  262. $( this )
  263. );
  264. return false;
  265. }
  266. );
  267. // Forgot passwort link / Register actions
  268. page.$container.find( '[data-refdivid]').click( function() {
  269. TB.Controller.do( 'Auth', $(this).attr( 'data-refdivid') );
  270. });
  271. },
  272. reset_password : function( params ) {
  273. var page = new TB.Page( 'auth'),
  274. token = ( params && params.t ) ? params.t : null;;
  275. page.onEnter = function( params ) {
  276. $( '#main').hide();
  277. $( '#main_auth').show(); this.$container = $( '#main_auth' );
  278. TB.Page.prototype.onEnter.call( this, params );
  279. };
  280. page.onLeave = function() {
  281. TB.Page.prototype.onLeave.call( this );
  282. $( '#main_auth').hide();
  283. $( '#main').show();
  284. };
  285. TB.GUI.show( page, { activeTab: 'reset_password', pwrtoken: token } );
  286. $( '#main_auth').on( 'blur', "#form_reset_password input", function() {
  287. TB.GUI.performInputValidation( $( this ) );
  288. });
  289. // Reset Password Button
  290. page.$container.find( '#btn_reset_password').click( function() {
  291. if (!TB.GUI.performFormValidation($('#form_reset_password'))) {
  292. return false;
  293. }
  294. TB.Service.request( 'Auth', 'resetPassword', {
  295. 'token' : page.$container.find( '#form_reset_password input[name="reset_password.token"]' ).val(),
  296. 'password1' : page.$container.find( '#form_reset_password input[name="reset_password.password1"]' ).val(),
  297. 'password2' : page.$container.find( '#form_reset_password input[name="reset_password.password2"]' ).val()
  298. },
  299. function( data ) {
  300. if ( data.res == 'ok' ) {
  301. TB.Ext.Alert.modal( {
  302. title: 'Erfolg!',
  303. text: 'Dein Passwort wurde erfolgreich geändert. Logge Dich gleich damit ein.',
  304. type: 'success',
  305. showCancelButton: false
  306. }, function() {
  307. TB.Client.logout();
  308. TB.Controller.do( 'Auth', 'login' );
  309. });
  310. } else {
  311. TB.Ext.Alert.modal( {
  312. title: 'Oooopps.',
  313. text: 'Leider ist bei der Aktion ein Fehler aufgetreten. Versuche es erneut oder kontaktiere unseren Support.',
  314. type: 'warning',
  315. showCancelButton: false
  316. }, function() {
  317. TB.Controller.do( 'Auth', 'login' );
  318. });
  319. }
  320. },
  321. $( this )
  322. );
  323. return false;
  324. }
  325. );
  326. // Forgot passwort link / Register actions
  327. page.$container.find( '[data-refdivid]').click( function() {
  328. TB.Controller.do( 'Auth', $(this).attr( 'data-refdivid') );
  329. });
  330. },
  331. confirm_email_change : function( params ) {
  332. var page = new TB.Page( 'auth'),
  333. token = ( params && params.t ) ? params.t : null;;
  334. page.onEnter = function( params ) {
  335. $( '#main').hide();
  336. $( '#main_auth').show(); this.$container = $( '#main_auth' );
  337. TB.Page.prototype.onEnter.call( this, params );
  338. };
  339. page.onLeave = function() {
  340. TB.Page.prototype.onLeave.call( this );
  341. $( '#main_auth').hide();
  342. $( '#main').show();
  343. };
  344. TB.Service.request( 'Auth', 'confirmEmailChange', { token : token }, function( res ) {
  345. var actionResult = 'nok';
  346. if ( res && res.email_change_result ) {
  347. switch ( res.email_change_result ) {
  348. case 'ok':
  349. actionResult = 'ok';
  350. break;
  351. case 'timeup':
  352. actionResult = 'timeup'
  353. break;
  354. }
  355. }
  356. TB.GUI.show( page, { activeTab: 'confirm_email_change', actionResult : actionResult } );
  357. page.$container.find( '#btn-continue-after-confirm-email').click( function() {
  358. if ( TB.Client.isLoggedIn() ) {
  359. TB.Controller.do( 'News' );
  360. } else {
  361. TB.Controller.do( 'Auth', 'login' );
  362. }
  363. return false;
  364. });
  365. });
  366. },
  367. validate_email : function( params ) {
  368. var token = ( params && params.t ) ? params.t : null;
  369. $( '#main').hide();
  370. $( '#main_auth').show();
  371. TB.Service.request('Auth', 'validateEmail', {token: token}, function (data) {
  372. if (data.res == 'ok') {
  373. TB.Ext.Alert.modal( {
  374. title: 'Super!',
  375. text: 'Dein Account ist nun erfolgreich validiert.',
  376. type: 'success',
  377. showCancelButton: false
  378. }, function() {
  379. if ( TB.Client.isLoggedIn() ) {
  380. if ( data.profile ) {
  381. TB.Client.updateSessionProfile( data.profile );
  382. }
  383. $( '#main_auth').hide();
  384. $( '#main').show();
  385. TB.Controller.do( 'News' );
  386. } else {
  387. TB.Controller.do( 'Auth', 'login' );
  388. }
  389. });
  390. } else {
  391. TB.Ext.Alert.modal( {
  392. title: 'Oooopps.',
  393. text: 'Leider ist bei der Aktion ein Fehler aufgetreten. Versuche es später erneut oder kontaktiere unseren Support.',
  394. type: 'warning',
  395. showCancelButton: false
  396. }, function() {
  397. TB.Controller.do( 'Auth', 'login' );
  398. });
  399. }
  400. });
  401. },
  402. redirect : function( params )
  403. {
  404. var target = params && params.redirect ? decodeURIComponent( params.redirect ) : null,
  405. reObj = TB.Utils.decoreRedirect( target ),
  406. targetProfileId = null,
  407. isValidTargetId = false;
  408. if ( false === TB.Client.isLoggedIn() )
  409. {
  410. TB.Controller.do( 'Auth', 'login', { redirect : ( params && params.redirect ? params.redirect : null ) } );
  411. }
  412. else if ( reObj && reObj.c && reObj.a )
  413. {
  414. if ( reObj.targetTeamId && TB.Client.profile.team_id != reObj.targetTeamId )
  415. {
  416. // Check if targetTeamId is valid for current session account
  417. for ( var pi = 0; pi < TB.Client.profiles.length; pi++ )
  418. {
  419. if ( TB.Client.profiles[ pi ].team_id == reObj.targetTeamId )
  420. {
  421. targetProfileId = TB.Client.profiles[ pi ].id;
  422. isValidTargetId = true;
  423. break;
  424. }
  425. }
  426. if ( true === isValidTargetId )
  427. {
  428. // If valid
  429. TB.Service.request( 'Profile', 'change', { profile_id : targetProfileId },
  430. function( data )
  431. {
  432. if ( data && data.session && data.profile && data.profiles && data.teams )
  433. {
  434. TB.Client.setSessionData( TB.Client.account, data.profile, data.session, data.profiles, data.teams );
  435. $( window ).off( 'hashchange' );
  436. TB.Controller.updateHash( reObj.c, reObj.a, reObj.params );
  437. location.reload( true );
  438. }
  439. }
  440. );
  441. }
  442. else
  443. {
  444. // If not - redirect to News index
  445. TB.Controller.do( 'News', 'index' );
  446. }
  447. }
  448. else
  449. {
  450. TB.Controller.do( reObj.c, reObj.a, reObj.params );
  451. }
  452. }
  453. else
  454. {
  455. // Default case - redirect to News index
  456. TB.Controller.do( 'News', 'index' );
  457. }
  458. }
  459. };