controllers.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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.persons || []).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.persons = [];
  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.persons list.
  127. var this_event = angular.copy($scope.event);
  128. if (this_event.persons) {
  129. delete this_event.persons;
  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.personsOrder = ["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: 'person_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.persons;
  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 persons.
  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.persons) {
  203. $scope.event.persons = [];
  204. }
  205. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  206. return data._id == el._id;
  207. });
  208. if (person_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' && person_idx != -1 && $scope.event.persons[person_idx] != data.person) {
  214. $scope.event.persons[person_idx] = data.person;
  215. } else if (data.action == 'add' && person_idx == -1) {
  216. $scope._localAddTicket(data.person);
  217. } else if (data.action == 'delete' && person_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.persons)) {
  228. $scope.countAttendees = 0;
  229. return;
  230. }
  231. var attendees = 0;
  232. angular.forEach($scope.event.persons, 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_person) {
  241. if (!$state.is('event.tickets')) {
  242. return true;
  243. }
  244. var ret = true;
  245. if (!$scope.event.persons) {
  246. $scope.event.persons = [];
  247. }
  248. var ticket_idx = $scope.event.persons.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.persons.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_person && original_person._id) {
  261. field = '_id';
  262. field_value = original_person._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.persons) {
  279. $scope.event.persons = [];
  280. }
  281. var ticket_idx = $scope.event.persons.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.persons[ticket_idx] = ticket;
  289. };
  290. $scope._localRemoveTicket = function(person) {
  291. if (!(person && person._id && $scope.event.persons)) {
  292. return;
  293. }
  294. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  295. return person._id == el._id;
  296. });
  297. if (person_idx == -1) {
  298. $log.warn('unable to find and delete ticket _id ' + person._id);
  299. return;
  300. }
  301. var removed_person = $scope.event.persons.splice(person_idx, 1);
  302. // to be used to populate allPersons, if needed.
  303. if (removed_person.length) {
  304. person = removed_person[0];
  305. }
  306. if (!$scope.allPersons) {
  307. $scope.allPersons = [];
  308. }
  309. var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
  310. return person._id == el._id;
  311. });
  312. if (all_person_idx == -1 && person._id) {
  313. $scope.allPersons.push(person);
  314. }
  315. };
  316. $scope.setTicketAttribute = function(person, key, value, callback, hideMessage) {
  317. $log.debug('setTicketAttribute for _id ' + person._id + ' key: ' + key + ' value: ' + value);
  318. var newData = {event_id: $state.params.id, _id: person._id};
  319. newData[key] = value;
  320. EventTicket.update(newData, function(data) {
  321. if (!(data && data._id && data.person)) {
  322. return;
  323. }
  324. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  325. return data._id == el._id;
  326. });
  327. if (person_idx == -1) {
  328. $log.warn('unable to find ticket _id ' + _id);
  329. return;
  330. }
  331. if ($scope.event.persons[person_idx] != data.person) {
  332. $scope.event.persons[person_idx] = data.person;
  333. }
  334. if (callback) {
  335. callback(data);
  336. }
  337. if (key === 'attended' && !hideMessage) {
  338. var msg = {};
  339. if (value) {
  340. msg.message = '' + person.name + ' ' + person.surname + ' successfully added to event ' + $scope.event.title;
  341. } else {
  342. msg.message = '' + person.name + ' ' + person.surname + ' successfully removed from event ' + $scope.event.title;
  343. msg.isError = true;
  344. }
  345. $scope.showMessage(msg);
  346. }
  347. });
  348. };
  349. $scope.setTicketAttributeAndRefocus = function(person, key, value) {
  350. $scope.setTicketAttribute(person, key, value);
  351. $scope.query = '';
  352. };
  353. $scope._setAttended = function(person) {
  354. $scope.setTicketAttribute(person, 'attended', true, null, true);
  355. };
  356. $scope.deleteTicket = function(person) {
  357. EventTicket.delete({
  358. event_id: $state.params.id,
  359. ticket_id: person._id
  360. }, function() {
  361. $scope._localRemoveTicket(person);
  362. });
  363. };
  364. $scope.addTicket = function(person) {
  365. person.event_id = $state.params.id;
  366. EventTicket.add(person, function(ticket) {
  367. $log.debug('addTicket');
  368. $log.debug(ticket);
  369. $scope._localAddTicket(ticket, person);
  370. if (!$state.is('event.tickets')) {
  371. $state.go('event.ticket.edit', {id: $scope.event._id, ticket_id: ticket._id});
  372. } else {
  373. $scope.query = '';
  374. $scope._setAttended(ticket);
  375. if ($scope.$close) {
  376. $scope.$close();
  377. }
  378. }
  379. });
  380. };
  381. $scope.updateTicket = function(ticket, cb) {
  382. ticket.event_id = $state.params.id;
  383. EventTicket.update(ticket, function(t) {
  384. $scope._localUpdateTicket(t.person);
  385. if (cb) {
  386. cb(t);
  387. }
  388. });
  389. };
  390. $scope.toggleCancelledTicket = function() {
  391. if (!$scope.ticket._id) {
  392. return;
  393. }
  394. $scope.ticket.cancelled = !$scope.ticket.cancelled;
  395. $scope.setTicketAttribute($scope.ticket, 'cancelled', $scope.ticket.cancelled, function() {
  396. $scope.guiOptions.dangerousActionsEnabled = false;
  397. });
  398. };
  399. $scope.openQuickAddTicket = function(_id) {
  400. var modalInstance = $uibModal.open({
  401. templateUrl: 'modal-quick-add-ticket.html',
  402. controller: 'EventTicketsCtrl'
  403. });
  404. modalInstance.result.then(function() {
  405. });
  406. };
  407. $scope.submitForm = function(dataModelSubmitted) {
  408. angular.forEach(dataModelSubmitted, function(value, key) {
  409. key = $scope.formFieldsMap[key] || key;
  410. $scope.ticket[key] = value;
  411. });
  412. if (!$state.params.ticket_id) {
  413. $scope.addTicket($scope.ticket);
  414. } else {
  415. $scope.updateTicket($scope.ticket);
  416. }
  417. };
  418. $scope.cancelForm = function() {
  419. if (!$state.is('event.tickets')) {
  420. $state.go('events');
  421. } else if ($scope.$close) {
  422. $scope.$close();
  423. }
  424. };
  425. $scope.extractFormFields = function(formlyFieldsModel) {
  426. if (!formlyFieldsModel) {
  427. return;
  428. }
  429. angular.forEach(formlyFieldsModel, function(row, idx) {
  430. if (!row.className == 'row') {
  431. return;
  432. }
  433. angular.forEach(row.fieldGroup || [], function(item, idx) {
  434. if (!(item.key && item.templateOptions && item.templateOptions.label)) {
  435. return;
  436. }
  437. var value = item.templateOptions.label.toLowerCase();
  438. $scope.formFieldsMap[item.key] = value;
  439. $scope.formFieldsMapRev[value] = item.key;
  440. });
  441. });
  442. };
  443. $scope.updateOrded = function(key) {
  444. var new_order = [key];
  445. var inv_key;
  446. if (key && key[0] === '-') {
  447. inv_key = key.substring(1);
  448. } else {
  449. inv_key = '-' + key;
  450. }
  451. angular.forEach($scope.personsOrder,
  452. function(value, idx) {
  453. if (value !== key && value !== inv_key) {
  454. new_order.push(value);
  455. }
  456. }
  457. );
  458. $scope.personsOrder = new_order;
  459. };
  460. $scope.showMessage = function(cfg) {
  461. $scope.message.show(cfg);
  462. };
  463. $scope.$on('$destroy', function() {
  464. $scope.EventUpdates && $scope.EventUpdates.close();
  465. });
  466. }]
  467. );
  468. eventManControllers.controller('UsersCtrl', ['$scope', '$rootScope', '$state', '$log', 'User', '$uibModal',
  469. function ($scope, $rootScope, $state, $log, User, $uibModal) {
  470. $scope.loginData = {};
  471. $scope.user = {};
  472. $scope.updateUserInfo = {};
  473. $scope.users = [];
  474. $scope.usersOrderProp = 'username';
  475. $scope.ticketsOrderProp = 'title';
  476. $scope.confirm_delete = 'Do you really want to delete this user?';
  477. $rootScope.$on('$translateChangeSuccess', function () {
  478. $translate('Do you really want to delete this user?').then(function (translation) {
  479. $scope.confirm_delete = translation;
  480. });
  481. });
  482. $scope.updateUsersList = function() {
  483. if ($state.is('users')) {
  484. $scope.users = User.all();
  485. }
  486. };
  487. $scope.updateUsersList();
  488. if ($state.is('user.edit') && $state.params.id) {
  489. $scope.user = User.get({id: $state.params.id}, function() {
  490. $scope.updateUserInfo = $scope.user;
  491. });
  492. }
  493. $scope.updateUser = function() {
  494. User.update($scope.updateUserInfo);
  495. };
  496. $scope.deleteUser = function(user_id) {
  497. var modalInstance = $uibModal.open({
  498. scope: $scope,
  499. templateUrl: 'modal-confirm-action.html',
  500. controller: 'ModalConfirmInstanceCtrl',
  501. resolve: {
  502. message: function() { return $scope.confirm_delete; }
  503. }
  504. });
  505. modalInstance.result.then(function() {
  506. User.delete({id: user_id}, $scope.updateUsersList);
  507. });
  508. };
  509. $scope.register = function() {
  510. User.add($scope.newUser, function(data) {
  511. $scope.login($scope.newUser);
  512. });
  513. };
  514. $scope.login = function(loginData) {
  515. if (!loginData) {
  516. loginData = $scope.loginData;
  517. }
  518. User.login(loginData, function(data) {
  519. if (!data.error) {
  520. $rootScope.readInfo(function(info) {
  521. $log.debug('logged in user: ' + info.user.username);
  522. $rootScope.clearError();
  523. $state.go('events');
  524. });
  525. }
  526. });
  527. };
  528. $scope.logout = function() {
  529. User.logout({}, function(data) {
  530. if (!data.error) {
  531. $rootScope.readInfo(function() {
  532. $log.debug('logged out user');
  533. $state.go('events');
  534. });
  535. }
  536. });
  537. };
  538. }]
  539. );
  540. eventManControllers.controller('FileUploadCtrl', ['$scope', '$log', '$upload', 'Event',
  541. function ($scope, $log, $upload, Event) {
  542. $scope.file = null;
  543. $scope.reply = {};
  544. $scope.events = Event.all();
  545. $scope.upload = function(file, url) {
  546. $log.debug("FileUploadCtrl.upload");
  547. $upload.upload({
  548. url: url,
  549. file: file,
  550. fields: {targetEvent: $scope.targetEvent}
  551. }).progress(function(evt) {
  552. var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
  553. $log.debug('progress: ' + progressPercentage + '%');
  554. }).success(function(data, status, headers, config) {
  555. $scope.file = null;
  556. $scope.reply = angular.fromJson(data);
  557. });
  558. };
  559. }]
  560. );