/app/IndexController.js

https://gitlab.com/umakantverma/LicExamApp · JavaScript · 246 lines · 226 code · 11 blank · 9 comment · 25 complexity · 2d1c8a434f5f73445c58b2a16143f972 MD5 · raw file

  1. licExamGuru.controller("IndexController", function($scope, $rootScope, $cookies, $cookieStore, $http, licExamGuruApi, $indexedDB)
  2. {
  3. $scope.init = function(){
  4. $indexedDB.openStore('users', function(store){
  5. store.getAll().then(function(member) {
  6. if(member == 0)
  7. {
  8. window.location.hash = "#/signup";
  9. }
  10. else if($.cookie('userid'))
  11. {
  12. if($.cookie('isPaid') != 'true')
  13. window.location.hash = "#/licence";
  14. else
  15. window.location = getUrl(url)+"home.html";
  16. }
  17. else
  18. {
  19. window.location.hash = "#/signin";
  20. }
  21. });
  22. });
  23. }
  24. $scope.init();
  25. });
  26. licExamGuru.controller("SigninController", function($scope, $rootScope, $cookies, $cookieStore, $http, licExamGuruApi, $indexedDB, dateFilter)
  27. {
  28. $.backstretch("img/login-bg.jpg", {speed: 500});
  29. $scope.email = '';
  30. $scope.paddword = '';
  31. $scope.isSuccess = false;
  32. $scope.loginFailMsg = '';
  33. $scope.isLoginSction = true;
  34. $scope.isLicenceSection = false;
  35. $scope.data = [];
  36. $scope.showLicence = function(){
  37. var exp_date = $scope.dateFormat();
  38. if( $.cookie('userStatus') != 'paid' || $.cookie('userExpDate') > exp_date)
  39. {
  40. $scope.isLoginSction = false;
  41. $scope.isLicenceSection = true;
  42. }
  43. }
  44. $scope.dateFormat = function(){
  45. var date = new Date();
  46. return dateFilter(date, 'yyyyMMdd');
  47. }
  48. $scope.login = function(){
  49. $scope.loginFailMsg = "Checking offline processing ...";
  50. $indexedDB.openStore('users', function(store){
  51. var query = store.QueryBulder().$index('email').$eq($scope.email);
  52. store.eachWhere(query).then(function(user){
  53. if(user){
  54. $.each(user, function(key, value){
  55. if(value.email == $scope.email && value.password.split('~-+-~')[1] == pass_decode($scope.password).split('~-+-~')[1])
  56. {
  57. $scope.loginFailMsg = "Login success processing ...";
  58. $scope.isSuccess = true;
  59. $.cookie('userid', value.id);
  60. $.cookie('userName',value.name);
  61. var today = $scope.dateFormat();
  62. var exp_date = value.exp_date ? value.exp_date.replace(' ', '') : today;
  63. if(value.status == 'paid' && exp_date.replace(' ', '') > today)
  64. {
  65. $.cookie('isPaid', 'true');
  66. }else
  67. {
  68. value['status'] = 'free';
  69. store.upsert(value);
  70. $.cookie('isPaid', 'false');
  71. }
  72. //window.location = getUrl(url)+"home.html";
  73. window.location.hash = "#/";
  74. }
  75. });
  76. $scope.loginFailMsg = "Checking online processing ...";
  77. }
  78. if(navigator.onLine){
  79. licExamGuruApi.serverPost('check_user', data={email : $scope.email, password: $scope.password})
  80. .success(function(data){
  81. if(data['result']){
  82. $scope.loginFailMsg = "Login success processing ...";
  83. $scope.isSuccess = true;
  84. var user_data = [{
  85. 'id' : (data['result']['id']).toString(),
  86. 'name' : data['result']['name'],
  87. 'email' : data['result']['email'],
  88. 'mobile' : data['result']['mobile'],
  89. 'state' : data['result']['state'],
  90. 'city' : data['result']['city'],
  91. 'division' : data['result']['division'],
  92. 'password' : pass_decode(data['result']['password']),
  93. 'passwordtext' : pass_decode(data['result']['passwordtext'])
  94. }];
  95. $scope.user_data = user_data;
  96. $scope.saveInDb();
  97. $.cookie('userid', data['result']['id']);
  98. $.cookie('userName',data['result']['name']);
  99. }
  100. else
  101. {
  102. $scope.isSuccess = false;
  103. $scope.loginFailMsg = "Email or password not match";
  104. }
  105. });
  106. }
  107. else
  108. {
  109. $scope.isSuccess = false;
  110. $scope.loginFailMsg = "Email or password not match";
  111. }
  112. });
  113. });
  114. }
  115. $scope.saveInDb = function()
  116. {
  117. $indexedDB.openStore('users', function(store){
  118. store.insert($scope.user_data);
  119. //window.location = getUrl(url)+"home.html";
  120. window.location.hash = "#/";
  121. });
  122. }
  123. });
  124. licExamGuru.controller("SignupController", function($scope, $rootScope, $cookies, $cookieStore, $http, licExamGuruApi, $indexedDB)
  125. {
  126. $.backstretch("img/login-bg.jpg", {speed: 500});
  127. $scope.name = '';
  128. $scope.email = '';
  129. $scope.mobile = '';
  130. $scope.state = '';
  131. $scope.city = '';
  132. $scope.division = '';
  133. $scope.paddword = '';
  134. $scope.passwordtext = '';
  135. $scope.errormsg = '';
  136. $scope.user_data = [];
  137. $scope.sugnup = function(){
  138. $indexedDB.openStore('users', function(store){
  139. store.count().then(function(count){
  140. var user_data = [{
  141. 'id' : (parseInt(count)+1).toString(),
  142. 'name' : $scope.name,
  143. 'email' : $scope.email,
  144. 'mobile' : $scope.mobile,
  145. 'state' : $scope.state,
  146. 'city' : $scope.city,
  147. 'division' : $scope.division,
  148. 'password' : $scope.password,
  149. 'passwordtext' : $scope.passwordtext,
  150. }];
  151. // if(navigator.onLine){
  152. licExamGuruApi.serverPost('create_user', user_data[0])
  153. .success(function(data){
  154. if(data['success']){
  155. $scope.isSuccess = false;
  156. $scope.loginFailMsg = data['success'];
  157. user_data[0]['password'] = pass_decode($scope.password);
  158. user_data[0]['passwordtext'] = pass_decode($scope.passwordtext);
  159. $scope.user_data = user_data;
  160. $scope.saveInDb();
  161. }
  162. else
  163. {
  164. $scope.isSuccess = false;
  165. $scope.loginFailMsg = data['errormsg'];
  166. }
  167. });
  168. // }
  169. // else
  170. // {
  171. // $scope.isSuccess = false;
  172. // $scope.loginFailMsg = 'Net connection is required to create ne user';
  173. // }
  174. });
  175. });
  176. }
  177. $scope.saveInDb = function()
  178. {
  179. $indexedDB.openStore('users', function(store){
  180. store.insert($scope.user_data);
  181. window.location.hash = "#/";
  182. });
  183. }
  184. });
  185. licExamGuru.controller("LicenceController", function($scope, $rootScope, $cookies, $cookieStore, $http, licExamGuruApi, $indexedDB, dateFilter)
  186. {
  187. $scope.licenceCode = '';
  188. $scope.locenceText = $.cookie('locenceText') ? $.cookie('locenceText') : create_licence_key();
  189. $.backstretch("img/login-bg.jpg", {speed: 500});
  190. $scope.isSuccess = false;
  191. $scope.loginFailMsg = '';
  192. $scope.dateFormat = function(){
  193. var date = new Date();
  194. return dateFilter(date, 'yyyy MM dd');
  195. }
  196. $scope.dateFormat30 = function(){
  197. var date = new Date();
  198. date.setTime(date.getTime()+ 30*24*60*60*1000);
  199. return dateFilter(date, 'yyyy MM dd');
  200. }
  201. $scope.licence = function()
  202. {
  203. $indexedDB.openStore('users', function(store){
  204. var query = store.QueryBulder().$index('id').$eq($.cookie('userid').toString());
  205. store.eachWhere(query).then(function(user){
  206. if(user){
  207. $.each(user, function(key, value){
  208. if(value.id == $.cookie('userid') && $scope.licenceCode == get_licence_value($scope.locenceText))
  209. {
  210. value['status'] = 'paid';
  211. value['exp_date'] = $scope.dateFormat30();
  212. store.upsert(value);
  213. $.cookie('isPaid', 'true');
  214. $.removeCookie('locenceText');
  215. window.location.hash = "#/";
  216. }
  217. else
  218. {
  219. $scope.isSuccess = false;
  220. $scope.loginFailMsg = 'Invalid Licence Code';
  221. }
  222. });
  223. }
  224. });
  225. });
  226. }
  227. })
  228. licExamGuru.controller("LogoutController", function($scope, $rootScope, $cookies, $cookieStore, $http, licExamGuruApi, $indexedDB)
  229. {
  230. $.removeCookie('userid');
  231. $.removeCookie('url');
  232. $.removeCookie('isPaid');
  233. $.removeCookie('userName');
  234. window.location = url;
  235. })