controllers.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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.go = function(url) {
  11. $location.url(url);
  12. };
  13. Setting.query({setting: 'logo'}, function(data) {
  14. if (data && data.length) {
  15. $scope.logo = data[0];
  16. }
  17. });
  18. $scope.isActive = function(view) {
  19. if (view === $location.path()) {
  20. return true;
  21. }
  22. if (view[view.length-1] !== '/') {
  23. view = view + '/';
  24. }
  25. return $location.path().indexOf(view) == 0;
  26. };
  27. }]
  28. );
  29. /* Controller for a group of date and time pickers. */
  30. eventManControllers.controller('DatetimePickerCtrl', ['$scope',
  31. function ($scope) {
  32. $scope.open = function($event) {
  33. $event.preventDefault();
  34. $event.stopPropagation();
  35. $scope.opened = true;
  36. };
  37. }]
  38. );
  39. /* Controller for modals. */
  40. eventManControllers.controller('ModalConfirmInstanceCtrl', ['$scope', '$modalInstance', 'message',
  41. function ($scope, $modalInstance, message) {
  42. $scope.message = message;
  43. $scope.ok = function () {
  44. $modalInstance.close($scope);
  45. };
  46. $scope.cancel = function () {
  47. $modalInstance.dismiss('cancel');
  48. };
  49. }]
  50. );
  51. eventManControllers.controller('EventsListCtrl', ['$scope', 'Event', '$modal', '$log', '$translate', '$rootScope',
  52. function ($scope, Event, $modal, $log, $translate, $rootScope) {
  53. $scope.events = Event.all();
  54. $scope.personsOrderProp = 'name';
  55. $scope.eventsOrderProp = "-begin_date";
  56. $scope.confirm_delete = 'Do you really want to delete this event?';
  57. $rootScope.$on('$translateChangeSuccess', function () {
  58. $translate('Do you really want to delete this event?').then(function (translation) {
  59. $scope.confirm_delete = translation;
  60. });
  61. });
  62. $scope.remove = function(_id) {
  63. var modalInstance = $modal.open({
  64. scope: $scope,
  65. templateUrl: 'modal-confirm-action.html',
  66. controller: 'ModalConfirmInstanceCtrl',
  67. resolve: {
  68. message: function() { return $scope.confirm_delete; }
  69. }
  70. });
  71. modalInstance.result.then(function() {
  72. Event.remove({'id': _id}, function() {
  73. $scope.events = Event.all();
  74. }
  75. );
  76. });
  77. };
  78. }]
  79. );
  80. eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event', 'EventTicket', 'Person', 'EventUpdates', '$stateParams', 'Setting', '$log', '$translate', '$rootScope',
  81. function ($scope, $state, Event, EventTicket, Person, EventUpdates, $stateParams, Setting, $log, $translate, $rootScope) {
  82. $scope.personsOrder = ["name", "surname"];
  83. $scope.countAttendees = 0;
  84. $scope.message = {};
  85. $scope.event = {};
  86. $scope.event.persons = [];
  87. $scope.customFields = Setting.query({setting: 'person_custom_field', in_event_details: true});
  88. $scope.newTicket = $state.is('event.ticket.new');
  89. if ($stateParams.id) {
  90. $scope.event = Event.get($stateParams, function() {
  91. if ($scope.newTicket) {
  92. return;
  93. }
  94. $scope.$watchCollection(function() {
  95. return $scope.event.persons;
  96. }, function(prev, old) {
  97. $scope.calcAttendees();
  98. }
  99. );
  100. });
  101. if ($state.is('event.info')) {
  102. $scope.allPersons = Person.all();
  103. // Handle WebSocket connection used to update the list of persons.
  104. $scope.EventUpdates = EventUpdates;
  105. $scope.EventUpdates.open();
  106. $scope.$watchCollection(function() {
  107. return $scope.EventUpdates.data;
  108. }, function(prev, old) {
  109. if (!($scope.EventUpdates.data && $scope.EventUpdates.data.update)) {
  110. return;
  111. }
  112. var data = $scope.EventUpdates.data.update;
  113. $log.debug('received ' + data.action + ' action from websocket source ' + data.uuid);
  114. if ($rootScope.app_uuid == data.uuid) {
  115. $log.debug('do not process our own message');
  116. return false;
  117. }
  118. if (!$scope.event.persons) {
  119. $scope.event.persons = [];
  120. }
  121. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  122. return data.person_id == el.person_id;
  123. });
  124. if (person_idx != -1) {
  125. $log.debug('person_id ' + data.person_id + ' found');
  126. } else {
  127. $log.debug('person_id ' + data.person_id + ' not found');
  128. }
  129. if (data.action == 'update' && person_idx != -1 && $scope.event.persons[person_idx] != data.person) {
  130. $scope.event.persons[person_idx] = data.person;
  131. } else if (data.action == 'add' && person_idx == -1) {
  132. $scope._localAddAttendee(data.person, true);
  133. } else if (data.action == 'delete' && person_idx != -1) {
  134. $scope._localRemoveAttendee({person_id: data.person_id});
  135. }
  136. }
  137. );
  138. }
  139. }
  140. $scope.updateOrded = function(key) {
  141. var new_order = [key];
  142. var inv_key;
  143. if (key && key[0] === '-') {
  144. inv_key = key.substring(1);
  145. } else {
  146. inv_key = '-' + key;
  147. }
  148. angular.forEach($scope.personsOrder,
  149. function(value, idx) {
  150. if (value !== key && value !== inv_key) {
  151. new_order.push(value);
  152. }
  153. }
  154. );
  155. $scope.personsOrder = new_order;
  156. };
  157. // store a new Event or update an existing one
  158. $scope.save = function() {
  159. // avoid override of event.persons list.
  160. var this_event = angular.copy($scope.event);
  161. if (this_event.persons) {
  162. delete this_event.persons;
  163. }
  164. if (this_event._id === undefined) {
  165. $scope.event = Event.save(this_event);
  166. } else {
  167. $scope.event = Event.update(this_event);
  168. }
  169. $scope.eventForm.$setPristine(false);
  170. };
  171. $scope.calcAttendees = function() {
  172. if (!($scope.event && $scope.event.persons)) {
  173. $scope.countAttendees = 0;
  174. return;
  175. }
  176. var attendees = 0;
  177. angular.forEach($scope.event.persons, function(value, key) {
  178. if (value.attended) {
  179. attendees += 1;
  180. }
  181. });
  182. $scope.countAttendees = attendees;
  183. };
  184. /* Stuff to do when an attendee is added locally. */
  185. $scope._localAddAttendee = function(person, hideMessage) {
  186. if (!$scope.event.persons) {
  187. $scope.event.persons = [];
  188. }
  189. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  190. return person.person_id == el.person_id;
  191. });
  192. if (person_idx != -1) {
  193. $log.debug('person already present: not added');
  194. return false;
  195. }
  196. $scope.event.persons.push(person);
  197. };
  198. $scope._addAttendee = function(person) {
  199. person.person_id = person._id;
  200. person._id = $stateParams.id; // that's the id of the event, not the person.
  201. Event.addPerson(person, function() {
  202. if (!$scope.newTicket) {
  203. $scope._localAddAttendee(person);
  204. }
  205. });
  206. $scope.query = '';
  207. return person;
  208. };
  209. $scope._setAttended = function(person) {
  210. $scope.setPersonAttribute(person, 'attended', true, function() {
  211. var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
  212. return person.person_id == el.person_id;
  213. });
  214. if (all_person_idx != -1) {
  215. $scope.allPersons.splice(all_person_idx, 1);
  216. }
  217. }, true);
  218. };
  219. $scope.fastAddAttendee = function(person, isNew) {
  220. $log.debug('EventDetailsCtrl.fastAddAttendee.person:');
  221. $log.debug(person);
  222. if (isNew) {
  223. var personObj = new Person(person);
  224. personObj.$save(function(p) {
  225. person = $scope._addAttendee(angular.copy(p));
  226. if (!$scope.newTicket) {
  227. $scope._setAttended(person);
  228. }
  229. $scope.newPerson = {};
  230. });
  231. } else {
  232. person = $scope._addAttendee(angular.copy(person));
  233. if (!$scope.newTicket) {
  234. $scope._setAttended(person);
  235. }
  236. }
  237. };
  238. $scope.addRegisteredPerson = function(person) {
  239. $scope.fastAddAttendee(person, true);
  240. };
  241. $scope.setPersonAttribute = function(person, key, value, callback, hideMessage) {
  242. $log.debug('EventDetailsCtrl.setPersonAttribute.event_id: ' + $stateParams.id);
  243. $log.debug('EventDetailsCtrl.setPersonAttribute.person_id: ' + person.person_id);
  244. $log.debug('EventDetailsCtrl.setPersonAttribute.key: ' + key + ' value: ' + value);
  245. var data = {_id: $stateParams.id, person_id: person.person_id};
  246. data[key] = value;
  247. Event.updatePerson(data,
  248. function(data) {
  249. if (!(data && data.person_id && data.person)) {
  250. return;
  251. }
  252. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  253. return data.person_id == el.person_id;
  254. });
  255. if (person_idx == -1) {
  256. $log.warn('unable to find person_id ' + person_id);
  257. return;
  258. }
  259. if ($scope.event.persons[person_idx] != data.person) {
  260. $scope.event.persons[person_idx] = data.person;
  261. }
  262. if (callback) {
  263. callback(data);
  264. }
  265. if (key === 'attended' && !hideMessage) {
  266. var msg = {};
  267. if (value) {
  268. msg.message = '' + person.name + ' ' + person.surname + ' successfully added to event ' + $scope.event.title;
  269. } else {
  270. msg.message = '' + person.name + ' ' + person.surname + ' successfully removed from event ' + $scope.event.title;
  271. msg.isError = true;
  272. }
  273. $scope.showMessage(msg);
  274. }
  275. });
  276. };
  277. $scope.setPersonAttributeAndRefocus = function(person, key, value) {
  278. $scope.setPersonAttribute(person, key, value);
  279. $scope.query = '';
  280. };
  281. /* Stuff to do when an attendee is removed locally. */
  282. $scope._localRemoveAttendee = function(person) {
  283. $log.debug('_localRemoveAttendee');
  284. $log.debug(person);
  285. if (!(person && person.person_id && $scope.event.persons)) {
  286. return;
  287. }
  288. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  289. return person.person_id == el.person_id;
  290. });
  291. if (person_idx == -1) {
  292. $log.warn('unable to find and delete person_id ' + person.person_id);
  293. return;
  294. }
  295. var removed_person = $scope.event.persons.splice(person_idx, 1);
  296. // to be used to populate allPersons, if needed.
  297. if (removed_person.length) {
  298. person = removed_person[0];
  299. }
  300. var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
  301. return person.person_id == el._id;
  302. });
  303. if (all_person_idx == -1 && person.person_id) {
  304. $scope.allPersons.push(person);
  305. }
  306. };
  307. $scope.removeAttendee = function(person) {
  308. Event.deletePerson({
  309. _id: $stateParams.id,
  310. person_id: person.person_id
  311. }, function() {
  312. $scope._localRemoveAttendee(person);
  313. });
  314. };
  315. $scope.showMessage = function(cfg) {
  316. $scope.message.show(cfg);
  317. };
  318. $scope.$on('$destroy', function() {
  319. $scope.EventUpdates && $scope.EventUpdates.close();
  320. });
  321. }]
  322. );
  323. eventManControllers.controller('EventTicketsCtrl', ['$scope', '$state', 'Event', 'EventTicket', 'Person', 'EventUpdates', '$stateParams', 'Setting', '$log', '$translate', '$rootScope',
  324. function ($scope, $state, Event, EventTicket, Person, EventUpdates, $stateParams, Setting, $log, $translate, $rootScope) {
  325. $scope.message = {};
  326. $scope.event = {};
  327. $scope.ticket = {};
  328. $scope.newTicket = $state.is('event.ticket.new');
  329. if ($state.params.id) {
  330. $scope.event = Event.get({id: $state.params.id}, function() {
  331. });
  332. }
  333. if ($state.params.ticket_id) {
  334. EventTicket.get({id: $state.params.id, ticket_id: $state.params.ticket_id}, function(data) {
  335. $scope.ticket = data;
  336. });
  337. }
  338. $scope.addTicket = function(person) {
  339. var personObj = new Person(person);
  340. personObj.$save(function(p) {
  341. person.person_id = p._id;
  342. person._id = $state.params.id; // that's the id of the event, not the person.
  343. EventTicket.add(person, function(ticket) {
  344. $log.debug(ticket);
  345. $state.go('event.ticket.edit', {ticket_id: ticket._id});
  346. });
  347. });
  348. };
  349. $scope.updateTicket = function(ticket) {
  350. var data = angular.copy(ticket);
  351. data.ticket_id = data._id;
  352. data._id = $state.params.id;
  353. EventTicket.update(data, function(t) {
  354. });
  355. };
  356. }]
  357. );
  358. eventManControllers.controller('PersonsListCtrl', ['$scope', 'Person', 'Setting', '$modal', '$translate', '$rootScope',
  359. function ($scope, Person, Setting, $modal, $translate, $rootScope) {
  360. $scope.persons = Person.all();
  361. $scope.personsOrder = ["name", "surname"];
  362. $scope.customFields = Setting.query({setting: 'person_custom_field',
  363. in_persons_list: true});
  364. $scope.confirm_delete = 'Do you really want to delete this person?';
  365. $rootScope.$on('$translateChangeSuccess', function () {
  366. $translate('Do you really want to delete this person?').then(function (translation) {
  367. $scope.confirm_delete = translation;
  368. });
  369. });
  370. $scope.updateOrded = function(key) {
  371. var new_order = [key];
  372. var inv_key;
  373. if (key && key[0] === '-') {
  374. inv_key = key.substring(1);
  375. } else {
  376. inv_key = '-' + key;
  377. }
  378. angular.forEach($scope.personsOrder,
  379. function(value, idx) {
  380. if (value !== key && value !== inv_key) {
  381. new_order.push(value);
  382. }
  383. }
  384. );
  385. $scope.personsOrder = new_order;
  386. };
  387. $scope.setAttribute = function(person, key, value) {
  388. var data = {_id: person._id};
  389. data[key] = value;
  390. Person.update(data, function() {
  391. $scope.persons = Person.all();
  392. });
  393. };
  394. $scope.remove = function(_id) {
  395. var modalInstance = $modal.open({
  396. scope: $scope,
  397. templateUrl: 'modal-confirm-action.html',
  398. controller: 'ModalConfirmInstanceCtrl',
  399. resolve: {
  400. message: function() { return $scope.confirm_delete; }
  401. }
  402. });
  403. modalInstance.result.then(function() {
  404. Person.remove({'id': _id}, function() {
  405. $scope.persons = Person.all();
  406. }
  407. );
  408. });
  409. };
  410. }]
  411. );
  412. eventManControllers.controller('PersonDetailsCtrl', ['$scope', '$stateParams', 'Person', 'Event', 'Setting', '$log',
  413. function ($scope, $stateParams, Person, Event, Setting, $log) {
  414. $scope.personsOrderProp = 'name';
  415. $scope.eventsOrderProp = '-begin_date';
  416. $scope.addToEvent = '';
  417. $scope.customFields = Setting.query({setting: 'person_custom_field',
  418. in_persons_list: true});
  419. if ($stateParams.id) {
  420. $scope.person = Person.get($stateParams);
  421. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  422. } else {
  423. $scope.events = Event.all();
  424. }
  425. // store a new Person or update an existing one
  426. $scope.save = function() {
  427. if ($scope.person._id === undefined) {
  428. $scope.person = new Person($scope.person);
  429. $scope.person.$save(function(person) {
  430. if ($scope.addToEvent) {
  431. var data = angular.copy(person);
  432. data.person_id = data._id;
  433. data._id = $scope.addToEvent;
  434. data.attended = false;
  435. Event.addPerson(data);
  436. }
  437. });
  438. } else {
  439. $scope.person = Person.update($scope.person, function(data) {
  440. if ($scope.addToEvent) {
  441. var data = angular.copy($scope.person);
  442. data._id = $scope.addToEvent;
  443. data.person_id = $scope.person._id;
  444. data.attended = false;
  445. Event.addPerson(data);
  446. }
  447. });
  448. }
  449. $scope.personForm.$setPristine(false);
  450. };
  451. $scope.setPersonAttributeAtEvent = function(evnt, key, value) {
  452. var attrs = {_id: evnt._id, person_id: $stateParams.id};
  453. attrs[key] = value;
  454. Event.updatePerson(attrs,
  455. function(data) {
  456. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  457. }
  458. );
  459. };
  460. $scope.switchRegistered = function(evnt, person, add) {
  461. $log.debug('PersonDetailsCtrl.switchRegistered.event_id: ' + evnt._id);
  462. $log.debug('PersonDetailsCtrl.switchRegistered.person_id: ' + person._id);
  463. $log.debug('PersonDetailsCtrl.switchRegistered.add: ' + add);
  464. if (add) {
  465. var data = angular.copy(person);
  466. data._id = evnt._id;
  467. data.person_id = person._id;
  468. data.attended = false;
  469. Event.addPerson(data,
  470. function(data) {
  471. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  472. }
  473. );
  474. } else {
  475. Event.deletePerson({_id: evnt._id, person_id: person._id},
  476. function(data) {
  477. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  478. }
  479. );
  480. }
  481. };
  482. }]
  483. );
  484. eventManControllers.controller('LoginCtrl', ['$scope', '$rootScope', '$state', '$log', 'User',
  485. function ($scope, $rootScope, $state, $log, User) {
  486. $scope.loginData = {};
  487. $scope.register = function() {
  488. User.add($scope.newUser, function(data) {
  489. $scope.login($scope.newUser);
  490. });
  491. };
  492. $scope.login = function(loginData) {
  493. if (!loginData) {
  494. loginData = $scope.loginData;
  495. }
  496. User.login(loginData, function(data) {
  497. if (!data.error) {
  498. $rootScope.readInfo(function() {
  499. $rootScope.clearError();
  500. $state.go('events');
  501. });
  502. }
  503. });
  504. };
  505. $scope.logout = function() {
  506. User.logout({}, function(data) {
  507. if (!data.error) {
  508. $rootScope.readInfo(function() {
  509. $state.go('events');
  510. });
  511. }
  512. });
  513. };
  514. }]
  515. );
  516. eventManControllers.controller('FileUploadCtrl', ['$scope', '$log', '$upload', 'Event',
  517. function ($scope, $log, $upload, Event) {
  518. $scope.file = null;
  519. $scope.reply = {};
  520. $scope.events = Event.all();
  521. $scope.upload = function(file, url) {
  522. $log.debug("FileUploadCtrl.upload");
  523. $upload.upload({
  524. url: url,
  525. file: file,
  526. fields: {targetEvent: $scope.targetEvent}
  527. }).progress(function(evt) {
  528. var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
  529. $log.debug('progress: ' + progressPercentage + '%');
  530. }).success(function(data, status, headers, config) {
  531. $scope.file = null;
  532. $scope.reply = angular.fromJson(data);
  533. });
  534. };
  535. }]
  536. );