controllers.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. 'use strict';
  2. /* Controllers; their method are available where specified with the ng-controller
  3. * directive or for a given route/state (see app.js). They use some services to
  4. * connect to the backend (see services.js). */
  5. var eventManControllers = angular.module('eventManControllers', []);
  6. /* A controller that can be used to navigate. */
  7. eventManControllers.controller('NavigationCtrl', ['$scope', '$rootScope', '$location', 'Setting', 'Info',
  8. function ($scope, $rootScope, $location, Setting, Info) {
  9. $scope.logo = {};
  10. $scope.getLocation = function() {
  11. return $location.absUrl();
  12. };
  13. $scope.go = function(url) {
  14. $location.url(url);
  15. };
  16. Setting.query({setting: 'logo'}, function(data) {
  17. if (data && data.length) {
  18. $scope.logo = data[0];
  19. }
  20. });
  21. $scope.isActive = function(view) {
  22. if (view === $location.path()) {
  23. return true;
  24. }
  25. if (view[view.length-1] !== '/') {
  26. view = view + '/';
  27. }
  28. return $location.path().indexOf(view) == 0;
  29. };
  30. }]
  31. );
  32. /* Controller for a group of date and time pickers. */
  33. eventManControllers.controller('DatetimePickerCtrl', ['$scope',
  34. function ($scope) {
  35. $scope.open = function($event) {
  36. $event.preventDefault();
  37. $event.stopPropagation();
  38. $scope.opened = true;
  39. };
  40. }]
  41. );
  42. /* Controller for modals. */
  43. eventManControllers.controller('ModalConfirmInstanceCtrl', ['$scope', '$uibModalInstance', 'message',
  44. function ($scope, $uibModalInstance, message) {
  45. $scope.message = message;
  46. $scope.ok = function () {
  47. $uibModalInstance.close($scope);
  48. };
  49. $scope.cancel = function () {
  50. $uibModalInstance.dismiss('cancel');
  51. };
  52. }]
  53. );
  54. eventManControllers.controller('EventsListCtrl', ['$scope', 'Event', '$uibModal', '$log', '$translate', '$rootScope', '$state',
  55. function ($scope, Event, $uibModal, $log, $translate, $rootScope, $state) {
  56. $scope.tickets = [];
  57. $scope.events = Event.all(function(events) {
  58. if (events && $state.is('tickets')) {
  59. angular.forEach(events, function(evt, idx) {
  60. var evt_tickets = (evt.tickets || []).slice(0);
  61. angular.forEach(evt_tickets, function(obj, obj_idx) {
  62. obj.event_title = evt.title;
  63. obj.event_id = evt._id;
  64. });
  65. $scope.tickets.push.apply($scope.tickets, evt_tickets || []);
  66. });
  67. }
  68. });
  69. $scope.eventsOrderProp = "-begin_date";
  70. $scope.ticketsOrderProp = ["name", "surname"];
  71. $scope.confirm_delete = 'Do you really want to delete this event?';
  72. $rootScope.$on('$translateChangeSuccess', function () {
  73. $translate('Do you really want to delete this event?').then(function (translation) {
  74. $scope.confirm_delete = translation;
  75. });
  76. });
  77. $scope.remove = function(_id) {
  78. var modalInstance = $uibModal.open({
  79. scope: $scope,
  80. templateUrl: 'modal-confirm-action.html',
  81. controller: 'ModalConfirmInstanceCtrl',
  82. resolve: {
  83. message: function() { return $scope.confirm_delete; }
  84. }
  85. });
  86. modalInstance.result.then(function() {
  87. Event.remove({'id': _id}, function() {
  88. $scope.events = Event.all();
  89. }
  90. );
  91. });
  92. };
  93. $scope.updateOrded = function(key) {
  94. var new_order = [key];
  95. var inv_key;
  96. if (key && key[0] === '-') {
  97. inv_key = key.substring(1);
  98. } else {
  99. inv_key = '-' + key;
  100. }
  101. angular.forEach($scope.ticketsOrderProp,
  102. function(value, idx) {
  103. if (value !== key && value !== inv_key) {
  104. new_order.push(value);
  105. }
  106. }
  107. );
  108. $scope.ticketsOrderProp = new_order;
  109. };
  110. }]
  111. );
  112. eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event', '$log', '$translate', '$rootScope',
  113. function ($scope, $state, Event, $log, $translate, $rootScope) {
  114. $scope.event = {};
  115. $scope.event.tickets = [];
  116. $scope.event.formSchema = {};
  117. $scope.eventFormDisabled = false;
  118. if ($state.params.id) {
  119. $scope.event = Event.get($state.params);
  120. if ($state.is('event.view') || !$rootScope.hasPermission('event|update')) {
  121. $scope.eventFormDisabled = true;
  122. }
  123. }
  124. // store a new Event or update an existing one
  125. $scope.save = function() {
  126. // avoid override of event.tickets list.
  127. var this_event = angular.copy($scope.event);
  128. if (this_event.tickets) {
  129. delete this_event.tickets;
  130. }
  131. if (this_event._id === undefined) {
  132. $scope.event = Event.save(this_event);
  133. } else {
  134. $scope.event = Event.update(this_event);
  135. }
  136. $scope.eventForm.$setPristine(false);
  137. };
  138. $scope.saveForm = function(easyFormGeneratorModel) {
  139. $scope.event.formSchema = easyFormGeneratorModel;
  140. $scope.save();
  141. };
  142. }]
  143. );
  144. eventManControllers.controller('EventTicketsCtrl', ['$scope', '$state', 'Event', 'EventTicket', 'Setting', '$log', '$translate', '$rootScope', 'EventUpdates', '$uibModal',
  145. function ($scope, $state, Event, EventTicket, Setting, $log, $translate, $rootScope, EventUpdates, $uibModal) {
  146. $scope.ticketsOrder = ["name", "surname"];
  147. $scope.countAttendees = 0;
  148. $scope.message = {};
  149. $scope.event = {};
  150. $scope.ticket = {}; // current ticket, for the event.ticket.* states
  151. $scope.tickets = []; // list of all tickets, for the 'tickets' state
  152. $scope.formSchema = {};
  153. $scope.formData = {};
  154. $scope.guiOptions = {dangerousActionsEnabled: false};
  155. $scope.customFields = Setting.query({setting: 'ticket_custom_field', in_event_details: true});
  156. $scope.formFieldsMap = {};
  157. $scope.formFieldsMapRev = {};
  158. if ($state.params.id) {
  159. $scope.event = Event.get({id: $state.params.id}, function(data) {
  160. $scope.$watchCollection(function() {
  161. return $scope.event.tickets;
  162. }, function(prev, old) {
  163. $scope.calcAttendees();
  164. }
  165. );
  166. if (!(data && data.formSchema)) {
  167. return;
  168. }
  169. $scope.formSchema = data.formSchema.edaFieldsModel;
  170. $scope.extractFormFields(data.formSchema.formlyFieldsModel);
  171. // Editing an existing ticket
  172. if ($state.params.ticket_id) {
  173. EventTicket.get({id: $state.params.id, ticket_id: $state.params.ticket_id}, function(data) {
  174. $scope.ticket = data;
  175. angular.forEach(data, function(value, key) {
  176. if (!$scope.formFieldsMapRev[key]) {
  177. return;
  178. }
  179. $scope.formData[$scope.formFieldsMapRev[key]] = value;
  180. });
  181. });
  182. }
  183. });
  184. // Managing the list of tickets.
  185. if ($state.is('event.tickets')) {
  186. $scope.allPersons = Event.group_persons({id: $state.params.id});
  187. // Handle WebSocket connection used to update the list of tickets.
  188. $scope.EventUpdates = EventUpdates;
  189. $scope.EventUpdates.open();
  190. $scope.$watchCollection(function() {
  191. return $scope.EventUpdates.data;
  192. }, function(prev, old) {
  193. if (!($scope.EventUpdates.data && $scope.EventUpdates.data.update)) {
  194. return;
  195. }
  196. var data = $scope.EventUpdates.data.update;
  197. $log.debug('received ' + data.action + ' action from websocket source ' + data.uuid);
  198. if ($rootScope.app_uuid == data.uuid) {
  199. $log.debug('do not process our own message');
  200. return false;
  201. }
  202. if (!$scope.event.tickets) {
  203. $scope.event.tickets = [];
  204. }
  205. var ticket_idx = $scope.event.tickets.findIndex(function(el, idx, array) {
  206. return data._id == el._id;
  207. });
  208. if (ticket_idx != -1) {
  209. $log.debug('_id ' + data._id + ' found');
  210. } else {
  211. $log.debug('_id ' + data._id + ' not found');
  212. }
  213. if (data.action == 'update' && ticket_idx != -1 && $scope.event.tickets[ticket_idx] != data.ticket) {
  214. $scope.event.tickets[ticket_idx] = data.ticket;
  215. } else if (data.action == 'add' && ticket_idx == -1) {
  216. $scope._localAddTicket(data.ticket);
  217. } else if (data.action == 'delete' && ticket_idx != -1) {
  218. $scope._localRemoveTicket({_id: data._id});
  219. }
  220. }
  221. );
  222. }
  223. } else if ($state.is('tickets')) {
  224. $scope.tickets = EventTicket.all();
  225. }
  226. $scope.calcAttendees = function() {
  227. if (!($scope.event && $scope.event.tickets)) {
  228. $scope.countAttendees = 0;
  229. return;
  230. }
  231. var attendees = 0;
  232. angular.forEach($scope.event.tickets, function(value, key) {
  233. if (value.attended && !value.cancelled) {
  234. attendees += 1;
  235. }
  236. });
  237. $scope.countAttendees = attendees;
  238. };
  239. /* Stuff to do when a ticket is added, modified or removed locally. */
  240. $scope._localAddTicket = function(ticket, original_ticket) {
  241. if (!$state.is('event.tickets')) {
  242. return true;
  243. }
  244. var ret = true;
  245. if (!$scope.event.tickets) {
  246. $scope.event.tickets = [];
  247. }
  248. var ticket_idx = $scope.event.tickets.findIndex(function(el, idx, array) {
  249. return ticket._id == el._id;
  250. });
  251. if (ticket_idx != -1) {
  252. $log.warn('ticket already present: not added');
  253. ret = false;
  254. } else {
  255. $scope.event.tickets.push(ticket);
  256. }
  257. // Try to remove this person from the allPersons list using ID or email.
  258. var field = null;
  259. var field_value = null;
  260. if (original_ticket && original_ticket._id) {
  261. field = '_id';
  262. field_value = original_ticket._id;
  263. } else if (ticket.email) {
  264. field = 'email';
  265. field_value = ticket.email;
  266. }
  267. if (field) {
  268. var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
  269. return field_value == el[field];
  270. });
  271. if (all_person_idx != -1) {
  272. $scope.allPersons.splice(all_person_idx, 1);
  273. }
  274. }
  275. return ret;
  276. };
  277. $scope._localUpdateTicket = function(ticket) {
  278. if (!$scope.event.tickets) {
  279. $scope.event.tickets = [];
  280. }
  281. var ticket_idx = $scope.event.tickets.findIndex(function(el, idx, array) {
  282. return ticket._id == el._id;
  283. });
  284. if (ticket_idx == -1) {
  285. $log.warn('ticket not present: not updated');
  286. return false;
  287. }
  288. $scope.event.tickets[ticket_idx] = ticket;
  289. };
  290. $scope._localRemoveTicket = function(ticket) {
  291. if (!(ticket && ticket._id && $scope.event.tickets)) {
  292. return;
  293. }
  294. var ticket_idx = $scope.event.tickets.findIndex(function(el, idx, array) {
  295. return ticket._id == el._id;
  296. });
  297. if (ticket_idx == -1) {
  298. $log.warn('unable to find and delete ticket _id ' + ticket._id);
  299. return;
  300. }
  301. var removed_person = $scope.event.tickets.splice(ticket_idx, 1);
  302. // to be used to populate allPersons, if needed.
  303. if (removed_person.length) {
  304. person = removed_person[0];
  305. } else {
  306. return;
  307. }
  308. if (!$scope.allPersons) {
  309. $scope.allPersons = [];
  310. }
  311. var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
  312. return person._id == el._id;
  313. });
  314. if (all_person_idx == -1 && person._id) {
  315. $scope.allPersons.push(person);
  316. }
  317. };
  318. $scope.setTicketAttribute = function(ticket, key, value, callback, hideMessage) {
  319. $log.debug('setTicketAttribute for _id ' + ticket._id + ' key: ' + key + ' value: ' + value);
  320. var newData = {event_id: $state.params.id, _id: ticket._id};
  321. newData[key] = value;
  322. EventTicket.update(newData, function(data) {
  323. if (!(data && data._id && data.ticket)) {
  324. return;
  325. }
  326. var ticket_idx = $scope.event.tickets.findIndex(function(el, idx, array) {
  327. return data._id == el._id;
  328. });
  329. if (ticket_idx == -1) {
  330. $log.warn('unable to find ticket _id ' + _id);
  331. return;
  332. }
  333. if ($scope.event.tickets[ticket_idx] != data.ticket) {
  334. $scope.event.tickets[ticket_idx] = data.ticket;
  335. }
  336. if (callback) {
  337. callback(data);
  338. }
  339. if (key === 'attended' && !hideMessage) {
  340. var msg = {};
  341. if (value) {
  342. msg.message = '' + ticket.name + ' ' + ticket.surname + ' successfully added to event ' + $scope.event.title;
  343. } else {
  344. msg.message = '' + ticket.name + ' ' + ticket.surname + ' successfully removed from event ' + $scope.event.title;
  345. msg.isError = true;
  346. }
  347. $scope.showMessage(msg);
  348. }
  349. });
  350. };
  351. $scope.setTicketAttributeAndRefocus = function(ticket, key, value) {
  352. $scope.setTicketAttribute(ticket, key, value);
  353. $scope.query = '';
  354. };
  355. $scope._setAttended = function(ticket) {
  356. $scope.setTicketAttribute(ticket, 'attended', true, null, true);
  357. };
  358. $scope.deleteTicket = function(ticket) {
  359. EventTicket.delete({
  360. event_id: $state.params.id,
  361. ticket_id: ticket._id
  362. }, function() {
  363. $scope._localRemoveTicket(ticket);
  364. });
  365. };
  366. $scope.addTicket = function(ticket) {
  367. ticket.event_id = $state.params.id;
  368. EventTicket.add(ticket, function(ticket) {
  369. $log.debug('addTicket');
  370. $log.debug(ticket);
  371. $scope._localAddTicket(ticket, ticket);
  372. if (!$state.is('event.tickets')) {
  373. $state.go('event.ticket.edit', {id: $scope.event._id, ticket_id: ticket._id});
  374. } else {
  375. $scope.query = '';
  376. $scope._setAttended(ticket);
  377. if ($scope.$close) {
  378. $scope.$close();
  379. }
  380. }
  381. });
  382. };
  383. $scope.updateTicket = function(ticket, cb) {
  384. ticket.event_id = $state.params.id;
  385. EventTicket.update(ticket, function(t) {
  386. $scope._localUpdateTicket(t.ticket);
  387. if (cb) {
  388. cb(t);
  389. }
  390. });
  391. };
  392. $scope.toggleCancelledTicket = function() {
  393. if (!$scope.ticket._id) {
  394. return;
  395. }
  396. $scope.ticket.cancelled = !$scope.ticket.cancelled;
  397. $scope.setTicketAttribute($scope.ticket, 'cancelled', $scope.ticket.cancelled, function() {
  398. $scope.guiOptions.dangerousActionsEnabled = false;
  399. });
  400. };
  401. $scope.openQuickAddTicket = function(_id) {
  402. var modalInstance = $uibModal.open({
  403. templateUrl: 'modal-quick-add-ticket.html',
  404. controller: 'EventTicketsCtrl'
  405. });
  406. modalInstance.result.then(function() {
  407. });
  408. };
  409. $scope.submitForm = function(dataModelSubmitted) {
  410. angular.forEach(dataModelSubmitted, function(value, key) {
  411. key = $scope.formFieldsMap[key] || key;
  412. $scope.ticket[key] = value;
  413. });
  414. if (!$state.params.ticket_id) {
  415. $scope.addTicket($scope.ticket);
  416. } else {
  417. $scope.updateTicket($scope.ticket);
  418. }
  419. };
  420. $scope.cancelForm = function() {
  421. if (!$state.is('event.tickets')) {
  422. $state.go('events');
  423. } else if ($scope.$close) {
  424. $scope.$close();
  425. }
  426. };
  427. $scope.extractFormFields = function(formlyFieldsModel) {
  428. if (!formlyFieldsModel) {
  429. return;
  430. }
  431. angular.forEach(formlyFieldsModel, function(row, idx) {
  432. if (!row.className == 'row') {
  433. return;
  434. }
  435. angular.forEach(row.fieldGroup || [], function(item, idx) {
  436. if (!(item.key && item.templateOptions && item.templateOptions.label)) {
  437. return;
  438. }
  439. var value = item.templateOptions.label.toLowerCase();
  440. $scope.formFieldsMap[item.key] = value;
  441. $scope.formFieldsMapRev[value] = item.key;
  442. });
  443. });
  444. };
  445. $scope.updateOrded = function(key) {
  446. var new_order = [key];
  447. var inv_key;
  448. if (key && key[0] === '-') {
  449. inv_key = key.substring(1);
  450. } else {
  451. inv_key = '-' + key;
  452. }
  453. angular.forEach($scope.ticketsOrder,
  454. function(value, idx) {
  455. if (value !== key && value !== inv_key) {
  456. new_order.push(value);
  457. }
  458. }
  459. );
  460. $scope.ticketsOrder = new_order;
  461. };
  462. $scope.showMessage = function(cfg) {
  463. $scope.message.show(cfg);
  464. };
  465. $scope.$on('$destroy', function() {
  466. $scope.EventUpdates && $scope.EventUpdates.close();
  467. });
  468. }]
  469. );
  470. eventManControllers.controller('UsersCtrl', ['$scope', '$rootScope', '$state', '$log', 'User', '$uibModal',
  471. function ($scope, $rootScope, $state, $log, User, $uibModal) {
  472. $scope.loginData = {};
  473. $scope.user = {};
  474. $scope.updateUserInfo = {};
  475. $scope.users = [];
  476. $scope.usersOrderProp = 'username';
  477. $scope.ticketsOrderProp = 'title';
  478. $scope.confirm_delete = 'Do you really want to delete this user?';
  479. $rootScope.$on('$translateChangeSuccess', function () {
  480. $translate('Do you really want to delete this user?').then(function (translation) {
  481. $scope.confirm_delete = translation;
  482. });
  483. });
  484. $scope.updateUsersList = function() {
  485. if ($state.is('users')) {
  486. $scope.users = User.all();
  487. }
  488. };
  489. $scope.updateUsersList();
  490. if ($state.is('user.edit') && $state.params.id) {
  491. $scope.user = User.get({id: $state.params.id}, function() {
  492. $scope.updateUserInfo = $scope.user;
  493. });
  494. }
  495. $scope.updateUser = function() {
  496. User.update($scope.updateUserInfo);
  497. };
  498. $scope.deleteUser = function(user_id) {
  499. var modalInstance = $uibModal.open({
  500. scope: $scope,
  501. templateUrl: 'modal-confirm-action.html',
  502. controller: 'ModalConfirmInstanceCtrl',
  503. resolve: {
  504. message: function() { return $scope.confirm_delete; }
  505. }
  506. });
  507. modalInstance.result.then(function() {
  508. User.delete({id: user_id}, $scope.updateUsersList);
  509. });
  510. };
  511. $scope.register = function() {
  512. User.add($scope.newUser, function(data) {
  513. $scope.login($scope.newUser);
  514. });
  515. };
  516. $scope.login = function(loginData) {
  517. if (!loginData) {
  518. loginData = $scope.loginData;
  519. }
  520. User.login(loginData, function(data) {
  521. if (!data.error) {
  522. $rootScope.readInfo(function(info) {
  523. $log.debug('logged in user: ' + info.user.username);
  524. $rootScope.clearError();
  525. $state.go('events');
  526. });
  527. }
  528. });
  529. };
  530. $scope.logout = function() {
  531. User.logout({}, function(data) {
  532. if (!data.error) {
  533. $rootScope.readInfo(function() {
  534. $log.debug('logged out user');
  535. $state.go('events');
  536. });
  537. }
  538. });
  539. };
  540. }]
  541. );
  542. eventManControllers.controller('FileUploadCtrl', ['$scope', '$log', '$upload', 'Event',
  543. function ($scope, $log, $upload, Event) {
  544. $scope.file = null;
  545. $scope.reply = {};
  546. $scope.events = Event.all();
  547. $scope.upload = function(file, url) {
  548. $log.debug("FileUploadCtrl.upload");
  549. $upload.upload({
  550. url: url,
  551. file: file,
  552. fields: {targetEvent: $scope.targetEvent}
  553. }).progress(function(evt) {
  554. var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
  555. $log.debug('progress: ' + progressPercentage + '%');
  556. }).success(function(data, status, headers, config) {
  557. $scope.file = null;
  558. $scope.reply = angular.fromJson(data);
  559. });
  560. };
  561. }]
  562. );