controllers.js 19 KB

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