controllers.js 25 KB

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