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.

238 lines
8.6 KiB

7 years ago
  1. angular.module('app.travels', ['pascalprecht.translate'])
  2. .controller('TravelsCtrl', function($scope, $http, $ionicModal, $timeout, $ionicLoading, $filter) {
  3. $scope.doRefresh = function() {
  4. /* travels refresh: */
  5. $http.get(urlapi + 'travels')
  6. .success(function(data, status, headers,config){
  7. console.log('data success travels');
  8. console.log(data); // for browser console
  9. $scope.travels = data; // for UI
  10. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  11. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  12. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  13. })
  14. .error(function(data, status, headers,config){
  15. console.log('data error');
  16. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  17. $ionicLoading.show({ template: 'Error connecting server', noBackdrop: true, duration: 2000 });
  18. })
  19. .then(function(result){
  20. travels = result.data;
  21. $ionicLoading.show({ template: 'Travels actualized from server!', noBackdrop: true, duration: 2000 });
  22. });
  23. /* users refresh: */
  24. $http.get(urlapi + 'users')
  25. .success(function(data, status, headers, config){
  26. console.log('data success users');
  27. console.log(data); // for browser console
  28. $scope.users = data; // for UI
  29. localStorage.setItem('c_users', JSON.stringify($scope.users));
  30. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  31. //set userdata
  32. $scope.userdata = $filter('filter')($scope.users, {username: $scope.storageusername}, true)[0];
  33. console.log("userdata");
  34. console.log($scope.userdata);
  35. localStorage.setItem("c_userdata", JSON.stringify($scope.userdata));
  36. })
  37. .error(function(data, status, headers,config){
  38. console.log('data error');
  39. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  40. })
  41. .then(function(result){
  42. users = result.data;
  43. });
  44. };
  45. $scope.newtravel={};
  46. /*$scope.newtravel={
  47. title: "prova",
  48. from: "prova",
  49. to: "prova",
  50. seats: 3,
  51. package: true,
  52. phone: 123,
  53. telegram: "telusr",
  54. description: "this is the description of prova"
  55. };*/
  56. // Create the login modal that we will use later
  57. $ionicModal.fromTemplateUrl('templates/newofferingtravel.html', {
  58. scope: $scope
  59. }).then(function(modal) {
  60. $scope.modalOffering = modal;
  61. });
  62. // Create the login modal that we will use later
  63. $ionicModal.fromTemplateUrl('templates/newaskingtravel.html', {
  64. scope: $scope
  65. }).then(function(modal) {
  66. $scope.modalAsking = modal;
  67. });
  68. $ionicModal.fromTemplateUrl('templates/newaskingpackage.html', {
  69. scope: $scope
  70. }).then(function(modal) {
  71. $scope.modalPackage = modal;
  72. });
  73. // Triggered in the login modal to close it
  74. $scope.closeNewOfferingTravel = function() {
  75. $scope.modalOffering.hide();
  76. };
  77. // Triggered in the login modal to close it
  78. $scope.closeNewAskingTravel = function() {
  79. $scope.modalAsking.hide();
  80. };
  81. $scope.closeNewAskingPackage = function() {
  82. $scope.modalPackage.hide();
  83. };
  84. // Open the login modal
  85. $scope.showNewOfferingTravel = function() {
  86. $scope.modalOffering.show();
  87. };
  88. // Open the login modal
  89. $scope.showNewAskingTravel = function() {
  90. $scope.modalAsking.show();
  91. };
  92. $scope.showNewAskingPackage = function() {
  93. $scope.modalPackage.show();
  94. };
  95. // Perform the login action when the user submits the login form
  96. $scope.doNewOfferingTravel = function() {
  97. console.log('Doing new travel', $scope.newtravel);
  98. $scope.newtravel.icon="lorry";
  99. $scope.newtravel.generateddate=$scope.newtravel.date;
  100. /*$scope.newtravel.owner=localStorage.getItem("c_username");
  101. $scope.newtravel.telegram=JSON.parse(localStorage.getItem("c_userdata")).telegram;
  102. $scope.newtravel.phone=JSON.parse(localStorage.getItem("c_userdata")).phone;*/
  103. $scope.newtravel.modality="offering";
  104. //$scope.newtravel.token=localStorage.getItem("c_token");
  105. console.log($scope.newtravel);
  106. $http({
  107. url: urlapi + 'travels',
  108. method: "POST",
  109. data: $scope.newtravel
  110. })
  111. .then(function(response) {
  112. // success
  113. console.log("response: ");
  114. console.log(response);
  115. //$scope.newtravel._id=response.data._id;
  116. //$scope.travels.push($scope.newtravel);
  117. $scope.travels=response.data;
  118. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  119. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  120. $scope.newtravel={};
  121. if(response.data.success==false){
  122. $ionicLoading.show({ template: 'failed to generate new travel', noBackdrop: true, duration: 2000 });
  123. }
  124. },
  125. function(response) { // optional
  126. // failed
  127. $ionicLoading.show({ template: 'failed to generate new publication, all input fields needed', noBackdrop: true, duration: 2000 });
  128. });
  129. // Simulate a login delay. Remove this and replace with your login
  130. // code if using a login system
  131. $timeout(function() {
  132. $scope.closeNewOfferingTravel();
  133. }, 1000);
  134. };
  135. $scope.doNewAskingTravel = function() {
  136. console.log('Doing new travel', $scope.newtravel);
  137. $scope.newtravel.icon="lorry";
  138. $scope.newtravel.generateddate=$scope.newtravel.date;
  139. /*$scope.newtravel.owner=localStorage.getItem("c_username");
  140. $scope.newtravel.telegram=JSON.parse(localStorage.getItem("c_userdata")).telegram;
  141. $scope.newtravel.phone=JSON.parse(localStorage.getItem("c_userdata")).phone;*/
  142. $scope.newtravel.modality="asking";
  143. console.log($scope.newtravel);
  144. $http({
  145. url: urlapi + 'travels',
  146. method: "POST",
  147. data: $scope.newtravel
  148. })
  149. .then(function(response) {
  150. // success
  151. console.log("response: ");
  152. console.log(response);
  153. //$scope.newtravel._id=response.data._id;
  154. //$scope.travels.push($scope.newtravel);
  155. $scope.travels=response.data;
  156. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  157. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  158. $scope.newtravel={};
  159. if(response.data.success==false){
  160. $ionicLoading.show({ template: 'failed to generate new asking travel', noBackdrop: true, duration: 2000 });
  161. }
  162. },
  163. function(response) { // optional
  164. // failed
  165. $ionicLoading.show({ template: 'failed to generate new publication, all input fields needed', noBackdrop: true, duration: 2000 });
  166. });
  167. // Simulate a login delay. Remove this and replace with your login
  168. // code if using a login system
  169. $timeout(function() {
  170. $scope.closeNewAskingTravel();
  171. }, 1000);
  172. };
  173. $scope.doNewAskingPackage = function() {
  174. console.log('Doing new package', $scope.newtravel);
  175. $scope.newtravel.icon="lorry";
  176. $scope.newtravel.generateddate=$scope.newtravel.date;
  177. /*$scope.newtravel.owner=localStorage.getItem("c_username");
  178. $scope.newtravel.telegram=JSON.parse(localStorage.getItem("c_userdata")).telegram;
  179. $scope.newtravel.phone=JSON.parse(localStorage.getItem("c_userdata")).phone;*/
  180. $scope.newtravel.package=true;
  181. $scope.newtravel.modality="package";
  182. console.log($scope.newtravel);
  183. $http({
  184. url: urlapi + 'travels',
  185. method: "POST",
  186. data: $scope.newtravel
  187. })
  188. .then(function(response) {
  189. // success
  190. console.log("response: ");
  191. console.log(response);
  192. //$scope.newtravel._id=response.data._id;
  193. //$scope.travels.push($scope.newtravel);
  194. $scope.travels=response.data;
  195. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  196. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  197. $scope.newtravel={};
  198. if(response.data.success==false){
  199. $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
  200. }
  201. },
  202. function(response) { // optional
  203. // failed
  204. $ionicLoading.show({ template: 'failed to generate new publication, all input fields needed', noBackdrop: true, duration: 2000 });
  205. });
  206. // Simulate a login delay. Remove this and replace with your login
  207. // code if using a login system
  208. $timeout(function() {
  209. $scope.closeNewAskingPackage();
  210. }, 1000);
  211. };
  212. });