controllers.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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 = Person.all();
  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. $log.debug(data);
  178. if (person_idx != -1) {
  179. $log.debug('_id ' + data._id + ' found');
  180. } else {
  181. $log.debug('_id ' + data._id + ' not found');
  182. }
  183. if (data.action == 'update' && person_idx != -1 && $scope.event.persons[person_idx] != data.person) {
  184. $scope.event.persons[person_idx] = data.person;
  185. } else if (data.action == 'add' && person_idx == -1) {
  186. $scope._localAddTicket(data.person);
  187. } else if (data.action == 'delete' && person_idx != -1) {
  188. $scope._localRemoveTicket({_id: data._id});
  189. }
  190. }
  191. );
  192. }
  193. }
  194. $scope.calcAttendees = function() {
  195. if (!($scope.event && $scope.event.persons)) {
  196. $scope.countAttendees = 0;
  197. return;
  198. }
  199. var attendees = 0;
  200. angular.forEach($scope.event.persons, function(value, key) {
  201. if (value.attended && !value.cancelled) {
  202. attendees += 1;
  203. }
  204. });
  205. $scope.countAttendees = attendees;
  206. };
  207. /* Stuff to do when a ticket is added, modified or removed locally. */
  208. $scope._localAddTicket = function(person) {
  209. if (!$scope.event.persons) {
  210. $scope.event.persons = [];
  211. }
  212. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  213. return person._id == el._id;
  214. });
  215. if (person_idx != -1) {
  216. $log.debug('person already present: not added');
  217. return false;
  218. }
  219. $scope.event.persons.push(person);
  220. };
  221. $scope._localUpdateTicket(ticket) {
  222. if (!$scope.event.persons) {
  223. $scope.event.persons = [];
  224. }
  225. var ticket_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  226. return ticket._id == el._id;
  227. });
  228. if (ticket_idx == -1) {
  229. $log.debug('person not present: not updated');
  230. return false;
  231. }
  232. $scope.event.persons[ticket_idx] = ticket;
  233. });
  234. $scope._localRemoveTicket = function(person) {
  235. $log.debug('_localRemoveTicket');
  236. $log.debug(person);
  237. if (!(person && person._id && $scope.event.persons)) {
  238. return;
  239. }
  240. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  241. return person._id == el._id;
  242. });
  243. if (person_idx == -1) {
  244. $log.warn('unable to find and delete ticket _id ' + person._id);
  245. return;
  246. }
  247. var removed_person = $scope.event.persons.splice(person_idx, 1);
  248. // to be used to populate allPersons, if needed.
  249. if (removed_person.length) {
  250. person = removed_person[0];
  251. }
  252. var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
  253. return person._id == el._id;
  254. });
  255. if (all_person_idx == -1 && person._id) {
  256. $scope.allPersons.push(person);
  257. }
  258. };
  259. $scope.setPersonAttribute = function(person, key, value, callback, hideMessage) {
  260. $log.debug('EventDetailsCtrl.setPersonAttribute.event_id: ' + $state.params.id);
  261. $log.debug('EventDetailsCtrl.setPersonAttribute._id: ' + person._id);
  262. $log.debug('EventDetailsCtrl.setPersonAttribute.key: ' + key + ' value: ' + value);
  263. var newData = {event_id: $state.params.id, _id: person._id};
  264. newData[key] = value;
  265. EventTicket.update(newData, function(data) {
  266. if (!(data && data._id && data.person)) {
  267. return;
  268. }
  269. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  270. return data._id == el._id;
  271. });
  272. if (person_idx == -1) {
  273. $log.warn('unable to find ticket _id ' + _id);
  274. return;
  275. }
  276. if ($scope.event.persons[person_idx] != data.person) {
  277. $scope.event.persons[person_idx] = data.person;
  278. }
  279. if (callback) {
  280. callback(data);
  281. }
  282. if (key === 'attended' && !hideMessage) {
  283. var msg = {};
  284. if (value) {
  285. msg.message = '' + person.name + ' ' + person.surname + ' successfully added to event ' + $scope.event.title;
  286. } else {
  287. msg.message = '' + person.name + ' ' + person.surname + ' successfully removed from event ' + $scope.event.title;
  288. msg.isError = true;
  289. }
  290. $scope.showMessage(msg);
  291. }
  292. });
  293. };
  294. $scope.setPersonAttributeAndRefocus = function(person, key, value) {
  295. $scope.setPersonAttribute(person, key, value);
  296. $scope.query = '';
  297. };
  298. $scope._setAttended = function(person) {
  299. $scope.setPersonAttribute(person, 'attended', true, function() {
  300. var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
  301. return person._id == el._id;
  302. });
  303. if (all_person_idx != -1) {
  304. $scope.allPersons.splice(all_person_idx, 1);
  305. }
  306. }, true);
  307. };
  308. $scope.deleteTicket = function(person) {
  309. EventTicket.delete({
  310. event_id: $state.params.id,
  311. ticket_id: person._id
  312. }, function() {
  313. $scope._localRemoveTicket(person);
  314. });
  315. };
  316. $scope.addTicket = function(person) {
  317. person.event_id = $state.params.id;
  318. EventTicket.add(person, function(ticket) {
  319. $log.debug(ticket);
  320. $scope._localAddTicket(ticket);
  321. if (!$state.is('event.tickets')) {
  322. $state.go('event.ticket.edit', {id: $scope.event._id, ticket_id: ticket._id});
  323. } else {
  324. $scope.query = '';
  325. $scope._setAttended(ticket);
  326. if ($scope.$close) {
  327. $scope.$close();
  328. }
  329. }
  330. });
  331. };
  332. $scope.updateTicket = function(ticket, cb) {
  333. ticket.event_id = $state.params.id;
  334. EventTicket.update(ticket, function(t) {
  335. $scope._localUpdateTicket(t.person);
  336. if (cb) {
  337. cb(t);
  338. }
  339. });
  340. };
  341. $scope.toggleCancelledTicket = function() {
  342. if (!$scope.ticket._id) {
  343. return;
  344. }
  345. $scope.ticket.cancelled = !$scope.ticket.cancelled;
  346. $scope.setPersonAttribute($scope.ticket, 'cancelled', $scope.ticket.cancelled, function() {
  347. $scope.guiOptions.dangerousActionsEnabled = false;
  348. });
  349. };
  350. $scope.openQuickAddTicket = function(_id) {
  351. var modalInstance = $uibModal.open({
  352. templateUrl: 'modal-quick-add-ticket.html',
  353. controller: 'EventTicketsCtrl'
  354. });
  355. modalInstance.result.then(function() {
  356. });
  357. };
  358. $scope.submitForm = function(dataModelSubmitted) {
  359. angular.forEach(dataModelSubmitted, function(value, key) {
  360. key = $scope.formFieldsMap[key] || key;
  361. $scope.ticket[key] = value;
  362. });
  363. if (!$state.params.ticket_id) {
  364. $scope.addTicket($scope.ticket);
  365. } else {
  366. $scope.updateTicket($scope.ticket);
  367. }
  368. };
  369. $scope.cancelForm = function() {
  370. if (!$state.is('event.tickets')) {
  371. $state.go('events');
  372. } else if ($scope.$close) {
  373. $scope.$close();
  374. }
  375. };
  376. $scope.extractFormFields = function(formlyFieldsModel) {
  377. if (!formlyFieldsModel) {
  378. return;
  379. }
  380. angular.forEach(formlyFieldsModel, function(row, idx) {
  381. if (!row.className == 'row') {
  382. return;
  383. }
  384. angular.forEach(row.fieldGroup || [], function(item, idx) {
  385. if (!(item.key && item.templateOptions && item.templateOptions.label)) {
  386. return;
  387. }
  388. var value = item.templateOptions.label.toLowerCase();
  389. $scope.formFieldsMap[item.key] = value;
  390. $scope.formFieldsMapRev[value] = item.key;
  391. });
  392. });
  393. };
  394. $scope.updateOrded = function(key) {
  395. var new_order = [key];
  396. var inv_key;
  397. if (key && key[0] === '-') {
  398. inv_key = key.substring(1);
  399. } else {
  400. inv_key = '-' + key;
  401. }
  402. angular.forEach($scope.personsOrder,
  403. function(value, idx) {
  404. if (value !== key && value !== inv_key) {
  405. new_order.push(value);
  406. }
  407. }
  408. );
  409. $scope.personsOrder = new_order;
  410. };
  411. $scope.showMessage = function(cfg) {
  412. $scope.message.show(cfg);
  413. };
  414. $scope.$on('$destroy', function() {
  415. $scope.EventUpdates && $scope.EventUpdates.close();
  416. });
  417. }]
  418. );
  419. eventManControllers.controller('PersonsListCtrl', ['$scope', 'Person', 'Setting', '$uibModal', '$translate', '$rootScope',
  420. function ($scope, Person, Setting, $uibModal, $translate, $rootScope) {
  421. $scope.persons = Person.all();
  422. $scope.personsOrder = ["name", "surname"];
  423. $scope.customFields = Setting.query({setting: 'person_custom_field',
  424. in_persons_list: true});
  425. $scope.confirm_delete = 'Do you really want to delete this person?';
  426. $rootScope.$on('$translateChangeSuccess', function () {
  427. $translate('Do you really want to delete this person?').then(function (translation) {
  428. $scope.confirm_delete = translation;
  429. });
  430. });
  431. $scope.updateOrded = function(key) {
  432. var new_order = [key];
  433. var inv_key;
  434. if (key && key[0] === '-') {
  435. inv_key = key.substring(1);
  436. } else {
  437. inv_key = '-' + key;
  438. }
  439. angular.forEach($scope.personsOrder,
  440. function(value, idx) {
  441. if (value !== key && value !== inv_key) {
  442. new_order.push(value);
  443. }
  444. }
  445. );
  446. $scope.personsOrder = new_order;
  447. };
  448. $scope.setAttribute = function(person, key, value) {
  449. var data = {_id: person._id};
  450. data[key] = value;
  451. Person.update(data, function() {
  452. $scope.persons = Person.all();
  453. });
  454. };
  455. $scope.remove = function(_id) {
  456. var modalInstance = $uibModal.open({
  457. scope: $scope,
  458. templateUrl: 'modal-confirm-action.html',
  459. controller: 'ModalConfirmInstanceCtrl',
  460. resolve: {
  461. message: function() { return $scope.confirm_delete; }
  462. }
  463. });
  464. modalInstance.result.then(function() {
  465. Person.remove({'id': _id}, function() {
  466. $scope.persons = Person.all();
  467. }
  468. );
  469. });
  470. };
  471. }]
  472. );
  473. eventManControllers.controller('PersonDetailsCtrl', ['$scope', '$state', 'Person', 'Event', 'Setting', '$log',
  474. function ($scope, $state, Person, Event, Setting, $log) {
  475. $scope.personsOrderProp = 'name';
  476. $scope.eventsOrderProp = '-begin_date';
  477. $scope.addToEvent = '';
  478. $scope.customFields = Setting.query({setting: 'person_custom_field',
  479. in_persons_list: true});
  480. if ($state.params.id) {
  481. $scope.person = Person.get($state.params);
  482. $scope.events = Person.getEvents({_id: $state.arams.id, all: true});
  483. } else {
  484. $scope.events = Event.all();
  485. }
  486. // store a new Person or update an existing one
  487. $scope.save = function() {
  488. if ($scope.person._id === undefined) {
  489. $scope.person = new Person($scope.person);
  490. $scope.person.$save(function(person) {
  491. if ($scope.addToEvent) {
  492. var data = angular.copy(person);
  493. data.person_id = data._id;
  494. data._id = $scope.addToEvent;
  495. data.attended = false;
  496. Event.addPerson(data);
  497. }
  498. });
  499. } else {
  500. $scope.person = Person.update($scope.person, function(data) {
  501. if ($scope.addToEvent) {
  502. var data = angular.copy($scope.person);
  503. data._id = $scope.addToEvent;
  504. data.person_id = $scope.person._id;
  505. data.attended = false;
  506. Event.addPerson(data);
  507. }
  508. });
  509. }
  510. $scope.personForm.$setPristine(false);
  511. };
  512. $scope.setPersonAttributeAtEvent = function(evnt, key, value) {
  513. var attrs = {_id: evnt._id, person_id: $state.params.id};
  514. attrs[key] = value;
  515. Event.updatePerson(attrs,
  516. function(data) {
  517. $scope.events = Person.getEvents({_id: $state.params.id, all: true});
  518. }
  519. );
  520. };
  521. $scope.switchRegistered = function(evnt, person, add) {
  522. $log.debug('PersonDetailsCtrl.switchRegistered.event_id: ' + evnt._id);
  523. $log.debug('PersonDetailsCtrl.switchRegistered.person_id: ' + person._id);
  524. $log.debug('PersonDetailsCtrl.switchRegistered.add: ' + add);
  525. if (add) {
  526. var data = angular.copy(person);
  527. data._id = evnt._id;
  528. data.person_id = person._id;
  529. data.attended = false;
  530. Event.addPerson(data,
  531. function(data) {
  532. $scope.events = Person.getEvents({_id: $state.params.id, all: true});
  533. }
  534. );
  535. } else {
  536. Event.deletePerson({_id: evnt._id, person_id: person._id},
  537. function(data) {
  538. $scope.events = Person.getEvents({_id: $state.params.id, all: true});
  539. }
  540. );
  541. }
  542. };
  543. }]
  544. );
  545. eventManControllers.controller('LoginCtrl', ['$scope', '$rootScope', '$state', '$log', 'User',
  546. function ($scope, $rootScope, $state, $log, User) {
  547. $scope.loginData = {};
  548. $scope.register = function() {
  549. User.add($scope.newUser, function(data) {
  550. $scope.login($scope.newUser);
  551. });
  552. };
  553. $scope.login = function(loginData) {
  554. if (!loginData) {
  555. loginData = $scope.loginData;
  556. }
  557. User.login(loginData, function(data) {
  558. if (!data.error) {
  559. $rootScope.readInfo(function() {
  560. $rootScope.clearError();
  561. $state.go('events');
  562. });
  563. }
  564. });
  565. };
  566. $scope.logout = function() {
  567. User.logout({}, function(data) {
  568. if (!data.error) {
  569. $rootScope.readInfo(function() {
  570. $state.go('events');
  571. });
  572. }
  573. });
  574. };
  575. }]
  576. );
  577. eventManControllers.controller('FileUploadCtrl', ['$scope', '$log', '$upload', 'Event',
  578. function ($scope, $log, $upload, Event) {
  579. $scope.file = null;
  580. $scope.reply = {};
  581. $scope.events = Event.all();
  582. $scope.upload = function(file, url) {
  583. $log.debug("FileUploadCtrl.upload");
  584. $upload.upload({
  585. url: url,
  586. file: file,
  587. fields: {targetEvent: $scope.targetEvent}
  588. }).progress(function(evt) {
  589. var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
  590. $log.debug('progress: ' + progressPercentage + '%');
  591. }).success(function(data, status, headers, config) {
  592. $scope.file = null;
  593. $scope.reply = angular.fromJson(data);
  594. });
  595. };
  596. }]
  597. );