controllers.js 23 KB

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