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', '$modalInstance', 'message',
  44. function ($scope, $modalInstance, message) {
  45. $scope.message = message;
  46. $scope.ok = function () {
  47. $modalInstance.close($scope);
  48. };
  49. $scope.cancel = function () {
  50. $modalInstance.dismiss('cancel');
  51. };
  52. }]
  53. );
  54. eventManControllers.controller('EventsListCtrl', ['$scope', 'Event', '$modal', '$log', '$translate', '$rootScope',
  55. function ($scope, Event, $modal, $log, $translate, $rootScope) {
  56. $scope.events = Event.all();
  57. $scope.personsOrderProp = 'name';
  58. $scope.eventsOrderProp = "-begin_date";
  59. $scope.confirm_delete = 'Do you really want to delete this event?';
  60. $rootScope.$on('$translateChangeSuccess', function () {
  61. $translate('Do you really want to delete this event?').then(function (translation) {
  62. $scope.confirm_delete = translation;
  63. });
  64. });
  65. $scope.remove = function(_id) {
  66. var modalInstance = $modal.open({
  67. scope: $scope,
  68. templateUrl: 'modal-confirm-action.html',
  69. controller: 'ModalConfirmInstanceCtrl',
  70. resolve: {
  71. message: function() { return $scope.confirm_delete; }
  72. }
  73. });
  74. modalInstance.result.then(function() {
  75. Event.remove({'id': _id}, function() {
  76. $scope.events = Event.all();
  77. }
  78. );
  79. });
  80. };
  81. }]
  82. );
  83. eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event', 'Person', 'EventUpdates', '$stateParams', 'Setting', '$log', '$translate', '$rootScope', 'easyFormSteWayConfig',
  84. function ($scope, $state, Event, Person, EventUpdates, $stateParams, Setting, $log, $translate, $rootScope, easyFormSteWayConfig) {
  85. $scope.personsOrder = ["name", "surname"];
  86. $scope.countAttendees = 0;
  87. $scope.message = {};
  88. $scope.event = {};
  89. $scope.event.persons = [];
  90. $scope.event.formSchema = {};
  91. $scope.eventFormDisabled = false;
  92. $scope.customFields = Setting.query({setting: 'person_custom_field', in_event_details: true});
  93. if ($stateParams.id) {
  94. $scope.event = Event.get($stateParams, function() {
  95. $scope.$watchCollection(function() {
  96. return $scope.event.persons;
  97. }, function(prev, old) {
  98. $scope.calcAttendees();
  99. }
  100. );
  101. });
  102. if ($state.is('event.view') || !$rootScope.hasPermission('event|update')) {
  103. $scope.eventFormDisabled = true;
  104. }
  105. if ($state.is('event.tickets')) {
  106. $scope.allPersons = Person.all();
  107. // Handle WebSocket connection used to update the list of persons.
  108. $scope.EventUpdates = EventUpdates;
  109. $scope.EventUpdates.open();
  110. $scope.$watchCollection(function() {
  111. return $scope.EventUpdates.data;
  112. }, function(prev, old) {
  113. if (!($scope.EventUpdates.data && $scope.EventUpdates.data.update)) {
  114. return;
  115. }
  116. var data = $scope.EventUpdates.data.update;
  117. $log.debug('received ' + data.action + ' action from websocket source ' + data.uuid);
  118. if ($rootScope.app_uuid == data.uuid) {
  119. $log.debug('do not process our own message');
  120. return false;
  121. }
  122. if (!$scope.event.persons) {
  123. $scope.event.persons = [];
  124. }
  125. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  126. return data.person_id == el.person_id;
  127. });
  128. if (person_idx != -1) {
  129. $log.debug('person_id ' + data.person_id + ' found');
  130. } else {
  131. $log.debug('person_id ' + data.person_id + ' not found');
  132. }
  133. if (data.action == 'update' && person_idx != -1 && $scope.event.persons[person_idx] != data.person) {
  134. $scope.event.persons[person_idx] = data.person;
  135. } else if (data.action == 'add' && person_idx == -1) {
  136. $scope._localAddAttendee(data.person, true);
  137. } else if (data.action == 'delete' && person_idx != -1) {
  138. $scope._localRemoveAttendee({person_id: data.person_id});
  139. }
  140. }
  141. );
  142. }
  143. }
  144. $scope.updateOrded = function(key) {
  145. var new_order = [key];
  146. var inv_key;
  147. if (key && key[0] === '-') {
  148. inv_key = key.substring(1);
  149. } else {
  150. inv_key = '-' + key;
  151. }
  152. angular.forEach($scope.personsOrder,
  153. function(value, idx) {
  154. if (value !== key && value !== inv_key) {
  155. new_order.push(value);
  156. }
  157. }
  158. );
  159. $scope.personsOrder = new_order;
  160. };
  161. // store a new Event or update an existing one
  162. $scope.save = function() {
  163. // avoid override of event.persons list.
  164. var this_event = angular.copy($scope.event);
  165. if (this_event.persons) {
  166. delete this_event.persons;
  167. }
  168. if (this_event._id === undefined) {
  169. $scope.event = Event.save(this_event);
  170. } else {
  171. $scope.event = Event.update(this_event);
  172. }
  173. $scope.eventForm.$setPristine(false);
  174. };
  175. $scope.calcAttendees = function() {
  176. if (!($scope.event && $scope.event.persons)) {
  177. $scope.countAttendees = 0;
  178. return;
  179. }
  180. var attendees = 0;
  181. angular.forEach($scope.event.persons, function(value, key) {
  182. if (value.attended && !value.cancelled) {
  183. attendees += 1;
  184. }
  185. });
  186. $scope.countAttendees = attendees;
  187. };
  188. /* Stuff to do when an attendee is added locally. */
  189. $scope._localAddAttendee = function(person, hideMessage) {
  190. if (!$scope.event.persons) {
  191. $scope.event.persons = [];
  192. }
  193. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  194. return person.person_id == el.person_id;
  195. });
  196. if (person_idx != -1) {
  197. $log.debug('person already present: not added');
  198. return false;
  199. }
  200. $scope.event.persons.push(person);
  201. };
  202. $scope._addAttendee = function(person) {
  203. person.person_id = person._id;
  204. person._id = $stateParams.id; // that's the id of the event, not the person.
  205. Event.addPerson(person, function() {
  206. $scope._localAddAttendee(person);
  207. });
  208. $scope.query = '';
  209. return person;
  210. };
  211. $scope._setAttended = function(person) {
  212. $scope.setPersonAttribute(person, 'attended', true, function() {
  213. var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
  214. return person.person_id == el.person_id;
  215. });
  216. if (all_person_idx != -1) {
  217. $scope.allPersons.splice(all_person_idx, 1);
  218. }
  219. }, true);
  220. };
  221. $scope.fastAddAttendee = function(person, isNew) {
  222. $log.debug('EventDetailsCtrl.fastAddAttendee.person:');
  223. $log.debug(person);
  224. if (isNew) {
  225. var personObj = new Person(person);
  226. personObj.$save(function(p) {
  227. person = $scope._addAttendee(angular.copy(p));
  228. $scope._setAttended(person);
  229. $scope.newPerson = {};
  230. });
  231. } else {
  232. person = $scope._addAttendee(angular.copy(person));
  233. $scope._setAttended(person);
  234. }
  235. };
  236. $scope.addRegisteredPerson = function(person) {
  237. $scope.fastAddAttendee(person, true);
  238. };
  239. $scope.setPersonAttribute = function(person, key, value, callback, hideMessage) {
  240. $log.debug('EventDetailsCtrl.setPersonAttribute.event_id: ' + $stateParams.id);
  241. $log.debug('EventDetailsCtrl.setPersonAttribute.person_id: ' + person.person_id);
  242. $log.debug('EventDetailsCtrl.setPersonAttribute.key: ' + key + ' value: ' + value);
  243. var data = {_id: $stateParams.id, person_id: person.person_id};
  244. data[key] = value;
  245. Event.updatePerson(data,
  246. function(data) {
  247. if (!(data && data.person_id && data.person)) {
  248. return;
  249. }
  250. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  251. return data.person_id == el.person_id;
  252. });
  253. if (person_idx == -1) {
  254. $log.warn('unable to find person_id ' + person_id);
  255. return;
  256. }
  257. if ($scope.event.persons[person_idx] != data.person) {
  258. $scope.event.persons[person_idx] = data.person;
  259. }
  260. if (callback) {
  261. callback(data);
  262. }
  263. if (key === 'attended' && !hideMessage) {
  264. var msg = {};
  265. if (value) {
  266. msg.message = '' + person.name + ' ' + person.surname + ' successfully added to event ' + $scope.event.title;
  267. } else {
  268. msg.message = '' + person.name + ' ' + person.surname + ' successfully removed from event ' + $scope.event.title;
  269. msg.isError = true;
  270. }
  271. $scope.showMessage(msg);
  272. }
  273. });
  274. };
  275. $scope.setPersonAttributeAndRefocus = function(person, key, value) {
  276. $scope.setPersonAttribute(person, key, value);
  277. $scope.query = '';
  278. };
  279. /* Stuff to do when an attendee is removed locally. */
  280. $scope._localRemoveAttendee = function(person) {
  281. $log.debug('_localRemoveAttendee');
  282. $log.debug(person);
  283. if (!(person && person.person_id && $scope.event.persons)) {
  284. return;
  285. }
  286. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  287. return person.person_id == el.person_id;
  288. });
  289. if (person_idx == -1) {
  290. $log.warn('unable to find and delete person_id ' + person.person_id);
  291. return;
  292. }
  293. var removed_person = $scope.event.persons.splice(person_idx, 1);
  294. // to be used to populate allPersons, if needed.
  295. if (removed_person.length) {
  296. person = removed_person[0];
  297. }
  298. var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
  299. return person.person_id == el._id;
  300. });
  301. if (all_person_idx == -1 && person.person_id) {
  302. $scope.allPersons.push(person);
  303. }
  304. };
  305. $scope.removeAttendee = function(person) {
  306. Event.deletePerson({
  307. _id: $stateParams.id,
  308. person_id: person.person_id
  309. }, function() {
  310. $scope._localRemoveAttendee(person);
  311. });
  312. };
  313. $scope.saveForm = function(easyFormGeneratorModel) {
  314. $scope.event.formSchema = easyFormGeneratorModel;
  315. $scope.save();
  316. };
  317. $scope.showMessage = function(cfg) {
  318. $scope.message.show(cfg);
  319. };
  320. $scope.$on('$destroy', function() {
  321. $scope.EventUpdates && $scope.EventUpdates.close();
  322. });
  323. }]
  324. );
  325. eventManControllers.controller('EventTicketsCtrl', ['$scope', '$state', 'Event', 'EventTicket', 'Person', 'Setting', '$log', '$translate', '$rootScope',
  326. function ($scope, $state, Event, EventTicket, Person, Setting, $log, $translate, $rootScope) {
  327. $scope.message = {};
  328. $scope.event = {};
  329. $scope.ticket = {};
  330. $scope.formSchema = {};
  331. $scope.formData = {};
  332. $scope.dangerousActionsEnabled = false;
  333. $scope.formFieldsMap = {};
  334. $scope.formFieldsMapRev = {};
  335. if ($state.params.id) {
  336. $scope.event = Event.get({id: $state.params.id}, function(data) {
  337. if (!(data && data.formSchema)) {
  338. return;
  339. }
  340. $scope.formSchema = data.formSchema.edaFieldsModel;
  341. $scope.extractFormFields(data.formSchema.formlyFieldsModel);
  342. if ($state.params.ticket_id) {
  343. EventTicket.get({id: $state.params.id, ticket_id: $state.params.ticket_id}, function(data) {
  344. $scope.ticket = data;
  345. angular.forEach(data, function(value, key) {
  346. if (!$scope.formFieldsMapRev[key]) {
  347. return;
  348. }
  349. $scope.formData[$scope.formFieldsMapRev[key]] = value;
  350. });
  351. });
  352. }
  353. });
  354. }
  355. $scope.extractFormFields = function(formlyFieldsModel) {
  356. if (!formlyFieldsModel) {
  357. return;
  358. }
  359. angular.forEach(formlyFieldsModel, function(row, idx) {
  360. if (!row.className == 'row') {
  361. return;
  362. }
  363. angular.forEach(row.fieldGroup || [], function(item, idx) {
  364. if (!(item.key && item.templateOptions && item.templateOptions.label)) {
  365. return;
  366. }
  367. var value = item.templateOptions.label.toLowerCase();
  368. $scope.formFieldsMap[item.key] = value;
  369. $scope.formFieldsMapRev[value] = item.key;
  370. });
  371. });
  372. };
  373. $scope.addTicket = function(person) {
  374. var personObj = new Person(person);
  375. personObj.$save(function(p) {
  376. person.person_id = p._id;
  377. person._id = $state.params.id; // that's the id of the event, not the person.
  378. EventTicket.add(person, function(ticket) {
  379. $log.debug(ticket);
  380. $state.go('event.ticket.edit', {id: $scope.event._id, ticket_id: ticket._id});
  381. });
  382. });
  383. };
  384. $scope.updateTicket = function(ticket, cb) {
  385. var data = angular.copy(ticket);
  386. data.ticket_id = data._id;
  387. data._id = $state.params.id;
  388. EventTicket.update(data, function(t) {
  389. if (cb) {
  390. cb(t);
  391. }
  392. });
  393. };
  394. $scope.submitForm = function(dataModelSubmitted) {
  395. angular.forEach(dataModelSubmitted, function(value, key) {
  396. key = $scope.formFieldsMap[key] || key;
  397. $scope.ticket[key] = value;
  398. });
  399. if (!$state.params.ticket_id) {
  400. $scope.addTicket($scope.ticket);
  401. } else {
  402. $scope.updateTicket($scope.ticket);
  403. }
  404. };
  405. $scope.toggleTicket = function() {
  406. if (!$scope.ticket._id) {
  407. return;
  408. }
  409. $scope.ticket.cancelled = !$scope.ticket.cancelled;
  410. $scope.updateTicket($scope.ticket, function() {
  411. $scope.dangerousActionsEnabled = false;
  412. });
  413. };
  414. $scope.cancelForm = function() {
  415. $state.go('events');
  416. };
  417. }]
  418. );
  419. eventManControllers.controller('PersonsListCtrl', ['$scope', 'Person', 'Setting', '$modal', '$translate', '$rootScope',
  420. function ($scope, Person, Setting, $modal, $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 = $modal.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', '$stateParams', 'Person', 'Event', 'Setting', '$log',
  474. function ($scope, $stateParams, 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 ($stateParams.id) {
  481. $scope.person = Person.get($stateParams);
  482. $scope.events = Person.getEvents({_id: $stateParams.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: $stateParams.id};
  514. attrs[key] = value;
  515. Event.updatePerson(attrs,
  516. function(data) {
  517. $scope.events = Person.getEvents({_id: $stateParams.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: $stateParams.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: $stateParams.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. );