controllers.js 23 KB

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